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