1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

reverted @kitgoncharov's commits that broke the browser runner.

This commit is contained in:
Jeremy Ashkenas 2011-04-30 13:33:28 -04:00
parent f1ad2e1fae
commit 0dfe2429bc
3 changed files with 43 additions and 93 deletions

File diff suppressed because one or more lines are too long

View file

@ -1,6 +1,5 @@
(function() { (function() {
var CoffeeScript, create, document, runScripts; var CoffeeScript, runScripts;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
CoffeeScript = require('./coffee-script'); CoffeeScript = require('./coffee-script');
CoffeeScript.require = require; CoffeeScript.require = require;
CoffeeScript.eval = function(code, options) { CoffeeScript.eval = function(code, options) {
@ -13,67 +12,41 @@
options.bare = true; options.bare = true;
return Function(CoffeeScript.compile(code, options))(); return Function(CoffeeScript.compile(code, options))();
}; };
document = typeof this.document !== 'undefined' ? document : null; if (typeof window === "undefined" || window === null) {
create = function() { return;
throw new Error('`XMLHttpRequest` is not supported.');
};
if (typeof this.ActiveXObject !== 'undefined') {
create = __bind(function() {
return new this.ActiveXObject('Microsoft.XMLHTTP');
}, this);
} else if (typeof this.XMLHttpRequest !== 'undefined') {
create = __bind(function() {
return new this.XMLHttpRequest;
}, this);
} }
CoffeeScript.load = function(url, options, callback) { CoffeeScript.load = function(url, options) {
var xhr; var xhr;
xhr = create(); xhr = new (window.ActiveXObject || XMLHttpRequest)('Microsoft.XMLHTTP');
xhr.open('GET', url, true); xhr.open('GET', url, true);
if ('overrideMimeType' in xhr) { if ('overrideMimeType' in xhr) {
xhr.overrideMimeType('text/plain'); xhr.overrideMimeType('text/plain');
} }
xhr.onreadystatechange = function() { xhr.onreadystatechange = function() {
var error, result;
if (xhr.readyState === 4) { if (xhr.readyState === 4) {
error = result = null; return CoffeeScript.run(xhr.responseText, options);
if (xhr.status === 200) {
try {
result = CoffeeScript.run(xhr.responseText);
} catch (error) {
error = exception;
}
} else {
error = new Error("An error occurred while loading the script `" + url + "`.");
}
return typeof callback === "function" ? callback(error, result) : void 0;
} }
}; };
return xhr.send(null); return xhr.send(null);
}; };
runScripts = function() { runScripts = function() {
var execute, index, length, scripts; var script, _i, _len, _ref;
scripts = document.getElementsByTagName('script'); _ref = document.getElementsByTagName('script');
index = 0; for (_i = 0, _len = _ref.length; _i < _len; _i++) {
length = scripts.length; script = _ref[_i];
(execute = function(error) { if (script.type === 'text/coffeescript') {
var script; if (script.src) {
if (error) { CoffeeScript.load(script.src);
throw error;
}
script = scripts[index++];
if (script.type === 'text/coffeescript' && script.src) {
return CoffeeScript.load(script.src, null, execute);
} else { } else {
CoffeeScript.run(script.innerHTML); CoffeeScript.run(script.innerHTML);
return execute();
} }
})(); }
}
return null; return null;
}; };
if (typeof this.addEventListener !== 'undefined') { if (window.addEventListener) {
this.addEventListener('DOMContentLoaded', runScripts, false); addEventListener('DOMContentLoaded', runScripts, false);
} else if (typeof this.attachEvent !== 'undefined') { } else {
this.attachEvent('onload', runScripts); attachEvent('onload', runScripts);
} }
}).call(this); }).call(this);

View file

@ -12,55 +12,32 @@ CoffeeScript.run = (code, options = {}) ->
options.bare = on options.bare = on
Function(CoffeeScript.compile code, options)() Function(CoffeeScript.compile code, options)()
# Capture a reference to the `document` object in the browser. # If we're not in a browser environment, we're finished with the public API.
document = if typeof @document isnt 'undefined' then document else null return unless window?
# Creates a new `XMLHttpRequest` object in IE and W3C-compliant browsers. # Load a remote script from the current domain via XHR.
create = -> throw new Error '`XMLHttpRequest` is not supported.' CoffeeScript.load = (url, options) ->
if typeof @ActiveXObject isnt 'undefined' xhr = new (window.ActiveXObject or XMLHttpRequest)('Microsoft.XMLHTTP')
create = => new @ActiveXObject 'Microsoft.XMLHTTP' xhr.open 'GET', url, true
else if typeof @XMLHttpRequest isnt 'undefined'
create = => new @XMLHttpRequest
# Load a remote script from the current domain via XHR. The optional `callback`
# function accepts two arguments: an error object if the script could not be
# loaded or failed to run, and the result of executing the script using
# `CoffeeScript.run`.
CoffeeScript.load = (url, options, callback) ->
xhr = create()
xhr.open 'GET', url, yes
xhr.overrideMimeType 'text/plain' if 'overrideMimeType' of xhr xhr.overrideMimeType 'text/plain' if 'overrideMimeType' of xhr
xhr.onreadystatechange = -> xhr.onreadystatechange = ->
if xhr.readyState is 4 CoffeeScript.run xhr.responseText, options if xhr.readyState is 4
error = result = null
if xhr.status is 200
try result = CoffeeScript.run xhr.responseText catch error then error = exception
else
error = new Error "An error occurred while loading the script `#{url}`."
callback? error, result
xhr.send null xhr.send null
# In the browser, the CoffeeScript compiler will asynchronously load, compile, # Activate CoffeeScript in the browser by having it compile and evaluate
# and evaluate all `script` elements with a content type of # all script tags with a content-type of `text/coffeescript`.
# `text/coffeescript`. The scripts are loaded and executed in order on page # This happens on page load.
# load.
runScripts = -> runScripts = ->
scripts = document.getElementsByTagName 'script' for script in document.getElementsByTagName 'script'
index = 0 if script.type is 'text/coffeescript'
length = scripts.length if script.src
(execute = (error) -> CoffeeScript.load script.src
throw error if error
script = scripts[index++]
if script.type is 'text/coffeescript' and script.src
CoffeeScript.load script.src, null, execute
else else
CoffeeScript.run script.innerHTML CoffeeScript.run script.innerHTML
execute()
)()
null null
# Execute scripts on page load in W3C-compliant browsers and IE. # Listen for window load, both in browsers and in IE.
if typeof @addEventListener isnt 'undefined' if window.addEventListener
@addEventListener 'DOMContentLoaded', runScripts, no addEventListener 'DOMContentLoaded', runScripts, no
else if typeof @attachEvent isnt 'undefined' else
@attachEvent 'onload', runScripts attachEvent 'onload', runScripts