Debian Codegraph @

text.js

raw symbols SCIP
1 /**
2 * @license text 2.0.16 Copyright jQuery Foundation and other contributors.
3 * Released under MIT license, http://github.com/requirejs/text/LICENSE
4 */
5 /*jslint regexp: true */
6 /*global require, XMLHttpRequest, ActiveXObject,
7 define, window, process, Packages,
8 java, location, Components, FileUtils */
9
10 define(['module'], function (module) {
11 'use strict';
12
13 var text, fs, Cc, Ci, xpcIsWindows,
14 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 masterConfig = (module.config && module.config()) || {};
23
24 function useDefault(value, defaultValue) {
25 return value === undefined || value === '' ? defaultValue : value;
26 }
27
28 //Allow for default ports for http and https.
29 function isSamePort(protocol1, port1, protocol2, port2) {
30 if (port1 === port2) {
31 return true;
32 } else if (protocol1 === protocol2) {
33 if (protocol1 === 'http') {
34 return useDefault(port1, '80') === useDefault(port2, '80');
35 } else if (protocol1 === 'https') {
36 return useDefault(port1, '443') === useDefault(port2, '443');
37 }
38 }
39 return false;
40 }
41
42 text = {
43 version: '2.0.16',
44
45 strip: function (content) {
46 //Strips <?xml ...?> declarations so that external SVG and XML
47 //documents can be added to a document without worry. Also, if the string
48 //is an HTML document, only the part inside the body tag is returned.
49 if (content) {
50 content = content.replace(xmlRegExp, "");
51 var matches = content.match(bodyRegExp);
52 if (matches) {
53 content = matches[1];
54 }
55 } else {
56 content = "";
57 }
58 return content;
59 },
60
61 jsEscape: function (content) {
62 return content.replace(/(['\\])/g, '\\$1')
63 .replace(/[\f]/g, "\\f")
64 .replace(/[\b]/g, "\\b")
65 .replace(/[\n]/g, "\\n")
66 .replace(/[\t]/g, "\\t")
67 .replace(/[\r]/g, "\\r")
68 .replace(/[\u2028]/g, "\\u2028")
69 .replace(/[\u2029]/g, "\\u2029");
70 },
71
72 createXhr: masterConfig.createXhr || function () {
73 //Would love to dump the ActiveX crap in here. Need IE 6 to die first.
74 var xhr, i, progId;
75 if (typeof XMLHttpRequest !== "undefined") {
76 return new XMLHttpRequest();
77 } else if (typeof ActiveXObject !== "undefined") {
78 for (i = 0; i < 3; i += 1) {
79 progId = progIds[i];
80 try {
81 xhr = new ActiveXObject(progId);
82 } catch (e) {}
83
84 if (xhr) {
85 progIds = [progId]; // so faster next time
86 break;
87 }
88 }
89 }
90
91 return xhr;
92 },
93
94 /**
95 * Parses a resource name into its component parts. Resource names
96 * look like: module/name.ext!strip, where the !strip part is
97 * optional.
98 * @param {String} name the resource name
99 * @returns {Object} with properties "moduleName", "ext" and "strip"
100 * where strip is a boolean.
101 */
102 parseName: function (name) {
103 var modName, ext, temp,
104 strip = false,
105 index = name.lastIndexOf("."),
106 isRelative = name.indexOf('./') === 0 ||
107 name.indexOf('../') === 0;
108
109 if (index !== -1 && (!isRelative || index > 1)) {
110 modName = name.substring(0, index);
111 ext = name.substring(index + 1);
112 } else {
113 modName = name;
114 }
115
116 temp = ext || modName;
117 index = temp.indexOf("!");
118 if (index !== -1) {
119 //Pull off the strip arg.
120 strip = temp.substring(index + 1) === "strip";
121 temp = temp.substring(0, index);
122 if (ext) {
123 ext = temp;
124 } else {
125 modName = temp;
126 }
127 }
128
129 return {
130 moduleName: modName,
131 ext: ext,
132 strip: strip
133 };
134 },
135
136 xdRegExp: /^((\w+)\:)?\/\/([^\/\\]+)/,
137
138 /**
139 * Is an URL on another domain. Only works for browser use, returns
140 * false in non-browser environments. Only used to know if an
141 * optimized .js version of a text resource should be loaded
142 * instead.
143 * @param {String} url
144 * @returns Boolean
145 */
146 useXhr: function (url, protocol, hostname, port) {
147 var uProtocol, uHostName, uPort,
148 match = text.xdRegExp.exec(url);
149 if (!match) {
150 return true;
151 }
152 uProtocol = match[2];
153 uHostName = match[3];
154
155 uHostName = uHostName.split(':');
156 uPort = uHostName[1];
157 uHostName = uHostName[0];
158
159 return (!uProtocol || uProtocol === protocol) &&
160 (!uHostName || uHostName.toLowerCase() === hostname.toLowerCase()) &&
161 ((!uPort && !uHostName) || isSamePort(uProtocol, uPort, protocol, port));
162 },
163
164 finishLoad: function (name, strip, content, onLoad) {
165 content = strip ? text.strip(content) : content;
166 if (masterConfig.isBuild) {
167 buildMap[name] = content;
168 }
169 onLoad(content);
170 },
171
172 load: function (name, req, onLoad, config) {
173 //Name has format: some.module.filext!strip
174 //The strip part is optional.
175 //if strip is present, then that means only get the string contents
176 //inside a body tag in an HTML string. For XML/SVG content it means
177 //removing the <?xml ...?> declarations so the content can be inserted
178 //into the current doc without problems.
179
180 // Do not bother with the work if a build and text will
181 // not be inlined.
182 if (config && config.isBuild && !config.inlineText) {
183 onLoad();
184 return;
185 }
186
187 masterConfig.isBuild = config && config.isBuild;
188
189 var parsed = text.parseName(name),
190 nonStripName = parsed.moduleName +
191 (parsed.ext ? '.' + parsed.ext : ''),
192 url = req.toUrl(nonStripName),
193 useXhr = (masterConfig.useXhr) ||
194 text.useXhr;
195
196 // Do not load if it is an empty: url
197 if (url.indexOf('empty:') === 0) {
198 onLoad();
199 return;
200 }
201
202 //Load the text. Use XHR if possible and in a browser.
203 if (!hasLocation || useXhr(url, defaultProtocol, defaultHostName, defaultPort)) {
204 text.get(url, function (content) {
205 text.finishLoad(name, parsed.strip, content, onLoad);
206 }, function (err) {
207 if (onLoad.error) {
208 onLoad.error(err);
209 }
210 });
211 } else {
212 //Need to fetch the resource across domains. Assume
213 //the resource has been optimized into a JS module. Fetch
214 //by the module name + extension, but do not include the
215 //!strip part to avoid file system issues.
216 req([nonStripName], function (content) {
217 text.finishLoad(parsed.moduleName + '.' + parsed.ext,
218 parsed.strip, content, onLoad);
219 }, function (err) {
220 if (onLoad.error) {
221 onLoad.error(err);
222 }
223 });
224 }
225 },
226
227 write: function (pluginName, moduleName, write, config) {
228 if (buildMap.hasOwnProperty(moduleName)) {
229 var content = text.jsEscape(buildMap[moduleName]);
230 write.asModule(pluginName + "!" + moduleName,
231 "define(function () { return '" +
232 content +
233 "';});\n");
234 }
235 },
236
237 writeFile: function (pluginName, moduleName, req, write, config) {
238 var parsed = text.parseName(moduleName),
239 extPart = parsed.ext ? '.' + parsed.ext : '',
240 nonStripName = parsed.moduleName + extPart,
241 //Use a '.js' file name so that it indicates it is a
242 //script that can be loaded across domains.
243 fileName = req.toUrl(parsed.moduleName + extPart) + '.js';
244
245 //Leverage own load() method to load plugin value, but only
246 //write out values that do not have the strip argument,
247 //to avoid any potential issues with ! in file names.
248 text.load(nonStripName, req, function (value) {
249 //Use own write() method to construct full module value.
250 //But need to create shell that translates writeFile's
251 //write() to the right interface.
252 var textWrite = function (contents) {
253 return write(fileName, contents);
254 };
255 textWrite.asModule = function (moduleName, contents) {
256 return write.asModule(moduleName, fileName, contents);
257 };
258
259 text.write(pluginName, nonStripName, textWrite, config);
260 }, config);
261 }
262 };
263
264 if (masterConfig.env === 'node' || (!masterConfig.env &&
265 typeof process !== "undefined" &&
266 process.versions &&
267 !!process.versions.node &&
268 !process.versions['node-webkit'] &&
269 !process.versions['atom-shell'])) {
270 //Using special require.nodeRequire, something added by r.js.
271 fs = require.nodeRequire('fs');
272
273 text.get = function (url, callback, errback) {
274 try {
275 var file = fs.readFileSync(url, 'utf8');
276 //Remove BOM (Byte Mark Order) from utf8 files if it is there.
277 if (file[0] === '\uFEFF') {
278 file = file.substring(1);
279 }
280 callback(file);
281 } catch (e) {
282 if (errback) {
283 errback(e);
284 }
285 }
286 };
287 } else if (masterConfig.env === 'xhr' || (!masterConfig.env &&
288 text.createXhr())) {
289 text.get = function (url, callback, errback, headers) {
290 var xhr = text.createXhr(), header;
291 xhr.open('GET', url, true);
292
293 //Allow plugins direct access to xhr headers
294 if (headers) {
295 for (header in headers) {
296 if (headers.hasOwnProperty(header)) {
297 xhr.setRequestHeader(header.toLowerCase(), headers[header]);
298 }
299 }
300 }
301
302 //Allow overrides specified in config
303 if (masterConfig.onXhr) {
304 masterConfig.onXhr(xhr, url);
305 }
306
307 xhr.onreadystatechange = function (evt) {
308 var status, err;
309 //Do not explicitly handle errors, those should be
310 //visible via console output in the browser.
311 if (xhr.readyState === 4) {
312 status = xhr.status || 0;
313 if (status > 399 && status < 600) {
314 //An http 4xx or 5xx error. Signal an error.
315 err = new Error(url + ' HTTP status: ' + status);
316 err.xhr = xhr;
317 if (errback) {
318 errback(err);
319 }
320 } else {
321 callback(xhr.responseText);
322 }
323
324 if (masterConfig.onXhrComplete) {
325 masterConfig.onXhrComplete(xhr, url);
326 }
327 }
328 };
329 xhr.send(null);
330 };
331 } else if (masterConfig.env === 'rhino' || (!masterConfig.env &&
332 typeof Packages !== 'undefined' && typeof java !== 'undefined')) {
333 //Why Java, why is this so awkward?
334 text.get = function (url, callback) {
335 var stringBuffer, line,
336 encoding = "utf-8",
337 file = new java.io.File(url),
338 lineSeparator = java.lang.System.getProperty("line.separator"),
339 input = new java.io.BufferedReader(new java.io.InputStreamReader(new java.io.FileInputStream(file), encoding)),
340 content = '';
341 try {
342 stringBuffer = new java.lang.StringBuffer();
343 line = input.readLine();
344
345 // Byte Order Mark (BOM) - The Unicode Standard, version 3.0, page 324
346 // http://www.unicode.org/faq/utf_bom.html
347
348 // 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:
349 // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4508058
350 if (line && line.length() && line.charAt(0) === 0xfeff) {
351 // Eat the BOM, since we've already found the encoding on this file,
352 // and we plan to concatenating this buffer with others; the BOM should
353 // only appear at the top of a file.
354 line = line.substring(1);
355 }
356
357 if (line !== null) {
358 stringBuffer.append(line);
359 }
360
361 while ((line = input.readLine()) !== null) {
362 stringBuffer.append(lineSeparator);
363 stringBuffer.append(line);
364 }
365 //Make sure we return a JavaScript string and not a Java string.
366 content = String(stringBuffer.toString()); //String
367 } finally {
368 input.close();
369 }
370 callback(content);
371 };
372 } else if (masterConfig.env === 'xpconnect' || (!masterConfig.env &&
373 typeof Components !== 'undefined' && Components.classes &&
374 Components.interfaces)) {
375 //Avert your gaze!
376 Cc = Components.classes;
377 Ci = Components.interfaces;
378 Components.utils['import']('resource://gre/modules/FileUtils.jsm');
379 xpcIsWindows = ('@mozilla.org/windows-registry-key;1' in Cc);
380
381 text.get = function (url, callback) {
382 var inStream, convertStream, fileObj,
383 readData = {};
384
385 if (xpcIsWindows) {
386 url = url.replace(/\//g, '\\');
387 }
388
389 fileObj = new FileUtils.File(url);
390
391 //XPCOM, you so crazy
392 try {
393 inStream = Cc['@mozilla.org/network/file-input-stream;1']
394 .createInstance(Ci.nsIFileInputStream);
395 inStream.init(fileObj, 1, 0, false);
396
397 convertStream = Cc['@mozilla.org/intl/converter-input-stream;1']
398 .createInstance(Ci.nsIConverterInputStream);
399 convertStream.init(inStream, "utf-8", inStream.available(),
400 Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER);
401
402 convertStream.readString(inStream.available(), readData);
403 convertStream.close();
404 inStream.close();
405 callback(readData.value);
406 } catch (e) {
407 throw new Error((fileObj && fileObj.path || '') + ': ' + e);
408 }
409 };
410 }
411 return text;
412 });