Add support for text/literate-coffeescript in the browser

This commit is contained in:
Sean B. Palmer 2013-02-25 16:43:59 +00:00
parent c39723c053
commit 881ae5528d
2 changed files with 28 additions and 16 deletions

View File

@ -1,6 +1,7 @@
// Generated by CoffeeScript 1.5.0 // Generated by CoffeeScript 1.5.0
(function() { (function() {
var CoffeeScript, runScripts; var CoffeeScript, runScripts,
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
CoffeeScript = require('./coffee-script'); CoffeeScript = require('./coffee-script');
@ -29,8 +30,11 @@
return; return;
} }
CoffeeScript.load = function(url, callback) { CoffeeScript.load = function(url, callback, options) {
var xhr; var xhr;
if (options == null) {
options = {};
}
xhr = window.ActiveXObject ? new window.ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest(); xhr = window.ActiveXObject ? new window.ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
xhr.open('GET', url, true); xhr.open('GET', url, true);
if ('overrideMimeType' in xhr) { if ('overrideMimeType' in xhr) {
@ -40,7 +44,7 @@
var _ref; var _ref;
if (xhr.readyState === 4) { if (xhr.readyState === 4) {
if ((_ref = xhr.status) === 0 || _ref === 200) { if ((_ref = xhr.status) === 0 || _ref === 200) {
CoffeeScript.run(xhr.responseText); CoffeeScript.run(xhr.responseText, options);
} else { } else {
throw new Error("Could not load " + url); throw new Error("Could not load " + url);
} }
@ -53,14 +57,15 @@
}; };
runScripts = function() { runScripts = function() {
var coffees, execute, index, length, s, scripts; var coffees, coffeetypes, execute, index, length, s, scripts;
scripts = document.getElementsByTagName('script'); scripts = document.getElementsByTagName('script');
coffeetypes = ['text/coffeescript', 'text/literate-coffeescript'];
coffees = (function() { coffees = (function() {
var _i, _len, _results; var _i, _len, _ref, _results;
_results = []; _results = [];
for (_i = 0, _len = scripts.length; _i < _len; _i++) { for (_i = 0, _len = scripts.length; _i < _len; _i++) {
s = scripts[_i]; s = scripts[_i];
if (s.type === 'text/coffeescript') { if (_ref = s.type, __indexOf.call(coffeetypes, _ref) >= 0) {
_results.push(s); _results.push(s);
} }
} }
@ -69,13 +74,17 @@
index = 0; index = 0;
length = coffees.length; length = coffees.length;
(execute = function() { (execute = function() {
var script; var mediatype, options, script;
script = coffees[index++]; script = coffees[index++];
if ((script != null ? script.type : void 0) === 'text/coffeescript') { mediatype = script != null ? script.type : void 0;
if (__indexOf.call(coffeetypes, mediatype) >= 0) {
options = {
literate: mediatype === 'text/literate-coffeescript'
};
if (script.src) { if (script.src) {
return CoffeeScript.load(script.src, execute); return CoffeeScript.load(script.src, execute, options);
} else { } else {
CoffeeScript.run(script.innerHTML); CoffeeScript.run(script.innerHTML, options);
return execute(); return execute();
} }
} }

View File

@ -17,7 +17,7 @@ CoffeeScript.run = (code, options = {}) ->
return unless window? return unless window?
# Load a remote script from the current domain via XHR. # Load a remote script from the current domain via XHR.
CoffeeScript.load = (url, callback) -> CoffeeScript.load = (url, callback, options = {}) ->
xhr = if window.ActiveXObject xhr = if window.ActiveXObject
new window.ActiveXObject('Microsoft.XMLHTTP') new window.ActiveXObject('Microsoft.XMLHTTP')
else else
@ -27,7 +27,7 @@ CoffeeScript.load = (url, callback) ->
xhr.onreadystatechange = -> xhr.onreadystatechange = ->
if xhr.readyState is 4 if xhr.readyState is 4
if xhr.status in [0, 200] if xhr.status in [0, 200]
CoffeeScript.run xhr.responseText CoffeeScript.run xhr.responseText, options
else else
throw new Error "Could not load #{url}" throw new Error "Could not load #{url}"
callback() if callback callback() if callback
@ -38,16 +38,19 @@ CoffeeScript.load = (url, callback) ->
# This happens on page load. # This happens on page load.
runScripts = -> runScripts = ->
scripts = document.getElementsByTagName 'script' scripts = document.getElementsByTagName 'script'
coffees = (s for s in scripts when s.type is 'text/coffeescript') coffeetypes = ['text/coffeescript', 'text/literate-coffeescript']
coffees = (s for s in scripts when s.type in coffeetypes)
index = 0 index = 0
length = coffees.length length = coffees.length
do execute = -> do execute = ->
script = coffees[index++] script = coffees[index++]
if script?.type is 'text/coffeescript' mediatype = script?.type
if mediatype in coffeetypes
options = {literate: mediatype is 'text/literate-coffeescript'}
if script.src if script.src
CoffeeScript.load script.src, execute CoffeeScript.load script.src, execute, options
else else
CoffeeScript.run script.innerHTML CoffeeScript.run script.innerHTML, options
execute() execute()
null null