Permalink
Browse files

allowing JS in slowparse (Experimental)

  • Loading branch information...
Pomax committed Sep 30, 2012
1 parent c6a7104 commit a646bde35a81f386997c6866b0d108d48163e264
Showing with 52 additions and 1 deletion.
  1. +49 −1 slowparse.js
  2. +3 −0 tree-inspectors.js
View
@@ -924,6 +924,47 @@ var Slowparse = (function() {
}
+ // ### Script Parsing
+ //
+ // `scriptParser` is our internal script token stream parser object. This
+ // object primarily glosses over the content of `<script>...</script>`
+ // blocks.
+ function ScriptParser(stream, domBuilder) {
+ this.stream = stream;
+ this.domBuilder = domBuilder;
+ }
+
+ ScriptParser.prototype = {
+ parse: function() {
+ var target = "</script>",
+ sliceStart = this.stream.pos;
+
+ while(!this.stream.end() && this.stream.substream(target.length)!==target) {
+ this.stream.next();
+ }
+
+ if(this.stream.substream(target.length)!==target) {
+ for(var i=0; i<target.length; i++) {
+ this.stream.next();
+ }
+ }
+
+ var sliceEnd = this.stream.pos;
+
+ // form content object
+ var scriptBlock = {
+ parseInfo: {
+ start: sliceStart,
+ end: sliceEnd,
+ text: this.stream.text.slice(sliceStart, sliceEnd)
+ }
+ };
+
+ return scriptBlock;
+ }
+ };
+
+
// ### HTML Parsing
//
// The HTML token stream parser object has references to the stream,
@@ -933,6 +974,7 @@ var Slowparse = (function() {
this.stream = stream;
this.domBuilder = domBuilder;
this.cssParser = new CSSParser(stream, domBuilder);
+ this.scriptParser = new ScriptParser(stream, domBuilder);
}
HTMLParser.prototype = {
@@ -1207,10 +1249,16 @@ var Slowparse = (function() {
// If the opening tag represents a `<textarea>` element, we need
// to parse all its contents as CDATA (unparsed character data)
- if (tagName && tagName === "textarea") {
+ if (!this.stream.end() && tagName === "textarea") {
this._parseCDATA("textarea");
}
+ // If the opening tag represents a `<script>` element, we
+ // forward the stream until we find the closing `</script>`
+ if (!this.stream.end() && tagName === "script") {
+ var scriptBlock = this.scriptParser.parse();
+ }
+
return;
} else
throw new ParseError("UNTERMINATED_OPEN_TAG", this);
View
@@ -73,12 +73,15 @@ var TreeInspectors = (function() {
// This is a Slowparse error detector that returns an error object
// if the given document fragment contains any JS.
forbidJS: function(html, doc) {
+ return null;
+/*
var js = TreeInspectors.findJS(doc);
if (!js.length)
return null;
var error = JSON.parse(JSON.stringify(js[0].node.parseInfo));
error.type = js[0].type + "_NOT_ALLOWED";
return error;
+*/
}
};

0 comments on commit a646bde

Please sign in to comment.