-Demo of HTML5 Parsing Algorithm with Scripting Enabled
+Live Scripting HTML Parser
+
+
');
@@ -224,12 +282,85 @@
this.openElements[this.openElements.length - 1].appendChild (el);
// 11. Let the insertion point have the value of the old ...
+
oldInsertionPoint += this.insertionPoint;
this.setInsertionPoint (oldInsertionPoint);
// 12. If there is a script that will execute as soon as ...
-
+ while (this.scriptExecutedWhenParserResumes) {
+ // 12.1. If the tree construction stage is being called reentrantly
+ if (this.reentrant) {
+ log ('parse: abort (reentrance)');
+ logIndentLevel--;
+ return;
+
+ // 12.2. Otherwise
+ } else {
+ // 1.
+ var script = this.scriptExecutedWhenParserResumes;
+ this.scriptExecutedWhenParserResumes = null;
+
+ // 2. Pause until the script has completed loading.
+ //
+
+ // 3. Let the insertion point to just before the next input char.
+ this.setInsertionPoint (0);
+
+ // 4. Execute the script.
+ executeScript (this.doc, script);
+
+ // 5. Let the insertion point be undefined again.
+ this.setInsertionPoint (undefined);
+
+ // 6. If there is once again a script that will execute ...
+ //
+ }
+ }
+ } else if (token.value == 'style' ||
+ token.value == 'noscript' ||
+ token.value == 'xmp') {
+ // 1. Create an element for the token in the HTML namespace.
+ var el = new JSElement (this.doc, token.value);
+
+ // 2. Append the new element to the current node.
+ this.openElements[this.openElements.length - 1].appendChild (el);
+ // 3. Switch the tokeniser's content model flag to the CDATA state.
+ this.parseMode = 'cdata';
+ this.endTagName = token.value;
+
+ // 4.1. Collect all the character tokens.
+ while (true) {
+ var token = this.getNextToken ();
+ log ('token: ' + token.type + ' "' + token.value + '"');
+
+ if (token.type == 'char') {
+ // 5. Append a single Text node to the script element node.
+ el.manakaiAppendText (token.value);
+
+ // 4.2. Until it returns a token that is not a character token, or
+ // until it stops tokenising.
+ } else if (token.type == 'eof' ||
+ token.type == 'end-tag' ||
+ token.type == 'abort') {
+ // 6. Switched back to the PCDATA state.
+ this.parseMode = 'pcdata';
+
+ // 7.1. If the next token is not an end tag token with ...
+ if (!(token.type == 'end-tag' &&
+ token.value == this.endTagName)) {
+ // 7.2. This is a parse error.
+ log ('Parse error: no ' + this.endTagName + '>');
+
+ // 7.3. Mark the script element as "already executed".
+ el.manakaiAlreadyExecuted = true;
+ } else {
+ // 7.4. Ignore it.
+ //
+ }
+ break;
+ }
+ }
} else {
var el = new JSElement (this.doc, token.value);
this.openElements[this.openElements.length - 1].appendChild (el);
@@ -249,6 +380,7 @@
break;
} else if (token.type == 'abort') {
log ('parse: abort');
+ logIndentLevel--;
return;
}
}
@@ -259,8 +391,41 @@
// "When a script completes loading" rules start applying.
- // TODO: Handles "list of scripts that will execute as soon as possible"
- // and "list of scripts that will execute asynchronously"
+ while (this.scriptsExecutedSoon.length > 0 ||
+ this.scriptsExecutedAsynchronously.length > 0) {
+ // Handle "list of scripts that will execute as soon as possible".
+ while (this.scriptsExecutedSoon.length > 0) {
+ var e = this.scriptsExecutedSoon.shift ();
+
+ // If it has completed loading
+ log ('Execute an external script not inserted by parser...');
+ executeScript (this.doc, e);
+
+ // NOTE: It MAY be executed before the end of the parsing, according
+ // to the spec.
+ this.hasAsyncScript = true;
+ }
+
+ // Handle "list of scripts that will execute asynchronously".
+ while (this.scriptsExecutedAsynchronously.length > 0) {
+ var e = this.scriptsExecutedAsynchronously.shift ();
+
+ // Step 1.
+ // We assume that all scripts have been loaded at this time.
+
+ // Step 2.
+ log ('Execute an asynchronous script...');
+ executeScript (this.doc, e);
+
+ // Step 3.
+ //
+
+ // Step 4.
+ //
+
+ this.hasAsyncScript = true;
+ }
+ }
// Handle "list of scripts that will execute when the document has finished
// parsing".
@@ -280,21 +445,23 @@
log ('DOMContentLoaded event fired');
- // "delays tha load event" things has completed:
+ // "delays the load event" things has completed:
// readyState = 'complete'
log ('load event fired');
+
+ logIndentLevel--;
} // parse
Parser.prototype.setInsertionPoint = function (ip) {
if (ip == undefined || ip == null || isNaN (ip)) {
log ('insertion point: set to undefined');
this.insertionPoint = undefined;
- } else if (ip == this.in.s.length) {
+ } else if (ip == this.input.s.length) {
log ('insertion point: end of file');
this.insertionPoint = ip;
} else {
log ('insertion point: set to ' + ip +
- ' (before "' + this.in.s.substring (0, 10) + '")');
+ ' (before "' + this.input.s.substring (0, 10) + '")');
this.insertionPoint = ip;
}
}; // setInsertionPoint
@@ -316,6 +483,7 @@
e.parentNode = this;
if (e.localName == 'script') {
+ logIndentLevel++;
log ('Running a script: start');
var doc = this.ownerDocument || this;
@@ -334,6 +502,7 @@
if (e.manakaiAlreadyExecuted) {
// 2.5. Abort these steps at this point.
log ('Running a script: aborted');
+ logIndentLevel--;
return e;
}
@@ -351,19 +520,28 @@
p.scriptsExecutedAfterParsing.push (e);
log ('Running a script: aborted (defer)');
} else if (e.async && e.src != null) {
- // TODO
- } else if (e.async && e.src == null
- /* && list of scripts that will execute asynchronously is not empty */) {
- // TODO
+ p.scriptsExecutedAsynchronously.push (e);
+ log ('Running a script: aborted (async src)');
+ } else if (e.async && e.src == null &&
+ p.scriptsExecutedAsynchronously.length > 0) {
+ p.scriptsExecutedAsynchronously.push (e);
+ log ('Running a script: aborted (async)');
+ // ISSUE: What is the difference with the case above?
} else if (e.src != null && e.manakaiParserInserted) {
- // TODO
+ if (p.scriptExecutedWhenParserResumes) {
+ log ('Error: There is a script that will execute as soon as the parser resumes.');
+ }
+ p.scriptExecutedWhenParserResumes = e;
+ log ('Running a script: aborted (src parser-inserted)');
} else if (e.src != null) {
- // TODO
+ p.scriptsExecutedSoon.push (e);
+ log ('Running a script: aborted (src)');
} else {
executeScript (doc, e); // even if other scripts are already executing.
}
log ('Running a script: end');
+ logIndentLevel--;
}
return e;
@@ -372,7 +550,20 @@
function executeScript (doc, e) {
log ('executing a script block: start');
- // If the load resulted in an error, then ... firing an error event ...
+ var s;
+ if (e.src != null) {
+ s = getExternalScript (e.src);
+
+ // If the load resulted in an error, then ... firing an error event ...
+ if (s == null) {
+ log ('error event fired at the script element');
+ return;
+ }
+
+ log ('External script loaded: "' + s + '"');
+ } else {
+ s = e.text;
+ }
// If the load was successful
log ('load event fired at the script element');
@@ -381,19 +572,33 @@
// Scripting is enabled, Document.designMode is disabled,
// Document is the active document in its browsing context
- var s;
- if (e.src != null) {
- // TODO: from external file
- } else {
- s = e.text;
- }
-
parseAndRunScript (doc, s);
}
log ('executing a script block: end');
} // executeScript
+ function getExternalScript (uri) {
+ if (uri.match (/^javascript:/i)) {
+ var m;
+ if (m = uri.match (/^javascript:\s*(?:'([^']*)'|"([^"]+)")\s*$/i)) {
+ if (m[1]) {
+ return unescapeJSLiteral (m[1]);
+ } else if (m[2]) {
+ return unescapeJSLiteral (m[2]);
+ } else {
+ return null;
+ }
+ } else {
+ log ('Complex javascript: URI is not supported: <' + uri + '>');
+ return null;
+ }
+ } else {
+ log ('URI scheme not supported: <' + uri + '>');
+ return null;
+ }
+ } // getExternalScript
+
function parseAndRunScript (doc, s) {
while (true) {
var matched = false;
@@ -401,12 +606,19 @@
matched = true;
var args = [];
t.replace (/('[^']*'|"[^"]*")/g, function (s, v) {
- args.push (v.substring (1, v.length - 1));
+ args.push (unescapeJSLiteral (v.substring (1, v.length - 1)));
return '';
});
doc.write.apply (doc, args);
return '';
});
+ s = s.replace (/^\s*var\s+s\s*=\s*document\.createElement\s*\(\s*['"]script['"]\s*\)\s*;\s*s\.src\s*=\s*(?:'(javascript:[^']*)'|"(javascript:[^"]*)")\s*;\s*document\.documentElement\.appendChild\s*\(\s*s\s*\)\s*;\s*/,
+ function (s, t, u) {
+ matched = true;
+ var args = [unescapeJSLiteral (t ? t : u)];
+ doc._insertExternalScript.apply (doc, args);
+ return '';
+ });
if (s == '') break;
if (!matched) {
log ('Script parse error: "' + s + '"');
@@ -415,6 +627,12 @@
}
} // parseAndRunScript
+ function unescapeJSLiteral (s) {
+ return s.replace (/\\u([0-9A-Fa-f]{4})/g, function (t, v) {
+ return String.fromCharCode (parseInt ('0x' + v));
+ });
+ } // unescapeJSLiteral
+
function JSText (data) {
this.data = data;
} // JSText
@@ -442,7 +660,7 @@
// Step 3.
if (this._parser &&
!this._parser.scriptCreated &&
- this._parser.in.insertionPoint != undefined) {
+ this._parser.input.insertionPoint != undefined) {
log ('document.open () in parsing mode is ignored');
return this;
}
@@ -476,13 +694,15 @@
}
// Step 11.
- this._parser.setInsertionPoint (this._parser.in.s.length);
+ this._parser.setInsertionPoint (this._parser.input.s.length);
// Step 12.
return this;
}; // document.open
JSDocument.prototype.write = function () {
+ logIndentLevel++;
+
var p = this._parser;
// 1. If the insertion point is undefined, the open() method must be ...
@@ -494,22 +714,51 @@
// 2. ... inserted into the input stream just before the insertion point.
var s = Array.join (arguments, '');
log ('document.write: insert "' + s + '"' +
- ' before "' + p.in.s.substring (p.insertionPoint, p.insertionPoint + 10) + '"');
- p.in.s = p.in.s.substring (0, p.insertionPoint) + s
- + p.in.s.substring (p.insertionPoint, p.in.s.length);
+ ' before "' +
+ p.input.s.substring (p.insertionPoint, p.insertionPoint + 10) + '"');
+ p.input.s = p.input.s.substring (0, p.insertionPoint) + s
+ + p.input.s.substring (p.insertionPoint, p.input.s.length);
p.insertionPoint += s.length;
// 3. If there is a script that will execute as soon as the parser resumes
- // TODO
+ if (p.scriptExecutedAfterParserResumes) {
+ log ('document.write: processed later (there is an unprocessed