text.js 12 KB

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