Skip to content
This repository has been archived by the owner on Feb 9, 2024. It is now read-only.

Commit

Permalink
Fix #72
Browse files Browse the repository at this point in the history
  • Loading branch information
jnewman committed Jun 6, 2014
1 parent 5c33c88 commit 971490e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
41 changes: 33 additions & 8 deletions postscribe.js
Expand Up @@ -6,6 +6,27 @@


(function() {
// Available options and defaults.
var OPTIONS = {
// Called when an async script has loaded.
afterAsync: doNothing,
// Called immediately before removing from the write queue.
afterDequeue: doNothing,
// Called sync after a stream's first thread release.
afterStreamStart: doNothing,
// Called after writing buffered document.write calls.
afterWrite: doNothing,
// Called immediately before adding to the write queue.
beforeEnqueue: doNothing,
// Called before writing buffered document.write calls.
beforeWrite: function(str) { return str; },
// Called when evaluation is finished.
done: doNothing,
// Called when a write results in an error.
error: function(e) { throw e; },
// Whether to let scripts w/ async attribute set fall out of the queue.
releaseAsync: false
};

var global = this;

Expand Down Expand Up @@ -85,6 +106,10 @@
}
}

var last = function(array) {
return array[array.length - 1];
};

// Test if token is a script tag.
function isScript(tok) {
return !tok || !('tagName' in tok) ? !1 : !!~tok.tagName.toLowerCase().indexOf('script');
Expand Down Expand Up @@ -452,6 +477,7 @@
var el = this.buildScript(tok);
var asyncRelease = this.shouldRelease(el);
var afterAsync = this.options.afterAsync;
var afterRelease = this.options.afterRelease;

if(tok.src) {
// Fix for attribute "SRC" (capitalized). IE does not recognize it.
Expand Down Expand Up @@ -563,8 +589,12 @@

function nextStream() {
var args = queue.shift();
var options;
if(args) {
options = last(args);
options.afterDequeue();
args.stream = runStream.apply(null, args);
options.afterStreamStart();
}
}

Expand Down Expand Up @@ -635,14 +665,7 @@
if(isFunction(options)) {
options = { done: options };
}
options = defaults(options, {
releaseAsync: false,
afterAsync: doNothing,
done: doNothing,
error: function(e) { throw e; },
beforeWrite: function(str) { return str; },
afterWrite: doNothing
});
options = defaults(options, OPTIONS);

el =
// id selector
Expand All @@ -664,7 +687,9 @@
}
};

options.beforeEnqueue(args);
queue.push(args);

if(!active) {
nextStream();
}
Expand Down
20 changes: 19 additions & 1 deletion test/test.js
@@ -1,6 +1,6 @@
/*jshint browser:true*/
/*globals $:false,module,setOptions,testWrite,asyncTest,postscribe,ok,start,skip,supports,test,stop*/
$(document).ready(function(){
$(document).ready(function() {

// style elements
// comments
Expand Down Expand Up @@ -647,5 +647,23 @@ $(document).ready(function(){
'</div>'
);
});


module('api');

var testCalled = function(desc, method) {
asyncTest(desc + ' ' + method, function() {
var options = {};
options[method] = function() {
ok(1);
start();
};
postscribe(document.body, '<script type="text/javascript" src="remote/set-global1.js"></script>', options);
});
};

testCalled('it calls', 'beforeEnqueue');
testCalled('it calls', 'afterDequeue');
testCalled('it calls', 'afterStreamStart');
});

0 comments on commit 971490e

Please sign in to comment.