This repository has been archived by the owner on Jul 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
text.ts
116 lines (99 loc) · 2.94 KB
/
text.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import Promise from '@dojo/shim/Promise';
import has from './has';
import request from './request';
import { NodeRequire, AmdRequire, AmdConfig } from './interfaces';
import { Require, isAmdRequire } from './load';
declare const require: Require;
/*
* Strips <?xml ...?> declarations so that external SVG and XML
* documents can be added to a document without worry. Also, if the string
* is an HTML document, only the part inside the body tag is returned.
*/
function strip(text: string): string {
if (!text) {
return '';
}
text = text.replace(/^\s*<\?xml(\s)+version=[\'\"](\d)*.(\d)*[\'\"](\s)*\?>/im, '');
let matches = text.match(/<body[^>]*>\s*([\s\S]+)\s*<\/body>/im);
text = matches ? matches[1] : text;
return text;
}
/*
* Host-specific method to retrieve text
*/
let getText: (url: string, callback: (value: string | null) => void) => void;
if (has('host-browser')) {
getText = function(url: string, callback: (value: string | null) => void): void {
request(url).then((response) => {
response.text().then((data) => {
callback(data);
});
});
};
} else if (has('host-node')) {
let fs = isAmdRequire(require) && require.nodeRequire ? require.nodeRequire('fs') : (<NodeRequire>require)('fs');
getText = function(url: string, callback: (value: string) => void): void {
fs.readFile(url, { encoding: 'utf8' }, function(error: Error, data: string): void {
if (error) {
throw error;
}
callback(data);
});
};
} else {
getText = function(): void {
throw new Error('dojo/text not supported on this platform');
};
}
/*
* Cache of previously-loaded text resources
*/
let textCache: { [key: string]: any } = {};
/*
* Cache of pending text resources
*/
let pending: { [key: string]: any } = {};
export function get(url: string): Promise<string | null> {
let promise = new Promise<string | null>(function(resolve, reject) {
getText(url, function(text) {
resolve(text);
});
});
return promise;
}
export function normalize(id: string, toAbsMid: (moduleId: string) => string): string {
let parts = id.split('!');
let url = parts[0];
return (/^\./.test(url) ? toAbsMid(url) : url) + (parts[1] ? '!' + parts[1] : '');
}
export function load(id: string, require: AmdRequire, load: (value?: any) => void, config?: AmdConfig): void {
let parts = id.split('!');
let stripFlag = parts.length > 1;
let mid = parts[0];
let url = require.toUrl(mid);
let text: string | undefined;
function finish(text: string): void {
load(stripFlag ? strip(text) : text);
}
if (mid in textCache) {
text = textCache[mid];
} else if (url in textCache) {
text = textCache[url];
}
if (!text) {
if (pending[url]) {
pending[url].push(finish);
} else {
let pendingList = (pending[url] = [finish]);
getText(url, function(value) {
textCache[mid] = textCache[url] = value;
for (let i = 0; i < pendingList.length; ) {
pendingList[i++](value || '');
}
delete pending[url];
});
}
} else {
finish(text);
}
}