Path: blob/main/public/games/files/garbage-collector/js/text.js
1036 views
/**1* @license RequireJS text 2.0.10 Copyright (c) 2010-2012, The Dojo Foundation All Rights Reserved.2* Available via the MIT or new BSD license.3* see: http://github.com/requirejs/text for details4*/5/*jslint regexp: true */6/*global require, XMLHttpRequest, ActiveXObject,7define, window, process, Packages,8java, location, Components, FileUtils */910define(['module'], function (module) {11'use strict';1213var text, fs, Cc, Ci, xpcIsWindows,14progIds = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'],15xmlRegExp = /^\s*<\?xml(\s)+version=[\'\"](\d)*.(\d)*[\'\"](\s)*\?>/im,16bodyRegExp = /<body[^>]*>\s*([\s\S]+)\s*<\/body>/im,17hasLocation = typeof location !== 'undefined' && location.href,18defaultProtocol = hasLocation && location.protocol && location.protocol.replace(/\:/, ''),19defaultHostName = hasLocation && location.hostname,20defaultPort = hasLocation && (location.port || undefined),21buildMap = {},22masterConfig = (module.config && module.config()) || {};2324text = {25version: '2.0.10',2627strip: function (content) {28//Strips <?xml ...?> declarations so that external SVG and XML29//documents can be added to a document without worry. Also, if the string30//is an HTML document, only the part inside the body tag is returned.31if (content) {32content = content.replace(xmlRegExp, "");33var matches = content.match(bodyRegExp);34if (matches) {35content = matches[1];36}37} else {38content = "";39}40return content;41},4243jsEscape: function (content) {44return 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.replace(/[\u2028]/g, "\\u2028")51.replace(/[\u2029]/g, "\\u2029");52},5354createXhr: masterConfig.createXhr || function () {55//Would love to dump the ActiveX crap in here. Need IE 6 to die first.56var xhr, i, progId;57if (typeof XMLHttpRequest !== "undefined") {58return new XMLHttpRequest();59} else if (typeof ActiveXObject !== "undefined") {60for (i = 0; i < 3; i += 1) {61progId = progIds[i];62try {63xhr = new ActiveXObject(progId);64} catch (e) {}6566if (xhr) {67progIds = [progId]; // so faster next time68break;69}70}71}7273return xhr;74},7576/**77* Parses a resource name into its component parts. Resource names78* look like: module/name.ext!strip, where the !strip part is79* optional.80* @param {String} name the resource name81* @returns {Object} with properties "moduleName", "ext" and "strip"82* where strip is a boolean.83*/84parseName: function (name) {85var modName, ext, temp,86strip = false,87index = name.indexOf("."),88isRelative = name.indexOf('./') === 0 ||89name.indexOf('../') === 0;9091if (index !== -1 && (!isRelative || index > 1)) {92modName = name.substring(0, index);93ext = name.substring(index + 1, name.length);94} else {95modName = name;96}9798temp = ext || modName;99index = temp.indexOf("!");100if (index !== -1) {101//Pull off the strip arg.102strip = temp.substring(index + 1) === "strip";103temp = temp.substring(0, index);104if (ext) {105ext = temp;106} else {107modName = temp;108}109}110111return {112moduleName: modName,113ext: ext,114strip: strip115};116},117118xdRegExp: /^((\w+)\:)?\/\/([^\/\\]+)/,119120/**121* Is an URL on another domain. Only works for browser use, returns122* false in non-browser environments. Only used to know if an123* optimized .js version of a text resource should be loaded124* instead.125* @param {String} url126* @returns Boolean127*/128useXhr: function (url, protocol, hostname, port) {129var uProtocol, uHostName, uPort,130match = text.xdRegExp.exec(url);131if (!match) {132return true;133}134uProtocol = match[2];135uHostName = match[3];136137uHostName = uHostName.split(':');138uPort = uHostName[1];139uHostName = uHostName[0];140141return (!uProtocol || uProtocol === protocol) &&142(!uHostName || uHostName.toLowerCase() === hostname.toLowerCase()) &&143((!uPort && !uHostName) || uPort === port);144},145146finishLoad: function (name, strip, content, onLoad) {147content = strip ? text.strip(content) : content;148if (masterConfig.isBuild) {149buildMap[name] = content;150}151onLoad(content);152},153154load: function (name, req, onLoad, config) {155//Name has format: some.module.filext!strip156//The strip part is optional.157//if strip is present, then that means only get the string contents158//inside a body tag in an HTML string. For XML/SVG content it means159//removing the <?xml ...?> declarations so the content can be inserted160//into the current doc without problems.161162// Do not bother with the work if a build and text will163// not be inlined.164if (config.isBuild && !config.inlineText) {165onLoad();166return;167}168169masterConfig.isBuild = config.isBuild;170171var parsed = text.parseName(name),172nonStripName = parsed.moduleName +173(parsed.ext ? '.' + parsed.ext : ''),174url = req.toUrl(nonStripName),175useXhr = (masterConfig.useXhr) ||176text.useXhr;177178// Do not load if it is an empty: url179if (url.indexOf('empty:') === 0) {180onLoad();181return;182}183184//Load the text. Use XHR if possible and in a browser.185if (!hasLocation || useXhr(url, defaultProtocol, defaultHostName, defaultPort)) {186text.get(url, function (content) {187text.finishLoad(name, parsed.strip, content, onLoad);188}, function (err) {189if (onLoad.error) {190onLoad.error(err);191}192});193} else {194//Need to fetch the resource across domains. Assume195//the resource has been optimized into a JS module. Fetch196//by the module name + extension, but do not include the197//!strip part to avoid file system issues.198req([nonStripName], function (content) {199text.finishLoad(parsed.moduleName + '.' + parsed.ext,200parsed.strip, content, onLoad);201});202}203},204205write: function (pluginName, moduleName, write, config) {206if (buildMap.hasOwnProperty(moduleName)) {207var content = text.jsEscape(buildMap[moduleName]);208write.asModule(pluginName + "!" + moduleName,209"define(function () { return '" +210content +211"';});\n");212}213},214215writeFile: function (pluginName, moduleName, req, write, config) {216var parsed = text.parseName(moduleName),217extPart = parsed.ext ? '.' + parsed.ext : '',218nonStripName = parsed.moduleName + extPart,219//Use a '.js' file name so that it indicates it is a220//script that can be loaded across domains.221fileName = req.toUrl(parsed.moduleName + extPart) + '.js';222223//Leverage own load() method to load plugin value, but only224//write out values that do not have the strip argument,225//to avoid any potential issues with ! in file names.226text.load(nonStripName, req, function (value) {227//Use own write() method to construct full module value.228//But need to create shell that translates writeFile's229//write() to the right interface.230var textWrite = function (contents) {231return write(fileName, contents);232};233textWrite.asModule = function (moduleName, contents) {234return write.asModule(moduleName, fileName, contents);235};236237text.write(pluginName, nonStripName, textWrite, config);238}, config);239}240};241242if (masterConfig.env === 'node' || (!masterConfig.env &&243typeof process !== "undefined" &&244process.versions &&245!!process.versions.node &&246!process.versions['node-webkit'])) {247//Using special require.nodeRequire, something added by r.js.248fs = require.nodeRequire('fs');249250text.get = function (url, callback, errback) {251try {252var file = fs.readFileSync(url, 'utf8');253//Remove BOM (Byte Mark Order) from utf8 files if it is there.254if (file.indexOf('\uFEFF') === 0) {255file = file.substring(1);256}257callback(file);258} catch (e) {259errback(e);260}261};262} else if (masterConfig.env === 'xhr' || (!masterConfig.env &&263text.createXhr())) {264text.get = function (url, callback, errback, headers) {265var xhr = text.createXhr(), header;266xhr.open('GET', url, true);267268//Allow plugins direct access to xhr headers269if (headers) {270for (header in headers) {271if (headers.hasOwnProperty(header)) {272xhr.setRequestHeader(header.toLowerCase(), headers[header]);273}274}275}276277//Allow overrides specified in config278if (masterConfig.onXhr) {279masterConfig.onXhr(xhr, url);280}281282xhr.onreadystatechange = function (evt) {283var status, err;284//Do not explicitly handle errors, those should be285//visible via console output in the browser.286if (xhr.readyState === 4) {287status = xhr.status;288if (status > 399 && status < 600) {289//An http 4xx or 5xx error. Signal an error.290err = new Error(url + ' HTTP status: ' + status);291err.xhr = xhr;292errback(err);293} else {294callback(xhr.responseText);295}296297if (masterConfig.onXhrComplete) {298masterConfig.onXhrComplete(xhr, url);299}300}301};302xhr.send(null);303};304} else if (masterConfig.env === 'rhino' || (!masterConfig.env &&305typeof Packages !== 'undefined' && typeof java !== 'undefined')) {306//Why Java, why is this so awkward?307text.get = function (url, callback) {308var stringBuffer, line,309encoding = "utf-8",310file = new java.io.File(url),311lineSeparator = java.lang.System.getProperty("line.separator"),312input = new java.io.BufferedReader(new java.io.InputStreamReader(new java.io.FileInputStream(file), encoding)),313content = '';314try {315stringBuffer = new java.lang.StringBuffer();316line = input.readLine();317318// Byte Order Mark (BOM) - The Unicode Standard, version 3.0, page 324319// http://www.unicode.org/faq/utf_bom.html320321// 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:322// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4508058323if (line && line.length() && line.charAt(0) === 0xfeff) {324// Eat the BOM, since we've already found the encoding on this file,325// and we plan to concatenating this buffer with others; the BOM should326// only appear at the top of a file.327line = line.substring(1);328}329330if (line !== null) {331stringBuffer.append(line);332}333334while ((line = input.readLine()) !== null) {335stringBuffer.append(lineSeparator);336stringBuffer.append(line);337}338//Make sure we return a JavaScript string and not a Java string.339content = String(stringBuffer.toString()); //String340} finally {341input.close();342}343callback(content);344};345} else if (masterConfig.env === 'xpconnect' || (!masterConfig.env &&346typeof Components !== 'undefined' && Components.classes &&347Components.interfaces)) {348//Avert your gaze!349Cc = Components.classes,350Ci = Components.interfaces;351Components.utils['import']('resource://gre/modules/FileUtils.jsm');352xpcIsWindows = ('@mozilla.org/windows-registry-key;1' in Cc);353354text.get = function (url, callback) {355var inStream, convertStream, fileObj,356readData = {};357358if (xpcIsWindows) {359url = url.replace(/\//g, '\\');360}361362fileObj = new FileUtils.File(url);363364//XPCOM, you so crazy365try {366inStream = Cc['@mozilla.org/network/file-input-stream;1']367.createInstance(Ci.nsIFileInputStream);368inStream.init(fileObj, 1, 0, false);369370convertStream = Cc['@mozilla.org/intl/converter-input-stream;1']371.createInstance(Ci.nsIConverterInputStream);372convertStream.init(inStream, "utf-8", inStream.available(),373Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER);374375convertStream.readString(inStream.available(), readData);376convertStream.close();377inStream.close();378callback(readData.value);379} catch (e) {380throw new Error((fileObj && fileObj.path || '') + ': ' + e);381}382};383}384return text;385});386387388