Parent Directory
|
Revision Log
|
Patch
| revision 1.1 by wakaba, Sun Apr 20 06:07:24 2008 UTC | revision 1.6 by wakaba, Fri Apr 25 13:42:51 2008 UTC | |
|---|---|---|
| # | Line 13 | Line 13 |
| 13 | output { | output { |
| 14 | display: block; | display: block; |
| 15 | font-family: monospace; | font-family: monospace; |
| 16 | white-space: pre; | white-space: -moz-pre-wrap; |
| 17 | white-space: pre-wrap; | |
| 18 | } | } |
| 19 | </style> | </style> |
| 20 | <script> | <script> |
| 21 | function update () { | function update () { |
| 22 | document.logElement.textContent = ''; | document.logElement.textContent = ''; |
| 23 | var p = new Parser (); | var p = new Parser (new InputStream (document.sourceElement.value)); |
| 24 | p.parse (new InputStream (document.sourceElement.value)); | var doc = p.doc; |
| 25 | log (dumpTree (p.doc, '')); | p.parse (); |
| 26 | log (dumpTree (doc, '')); | |
| 27 | } // update | } // update |
| 28 | ||
| 29 | var logIndentLevel = 0; | |
| 30 | function log (s) { | function log (s) { |
| 31 | for (var i = 0; i < logIndentLevel; i++) { | |
| 32 | s = ' ' + s; | |
| 33 | } | |
| 34 | document.logElement.appendChild (document.createTextNode (s + "\n")); | document.logElement.appendChild (document.createTextNode (s + "\n")); |
| 35 | } // log | } // log |
| 36 | ||
| # | Line 32 | Line 38 |
| 38 | this.s = s; | this.s = s; |
| 39 | } // InputStream | } // InputStream |
| 40 | ||
| 41 | function Parser () { | function Parser (i, doc) { |
| 42 | this.parseMode = 'pcdata'; | this.parseMode = 'pcdata'; |
| 43 | this.doc = new JSDocument (); | if (!doc) { |
| 44 | this.openElements = [this.doc]; | doc = new JSDocument (this); |
| 45 | doc.manakaiIsHTML = true; | |
| 46 | } | |
| 47 | this.doc = doc; | |
| 48 | this.openElements = [doc]; | |
| 49 | this.in = i; | |
| 50 | this.scriptsExecutedAfterParsing = []; | |
| 51 | } // Parser | } // Parser |
| 52 | ||
| 53 | Parser.prototype.getNextToken = function (i) { | Parser.prototype.getNextToken = function () { |
| 54 | var p = this; | |
| 55 | var i = this.in; | |
| 56 | if (this.parseMode == 'script') { | if (this.parseMode == 'script') { |
| 57 | var token; | var token; |
| 58 | i.s = i.s.replace (/^([\s\S]+?)<\/[Ss][Cc][Rr][Ii][Pp][Tt]>/, | if (p.insertionPoint <= 0) { |
| 59 | return {type: 'abort'}; | |
| 60 | } | |
| 61 | i.s = i.s.replace (/^([^<]+)/, | |
| 62 | function (s, t) { | function (s, t) { |
| 63 | if (0 < p.insertionPoint && p.insertionPoint < t.length) { | |
| 64 | token = {type: 'char', value: t.substring (0, p.insertionPoint)}; | |
| 65 | var ip = p.insertionPoint; | |
| 66 | p.insertionPoint = 0; | |
| 67 | return t.substring (ip, t.length); | |
| 68 | } | |
| 69 | token = {type: 'char', value: t}; | token = {type: 'char', value: t}; |
| 70 | return '<' + '/script>'; | p.insertionPoint -= t.length; |
| 71 | return ''; | |
| 72 | }); | }); |
| 73 | if (token) return token; | if (token) return token; |
| 74 | i.s = i.s.replace (/^<\/[Ss][Cc][Rr][Ii][Pp][Tt]>/, function () { | i.s = i.s.replace (/^<\/[Ss][Cc][Rr][Ii][Pp][Tt]>/, function (s) { |
| 75 | if (p.insertionPoint < s.length) { | |
| 76 | token = {type: 'abort'}; | |
| 77 | return s; | |
| 78 | } | |
| 79 | token = {type: 'end-tag', value: 'script'}; | token = {type: 'end-tag', value: 'script'}; |
| 80 | p.insertionPoint -= s.length; | |
| 81 | return ''; | |
| 82 | }); | |
| 83 | if (token) return token; | |
| 84 | var m; | |
| 85 | if ((p.insertionPoint < '</script'.length) && | |
| 86 | (m = i.s.match (/^<\/([SCRIPTscript]+)/))) { | |
| 87 | var v = m[1].substring (0, p.insertionPoint).toLowerCase (); | |
| 88 | if (v == 'script'.substring (0, p.insertionPoint - '</'.length)) { | |
| 89 | return {type: 'abort'}; | |
| 90 | } | |
| 91 | } | |
| 92 | i.s = i.s.replace (/^</, | |
| 93 | function (s) { | |
| 94 | token = {type: 'char', value: s}; | |
| 95 | p.insertionPoint -= s.length; | |
| 96 | return ''; | return ''; |
| 97 | }); | }); |
| 98 | if (token) return token; | if (token) return token; |
| # | Line 56 | Line 100 |
| 100 | } | } |
| 101 | ||
| 102 | var token; | var token; |
| 103 | i.s = i.s.replace (/^<\/([^>]+)>/, function (s, e) { | i.s = i.s.replace (/^<\/([^>]+)(?:>|$)/, function (s, e) { |
| 104 | if (p.insertionPoint < s.length || | |
| 105 | (p.insertionPoint <= s.length && | |
| 106 | s.substring (s.length - 1, 1) != '>')) { | |
| 107 | token = {type: 'abort'}; | |
| 108 | return s; | |
| 109 | } | |
| 110 | token = {type: 'end-tag', value: e.toLowerCase ()}; | token = {type: 'end-tag', value: e.toLowerCase ()}; |
| 111 | p.insertionPoint -= s.length; | |
| 112 | return ''; | return ''; |
| 113 | }); | }); |
| 114 | if (token) return token; | if (token) return token; |
| 115 | i.s = i.s.replace (/^<([^>]+)>/, function (s, e) { | i.s = i.s.replace (/^<([^>]+)(?:>|$)/, function (s, e) { |
| 116 | token = {type: 'start-tag', value: e.toLowerCase ()}; | if (p.insertionPoint < s.length || |
| 117 | (p.insertionPoint <= s.length && | |
| 118 | s.substring (s.length - 1, 1) != '>')) { | |
| 119 | token = {type: 'abort'}; | |
| 120 | return s; | |
| 121 | } | |
| 122 | var tagName; | |
| 123 | var attrs = {}; | |
| 124 | e = e.replace (/^[\S]+/, function (v) { | |
| 125 | tagName = v.toLowerCase (); | |
| 126 | return ''; | |
| 127 | }); | |
| 128 | e = e.replace (/^\s*([^\s=]+)\s*(?:=\s*(?:"([^"]*)"|'([^']*)'|([^"']+)))?/, | |
| 129 | function (x, attrName, attrValue1, attrValue2, attrValue3) { | |
| 130 | v = attrValue1 || attrValue2 || attrValue3; | |
| 131 | v = v.replace (/"/g, '"').replace (/'/g, "'") | |
| 132 | .replace (/&/g, '&'); | |
| 133 | attrs[attrName.toLowerCase ()] = v; | |
| 134 | return ''; | |
| 135 | }); | |
| 136 | if (e.length) { | |
| 137 | log ('Broken start tag: "' + e + '"'); | |
| 138 | } | |
| 139 | token = {type: 'start-tag', value: tagName, attrs: attrs}; | |
| 140 | p.insertionPoint -= s.length; | |
| 141 | return ''; | return ''; |
| 142 | }); | }); |
| 143 | if (token) return token; | if (token) return token; |
| 144 | if (p.insertionPoint <= 0) { | |
| 145 | return {type: 'abort'}; | |
| 146 | } | |
| 147 | i.s = i.s.replace (/^[^<]+/, function (s) { | i.s = i.s.replace (/^[^<]+/, function (s) { |
| 148 | if (p.insertionPoint < s.length) { | |
| 149 | token = {type: 'char', value: s.substring (0, p.insertionPoint)}; | |
| 150 | var ip = p.insertionPoint; | |
| 151 | p.insertionPoint = 0; | |
| 152 | return s.substring (ip, s.length); | |
| 153 | } | |
| 154 | token = {type: 'char', value: s}; | token = {type: 'char', value: s}; |
| 155 | p.insertionPoint -= s.length; | |
| 156 | return ''; | return ''; |
| 157 | }); | }); |
| 158 | if (token) return token; | if (token) return token; |
| 159 | i.s = i.s.replace (/^[\s\S]/, function (s) { | i.s = i.s.replace (/^[\s\S]/, function (s) { |
| 160 | token = {type: 'char', value: s}; | token = {type: 'char', value: s}; |
| 161 | p.insertionPoint -= s.length; | |
| 162 | return ''; | return ''; |
| 163 | }); | }); |
| 164 | if (token) return token; | if (token) return token; |
| 165 | return {type: 'eof'}; | return {type: 'eof'}; |
| 166 | } // getNextToken | } // getNextToken |
| 167 | ||
| 168 | Parser.prototype.parse = function (i) { | Parser.prototype.parse = function () { |
| 169 | log ('start parsing'); | logIndentLevel++; |
| 170 | log ('parse: start'); | |
| 171 | ||
| 172 | while (true) { | while (true) { |
| 173 | var token = this.getNextToken (i); | var token = this.getNextToken (); |
| 174 | log ('token: ' + token.type + ' "' + token.value + '"'); | log ('token: ' + token.type + ' "' + token.value + '"'); |
| 175 | ||
| 176 | if (token.type == 'start-tag') { | if (token.type == 'start-tag') { |
| var el = new JSElement (token.value); | ||
| 177 | if (token.value == 'script') { | if (token.value == 'script') { |
| 178 | // 1. Create an element for the token in the HTML namespace. | |
| 179 | var el = new JSElement (this.doc, token.value); | |
| 180 | if (token.attrs.async != null) el.async = true; | |
| 181 | if (token.attrs.defer != null) el.defer = true; | |
| 182 | if (token.attrs.src != null) el.src = token.attrs.src; | |
| 183 | ||
| 184 | // 2. Mark the element as being "parser-inserted". | |
| 185 | el.manakaiParserInserted = true; | |
| 186 | ||
| 187 | // 3. Switch the tokeniser's content model flag to the CDATA state. | |
| 188 | this.parseMode = 'script'; | this.parseMode = 'script'; |
| 189 | ||
| 190 | // 4.1. Collect all the character tokens. | |
| 191 | while (true) { | while (true) { |
| 192 | var token = this.getNextToken (i); | var token = this.getNextToken (); |
| 193 | log ('token: ' + token.type + ' "' + token.value + '"'); | log ('token: ' + token.type + ' "' + token.value + '"'); |
| 194 | ||
| 195 | if (token.type == 'char') { | if (token.type == 'char') { |
| 196 | // 5. Append a single Text node to the script element node. | |
| 197 | el.manakaiAppendText (token.value); | el.manakaiAppendText (token.value); |
| 198 | ||
| 199 | // 4.2. Until it returns a token that is not a character token, or | |
| 200 | // until it stops tokenising. | |
| 201 | } else if (token.type == 'eof' || | } else if (token.type == 'eof' || |
| 202 | (token.type == 'end-tag' && token.value == 'script')) { | (token.type == 'end-tag' && token.value == 'script') || |
| 203 | token.type == 'abort') { | |
| 204 | // 6. Switched back to the PCDATA state. | |
| 205 | this.parseMode = 'pcdata'; | this.parseMode = 'pcdata'; |
| 206 | ||
| 207 | // 7.1. If the next token is not an end tag token with ... | |
| 208 | if (token.type != 'end-tag') { | |
| 209 | // 7.2. This is a parse error. | |
| 210 | log ('Parse error: no </' + 'script>'); | |
| 211 | ||
| 212 | // 7.3. Mark the script element as "already executed". | |
| 213 | el.manakaiAlreadyExecuted = true; | |
| 214 | } else { | |
| 215 | // 7.4. Ignore it. | |
| 216 | // | |
| 217 | } | |
| 218 | break; | break; |
| 219 | } | } |
| 220 | } | } |
| 221 | ||
| 222 | // 8.1. If the parser were originally created for the ... | |
| 223 | if (this.fragmentParsingMode) { | |
| 224 | // 8.2. Mark the script element as "already executed" and ... | |
| 225 | el.alreadyExecuted = true; | |
| 226 | continue; | |
| 227 | } | |
| 228 | ||
| 229 | // 9.1. Let the old insertion point have the same value as the ... | |
| 230 | var oldInsertionPoint = this.insertionPoint; | |
| 231 | // 9.2. Let the insertion point be just before the next input ... | |
| 232 | this.setInsertionPoint (0); | |
| 233 | ||
| 234 | // 10. Append the new element to the current node. | |
| 235 | this.openElements[this.openElements.length - 1].appendChild (el); | this.openElements[this.openElements.length - 1].appendChild (el); |
| 236 | ||
| 237 | // 11. Let the insertion point have the value of the old ... | |
| 238 | oldInsertionPoint += this.insertionPoint; | |
| 239 | this.setInsertionPoint (oldInsertionPoint); | |
| 240 | ||
| 241 | // 12. If there is a script that will execute as soon as ... | |
| 242 | while (this.scriptExecutedWhenParserResumes) { | |
| 243 | // 12.1. If the tree construction stage is being called reentrantly | |
| 244 | if (this.reentrant) { | |
| 245 | log ('parse: abort (reentrance)'); | |
| 246 | logIndentLevel--; | |
| 247 | return; | |
| 248 | ||
| 249 | // 12.2. Otherwise | |
| 250 | } else { | |
| 251 | // 1. | |
| 252 | var script = this.scriptExecutedWhenParserResumes; | |
| 253 | this.scriptExecutedWhenParserResumes = null; | |
| 254 | ||
| 255 | // 2. Pause until the script has completed loading. | |
| 256 | // | |
| 257 | ||
| 258 | // 3. Let the insertion point to just before the next input char. | |
| 259 | this.setInsertionPoint (0); | |
| 260 | ||
| 261 | // 4. Execute the script. | |
| 262 | executeScript (this.doc, script); | |
| 263 | ||
| 264 | // 5. Let the insertion point be undefined again. | |
| 265 | this.setInsertionPoint (undefined); | |
| 266 | ||
| 267 | // 6. If there is once again a script that will execute ... | |
| 268 | // | |
| 269 | } | |
| 270 | } | |
| 271 | } else { | } else { |
| 272 | var el = new JSElement (this.doc, token.value); | |
| 273 | this.openElements[this.openElements.length - 1].appendChild (el); | this.openElements[this.openElements.length - 1].appendChild (el); |
| 274 | this.openElements.push (el); | this.openElements.push (el); |
| 275 | } | } |
| # | Line 116 | Line 280 |
| 280 | } else { | } else { |
| 281 | log ('parse error: unmatched end tag: ' + token.value); | log ('parse error: unmatched end tag: ' + token.value); |
| 282 | } | } |
| 283 | } else if (token.type == 'char') { | |
| 284 | this.openElements[this.openElements.length - 1].manakaiAppendText | |
| 285 | (token.value); | |
| 286 | } else if (token.type == 'eof') { | } else if (token.type == 'eof') { |
| 287 | break; | break; |
| 288 | } else if (token.type == 'abort') { | |
| 289 | log ('parse: abort'); | |
| 290 | logIndentLevel--; | |
| 291 | return; | |
| 292 | } | } |
| 293 | } | } |
| 294 | ||
| 295 | log ('stop parsing'); | log ('stop parsing'); |
| 296 | ||
| 297 | // readyState = 'interactive' | |
| 298 | ||
| 299 | // "When a script completes loading" rules start applying. | |
| 300 | ||
| 301 | // TODO: Handles "list of scripts that will execute as soon as possible" | |
| 302 | // and "list of scripts that will execute asynchronously" | |
| 303 | ||
| 304 | // Handle "list of scripts that will execute when the document has finished | |
| 305 | // parsing". | |
| 306 | var list = this.scriptsExecutedAfterParsing; | |
| 307 | while (list.length > 0) { | |
| 308 | // TODO: break unless completed loading | |
| 309 | ||
| 310 | // Step 1. | |
| 311 | // | |
| 312 | ||
| 313 | // Step 2. and Step 3. | |
| 314 | log ('Executing a |defer|red script...'); | |
| 315 | executeScript (this.doc, list.shift ()); | |
| 316 | ||
| 317 | // Step 4. | |
| 318 | } | |
| 319 | ||
| 320 | log ('DOMContentLoaded event fired'); | |
| 321 | ||