Parent Directory
|
Revision Log
|
Patch
| revision 1.5 by wakaba, Fri Apr 25 11:40:56 2008 UTC | revision 1.11 by wakaba, Sun Apr 27 10:44:36 2008 UTC | |
|---|---|---|
| # | Line 1 | Line 1 |
| 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; |
| # | Line 18 | Line 23 |
| 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 | ||
| 51 | log (dumpTree (doc, '')); | |
| 52 | ||
| 53 | if (p.hasAsyncScript) { | |
| 54 | log ('Some script codes are executed asynchronously; it means that the document might be rendered in different ways depending on the network condition and other factors'); | |
| 55 | } | |
| 56 | } | |
| 57 | } // update2 | |
| 58 | ||
| 59 | var logIndentLevel = 0; | |
| 60 | function log (s) { | function log (s) { |
| 61 | for (var i = 0; i < logIndentLevel; i++) { | |
| 62 | s = ' ' + s; | |
| 63 | } | |
| 64 | document.logElement.appendChild (document.createTextNode (s + "\n")); | document.logElement.appendChild (document.createTextNode (s + "\n")); |
| 65 | } // log | } // log |
| 66 | ||
| # | Line 42 | Line 76 |
| 76 | } | } |
| 77 | this.doc = doc; | this.doc = doc; |
| 78 | this.openElements = [doc]; | this.openElements = [doc]; |
| 79 | this.in = i; | this.input = i; |
| 80 | this.scriptsExecutedAfterParsing = []; | this.scriptsExecutedAfterParsing = []; |
| 81 | this.scriptsExecutedSoon = []; | |
| 82 | } // Parser | } // Parser |
| 83 | ||
| 84 | Parser.prototype.getNextToken = function () { | Parser.prototype.getNextToken = function () { |
| 85 | var p = this; | var p = this; |
| 86 | var i = this.in; | var i = this.input; |
| 87 | if (this.parseMode == 'script') { | if (this.parseMode == 'script') { |
| 88 | var token; | var token; |
| 89 | if (p.insertionPoint <= 0) { | if (p.insertionPoint <= 0) { |
| # | Line 121 | Line 156 |
| 156 | tagName = v.toLowerCase (); | tagName = v.toLowerCase (); |
| 157 | return ''; | return ''; |
| 158 | }); | }); |
| 159 | e = e.replace (/^\s*(\S+)\s*(?:=\s*"([^"]*)"|'([^']*)'|([^"']+))?/, | while (true) { |
| 160 | function (x, attrName, attrValue1, attrValue2, attrValue3) { | var m = false; |
| 161 | attrs[attrName] = attrValue1 || attrValue2 || attrValue3; | e = e.replace (/^\s*([^\s=]+)\s*(?:=\s*(?:"([^"]*)"|'([^']*)'|([^"'\s]*)))?/, |
| 162 | return ''; | function (x, attrName, attrValue1, attrValue2, attrValue3) { |
| 163 | }); | v = attrValue1 || attrValue2 || attrValue3; |
| 164 | v = v.replace (/"/g, '"').replace (/'/g, "'") | |
| 165 | .replace (/&/g, '&'); | |
| 166 | attrs[attrName.toLowerCase ()] = v; | |
| 167 | m = true; | |
| 168 | return ''; | |
| 169 | }); | |
| 170 | if (!m) break; | |
| 171 | } | |
| 172 | if (e.length) { | |
| 173 | log ('Broken start tag: "' + e + '"'); | |
| 174 | } | |
| 175 | token = {type: 'start-tag', value: tagName, attrs: attrs}; | token = {type: 'start-tag', value: tagName, attrs: attrs}; |
| 176 | p.insertionPoint -= s.length; | p.insertionPoint -= s.length; |
| 177 | return ''; | return ''; |
| # | Line 156 | Line 202 |
| 202 | } // getNextToken | } // getNextToken |
| 203 | ||
| 204 | Parser.prototype.parse = function () { | Parser.prototype.parse = function () { |
| 205 | log ('start parsing'); | logIndentLevel++; |
| 206 | log ('parse: start'); | |
| 207 | ||
| 208 | while (true) { | while (true) { |
| 209 | var token = this.getNextToken (); | var token = this.getNextToken (); |
| # | Line 224 | Line 271 |
| 271 | this.openElements[this.openElements.length - 1].appendChild (el); | this.openElements[this.openElements.length - 1].appendChild (el); |
| 272 | ||
| 273 | // 11. Let the insertion point have the value of the old ... | // 11. Let the insertion point have the value of the old ... |
| 274 | ||
| 275 | oldInsertionPoint += this.insertionPoint; | oldInsertionPoint += this.insertionPoint; |
| 276 | this.setInsertionPoint (oldInsertionPoint); | this.setInsertionPoint (oldInsertionPoint); |
| 277 | ||
| 278 | // 12. If there is a script that will execute as soon as ... | // 12. If there is a script that will execute as soon as ... |
| 279 | while (this.scriptExecutedWhenParserResumes) { | |
| 280 | // 12.1. If the tree construction stage is being called reentrantly | |
| 281 | if (this.reentrant) { | |
| 282 | log ('parse: abort (reentrance)'); | |
| 283 | logIndentLevel--; | |
| 284 | return; | |
| 285 | ||
| 286 | // 12.2. Otherwise | |
| 287 | } else { | |
| 288 | // 1. | |
| 289 | var script = this.scriptExecutedWhenParserResumes; | |
| 290 | this.scriptExecutedWhenParserResumes = null; | |
| 291 | ||
| 292 | // 2. Pause until the script has completed loading. | |
| 293 | // | |
| 294 | ||
| 295 | // 3. Let the insertion point to just before the next input char. | |
| 296 | this.setInsertionPoint (0); | |
| 297 | ||
| 298 | // 4. Execute the script. | |
| 299 | executeScript (this.doc, script); | |
| 300 | ||
| 301 | // 5. Let the insertion point be undefined again. | |
| 302 | this.setInsertionPoint (undefined); | |
| 303 | ||
| 304 | // 6. If there is once again a script that will execute ... | |
| 305 | // | |
| 306 | } | |
| 307 | } | |
| 308 | } else { | } else { |
| 309 | var el = new JSElement (this.doc, token.value); | var el = new JSElement (this.doc, token.value); |
| 310 | this.openElements[this.openElements.length - 1].appendChild (el); | this.openElements[this.openElements.length - 1].appendChild (el); |
| # | Line 249 | Line 324 |
| 324 | break; | break; |
| 325 | } else if (token.type == 'abort') { | } else if (token.type == 'abort') { |
| 326 | log ('parse: abort'); | log ('parse: abort'); |
| 327 | logIndentLevel--; | |
| 328 | return; | return; |
| 329 | } | } |
| 330 | } | } |
| # | Line 259 | Line 335 |
| 335 | ||
| 336 | // "When a script completes loading" rules start applying. | // "When a script completes loading" rules start applying. |
| 337 | ||
| 338 | // TODO: Handles "list of scripts that will execute as soon as possible" | // List of scripts that will execute as soon as possible |
| 339 | // and "list of scripts that will execute asynchronously" | for (var i = 0; i < this.scriptsExecutedSoon.length; i++) { |
| 340 | var e = this.scriptsExecutedSoon[i]; | |
| 341 | ||
| 342 | // If it has completed loading | |
| 343 | log ('Execute an external script not inserted by parser...'); | |
| 344 | executeScript (this.doc, e); | |
| 345 | ||
| 346 | // NOTE: It MAY be executed before the end of the parsing, according | |
| 347 | // to the spec. | |
| 348 | this.hasAsyncScript = true; | |
| 349 | } | |
| 350 | ||
| 351 | // TODO: Handles | |
| 352 | // "list of scripts that will execute asynchronously" | |
| 353 | ||
| 354 | // Handle "list of scripts that will execute when the document has finished | // Handle "list of scripts that will execute when the document has finished |
| 355 | // parsing". | // parsing". |
| # | Line 283 | Line 372 |
| 372 | // "delays tha load event" things has completed: | // "delays tha load event" things has completed: |
| 373 | // readyState = 'complete' | // readyState = 'complete' |
| 374 | log ('load event fired'); | log ('load event fired'); |
| 375 | ||
| 376 | logIndentLevel--; | |
| 377 | } // parse | } // parse |
| 378 | ||
| 379 | Parser.prototype.setInsertionPoint = function (ip) { | Parser.prototype.setInsertionPoint = function (ip) { |
| 380 | if (ip == undefined || ip == null || isNaN (ip)) { | if (ip == undefined || ip == null || isNaN (ip)) { |
| 381 | log ('insertion point: set to undefined'); | log ('insertion point: set to undefined'); |
| 382 | this.insertionPoint = undefined; | this.insertionPoint = undefined; |
| 383 | } else if (ip == this.in.s.length) { | } else if (ip == this.input.s.length) { |
| 384 | log ('insertion point: end of file'); | log ('insertion point: end of file'); |
| 385 | this.insertionPoint = ip; | this.insertionPoint = ip; |
| 386 | } else { | } else { |
| 387 | log ('insertion point: set to ' + ip + | log ('insertion point: set to ' + ip + |
| 388 | ' (before "' + this.in.s.substring (0, 10) + '")'); | ' (before "' + this.input.s.substring (0, 10) + '")'); |
| 389 | this.insertionPoint = ip; | this.insertionPoint = ip; |
| 390 | } | } |
| 391 | }; // setInsertionPoint | }; // setInsertionPoint |
| # | Line 316 | Line 407 |
| 407 | e.parentNode = this; | e.parentNode = this; |
| 408 | ||
| 409 | if (e.localName == 'script') { | if (e.localName == 'script') { |
| 410 | logIndentLevel++; | |
| 411 | log ('Running a script: start'); | log ('Running a script: start'); |
| 412 | ||
| 413 | var doc = this.ownerDocument || this; | var doc = this.ownerDocument || this; |
| # | Line 334 | Line 426 |
| 426 | if (e.manakaiAlreadyExecuted) { | if (e.manakaiAlreadyExecuted) { |
| 427 | // 2.5. Abort these steps at this point. | // 2.5. Abort these steps at this point. |
| 428 | log ('Running a script: aborted'); | log ('Running a script: aborted'); |
| 429 | logIndentLevel--; | |
| 430 | return e; | return e; |
| 431 | } | } |
| 432 | ||
| # | Line 356 | Line 449 |
| 449 | /* && list of scripts that will execute asynchronously is not empty */) { | /* && list of scripts that will execute asynchronously is not empty */) { |
| 450 | // TODO | // TODO |
| 451 | } else if (e.src != null && e.manakaiParserInserted) { | } else if (e.src != null && e.manakaiParserInserted) { |
| 452 | // TODO | if (p.scriptExecutedWhenParserResumes) { |
| 453 | log ('Error: There is a script that will execute as soon as the parser resumes.'); | |
| 454 | } | |
| 455 | p.scriptExecutedWhenParserResumes = e; | |
| 456 | log ('Running a script: aborted (src parser-inserted)'); | |
| 457 | } else if (e.src != null) { | } else if (e.src != null) { |
| 458 | // TODO | p.scriptsExecutedSoon.push (e); |
| 459 | log ('Running a script: aborted (src)'); | |
| 460 | } else { | } else { |
| 461 | executeScript (doc, e); // even if other scripts are already executing. | executeScript (doc, e); // even if other scripts are already executing. |
| 462 | } | } |
| 463 | ||
| 464 | log ('Running a script: end'); | log ('Running a script: end'); |
| 465 | logIndentLevel--; | |
| 466 | } | } |
| 467 | ||
| 468 | return e; | return e; |
| # | Line 372 | Line 471 |
| 471 | function executeScript (doc, e) { | function executeScript (doc, e) { |
| 472 | log ('executing a script block: start'); | log ('executing a script block: start'); |
| 473 | ||
| 474 | // If the load resulted in an error, then ... firing an error event ... | var s; |
| 475 | if (e.src != null) { | |
| 476 | s = getExternalScript (e.src); | |
| 477 | ||
| 478 | // If the load resulted in an error, then ... firing an error event ... | |
| 479 | if (s == null) { | |
| 480 | log ('error event fired at the script element'); | |
| 481 | return; | |
| 482 | } | |
| 483 | ||
| 484 | log ('External script loaded: "' + s + '"'); | |
| 485 | } else { | |
| 486 | s = e.text; | |
| 487 | } | |
| 488 | ||
| 489 | // If the load was successful | // If the load was successful |
| 490 | log ('load event fired at the script element'); | & |