cs.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /**
  2. * @license cs 0.5.0 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/require-cs for details
  5. */
  6. /*jslint */
  7. /*global define, window, XMLHttpRequest, importScripts, Packages, java,
  8. ActiveXObject, process, require */
  9. /**
  10. *
  11. * Base64 encode / decode
  12. * http://www.webtoolkit.info/
  13. *
  14. **/
  15. var Base64 = {
  16. // private property
  17. _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
  18. // public method for encoding
  19. encode : function (input) {
  20. var output = "";
  21. var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
  22. var i = 0;
  23. input = Base64._utf8_encode(input);
  24. while (i < input.length) {
  25. chr1 = input.charCodeAt(i++);
  26. chr2 = input.charCodeAt(i++);
  27. chr3 = input.charCodeAt(i++);
  28. enc1 = chr1 >> 2;
  29. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  30. enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  31. enc4 = chr3 & 63;
  32. if (isNaN(chr2)) {
  33. enc3 = enc4 = 64;
  34. } else if (isNaN(chr3)) {
  35. enc4 = 64;
  36. }
  37. output = output +
  38. this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
  39. this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
  40. }
  41. return output;
  42. },
  43. // public method for decoding
  44. decode : function (input) {
  45. var output = "";
  46. var chr1, chr2, chr3;
  47. var enc1, enc2, enc3, enc4;
  48. var i = 0;
  49. input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
  50. while (i < input.length) {
  51. enc1 = this._keyStr.indexOf(input.charAt(i++));
  52. enc2 = this._keyStr.indexOf(input.charAt(i++));
  53. enc3 = this._keyStr.indexOf(input.charAt(i++));
  54. enc4 = this._keyStr.indexOf(input.charAt(i++));
  55. chr1 = (enc1 << 2) | (enc2 >> 4);
  56. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  57. chr3 = ((enc3 & 3) << 6) | enc4;
  58. output = output + String.fromCharCode(chr1);
  59. if (enc3 != 64) {
  60. output = output + String.fromCharCode(chr2);
  61. }
  62. if (enc4 != 64) {
  63. output = output + String.fromCharCode(chr3);
  64. }
  65. }
  66. output = Base64._utf8_decode(output);
  67. return output;
  68. },
  69. // private method for UTF-8 encoding
  70. _utf8_encode : function (string) {
  71. string = string.replace(/\r\n/g,"\n");
  72. var utftext = "";
  73. for (var n = 0; n < string.length; n++) {
  74. var c = string.charCodeAt(n);
  75. if (c < 128) {
  76. utftext += String.fromCharCode(c);
  77. }
  78. else if((c > 127) && (c < 2048)) {
  79. utftext += String.fromCharCode((c >> 6) | 192);
  80. utftext += String.fromCharCode((c & 63) | 128);
  81. }
  82. else {
  83. utftext += String.fromCharCode((c >> 12) | 224);
  84. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  85. utftext += String.fromCharCode((c & 63) | 128);
  86. }
  87. }
  88. return utftext;
  89. },
  90. // private method for UTF-8 decoding
  91. _utf8_decode : function (utftext) {
  92. var string = "";
  93. var i = 0;
  94. var c = c1 = c2 = 0;
  95. while ( i < utftext.length ) {
  96. c = utftext.charCodeAt(i);
  97. if (c < 128) {
  98. string += String.fromCharCode(c);
  99. i++;
  100. }
  101. else if((c > 191) && (c < 224)) {
  102. c2 = utftext.charCodeAt(i+1);
  103. string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
  104. i += 2;
  105. }
  106. else {
  107. c2 = utftext.charCodeAt(i+1);
  108. c3 = utftext.charCodeAt(i+2);
  109. string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
  110. i += 3;
  111. }
  112. }
  113. return string;
  114. }
  115. }
  116. define(['coffee-script'], function (CoffeeScript) {
  117. 'use strict';
  118. var fs, getXhr,
  119. progIds = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'],
  120. fetchText = function () {
  121. throw new Error('Environment unsupported.');
  122. },
  123. buildMap = {};
  124. if (typeof process !== "undefined" &&
  125. process.versions &&
  126. !!process.versions.node) {
  127. //Using special require.nodeRequire, something added by r.js.
  128. fs = require.nodeRequire('fs');
  129. fetchText = function (path, callback) {
  130. callback(fs.readFileSync(path, 'utf8'));
  131. };
  132. } else if ((typeof window !== "undefined" && window.navigator && window.document) || typeof importScripts !== "undefined") {
  133. // Browser action
  134. getXhr = function () {
  135. //Would love to dump the ActiveX crap in here. Need IE 6 to die first.
  136. var xhr, i, progId;
  137. if (typeof XMLHttpRequest !== "undefined") {
  138. return new XMLHttpRequest();
  139. } else {
  140. for (i = 0; i < 3; i += 1) {
  141. progId = progIds[i];
  142. try {
  143. xhr = new ActiveXObject(progId);
  144. } catch (e) {}
  145. if (xhr) {
  146. progIds = [progId]; // so faster next time
  147. break;
  148. }
  149. }
  150. }
  151. if (!xhr) {
  152. throw new Error("getXhr(): XMLHttpRequest not available");
  153. }
  154. return xhr;
  155. };
  156. fetchText = function (url, callback) {
  157. var xhr = getXhr();
  158. xhr.open('GET', url, true);
  159. xhr.onreadystatechange = function (evt) {
  160. //Do not explicitly handle errors, those should be
  161. //visible via console output in the browser.
  162. if (xhr.readyState === 4) {
  163. callback(xhr.responseText);
  164. }
  165. };
  166. xhr.send(null);
  167. };
  168. // end browser.js adapters
  169. } else if (typeof Packages !== 'undefined') {
  170. //Why Java, why is this so awkward?
  171. fetchText = function (path, callback) {
  172. var stringBuffer, line,
  173. encoding = "utf-8",
  174. file = new java.io.File(path),
  175. lineSeparator = java.lang.System.getProperty("line.separator"),
  176. input = new java.io.BufferedReader(new java.io.InputStreamReader(new java.io.FileInputStream(file), encoding)),
  177. content = '';
  178. try {
  179. stringBuffer = new java.lang.StringBuffer();
  180. line = input.readLine();
  181. // Byte Order Mark (BOM) - The Unicode Standard, version 3.0, page 324
  182. // http://www.unicode.org/faq/utf_bom.html
  183. // 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:
  184. // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4508058
  185. if (line && line.length() && line.charAt(0) === 0xfeff) {
  186. // Eat the BOM, since we've already found the encoding on this file,
  187. // and we plan to concatenating this buffer with others; the BOM should
  188. // only appear at the top of a file.
  189. line = line.substring(1);
  190. }
  191. stringBuffer.append(line);
  192. while ((line = input.readLine()) !== null) {
  193. stringBuffer.append(lineSeparator);
  194. stringBuffer.append(line);
  195. }
  196. //Make sure we return a JavaScript string and not a Java string.
  197. content = String(stringBuffer.toString()); //String
  198. } finally {
  199. input.close();
  200. }
  201. callback(content);
  202. };
  203. }
  204. return {
  205. fetchText: fetchText,
  206. get: function () {
  207. return CoffeeScript;
  208. },
  209. write: function (pluginName, name, write) {
  210. if (buildMap.hasOwnProperty(name)) {
  211. var text = buildMap[name];
  212. write.asModule(pluginName + "!" + name, text);
  213. }
  214. },
  215. version: '0.4.3',
  216. load: function (name, parentRequire, load, config) {
  217. // preserve existing logic with new literate coffeescript extensions (*.litcoffee or *.coffee.md).
  218. // if name passes check, use it, as-is. otherwise, behave as before, appending .coffee to the
  219. // requirejs binding.
  220. var fullName = CoffeeScript.helpers.isCoffee(name) ? name : name + '.coffee';
  221. var path = parentRequire.toUrl(fullName);
  222. fetchText(path, function (text) {
  223. // preserve existing logic. integrate new 'literate' compile flag with any requirejs configs.
  224. var opts = config.CoffeeScript || {};
  225. opts.literate = CoffeeScript.helpers.isLiterate(fullName);
  226. opts.sourceMap = true;
  227. opts.header = true;
  228. opts.inline = true;
  229. opts.sourceFiles = [name + opts.literate ? '' : '.coffee'];
  230. opts.generatedFile = name + opts.literate ? '' : '.coffee';
  231. var compiled;
  232. //Do CoffeeScript transform.
  233. try {
  234. compiled = CoffeeScript.compile(text, opts);
  235. } catch (err) {
  236. err.message = "In " + path + ", " + err.message;
  237. throw err;
  238. }
  239. text = compiled.js;
  240. //IE with conditional comments on cannot handle the
  241. //sourceURL trick, so skip it if enabled.
  242. /*@if (@_jscript) @else @*/
  243. if (!config.isBuild) {
  244. text += '\n//# sourceMappingURL=data:application/json;base64,' + Base64.encode(compiled.v3SourceMap || '') + '\n//# sourceURL=' + path;
  245. }
  246. /*@end@*/
  247. //Hold on to the transformed text if a build.
  248. if (config.isBuild) {
  249. buildMap[name] = text;
  250. }
  251. load.fromText(name, text);
  252. //Give result to load. Need to wait until the module
  253. //is fully parse, which will happen after this
  254. //execution.
  255. parentRequire([name], function (value) {
  256. load(value);
  257. });
  258. });
  259. }
  260. };
  261. });