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:
parent
f1ad2e1fae
commit
0dfe2429bc
3 changed files with 43 additions and 93 deletions
File diff suppressed because one or more lines are too long
|
@ -1,6 +1,5 @@
|
|||
(function() {
|
||||
var CoffeeScript, create, document, runScripts;
|
||||
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
|
||||
var CoffeeScript, runScripts;
|
||||
CoffeeScript = require('./coffee-script');
|
||||
CoffeeScript.require = require;
|
||||
CoffeeScript.eval = function(code, options) {
|
||||
|
@ -13,67 +12,41 @@
|
|||
options.bare = true;
|
||||
return Function(CoffeeScript.compile(code, options))();
|
||||
};
|
||||
document = typeof this.document !== 'undefined' ? document : null;
|
||||
create = function() {
|
||||
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);
|
||||
if (typeof window === "undefined" || window === null) {
|
||||
return;
|
||||
}
|
||||
CoffeeScript.load = function(url, options, callback) {
|
||||
CoffeeScript.load = function(url, options) {
|
||||
var xhr;
|
||||
xhr = create();
|
||||
xhr = new (window.ActiveXObject || XMLHttpRequest)('Microsoft.XMLHTTP');
|
||||
xhr.open('GET', url, true);
|
||||
if ('overrideMimeType' in xhr) {
|
||||
xhr.overrideMimeType('text/plain');
|
||||
}
|
||||
xhr.onreadystatechange = function() {
|
||||
var error, result;
|
||||
if (xhr.readyState === 4) {
|
||||
error = result = null;
|
||||
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 CoffeeScript.run(xhr.responseText, options);
|
||||
}
|
||||
};
|
||||
return xhr.send(null);
|
||||
};
|
||||
runScripts = function() {
|
||||
var execute, index, length, scripts;
|
||||
scripts = document.getElementsByTagName('script');
|
||||
index = 0;
|
||||
length = scripts.length;
|
||||
(execute = function(error) {
|
||||
var script;
|
||||
if (error) {
|
||||
throw error;
|
||||
}
|
||||
script = scripts[index++];
|
||||
if (script.type === 'text/coffeescript' && script.src) {
|
||||
return CoffeeScript.load(script.src, null, execute);
|
||||
var script, _i, _len, _ref;
|
||||
_ref = document.getElementsByTagName('script');
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
script = _ref[_i];
|
||||
if (script.type === 'text/coffeescript') {
|
||||
if (script.src) {
|
||||
CoffeeScript.load(script.src);
|
||||
} else {
|
||||
CoffeeScript.run(script.innerHTML);
|
||||
return execute();
|
||||
}
|
||||
})();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
if (typeof this.addEventListener !== 'undefined') {
|
||||
this.addEventListener('DOMContentLoaded', runScripts, false);
|
||||
} else if (typeof this.attachEvent !== 'undefined') {
|
||||
this.attachEvent('onload', runScripts);
|
||||
if (window.addEventListener) {
|
||||
addEventListener('DOMContentLoaded', runScripts, false);
|
||||
} else {
|
||||
attachEvent('onload', runScripts);
|
||||
}
|
||||
}).call(this);
|
||||
|
|
|
@ -12,55 +12,32 @@ CoffeeScript.run = (code, options = {}) ->
|
|||
options.bare = on
|
||||
Function(CoffeeScript.compile code, options)()
|
||||
|
||||
# Capture a reference to the `document` object in the browser.
|
||||
document = if typeof @document isnt 'undefined' then document else null
|
||||
# If we're not in a browser environment, we're finished with the public API.
|
||||
return unless window?
|
||||
|
||||
# Creates a new `XMLHttpRequest` object in IE and W3C-compliant browsers.
|
||||
create = -> throw new Error '`XMLHttpRequest` is not supported.'
|
||||
if typeof @ActiveXObject isnt 'undefined'
|
||||
create = => new @ActiveXObject 'Microsoft.XMLHTTP'
|
||||
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
|
||||
# Load a remote script from the current domain via XHR.
|
||||
CoffeeScript.load = (url, options) ->
|
||||
xhr = new (window.ActiveXObject or XMLHttpRequest)('Microsoft.XMLHTTP')
|
||||
xhr.open 'GET', url, true
|
||||
xhr.overrideMimeType 'text/plain' if 'overrideMimeType' of xhr
|
||||
xhr.onreadystatechange = ->
|
||||
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
|
||||
CoffeeScript.run xhr.responseText, options if xhr.readyState is 4
|
||||
xhr.send null
|
||||
|
||||
# In the browser, the CoffeeScript compiler will asynchronously load, compile,
|
||||
# and evaluate all `script` elements with a content type of
|
||||
# `text/coffeescript`. The scripts are loaded and executed in order on page
|
||||
# load.
|
||||
# Activate CoffeeScript in the browser by having it compile and evaluate
|
||||
# all script tags with a content-type of `text/coffeescript`.
|
||||
# This happens on page load.
|
||||
runScripts = ->
|
||||
scripts = document.getElementsByTagName 'script'
|
||||
index = 0
|
||||
length = scripts.length
|
||||
(execute = (error) ->
|
||||
throw error if error
|
||||
script = scripts[index++]
|
||||
if script.type is 'text/coffeescript' and script.src
|
||||
CoffeeScript.load script.src, null, execute
|
||||
for script in document.getElementsByTagName 'script'
|
||||
if script.type is 'text/coffeescript'
|
||||
if script.src
|
||||
CoffeeScript.load script.src
|
||||
else
|
||||
CoffeeScript.run script.innerHTML
|
||||
execute()
|
||||
)()
|
||||
null
|
||||
|
||||
# Execute scripts on page load in W3C-compliant browsers and IE.
|
||||
if typeof @addEventListener isnt 'undefined'
|
||||
@addEventListener 'DOMContentLoaded', runScripts, no
|
||||
else if typeof @attachEvent isnt 'undefined'
|
||||
@attachEvent 'onload', runScripts
|
||||
# Listen for window load, both in browsers and in IE.
|
||||
if window.addEventListener
|
||||
addEventListener 'DOMContentLoaded', runScripts, no
|
||||
else
|
||||
attachEvent 'onload', runScripts
|
||||
|
|
Loading…
Reference in a new issue