Parent Directory
|
Revision Log
|
Patch
| revision 1.5 by wakaba, Fri Apr 25 11:40:56 2008 UTC | revision 1.7 by wakaba, Fri Apr 25 23:03:35 2008 UTC | |
|---|---|---|
| # | Line 3 | Line 3 |
| 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; |
| # | Line 18 | Line 23 |
| 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 | ||
| # | Line 121 | Line 144 |
| 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 ''; |
| # | Line 156 | Line 185 |
| 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 (); |
| # | Line 224 | Line 254 |
| 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; | 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) { | |