--- markup/html/scripting-parser/parser.html 2008/04/20 10:02:43 1.3
+++ markup/html/scripting-parser/parser.html 2008/04/27 09:16:11 1.9
@@ -1,30 +1,61 @@
-Demo of HTML5 Parsing Algorithm with Scripting Enabled
+Live Scripting HTML Parser
]+)>/, function (s, e) {
- if (p.insertionPoint < s.length) {
+ i.s = i.s.replace (/^<\/([^>]+)(?:>|$)/, function (s, e) {
+ if (p.insertionPoint < s.length ||
+ (p.insertionPoint <= s.length &&
+ s.substring (s.length - 1, 1) != '>')) {
token = {type: 'abort'};
return s;
}
@@ -85,12 +137,36 @@
return '';
});
if (token) return token;
- i.s = i.s.replace (/^<([^>]+)>/, function (s, e) {
- if (p.insertionPoint < s.length) {
+ i.s = i.s.replace (/^<([^>]+)(?:>|$)/, function (s, e) {
+ if (p.insertionPoint < s.length ||
+ (p.insertionPoint <= s.length &&
+ s.substring (s.length - 1, 1) != '>')) {
token = {type: 'abort'};
return s;
}
- token = {type: 'start-tag', value: e.toLowerCase ()};
+ var tagName;
+ var attrs = {};
+ e = e.replace (/^[\S]+/, function (v) {
+ tagName = v.toLowerCase ();
+ return '';
+ });
+ while (true) {
+ var m = false;
+ e = e.replace (/^\s*([^\s=]+)\s*(?:=\s*(?:"([^"]*)"|'([^']*)'|([^"'\s]*)))?/,
+ function (x, attrName, attrValue1, attrValue2, attrValue3) {
+ v = attrValue1 || attrValue2 || attrValue3;
+ v = v.replace (/"/g, '"').replace (/'/g, "'")
+ .replace (/&/g, '&');
+ attrs[attrName.toLowerCase ()] = v;
+ m = true;
+ return '';
+ });
+ if (!m) break;
+ }
+ if (e.length) {
+ log ('Broken start tag: "' + e + '"');
+ }
+ token = {type: 'start-tag', value: tagName, attrs: attrs};
p.insertionPoint -= s.length;
return '';
});
@@ -120,7 +196,8 @@
} // getNextToken
Parser.prototype.parse = function () {
- log ('start parsing');
+ logIndentLevel++;
+ log ('parse: start');
while (true) {
var token = this.getNextToken ();
@@ -130,6 +207,9 @@
if (token.value == 'script') {
// 1. Create an element for the token in the HTML namespace.
var el = new JSElement (this.doc, token.value);
+ if (token.attrs.async != null) el.async = true;
+ if (token.attrs.defer != null) el.defer = true;
+ if (token.attrs.src != null) el.src = token.attrs.src;
// 2. Mark the element as being "parser-inserted".
el.manakaiParserInserted = true;
@@ -185,11 +265,40 @@
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 {
var el = new JSElement (this.doc, token.value);
this.openElements[this.openElements.length - 1].appendChild (el);
@@ -209,20 +318,55 @@
break;
} else if (token.type == 'abort') {
log ('parse: abort');
+ logIndentLevel--;
return;
}
}
log ('stop parsing');
+
+ // readyState = 'interactive'
+
+ // "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"
+
+ // Handle "list of scripts that will execute when the document has finished
+ // parsing".
+ var list = this.scriptsExecutedAfterParsing;
+ while (list.length > 0) {
+ // TODO: break unless completed loading
+
+ // Step 1.
+ //
+
+ // Step 2. and Step 3.
+ log ('Executing a |defer|red script...');
+ executeScript (this.doc, list.shift ());
+
+ // Step 4.
+ }
+
+ log ('DOMContentLoaded event fired');
+
+ // "delays tha 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.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
@@ -244,7 +388,8 @@
e.parentNode = this;
if (e.localName == 'script') {
- log ('start running a script');
+ logIndentLevel++;
+ log ('Running a script: start');
var doc = this.ownerDocument || this;
var p = doc._parser;
@@ -261,7 +406,8 @@
// 2.4. If the script element has its "already executed" flag set
if (e.manakaiAlreadyExecuted) {
// 2.5. Abort these steps at this point.
- log ('running a script: aborted');
+ log ('Running a script: aborted');
+ logIndentLevel--;
return e;
}
@@ -276,21 +422,27 @@
// 5.1.
if (/* TODO: If the document is still being parsed && */
e.defer && !e.async) {
- // TODO
+ 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
} 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)');
} else if (e.src != null) {
// TODO
} else {
executeScript (doc, e); // even if other scripts are already executing.
}
- log ('end running a script');
+ log ('Running a script: end');
+ logIndentLevel--;
}
return e;
@@ -299,7 +451,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');
@@ -308,19 +473,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 m[1];
+ } else if (m[2]) {
+ return 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;
@@ -357,30 +536,98 @@
}
}; // manakaiAppendText
+ JSDocument.prototype.open = function () {
+ // Two or fewer arguments
+
+ // Step 1.
+ var type = arguments[0] || 'text/html';
+
+ // Step 2.
+ var replace = arguments[1] == 'replace';
+
+ // Step 3.
+ if (this._parser &&
+ !this._parser.scriptCreated &&
+ this._parser.input.insertionPoint != undefined) {
+ log ('document.open () in parsing mode is ignored');
+ return this;
+ }
+
+ // Step 4.
+ log ('onbeforeunload event fired');
+ log ('onunload event fired');
+
+ // Step 5.
+ if (this._parser) {
+ // Discard the parser.
+ }
+
+ // Step 6.
+ log ('document cleared by document.open ()');
+ this.childNodes = [];
+
+ // Step 7.
+ this._parser = new Parser (new InputStream (''), this);
+ this._parser.scriptCreated = true;
+
+ // Step 8.
+ this.manakaiIsHTML = true;
+
+ // Step 9.
+ // If not text/html, ...
+
+ // Step 10.
+ if (!replace) {
+ // History
+ }
+
+ // Step 11.
+ 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 ...
- if (p.insertionPoint == NaN || p.insertionPoint == undefined) {
- // TODO: open ()
+ if (isNaN (p.insertionPoint) || p.insertionPoint == undefined) {
+ this.open ();
+ p = this._parser;
}
// 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