40 |
} // Parser |
} // Parser |
41 |
|
|
42 |
Parser.prototype.getNextToken = function () { |
Parser.prototype.getNextToken = function () { |
43 |
|
var p = this; |
44 |
var i = this.in; |
var i = this.in; |
45 |
if (this.parseMode == 'script') { |
if (this.parseMode == 'script') { |
46 |
var token; |
var token; |
47 |
|
if (p.insertionPoint <= 0) { |
48 |
|
return {type: 'abort'}; |
49 |
|
} |
50 |
i.s = i.s.replace (/^([\s\S]+?)<\/[Ss][Cc][Rr][Ii][Pp][Tt]>/, |
i.s = i.s.replace (/^([\s\S]+?)<\/[Ss][Cc][Rr][Ii][Pp][Tt]>/, |
51 |
function (s, t) { |
function (s, t) { |
52 |
|
if (0 < p.insertionPoint && p.insertionPoint < t.length) { |
53 |
|
token = {type: 'char', value: t.substring (0, p.insertionPoint)}; |
54 |
|
var ip = p.insertionPoint; |
55 |
|
p.insertionPoint = 0; |
56 |
|
return t.substring (ip, t.length) + |
57 |
|
s.substring (s.length - 9, s.length); |
58 |
|
} |
59 |
token = {type: 'char', value: t}; |
token = {type: 'char', value: t}; |
60 |
|
p.insertionPoint -= s.length; |
61 |
return '<' + '/script>'; |
return '<' + '/script>'; |
62 |
}); |
}); |
63 |
if (token) return token; |
if (token) return token; |
64 |
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) { |
65 |
|
if (s.length < p.insertionPoint) { |
66 |
|
token = {type: 'abort'}; |
67 |
|
return s; |
68 |
|
} |
69 |
token = {type: 'end-tag', value: 'script'}; |
token = {type: 'end-tag', value: 'script'}; |
70 |
|
p.insertionPoint -= s.length; |
71 |
return ''; |
return ''; |
72 |
}); |
}); |
73 |
if (token) return token; |
if (token) return token; |
76 |
|
|
77 |
var token; |
var token; |
78 |
i.s = i.s.replace (/^<\/([^>]+)>/, function (s, e) { |
i.s = i.s.replace (/^<\/([^>]+)>/, function (s, e) { |
79 |
|
if (p.insertionPoint < s.length) { |
80 |
|
token = {type: 'abort'}; |
81 |
|
return s; |
82 |
|
} |
83 |
token = {type: 'end-tag', value: e.toLowerCase ()}; |
token = {type: 'end-tag', value: e.toLowerCase ()}; |
84 |
|
p.insertionPoint -= s.length; |
85 |
return ''; |
return ''; |
86 |
}); |
}); |
87 |
if (token) return token; |
if (token) return token; |
88 |
i.s = i.s.replace (/^<([^>]+)>/, function (s, e) { |
i.s = i.s.replace (/^<([^>]+)>/, function (s, e) { |
89 |
|
if (p.insertionPoint < s.length) { |
90 |
|
token = {type: 'abort'}; |
91 |
|
return s; |
92 |
|
} |
93 |
token = {type: 'start-tag', value: e.toLowerCase ()}; |
token = {type: 'start-tag', value: e.toLowerCase ()}; |
94 |
|
p.insertionPoint -= s.length; |
95 |
return ''; |
return ''; |
96 |
}); |
}); |
97 |
if (token) return token; |
if (token) return token; |
98 |
|
if (p.insertionPoint <= 0) { |
99 |
|
return {type: 'abort'}; |
100 |
|
} |
101 |
i.s = i.s.replace (/^[^<]+/, function (s) { |
i.s = i.s.replace (/^[^<]+/, function (s) { |
102 |
|
if (p.insertionPoint < s.length) { |
103 |
|
token = {type: 'char', value: s.substring (0, p.insertionPoint)}; |
104 |
|
var ip = p.insertionPoint; |
105 |
|
p.insertionPoint = 0; |
106 |
|
return s.substring (ip, s.length); |
107 |
|
} |
108 |
token = {type: 'char', value: s}; |
token = {type: 'char', value: s}; |
109 |
|
p.insertionPoint -= s.length; |
110 |
return ''; |
return ''; |
111 |
}); |
}); |
112 |
if (token) return token; |
if (token) return token; |
113 |
i.s = i.s.replace (/^[\s\S]/, function (s) { |
i.s = i.s.replace (/^[\s\S]/, function (s) { |
114 |
token = {type: 'char', value: s}; |
token = {type: 'char', value: s}; |
115 |
|
p.insertionPoint -= s.length; |
116 |
return ''; |
return ''; |
117 |
}); |
}); |
118 |
if (token) return token; |
if (token) return token; |
147 |
el.manakaiAppendText (token.value); |
el.manakaiAppendText (token.value); |
148 |
|
|
149 |
// 4.2. Until it returns a token that is not a character token, or |
// 4.2. Until it returns a token that is not a character token, or |
150 |
// TODO: 4.3. Until it stops tokenising. |
// until it stops tokenising. |
151 |
} else if (token.type == 'eof' || |
} else if (token.type == 'eof' || |
152 |
(token.type == 'end-tag' && token.value == 'script')) { |
(token.type == 'end-tag' && token.value == 'script') || |
153 |
|
token.type == 'abort') { |
154 |
// 6. Switched back to the PCDATA state. |
// 6. Switched back to the PCDATA state. |
155 |
this.parseMode = 'pcdata'; |
this.parseMode = 'pcdata'; |
156 |
|
|
177 |
} |
} |
178 |
|
|
179 |
// 9.1. Let the old insertion point have the same value as the ... |
// 9.1. Let the old insertion point have the same value as the ... |
180 |
|
var oldInsertionPoint = this.insertionPoint; |
181 |
// 9.2. Let the insertion point be just before the next input ... |
// 9.2. Let the insertion point be just before the next input ... |
182 |
|
this.setInsertionPoint (0); |
183 |
|
|
184 |
// 10. Append the new element to the current node. |
// 10. Append the new element to the current node. |
185 |
this.openElements[this.openElements.length - 1].appendChild (el); |
this.openElements[this.openElements.length - 1].appendChild (el); |
186 |
|
|
187 |
// 11. Let the insertion point have the value of the old ... |
// 11. Let the insertion point have the value of the old ... |
188 |
|
this.setInsertionPoint (oldInsertionPoint); |
189 |
|
|
190 |
// 12. If there is a script that will execute as soon as ... |
// 12. If there is a script that will execute as soon as ... |
191 |
|
|
202 |
} else { |
} else { |
203 |
log ('parse error: unmatched end tag: ' + token.value); |
log ('parse error: unmatched end tag: ' + token.value); |
204 |
} |
} |
205 |
|
} else if (token.type == 'char') { |
206 |
|
this.openElements[this.openElements.length - 1].manakaiAppendText |
207 |
|
(token.value); |
208 |
} else if (token.type == 'eof') { |
} else if (token.type == 'eof') { |
209 |
break; |
break; |
210 |
|
} else if (token.type == 'abort') { |
211 |
|
log ('parse: abort'); |
212 |
|
return; |
213 |
} |
} |
214 |
} |
} |
215 |
|
|
216 |
log ('stop parsing'); |
log ('stop parsing'); |
217 |
} // parse |
} // parse |
218 |
|
|
219 |
|
Parser.prototype.setInsertionPoint = function (ip) { |
220 |
|
if (ip == undefined || ip == null || isNaN (ip)) { |
221 |
|
log ('insertion point: set to undefined'); |
222 |
|
this.insertionPoint = undefined; |
223 |
|
} else { |
224 |
|
log ('insertion point: set to ' + ip + |
225 |
|
' (before "' + this.in.s.substring (0, 10) + '")'); |
226 |
|
this.insertionPoint = ip; |
227 |
|
} |
228 |
|
}; // setInsertionPoint |
229 |
|
|
230 |
function JSDocument (p) { |
function JSDocument (p) { |
231 |
this.childNodes = []; |
this.childNodes = []; |
232 |
this._parser = p; |
this._parser = p; |
246 |
if (e.localName == 'script') { |
if (e.localName == 'script') { |
247 |
log ('start running a script'); |
log ('start running a script'); |
248 |
|
|
249 |
var doc = this.ownerDocument; |
var doc = this.ownerDocument || this; |
250 |
var p = doc._parser; |
var p = doc._parser; |
251 |
|
|
252 |
// 1. Script type |
// 1. Script type |
358 |
}; // manakaiAppendText |
}; // manakaiAppendText |
359 |
|
|
360 |
JSDocument.prototype.write = function () { |
JSDocument.prototype.write = function () { |
361 |
|
var p = this._parser; |
362 |
|
|
363 |
// 1. If the insertion point is undefined, the open() method must be ... |
// 1. If the insertion point is undefined, the open() method must be ... |
364 |
// |
if (p.insertionPoint == NaN || p.insertionPoint == undefined) { |
365 |
|
// TODO: open () |
366 |
|
} |
367 |
|
|
368 |
// 2. ... inserted into the input stream just before the insertion point. |
// 2. ... inserted into the input stream just before the insertion point. |
369 |
log ('document.write: insert "' + Array.join (arguments, '') + '"'); |
var s = Array.join (arguments, ''); |
370 |
|
log ('document.write: insert "' + s + '"' + |
371 |
|
' before "' + p.in.s.substring (p.insertionPoint, p.insertionPoint + 10) + '"'); |
372 |
|
p.in.s = p.in.s.substring (0, p.insertionPoint) + s |
373 |
|
+ p.in.s.substring (p.insertionPoint, p.in.s.length); |
374 |
|
p.insertionPoint += s.length; |
375 |
|
|
376 |
// 3. If there is a script that will execute as soon as the parser resumes |
// 3. If there is a script that will execute as soon as the parser resumes |
377 |
// TODO |
// TODO |
378 |
|
|
379 |
// 4. Process the characters that were inserted, ... |
// 4. Process the characters that were inserted, ... |
380 |
|
p.parse (); |
381 |
|
|
382 |
// 5. Return |
// 5. Return |
383 |
log ('document.write: return'); |
log ('document.write: return'); |
421 |
<head></head><body> |
<head></head><body> |
422 |
<p> |
<p> |
423 |
<script> |
<script> |
424 |
document.write ('aaaaaaa</p>\n<script>\ndocument.write("cccccc")\n</', 'script>\nbbbbbb'); |
document.write ('aaaaaaa</p><script>document.write("cccccc");</', 'script>bbbbbb'); |
425 |
</script> |
</script> |
426 |
<p> |
<p> |
427 |
</textarea> |
</textarea> |