PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/src/loaders/blanket_cs.js

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