1 |
<!DOCTYPE HTML> |
<!DOCTYPE HTML> |
2 |
<html lang=en> |
<html lang=en> |
3 |
<head> |
<head> |
4 |
<title>Demo of HTML5 Parsing Algorithm with Scripting Enabled</title> |
<title>Live Scripting HTML Parser</title> |
5 |
<style> |
<style> |
6 |
|
h1, h2 { |
7 |
|
margin: 0; |
8 |
|
font-size: 100%; |
9 |
|
} |
10 |
|
p, pre { |
11 |
|
margin: 0; |
12 |
|
} |
13 |
textarea { |
textarea { |
14 |
display: block; |
width: 100%; |
15 |
width: 80%; |
-width: 99%; |
16 |
margin-left: auto; |
height: 10em; |
|
margin-right: auto; |
|
|
min-height: 20em; |
|
17 |
} |
} |
18 |
output { |
output { |
19 |
display: block; |
display: block; |
23 |
} |
} |
24 |
</style> |
</style> |
25 |
<script> |
<script> |
26 |
|
var delayedUpdater = 0; |
27 |
|
|
28 |
function update () { |
function update () { |
29 |
document.logElement.textContent = ''; |
if (delayedUpdater) { |
30 |
var p = new Parser (new InputStream (document.sourceElement.value)); |
clearTimeout (delayedUpdater); |
31 |
var doc = p.doc; |
delayedUpdater = 0; |
32 |
p.parse (); |
} |
33 |
log (dumpTree (doc, '')); |
delayedUpdater = setTimeout (update2, 100); |
34 |
} // update |
} // update |
35 |
|
|
36 |
|
function update2 () { |
37 |
|
var v = document.sourceElement.value; |
38 |
|
if (v != document.previousSourceText) { |
39 |
|
document.previousSourceText = v; |
40 |
|
document.links['permalink'].href |
41 |
|
= location.pathname + '?s=' + encodeURIComponent (v); |
42 |
|
document.links['ldvlink'].href |
43 |
|
= 'http://software.hixie.ch/utilities/js/live-dom-viewer/?' |
44 |
|
+ encodeURIComponent (v); |
45 |
|
|
46 |
|
document.logElement.textContent = ''; |
47 |
|
var p = new Parser (new InputStream (v)); |
48 |
|
var doc = p.doc; |
49 |
|
p.parse (); |
50 |
|
log (dumpTree (doc, '')); |
51 |
|
} |
52 |
|
} // update2 |
53 |
|
|
54 |
|
var logIndentLevel = 0; |
55 |
function log (s) { |
function log (s) { |
56 |
|
for (var i = 0; i < logIndentLevel; i++) { |
57 |
|
s = ' ' + s; |
58 |
|
} |
59 |
document.logElement.appendChild (document.createTextNode (s + "\n")); |
document.logElement.appendChild (document.createTextNode (s + "\n")); |
60 |
} // log |
} // log |
61 |
|
|
71 |
} |
} |
72 |
this.doc = doc; |
this.doc = doc; |
73 |
this.openElements = [doc]; |
this.openElements = [doc]; |
74 |
this.in = i; |
this.input = i; |
75 |
this.scriptsExecutedAfterParsing = []; |
this.scriptsExecutedAfterParsing = []; |
76 |
} // Parser |
} // Parser |
77 |
|
|
78 |
Parser.prototype.getNextToken = function () { |
Parser.prototype.getNextToken = function () { |
79 |
var p = this; |
var p = this; |
80 |
var i = this.in; |
var i = this.input; |
81 |
if (this.parseMode == 'script') { |
if (this.parseMode == 'script') { |
82 |
var token; |
var token; |
83 |
if (p.insertionPoint <= 0) { |
if (p.insertionPoint <= 0) { |
106 |
return ''; |
return ''; |
107 |
}); |
}); |
108 |
if (token) return token; |
if (token) return token; |
109 |
|
var m; |
110 |
|
if ((p.insertionPoint < '</script'.length) && |
111 |
|
(m = i.s.match (/^<\/([SCRIPTscript]+)/))) { |
112 |
|
var v = m[1].substring (0, p.insertionPoint).toLowerCase (); |
113 |
|
if (v == 'script'.substring (0, p.insertionPoint - '</'.length)) { |
114 |
|
return {type: 'abort'}; |
115 |
|
} |
116 |
|
} |
117 |
i.s = i.s.replace (/^</, |
i.s = i.s.replace (/^</, |
118 |
function (s) { |
function (s) { |
119 |
token = {type: 'char', value: s}; |
token = {type: 'char', value: s}; |
125 |
} |
} |
126 |
|
|
127 |
var token; |
var token; |
128 |
i.s = i.s.replace (/^<\/([^>]+)>/, function (s, e) { |
i.s = i.s.replace (/^<\/([^>]+)(?:>|$)/, function (s, e) { |
129 |
if (p.insertionPoint < s.length) { |
if (p.insertionPoint < s.length || |
130 |
|
(p.insertionPoint <= s.length && |
131 |
|
s.substring (s.length - 1, 1) != '>')) { |
132 |
token = {type: 'abort'}; |
token = {type: 'abort'}; |
133 |
return s; |
return s; |
134 |
} |
} |
137 |
return ''; |
return ''; |
138 |
}); |
}); |
139 |
if (token) return token; |
if (token) return token; |
140 |
i.s = i.s.replace (/^<([^>]+)>/, function (s, e) { |
i.s = i.s.replace (/^<([^>]+)(?:>|$)/, function (s, e) { |
141 |
if (p.insertionPoint < s.length) { |
if (p.insertionPoint < s.length || |
142 |
|
(p.insertionPoint <= s.length && |
143 |
|
s.substring (s.length - 1, 1) != '>')) { |
144 |
token = {type: 'abort'}; |
token = {type: 'abort'}; |
145 |
return s; |
return s; |
146 |
} |
} |
150 |
tagName = v.toLowerCase (); |
tagName = v.toLowerCase (); |
151 |
return ''; |
return ''; |
152 |
}); |
}); |
153 |
e = e.replace (/^\s*(\S+)\s*(?:=\s*"([^"]*)"|'([^']*)'|([^"']+))?/, |
e = e.replace (/^\s*([^\s=]+)\s*(?:=\s*(?:"([^"]*)"|'([^']*)'|([^"']+)))?/, |
154 |
function (x, attrName, attrValue1, attrValue2, attrValue3) { |
function (x, attrName, attrValue1, attrValue2, attrValue3) { |
155 |
attrs[attrName] = attrValue1 || attrValue2 || attrValue3; |
v = attrValue1 || attrValue2 || attrValue3; |
156 |
|
v = v.replace (/"/g, '"').replace (/'/g, "'") |
157 |
|
.replace (/&/g, '&'); |
158 |
|
attrs[attrName.toLowerCase ()] = v; |
159 |
return ''; |
return ''; |
160 |
}); |
}); |
161 |
|
if (e.length) { |
162 |
|
log ('Broken start tag: "' + e + '"'); |
163 |
|
} |
164 |
token = {type: 'start-tag', value: tagName, attrs: attrs}; |
token = {type: 'start-tag', value: tagName, attrs: attrs}; |
165 |
p.insertionPoint -= s.length; |
p.insertionPoint -= s.length; |
166 |
return ''; |
return ''; |
191 |
} // getNextToken |
} // getNextToken |
192 |
|
|
193 |
Parser.prototype.parse = function () { |
Parser.prototype.parse = function () { |
194 |
log ('start parsing'); |
logIndentLevel++; |
195 |
|
log ('parse: start'); |
196 |
|
|
197 |
while (true) { |
while (true) { |
198 |
var token = this.getNextToken (); |
var token = this.getNextToken (); |
260 |
this.openElements[this.openElements.length - 1].appendChild (el); |
this.openElements[this.openElements.length - 1].appendChild (el); |
261 |
|
|
262 |
// 11. Let the insertion point have the value of the old ... |
// 11. Let the insertion point have the value of the old ... |
263 |
|
|
264 |
|
oldInsertionPoint += this.insertionPoint; |
265 |
this.setInsertionPoint (oldInsertionPoint); |
this.setInsertionPoint (oldInsertionPoint); |
266 |
|
|
267 |
// 12. If there is a script that will execute as soon as ... |
// 12. If there is a script that will execute as soon as ... |
268 |
|
while (this.scriptExecutedWhenParserResumes) { |
269 |
|
// 12.1. If the tree construction stage is being called reentrantly |
270 |
|
if (this.reentrant) { |
271 |
|
log ('parse: abort (reentrance)'); |
272 |
|
logIndentLevel--; |
273 |
|
return; |
274 |
|
|
275 |
|
// 12.2. Otherwise |
276 |
|
} else { |
277 |
|
// 1. |
278 |
|
var script = this.scriptExecutedWhenParserResumes; |
279 |
|
this.scriptExecutedWhenParserResumes = null; |
280 |
|
|
281 |
|
// 2. Pause until the script has completed loading. |
282 |
|
// |
283 |
|
|
284 |
|
// 3. Let the insertion point to just before the next input char. |
285 |
|
this.setInsertionPoint (0); |
286 |
|
|
287 |
|
// 4. Execute the script. |
288 |
|
executeScript (this.doc, script); |
289 |
|
|
290 |
|
// 5. Let the insertion point be undefined again. |
291 |
|
this.setInsertionPoint (undefined); |
292 |
|
|
293 |
|
// 6. If there is once again a script that will execute ... |
294 |
|
// |
295 |
|
} |
296 |
|
} |
297 |
} else { |
} else { |
298 |
var el = new JSElement (this.doc, token.value); |
var el = new JSElement (this.doc, token.value); |
299 |
this.openElements[this.openElements.length - 1].appendChild (el); |
this.openElements[this.openElements.length - 1].appendChild (el); |
313 |
break; |
break; |
314 |
} else if (token.type == 'abort') { |
} else if (token.type == 'abort') { |
315 |
log ('parse: abort'); |
log ('parse: abort'); |
316 |
|
logIndentLevel--; |
317 |
return; |
return; |
318 |
} |
} |
319 |
} |
} |
348 |
// "delays tha load event" things has completed: |
// "delays tha load event" things has completed: |
349 |
// readyState = 'complete' |
// readyState = 'complete' |
350 |
log ('load event fired'); |
log ('load event fired'); |
351 |
|
|
352 |
|
logIndentLevel--; |
353 |
} // parse |
} // parse |
354 |
|
|
355 |
Parser.prototype.setInsertionPoint = function (ip) { |
Parser.prototype.setInsertionPoint = function (ip) { |
356 |
if (ip == undefined || ip == null || isNaN (ip)) { |
if (ip == undefined || ip == null || isNaN (ip)) { |
357 |
log ('insertion point: set to undefined'); |
log ('insertion point: set to undefined'); |
358 |
this.insertionPoint = undefined; |
this.insertionPoint = undefined; |
359 |
} else if (ip == this.in.s.length) { |
} else if (ip == this.input.s.length) { |
360 |
log ('insertion point: end of file'); |
log ('insertion point: end of file'); |
361 |
this.insertionPoint = ip; |
this.insertionPoint = ip; |
362 |
} else { |
} else { |
363 |
log ('insertion point: set to ' + ip + |
log ('insertion point: set to ' + ip + |
364 |
' (before "' + this.in.s.substring (0, 10) + '")'); |
' (before "' + this.input.s.substring (0, 10) + '")'); |
365 |
this.insertionPoint = ip; |
this.insertionPoint = ip; |
366 |
} |
} |
367 |
}; // setInsertionPoint |
}; // setInsertionPoint |
383 |
e.parentNode = this; |
e.parentNode = this; |
384 |
|
|
385 |
if (e.localName == 'script') { |
if (e.localName == 'script') { |
386 |
|
logIndentLevel++; |
387 |
log ('Running a script: start'); |
log ('Running a script: start'); |
388 |
|
|
389 |
var doc = this.ownerDocument || this; |
var doc = this.ownerDocument || this; |
402 |
if (e.manakaiAlreadyExecuted) { |
if (e.manakaiAlreadyExecuted) { |
403 |
// 2.5. Abort these steps at this point. |
// 2.5. Abort these steps at this point. |
404 |
log ('Running a script: aborted'); |
log ('Running a script: aborted'); |
405 |
|
logIndentLevel--; |
406 |
return e; |
return e; |
407 |
} |
} |
408 |
|
|
425 |
/* && list of scripts that will execute asynchronously is not empty */) { |
/* && list of scripts that will execute asynchronously is not empty */) { |
426 |
// TODO |
// TODO |
427 |
} else if (e.src != null && e.manakaiParserInserted) { |
} else if (e.src != null && e.manakaiParserInserted) { |
428 |
// TODO |
if (p.scriptExecutedWhenParserResumes) { |
429 |
|
log ('Error: There is a script that will execute as soon as the parser resumes.'); |
430 |
|
} |
431 |
|
p.scriptExecutedWhenParserResumes = e; |
432 |
|
log ('Running a script: aborted (src)'); |
433 |
} else if (e.src != null) { |
} else if (e.src != null) { |
434 |
// TODO |
// TODO |
435 |
} else { |
} else { |
437 |
} |
} |
438 |
|
|
439 |
log ('Running a script: end'); |
log ('Running a script: end'); |
440 |
|
logIndentLevel--; |
441 |
} |
} |
442 |
|
|
443 |
return e; |
return e; |
446 |
function executeScript (doc, e) { |
function executeScript (doc, e) { |
447 |
log ('executing a script block: start'); |
log ('executing a script block: start'); |
448 |
|
|
449 |
// If the load resulted in an error, then ... firing an error event ... |
var s; |
450 |
|
if (e.src != null) { |
451 |
|
s = getExternalScript (e.src); |
452 |
|
|
453 |
|
// If the load resulted in an error, then ... firing an error event ... |
454 |
|
if (s == null) { |
455 |
|
log ('error event fired at the script element'); |
456 |
|
return; |
457 |
|
} |
458 |
|
|
459 |
|
log ('External script loaded: "' + s + '"'); |
460 |
|
} else { |
461 |
|
s = e.text; |
462 |
|
} |
463 |
|
|
464 |
// If the load was successful |
// If the load was successful |
465 |
log ('load event fired at the script element'); |
log ('load event fired at the script element'); |
468 |
// Scripting is enabled, Document.designMode is disabled, |
// Scripting is enabled, Document.designMode is disabled, |
469 |
// Document is the active document in its browsing context |
// Document is the active document in its browsing context |
470 |
|
|
|
var s; |
|
|
if (e.src != null) { |
|
|
// TODO: from external file |
|
|
} else { |
|
|
s = e.text; |
|
|
} |
|
|
|
|
471 |
parseAndRunScript (doc, s); |
parseAndRunScript (doc, s); |
472 |
} |
} |
473 |
|
|
474 |
log ('executing a script block: end'); |
log ('executing a script block: end'); |
475 |
} // executeScript |
} // executeScript |
476 |
|
|
477 |
|
function getExternalScript (uri) { |
478 |
|
if (uri.match (/^javascript:/i)) { |
479 |
|
var m; |
480 |
|
if (m = uri.match (/^javascript:\s*(?:'([^']*)'|"([^"]+)")\s*$/i)) { |
481 |
|
if (m[1]) { |
482 |
|
return m[1]; |
483 |
|
} else if (m[2]) { |
484 |
|
return m[2]; |
485 |
|
} else { |
486 |
|
return null; |
487 |
|
} |
488 |
|
} else { |
489 |
|
log ('Complex javascript: URI is not supported: <' + uri + '>'); |
490 |
|
return null; |
491 |
|
} |
492 |
|
} else { |
493 |
|
log ('URI scheme not supported: <' + uri + '>'); |
494 |
|
return null; |
495 |
|
} |
496 |
|
} // getExternalScript |
497 |
|
|
498 |
function parseAndRunScript (doc, s) { |
function parseAndRunScript (doc, s) { |
499 |
while (true) { |
while (true) { |
500 |
var matched = false; |
var matched = false; |
543 |
// Step 3. |
// Step 3. |
544 |
if (this._parser && |
if (this._parser && |
545 |
!this._parser.scriptCreated && |
!this._parser.scriptCreated && |
546 |
this._parser.in.insertionPoint != undefined) { |
this._parser.input.insertionPoint != undefined) { |
547 |
log ('document.open () in parsing mode is ignored'); |
log ('document.open () in parsing mode is ignored'); |
548 |
return this; |
return this; |
549 |
} |
} |
577 |
} |
} |
578 |
|
|
579 |
// Step 11. |
// Step 11. |
580 |
this._parser.setInsertionPoint (this._parser.in.s.length); |
this._parser.setInsertionPoint (this._parser.input.s.length); |
581 |
|
|
582 |
// Step 12. |
// Step 12. |
583 |
return this; |
return this; |
584 |
}; // document.open |
}; // document.open |
585 |
|
|
586 |
JSDocument.prototype.write = function () { |
JSDocument.prototype.write = function () { |
587 |
|
logIndentLevel++; |
588 |
|
|
589 |
var p = this._parser; |
var p = this._parser; |
590 |
|
|
591 |
// 1. If the insertion point is undefined, the open() method must be ... |
// 1. If the insertion point is undefined, the open() method must be ... |
597 |
// 2. ... inserted into the input stream just before the insertion point. |
// 2. ... inserted into the input stream just before the insertion point. |
598 |
var s = Array.join (arguments, ''); |
var s = Array.join (arguments, ''); |
599 |
log ('document.write: insert "' + s + '"' + |
log ('document.write: insert "' + s + '"' + |
600 |
' before "' + p.in.s.substring (p.insertionPoint, p.insertionPoint + 10) + '"'); |
' before "' + |
601 |
p.in.s = p.in.s.substring (0, p.insertionPoint) + s |
p.input.s.substring (p.insertionPoint, p.insertionPoint + 10) + '"'); |
602 |
+ p.in.s.substring (p.insertionPoint, p.in.s.length); |
p.input.s = p.input.s.substring (0, p.insertionPoint) + s |
603 |
|
+ p.input.s.substring (p.insertionPoint, p.input.s.length); |
604 |
p.insertionPoint += s.length; |
p.insertionPoint += s.length; |
605 |
|
|
606 |
// 3. If there is a script that will execute as soon as the parser resumes |
// 3. If there is a script that will execute as soon as the parser resumes |
607 |
// TODO |
if (p.scriptExecutedAfterParserResumes) { |
608 |
|
log ('document.write: processed later (there is an unprocessed <script src>)'); |
609 |
|
logIndentLevel--; |
610 |
|
return; |
611 |
|
} |
612 |
|
|
613 |
// 4. Process the characters that were inserted, ... |
// 4. Process the characters that were inserted, ... |
614 |
|
var originalReentrant = p.reentrant; |
615 |
|
p.reentrant = true; |
616 |
p.parse (); |
p.parse (); |
617 |
|
p.reentrant = originalReentrant; |
618 |
|
// TODO: "Abort the processing of any nested invokations of the tokeniser, |
619 |
|
// yielding control back to the caller." (<script> parsing). Do we need |
620 |
|
// to do something here? |
621 |
|
|
622 |
// 5. Return |
// 5. Return |
623 |
log ('document.write: return'); |
log ('document.write: return'); |
624 |
|
|
625 |
|
logIndentLevel--; |
626 |
return; |
return; |
627 |
}; // document.write |
}; // document.write |
628 |
|
|
658 |
</head> |
</head> |
659 |
<body onload=" |
<body onload=" |
660 |
document.sourceElement = document.getElementsByTagName ('textarea')[0]; |
document.sourceElement = document.getElementsByTagName ('textarea')[0]; |
661 |
|
|
662 |
|
var q = location.search; |
663 |
|
if (q != null) { |
664 |
|
q = q.substring (1).split (/;/); |
665 |
|
for (var i = 0; i < q.length; i++) { |
666 |
|
var v = q[i].split (/=/, 2); |
667 |
|
v[0] = decodeURIComponent (v[0]); |
668 |
|
v[1] = decodeURIComponent (v[1] || ''); |
669 |
|
if (v[0] == 's') { |
670 |
|
document.sourceElement.value = v[1]; |
671 |
|
} |
672 |
|
} |
673 |
|
} |
674 |
|
|
675 |
document.logElement = document.getElementsByTagName ('output')[0]; |
document.logElement = document.getElementsByTagName ('output')[0]; |
676 |
update (); |
update (); |
677 |
"> |
"> |
678 |
|
<h1>Live Scripting <abbr title="Hypertext Markup Language">HTML</abbr> |
679 |
|
Parser</h1> |
680 |
|
|
681 |
<textarea onchange=" update () "><html> |
<h2>Markup to test |
682 |
|
(<a href=data:, id=permalink rel=bookmark>permalink</a>, |
683 |
|
<a href="http://software.hixie.ch/utilities/js/live-dom-viewer/" |
684 |
|
id=ldvlink>Live <abbr title="Document Object Model">DOM</abbr> |
685 |
|
Viewer</a>)</h2> |
686 |
|
<p> |
687 |
|
<textarea onkeydown=" update () " onchange=" update () " oninput=" update () "><html> |
688 |
<head></head><body> |
<head></head><body> |
689 |
<p> |
<p> |
690 |
<script> |
<script> |
693 |
<p> |
<p> |
694 |
</textarea> |
</textarea> |
695 |
|
|
696 |
<output></output> |
<h2>Log</h2> |
697 |
|
<p><output></output> |
698 |
|
|
699 |
|
<h2>Note</h2> |
700 |
|
|
701 |
|
<p>This is a <em>simplified</em> implementation of |
702 |
|
<a href="http://www.whatwg.org/specs/web-apps/current-work/#parsing">HTML5 |
703 |
|
Parsing Algorithm</a>. It only implements script-related part of the |
704 |
|
algorithm. Especially, this parser: |
705 |
|
<ul> |
706 |
|
<li>Does not support <code>DOCTYPE</code> and comment tokens. |
707 |
|
<li>Does not support entities except for <code>&quot;</code>, |
708 |
|
<code>&apos;</code>, and <code>&amp;</code> in <code>script</code> |
709 |
|
<code>src</code> attribute value. |
710 |
|
<li>Does not support omissions of start or end tags, the <abbr>AAA</abbr> |
711 |
|
algorithm, and so on. |
712 |
|
<li>Does not raise parse errors for invalid attribute specifications in start |
713 |
|
or end tags. |
714 |
|
<li>Does not support CDATA/PCDATA element other than <code>script</code>. |
715 |
|
<li>Does not support <code><!--</code>..<code>--></code> parsing rule |
716 |
|
in <code>script</code> element. |
717 |
|
<li>Does not support foreign (SVG or MathML) elements. |
718 |
|
<li>Only supports <code>script</code> <code>type</code> |
719 |
|
<code>text/javascript</code>. <code>type</code> and <code>language</code> |
720 |
|
attributes are ignored. |
721 |
|
<li>Only supports <code>document.write</code>. |
722 |
|
The script code must be match to the regular expression |
723 |
|
<code>^\s*(?:document\.write\s*\(<var>v</var>\s*(?:,\s*<var>v</var>\s*)*\)\s*;\s*)*$</code> |
724 |
|
where <var>v</var> is <code>"[^"]*"|'[^']*'</code>. |
725 |
|
<li>Only supports <code>javascript:</code> |
726 |
|
<abbr title="Uniform Resourace Identifiers">URI</abbr> scheme in the |
727 |
|
<code>src</code> attribute of the <code>script</code> element. In addition, |
728 |
|
the <abbr title="Uniform Resource Identifiers">URI</abbr> must be conform to |
729 |
|
the regular expression <code>^javascript:\s*(?:"[^"]*"|'[^']*')\s*$</code>. |
730 |
|
</ul> |
731 |
|
|
732 |
|
<p>For some reason, this parser does not work in browsers that do |
733 |
|
not support JavaScript 1.5. |
734 |
|
|
735 |
|
<!-- TODO: multiple attributes are not supported yet --> |
736 |
|
|
737 |
</body> |
</body> |
738 |
</html> |
</html> |