Debian Codegraph @

tests/plugins/relative/text.js

raw symbols SCIP
1 if (typeof define !== 'function') { var define = (require('../../../amdefine'))(module); }
2
3 /**
4 * @license RequireJS text 1.0.2 Copyright (c) 2010-2011, The Dojo Foundation All Rights Reserved.
5 * Available via the MIT or new BSD license.
6 * see: http://github.com/jrburke/requirejs for details
7 */
8 /*jslint regexp: false, nomen: false, plusplus: false, strict: false */
9 /*global require: false, XMLHttpRequest: false, ActiveXObject: false,
10 define: false, window: false, process: false, Packages: false,
11 java: false, location: false */
12
13 (function () {
14 var progIds = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'],
15 xmlRegExp = /^\s*<\?xml(\s)+version=[\'\"](\d)*.(\d)*[\'\"](\s)*\?>/im,
16 bodyRegExp = /<body[^>]*>\s*([\s\S]+)\s*<\/body>/im,
17 hasLocation = typeof location !== 'undefined' && location.href,
18 defaultProtocol = hasLocation && location.protocol && location.protocol.replace(/\:/, ''),
19 defaultHostName = hasLocation && location.hostname,
20 defaultPort = hasLocation && (location.port || undefined),
21 buildMap = [];
22
23 define(function () {
24 var text, get, fs;
25
26 if (typeof window !== "undefined" && window.navigator && window.document) {
27 get = function (url, callback) {
28 var xhr = text.createXhr();
29 xhr.open('GET', url, true);
30 xhr.onreadystatechange = function (evt) {
31 //Do not explicitly handle errors, those should be
32 //visible via console output in the browser.
33 if (xhr.readyState === 4) {
34 callback(xhr.responseText);
35 }
36 };
37 xhr.send(null);
38 };
39 } else if (typeof process !== "undefined" &&
40 process.versions &&
41 !!process.versions.node) {
42 //Using special require.nodeRequire, something added by r.js.
43 fs = (require.nodeRequire || require)('fs');
44
45 get = function (url, callback) {
46 callback(fs.readFileSync(url, 'utf8'));
47 };
48 } else if (typeof Packages !== 'undefined') {
49 //Why Java, why is this so awkward?
50 get = function (url, callback) {
51 var encoding = "utf-8",
52 file = new java.io.File(url),
53 lineSeparator = java.lang.System.getProperty("line.separator"),
54 input = new java.io.BufferedReader(new java.io.InputStreamReader(new java.io.FileInputStream(file), encoding)),
55 stringBuffer, line,
56 content = '';
57 try {
58 stringBuffer = new java.lang.StringBuffer();
59 line = input.readLine();
60
61 // Byte Order Mark (BOM) - The Unicode Standard, version 3.0, page 324
62 // http://www.unicode.org/faq/utf_bom.html
63
64 // Note that when we use utf-8, the BOM should appear as "EF BB BF", but it doesn't due to this bug in the JDK:
65 // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4508058
66 if (line && line.length() && line.charAt(0) === 0xfeff) {
67 // Eat the BOM, since we've already found the encoding on this file,
68 // and we plan to concatenating this buffer with others; the BOM should
69 // only appear at the top of a file.
70 line = line.substring(1);
71 }
72
73 stringBuffer.append(line);
74
75 while ((line = input.readLine()) !== null) {
76 stringBuffer.append(lineSeparator);
77 stringBuffer.append(line);
78 }
79 //Make sure we return a JavaScript string and not a Java string.
80 content = String(stringBuffer.toString()); //String
81 } finally {
82 input.close();
83 }
84 callback(content);
85 };
86 }
87
88 text = {
89 version: '1.0.2',
90
91 strip: function (content) {
92 //Strips <?xml ...?> declarations so that external SVG and XML
93 //documents can be added to a document without worry. Also, if the string
94 //is an HTML document, only the part inside the body tag is returned.
95 if (content) {
96 content = content.replace(xmlRegExp, "");
97 var matches = content.match(bodyRegExp);
98 if (matches) {
99 content = matches[1];
100 }
101 } else {
102 content = "";
103 }
104 return content;
105 },
106
107 jsEscape: function (content) {
108 return content.replace(/(['\\])/g, '\\$1')
109 .replace(/[\f]/g, "\\f")
110 .replace(/[\b]/g, "\\b")
111 .replace(/[\n]/g, "\\n")
112 .replace(/[\t]/g, "\\t")
113 .replace(/[\r]/g, "\\r");
114 },
115
116 createXhr: function () {
117 //Would love to dump the ActiveX crap in here. Need IE 6 to die first.
118 var xhr, i, progId;
119 if (typeof XMLHttpRequest !== "undefined") {
120 return new XMLHttpRequest();
121 } else {
122 for (i = 0; i < 3; i++) {
123 progId = progIds[i];
124 try {
125 xhr = new ActiveXObject(progId);
126 } catch (e) {}
127
128 if (xhr) {
129 progIds = [progId]; // so faster next time
130 break;
131 }
132 }
133 }
134
135 if (!xhr) {
136 throw new Error("createXhr(): XMLHttpRequest not available");
137 }
138
139 return xhr;
140 },
141
142 get: get,
143
144 /**
145 * Parses a resource name into its component parts. Resource names
146 * look like: module/name.ext!strip, where the !strip part is
147 * optional.
148 * @param {String} name the resource name
149 * @returns {Object} with properties "moduleName", "ext" and "strip"
150 * where strip is a boolean.
151 */
152 parseName: function (name) {
153 var strip = false, index = name.indexOf("."),
154 modName = name.substring(0, index),
155 ext = name.substring(index + 1, name.length);
156
157 index = ext.indexOf("!");
158 if (index !== -1) {
159 //Pull off the strip arg.
160 strip = ext.substring(index + 1, ext.length);
161 strip = strip === "strip";
162 ext = ext.substring(0, index);
163 }
164
165 return {
166 moduleName: modName,
167 ext: ext,
168 strip: strip
169 };
170 },
171
172 xdRegExp: /^((\w+)\:)?\/\/([^\/\\]+)/,
173
174 /**
175 * Is an URL on another domain. Only works for browser use, returns
176 * false in non-browser environments. Only used to know if an
177 * optimized .js version of a text resource should be loaded
178 * instead.
179 * @param {String} url
180 * @returns Boolean
181 */
182 useXhr: function (url, protocol, hostname, port) {
183 var match = text.xdRegExp.exec(url),
184 uProtocol, uHostName, uPort;
185 if (!match) {
186 return true;
187 }
188 uProtocol = match[2];
189 uHostName = match[3];
190
191 uHostName = uHostName.split(':');
192 uPort = uHostName[1];
193 uHostName = uHostName[0];
194
195 return (!uProtocol || uProtocol === protocol) &&
196 (!uHostName || uHostName === hostname) &&
197 ((!uPort && !uHostName) || uPort === port);
198 },
199
200 finishLoad: function (name, strip, content, onLoad, config) {
201 content = strip ? text.strip(content) : content;
202 if (config.isBuild) {
203 buildMap[name] = content;
204 }
205 onLoad(content);
206 },
207
208 load: function (name, req, onLoad, config) {
209 //Name has format: some.module.filext!strip
210 //The strip part is optional.
211 //if strip is present, then that means only get the string contents
212 //inside a body tag in an HTML string. For XML/SVG content it means
213 //removing the <?xml ...?> declarations so the content can be inserted
214 //into the current doc without problems.
215
216 // Do not bother with the work if a build and text will
217 // not be inlined.
218 if (config.isBuild && !config.inlineText) {
219 onLoad();
220 return;
221 }
222
223 var parsed = text.parseName(name),
224 nonStripName = parsed.moduleName + '.' + parsed.ext,
225 url = req.toUrl(nonStripName),
226 useXhr = (config && config.text && config.text.useXhr) ||
227 text.useXhr;
228
229 //Load the text. Use XHR if possible and in a browser.
230 if (!hasLocation || useXhr(url, defaultProtocol, defaultHostName, defaultPort)) {
231 text.get(url, function (content) {
232 text.finishLoad(name, parsed.strip, content, onLoad, config);
233 });
234 } else {
235 //Need to fetch the resource across domains. Assume
236 //the resource has been optimized into a JS module. Fetch
237 //by the module name + extension, but do not include the
238 //!strip part to avoid file system issues.
239 req([nonStripName], function (content) {
240 text.finishLoad(parsed.moduleName + '.' + parsed.ext,
241 parsed.strip, content, onLoad, config);
242 });
243 }
244 },
245
246 write: function (pluginName, moduleName, write, config) {
247 if (moduleName in buildMap) {
248 var content = text.jsEscape(buildMap[moduleName]);
249 write.asModule(pluginName + "!" + moduleName,
250 "define(function () { return '" +
251 content +
252 "';});\n");
253 }
254 },
255
256 writeFile: function (pluginName, moduleName, req, write, config) {
257 var parsed = text.parseName(moduleName),
258 nonStripName = parsed.moduleName + '.' + parsed.ext,
259 //Use a '.js' file name so that it indicates it is a
260 //script that can be loaded across domains.
261 fileName = req.toUrl(parsed.moduleName + '.' +
262 parsed.ext) + '.js';
263
264 //Leverage own load() method to load plugin value, but only
265 //write out values that do not have the strip argument,
266 //to avoid any potential issues with ! in file names.
267 text.load(nonStripName, req, function (value) {
268 //Use own write() method to construct full module value.
269 //But need to create shell that translates writeFile's
270 //write() to the right interface.
271 var textWrite = function (contents) {
272 return write(fileName, contents);
273 };
274 textWrite.asModule = function (moduleName, contents) {
275 return write.asModule(moduleName, fileName, contents);
276 };
277
278 text.write(pluginName, nonStripName, textWrite, config);
279 }, config);
280 }
281 };
282
283 return text;
284 });
285 }());