Parent Directory
|
Revision Log
|
Patch
| revision 1.4 by wakaba, Sun Apr 20 12:19:13 2008 UTC | revision 1.16 by wakaba, Tue Apr 29 04:07:18 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 | <link rel=author href="http://suika.fam.cx/~wakaba/who?"> | |
| 6 | <link rel=license href="http://suika.fam.cx/c/gnu/gpl" | |
| 7 | title="GNU GPL2 or later"> | |
| 8 | <style> | <style> |
| 9 | h1 { | |
| 10 | margin: 0; | |
| 11 | font-size: 150%; | |
| 12 | } | |
| 13 | h2 { | |
| 14 | margin: 0; | |
| 15 | font-size: 100%; | |
| 16 | } | |
| 17 | p { | |
| 18 | margin: 0 1em; | |
| 19 | } | |
| 20 | textarea { | textarea { |
| 21 | display: block; | width: 100%; |
| 22 | width: 80%; | -width: 99%; |
| 23 | margin-left: auto; | height: 10em; |
| margin-right: auto; | ||
| min-height: 20em; | ||
| 24 | } | } |
| 25 | output { | output { |
| 26 | display: block; | display: block; |
| # | Line 18 | Line 30 |
| 30 | } | } |
| 31 | </style> | </style> |
| 32 | <script> | <script> |
| 33 | var delayedUpdater = 0; | |
| 34 | ||
| 35 | function update () { | function update () { |
| 36 | document.logElement.textContent = ''; | if (delayedUpdater) { |
| 37 | var p = new Parser (new InputStream (document.sourceElement.value)); | clearTimeout (delayedUpdater); |
| 38 | var doc = p.doc; | delayedUpdater = 0; |
| 39 | p.parse (); | } |
| 40 | log (dumpTree (doc, '')); | delayedUpdater = setTimeout (update2, 100); |
| 41 | } // update | } // update |
| 42 | ||
| 43 | function update2 () { | |
| 44 | var v = document.sourceElement.value; | |
| 45 | if (v != document.previousSourceText) { | |
| 46 | document.previousSourceText = v; | |
| 47 | document.links['permalink'].href | |
| 48 | = location.pathname + '?s=' + encodeURIComponent (v); | |
| 49 | document.links['ldvlink'].href | |
| 50 | = 'http://software.hixie.ch/utilities/js/live-dom-viewer/?' | |
| 51 | + encodeURIComponent (v); | |
| 52 | ||
| 53 | document.logElement.textContent = ''; | |
| 54 | var p = new Parser (new InputStream (v)); | |
| 55 | var doc = p.doc; | |
| 56 | p.parse (); | |
| 57 | ||
| 58 | log (dumpTree (doc, '')); | |
| 59 | ||
| 60 | if (p.hasAsyncScript) { | |
| 61 | 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'); | |
| 62 | } | |
| 63 | } | |
| 64 | } // update2 | |
| 65 | ||
| 66 | var logIndentLevel = 0; | |
| 67 | function log (s) { | function log (s) { |
| 68 | var indent = ''; | |
| 69 | for (var i = 0; i < logIndentLevel; i++) { | |
| 70 | indent += ' '; | |
| 71 | } | |
| 72 | s = indent + s.replace (/\n/g, "\n" + indent); | |
| 73 | document.logElement.appendChild (document.createTextNode (s + "\n")); | document.logElement.appendChild (document.createTextNode (s + "\n")); |
| 74 | } // log | } // log |
| 75 | ||
| # | Line 40 | Line 83 |
| 83 | doc = new JSDocument (this); | doc = new JSDocument (this); |
| 84 | doc.manakaiIsHTML = true; | doc.manakaiIsHTML = true; |
| 85 | } | } |
| 86 | this.nextToken = []; | |
| 87 | this.doc = doc; | this.doc = doc; |
| 88 | this.openElements = [doc]; | this.openElements = [doc]; |
| 89 | this.in = i; | this.input = i; |
| 90 | this.scriptsExecutedAfterParsing = []; | this.scriptsExecutedAfterParsing = []; |
| 91 | this.scriptsExecutedSoon = []; | |
| 92 | this.scriptsExecutedAsynchronously = []; | |
| 93 | } // Parser | } // Parser |
| 94 | ||
| 95 | Parser.prototype.getNextToken = function () { | Parser.prototype.getNextToken = function () { |
| 96 | if (this.nextToken.length) { | |
| 97 | return this.nextToken.shift (); | |
| 98 | } | |
| 99 | ||
| 100 | var p = this; | var p = this; |
| 101 | var i = this.in; | var i = this.input; |
| 102 | if (this.parseMode == 'script') { | if (this.parseMode == 'cdata') { |
| 103 | var tagName = this.endTagName; | |
| 104 | var token; | var token; |
| 105 | if (p.insertionPoint <= 0) { | if (p.insertionPoint <= 0) { |
| 106 | return {type: 'abort'}; | return {type: 'abort'}; |
| # | Line 67 | Line 118 |
| 118 | return ''; | return ''; |
| 119 | }); | }); |
| 120 | if (token) return token; | if (token) return token; |
| 121 | i.s = i.s.replace (/^<\/[Ss][Cc][Rr][Ii][Pp][Tt]>/, function (s) { | var pattern = new RegExp ('^</' + tagName + '>', 'i'); |
| 122 | i.s = i.s.replace (pattern, function (s) { | |
| 123 | if (p.insertionPoint < s.length) { | if (p.insertionPoint < s.length) { |
| 124 | token = {type: 'abort'}; | token = {type: 'abort'}; |
| 125 | return s; | return s; |
| 126 | } | } |
| 127 | token = {type: 'end-tag', value: 'script'}; | token = {type: 'end-tag', value: tagName}; |
| 128 | p.insertionPoint -= s.length; | p.insertionPoint -= s.length; |
| 129 | return ''; | return ''; |
| 130 | }); | }); |
| 131 | if (token) return token; | if (token) return token; |
| 132 | var m; | |
| 133 | if ((p.insertionPoint < ('</' + tagName).length) && | |
| 134 | (m = i.s.match (/^<\/([A-Za-z]+)/))) { | |
| 135 | var v = m[1].substring (0, p.insertionPoint).toLowerCase (); | |
| 136 | if (v == tagName.substring (0, p.insertionPoint - '</'.length)) { | |
| 137 | return {type: 'abort'}; | |
| 138 | } | |
| 139 | } | |
| 140 | i.s = i.s.replace (/^</, | i.s = i.s.replace (/^</, |
| 141 | function (s) { | function (s) { |
| 142 | token = {type: 'char', value: s}; | token = {type: 'char', value: s}; |
| # | Line 88 | Line 148 |
| 148 | } | } |
| 149 | ||
| 150 | var token; | var token; |
| 151 | i.s = i.s.replace (/^<\/([^>]+)>/, function (s, e) { | i.s = i.s.replace (/^<\/([^>]+)(?:>|$)/, function (s, e) { |
| 152 | if (p.insertionPoint < s.length) { | if (p.insertionPoint < s.length || |
| 153 | (p.insertionPoint <= s.length && | |
| 154 | s.substring (s.length - 1, s.length) != '>')) { | |
| 155 | token = {type: 'abort'}; | token = {type: 'abort'}; |
| 156 | return s; | return s; |
| 157 | } | } |
| # | Line 98 | Line 160 |
| 160 | return ''; | return ''; |
| 161 | }); | }); |
| 162 | if (token) return token; | if (token) return token; |
| 163 | i.s = i.s.replace (/^<([^>]+)>/, function (s, e) { | i.s = i.s.replace (/^<([^>]+)(?:>|$)/, function (s, e) { |
| 164 | if (p.insertionPoint < s.length) { | if (p.insertionPoint < s.length || |
| 165 | (p.insertionPoint <= s.length && | |
| 166 | s.substring (s.length - 1, s.length) != '>')) { | |
| 167 | token = {type: 'abort'}; | token = {type: 'abort'}; |
| 168 | return s; | return s; |
| 169 | } | } |
| # | Line 109 | Line 173 |
| 173 | tagName = v.toLowerCase (); | tagName = v.toLowerCase (); |
| 174 | return ''; | return ''; |
| 175 | }); | }); |
| 176 | e = e.replace (/^\s*(\S+)\s*(?:=\s*"([^"]*)"|'([^']*)'|([^"']+))?/, | while (true) { |
| 177 | function (x, attrName, attrValue1, attrValue2, attrValue3) { | var m = false; |
| 178 | attrs[attrName] = attrValue1 || attrValue2 || attrValue3; | e = e.replace (/^\s*([^\s=]+)\s*(?:=\s*(?:"([^"]*)"|'([^']*)'|([^"'\s]*)))?/, |
| 179 | return ''; | function (x, attrName, attrValue1, attrValue2, attrValue3) { |
| 180 | }); | v = attrValue1 || attrValue2 || attrValue3; |
| 181 | v = v.replace (/"/g, '"').replace (/'/g, "'") | |
| 182 | .replace (/&/g, '&'); | |
| 183 | attrs[attrName.toLowerCase ()] = v; | |
| 184 | m = true; | |
| 185 | return ''; | |
| 186 | }); | |
| 187 | if (!m) break; | |
| 188 | } | |
| 189 | if (e.length) { | |
| 190 | log ('Broken start tag: "' + e + '"'); | |
| 191 | } | |
| 192 | token = {type: 'start-tag', value: tagName, attrs: attrs}; | token = {type: 'start-tag', value: tagName, attrs: attrs}; |
| 193 | p.insertionPoint -= s.length; | p.insertionPoint -= s.length; |
| 194 | return ''; | return ''; |
| # | Line 144 | Line 219 |
| 219 | } // getNextToken | } // getNextToken |
| 220 | ||
| 221 | Parser.prototype.parse = function () { | Parser.prototype.parse = function () { |
| 222 | log ('start parsing'); | logIndentLevel++; |
| 223 | log ('parse: start'); | |
| 224 | ||
| 225 | while (true) { | while (true) { |
| 226 | var token = this.getNextToken (); | var token = this.getNextToken (); |
| 227 | log ('token: ' + token.type + ' "' + token.value + '"'); | log ('token: ' + token.type + ' "' + token.value + '"'); |
| 228 | ||
| 229 | if (this.cdataEndTagRequired) { | |
| 230 | // Generic CDATA parsing algorithm | |
| 231 | ||
| 232 | if (token.type != 'abort') { | |
| 233 | // 7. | |
| 234 | if (token.type == 'end-tag' && token.value == this.endTagName) { | |
| 235 | // 7.1. Ignores it. | |