You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

391 lines
15 KiB

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