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 |
|
|
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.input = i; |
this.input = i; |
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.input; |
var i = this.input; |
102 |
if (this.parseMode == 'cdata') { |
if (this.parseMode == 'cdata') { |
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 |
} |
} |
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 && |
166 |
s.substring (s.length - 1, 1) != '>')) { |
s.substring (s.length - 1, s.length) != '>')) { |
167 |
token = {type: 'abort'}; |
token = {type: 'abort'}; |
168 |
return s; |
return s; |
169 |
} |
} |
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. |
236 |
|
// |
237 |
|
} else { |
238 |
|
// 7.2. Parse error. |
239 |
|
log ('Parse error: no </' + this.endTagName + '>'); |
240 |
|
this.nextToken.unshift (token); |
241 |
|
} |
242 |
|
this.cdataEndTagRequired = false; |
243 |
|
continue; |
244 |
|
} |
245 |
|
} |
246 |
|
|
247 |
if (token.type == 'start-tag') { |
if (token.type == 'start-tag') { |
248 |
if (token.value == 'script') { |
if (token.value == 'script') { |
249 |
// 1. Create an element for the token in the HTML namespace. |
// 1. Create an element for the token in the HTML namespace. |
280 |
if (!(token.type == 'end-tag' && token.value == 'script')) { |
if (!(token.type == 'end-tag' && token.value == 'script')) { |
281 |
// 7.2. This is a parse error. |
// 7.2. This is a parse error. |
282 |
log ('Parse error: no </' + 'script>'); |
log ('Parse error: no </' + 'script>'); |
283 |
|
this.nextToken.unshift (token); |
284 |
|
|
285 |
// 7.3. Mark the script element as "already executed". |
// 7.3. Mark the script element as "already executed". |
286 |
el.manakaiAlreadyExecuted = true; |
el.manakaiAlreadyExecuted = true; |
372 |
// 6. Switched back to the PCDATA state. |
// 6. Switched back to the PCDATA state. |
373 |
this.parseMode = 'pcdata'; |
this.parseMode = 'pcdata'; |
374 |
|
|
375 |
|
if (token.type == 'abort') { |
376 |
|
this.cdataEndTagRequired = true; |
377 |
|
break; |
378 |
|
} |
379 |
|
|
380 |
// 7.1. If the next token is not an end tag token with ... |
// 7.1. If the next token is not an end tag token with ... |
381 |
if (!(token.type == 'end-tag' && |
if (!(token.type == 'end-tag' && |
382 |
token.value == this.endTagName)) { |
token.value == this.endTagName)) { |
383 |
// 7.2. This is a parse error. |
// 7.2. This is a parse error. |
384 |
log ('Parse error: no </' + this.endTagName + '>'); |
log ('Parse error: no </' + this.endTagName + '>'); |
385 |
|
this.nextToken.unshift (token); |
386 |
|
|
387 |
// 7.3. Mark the script element as "already executed". |
// 7.3. Mark the script element as "already executed". |
388 |
el.manakaiAlreadyExecuted = true; |
el.manakaiAlreadyExecuted = true; |
533 |
// 2.4. If the script element has its "already executed" flag set |
// 2.4. If the script element has its "already executed" flag set |
534 |
if (e.manakaiAlreadyExecuted) { |
if (e.manakaiAlreadyExecuted) { |
535 |
// 2.5. Abort these steps at this point. |
// 2.5. Abort these steps at this point. |
536 |
log ('Running a script: aborted'); |
log ('Running a script: aborted (already executed)'); |
537 |
logIndentLevel--; |
logIndentLevel--; |
538 |
return e; |
return e; |
539 |
} |
} |
644 |
doc.write.apply (doc, args); |
doc.write.apply (doc, args); |
645 |
return ''; |
return ''; |
646 |
}); |
}); |
647 |
s = s.replace (/^\s*var\s+s\s*=\s*document\.createElement\s*\(\s*['"]script['"]\s*\)\s*;\s*s\.src\s*=\s*(?:'(javascript:[^']*)'|"(javascript:[^"]*)")\s*;\s*document\.documentElement\.appendChild\s*\(\s*s\s*\)\s*;\s*/, |
var noDocumentElement = false; |
648 |
|
s = s.replace (/^\s*var\s+s\s*=\s*document\.createElement\s*\(\s*['"]script['"]\s*\)\s*;\s*s\.src\s*=\s*(?:'([^']*)'|"([^"]*)")\s*;\s*document\.documentElement\.appendChild\s*\(\s*s\s*\)\s*;\s*/, |
649 |
function (s, t, u) { |
function (s, t, u) { |
650 |
matched = true; |
matched = true; |
651 |
var args = [unescapeJSLiteral (t ? t : u)]; |
var args = [unescapeJSLiteral (t ? t : u)]; |
652 |
doc._insertExternalScript.apply (doc, args); |
noDocumentElement = !doc._insertExternalScript.apply (doc, args); |
653 |
|
return ''; |
654 |
|
}); |
655 |
|
if (noDocumentElement) { |
656 |
|
log ('Script error: documentElement is null'); |
657 |
|
break; |
658 |
|
} |
659 |
|
s = s.replace (/^\s*w\s*\(\s*document\.documentElement\.innerHTML\s*\)\s*;\s*/, |
660 |
|
function (s, t) { |
661 |
|
matched = true; |
662 |
|
log (dumpTree (doc, '')); |
663 |
return ''; |
return ''; |
664 |
}); |
}); |
665 |
if (s == '') break; |
if (s == '') break; |
744 |
}; // document.open |
}; // document.open |
745 |
|
|
746 |
JSDocument.prototype.write = function () { |
JSDocument.prototype.write = function () { |
747 |
|
log ('document.write: start'); |
748 |
logIndentLevel++; |
logIndentLevel++; |
749 |
|
|
750 |
var p = this._parser; |
var p = this._parser; |
768 |
if (p.scriptExecutedAfterParserResumes) { |
if (p.scriptExecutedAfterParserResumes) { |
769 |
log ('document.write: processed later (there is an unprocessed <script src>)'); |
log ('document.write: processed later (there is an unprocessed <script src>)'); |
770 |
logIndentLevel--; |
logIndentLevel--; |
771 |
|
log ('document.write: return'); |
772 |
return; |
return; |
773 |
} |
} |
774 |
|
|
782 |
// to do something here? |
// to do something here? |
783 |
|
|
784 |
// 5. Return |
// 5. Return |
785 |
|
logIndentLevel--; |
786 |
log ('document.write: return'); |
log ('document.write: return'); |
787 |
|
|
|
logIndentLevel--; |
|
788 |
return; |
return; |
789 |
}; // document.write |
}; // document.write |
790 |
|
|
791 |
JSDocument.prototype._insertExternalScript = function (uri) { |
JSDocument.prototype._insertExternalScript = function (uri) { |
792 |
var s = new JSElement (this, 'script'); |
var s = new JSElement (this, 'script'); |
793 |
s.src = uri; |
s.src = uri; |
794 |
this.documentElement.appendChild (s); |
if (this.documentElement) { |
795 |
|
this.documentElement.appendChild (s); |
796 |
|
return true; |
797 |
|
} else { |
798 |
|
return false; |
799 |
|
} |
800 |
}; // _insertExternalScript |
}; // _insertExternalScript |
801 |
|
|
802 |
JSDocument.prototype.__defineGetter__ ('documentElement', function () { |
JSDocument.prototype.__defineGetter__ ('documentElement', function () { |
913 |
<li><code>var s = document.createElement ("script"); |
<li><code>var s = document.createElement ("script"); |
914 |
s.src = "<var>string</var>"; |
s.src = "<var>string</var>"; |
915 |
document.documentElement.appendChild (s);</code> |
document.documentElement.appendChild (s);</code> |
916 |
|
<li><code>w (document.documentElement.innerHTML);</code> (This statement |
917 |
|
can be used to dump the document, even when the document has no |
918 |
|
document element. The output format is the tree dump format used |
919 |
|
in html5lib test data, not <abbr>HTML</abbr>.) |
920 |
</ul> |
</ul> |
921 |
Note that strings may be delimited by <code>'</code>s instead of |
Note that strings may be delimited by <code>'</code>s instead of |
922 |
<code>"</code>s. |
<code>"</code>s. |