text.js 12 KB

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