3 |
<head> |
<head> |
4 |
<title>Demo of HTML5 Parsing Algorithm with Scripting Enabled</title> |
<title>Demo of HTML5 Parsing Algorithm with Scripting Enabled</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 |
|
if (delayedUpdater) { |
30 |
|
clearTimeout (delayedUpdater); |
31 |
|
delayedUpdater = 0; |
32 |
|
} |
33 |
|
delayedUpdater = setTimeout (update2, 100); |
34 |
|
} // update |
35 |
|
|
36 |
|
function update2 () { |
37 |
document.logElement.textContent = ''; |
document.logElement.textContent = ''; |
38 |
var p = new Parser (new InputStream (document.sourceElement.value)); |
var v = document.sourceElement.value; |
39 |
|
var p = new Parser (new InputStream (v)); |
40 |
var doc = p.doc; |
var doc = p.doc; |
41 |
p.parse (); |
p.parse (); |
42 |
log (dumpTree (doc, '')); |
log (dumpTree (doc, '')); |
|
} // update |
|
43 |
|
|
44 |
|
document.links['permalink'].href |
45 |
|
= location.href + '?s=' + encodeURIComponent (v); |
46 |
|
} // update2 |
47 |
|
|
48 |
|
var logIndentLevel = 0; |
49 |
function log (s) { |
function log (s) { |
50 |
|
for (var i = 0; i < logIndentLevel; i++) { |
51 |
|
s = ' ' + s; |
52 |
|
} |
53 |
document.logElement.appendChild (document.createTextNode (s + "\n")); |
document.logElement.appendChild (document.createTextNode (s + "\n")); |
54 |
} // log |
} // log |
55 |
|
|
100 |
return ''; |
return ''; |
101 |
}); |
}); |
102 |
if (token) return token; |
if (token) return token; |
103 |
|
var m; |
104 |
|
if ((p.insertionPoint < '</script'.length) && |
105 |
|
(m = i.s.match (/^<\/([SCRIPTscript]+)/))) { |
106 |
|
var v = m[1].substring (0, p.insertionPoint).toLowerCase (); |
107 |
|
if (v == 'script'.substring (0, p.insertionPoint - '</'.length)) { |
108 |
|
return {type: 'abort'}; |
109 |
|
} |
110 |
|
} |
111 |
i.s = i.s.replace (/^</, |
i.s = i.s.replace (/^</, |
112 |
function (s) { |
function (s) { |
113 |
token = {type: 'char', value: s}; |
token = {type: 'char', value: s}; |
119 |
} |
} |
120 |
|
|
121 |
var token; |
var token; |
122 |
i.s = i.s.replace (/^<\/([^>]+)>/, function (s, e) { |
i.s = i.s.replace (/^<\/([^>]+)(?:>|$)/, function (s, e) { |
123 |
if (p.insertionPoint < s.length) { |
if (p.insertionPoint < s.length || |
124 |
|
(p.insertionPoint <= s.length && |
125 |
|
s.substring (s.length - 1, 1) != '>')) { |
126 |
token = {type: 'abort'}; |
token = {type: 'abort'}; |
127 |
return s; |
return s; |
128 |
} |
} |
131 |
return ''; |
return ''; |
132 |
}); |
}); |
133 |
if (token) return token; |
if (token) return token; |
134 |
i.s = i.s.replace (/^<([^>]+)>/, function (s, e) { |
i.s = i.s.replace (/^<([^>]+)(?:>|$)/, function (s, e) { |
135 |
if (p.insertionPoint < s.length) { |
if (p.insertionPoint < s.length || |
136 |
|
(p.insertionPoint <= s.length && |
137 |
|
s.substring (s.length - 1, 1) != '>')) { |
138 |
token = {type: 'abort'}; |
token = {type: 'abort'}; |
139 |
return s; |
return s; |
140 |
} |
} |
144 |
tagName = v.toLowerCase (); |
tagName = v.toLowerCase (); |
145 |
return ''; |
return ''; |
146 |
}); |
}); |
147 |
e = e.replace (/^\s*(\S+)\s*(?:=\s*"([^"]*)"|'([^']*)'|([^"']+))?/, |
e = e.replace (/^\s*([^\s=]+)\s*(?:=\s*(?:"([^"]*)"|'([^']*)'|([^"']+)))?/, |
148 |
function (x, attrName, attrValue1, attrValue2, attrValue3) { |
function (x, attrName, attrValue1, attrValue2, attrValue3) { |
149 |
attrs[attrName] = attrValue1 || attrValue2 || attrValue3; |
v = attrValue1 || attrValue2 || attrValue3; |
150 |
|
v = v.replace (/"/g, '"').replace (/'/g, "'") |
151 |
|
.replace (/&/g, '&'); |
152 |
|
attrs[attrName.toLowerCase ()] = v; |
153 |
return ''; |
return ''; |
154 |
}); |
}); |
155 |
|
if (e.length) { |
156 |
|
log ('Broken start tag: "' + e + '"'); |
157 |
|
} |
158 |
token = {type: 'start-tag', value: tagName, attrs: attrs}; |
token = {type: 'start-tag', value: tagName, attrs: attrs}; |
159 |
p.insertionPoint -= s.length; |
p.insertionPoint -= s.length; |
160 |
return ''; |
return ''; |
185 |
} // getNextToken |
} // getNextToken |
186 |
|
|
187 |
Parser.prototype.parse = function () { |
Parser.prototype.parse = function () { |
188 |
log ('start parsing'); |
logIndentLevel++; |
189 |
|
log ('parse: start'); |
190 |
|
|
191 |
while (true) { |
while (true) { |
192 |
var token = this.getNextToken (); |
var token = this.getNextToken (); |
254 |
this.openElements[this.openElements.length - 1].appendChild (el); |
this.openElements[this.openElements.length - 1].appendChild (el); |
255 |
|
|
256 |
// 11. Let the insertion point have the value of the old ... |
// 11. Let the insertion point have the value of the old ... |
257 |
|
|
258 |
|
oldInsertionPoint += this.insertionPoint; |
259 |
this.setInsertionPoint (oldInsertionPoint); |
this.setInsertionPoint (oldInsertionPoint); |
260 |
|
|
261 |
// 12. If there is a script that will execute as soon as ... |
// 12. If there is a script that will execute as soon as ... |
262 |
|
while (this.scriptExecutedWhenParserResumes) { |
263 |
|
// 12.1. If the tree construction stage is being called reentrantly |
264 |
|
if (this.reentrant) { |
265 |
|
log ('parse: abort (reentrance)'); |
266 |
|
logIndentLevel--; |
267 |
|
return; |
268 |
|
|
269 |
|
// 12.2. Otherwise |
270 |
|
} else { |
271 |
|
// 1. |
272 |
|
var script = this.scriptExecutedWhenParserResumes; |
273 |
|
this.scriptExecutedWhenParserResumes = null; |
274 |
|
|
275 |
|
// 2. Pause until the script has completed loading. |
276 |
|
// |
277 |
|
|
278 |
|
// 3. Let the insertion point to just before the next input char. |
279 |
|
this.setInsertionPoint (0); |
280 |
|
|
281 |
|
// 4. Execute the script. |
282 |
|
executeScript (this.doc, script); |
283 |
|
|
284 |
|
// 5. Let the insertion point be undefined again. |
285 |
|
this.setInsertionPoint (undefined); |
286 |
|
|
287 |
|
// 6. If there is once again a script that will execute ... |
288 |
|
// |
289 |
|
} |
290 |
|
} |
291 |
} else { |
} else { |
292 |
var el = new JSElement (this.doc, token.value); |
var el = new JSElement (this.doc, token.value); |
293 |
this.openElements[this.openElements.length - 1].appendChild (el); |
this.openElements[this.openElements.length - 1].appendChild (el); |
307 |
break; |
break; |
308 |
} else if (token.type == 'abort') { |
} else if (token.type == 'abort') { |
309 |
log ('parse: abort'); |
log ('parse: abort'); |
310 |
|
logIndentLevel--; |
311 |
return; |
return; |
312 |
} |
} |
313 |
} |
} |
342 |
// "delays tha load event" things has completed: |
// "delays tha load event" things has completed: |
343 |
// readyState = 'complete' |
// readyState = 'complete' |
344 |
log ('load event fired'); |
log ('load event fired'); |
345 |
|
|
346 |
|
logIndentLevel--; |
347 |
} // parse |
} // parse |
348 |
|
|
349 |
Parser.prototype.setInsertionPoint = function (ip) { |
Parser.prototype.setInsertionPoint = function (ip) { |
377 |
e.parentNode = this; |
e.parentNode = this; |
378 |
|
|
379 |
if (e.localName == 'script') { |
if (e.localName == 'script') { |
380 |
|
logIndentLevel++; |
381 |
log ('Running a script: start'); |
log ('Running a script: start'); |
382 |
|
|
383 |
var doc = this.ownerDocument || this; |
var doc = this.ownerDocument || this; |
396 |
if (e.manakaiAlreadyExecuted) { |
if (e.manakaiAlreadyExecuted) { |
397 |
// 2.5. Abort these steps at this point. |
// 2.5. Abort these steps at this point. |
398 |
log ('Running a script: aborted'); |
log ('Running a script: aborted'); |
399 |
|
logIndentLevel--; |
400 |
return e; |
return e; |
401 |
} |
} |
402 |
|
|
419 |
/* && list of scripts that will execute asynchronously is not empty */) { |
/* && list of scripts that will execute asynchronously is not empty */) { |
420 |
// TODO |
// TODO |
421 |
} else if (e.src != null && e.manakaiParserInserted) { |
} else if (e.src != null && e.manakaiParserInserted) { |
422 |
// TODO |
if (p.scriptExecutedWhenParserResumes) { |
423 |
|
log ('Error: There is a script that will execute as soon as the parser resumes.'); |
424 |
|
} |
425 |
|
p.scriptExecutedWhenParserResumes = e; |
426 |
|
log ('Running a script: aborted (src)'); |
427 |
} else if (e.src != null) { |
} else if (e.src != null) { |
428 |
// TODO |
// TODO |
429 |
} else { |
} else { |
431 |
} |
} |
432 |
|
|
433 |
log ('Running a script: end'); |
log ('Running a script: end'); |
434 |
|
logIndentLevel--; |
435 |
} |
} |
436 |
|
|
437 |
return e; |
return e; |
440 |
function executeScript (doc, e) { |
function executeScript (doc, e) { |
441 |
log ('executing a script block: start'); |
log ('executing a script block: start'); |
442 |
|
|
443 |
// If the load resulted in an error, then ... firing an error event ... |
var s; |
444 |
|
if (e.src != null) { |
445 |
|
s = getExternalScript (e.src); |
446 |
|
|
447 |
|
// If the load resulted in an error, then ... firing an error event ... |
448 |
|
if (s == null) { |
449 |
|
log ('error event fired at the script element'); |
450 |
|
return; |
451 |
|
} |
452 |
|
|
453 |
|
log ('External script loaded: "' + s + '"'); |
454 |
|
} else { |
455 |
|
s = e.text; |
456 |
|
} |
457 |
|
|
458 |
// If the load was successful |
// If the load was successful |
459 |
log ('load event fired at the script element'); |
log ('load event fired at the script element'); |
462 |
// Scripting is enabled, Document.designMode is disabled, |
// Scripting is enabled, Document.designMode is disabled, |
463 |
// Document is the active document in its browsing context |
// Document is the active document in its browsing context |
464 |
|
|
|
var s; |
|
|
if (e.src != null) { |
|
|
// TODO: from external file |
|
|
} else { |
|
|
s = e.text; |
|
|
} |
|
|
|
|
465 |
parseAndRunScript (doc, s); |
parseAndRunScript (doc, s); |
466 |
} |
} |
467 |
|
|
468 |
log ('executing a script block: end'); |
log ('executing a script block: end'); |
469 |
} // executeScript |
} // executeScript |
470 |
|
|
471 |
|
function getExternalScript (uri) { |
472 |
|
if (uri.match (/^javascript:/i)) { |
473 |
|
var m; |
474 |
|
if (m = uri.match (/^javascript:\s*(?:'([^']*)'|"([^"]+)")\s*$/i)) { |
475 |
|
if (m[1]) { |
476 |
|
return m[1]; |
477 |
|
} else if (m[2]) { |
478 |
|
return m[2]; |
479 |
|
} else { |
480 |
|
return null; |
481 |
|
} |
482 |
|
} else { |
483 |
|
log ('Complex javascript: URI is not supported: <' + uri + '>'); |
484 |
|
return null; |
485 |
|
} |
486 |
|
} else { |
487 |
|
log ('URI scheme not supported: <' + uri + '>'); |
488 |
|
return null; |
489 |
|
} |
490 |
|
} // getExternalScript |
491 |
|
|
492 |
function parseAndRunScript (doc, s) { |
function parseAndRunScript (doc, s) { |
493 |
while (true) { |
while (true) { |
494 |
var matched = false; |
var matched = false; |
578 |
}; // document.open |
}; // document.open |
579 |
|
|
580 |
JSDocument.prototype.write = function () { |
JSDocument.prototype.write = function () { |
581 |
|
logIndentLevel++; |
582 |
|
|
583 |
var p = this._parser; |
var p = this._parser; |
584 |
|
|
585 |
// 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 |
p.insertionPoint += s.length; |
p.insertionPoint += s.length; |
598 |
|
|
599 |
// 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 |
600 |
// TODO |
if (p.scriptExecutedAfterParserResumes) { |
601 |
|
log ('document.write: processed later (there is an unprocessed <script src>)'); |
602 |
|
logIndentLevel--; |
603 |
|
return; |
604 |
|
} |
605 |
|
|
606 |
// 4. Process the characters that were inserted, ... |
// 4. Process the characters that were inserted, ... |
607 |
|
var originalReentrant = p.reentrant; |
608 |
|
p.reentrant = true; |
609 |
p.parse (); |
p.parse (); |
610 |
|
p.reentrant = originalReentrant; |
611 |
|
// TODO: "Abort the processing of any nested invokations of the tokeniser, |
612 |
|
// yielding control back to the caller." (<script> parsing). Do we need |
613 |
|
// to do something here? |
614 |
|
|
615 |
// 5. Return |
// 5. Return |
616 |
log ('document.write: return'); |
log ('document.write: return'); |
617 |
|
|
618 |
|
logIndentLevel--; |
619 |
return; |
return; |
620 |
}; // document.write |
}; // document.write |
621 |
|
|
654 |
document.logElement = document.getElementsByTagName ('output')[0]; |
document.logElement = document.getElementsByTagName ('output')[0]; |
655 |
update (); |
update (); |
656 |
"> |
"> |
657 |
|
<h1>Live Scripting Parser</h1> |
658 |
|
|
659 |
<textarea onchange=" update () "><html> |
<h2>Markup to test |
660 |
|
(<a href=data:, id=permalink rel=bookmark>permalink</a>)</h2> |
661 |
|
<p> |
662 |
|
<textarea onkeydown=" update () " onchange=" update () " oninput=" update () "><html> |
663 |
<head></head><body> |
<head></head><body> |
664 |
<p> |
<p> |
665 |
<script> |
<script> |
668 |
<p> |
<p> |
669 |
</textarea> |
</textarea> |
670 |
|
|
671 |
<output></output> |
<h2>Log</h2> |
672 |
|
<p><output></output> |
673 |
|
|
674 |
|
<!-- TODO: short description --> |
675 |
|
|
676 |
|
<!-- TODO: permalink query -> textarea --> |
677 |
|
|
678 |
|
<!-- TODO: multiple attributes are not supported yet --> |
679 |
|
|
680 |
</body> |
</body> |
681 |
</html> |
</html> |