text.js revision 89092dc10fe08b037266c0b4efb94b221f6fffb3
0N/A/**
2362N/A * @license RequireJS text 2.0.10 Copyright (c) 2010-2012, The Dojo Foundation All Rights Reserved.
0N/A * Available via the MIT or new BSD license.
0N/A * see: http://github.com/requirejs/text for details
0N/A */
0N/A/*jslint regexp: true */
2362N/A/*global require, XMLHttpRequest, ActiveXObject,
0N/A define, window, process, Packages,
2362N/A java, location, Components, FileUtils */
0N/A
0N/Adefine(['module'], function (module) {
0N/A 'use strict';
0N/A
0N/A var text, fs, Cc, Ci, xpcIsWindows,
0N/A progIds = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'],
0N/A xmlRegExp = /^\s*<\?xml(\s)+version=[\'\"](\d)*.(\d)*[\'\"](\s)*\?>/im,
0N/A bodyRegExp = /<body[^>]*>\s*([\s\S]+)\s*<\/body>/im,
0N/A hasLocation = typeof location !== 'undefined' && location.href,
0N/A defaultProtocol = hasLocation && location.protocol && location.protocol.replace(/\:/, ''),
0N/A defaultHostName = hasLocation && location.hostname,
2362N/A defaultPort = hasLocation && (location.port || undefined),
2362N/A buildMap = {},
2362N/A masterConfig = (module.config && module.config()) || {};
0N/A
0N/A text = {
0N/A version: '2.0.10',
0N/A
0N/A strip: function (content) {
0N/A //Strips <?xml ...?> declarations so that external SVG and XML
0N/A //documents can be added to a document without worry. Also, if the string
0N/A //is an HTML document, only the part inside the body tag is returned.
0N/A if (content) {
0N/A content = content.replace(xmlRegExp, "");
0N/A var matches = content.match(bodyRegExp);
0N/A if (matches) {
0N/A content = matches[1];
0N/A }
0N/A } else {
0N/A content = "";
0N/A }
0N/A return content;
0N/A },
0N/A
0N/A jsEscape: function (content) {
0N/A return content.replace(/(['\\])/g, '\\$1')
0N/A .replace(/[\f]/g, "\\f")
0N/A .replace(/[\b]/g, "\\b")
0N/A .replace(/[\n]/g, "\\n")
0N/A .replace(/[\t]/g, "\\t")
0N/A .replace(/[\r]/g, "\\r")
0N/A .replace(/[\u2028]/g, "\\u2028")
0N/A .replace(/[\u2029]/g, "\\u2029");
0N/A },
0N/A
0N/A createXhr: masterConfig.createXhr || function () {
0N/A //Would love to dump the ActiveX crap in here. Need IE 6 to die first.
0N/A var xhr, i, progId;
0N/A if (typeof XMLHttpRequest !== "undefined") {
0N/A return new XMLHttpRequest();
0N/A } else if (typeof ActiveXObject !== "undefined") {
0N/A for (i = 0; i < 3; i += 1) {
0N/A progId = progIds[i];
0N/A try {
0N/A xhr = new ActiveXObject(progId);
0N/A } catch (e) {}
0N/A
0N/A if (xhr) {
0N/A progIds = [progId]; // so faster next time
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A
0N/A return xhr;
0N/A },
0N/A
0N/A /**
0N/A * Parses a resource name into its component parts. Resource names
0N/A * look like: module/name.ext!strip, where the !strip part is
0N/A * optional.
0N/A * @param {String} name the resource name
0N/A * @returns {Object} with properties "moduleName", "ext" and "strip"
0N/A * where strip is a boolean.
0N/A */
0N/A parseName: function (name) {
0N/A var modName, ext, temp,
0N/A strip = false,
0N/A index = name.indexOf("."),
0N/A isRelative = name.indexOf('./') === 0 ||
0N/A name.indexOf('../') === 0;
0N/A
0N/A if (index !== -1 && (!isRelative || index > 1)) {
0N/A modName = name.substring(0, index);
0N/A ext = name.substring(index + 1, name.length);
0N/A } else {
0N/A modName = name;
0N/A }
0N/A
0N/A temp = ext || modName;
0N/A index = temp.indexOf("!");
0N/A if (index !== -1) {
0N/A //Pull off the strip arg.
0N/A strip = temp.substring(index + 1) === "strip";
0N/A temp = temp.substring(0, index);
0N/A if (ext) {
0N/A ext = temp;
0N/A } else {
0N/A modName = temp;
0N/A }
0N/A }
0N/A
0N/A return {
0N/A moduleName: modName,
0N/A ext: ext,
0N/A strip: strip
0N/A };
0N/A },
0N/A
0N/A xdRegExp: /^((\w+)\:)?\/\/([^\/\\]+)/,
0N/A
0N/A /**
0N/A * Is an URL on another domain. Only works for browser use, returns
0N/A * false in non-browser environments. Only used to know if an
0N/A * optimized .js version of a text resource should be loaded
0N/A * instead.
0N/A * @param {String} url
0N/A * @returns Boolean
0N/A */
0N/A useXhr: function (url, protocol, hostname, port) {
0N/A var uProtocol, uHostName, uPort,
0N/A match = text.xdRegExp.exec(url);
0N/A if (!match) {
0N/A return true;
0N/A }
0N/A uProtocol = match[2];
0N/A uHostName = match[3];
0N/A
0N/A uHostName = uHostName.split(':');
0N/A uPort = uHostName[1];
0N/A uHostName = uHostName[0];
0N/A
0N/A return (!uProtocol || uProtocol === protocol) &&
0N/A (!uHostName || uHostName.toLowerCase() === hostname.toLowerCase()) &&
0N/A ((!uPort && !uHostName) || uPort === port);
0N/A },
0N/A
0N/A finishLoad: function (name, strip, content, onLoad) {
0N/A content = strip ? text.strip(content) : content;
0N/A if (masterConfig.isBuild) {
0N/A buildMap[name] = content;
0N/A }
0N/A onLoad(content);
0N/A },
0N/A
0N/A load: function (name, req, onLoad, config) {
0N/A //Name has format: some.module.filext!strip
0N/A //The strip part is optional.
0N/A //if strip is present, then that means only get the string contents
0N/A //inside a body tag in an HTML string. For XML/SVG content it means
0N/A //removing the <?xml ...?> declarations so the content can be inserted
0N/A //into the current doc without problems.
0N/A
0N/A // Do not bother with the work if a build and text will
0N/A // not be inlined.
0N/A if (config.isBuild && !config.inlineText) {
0N/A onLoad();
0N/A return;
0N/A }
0N/A
0N/A masterConfig.isBuild = config.isBuild;
0N/A
0N/A var parsed = text.parseName(name),
0N/A nonStripName = parsed.moduleName +
0N/A (parsed.ext ? '.' + parsed.ext : ''),
0N/A url = req.toUrl(nonStripName),
0N/A useXhr = (masterConfig.useXhr) ||
0N/A text.useXhr;
0N/A
0N/A // Do not load if it is an empty: url
0N/A if (url.indexOf('empty:') === 0) {
0N/A onLoad();
0N/A return;
0N/A }
0N/A
0N/A //Load the text. Use XHR if possible and in a browser.
0N/A if (!hasLocation || useXhr(url, defaultProtocol, defaultHostName, defaultPort)) {
0N/A text.get(url, function (content) {
0N/A text.finishLoad(name, parsed.strip, content, onLoad);
0N/A }, function (err) {
0N/A if (onLoad.error) {
0N/A onLoad.error(err);
0N/A }
0N/A });
0N/A } else {
0N/A //Need to fetch the resource across domains. Assume
0N/A //the resource has been optimized into a JS module. Fetch
0N/A //by the module name + extension, but do not include the
0N/A //!strip part to avoid file system issues.
0N/A req([nonStripName], function (content) {
0N/A text.finishLoad(parsed.moduleName + '.' + parsed.ext,
0N/A parsed.strip, content, onLoad);
0N/A });
0N/A }
0N/A },
0N/A
0N/A write: function (pluginName, moduleName, write, config) {
0N/A if (buildMap.hasOwnProperty(moduleName)) {
0N/A var content = text.jsEscape(buildMap[moduleName]);
0N/A write.asModule(pluginName + "!" + moduleName,
0N/A "define(function () { return '" +
0N/A content +
0N/A "';});\n");
0N/A }
0N/A },
0N/A
0N/A writeFile: function (pluginName, moduleName, req, write, config) {
0N/A var parsed = text.parseName(moduleName),
0N/A extPart = parsed.ext ? '.' + parsed.ext : '',
0N/A nonStripName = parsed.moduleName + extPart,
0N/A //Use a '.js' file name so that it indicates it is a
0N/A //script that can be loaded across domains.
0N/A fileName = req.toUrl(parsed.moduleName + extPart) + '.js';
0N/A
0N/A //Leverage own load() method to load plugin value, but only
0N/A //write out values that do not have the strip argument,
0N/A //to avoid any potential issues with ! in file names.
0N/A text.load(nonStripName, req, function (value) {
0N/A //Use own write() method to construct full module value.
0N/A //But need to create shell that translates writeFile's
0N/A //write() to the right interface.
0N/A var textWrite = function (contents) {
0N/A return write(fileName, contents);
0N/A };
0N/A textWrite.asModule = function (moduleName, contents) {
0N/A return write.asModule(moduleName, fileName, contents);
0N/A };
0N/A
0N/A text.write(pluginName, nonStripName, textWrite, config);
0N/A }, config);
0N/A }
0N/A };
0N/A
0N/A if (masterConfig.env === 'node' || (!masterConfig.env &&
0N/A typeof process !== "undefined" &&
0N/A process.versions &&
0N/A !!process.versions.node &&
0N/A !process.versions['node-webkit'])) {
0N/A //Using special require.nodeRequire, something added by r.js.
0N/A fs = require.nodeRequire('fs');
0N/A
0N/A text.get = function (url, callback, errback) {
0N/A try {
0N/A var file = fs.readFileSync(url, 'utf8');
0N/A //Remove BOM (Byte Mark Order) from utf8 files if it is there.
0N/A if (file.indexOf('\uFEFF') === 0) {
0N/A file = file.substring(1);
0N/A }
0N/A callback(file);
0N/A } catch (e) {
0N/A errback(e);
0N/A }
0N/A };
0N/A } else if (masterConfig.env === 'xhr' || (!masterConfig.env &&
0N/A text.createXhr())) {
0N/A text.get = function (url, callback, errback, headers) {
0N/A var xhr = text.createXhr(), header;
0N/A xhr.open('GET', url, true);
0N/A
0N/A //Allow plugins direct access to xhr headers
0N/A if (headers) {
0N/A for (header in headers) {
0N/A if (headers.hasOwnProperty(header)) {
0N/A xhr.setRequestHeader(header.toLowerCase(), headers[header]);
0N/A }
0N/A }
0N/A }
0N/A
0N/A //Allow overrides specified in config
0N/A if (masterConfig.onXhr) {
0N/A masterConfig.onXhr(xhr, url);
0N/A }
0N/A
0N/A xhr.onreadystatechange = function (evt) {
0N/A var status, err;
0N/A //Do not explicitly handle errors, those should be
0N/A //visible via console output in the browser.
0N/A if (xhr.readyState === 4) {
0N/A status = xhr.status;
0N/A if (status > 399 && status < 600) {
0N/A //An http 4xx or 5xx error. Signal an error.
0N/A err = new Error(url + ' HTTP status: ' + status);
0N/A err.xhr = xhr;
0N/A errback(err);
0N/A } else {
0N/A callback(xhr.responseText);
0N/A }
0N/A
0N/A if (masterConfig.onXhrComplete) {
0N/A masterConfig.onXhrComplete(xhr, url);
0N/A }
0N/A }
0N/A };
0N/A xhr.send(null);
0N/A };
0N/A } else if (masterConfig.env === 'rhino' || (!masterConfig.env &&
0N/A typeof Packages !== 'undefined' && typeof java !== 'undefined')) {
0N/A //Why Java, why is this so awkward?
0N/A text.get = function (url, callback) {
0N/A var stringBuffer, line,
0N/A encoding = "utf-8",
0N/A file = new java.io.File(url),
0N/A lineSeparator = java.lang.System.getProperty("line.separator"),
0N/A input = new java.io.BufferedReader(new java.io.InputStreamReader(new java.io.FileInputStream(file), encoding)),
0N/A content = '';
0N/A try {
0N/A stringBuffer = new java.lang.StringBuffer();
0N/A line = input.readLine();
0N/A
0N/A // Byte Order Mark (BOM) - The Unicode Standard, version 3.0, page 324
0N/A // http://www.unicode.org/faq/utf_bom.html
0N/A
0N/A // 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:
0N/A // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4508058
0N/A if (line && line.length() && line.charAt(0) === 0xfeff) {
0N/A // Eat the BOM, since we've already found the encoding on this file,
0N/A // and we plan to concatenating this buffer with others; the BOM should
0N/A // only appear at the top of a file.
0N/A line = line.substring(1);
0N/A }
if (line !== null) {
stringBuffer.append(line);
}
while ((line = input.readLine()) !== null) {
stringBuffer.append(lineSeparator);
stringBuffer.append(line);
}
//Make sure we return a JavaScript string and not a Java string.
content = String(stringBuffer.toString()); //String
} finally {
input.close();
}
callback(content);
};
} else if (masterConfig.env === 'xpconnect' || (!masterConfig.env &&
typeof Components !== 'undefined' && Components.classes &&
Components.interfaces)) {
//Avert your gaze!
Cc = Components.classes,
Ci = Components.interfaces;
Components.utils['import']('resource://gre/modules/FileUtils.jsm');
xpcIsWindows = ('@mozilla.org/windows-registry-key;1' in Cc);
text.get = function (url, callback) {
var inStream, convertStream, fileObj,
readData = {};
if (xpcIsWindows) {
url = url.replace(/\//g, '\\');
}
fileObj = new FileUtils.File(url);
//XPCOM, you so crazy
try {
inStream = Cc['@mozilla.org/network/file-input-stream;1']
.createInstance(Ci.nsIFileInputStream);
inStream.init(fileObj, 1, 0, false);
convertStream = Cc['@mozilla.org/intl/converter-input-stream;1']
.createInstance(Ci.nsIConverterInputStream);
convertStream.init(inStream, "utf-8", inStream.available(),
Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER);
convertStream.readString(inStream.available(), readData);
convertStream.close();
inStream.close();
callback(readData.value);
} catch (e) {
throw new Error((fileObj && fileObj.path || '') + ': ' + e);
}
};
}
return text;
});