2 * @license RequireJS text 1.0.2 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
6 /*jslint regexp: false, nomen: false, plusplus: false, strict: false */
7 /*global require: false, XMLHttpRequest: false, ActiveXObject: false,
8 define: false, window: false, process: false, Packages: false,
9 java: false, location: false */
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),
24 if (typeof window !== "undefined" && window.navigator && window.document) {
25 get = function (url, callback) {
26 var xhr = text.createXhr();
27 xhr.open('GET', url, true);
28 xhr.onreadystatechange = function (evt) {
29 //Do not explicitly handle errors, those should be
30 //visible via console output in the browser.
31 if (xhr.readyState === 4) {
32 callback(xhr.responseText);
37 } else if (typeof process !== "undefined" &&
39 !!process.versions.node) {
40 //Using special require.nodeRequire, something added by r.js.
41 fs = require.nodeRequire('fs');
43 get = function (url, callback) {
44 callback(fs.readFileSync(url, 'utf8'));
46 } else if (typeof Packages !== 'undefined') {
47 //Why Java, why is this so awkward?
48 get = function (url, callback) {
49 var encoding = "utf-8",
50 file = new java.io.File(url),
51 lineSeparator = java.lang.System.getProperty("line.separator"),
52 input = new java.io.BufferedReader(new java.io.InputStreamReader(new java.io.FileInputStream(file), encoding)),
56 stringBuffer = new java.lang.StringBuffer();
57 line = input.readLine();
59 // Byte Order Mark (BOM) - The Unicode Standard, version 3.0, page 324
60 // http://www.unicode.org/faq/utf_bom.html
62 // 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:
63 // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4508058
64 if (line && line.length() && line.charAt(0) === 0xfeff) {
65 // Eat the BOM, since we've already found the encoding on this file,
66 // and we plan to concatenating this buffer with others; the BOM should
67 // only appear at the top of a file.
68 line = line.substring(1);
71 stringBuffer.append(line);
73 while ((line = input.readLine()) !== null) {
74 stringBuffer.append(lineSeparator);
75 stringBuffer.append(line);
77 //Make sure we return a JavaScript string and not a Java string.
78 content = String(stringBuffer.toString()); //String
89 strip: function (content) {
90 //Strips <?xml ...?> declarations so that external SVG and XML
91 //documents can be added to a document without worry. Also, if the string
92 //is an HTML document, only the part inside the body tag is returned.
94 content = content.replace(xmlRegExp, "");
95 var matches = content.match(bodyRegExp);
105 jsEscape: function (content) {
106 return content.replace(/(['\\])/g, '\\$1')
107 .replace(/[\f]/g, "\\f")
108 .replace(/[\b]/g, "\\b")
109 .replace(/[\n]/g, "\\n")
110 .replace(/[\t]/g, "\\t")
111 .replace(/[\r]/g, "\\r");
114 createXhr: function () {
115 //Would love to dump the ActiveX crap in here. Need IE 6 to die first.
117 if (typeof XMLHttpRequest !== "undefined") {
118 return new XMLHttpRequest();
120 for (i = 0; i < 3; i++) {
123 xhr = new ActiveXObject(progId);
127 progIds = [progId]; // so faster next time
134 throw new Error("createXhr(): XMLHttpRequest not available");
143 * Parses a resource name into its component parts. Resource names
144 * look like: module/name.ext!strip, where the !strip part is
146 * @param {String} name the resource name
147 * @returns {Object} with properties "moduleName", "ext" and "strip"
148 * where strip is a boolean.
150 parseName: function (name) {
151 var strip = false, index = name.indexOf("."),
152 modName = name.substring(0, index),
153 ext = name.substring(index + 1, name.length);
155 index = ext.indexOf("!");
157 //Pull off the strip arg.
158 strip = ext.substring(index + 1, ext.length);
159 strip = strip === "strip";
160 ext = ext.substring(0, index);
170 xdRegExp: /^((\w+)\:)?\/\/([^\/\\]+)/,
173 * Is an URL on another domain. Only works for browser use, returns
174 * false in non-browser environments. Only used to know if an
175 * optimized .js version of a text resource should be loaded
177 * @param {String} url
180 useXhr: function (url, protocol, hostname, port) {
181 var match = text.xdRegExp.exec(url),
182 uProtocol, uHostName, uPort;
186 uProtocol = match[2];
187 uHostName = match[3];
189 uHostName = uHostName.split(':');
190 uPort = uHostName[1];
191 uHostName = uHostName[0];
193 return (!uProtocol || uProtocol === protocol) &&
194 (!uHostName || uHostName === hostname) &&
195 ((!uPort && !uHostName) || uPort === port);
198 finishLoad: function (name, strip, content, onLoad, config) {
199 content = strip ? text.strip(content) : content;
200 if (config.isBuild) {
201 buildMap[name] = content;
206 load: function (name, req, onLoad, config) {
207 //Name has format: some.module.filext!strip
208 //The strip part is optional.
209 //if strip is present, then that means only get the string contents
210 //inside a body tag in an HTML string. For XML/SVG content it means
211 //removing the <?xml ...?> declarations so the content can be inserted
212 //into the current doc without problems.
214 // Do not bother with the work if a build and text will
216 if (config.isBuild && !config.inlineText) {
221 var parsed = text.parseName(name),
222 nonStripName = parsed.moduleName + '.' + parsed.ext,
223 url = req.toUrl(nonStripName),
224 useXhr = (config && config.text && config.text.useXhr) ||
227 //Load the text. Use XHR if possible and in a browser.
228 if (!hasLocation || useXhr(url, defaultProtocol, defaultHostName, defaultPort)) {
229 text.get(url, function (content) {
230 text.finishLoad(name, parsed.strip, content, onLoad, config);
233 //Need to fetch the resource across domains. Assume
234 //the resource has been optimized into a JS module. Fetch
235 //by the module name + extension, but do not include the
236 //!strip part to avoid file system issues.
237 req([nonStripName], function (content) {
238 text.finishLoad(parsed.moduleName + '.' + parsed.ext,
239 parsed.strip, content, onLoad, config);
244 write: function (pluginName, moduleName, write, config) {
245 if (moduleName in buildMap) {
246 var content = text.jsEscape(buildMap[moduleName]);
247 write.asModule(pluginName + "!" + moduleName,
248 "define(function () { return '" +
254 writeFile: function (pluginName, moduleName, req, write, config) {
255 var parsed = text.parseName(moduleName),
256 nonStripName = parsed.moduleName + '.' + parsed.ext,
257 //Use a '.js' file name so that it indicates it is a
258 //script that can be loaded across domains.
259 fileName = req.toUrl(parsed.moduleName + '.' +
262 //Leverage own load() method to load plugin value, but only
263 //write out values that do not have the strip argument,
264 //to avoid any potential issues with ! in file names.
265 text.load(nonStripName, req, function (value) {
266 //Use own write() method to construct full module value.
267 //But need to create shell that translates writeFile's
268 //write() to the right interface.
269 var textWrite = function (contents) {
270 return write(fileName, contents);
272 textWrite.asModule = function (moduleName, contents) {
273 return write.asModule(moduleName, fileName, contents);
276 text.write(pluginName, nonStripName, textWrite, config);