Skip to content

Commit

Permalink
Release 1.4.0
Browse files Browse the repository at this point in the history
Fix OPTIONS to options
  • Loading branch information
jnewman committed Apr 25, 2015
1 parent 4de73f9 commit 228335e
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 28 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ We use jshint to do static analysis of the javascript and keep things smelling g
# History
Postscribe uses [software versioning standards](http://semver.org) as follows: major.new.maintenance[.trivial]. There are git tags for each release if you would like to see older versions.

##### 1.4.0
* Fix incomplete tags blocking UI thread
* beforeWriteToken hook
* Handle boolean attrs
* Comments fix
* Make autoFix a postscribe option

##### 1.3.2
* Screwed up the tagging. (automation coming soon...)
Expand Down
84 changes: 62 additions & 22 deletions dist/postscribe.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Asynchronously write javascript, even with document.write., v1.3.2 https://krux.github.io/postscribe
Copyright (c) 2014 Derek Brans, MIT license https://github.com/krux/postscribe/blob/master/LICENSE */// An html parser written in JavaScript
/* Asynchronously write javascript, even with document.write., v1.4.0 https://krux.github.io/postscribe
Copyright (c) 2015 Derek Brans, MIT license https://github.com/krux/postscribe/blob/master/LICENSE */// An html parser written in JavaScript
// Based on http://ejohn.org/blog/pure-javascript-html-parser/
//TODO(#39)
/*globals console:false*/
Expand All @@ -25,7 +25,7 @@ Copyright (c) 2014 Derek Brans, MIT license https://github.com/krux/postscribe/b
// Regular Expressions for parsing tags and attributes
var startTag = /^<([\-A-Za-z0-9_]+)((?:\s+[\w\-]+(?:\s*=?\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*)\s*(\/?)>/;
var endTag = /^<\/([\-A-Za-z0-9_]+)[^>]*>/;
var attr = /([\-A-Za-z0-9_]+)(?:\s*=\s*(?:(?:"((?:\\.|[^"])*)")|(?:'((?:\\.|[^'])*)')|([^>\s]+)))?/g;
var attr = /(?:([\-A-Za-z0-9_]+)\s*=\s*(?:(?:"((?:\\.|[^"])*)")|(?:'((?:\\.|[^'])*)')|([^>\s]+)))|(?:([\-A-Za-z0-9_]+)(\s|$)+)/g;
var fillAttr = /^(checked|compact|declare|defer|disabled|ismap|multiple|nohref|noresize|noshade|nowrap|readonly|selected)$/i;

var DEBUG = false;
Expand Down Expand Up @@ -135,17 +135,28 @@ Copyright (c) 2014 Derek Brans, MIT license https://github.com/krux/postscribe/b

if ( match ) {
var attrs = {};
var booleanAttrs = {};
var rest = match[2];

match[2].replace(attr, function(match, name) {
var value = arguments[2] || arguments[3] || arguments[4] ||
fillAttr.test(name) && name || null;

attrs[name] = unescapeHTMLEntities(value);
if (!(arguments[2] || arguments[3] || arguments[4] || arguments[5])) {
attrs[name] = null;
} else if (arguments[5]) {
attrs[arguments[5]] = '';
booleanAttrs[name] = true;
} else {
var value = arguments[2] || arguments[3] || arguments[4] ||
fillAttr.test(name) && name || '';
attrs[name] = unescapeHTMLEntities(value);
}
rest = rest.replace(match, '');
});

return {
tagName: match[1],
attrs: attrs,
booleanAttrs: booleanAttrs,
rest: rest,
unary: !!match[3],
length: match[0].length
};
Expand Down Expand Up @@ -238,6 +249,7 @@ Copyright (c) 2014 Derek Brans, MIT license https://github.com/krux/postscribe/b
if(tok && tok.type === 'startTag') {
// unary
tok.unary = EMPTY.test(tok.tagName) || tok.unary;
tok.html5Unary = !/\/>$/.test(tok.text);
}
return tok;
};
Expand Down Expand Up @@ -331,25 +343,32 @@ Copyright (c) 2014 Derek Brans, MIT license https://github.com/krux/postscribe/b
htmlParser.tokenToString = function(tok) {
var handler = {
comment: function(tok) {
return '<--' + tok.content + '-->';
return '<!--' + tok.content;
},
endTag: function(tok) {
return '</'+tok.tagName+'>';
},
atomicTag: function(tok) {
console.log(tok);
if(DEBUG) { console.log(tok); }
return handler.startTag(tok) +
tok.content +
handler.endTag(tok);
},
startTag: function(tok) {
var str = '<'+tok.tagName;
for (var key in tok.attrs) {
str += ' '+key;

var val = tok.attrs[key];
// escape quotes
str += ' '+key+'="'+(val ? val.replace(/(^|[^\\])"/g, '$1\\\"') : '')+'"';
if (typeof tok.booleanAttrs == 'undefined' || typeof tok.booleanAttrs[key] == 'undefined') {
// escape quotes
str += '="'+(val ? val.replace(/(^|[^\\])"/g, '$1\\\"') : '')+'"';
}
}
if (tok.rest) {
str += tok.rest;
}
return str + (tok.unary ? '/>' : '>');
return str + (tok.unary && !tok.html5Unary ? '/>' : '>');
},
chars: function(tok) {
return tok.text;
Expand All @@ -376,8 +395,8 @@ Copyright (c) 2014 Derek Brans, MIT license https://github.com/krux/postscribe/b
this.htmlParser = htmlParser;
})();

// postscribe.js 1.3.2
// (c) Copyright 2012 to the present, Krux
// postscribe.js 1.4.0
// (c) Copyright 2012-2015 to the present, Krux
// postscribe is freely distributable under the MIT license.
// For all details and documentation:
// http://krux.github.io/postscribe
Expand All @@ -396,8 +415,12 @@ Copyright (c) 2014 Derek Brans, MIT license https://github.com/krux/postscribe/b
afterStreamStart: doNothing,
// Called after writing buffered document.write calls.
afterWrite: doNothing,
// Allows disabling the autoFix feature of htmlParser
autoFix: true,
// Called immediately before adding to the write queue.
beforeEnqueue: doNothing,
// Called before writing a token.
beforeWriteToken: function(tok) { return tok; },
// Called before writing buffered document.write calls.
beforeWrite: function(str) { return str; },
// Called when evaluation is finished.
Expand Down Expand Up @@ -548,7 +571,7 @@ Copyright (c) 2014 Derek Brans, MIT license https://github.com/krux/postscribe/b

doc: doc,

parser: htmlParser('', { autoFix: true }),
parser: htmlParser('', { autoFix: options.autoFix }),

// Actual elements by id.
actuals: [root],
Expand Down Expand Up @@ -600,7 +623,11 @@ Copyright (c) 2014 Derek Brans, MIT license https://github.com/krux/postscribe/b

// stop if we see a script token
while((tok = this.parser.readToken()) && !(script=isScript(tok)) && !(style=isStyle(tok))) {
tokens.push(tok);
tok = this.options.beforeWriteToken(tok);

if (tok) {
tokens.push(tok);
}
}

this.writeStaticTokens(tokens);
Expand Down Expand Up @@ -654,7 +681,9 @@ Copyright (c) 2014 Derek Brans, MIT license https://github.com/krux/postscribe/b

each(tokens, function(tok) {

raw.push(tok.text);
var tokenRaw = htmlParser.tokenToString(tok);

raw.push(tokenRaw);

if(tok.attrs) { // tok.attrs <==> startTag or atomicTag or cursor
// Ignore noscript tags. They are atomic, so we don't have to worry about children.
Expand All @@ -663,7 +692,7 @@ Copyright (c) 2014 Derek Brans, MIT license https://github.com/krux/postscribe/b

// Actual: inject id attribute: replace '>' at end of start tag with id attribute + '>'
actual.push(
tok.text.replace(/(\/?>)/, ' '+BASEATTR+'id='+id+' $1')
tokenRaw.replace(/(\/?>)/, ' '+BASEATTR+'id='+id+' $1')
);

// Don't proxy scripts: they have no bearing on DOM structure.
Expand All @@ -680,9 +709,9 @@ Copyright (c) 2014 Derek Brans, MIT license https://github.com/krux/postscribe/b
} else {
// Visit any other type of token
// Actual: append.
actual.push(tok.text);
actual.push(tokenRaw);
// Proxy: append endTags. Ignore everything else.
proxy.push(tok.type === 'endTag' ? tok.text : '');
proxy.push(tok.type === 'endTag' ? tokenRaw : '');
}
});

Expand Down Expand Up @@ -737,6 +766,13 @@ Copyright (c) 2014 Derek Brans, MIT license https://github.com/krux/postscribe/b
//noinspection JSUnresolvedVariable
tok.src = tok.attrs.src || tok.attrs.SRC;

tok = this.options.beforeWriteToken(tok);

if(!tok) {
// User has removed this token
return;
}

if(tok.src && this.scriptStack.length) {
// Defer this script until scriptStack is empty.
// Assumption 1: This script will not start executing until
Expand Down Expand Up @@ -765,8 +801,12 @@ Copyright (c) 2014 Derek Brans, MIT license https://github.com/krux/postscribe/b

tok.type = tok.attrs.type || tok.attrs.TYPE || 'text/css';

// Put the style node in the DOM.
this.writeStyleToken(tok);
tok = this.options.beforeWriteToken(tok);

if(tok) {
// Put the style node in the DOM.
this.writeStyleToken(tok);
}

if(remainder) {
this.write();
Expand Down
Loading

0 comments on commit 228335e

Please sign in to comment.