Parent Directory
|
Revision Log
|
Patch
| revision 1.7 by wakaba, Fri Apr 25 23:03:35 2008 UTC | revision 1.18 by wakaba, Sun Aug 31 09:46:14 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, h2 { | h1 { |
| 10 | margin: 0; | |
| 11 | font-size: 150%; | |
| 12 | } | |
| 13 | h2 { | |
| 14 | margin: 0; | margin: 0; |
| 15 | font-size: 100%; | font-size: 100%; |
| 16 | } | } |
| 17 | p, pre { | p { |
| 18 | margin: 0; | margin: 0 1em; |
| 19 | } | } |
| 20 | textarea { | textarea { |
| 21 | width: 100%; | width: 100%; |
| # | Line 34 | Line 41 |
| 41 | } // update | } // update |
| 42 | ||
| 43 | function update2 () { | function update2 () { |
| document.logElement.textContent = ''; | ||
| 44 | var v = document.sourceElement.value; | var v = document.sourceElement.value; |
| 45 | var p = new Parser (new InputStream (v)); | if (v != document.previousSourceText) { |
| 46 | var doc = p.doc; | document.previousSourceText = v; |
| 47 | p.parse (); | document.links['permalink'].href |
| 48 | log (dumpTree (doc, '')); | = location.pathname + '?s=' + encodeURIComponent (v); |
| 49 | document.links['ldvlink'].href | |
| 50 | document.links['permalink'].href | = 'http://software.hixie.ch/utilities/js/live-dom-viewer/?' |
| 51 | = location.href + '?s=' + encodeURIComponent (v); | + 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 | } // update2 |
| 65 | ||
| 66 | var logIndentLevel = 0; | var logIndentLevel = 0; |
| 67 | function log (s) { | function log (s) { |
| 68 | var indent = ''; | |
| 69 | for (var i = 0; i < logIndentLevel; i++) { | for (var i = 0; i < logIndentLevel; i++) { |
| 70 | s = ' ' + s; | 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 63 | 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 90 | 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; | var m; |
| 133 | if ((p.insertionPoint < '</script'.length) && | if ((p.insertionPoint < ('</' + tagName).length) && |
| 134 | (m = i.s.match (/^<\/([SCRIPTscript]+)/))) { | (m = i.s.match (/^<\/([A-Za-z]+)/))) { |
| 135 | var v = m[1].substring (0, p.insertionPoint).toLowerCase (); | var v = m[1].substring (0, p.insertionPoint).toLowerCase (); |
| 136 | if (v == 'script'.substring (0, p.insertionPoint - '</'.length)) { | if (v == tagName.substring (0, p.insertionPoint - '</'.length)) { |
| 137 | return {type: 'abort'}; | return {type: 'abort'}; |
| 138 | } | } |
| 139 | } | } |
| # | Line 122 | Line 151 |
| 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 && | (p.insertionPoint <= s.length && |
| 154 | s.substring (s.length - 1, 1) != '>')) { | s.substring (s.length - 1, s.length) != '>')) { |
| 155 | token = {type: 'abort'}; | token = {type: 'abort'}; |
| 156 | return s; | return s; |
| 157 | } | } |
| # | Line 134 | Line 163 |
| 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 && | (p.insertionPoint <= s.length &am |