/[suikacvs]/markup/html/whatpm/Whatpm/HTML.pm
Suika

Contents of /markup/html/whatpm/Whatpm/HTML.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.217 - (hide annotations) (download)
Sun Jul 26 10:56:23 2009 UTC (17 years ago) by wakaba
Branch: MAIN
Changes since 1.216: +11 -5 lines
++ whatpm/t/ChangeLog	26 Jul 2009 10:52:36 -0000
	* tree-test-foreign.dat: Added xmlns:xlink test cases (cf. HTML5
	revision 2701).

2009-07-26  Wakaba  <wakaba@suika.fam.cx>

++ whatpm/Whatpm/ChangeLog	26 Jul 2009 10:53:56 -0000
	* mkhtmlclass.pl: Made xmlns:xlink with wrong namespace URL
	invalid (HTML5 revision 2701).

2009-07-26  Wakaba  <wakaba@suika.fam.cx>

1 wakaba 1.2 package Whatpm::HTML;
2 wakaba 1.1 use strict;
3 wakaba 1.217 our $VERSION=do{my @r=(q$Revision: 1.220 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4 wakaba 1.65 use Error qw(:try);
5 wakaba 1.1
6 wakaba 1.203 use Whatpm::HTML::Tokenizer;
7    
8 wakaba 1.179 ## NOTE: This module don't check all HTML5 parse errors; character
9     ## encoding related parse errors are expected to be handled by relevant
10     ## modules.
11     ## Parse errors for control characters that are not allowed in HTML5
12     ## documents, for surrogate code points, and for noncharacter code
13     ## points, as well as U+FFFD substitions for characters whose code points
14     ## is higher than U+10FFFF may be detected by combining the parser with
15     ## the checker implemented by Whatpm::Charset::UnicodeChecker (for its
16     ## usage example, see |t/HTML-tree.t| in the Whatpm package or the
17     ## WebHACC::Language::HTML module in the WebHACC package).
18    
19 wakaba 1.18 ## ISSUE:
20     ## var doc = implementation.createDocument (null, null, null);
21     ## doc.write ('');
22     ## alert (doc.compatMode);
23 wakaba 1.1
24 wakaba 1.136 require IO::Handle;
25    
26 wakaba 1.203 ## Namespace URLs
27    
28 wakaba 1.123 my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
29     my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
30     my $SVG_NS = q<http://www.w3.org/2000/svg>;
31     my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
32     my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
33     my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
34    
35 wakaba 1.203 ## Element categories
36    
37 wakaba 1.201 ## Bits 12-15
38     sub SPECIAL_EL () { 0b1_000000000000000 }
39     sub SCOPING_EL () { 0b1_00000000000000 }
40     sub FORMATTING_EL () { 0b1_0000000000000 }
41     sub PHRASING_EL () { 0b1_000000000000 }
42    
43     ## Bits 10-11
44 wakaba 1.203 #sub FOREIGN_EL () { 0b1_00000000000 } # see Whatpm::HTML::Tokenizer
45 wakaba 1.201 sub FOREIGN_FLOW_CONTENT_EL () { 0b1_0000000000 }
46    
47     ## Bits 6-9
48     sub TABLE_SCOPING_EL () { 0b1_000000000 }
49     sub TABLE_ROWS_SCOPING_EL () { 0b1_00000000 }
50     sub TABLE_ROW_SCOPING_EL () { 0b1_0000000 }
51     sub TABLE_ROWS_EL () { 0b1_000000 }
52    
53     ## Bit 5
54     sub ADDRESS_DIV_P_EL () { 0b1_00000 }
55    
56     ## NOTE: Used in </body> and EOF algorithms.
57     ## Bit 4
58     sub ALL_END_TAG_OPTIONAL_EL () { 0b1_0000 }
59 wakaba 1.121
60 wakaba 1.149 ## NOTE: Used in "generate implied end tags" algorithm.
61 wakaba 1.189 ## NOTE: There is a code where a modified version of
62     ## END_TAG_OPTIONAL_EL is used in "generate implied end tags"
63     ## implementation (search for the algorithm name).
64 wakaba 1.201 ## Bit 3
65     sub END_TAG_OPTIONAL_EL () { 0b1_000 }
66    
67     ## Bits 0-2
68    
69     sub MISC_SPECIAL_EL () { SPECIAL_EL | 0b000 }
70     sub FORM_EL () { SPECIAL_EL | 0b001 }
71     sub FRAMESET_EL () { SPECIAL_EL | 0b010 }
72     sub HEADING_EL () { SPECIAL_EL | 0b011 }
73     sub SELECT_EL () { SPECIAL_EL | 0b100 }
74     sub SCRIPT_EL () { SPECIAL_EL | 0b101 }
75    
76     sub ADDRESS_DIV_EL () { SPECIAL_EL | ADDRESS_DIV_P_EL | 0b001 }
77     sub BODY_EL () { SPECIAL_EL | ALL_END_TAG_OPTIONAL_EL | 0b001 }
78    
79 wakaba 1.202 sub DTDD_EL () {
80 wakaba 1.201 SPECIAL_EL |
81     END_TAG_OPTIONAL_EL |
82     ALL_END_TAG_OPTIONAL_EL |
83     0b010
84     }
85     sub LI_EL () {
86     SPECIAL_EL |
87     END_TAG_OPTIONAL_EL |
88     ALL_END_TAG_OPTIONAL_EL |
89     0b100
90     }
91     sub P_EL () {
92     SPECIAL_EL |
93     ADDRESS_DIV_P_EL |
94     END_TAG_OPTIONAL_EL |
95     ALL_END_TAG_OPTIONAL_EL |
96     0b001
97 wakaba 1.121 }
98    
99 wakaba 1.201 sub TABLE_ROW_EL () {
100     SPECIAL_EL |
101     TABLE_ROWS_EL |
102     TABLE_ROW_SCOPING_EL |
103     ALL_END_TAG_OPTIONAL_EL |
104     0b001
105     }
106     sub TABLE_ROW_GROUP_EL () {
107     SPECIAL_EL |
108     TABLE_ROWS_EL |
109     TABLE_ROWS_SCOPING_EL |
110     ALL_END_TAG_OPTIONAL_EL |
111     0b001
112 wakaba 1.121 }
113    
114 wakaba 1.201 sub MISC_SCOPING_EL () { SCOPING_EL | 0b000 }
115     sub BUTTON_EL () { SCOPING_EL | 0b001 }
116     sub CAPTION_EL () { SCOPING_EL | 0b010 }
117     sub HTML_EL () {
118     SCOPING_EL |
119     TABLE_SCOPING_EL |
120     TABLE_ROWS_SCOPING_EL |
121     TABLE_ROW_SCOPING_EL |
122     ALL_END_TAG_OPTIONAL_EL |
123     0b001
124 wakaba 1.121 }
125 wakaba 1.201 sub TABLE_EL () {
126     SCOPING_EL |
127     TABLE_ROWS_EL |
128     TABLE_SCOPING_EL |
129     0b001
130 wakaba 1.121 }
131 wakaba 1.201 sub TABLE_CELL_EL () {
132     SCOPING_EL |
133     TABLE_ROW_SCOPING_EL |
134     ALL_END_TAG_OPTIONAL_EL |
135     0b001
136 wakaba 1.121 }
137    
138 wakaba 1.201 sub MISC_FORMATTING_EL () { FORMATTING_EL | 0b000 }
139     sub A_EL () { FORMATTING_EL | 0b001 }
140     sub NOBR_EL () { FORMATTING_EL | 0b010 }
141    
142     sub RUBY_EL () { PHRASING_EL | 0b001 }
143    
144     ## ISSUE: ALL_END_TAG_OPTIONAL_EL?
145     sub OPTGROUP_EL () { PHRASING_EL | END_TAG_OPTIONAL_EL | 0b001 }
146     sub OPTION_EL () { PHRASING_EL | END_TAG_OPTIONAL_EL | 0b010 }
147     sub RUBY_COMPONENT_EL () { PHRASING_EL | END_TAG_OPTIONAL_EL | 0b100 }
148 wakaba 1.121
149 wakaba 1.201 sub MML_AXML_EL () { PHRASING_EL | FOREIGN_EL | 0b001 }
150 wakaba 1.121
151     my $el_category = {
152 wakaba 1.201 a => A_EL,
153     address => ADDRESS_DIV_EL,
154 wakaba 1.121 applet => MISC_SCOPING_EL,
155     area => MISC_SPECIAL_EL,
156 wakaba 1.188 article => MISC_SPECIAL_EL,
157     aside => MISC_SPECIAL_EL,
158 wakaba 1.121 b => FORMATTING_EL,
159     base => MISC_SPECIAL_EL,
160     basefont => MISC_SPECIAL_EL,
161     bgsound => MISC_SPECIAL_EL,
162     big => FORMATTING_EL,
163     blockquote => MISC_SPECIAL_EL,
164     body => BODY_EL,
165     br => MISC_SPECIAL_EL,
166     button => BUTTON_EL,
167     caption => CAPTION_EL,
168     center => MISC_SPECIAL_EL,
169     col => MISC_SPECIAL_EL,
170     colgroup => MISC_SPECIAL_EL,
171 wakaba 1.188 command => MISC_SPECIAL_EL,
172     datagrid => MISC_SPECIAL_EL,
173 wakaba 1.202 dd => DTDD_EL,
174 wakaba 1.188 details => MISC_SPECIAL_EL,
175     dialog => MISC_SPECIAL_EL,
176 wakaba 1.121 dir => MISC_SPECIAL_EL,
177 wakaba 1.201 div => ADDRESS_DIV_EL,
178 wakaba 1.121 dl => MISC_SPECIAL_EL,
179 wakaba 1.202 dt => DTDD_EL,
180 wakaba 1.121 em => FORMATTING_EL,
181     embed => MISC_SPECIAL_EL,
182 wakaba 1.188 eventsource => MISC_SPECIAL_EL,
183 wakaba 1.121 fieldset => MISC_SPECIAL_EL,
184 wakaba 1.188 figure => MISC_SPECIAL_EL,
185 wakaba 1.121 font => FORMATTING_EL,
186 wakaba 1.188 footer => MISC_SPECIAL_EL,
187 wakaba 1.121 form => FORM_EL,
188     frame => MISC_SPECIAL_EL,
189     frameset => FRAMESET_EL,
190     h1 => HEADING_EL,
191     h2 => HEADING_EL,
192     h3 => HEADING_EL,
193     h4 => HEADING_EL,
194     h5 => HEADING_EL,
195     h6 => HEADING_EL,
196     head => MISC_SPECIAL_EL,
197 wakaba 1.188 header => MISC_SPECIAL_EL,
198 wakaba 1.121 hr => MISC_SPECIAL_EL,
199     html => HTML_EL,
200     i => FORMATTING_EL,
201     iframe => MISC_SPECIAL_EL,
202     img => MISC_SPECIAL_EL,
203 wakaba 1.188 #image => MISC_SPECIAL_EL, ## NOTE: Commented out in the spec.
204 wakaba 1.121 input => MISC_SPECIAL_EL,
205     isindex => MISC_SPECIAL_EL,
206     li => LI_EL,
207     link => MISC_SPECIAL_EL,
208     listing => MISC_SPECIAL_EL,
209     marquee => MISC_SCOPING_EL,
210     menu => MISC_SPECIAL_EL,
211     meta => MISC_SPECIAL_EL,
212 wakaba 1.188 nav => MISC_SPECIAL_EL,
213 wakaba 1.201 nobr => NOBR_EL,
214 wakaba 1.121 noembed => MISC_SPECIAL_EL,
215     noframes => MISC_SPECIAL_EL,
216     noscript => MISC_SPECIAL_EL,
217     object => MISC_SCOPING_EL,
218     ol => MISC_SPECIAL_EL,
219     optgroup => OPTGROUP_EL,
220     option => OPTION_EL,
221     p => P_EL,
222     param => MISC_SPECIAL_EL,
223     plaintext => MISC_SPECIAL_EL,
224     pre => MISC_SPECIAL_EL,
225 wakaba 1.149 rp => RUBY_COMPONENT_EL,
226     rt => RUBY_COMPONENT_EL,
227     ruby => RUBY_EL,
228 wakaba 1.121 s => FORMATTING_EL,
229     script => MISC_SPECIAL_EL,
230     select => SELECT_EL,
231 wakaba 1.188 section => MISC_SPECIAL_EL,
232 wakaba 1.121 small => FORMATTING_EL,
233     spacer => MISC_SPECIAL_EL,
234     strike => FORMATTING_EL,
235     strong => FORMATTING_EL,
236     style => MISC_SPECIAL_EL,
237     table => TABLE_EL,
238     tbody => TABLE_ROW_GROUP_EL,
239     td => TABLE_CELL_EL,
240     textarea => MISC_SPECIAL_EL,
241     tfoot => TABLE_ROW_GROUP_EL,
242     th => TABLE_CELL_EL,
243     thead => TABLE_ROW_GROUP_EL,
244     title => MISC_SPECIAL_EL,
245     tr => TABLE_ROW_EL,
246     tt => FORMATTING_EL,
247     u => FORMATTING_EL,
248     ul => MISC_SPECIAL_EL,
249     wbr => MISC_SPECIAL_EL,
250     };
251    
252 wakaba 1.123 my $el_category_f = {
253     $MML_NS => {
254     'annotation-xml' => MML_AXML_EL,
255 wakaba 1.201 mi => FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
256     mo => FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
257     mn => FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
258     ms => FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
259     mtext => FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
260 wakaba 1.123 },
261     $SVG_NS => {
262 wakaba 1.201 foreignObject => SCOPING_EL | FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
263     desc => FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
264     title => FOREIGN_EL | FOREIGN_FLOW_CONTENT_EL,
265 wakaba 1.123 },
266     ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
267     };
268    
269 wakaba 1.128 my $svg_attr_name = {
270 wakaba 1.143 attributename => 'attributeName',
271 wakaba 1.128 attributetype => 'attributeType',
272     basefrequency => 'baseFrequency',
273     baseprofile => 'baseProfile',
274     calcmode => 'calcMode',
275     clippathunits => 'clipPathUnits',
276     contentscripttype => 'contentScriptType',
277     contentstyletype => 'contentStyleType',
278     diffuseconstant => 'diffuseConstant',
279     edgemode => 'edgeMode',
280     externalresourcesrequired => 'externalResourcesRequired',
281     filterres => 'filterRes',
282     filterunits => 'filterUnits',
283     glyphref => 'glyphRef',
284     gradienttransform => 'gradientTransform',
285     gradientunits => 'gradientUnits',
286     kernelmatrix => 'kernelMatrix',
287     kernelunitlength => 'kernelUnitLength',
288     keypoints => 'keyPoints',
289     keysplines => 'keySplines',
290     keytimes => 'keyTimes',
291     lengthadjust => 'lengthAdjust',
292     limitingconeangle => 'limitingConeAngle',
293     markerheight => 'markerHeight',
294     markerunits => 'markerUnits',
295     markerwidth => 'markerWidth',
296     maskcontentunits => 'maskContentUnits',
297     maskunits => 'maskUnits',
298     numoctaves => 'numOctaves',
299     pathlength => 'pathLength',
300     patterncontentunits => 'patternContentUnits',
301     patterntransform => 'patternTransform',
302     patternunits => 'patternUnits',
303     pointsatx => 'pointsAtX',
304     pointsaty => 'pointsAtY',
305     pointsatz => 'pointsAtZ',
306     preservealpha => 'preserveAlpha',
307     preserveaspectratio => 'preserveAspectRatio',
308     primitiveunits => 'primitiveUnits',
309     refx => 'refX',
310     refy => 'refY',
311     repeatcount => 'repeatCount',
312     repeatdur => 'repeatDur',
313     requiredextensions => 'requiredExtensions',
314 wakaba 1.143 requiredfeatures => 'requiredFeatures',
315 wakaba 1.128 specularconstant => 'specularConstant',
316     specularexponent => 'specularExponent',
317     spreadmethod => 'spreadMethod',
318     startoffset => 'startOffset',
319     stddeviation => 'stdDeviation',
320     stitchtiles => 'stitchTiles',
321     surfacescale => 'surfaceScale',
322     systemlanguage => 'systemLanguage',
323     tablevalues => 'tableValues',
324     targetx => 'targetX',
325     targety => 'targetY',
326     textlength => 'textLength',
327     viewbox => 'viewBox',
328     viewtarget => 'viewTarget',
329     xchannelselector => 'xChannelSelector',
330     ychannelselector => 'yChannelSelector',
331     zoomandpan => 'zoomAndPan',
332     };
333    
334     my $foreign_attr_xname = {
335     'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
336     'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
337     'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
338     'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
339     'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
340     'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
341     'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
342     'xml:base' => [$XML_NS, ['xml', 'base']],
343     'xml:lang' => [$XML_NS, ['xml', 'lang']],
344     'xml:space' => [$XML_NS, ['xml', 'space']],
345     'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
346     'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
347     };
348    
349 wakaba 1.129 ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
350    
351 wakaba 1.188 ## TODO: Invoke the reset algorithm when a resettable element is
352     ## created (cf. HTML5 revision 2259).
353    
354 wakaba 1.65 sub parse_byte_string ($$$$;$) {
355 wakaba 1.135 my $self = shift;
356     my $charset_name = shift;
357     open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
358     return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
359     } # parse_byte_string
360    
361 wakaba 1.158 sub parse_byte_stream ($$$$;$$) {
362     # my ($self, $charset_name, $byte_stream, $doc, $onerror, $get_wrapper) = @_;
363 wakaba 1.65 my $self = ref $_[0] ? shift : shift->new;
364 wakaba 1.130 my $charset_name = shift;
365 wakaba 1.135 my $byte_stream = $_[0];
366 wakaba 1.130
367 wakaba 1.131 my $onerror = $_[2] || sub {
368     my (%opt) = @_;
369     warn "Parse error ($opt{type})\n";
370     };
371     $self->{parse_error} = $onerror; # updated later by parse_char_string
372    
373 wakaba 1.158 my $get_wrapper = $_[3] || sub ($) {
374     return $_[0]; # $_[0] = byte stream handle, returned = arg to char handle
375     };
376    
377 wakaba 1.130 ## HTML5 encoding sniffing algorithm
378     require Message::Charset::Info;
379     my $charset;
380 wakaba 1.133 my $buffer;
381     my ($char_stream, $e_status);
382 wakaba 1.130
383     SNIFFING: {
384 wakaba 1.156 ## NOTE: By setting |allow_fallback| option true when the
385     ## |get_decode_handle| method is invoked, we ignore what the HTML5
386     ## spec requires, i.e. unsupported encoding should be ignored.
387     ## TODO: We should not do this unless the parser is invoked
388     ## in the conformance checking mode, in which this behavior
389     ## would be useful.
390 wakaba 1.130
391     ## Step 1
392     if (defined $charset_name) {
393 wakaba 1.157 $charset = Message::Charset::Info->get_by_html_name ($charset_name);
394     ## TODO: Is this ok? Transfer protocol's parameter should be
395     ## interpreted in its semantics?
396 wakaba 1.130
397 wakaba 1.133 ($char_stream, $e_status) = $charset->get_decode_handle
398     ($byte_stream, allow_error_reporting => 1,
399 wakaba 1.130 allow_fallback => 1);
400 wakaba 1.133 if ($char_stream) {
401 wakaba 1.130 $self->{confident} = 1;
402     last SNIFFING;
403 wakaba 1.133 } else {
404 wakaba 1.186 $self->{parse_error}->(level => $self->{level}->{must}, type => 'charset:not supported',
405 wakaba 1.187 layer => 'encode',
406 wakaba 1.186 line => 1, column => 1,
407     value => $charset_name,
408     level => $self->{level}->{uncertain});
409 wakaba 1.130 }
410     }
411    
412     ## Step 2
413 wakaba 1.133 my $byte_buffer = '';
414     for (1..1024) {
415     my $char = $byte_stream->getc;
416     last unless defined $char;
417     $byte_buffer .= $char;
418     } ## TODO: timeout
419 wakaba 1.130
420     ## Step 3
421 wakaba 1.133 if ($byte_buffer =~ /^\xFE\xFF/) {
422 wakaba 1.157 $charset = Message::Charset::Info->get_by_html_name ('utf-16be');
423 wakaba 1.133 ($char_stream, $e_status) = $charset->get_decode_handle
424     ($byte_stream, allow_error_reporting => 1,
425     allow_fallback => 1, byte_buffer => \$byte_buffer);
426 wakaba 1.130 $self->{confident} = 1;
427     last SNIFFING;
428 wakaba 1.133 } elsif ($byte_buffer =~ /^\xFF\xFE/) {
429 wakaba 1.157 $charset = Message::Charset::Info->get_by_html_name ('utf-16le');
430 wakaba 1.133 ($char_stream, $e_status) = $charset->get_decode_handle
431     ($byte_stream, allow_error_reporting => 1,
432     allow_fallback => 1, byte_buffer => \$byte_buffer);
433 wakaba 1.130 $self->{confident} = 1;
434     last SNIFFING;
435 wakaba 1.133 } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
436 wakaba 1.157 $charset = Message::Charset::Info->get_by_html_name ('utf-8');
437 wakaba 1.133 ($char_stream, $e_status) = $charset->get_decode_handle
438     ($byte_stream, allow_error_reporting => 1,
439     allow_fallback => 1, byte_buffer => \$byte_buffer);
440 wakaba 1.130 $self->{confident} = 1;
441     last SNIFFING;
442     }
443    
444     ## Step 4
445     ## TODO: <meta charset>
446    
447     ## Step 5
448     ## TODO: from history
449    
450     ## Step 6
451 wakaba 1.67 require Whatpm::Charset::UniversalCharDet;
452 wakaba 1.130 $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
453 wakaba 1.133 ($byte_buffer);
454 wakaba 1.130 if (defined $charset_name) {
455 wakaba 1.157 $charset = Message::Charset::Info->get_by_html_name ($charset_name);
456 wakaba 1.130
457 wakaba 1.133 require Whatpm::Charset::DecodeHandle;
458     $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
459     ($byte_stream);
460     ($char_stream, $e_status) = $charset->get_decode_handle
461     ($buffer, allow_error_reporting => 1,
462     allow_fallback => 1, byte_buffer => \$byte_buffer);
463     if ($char_stream) {
464     $buffer->{buffer} = $byte_buffer;
465 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'sniffing:chardet',
466     text => $charset_name,
467     level => $self->{level}->{info},
468     layer => 'encode',
469 wakaba 1.131 line => 1, column => 1);
470 wakaba 1.130 $self->{confident} = 0;
471     last SNIFFING;
472     }
473     }
474    
475     ## Step 7: default
476     ## TODO: Make this configurable.
477 wakaba 1.157 $charset = Message::Charset::Info->get_by_html_name ('windows-1252');
478 wakaba 1.130 ## NOTE: We choose |windows-1252| here, since |utf-8| should be
479     ## detectable in the step 6.
480 wakaba 1.133 require Whatpm::Charset::DecodeHandle;
481     $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
482     ($byte_stream);
483     ($char_stream, $e_status)
484     = $charset->get_decode_handle ($buffer,
485     allow_error_reporting => 1,
486     allow_fallback => 1,
487     byte_buffer => \$byte_buffer);
488     $buffer->{buffer} = $byte_buffer;
489 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'sniffing:default',
490     text => 'windows-1252',
491     level => $self->{level}->{info},
492     line => 1, column => 1,
493     layer => 'encode');
494 wakaba 1.65 $self->{confident} = 0;
495 wakaba 1.130 } # SNIFFING
496    
497     if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
498 wakaba 1.156 $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
499 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'chardecode:fallback',
500 wakaba 1.156 #text => $self->{input_encoding},
501 wakaba 1.150 level => $self->{level}->{uncertain},
502     line => 1, column => 1,
503     layer => 'encode');
504 wakaba 1.130 } elsif (not ($e_status &
505 wakaba 1.174 Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL ())) {
506 wakaba 1.156 $self->{input_encoding} = $charset->get_iana_name;
507 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'chardecode:no error',
508     text => $self->{input_encoding},
509     level => $self->{level}->{uncertain},
510     line => 1, column => 1,
511     layer => 'encode');
512 wakaba 1.156 } else {
513     $self->{input_encoding} = $charset->get_iana_name;
514 wakaba 1.65 }
515    
516     $self->{change_encoding} = sub {
517     my $self = shift;
518 wakaba 1.131 $charset_name = shift;
519 wakaba 1.112 my $token = shift;
520 wakaba 1.65
521 wakaba 1.157 $charset = Message::Charset::Info->get_by_html_name ($charset_name);
522 wakaba 1.133 ($char_stream, $e_status) = $charset->get_decode_handle
523     ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
524     byte_buffer => \ $buffer->{buffer});
525 wakaba 1.131
526 wakaba 1.133 if ($char_stream) { # if supported
527 wakaba 1.131 ## "Change the encoding" algorithm:
528 wakaba 1.209
529     ## Step 1
530     if (defined $self->{input_encoding} and
531     $self->{input_encoding} eq $charset_name) {
532     $self->{parse_error}->(level => $self->{level}->{must}, type => 'charset label:matching',
533     text => $charset_name,
534     level => $self->{level}->{info});
535     $self->{confident} = 1;
536     return;
537     }
538 wakaba 1.131
539 wakaba 1.208 ## Step 2 (HTML5 revision 3205)
540     if (defined $self->{input_encoding} and
541     Message::Charset::Info->get_by_html_name ($self->{input_encoding})
542     ->{category} & Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
543     $self->{confident} = 1;
544     return;
545     }
546    
547     ## Step 3
548 wakaba 1.146 if ($charset->{category} &
549     Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
550 wakaba 1.157 $charset = Message::Charset::Info->get_by_html_name ('utf-8');
551 wakaba 1.133 ($char_stream, $e_status) = $charset->get_decode_handle
552     ($byte_stream,
553     byte_buffer => \ $buffer->{buffer});
554 wakaba 1.131 }
555     $charset_name = $charset->get_iana_name;
556 wakaba 1.65
557 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'charset label detected',
558     text => $self->{input_encoding},
559     value => $charset_name,
560     level => $self->{level}->{warn},
561     token => $token);
562 wakaba 1.131
563 wakaba 1.208 ## Step 4
564 wakaba 1.131 # if (can) {
565     ## change the encoding on the fly.
566     #$self->{confident} = 1;
567     #return;
568     # }
569    
570 wakaba 1.208 ## Step 5
571 wakaba 1.131 throw Whatpm::HTML::RestartParser ();
572 wakaba 1.65 }
573     }; # $self->{change_encoding}
574    
575 wakaba 1.133 my $char_onerror = sub {
576     my (undef, $type, %opt) = @_;
577 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, layer => 'encode',
578 wakaba 1.170 line => $self->{line}, column => $self->{column} + 1,
579     %opt, type => $type);
580 wakaba 1.133 if ($opt{octets}) {
581     ${$opt{octets}} = "\x{FFFD}"; # relacement character
582     }
583     };
584 wakaba 1.158
585     my $wrapped_char_stream = $get_wrapper->($char_stream);
586     $wrapped_char_stream->onerror ($char_onerror);
587 wakaba 1.133
588 wakaba 1.178 my @args = ($_[1], $_[2]); # $doc, $onerror - $get_wrapper = undef;
589 wakaba 1.65 my $return;
590     try {
591 wakaba 1.158 $return = $self->parse_char_stream ($wrapped_char_stream, @args);
592 wakaba 1.65 } catch Whatpm::HTML::RestartParser with {
593 wakaba 1.131 ## NOTE: Invoked after {change_encoding}.
594    
595     if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
596 wakaba 1.156 $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
597 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'chardecode:fallback',
598     level => $self->{level}->{uncertain},
599 wakaba 1.156 #text => $self->{input_encoding},
600 wakaba 1.150 line => 1, column => 1,
601     layer => 'encode');
602 wakaba 1.131 } elsif (not ($e_status &
603 wakaba 1.174 Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL ())) {
604 wakaba 1.156 $self->{input_encoding} = $charset->get_iana_name;
605 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'chardecode:no error',
606     text => $self->{input_encoding},
607     level => $self->{level}->{uncertain},
608     line => 1, column => 1,
609     layer => 'encode');
610 wakaba 1.156 } else {
611     $self->{input_encoding} = $charset->get_iana_name;
612 wakaba 1.131 }
613 wakaba 1.65 $self->{confident} = 1;
614 wakaba 1.158
615     $wrapped_char_stream = $get_wrapper->($char_stream);
616     $wrapped_char_stream->onerror ($char_onerror);
617    
618     $return = $self->parse_char_stream ($wrapped_char_stream, @args);
619 wakaba 1.65 };
620     return $return;
621 wakaba 1.135 } # parse_byte_stream
622 wakaba 1.65
623 wakaba 1.71 ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
624     ## and the HTML layer MUST ignore it. However, we does strip BOM in
625     ## the encoding layer and the HTML layer does not ignore any U+FEFF,
626     ## because the core part of our HTML parser expects a string of character,
627     ## not a string of bytes or code units or anything which might contain a BOM.
628     ## Therefore, any parser interface that accepts a string of bytes,
629     ## such as |parse_byte_string| in this module, must ensure that it does
630     ## strip the BOM and never strip any ZWNBSP.
631    
632 wakaba 1.158 sub parse_char_string ($$$;$$) {
633     #my ($self, $s, $doc, $onerror, $get_wrapper) = @_;
634 wakaba 1.132 my $self = shift;
635 wakaba 1.136 my $s = ref $_[0] ? $_[0] : \($_[0]);
636 wakaba 1.167 require Whatpm::Charset::DecodeHandle;
637     my $input = Whatpm::Charset::DecodeHandle::CharString->new ($s);
638 wakaba 1.132 return $self->parse_char_stream ($input, @_[1..$#_]);
639     } # parse_char_string
640 wakaba 1.158 *parse_string = \&parse_char_string; ## NOTE: Alias for backward compatibility.
641 wakaba 1.65
642 wakaba 1.178 sub parse_char_stream ($$$;$$) {
643 wakaba 1.65 my $self = ref $_[0] ? shift : shift->new;
644 wakaba 1.132 my $input = $_[0];
645 wakaba 1.1 $self->{document} = $_[1];
646 wakaba 1.65 @{$self->{document}->child_nodes} = ();
647 wakaba 1.1
648 wakaba 1.3 ## NOTE: |set_inner_html| copies most of this method's code
649    
650 wakaba 1.65 $self->{confident} = 1 unless exists $self->{confident};
651 wakaba 1.66 $self->{document}->input_encoding ($self->{input_encoding})
652     if defined $self->{input_encoding};
653 wakaba 1.174 ## TODO: |{input_encoding}| is needless?
654 wakaba 1.65
655 wakaba 1.110 $self->{line_prev} = $self->{line} = 1;
656 wakaba 1.175 $self->{column_prev} = -1;
657     $self->{column} = 0;
658 wakaba 1.179 $self->{set_nc} = sub {
659 wakaba 1.1 my $self = shift;
660 wakaba 1.13
661 wakaba 1.174 my $char = '';
662 wakaba 1.179 if (defined $self->{next_nc}) {
663     $char = $self->{next_nc};
664     delete $self->{next_nc};
665     $self->{nc} = ord $char;
666 wakaba 1.136 } else {
667 wakaba 1.175 $self->{char_buffer} = '';
668     $self->{char_buffer_pos} = 0;
669    
670     my $count = $input->manakai_read_until
671 wakaba 1.178 ($self->{char_buffer}, qr/[^\x00\x0A\x0D]/, $self->{char_buffer_pos});
672 wakaba 1.175 if ($count) {
673     $self->{line_prev} = $self->{line};
674     $self->{column_prev} = $self->{column};
675     $self->{column}++;
676 wakaba 1.179 $self->{nc}
677 wakaba 1.175 = ord substr ($self->{char_buffer}, $self->{char_buffer_pos}++, 1);
678     return;
679     }
680    
681 wakaba 1.174 if ($input->read ($char, 1)) {
682 wakaba 1.179 $self->{nc} = ord $char;
683 wakaba 1.174 } else {
684 wakaba 1.179 $self->{nc} = -1;
685 wakaba 1.174 return;
686     }
687 wakaba 1.136 }
688 wakaba 1.110
689     ($self->{line_prev}, $self->{column_prev})
690     = ($self->{line}, $self->{column});
691     $self->{column}++;
692 wakaba 1.1
693 wakaba 1.179 if ($self->{nc} == 0x000A) { # LF
694 wakaba 1.129
695 wakaba 1.110 $self->{line}++;
696     $self->{column} = 0;
697 wakaba 1.179 } elsif ($self->{nc} == 0x000D) { # CR
698 wakaba 1.129
699 wakaba 1.166 ## TODO: support for abort/streaming
700 wakaba 1.174 my $next = '';
701     if ($input->read ($next, 1) and $next ne "\x0A") {
702 wakaba 1.179 $self->{next_nc} = $next;
703 wakaba 1.132 }
704 wakaba 1.179 $self->{nc} = 0x000A; # LF # MUST
705 wakaba 1.110 $self->{line}++;
706     $self->{column} = 0;
707 wakaba 1.179 } elsif ($self->{nc} == 0x0000) { # NULL
708 wakaba 1.129
709 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'NULL');
710 wakaba 1.179 $self->{nc} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
711 wakaba 1.1 }
712     };
713    
714 wakaba 1.168 $self->{read_until} = sub {
715     #my ($scalar, $specials_range, $offset) = @_;
716 wakaba 1.179 return 0 if defined $self->{next_nc};
717 wakaba 1.176
718 wakaba 1.178 my $pattern = qr/[^$_[1]\x00\x0A\x0D]/;
719 wakaba 1.176 my $offset = $_[2] || 0;
720    
721     if ($self->{char_buffer_pos} < length $self->{char_buffer}) {
722     pos ($self->{char_buffer}) = $self->{char_buffer_pos};
723     if ($self->{char_buffer} =~ /\G(?>$pattern)+/) {
724     substr ($_[0], $offset)
725     = substr ($self->{char_buffer}, $-[0], $+[0] - $-[0]);
726     my $count = $+[0] - $-[0];
727     if ($count) {
728     $self->{column} += $count;
729     $self->{char_buffer_pos} += $count;
730     $self->{line_prev} = $self->{line};
731     $self->{column_prev} = $self->{column} - 1;
732 wakaba 1.179 $self->{nc} = -1;
733 wakaba 1.176 }
734     return $count;
735     } else {
736     return 0;
737     }
738     } else {
739     my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
740     if ($count) {
741     $self->{column} += $count;
742     $self->{line_prev} = $self->{line};
743     $self->{column_prev} = $self->{column} - 1;
744 wakaba 1.179 $self->{nc} = -1;
745 wakaba 1.176 }
746     return $count;
747 wakaba 1.168 }
748     }; # $self->{read_until}
749 wakaba 1.167
750 wakaba 1.3 my $onerror = $_[2] || sub {
751     my (%opt) = @_;
752 wakaba 1.110 my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
753     my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
754     warn "Parse error ($opt{type}) at line $line column $column\n";
755 wakaba 1.3 };
756     $self->{parse_error} = sub {
757 wakaba 1.110 $onerror->(line => $self->{line}, column => $self->{column}, @_);
758 wakaba 1.1 };
759    
760 wakaba 1.178 my $char_onerror = sub {
761     my (undef, $type, %opt) = @_;
762     $self->{parse_error}->(level => $self->{level}->{must}, layer => 'encode',
763     line => $self->{line}, column => $self->{column} + 1,
764     %opt, type => $type);
765     }; # $char_onerror
766    
767     if ($_[3]) {
768     $input = $_[3]->($input);
769     $input->onerror ($char_onerror);
770     } else {
771     $input->onerror ($char_onerror) unless defined $input->onerror;
772     }
773    
774 wakaba 1.1 $self->_initialize_tokenizer;
775     $self->_initialize_tree_constructor;
776     $self->_construct_tree;
777     $self->_terminate_tree_constructor;
778    
779 wakaba 1.110 delete $self->{parse_error}; # remove loop
780    
781 wakaba 1.1 return $self->{document};
782 wakaba 1.132 } # parse_char_stream
783 wakaba 1.1
784     sub new ($) {
785     my $class = shift;
786 wakaba 1.131 my $self = bless {
787 wakaba 1.150 level => {must => 'm',
788 wakaba 1.155 should => 's',
789 wakaba 1.150 warn => 'w',
790     info => 'i',
791     uncertain => 'u'},
792 wakaba 1.131 }, $class;
793 wakaba 1.179 $self->{set_nc} = sub {
794     $self->{nc} = -1;
795 wakaba 1.1 };
796     $self->{parse_error} = sub {
797     #
798     };
799 wakaba 1.65 $self->{change_encoding} = sub {
800     # if ($_[0] is a supported encoding) {
801     # run "change the encoding" algorithm;
802     # throw Whatpm::HTML::RestartParser (charset => $new_encoding);
803     # }
804     };
805 wakaba 1.63 $self->{application_cache_selection} = sub {
806     #
807     };
808 wakaba 1.1 return $self;
809     } # new
810    
811 wakaba 1.203 ## Insertion modes
812 wakaba 1.57
813 wakaba 1.56 sub AFTER_HTML_IMS () { 0b100 }
814     sub HEAD_IMS () { 0b1000 }
815     sub BODY_IMS () { 0b10000 }
816 wakaba 1.58 sub BODY_TABLE_IMS () { 0b100000 }
817 wakaba 1.56 sub TABLE_IMS () { 0b1000000 }
818 wakaba 1.58 sub ROW_IMS () { 0b10000000 }
819 wakaba 1.56 sub BODY_AFTER_IMS () { 0b100000000 }
820     sub FRAME_IMS () { 0b1000000000 }
821 wakaba 1.101 sub SELECT_IMS () { 0b10000000000 }
822 wakaba 1.203 #sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 } # see Whatpm::HTML::Tokenizer
823 wakaba 1.123 ## NOTE: "in foreign content" insertion mode is special; it is combined
824     ## with the secondary insertion mode. In this parser, they are stored
825     ## together in the bit-or'ed form.
826 wakaba 1.200 sub IN_CDATA_RCDATA_IM () { 0b1000000000000 }
827     ## NOTE: "in CDATA/RCDATA" insertion mode is also special; it is
828     ## combined with the original insertion mode. In thie parser,
829     ## they are stored together in the bit-or'ed form.
830 wakaba 1.56
831 wakaba 1.206 sub IM_MASK () { 0b11111111111 }
832    
833 wakaba 1.85 ## NOTE: "initial" and "before html" insertion modes have no constants.
834 wakaba 1.84
835     ## NOTE: "after after body" insertion mode.
836 wakaba 1.56 sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
837 wakaba 1.84
838     ## NOTE: "after after frameset" insertion mode.
839 wakaba 1.56 sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
840 wakaba 1.84
841 wakaba 1.56 sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
842     sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
843     sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
844     sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
845     sub IN_BODY_IM () { BODY_IMS }
846 wakaba 1.58 sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
847     sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
848     sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
849     sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
850 wakaba 1.56 sub IN_TABLE_IM () { TABLE_IMS }
851     sub AFTER_BODY_IM () { BODY_AFTER_IMS }
852     sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
853     sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
854 wakaba 1.101 sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
855     sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
856 wakaba 1.56 sub IN_COLUMN_GROUP_IM () { 0b10 }
857    
858 wakaba 1.1 sub _initialize_tree_constructor ($) {
859     my $self = shift;
860     ## NOTE: $self->{document} MUST be specified before this method is called
861     $self->{document}->strict_error_checking (0);
862     ## TODO: Turn mutation events off # MUST
863     ## TODO: Turn loose Document option (manakai extension) on
864 wakaba 1.18 $self->{document}->manakai_is_html (1); # MUST
865 wakaba 1.151 $self->{document}->set_user_data (manakai_source_line => 1);
866     $self->{document}->set_user_data (manakai_source_column => 1);
867 wakaba 1.1 } # _initialize_tree_constructor
868    
869     sub _terminate_tree_constructor ($) {
870     my $self = shift;
871     $self->{document}->strict_error_checking (1);
872     ## TODO: Turn mutation events on
873     } # _terminate_tree_constructor
874    
875     ## ISSUE: Should append_child (for example) in script executed in tree construction stage fire mutation events?
876    
877 wakaba 1.3 { # tree construction stage
878     my $token;
879    
880 wakaba 1.1 sub _construct_tree ($) {
881     my ($self) = @_;
882    
883     ## When an interactive UA render the $self->{document} available
884     ## to the user, or when it begin accepting user input, are
885     ## not defined.
886    
887     $token = $self->_get_next_token;
888    
889 wakaba 1.3 undef $self->{form_element};
890     undef $self->{head_element};
891 wakaba 1.197 undef $self->{head_element_inserted};
892 wakaba 1.3 $self->{open_elements} = [];
893     undef $self->{inner_html_node};
894 wakaba 1.201 undef $self->{ignore_newline};
895 wakaba 1.3
896 wakaba 1.84 ## NOTE: The "initial" insertion mode.
897 wakaba 1.3 $self->_tree_construction_initial; # MUST
898 wakaba 1.84
899 wakaba 1.85 ## NOTE: The "before html" insertion mode.
900 wakaba 1.3 $self->_tree_construction_root_element;
901 wakaba 1.84 $self->{insertion_mode} = BEFORE_HEAD_IM;
902    
903     ## NOTE: The "before head" insertion mode and so on.
904 wakaba 1.3 $self->_tree_construction_main;
905     } # _construct_tree
906    
907     sub _tree_construction_initial ($) {
908     my $self = shift;
909 wakaba 1.84
910     ## NOTE: "initial" insertion mode
911    
912 wakaba 1.18 INITIAL: {
913 wakaba 1.57 if ($token->{type} == DOCTYPE_TOKEN) {
914 wakaba 1.18 ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
915     ## error, switch to a conformance checking mode for another
916     ## language.
917     my $doctype_name = $token->{name};
918     $doctype_name = '' unless defined $doctype_name;
919 wakaba 1.155 $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
920 wakaba 1.18 if (not defined $token->{name} or # <!DOCTYPE>
921 wakaba 1.179 defined $token->{sysid}) {
922 wakaba 1.79
923 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'not HTML5', token => $token);
924 wakaba 1.18 } elsif ($doctype_name ne 'HTML') {
925 wakaba 1.79
926 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'not HTML5', token => $token);
927 wakaba 1.179 } elsif (defined $token->{pubid}) {
928     if ($token->{pubid} eq 'XSLT-compat') {
929 wakaba 1.155
930     $self->{parse_error}->(level => $self->{level}->{must}, type => 'XSLT-compat', token => $token,
931     level => $self->{level}->{should});
932     } else {
933     $self->{parse_error}->(level => $self->{level}->{must}, type => 'not HTML5', token => $token);
934     }
935 wakaba 1.79 } else {
936    
937 wakaba 1.155 #
938 wakaba 1.18 }
939    
940     my $doctype = $self->{document}->create_document_type_definition
941     ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
942 wakaba 1.120 ## NOTE: Default value for both |public_id| and |system_id| attributes
943     ## are empty strings, so that we don't set any value in missing cases.
944 wakaba 1.179 $doctype->public_id ($token->{pubid}) if defined $token->{pubid};
945     $doctype->system_id ($token->{sysid}) if defined $token->{sysid};
946 wakaba 1.18 ## NOTE: Other DocumentType attributes are null or empty lists.
947 wakaba 1.207 ## In Firefox3, |internalSubset| attribute is set to the empty
948     ## string, while |null| is an allowed value for the attribute
949     ## according to DOM3 Core.
950 wakaba 1.18 $self->{document}->append_child ($doctype);
951    
952 wakaba 1.75 if ($token->{quirks} or $doctype_name ne 'HTML') {
953 wakaba 1.79
954 wakaba 1.18 $self->{document}->manakai_compat_mode ('quirks');
955 wakaba 1.179 } elsif (defined $token->{pubid}) {
956     my $pubid = $token->{pubid};
957 wakaba 1.18 $pubid =~ tr/a-z/A-z/;
958 wakaba 1.140 my $prefix = [
959     "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
960     "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
961     "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
962     "-//IETF//DTD HTML 2.0 LEVEL 1//",
963     "-//IETF//DTD HTML 2.0 LEVEL 2//",
964     "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
965     "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
966     "-//IETF//DTD HTML 2.0 STRICT//",
967     "-//IETF//DTD HTML 2.0//",
968     "-//IETF//DTD HTML 2.1E//",
969     "-//IETF//DTD HTML 3.0//",
970     "-//IETF//DTD HTML 3.2 FINAL//",
971     "-//IETF//DTD HTML 3.2//",
972     "-//IETF//DTD HTML 3//",
973     "-//IETF//DTD HTML LEVEL 0//",
974     "-//IETF//DTD HTML LEVEL 1//",
975     "-//IETF//DTD HTML LEVEL 2//",
976     "-//IETF//DTD HTML LEVEL 3//",
977     "-//IETF//DTD HTML STRICT LEVEL 0//",
978     "-//IETF//DTD HTML STRICT LEVEL 1//",
979     "-//IETF//DTD HTML STRICT LEVEL 2//",
980     "-//IETF//DTD HTML STRICT LEVEL 3//",
981     "-//IETF//DTD HTML STRICT//",
982     "-//IETF//DTD HTML//",
983     "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
984     "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
985     "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
986     "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
987     "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
988     "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
989     "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
990     "-//NETSCAPE COMM. CORP.//DTD HTML//",
991     "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
992     "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
993     "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
994     "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
995     "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
996     "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
997     "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
998     "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
999     "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
1000     "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
1001     "-//W3C//DTD HTML 3 1995-03-24//",
1002     "-//W3C//DTD HTML 3.2 DRAFT//",
1003     "-//W3C//DTD HTML 3.2 FINAL//",
1004     "-//W3C//DTD HTML 3.2//",
1005     "-//W3C//DTD HTML 3.2S DRAFT//",
1006     "-//W3C//DTD HTML 4.0 FRAMESET//",
1007     "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
1008     "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
1009     "-//W3C//DTD HTML EXPERIMENTAL 970421//",
1010     "-//W3C//DTD W3 HTML//",
1011     "-//W3O//DTD W3 HTML 3.0//",
1012     "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
1013     "-//WEBTECHS//DTD MOZILLA HTML//",
1014     ]; # $prefix
1015     my $match;
1016     for (@$prefix) {
1017     if (substr ($prefix, 0, length $_) eq $_) {
1018     $match = 1;
1019     last;
1020     }
1021     }
1022     if ($match or
1023     $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
1024     $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
1025     $pubid eq "HTML") {
1026 wakaba 1.79
1027 wakaba 1.18 $self->{document}->manakai_compat_mode ('quirks');
1028 wakaba 1.140 } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
1029     $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
1030 wakaba 1.179 if (defined $token->{sysid}) {
1031 wakaba 1.79
1032 wakaba 1.18 $self->{document}->manakai_compat_mode ('quirks');
1033     } else {
1034 wakaba 1.79
1035 wakaba 1.18 $self->{document}->manakai_compat_mode ('limited quirks');
1036 wakaba 1.3 }
1037 wakaba 1.140 } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
1038     $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
1039 wakaba 1.79
1040 wakaba 1.18 $self->{document}->manakai_compat_mode ('limited quirks');
1041 wakaba 1.79 } else {
1042    
1043 wakaba 1.18 }
1044 wakaba 1.79 } else {
1045    
1046 wakaba 1.18 }
1047 wakaba 1.179 if (defined $token->{sysid}) {
1048     my $sysid = $token->{sysid};
1049 wakaba 1.18 $sysid =~ tr/A-Z/a-z/;
1050     if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
1051 wakaba 1.140 ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
1052     ## marked as quirks.
1053 wakaba 1.18 $self->{document}->manakai_compat_mode ('quirks');
1054 wakaba 1.79
1055     } else {
1056    
1057 wakaba 1.18 }
1058 wakaba 1.79 } else {
1059    
1060 wakaba 1.18 }
1061    
1062 wakaba 1.85 ## Go to the "before html" insertion mode.
1063 wakaba 1.18 $token = $self->_get_next_token;
1064     return;
1065     } elsif ({
1066 wakaba 1.57 START_TAG_TOKEN, 1,
1067     END_TAG_TOKEN, 1,
1068     END_OF_FILE_TOKEN, 1,
1069 wakaba 1.18 }->{$token->{type}}) {
1070 wakaba 1.79
1071 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'no DOCTYPE', token => $token);
1072 wakaba 1.18 $self->{document}->manakai_compat_mode ('quirks');
1073 wakaba 1.85 ## Go to the "before html" insertion mode.
1074 wakaba 1.18 ## reprocess
1075 wakaba 1.122
1076 wakaba 1.18 return;
1077 wakaba 1.57 } elsif ($token->{type} == CHARACTER_TOKEN) {
1078 wakaba 1.184 if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
1079 wakaba 1.18 ## Ignore the token
1080 wakaba 1.26
1081 wakaba 1.18 unless (length $token->{data}) {
1082 wakaba 1.79
1083 wakaba 1.84 ## Stay in the insertion mode.
1084 wakaba 1.18 $token = $self->_get_next_token;
1085     redo INITIAL;
1086 wakaba 1.79 } else {
1087    
1088 wakaba 1.3 }
1089 wakaba 1.79 } else {
1090    
1091 wakaba 1.3 }
1092 wakaba 1.18
1093 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'no DOCTYPE', token => $token);
1094 wakaba 1.18 $self->{document}->manakai_compat_mode ('quirks');
1095 wakaba 1.85 ## Go to the "before html" insertion mode.
1096 wakaba 1.18 ## reprocess
1097     return;
1098 wakaba 1.57 } elsif ($token->{type} == COMMENT_TOKEN) {
1099 wakaba 1.79
1100 wakaba 1.18 my $comment = $self->{document}->create_comment ($token->{data});
1101     $self->{document}->append_child ($comment);
1102    
1103 wakaba 1.84 ## Stay in the insertion mode.
1104 wakaba 1.18 $token = $self->_get_next_token;
1105     redo INITIAL;
1106     } else {
1107 wakaba 1.57 die "$0: $token->{type}: Unknown token type";
1108 wakaba 1.18 }
1109     } # INITIAL
1110 wakaba 1.79
1111     die "$0: _tree_construction_initial: This should be never reached";
1112 wakaba 1.3 } # _tree_construction_initial
1113    
1114     sub _tree_construction_root_element ($) {
1115     my $self = shift;
1116 wakaba 1.84
1117 wakaba 1.85 ## NOTE: "before html" insertion mode.
1118 wakaba 1.3
1119     B: {
1120 wakaba 1.57 if ($token->{type} == DOCTYPE_TOKEN) {
1121 wakaba 1.79
1122 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'in html:#DOCTYPE', token => $token);
1123 wakaba 1.3 ## Ignore the token
1124 wakaba 1.84 ## Stay in the insertion mode.
1125 wakaba 1.3 $token = $self->_get_next_token;
1126     redo B;
1127 wakaba 1.57 } elsif ($token->{type} == COMMENT_TOKEN) {
1128 wakaba 1.79
1129 wakaba 1.3 my $comment = $self->{document}->create_comment ($token->{data});
1130     $self->{document}->append_child ($comment);
1131 wakaba 1.84 ## Stay in the insertion mode.
1132 wakaba 1.3 $token = $self->_get_next_token;
1133     redo B;
1134 wakaba 1.57 } elsif ($token->{type} == CHARACTER_TOKEN) {
1135 wakaba 1.184 if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
1136 wakaba 1.26 ## Ignore the token.
1137    
1138 wakaba 1.3 unless (length $token->{data}) {
1139 wakaba 1.79
1140 wakaba 1.84 ## Stay in the insertion mode.
1141 wakaba 1.3 $token = $self->_get_next_token;
1142     redo B;
1143 wakaba 1.79 } else {
1144    
1145 wakaba 1.3 }
1146 wakaba 1.79 } else {
1147    
1148 wakaba 1.3 }
1149 wakaba 1.63
1150     $self->{application_cache_selection}->(undef);
1151    
1152     #
1153     } elsif ($token->{type} == START_TAG_TOKEN) {
1154 wakaba 1.84 if ($token->{tag_name} eq 'html') {
1155     my $root_element;
1156 wakaba 1.79
1157 wakaba 1.84 $root_element = $self->{document}->create_element_ns
1158 wakaba 1.128 ($HTML_NS, [undef, $token->{tag_name}]);
1159 wakaba 1.84
1160     for my $attr_name (keys %{ $token->{attributes}}) {
1161 wakaba 1.117 my $attr_t = $token->{attributes}->{$attr_name};
1162 wakaba 1.128 my $attr = $self->{document}->create_attribute_ns (undef, [undef, $attr_name]);
1163 wakaba 1.117 $attr->value ($attr_t->{value});
1164     $attr->set_user_data (manakai_source_line => $attr_t->{line});
1165     $attr->set_user_data (manakai_source_column => $attr_t->{column});
1166     $root_element->set_attribute_node_ns ($attr);
1167 wakaba 1.84 }
1168    
1169 wakaba 1.114 $root_element->set_user_data (manakai_source_line => $token->{line})
1170     if defined $token->{line};
1171     $root_element->set_user_data (manakai_source_column => $token->{column})
1172     if defined $token->{column};
1173    
1174 wakaba 1.84 $self->{document}->append_child ($root_element);
1175 wakaba 1.121 push @{$self->{open_elements}},
1176     [$root_element, $el_category->{html}];
1177 wakaba 1.84
1178     if ($token->{attributes}->{manifest}) {
1179    
1180     $self->{application_cache_selection}
1181     ->($token->{attributes}->{manifest}->{value});
1182 wakaba 1.116 ## ISSUE: Spec is unclear on relative references.
1183     ## According to Hixie (#whatwg 2008-03-19), it should be
1184     ## resolved against the base URI of the document in HTML
1185     ## or xml:base of the element in XHTML.
1186 wakaba 1.84 } else {
1187    
1188     $self->{application_cache_selection}->(undef);
1189     }
1190    
1191 wakaba 1.122
1192    
1193 wakaba 1.84 $token = $self->_get_next_token;
1194     return; ## Go to the "before head" insertion mode.
1195 wakaba 1.63 } else {
1196 wakaba 1.79
1197 wakaba 1.84 #
1198 wakaba 1.63 }
1199 wakaba 1.3 } elsif ({
1200 wakaba 1.57 END_TAG_TOKEN, 1,
1201     END_OF_FILE_TOKEN, 1,
1202 wakaba 1.3 }->{$token->{type}}) {
1203 wakaba 1.79
1204 wakaba 1.3 #
1205     } else {
1206 wakaba 1.57 die "$0: $token->{type}: Unknown token type";
1207 wakaba 1.3 }
1208 wakaba 1.63
1209 wakaba 1.123 my $root_element;
1210    
1211 wakaba 1.3 $root_element = $self->{document}->create_element_ns
1212 wakaba 1.128 ($HTML_NS, [undef, 'html']);
1213 wakaba 1.3
1214 wakaba 1.114 $root_element->set_user_data (manakai_source_line => $token->{line})
1215     if defined $token->{line};
1216     $root_element->set_user_data (manakai_source_column => $token->{column})
1217     if defined $token->{column};
1218    
1219 wakaba 1.84 $self->{document}->append_child ($root_element);
1220 wakaba 1.121 push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
1221 wakaba 1.84
1222     $self->{application_cache_selection}->(undef);
1223    
1224     ## NOTE: Reprocess the token.
1225 wakaba 1.122
1226 wakaba 1.84 return; ## Go to the "before head" insertion mode.
1227 wakaba 1.3 } # B
1228 wakaba 1.79
1229     die "$0: _tree_construction_root_element: This should never be reached";
1230 wakaba 1.3 } # _tree_construction_root_element
1231    
1232     sub _reset_insertion_mode ($) {
1233     my $self = shift;
1234    
1235     ## Step 1
1236     my $last;
1237    
1238     ## Step 2
1239     my $i = -1;
1240     my $node = $self->{open_elements}->[$i];
1241    
1242     ## Step 3
1243     S3: {
1244 wakaba 1.29 if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
1245     $last = 1;
1246     if (defined $self->{inner_html_node}) {
1247 wakaba 1.137
1248     $node = $self->{inner_html_node};
1249     } else {
1250     die "_reset_insertion_mode: t27";
1251 wakaba 1.3 }
1252     }
1253 wakaba 1.137
1254     ## Step 4..14
1255     my $new_mode;
1256     if ($node->[1] & FOREIGN_EL) {
1257    
1258     ## NOTE: Strictly spaking, the line below only applies to MathML and
1259     ## SVG elements. Currently the HTML syntax supports only MathML and
1260     ## SVG elements as foreigners.
1261 wakaba 1.145 $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
1262 wakaba 1.201 } elsif ($node->[1] == TABLE_CELL_EL) {
1263 wakaba 1.137 if ($last) {
1264    
1265     #
1266     } else {
1267    
1268     $new_mode = IN_CELL_IM;
1269     }
1270     } else {
1271    
1272     $new_mode = {
1273 wakaba 1.56 select => IN_SELECT_IM,
1274 wakaba 1.83 ## NOTE: |option| and |optgroup| do not set
1275     ## insertion mode to "in select" by themselves.
1276 wakaba 1.56 tr => IN_ROW_IM,
1277     tbody => IN_TABLE_BODY_IM,
1278     thead => IN_TABLE_BODY_IM,
1279     tfoot => IN_TABLE_BODY_IM,
1280     caption => IN_CAPTION_IM,
1281     colgroup => IN_COLUMN_GROUP_IM,
1282     table => IN_TABLE_IM,
1283     head => IN_BODY_IM, # not in head!
1284     body => IN_BODY_IM,
1285     frameset => IN_FRAMESET_IM,
1286 wakaba 1.121 }->{$node->[0]->manakai_local_name};
1287 wakaba 1.137 }
1288     $self->{insertion_mode} = $new_mode and return if defined $new_mode;
1289 wakaba 1.3
1290 wakaba 1.123 ## Step 15
1291 wakaba 1.201 if ($node->[1] == HTML_EL) {
1292 wakaba 1.3 unless (defined $self->{head_element}) {
1293 wakaba 1.79
1294 wakaba 1.56 $self->{insertion_mode} = BEFORE_HEAD_IM;
1295 wakaba 1.3 } else {
1296 wakaba 1.81 ## ISSUE: Can this state be reached?
1297 wakaba 1.79
1298 wakaba 1.56 $self->{insertion_mode} = AFTER_HEAD_IM;
1299 wakaba 1.3 }
1300     return;
1301 wakaba 1.79 } else {
1302    
1303 wakaba 1.3 }
1304    
1305 wakaba 1.123 ## Step 16
1306 wakaba 1.56 $self->{insertion_mode} = IN_BODY_IM and return if $last;
1307 wakaba 1.3
1308 wakaba 1.123 ## Step 17
1309 wakaba 1.3 $i--;
1310     $node = $self->{open_elements}->[$i];
1311    
1312 wakaba 1.123 ## Step 18
1313 wakaba 1.3 redo S3;
1314     } # S3
1315 wakaba 1.79
1316     die "$0: _reset_insertion_mode: This line should never be reached";
1317 wakaba 1.3 } # _reset_insertion_mode
1318    
1319     sub _tree_construction_main ($) {
1320     my $self = shift;
1321    
1322 wakaba 1.1 my $active_formatting_elements = [];
1323    
1324     my $reconstruct_active_formatting_elements = sub { # MUST
1325     my $insert = shift;
1326    
1327     ## Step 1
1328     return unless @$active_formatting_elements;
1329    
1330     ## Step 3
1331     my $i = -1;
1332     my $entry = $active_formatting_elements->[$i];
1333    
1334     ## Step 2
1335     return if $entry->[0] eq '#marker';
1336 wakaba 1.3 for (@{$self->{open_elements}}) {
1337 wakaba 1.1 if ($entry->[0] eq $_->[0]) {
1338 wakaba 1.79
1339 wakaba 1.1 return;
1340     }
1341     }
1342    
1343     S4: {
1344     ## Step 4
1345     last S4 if $active_formatting_elements->[0]->[0] eq $entry->[0];
1346    
1347     ## Step 5
1348     $i--;
1349     $entry = $active_formatting_elements->[$i];
1350    
1351     ## Step 6
1352     if ($entry->[0] eq '#marker') {
1353 wakaba 1.79
1354 wakaba 1.1 #
1355     } else {
1356     my $in_open_elements;
1357 wakaba 1.3 OE: for (@{$self->{open_elements}}) {
1358 wakaba 1.1 if ($entry->[0] eq $_->[0]) {
1359 wakaba 1.79
1360 wakaba 1.1 $in_open_elements = 1;
1361     last OE;
1362     }
1363     }
1364     if ($in_open_elements) {
1365 wakaba 1.79
1366 wakaba 1.1 #
1367     } else {
1368 wakaba 1.81 ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
1369 wakaba 1.79
1370 wakaba 1.1 redo S4;
1371     }
1372     }
1373    
1374     ## Step 7
1375     $i++;
1376     $entry = $active_formatting_elements->[$i];
1377     } # S4
1378    
1379     S7: {
1380     ## Step 8
1381     my $clone = [$entry->[0]->clone_node (0), $entry->[1]];
1382    
1383     ## Step 9
1384     $insert->($clone->[0]);
1385 wakaba 1.3 push @{$self->{open_elements}}, $clone;
1386 wakaba 1.1
1387     ## Step 10
1388 wakaba 1.3 $active_formatting_elements->[$i] = $self->{open_elements}->[-1];
1389 wakaba 1.1
1390     ## Step 11
1391     unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
1392 wakaba 1.79
1393 wakaba 1.1 ## Step 7'
1394     $i++;
1395     $entry = $active_formatting_elements->[$i];
1396    
1397     redo S7;
1398     }
1399 wakaba 1.79
1400    
1401 wakaba 1.1 } # S7
1402     }; # $reconstruct_active_formatting_elements
1403    
1404     my $clear_up_to_marker = sub {
1405     for (reverse 0..$#$active_formatting_elements) {
1406     if ($active_formatting_elements->[$_]->[0] eq '#marker') {
1407 wakaba 1.79
1408 wakaba 1.1 splice @$active_formatting_elements, $_;
1409     return;
1410     }
1411     }
1412 wakaba 1.79
1413    
1414 wakaba 1.1 }; # $clear_up_to_marker
1415    
1416 wakaba 1.96 my $insert;
1417    
1418     my $parse_rcdata = sub ($) {
1419     my ($content_model_flag) = @_;
1420 wakaba 1.25
1421     ## Step 1
1422     my $start_tag_name = $token->{tag_name};
1423    
1424 wakaba 1.200 {
1425     my $el;
1426    
1427 wakaba 1.25 $el = $self->{document}->create_element_ns
1428 wakaba 1.200 ($HTML_NS, [undef, $token->{tag_name}]);
1429 wakaba 1.1
1430 wakaba 1.200 for my $attr_name (keys %{ $token->{attributes}}) {
1431     my $attr_t = $token->{attributes}->{$attr_name};
1432 wakaba 1.128 my $attr = $self->{document}->create_attribute_ns (undef, [undef, $attr_name]);
1433 wakaba 1.117 $attr->value ($attr_t->{value});
1434     $attr->set_user_data (manakai_source_line => $attr_t->{line});
1435     $attr->set_user_data (manakai_source_column => $attr_t->{column});
1436     $el->set_attribute_node_ns ($attr);
1437 wakaba 1.6 }
1438    
1439 wakaba 1.114 $el->set_user_data (manakai_source_line => $token->{line})
1440     if defined $token->{line};
1441     $el->set_user_data (manakai_source_column => $token->{column})
1442     if defined $token->{column};
1443    
1444 wakaba 1.200 $insert->($el);
1445     push @{$self->{open_elements}}, [$el, $el_category->{$token->{tag_name}} || 0];
1446     }
1447    
1448 wakaba 1.25
1449     ## Step 2
1450 wakaba 1.41 $self->{content_model} = $content_model_flag; # CDATA or RCDATA
1451 wakaba 1.13 delete $self->{escape}; # MUST
1452 wakaba 1.25
1453 wakaba 1.200 ## Step 3, 4
1454     $self->{insertion_mode} |= IN_CDATA_RCDATA_IM;
1455    
1456 wakaba 1.122
1457 wakaba 1.1 $token = $self->_get_next_token;
1458 wakaba 1.25 }; # $parse_rcdata
1459 wakaba 1.1
1460 wakaba 1.96 my $script_start_tag = sub () {
1461 wakaba 1.200 ## Step 1
1462 wakaba 1.1 my $script_el;
1463    
1464     $script_el = $self->{document}->create_element_ns
1465 wakaba 1.128 ($HTML_NS, [undef, 'script']);
1466 wakaba 1.1
1467     for my $attr_name (keys %{ $token->{attributes}}) {
1468 wakaba 1.117 my $attr_t = $token->{attributes}->{$attr_name};
1469 wakaba 1.128 my $attr = $self->{document}->create_attribute_ns (undef, [undef, $attr_name]);
1470 wakaba 1.117 $attr->value ($attr_t->{value});
1471     $attr->set_user_data (manakai_source_line => $attr_t->{line});
1472     $attr->set_user_data (manakai_source_column => $attr_t->{column});
1473     $script_el->set_attribute_node_ns ($attr);
1474 wakaba 1.1 }
1475    
1476 wakaba 1.114 $script_el->set_user_data (manakai_source_line => $token->{line})
1477     if defined $token->{line};
1478     $script_el->set_user_data (manakai_source_column => $token->{column})
1479     if defined $token->{column};
1480    
1481 wakaba 1.200
1482     ## Step 2
1483 wakaba 1.1 ## TODO: mark as "parser-inserted"
1484    
1485 wakaba 1.200 ## Step 3
1486     ## TODO: Mark as "already executed", if ...
1487    
1488 wakaba 1.217 ## Step 4 (HTML5 revision 2702)
1489 wakaba 1.200 $insert->($script_el);
1490     push @{$self->{open_elements}}, [$script_el, $el_category->{script}];
1491    
1492     ## Step 5
1493 wakaba 1.41 $self->{content_model} = CDATA_CONTENT_MODEL;
1494 wakaba 1.13 delete $self->{escape}; # MUST
1495 wakaba 1.1
1496 wakaba 1.200 ## Step 6-7
1497     $self->{insertion_mode} |= IN_CDATA_RCDATA_IM;
1498 wakaba 1.25
1499 wakaba 1.1
1500     $token = $self->_get_next_token;
1501     }; # $script_start_tag
1502    
1503 wakaba 1.102 ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
1504     ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
1505 wakaba 1.197 ## NOTE: $open_tables->[-1]->[2] is set false when non-Text node inserted.
1506 wakaba 1.102 my $open_tables = [[$self->{open_elements}->[0]->[0]]];
1507    
1508 wakaba 1.1 my $formatting_end_tag = sub {
1509 wakaba 1.111 my $end_tag_token = shift;
1510     my $tag_name = $end_tag_token->{tag_name};
1511 wakaba 1.1
1512 wakaba 1.102 ## NOTE: The adoption agency algorithm (AAA).
1513    
1514 wakaba 1.1 FET: {
1515     ## Step 1
1516     my $formatting_element;
1517     my $formatting_element_i_in_active;
1518     AFE: for (reverse 0..$#$active_formatting_elements) {
1519 wakaba 1.121 if ($active_formatting_elements->[$_]->[0] eq '#marker') {
1520    
1521     last AFE;
1522     } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
1523     eq $tag_name) {
1524 wakaba 1.79
1525 wakaba 1.1 $formatting_element = $active_formatting_elements->[$_];
1526     $formatting_element_i_in_active = $_;
1527     last AFE;
1528     }
1529     } # AFE
1530     unless (defined $formatting_element) {
1531 wakaba 1.79
1532 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
1533 wakaba 1.1 ## Ignore the token
1534     $token = $self->_get_next_token;
1535     return;
1536     }
1537     ## has an element in scope
1538     my $in_scope = 1;
1539     my $formatting_element_i_in_open;
1540 wakaba 1.3 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
1541     my $node = $self->{open_elements}->[$_];
1542 wakaba 1.1 if ($node->[0] eq $formatting_element->[0]) {
1543     if ($in_scope) {
1544 wakaba 1.79
1545 wakaba 1.1 $formatting_element_i_in_open = $_;
1546     last INSCOPE;
1547     } else { # in open elements but not in scope
1548 wakaba 1.79
1549 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
1550     text => $token->{tag_name},
1551 wakaba 1.111 token => $end_tag_token);
1552 wakaba 1.1 ## Ignore the token
1553     $token = $self->_get_next_token;
1554     return;
1555     }
1556 wakaba 1.121 } elsif ($node->[1] & SCOPING_EL) {
1557 wakaba 1.79
1558 wakaba 1.1 $in_scope = 0;
1559     }
1560     } # INSCOPE
1561     unless (defined $formatting_element_i_in_open) {
1562 wakaba 1.79
1563 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
1564     text => $token->{tag_name},
1565 wakaba 1.111 token => $end_tag_token);
1566 wakaba 1.1 pop @$active_formatting_elements; # $formatting_element
1567     $token = $self->_get_next_token; ## TODO: ok?
1568     return;
1569     }
1570 wakaba 1.3 if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
1571 wakaba 1.79
1572 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'not closed',
1573     text => $self->{open_elements}->[-1]->[0]
1574 wakaba 1.120 ->manakai_local_name,
1575 wakaba 1.111 token => $end_tag_token);
1576 wakaba 1.1 }
1577    
1578     ## Step 2
1579     my $furthest_block;
1580     my $furthest_block_i_in_open;
1581 wakaba 1.3 OE: for (reverse 0..$#{$self->{open_elements}}) {
1582     my $node = $self->{open_elements}->[$_];
1583 wakaba 1.121 if (not ($node->[1] & FORMATTING_EL) and
1584 wakaba 1.1 #not $phrasing_category->{$node->[1]} and
1585 wakaba 1.121 ($node->[1] & SPECIAL_EL or
1586     $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
1587 wakaba 1.79
1588 wakaba 1.1 $furthest_block = $node;
1589     $furthest_block_i_in_open = $_;
1590 wakaba 1.198 ## NOTE: The topmost (eldest) node.
1591 wakaba 1.1 } elsif ($node->[0] eq $formatting_element->[0]) {
1592 wakaba 1.79
1593 wakaba 1.1 last OE;
1594     }
1595     } # OE
1596    
1597     ## Step 3
1598     unless (defined $furthest_block) { # MUST
1599 wakaba 1.79
1600 wakaba 1.3 splice @{$self->{open_elements}}, $formatting_element_i_in_open;
1601 wakaba 1.1 splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
1602     $token = $self->_get_next_token;
1603     return;
1604     }
1605    
1606     ## Step 4
1607 wakaba 1.3 my $common_ancestor_node = $self->{open_elements}->[$formatting_element_i_in_open - 1];
1608 wakaba 1.1
1609     ## Step 5
1610     my $furthest_block_parent = $furthest_block->[0]->parent_node;
1611     if (defined $furthest_block_parent) {
1612 wakaba 1.79
1613 wakaba 1.1 $furthest_block_parent->remove_child ($furthest_block->[0]);
1614     }
1615    
1616     ## Step 6
1617     my $bookmark_prev_el
1618     = $active_formatting_elements->[$formatting_element_i_in_active - 1]
1619     ->[0];
1620    
1621     ## Step 7
1622     my $node = $furthest_block;
1623     my $node_i_in_open = $furthest_block_i_in_open;
1624     my $last_node = $furthest_block;
1625     S7: {
1626     ## Step 1
1627     $node_i_in_open--;
1628 wakaba 1.3 $node = $self->{open_elements}->[$node_i_in_open];
1629 wakaba 1.1
1630     ## Step 2
1631     my $node_i_in_active;
1632     S7S2: {
1633     for (reverse 0..$#$active_formatting_elements) {
1634     if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
1635 wakaba 1.79
1636 wakaba 1.1 $node_i_in_active = $_;
1637     last S7S2;
1638     }
1639     }
1640 wakaba 1.3 splice @{$self->{open_elements}}, $node_i_in_open, 1;
1641 wakaba 1.1 redo S7;
1642     } # S7S2
1643    
1644     ## Step 3
1645     last S7 if $node->[0] eq $formatting_element->[0];
1646    
1647     ## Step 4
1648     if ($last_node->[0] eq $furthest_block->[0]) {
1649 wakaba 1.79
1650 wakaba 1.1 $bookmark_prev_el = $node->[0];
1651     }
1652    
1653     ## Step 5
1654     if ($node->[0]->has_child_nodes ()) {
1655 wakaba 1.79
1656 wakaba 1.1 my $clone = [$node->[0]->clone_node (0), $node->[1]];
1657     $active_formatting_elements->[$node_i_in_active] = $clone;
1658 wakaba 1.3 $self->{open_elements}->[$node_i_in_open] = $clone;
1659 wakaba 1.1 $node = $clone;
1660     }
1661    
1662     ## Step 6
1663     $node->[0]->append_child ($last_node->[0]);
1664    
1665     ## Step 7
1666     $last_node = $node;
1667    
1668     ## Step 8
1669     redo S7;
1670     } # S7
1671    
1672     ## Step 8
1673 wakaba 1.121 if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
1674 wakaba 1.102 my $foster_parent_element;
1675     my $next_sibling;
1676 wakaba 1.121 OE: for (reverse 0..$#{$self->{open_elements}}) {
1677 wakaba 1.201 if ($self->{open_elements}->[$_]->[1] == TABLE_EL) {
1678 wakaba 1.102 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
1679     if (defined $parent and $parent->node_type == 1) {
1680    
1681     $foster_parent_element = $parent;
1682     $next_sibling = $self->{open_elements}->[$_]->[0];
1683     } else {
1684    
1685     $foster_parent_element
1686     = $self->{open_elements}->[$_ - 1]->[0];
1687     }
1688     last OE;
1689     }
1690     } # OE
1691     $foster_parent_element = $self->{open_elements}->[0]->[0]
1692     unless defined $foster_parent_element;
1693     $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
1694     $open_tables->[-1]->[1] = 1; # tainted
1695     } else {
1696    
1697     $common_ancestor_node->[0]->append_child ($last_node->[0]);
1698     }
1699 wakaba 1.1
1700     ## Step 9
1701     my $clone = [$formatting_element->[0]->clone_node (0),
1702     $formatting_element->[1]];
1703    
1704     ## Step 10
1705     my @cn = @{$furthest_block->[0]->child_nodes};
1706     $clone->[0]->append_child ($_) for @cn;
1707    
1708     ## Step 11
1709     $furthest_block->[0]->append_child ($clone->[0]);
1710    
1711     ## Step 12
1712     my $i;
1713     AFE: for (reverse 0..$#$active_formatting_elements) {
1714     if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
1715 wakaba 1.79
1716 wakaba 1.1 splice @$active_formatting_elements, $_, 1;
1717     $i-- and last AFE if defined $i;
1718     } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
1719 wakaba 1.79
1720 wakaba 1.1 $i = $_;
1721     }
1722     } # AFE
1723     splice @$active_formatting_elements, $i + 1, 0, $clone;
1724    
1725     ## Step 13
1726     undef $i;
1727 wakaba 1.3 OE: for (reverse 0..$#{$self->{open_elements}}) {
1728     if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
1729 wakaba 1.79
1730 wakaba 1.3 splice @{$self->{open_elements}}, $_, 1;
1731 wakaba 1.1 $i-- and last OE if defined $i;
1732 wakaba 1.3 } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
1733 wakaba 1.79
1734 wakaba 1.1 $i = $_;
1735     }
1736     } # OE
1737 wakaba 1.198 splice @{$self->{open_elements}}, $i + 1, 0, $clone;
1738 wakaba 1.1
1739     ## Step 14
1740     redo FET;
1741     } # FET
1742     }; # $formatting_end_tag
1743    
1744 wakaba 1.96 $insert = my $insert_to_current = sub {
1745 wakaba 1.25 $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
1746 wakaba 1.1 }; # $insert_to_current
1747    
1748     my $insert_to_foster = sub {
1749 wakaba 1.95 my $child = shift;
1750 wakaba 1.121 if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
1751 wakaba 1.95 # MUST
1752     my $foster_parent_element;
1753     my $next_sibling;
1754 wakaba 1.121 OE: for (reverse 0..$#{$self->{open_elements}}) {
1755 wakaba 1.201 if ($self->{open_elements}->[$_]->[1] == TABLE_EL) {
1756 wakaba 1.3 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
1757 wakaba 1.1 if (defined $parent and $parent->node_type == 1) {
1758 wakaba 1.79
1759 wakaba 1.1 $foster_parent_element = $parent;
1760 wakaba 1.3 $next_sibling = $self->{open_elements}->[$_]->[0];
1761 wakaba 1.1 } else {
1762 wakaba 1.79
1763 wakaba 1.1 $foster_parent_element
1764 wakaba 1.3 = $self->{open_elements}->[$_ - 1]->[0];
1765 wakaba 1.1 }
1766     last OE;
1767     }
1768     } # OE
1769 wakaba 1.3 $foster_parent_element = $self->{open_elements}->[0]->[0]
1770 wakaba 1.1 unless defined $foster_parent_element;
1771     $foster_parent_element->insert_before
1772     ($child, $next_sibling);
1773 wakaba 1.95 $open_tables->[-1]->[1] = 1; # tainted
1774     } else {
1775    
1776     $self->{open_elements}->[-1]->[0]->append_child ($child);
1777     }
1778 wakaba 1.1 }; # $insert_to_foster
1779    
1780 wakaba 1.199 ## NOTE: Insert a character (MUST): When a character is inserted, if
1781     ## the last node that was inserted by the parser is a Text node and
1782     ## the character has to be inserted after that node, then the
1783     ## character is appended to the Text node. However, if any other
1784     ## node is inserted by the parser, then a new Text node is created
1785     ## and the character is appended as that Text node. If I'm not
1786     ## wrong, for a parser with scripting disabled, there are only two
1787     ## cases where this occurs. One is the case where an element node
1788     ## is inserted to the |head| element. This is covered by using the
1789 wakaba 1.197 ## |$self->{head_element_inserted}| flag. Another is the case where
1790     ## an element or comment is inserted into the |table| subtree while
1791     ## foster parenting happens. This is covered by using the [2] flag
1792     ## of the |$open_tables| structure. All other cases are handled
1793     ## simply by calling |manakai_append_text| method.
1794    
1795 wakaba 1.199 ## TODO: |<body><script>document.write("a<br>");
1796     ## document.body.removeChild (document.body.lastChild);
1797     ## document.write ("b")</script>|
1798    
1799 wakaba 1.123 B: while (1) {
1800 wakaba 1.57 if ($token->{type} == DOCTYPE_TOKEN) {
1801 wakaba 1.79
1802 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'in html:#DOCTYPE', token => $token);
1803 wakaba 1.54 ## Ignore the token
1804     ## Stay in the phase
1805     $token = $self->_get_next_token;
1806 wakaba 1.123 next B;
1807 wakaba 1.57 } elsif ($token->{type} == START_TAG_TOKEN and
1808 wakaba 1.54 $token->{tag_name} eq 'html') {
1809 wakaba 1.56 if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
1810 wakaba 1.79
1811 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'after html', text => 'html', token => $token);
1812 wakaba 1.56 $self->{insertion_mode} = AFTER_BODY_IM;
1813     } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
1814 wakaba 1.79
1815 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'after html', text => 'html', token => $token);
1816 wakaba 1.56 $self->{insertion_mode} = AFTER_FRAMESET_IM;
1817 wakaba 1.79 } else {
1818    
1819 wakaba 1.54 }
1820    
1821 wakaba 1.84
1822 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'not first start tag', token => $token);
1823 wakaba 1.54 my $top_el = $self->{open_elements}->[0]->[0];
1824     for my $attr_name (keys %{$token->{attributes}}) {
1825     unless ($top_el->has_attribute_ns (undef, $attr_name)) {
1826 wakaba 1.79
1827 wakaba 1.54 $top_el->set_attribute_ns
1828     (undef, [undef, $attr_name],
1829     $token->{attributes}->{$attr_name}->{value});
1830     }
1831     }
1832 wakaba 1.122
1833 wakaba 1.54 $token = $self->_get_next_token;
1834 wakaba 1.123 next B;
1835 wakaba 1.57 } elsif ($token->{type} == COMMENT_TOKEN) {
1836 wakaba 1.54 my $comment = $self->{document}->create_comment ($token->{data});
1837 wakaba 1.58 if ($self->{insertion_mode} & AFTER_HTML_IMS) {
1838 wakaba 1.79
1839 wakaba 1.54 $self->{document}->append_child ($comment);
1840 wakaba 1.56 } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
1841 wakaba 1.79
1842 wakaba 1.54 $self->{open_elements}->[0]->[0]->append_child ($comment);
1843     } else {
1844 wakaba 1.79
1845 wakaba 1.54 $self->{open_elements}->[-1]->[0]->append_child ($comment);
1846 wakaba 1.197 $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
1847 wakaba 1.54 }
1848     $token = $self->_get_next_token;
1849 wakaba 1.123 next B;
1850 wakaba 1.200 } elsif ($self->{insertion_mode} & IN_CDATA_RCDATA_IM) {
1851     if ($token->{type} == CHARACTER_TOKEN) {
1852     $token->{data} =~ s/^\x0A// if $self->{ignore_newline};
1853     delete $self->{ignore_newline};
1854    
1855     if (length $token->{data}) {
1856    
1857     $self->{open_elements}->[-1]->[0]->manakai_append_text
1858     ($token->{data});
1859     } else {
1860    
1861     }
1862     $token = $self->_get_next_token;
1863     next B;
1864     } elsif ($token->{type} == END_TAG_TOKEN) {
1865     delete $self->{ignore_newline};
1866    
1867     if ($token->{tag_name} eq 'script') {
1868    
1869    
1870     ## Para 1-2
1871     my $script = pop @{$self->{open_elements}};
1872    
1873     ## Para 3
1874     $self->{insertion_mode} &= ~ IN_CDATA_RCDATA_IM;
1875    
1876     ## Para 4
1877     ## TODO: $old_insertion_point = $current_insertion_point;
1878     ## TODO: $current_insertion_point = just before $self->{nc};
1879    
1880     ## Para 5
1881     ## TODO: Run the $script->[0].
1882    
1883     ## Para 6
1884     ## TODO: $current_insertion_point = $old_insertion_point;
1885    
1886     ## Para 7
1887     ## TODO: if ($pending_external_script) {
1888     ## TODO: ...
1889     ## TODO: }
1890    
1891     $token = $self->_get_next_token;
1892     next B;
1893     } else {
1894    
1895    
1896     pop @{$self->{open_elements}};
1897    
1898     $self->{insertion_mode} &= ~ IN_CDATA_RCDATA_IM;
1899     $token = $self->_get_next_token;
1900     next B;
1901     }
1902     } elsif ($token->{type} == END_OF_FILE_TOKEN) {
1903     delete $self->{ignore_newline};
1904    
1905    
1906     $self->{parse_error}->(level => $self->{level}->{must}, type => 'not closed',
1907     text => $self->{open_elements}->[-1]->[0]
1908     ->manakai_local_name,
1909     token => $token);
1910    
1911 wakaba 1.201 #if ($self->{open_elements}->[-1]->[1] == SCRIPT_EL) {
1912 wakaba 1.200 # ## TODO: Mark as "already executed"
1913     #}
1914    
1915     pop @{$self->{open_elements}};
1916    
1917     $self->{insertion_mode} &= ~ IN_CDATA_RCDATA_IM;
1918     ## Reprocess.
1919     next B;
1920     } else {
1921     die "$0: $token->{type}: In CDATA/RCDATA: Unknown token type";
1922     }
1923 wakaba 1.123 } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
1924     if ($token->{type} == CHARACTER_TOKEN) {
1925    
1926     $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
1927     $token = $self->_get_next_token;
1928     next B;
1929     } elsif ($token->{type} == START_TAG_TOKEN) {
1930 wakaba 1.126 if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
1931     $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
1932 wakaba 1.123 not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
1933     ($token->{tag_name} eq 'svg' and
1934 wakaba 1.201 $self->{open_elements}->[-1]->[1] == MML_AXML_EL)) {
1935 wakaba 1.123 ## NOTE: "using the rules for secondary insertion mode"then"continue"
1936    
1937     #
1938     } elsif ({
1939 wakaba 1.127 b => 1, big => 1, blockquote => 1, body => 1, br => 1,
1940 wakaba 1.143 center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
1941     em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
1942     h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
1943     img => 1, li => 1, listing => 1, menu => 1, meta => 1,
1944     nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
1945     small => 1, span => 1, strong => 1, strike => 1, sub => 1,
1946     sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
1947 wakaba 1.123 }->{$token->{tag_name}}) {
1948    
1949 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'not closed',
1950     text => $self->{open_elements}->[-1]->[0]
1951 wakaba 1.123 ->manakai_local_name,
1952     token => $token);
1953    
1954     pop @{$self->{open_elements}}
1955     while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
1956    
1957 wakaba 1.127 $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
1958 wakaba 1.123 ## Reprocess.
1959     next B;
1960     } else {
1961 wakaba 1.128 my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
1962     my $tag_name = $token->{tag_name};
1963     if ($nsuri eq $SVG_NS) {
1964     $tag_name = {
1965     altglyph => 'altGlyph',
1966     altglyphdef => 'altGlyphDef',
1967     altglyphitem => 'altGlyphItem',
1968     animatecolor => 'animateColor',
1969     animatemotion => 'animateMotion',
1970     animatetransform => 'animateTransform',
1971     clippath => 'clipPath',
1972     feblend => 'feBlend',
1973     fecolormatrix => 'feColorMatrix',
1974     fecomponenttransfer => 'feComponentTransfer',
1975     fecomposite => 'feComposite',
1976     feconvolvematrix => 'feConvolveMatrix',
1977     fediffuselighting => 'feDiffuseLighting',
1978     fedisplacementmap => 'feDisplacementMap',
1979     fedistantlight => 'feDistantLight',
1980     feflood => 'feFlood',
1981     fefunca => 'feFuncA',
1982     fefuncb => 'feFuncB',
1983     fefuncg => 'feFuncG',
1984     fefuncr => 'feFuncR',
1985     fegaussianblur => 'feGaussianBlur',
1986     feimage => 'feImage',
1987     femerge => 'feMerge',
1988     femergenode => 'feMergeNode',
1989     femorphology => 'feMorphology',
1990     feoffset => 'feOffset',
1991     fepointlight => 'fePointLight',
1992     fespecularlighting => 'feSpecularLighting',
1993     fespotlight => 'feSpotLight',
1994     fetile => 'feTile',
1995     feturbulence => 'feTurbulence',
1996     foreignobject => 'foreignObject',
1997     glyphref => 'glyphRef',
1998     lineargradient => 'linearGradient',
1999     radialgradient => 'radialGradient',
2000     #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
2001     textpath => 'textPath',
2002     }->{$tag_name} || $tag_name;
2003     }
2004    
2005     ## "adjust SVG attributes" (SVG only) - done in insert-element-f
2006    
2007     ## "adjust foreign attributes" - done in insert-element-f
2008 wakaba 1.123
2009    
2010     {
2011     my $el;
2012    
2013     $el = $self->{document}->create_element_ns
2014 wakaba 1.128 ($nsuri, [undef, $tag_name]);
2015 wakaba 1.123
2016     for my $attr_name (keys %{ $token->{attributes}}) {
2017     my $attr_t = $token->{attributes}->{$attr_name};
2018 wakaba 1.128 my $attr = $self->{document}->create_attribute_ns (
2019     @{
2020     $foreign_attr_xname->{$attr_name} ||
2021     [undef, [undef,
2022 wakaba 1.152 ($nsuri) eq $SVG_NS ?
2023 wakaba 1.128 ($svg_attr_name->{$attr_name} || $attr_name) :
2024 wakaba 1.152 ($nsuri) eq $MML_NS ?
2025     ($attr_name eq 'definitionurl' ?
2026     'definitionURL' : $attr_name) :
2027 wakaba 1.128 $attr_name]]
2028     }
2029     );
2030 wakaba 1.123 $attr->value ($attr_t->{value});
2031     $attr->set_user_data (manakai_source_line => $attr_t->{line});
2032     $attr->set_user_data (manakai_source_column => $attr_t->{column});
2033     $el->set_attribute_node_ns ($attr);
2034     }
2035    
2036     $el->set_user_data (manakai_source_line => $token->{line})
2037     if defined $token->{line};
2038     $el->set_user_data (manakai_source_column => $token->{column})
2039     if defined $token->{column};
2040    
2041     $insert->($el);
2042 wakaba 1.128 push @{$self->{open_elements}}, [$el, ($el_category_f->{$nsuri}->{ $tag_name} || 0) | FOREIGN_EL];
2043    
2044     if ( $token->{attributes}->{xmlns} and $token->{attributes}->{xmlns}->{value} ne ($nsuri)) {
2045 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'bad namespace', token => $token);
2046 wakaba 1.128 ## TODO: Error type documentation
2047     }
2048 wakaba 1.217 if ( $token->{attributes}->{'xmlns:xlink'} and
2049     $token->{attributes}->{'xmlns:xlink'}->{value} ne q<http://www.w3.org/1999/xlink>) {
2050     $self->{parse_error}->(level => $self->{level}->{must}, type => 'bad namespace', token => $token);
2051     }
2052 wakaba 1.123 }
2053    
2054    
2055     if ($self->{self_closing}) {
2056     pop @{$self->{open_elements}};
2057     delete $self->{self_closing};
2058     } else {
2059    
2060     }
2061    
2062     $token = $self->_get_next_token;
2063     next B;
2064     }
2065     } elsif ($token->{type} == END_TAG_TOKEN) {
2066     ## NOTE: "using the rules for secondary insertion mode" then "continue"
2067 wakaba 1.215 if ($token->{tag_name} eq 'script') {
2068    
2069     #
2070     ## XXXscript: Execute script here.
2071     } else {
2072    
2073     #
2074     }
2075 wakaba 1.123 } elsif ($token->{type} == END_OF_FILE_TOKEN) {
2076    
2077 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'not closed',
2078     text => $self->{open_elements}->[-1]->[0]
2079 wakaba 1.143 ->manakai_local_name,
2080     token => $token);
2081    
2082     pop @{$self->{open_elements}}
2083     while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
2084    
2085 wakaba 1.196 ## NOTE: |<span><svg>| ... two parse errors, |<svg>| ... a parse error.
2086    
2087 wakaba 1.143 $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
2088     ## Reprocess.
2089     next B;
2090 wakaba 1.123 } else {
2091     die "$0: $token->{type}: Unknown token type";
2092     }
2093     }
2094    
2095     if ($self->{insertion_mode} & HEAD_IMS) {
2096 wakaba 1.57 if ($token->{type} == CHARACTER_TOKEN) {
2097 wakaba 1.184 if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
2098 wakaba 1.99 unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2099 wakaba 1.197 if ($self->{head_element_inserted}) {
2100    
2101     $self->{open_elements}->[-1]->[0]->append_child
2102     ($self->{document}->create_text_node ($1));
2103     delete $self->{head_element_inserted};
2104     ## NOTE: |</head> <link> |
2105     #
2106     } else {
2107    
2108     $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
2109     ## NOTE: |</head> &#x20;|
2110     #
2111     }
2112 wakaba 1.99 } else {
2113    
2114     ## Ignore the token.
2115 wakaba 1.174 #
2116 wakaba 1.99 }
2117 wakaba 1.54 unless (length $token->{data}) {
2118 wakaba 1.79
2119 wakaba 1.54 $token = $self->_get_next_token;
2120 wakaba 1.123 next B;
2121 wakaba 1.54 }
2122 wakaba 1.173 ## TODO: set $token->{column} appropriately
2123 wakaba 1.54 }
2124    
2125 wakaba 1.56 if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2126 wakaba 1.79
2127 wakaba 1.54 ## As if <head>
2128    
2129     $self->{head_element} = $self->{document}->create_element_ns
2130 wakaba 1.128 ($HTML_NS, [undef, 'head']);
2131 wakaba 1.54
2132 wakaba 1.114 $self->{head_element}->set_user_data (manakai_source_line => $token->{line})
2133     if defined $token->{line};
2134     $self->{head_element}->set_user_data (manakai_source_column => $token->{column})
2135     if defined $token->{column};
2136    
2137 wakaba 1.54 $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2138 wakaba 1.121 push @{$self->{open_elements}},
2139     [$self->{head_element}, $el_category->{head}];
2140 wakaba 1.54
2141     ## Reprocess in the "in head" insertion mode...
2142     pop @{$self->{open_elements}};
2143    
2144     ## Reprocess in the "after head" insertion mode...
2145 wakaba 1.56 } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2146 wakaba 1.79
2147 wakaba 1.54 ## As if </noscript>
2148     pop @{$self->{open_elements}};
2149 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'in noscript:#text', token => $token);
2150 wakaba 1.54
2151     ## Reprocess in the "in head" insertion mode...
2152     ## As if </head>
2153     pop @{$self->{open_elements}};
2154    
2155     ## Reprocess in the "after head" insertion mode...
2156 wakaba 1.56 } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2157 wakaba 1.79
2158 wakaba 1.54 pop @{$self->{open_elements}};
2159    
2160     ## Reprocess in the "after head" insertion mode...
2161 wakaba 1.79 } else {
2162    
2163 wakaba 1.54 }
2164    
2165 wakaba 1.121 ## "after head" insertion mode
2166     ## As if <body>
2167    
2168 wakaba 1.54 {
2169     my $el;
2170    
2171     $el = $self->{document}->create_element_ns
2172 wakaba 1.128 ($HTML_NS, [undef, 'body']);
2173 wakaba 1.54
2174 wakaba 1.114 $el->set_user_data (manakai_source_line => $token->{line})
2175     if defined $token->{line};
2176     $el->set_user_data (manakai_source_column => $token->{column})
2177     if defined $token->{column};
2178    
2179 wakaba 1.54 $self->{open_elements}->[-1]->[0]->append_child ($el);
2180 wakaba 1.121 push @{$self->{open_elements}}, [$el, $el_category->{'body'} || 0];
2181 wakaba 1.54 }
2182    
2183 wakaba 1.121 $self->{insertion_mode} = IN_BODY_IM;
2184     ## reprocess
2185 wakaba 1.123 next B;
2186 wakaba 1.121 } elsif ($token->{type} == START_TAG_TOKEN) {
2187     if ($token->{tag_name} eq 'head') {
2188     if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2189    
2190    
2191 wakaba 1.54 $self->{head_element} = $self->{document}->create_element_ns
2192 wakaba 1.128 ($HTML_NS, [undef, $token->{tag_name}]);
2193 wakaba 1.54
2194     for my $attr_name (keys %{ $token->{attributes}}) {
2195 wakaba 1.117 my $attr_t = $token->{attributes}->{$attr_name};
2196 wakaba 1.128 my $attr = $self->{document}->create_attribute_ns (undef, [undef, $attr_name]);
2197 wakaba 1.117 $attr->value ($attr_t->{value});
2198     $attr->set_user_data (manakai_source_line => $attr_t->{line});
2199     $attr->set_user_data (manakai_source_column => $attr_t->{column});
2200     $self->{head_element}->set_attribute_node_ns ($attr);
2201 wakaba 1.54 }
2202    
2203 wakaba 1.114 $self->{head_element}->set_user_data (manakai_source_line => $token->{line})
2204     if defined $token->{line};
2205     $self->{head_element}->set_user_data (manakai_source_column => $token->{column})
2206     if defined $token->{column};
2207    
2208 wakaba 1.121 $self->{open_elements}->[-1]->[0]->append_child
2209     ($self->{head_element});
2210     push @{$self->{open_elements}},
2211     [$self->{head_element}, $el_category->{head}];
2212     $self->{insertion_mode} = IN_HEAD_IM;
2213 wakaba 1.122
2214     $token = $self->_get_next_token;
2215 wakaba 1.123 next B;
2216 wakaba 1.122 } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2217    
2218 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'after head', text => 'head',
2219     token => $token);
2220 wakaba 1.136 ## Ignore the token
2221    
2222     $token = $self->_get_next_token;
2223     next B;
2224 wakaba 1.122 } else {
2225    
2226 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'in head:head',
2227     token => $token); # or in head noscript
2228 wakaba 1.122 ## Ignore the token
2229    
2230 wakaba 1.121 $token = $self->_get_next_token;
2231 wakaba 1.123 next B;
2232 wakaba 1.122 }
2233     } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2234 wakaba 1.123
2235     ## As if <head>
2236    
2237 wakaba 1.54 $self->{head_element} = $self->{document}->create_element_ns
2238 wakaba 1.128 ($HTML_NS, [undef, 'head']);
2239 wakaba 1.54
2240 wakaba 1.114 $self->{head_element}->set_user_data (manakai_source_line => $token->{line})
2241     if defined $token->{line};
2242     $self->{head_element}->set_user_data (manakai_source_column => $token->{column})
2243     if defined $token->{column};
2244    
2245 wakaba 1.123 $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2246     push @{$self->{open_elements}},
2247     [$self->{head_element}, $el_category->{head}];
2248 wakaba 1.54
2249 wakaba 1.123 $self->{insertion_mode} = IN_HEAD_IM;
2250     ## Reprocess in the "in head" insertion mode...
2251     } else {
2252    
2253     }
2254 wakaba 1.54
2255 wakaba 1.197 if ($token->{tag_name} eq 'base') {
2256     if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2257    
2258     ## As if </noscript>
2259     pop @{$self->{open_elements}};
2260     $self->{parse_error}->(level => $self->{level}->{must}, type => 'in noscript', text => 'base',
2261     token => $token);
2262    
2263     $self->{insertion_mode} = IN_HEAD_IM;
2264     ## Reprocess in the "in head" insertion mode...
2265     } else {
2266    
2267     }
2268 wakaba 1.54
2269 wakaba 1.197 ## NOTE: There is a "as if in head" code clone.
2270     if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2271    
2272     $self->{parse_error}->(level => $self->{level}->{must}, type => 'after head',
2273     text => $token->{tag_name}, token => $token);
2274     push @{$self->{open_elements}},
2275     [$self->{head_element}, $el_category->{head}];
2276     $self->{head_element_inserted} = 1;
2277     } else {
2278    
2279     }
2280    
2281 wakaba 1.54 {
2282     my $el;
2283    
2284     $el = $self->{document}->create_element_ns
2285 wakaba 1.128 ($HTML_NS, [undef, $token->{tag_name}]);
2286 wakaba 1.54
2287     for my $attr_name (keys %{ $token->{attributes}}) {
2288 wakaba 1.117 my $attr_t = $token->{attributes}->{$attr_name};
2289 wakaba 1.128 my $attr = $self->{document}->create_attribute_ns (undef, [undef, $attr_name]);
2290 wakaba 1.117 $attr->value ($attr_t->{value});
2291     $attr->set_user_data (manakai_source_line => $attr_t->{line});
2292     $attr->set_user_data (manakai_source_column => $attr_t->{column});
2293     $el->set_attribute_node_ns ($attr);
2294 wakaba 1.54 }
2295    
2296 wakaba 1.114 $el->set_user_data (manakai_source_line => $token->{line})
2297     if defined $token->{line};
2298     $el->set_user_data (manakai_source_column => $token->{column})
2299     if defined $token->{column};
2300    
2301 wakaba 1.54 $self->{open_elements}->[-1]->[0]->append_child ($el);
2302 wakaba 1.121 push @{$self->{open_elements}}, [$el, $el_category->{$token->{tag_name}} || 0];
2303 wakaba 1.54 }
2304    
2305 wakaba 1.197 pop @{$self->{open_elements}};
2306     pop @{$self->{open_elements}} # <head>
2307     if $self->{insertion_mode} == AFTER_HEAD_IM;
2308    
2309     $token = $self->_get_next_token;
2310     next B;
2311 wakaba 1.189 } elsif ($token->{tag_name} eq 'link') {
2312     ## NOTE: There is a "as if in head" code clone.
2313     if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2314    
2315     $self->{parse_error}->(level => $self->{level}->{must}, type => 'after head',
2316     text => $token->{tag_name}, token => $token);
2317     push @{$self->{open_elements}},
2318     [$self->{head_element}, $el_category->{head}];
2319 wakaba 1.197 $self->{head_element_inserted} = 1;
2320 wakaba 1.189 } else {
2321    
2322     }
2323    
2324     {
2325     my $el;
2326    
2327     $el = $self->{document}->create_element_ns
2328     ($HTML_NS, [undef, $token->{tag_name}]);
2329    
2330     for my $attr_name (keys %{ $token->{attributes}}) {
2331     my $attr_t = $token->{attributes}->{$attr_name};
2332     my $attr = $self->{document}->create_attribute_ns (undef, [undef, $attr_name]);
2333     $attr->value ($attr_t->{value});
2334     $attr->set_user_data (manakai_source_line => $attr_t->{line});
2335     $attr->set_user_data (manakai_source_column => $attr_t->{column});
2336     $el->set_attribute_node_ns ($attr);
2337     }
2338    
2339     $el->set_user_data (manakai_source_line => $token->{line})
2340     if defined $token->{line};
2341     $el->set_user_data (manakai_source_column => $token->{column})
2342     if defined $token->{column};
2343    
2344     $self->{open_elements}->[-1]->[0]->append_child ($el);
2345     push @{$self->{open_elements}}, [$el, $el_category->{$token->{tag_name}} || 0];
2346     }
2347    
2348     pop @{$self->{open_elements}};
2349     pop @{$self->{open_elements}} # <head>
2350     if $self->{insertion_mode} == AFTER_HEAD_IM;
2351     delete $self->{self_closing};
2352     $token = $self->_get_next_token;
2353     next B;
2354     } elsif ($token->{tag_name} eq 'command' or
2355     $token->{tag_name} eq 'eventsource') {
2356     if ($self->{insertion_mode} == IN_HEAD_IM) {
2357     ## NOTE: If the insertion mode at the time of the emission
2358     ## of the token was "before head", $self->{insertion_mode}
2359     ## is already changed to |IN_HEAD_IM|.
2360    
2361     ## NOTE: There is a "as if in head" code clone.
2362    
2363 wakaba 1.25 {
2364     my $el;
2365    
2366 wakaba 1.1 $el = $self->{document}->create_element_ns
2367 wakaba 1.128 ($HTML_NS, [undef, $token->{tag_name}]);
2368 wakaba 1.1
2369 wakaba 1.25 for my $attr_name (keys %{ $token->{attributes}}) {
2370 wakaba 1.117 my $attr_t = $token->{attributes}->{$attr_name};
2371 wakaba 1.128 my $attr = $self->{document}->create_attribute_ns (undef, [undef, $attr_name]);
2372 wakaba 1.117 $attr->value ($attr_t->{value});
2373     $attr->set_user_data (manakai_source_line => $attr_t->{line});
2374     $attr->set_user_data (manakai_source_column => $attr_t->{column});
2375     $el->set_attribute_node_ns ($attr);
2376 wakaba 1.1 }
2377    
2378 wakaba 1.114 $el->set_user_data (manakai_source_line => $token->{line})
2379     if defined $token->{line};
2380     $el->set_user_data (manakai_source_column => $token->{column})
2381     if defined $token->{column};
2382    
2383 wakaba 1.54 $self->{open_elements}->[-1]->[0]->append_child ($el);
2384 wakaba 1.121 push @{$self->{open_elements}}, [$el, $el_category->{$token->{tag_name}} || 0];
2385 wakaba 1.25 }
2386    
2387 wakaba 1.189 pop @{$self->{open_elements}};
2388     pop @{$self->{open_elements}} # <head>
2389     if $self->{insertion_mode} == AFTER_HEAD_IM;
2390     delete $self->{self_closing};
2391     $token = $self->_get_next_token;
2392     next B;
2393     } else {
2394     ## NOTE: "in head noscript" or "after head" insertion mode
2395     ## - in these cases, these tags are treated as same as
2396     ## normal in-body tags.
2397    
2398     #
2399     }
2400 wakaba 1.197 } elsif ($token->{tag_name} eq 'meta') {
2401     ## NOTE: There is a "as if in head" code clone.
2402     if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2403    
2404     $self->{parse_error}->(level => $self->{level}->{must}, type => 'after head',
2405     text => $token->{tag_name}, token => $token);
2406     push @{$self->{open_elements}},
2407     [$self->{head_element}, $el_category->{head}];
2408     $self->{head_element_inserted} = 1;
2409     } else {
2410    
2411     }
2412    
2413 wakaba 1.34 {
2414     my $el;
2415    
2416     $el = $self->{document}->create_element_ns
2417 wakaba 1.128 ($HTML_NS, [undef, $token->{tag_name}]);
2418 wakaba 1.34
2419     for my $attr_name (keys %{ $token->{attributes}}) {
2420 wakaba 1.117 my $attr_t = $token->{attributes}->{$attr_name};
2421 wakaba 1.128 my $attr = $self->{document}->create_attribute_ns (undef, [undef, $attr_name]);
2422 wakaba 1.117 $attr->value ($attr_t->{value});
2423     $attr->set_user_data (manakai_source_line => $attr_t->{line});
2424     $attr->set_user_data (manakai_source_column => $attr_t->{column});
2425     $el->set_attribute_node_ns ($attr);
2426 wakaba 1.34 }
2427    
2428 wakaba 1.114 $el->set_user_data (manakai_source_line => $token->{line})
2429     if defined $token->{line};
2430     $el->set_user_data (manakai_source_column => $token->{column})
2431     if defined $token->{column};
2432    
2433 wakaba 1.54 $self->{open_elements}->[-1]->[0]->append_child ($el);
2434 wakaba 1.121 push @{$self->{open_elements}}, [$el, $el_category->{$token->{tag_name}} || 0];
2435 wakaba 1.34 }
2436    
2437 wakaba 1.197 my $meta_el = pop @{$self->{open_elements}};
2438 wakaba 1.34
2439 wakaba 1.54 unless ($self->{confident}) {
2440 wakaba 1.131 if ($token->{attributes}->{charset}) {
2441 wakaba 1.79
2442 wakaba 1.131 ## NOTE: Whether the encoding is supported or not is handled
2443     ## in the {change_encoding} callback.
2444 wakaba 1.65 $self->{change_encoding}
2445 wakaba 1.112 ->($self, $token->{attributes}->{charset}->{value},
2446     $token);
2447 wakaba 1.68
2448     $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
2449     ->set_user_data (manakai_has_reference =>
2450     $token->{attributes}->{charset}
2451     ->{has_reference});
2452 wakaba 1.65 } elsif ($token->{attributes}->{content}) {
2453     if ($token->{attributes}->{content}->{value}
2454 wakaba 1.141 =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
2455 wakaba 1.183 [\x09\x0A\x0C\x0D\x20]*=
2456     [\x09\x0A\x0C\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2457     ([^"'\x09\x0A\x0C\x0D\x20]
2458     [^\x09\x0A\x0C\x0D\x20\x3B]*))/x) {
2459 wakaba 1.79
2460 wakaba 1.131 ## NOTE: Whether the encoding is supported or not is handled
2461     ## in the {change_encoding} callback.
2462 wakaba 1.65 $self->{change_encoding}
2463 wakaba 1.112 ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
2464     $token);
2465 wakaba 1.69 $meta_el->[0]->get_attribute_node_ns (undef, 'content')
2466     ->set_user_data (manakai_has_reference =>
2467     $token->{attributes}->{content}
2468     ->{has_reference});
2469 wakaba 1.79 } else {
2470    
2471 wakaba 1.65 }
2472 wakaba 1.54 }
2473 wakaba 1.68 } else {
2474     if ($token->{attributes}->{charset}) {
2475 wakaba 1.79
2476 wakaba 1.68 $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
2477     ->set_user_data (manakai_has_reference =>
2478     $token->{attributes}->{charset}
2479     ->{has_reference});
2480     }
2481 wakaba 1.69 if ($token->{attributes}->{content}) {
2482 wakaba 1.79
2483 wakaba 1.69 $meta_el->[0]->get_attribute_node_ns (undef, 'content')
2484     ->set_user_data (manakai_has_reference =>
2485     $token->{attributes}->{content}
2486     ->{has_reference});
2487     }
2488 wakaba 1.54 }
2489 wakaba 1.34
2490 wakaba 1.100 pop @{$self->{open_elements}} # <head>
2491 wakaba 1.56 if $self->{insertion_mode} == AFTER_HEAD_IM;
2492 wakaba 1.122 delete $self->{self_closing};
2493 wakaba 1.54 $token = $self->_get_next_token;
2494 wakaba 1.123 next B;
2495 wakaba 1.197 } elsif ($token->{tag_name} eq 'title') {
2496     if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2497    
2498     ## As if </noscript>
2499     pop @{$self->{open_elements}};
2500     $self->{parse_error}->(level => $self->{level}->{must}, type => 'in noscript', text => 'title',
2501     token => $token);
2502    
2503     $self->{insertion_mode} = IN_HEAD_IM;
2504     ## Reprocess in the "in head" insertion mode...
2505     } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2506    
2507     $self->{parse_error}->(level => $self->{level}->{must}, type => 'after head',
2508     text => $token->{tag_name}, token => $token);
2509     push @{$self->{open_elements}},
2510     [$self->{head_element}, $el_category->{head}];
2511     $self->{head_element_inserted} = 1;
2512     } else {
2513    
2514     }
2515 wakaba 1.54
2516 wakaba 1.197 ## NOTE: There is a "as if in head" code clone.
2517     $parse_rcdata->(RCDATA_CONTENT_MODEL);
2518 wakaba 1.201 ## ISSUE: A spec bug [Bug 6038]
2519 wakaba 1.200 splice @{$self->{open_elements}}, -2, 1, () # <head>
2520 wakaba 1.206 if ($self->{insertion_mode} & IM_MASK) == AFTER_HEAD_IM;
2521 wakaba 1.197 next B;
2522     } elsif ($token->{tag_name} eq 'style' or
2523     $token->{tag_name} eq 'noframes') {
2524     ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
2525     ## insertion mode IN_HEAD_IM)
2526     ## NOTE: There is a "as if in head" code clone.
2527     if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2528    
2529     $self->{parse_error}->(level => $self->{level}->{must}, type => 'after head',
2530     text => $token->{tag_name}, token => $token);
2531     push @{$self->{open_elements}},
2532     [$self->{head_element}, $el_category->{head}];
2533     $self->{head_element_inserted} = 1;
2534     } else {
2535    
2536     }
2537     $parse_rcdata->(CDATA_CONTENT_MODEL);
2538 wakaba 1.201 ## ISSUE: A spec bug [Bug 6038]
2539 wakaba 1.200 splice @{$self->{open_elements}}, -2, 1, () # <head>
2540 wakaba 1.206 if ($self->{insertion_mode} & IM_MASK) == AFTER_HEAD_IM;
2541 wakaba 1.197 next B;
2542 wakaba 1.200 } elsif ($token->{tag_name} eq 'noscript') {
2543 wakaba 1.56 if ($self->{insertion_mode} == IN_HEAD_IM) {
2544 wakaba 1.79
2545 wakaba 1.54 ## NOTE: and scripting is disalbed
2546    
2547 wakaba 1.1 {
2548     my $el;
2549    
2550     $el = $self->{document}->create_element_ns
2551 wakaba 1.128 ($HTML_NS, [undef, $token->{tag_name}]);
2552 wakaba 1.1
2553     for my $attr_name (keys %{ $token->{attributes}}) {
2554 wakaba 1.117 my $attr_t = $token->{attributes}->{$attr_name};
2555 wakaba 1.128 my $attr = $self->{document}->create_attribute_ns (undef, [undef, $attr_name]);
2556 wakaba 1.117 $attr->value ($attr_t->{value});
2557     $attr->set_user_data (manakai_source_line => $attr_t->{line});
2558     $attr->set_user_data (manakai_source_column => $attr_t->{column});
2559     $el->set_attribute_node_ns ($attr);
2560 wakaba 1.1 }
2561    
2562 wakaba 1.114 $el->set_user_data (manakai_source_line => $token->{line})
2563     if defined $token->{line};
2564     $el->set_user_data (manakai_source_column => $token->{column})
2565     if defined $token->{column};
2566    
2567 wakaba 1.54 $self->{open_elements}->[-1]->[0]->append_child ($el);
2568 wakaba 1.121 push @{$self->{open_elements}}, [$el, $el_category->{$token->{tag_name}} || 0];
2569 wakaba 1.1 }
2570    
2571 wakaba 1.56 $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
2572 wakaba 1.122
2573 wakaba 1.54 $token = $self->_get_next_token;
2574 wakaba 1.123 next B;
2575 wakaba 1.56 } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2576 wakaba 1.79
2577 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'in noscript', text => 'noscript',
2578     token => $token);
2579 wakaba 1.54 ## Ignore the token
2580 wakaba 1.122
2581 wakaba 1.54 $token = $self->_get_next_token;
2582 wakaba 1.123 next B;
2583 wakaba 1.54 } else {
2584 wakaba 1.79
2585 wakaba 1.54 #
2586     }
2587 wakaba 1.197 } elsif ($token->{tag_name} eq 'script') {
2588     if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2589    
2590     ## As if </noscript>
2591     pop @{$self->{open_elements}};
2592     $self->{parse_error}->(level => $self->{level}->{must}, type => 'in noscript', text => 'script',
2593     token => $token);
2594    
2595     $self->{insertion_mode} = IN_HEAD_IM;
2596     ## Reprocess in the "in head" insertion mode...
2597     } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2598    
2599     $self->{parse_error}->(level => $self->{level}->{must}, type => 'after head',
2600     text => $token->{tag_name}, token => $token);
2601     push @{$self->{open_elements}},
2602     [$self->{head_element}, $el_category->{head}];
2603     $self->{head_element_inserted} = 1;
2604     } else {
2605    
2606     }
2607 wakaba 1.54
2608 wakaba 1.197 ## NOTE: There is a "as if in head" code clone.
2609     $script_start_tag->();
2610 wakaba 1.201 ## ISSUE: A spec bug [Bug 6038]
2611 wakaba 1.200 splice @{$self->{open_elements}}, -2, 1 # <head>
2612 wakaba 1.206 if ($self->{insertion_mode} & IM_MASK) == AFTER_HEAD_IM;
2613 wakaba 1.197 next B;
2614     } elsif ($token->{tag_name} eq 'body' or
2615     $token->{tag_name} eq 'frameset') {
2616 wakaba 1.56 if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2617 wakaba 1.79
2618 wakaba 1.54 ## As if </noscript>
2619     pop @{$self->{open_elements}};
2620 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'in noscript',
2621     text => $token->{tag_name}, token => $token);
2622 wakaba 1.54
2623     ## Reprocess in the "in head" insertion mode...
2624     ## As if </head>
2625     pop @{$self->{open_elements}};
2626    
2627     ## Reprocess in the "after head" insertion mode...
2628 wakaba 1.56 } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2629 wakaba 1.79
2630 wakaba 1.54 pop @{$self->{open_elements}};
2631    
2632     ## Reprocess in the "after head" insertion mode...
2633 wakaba 1.79 } else {
2634    
2635 wakaba 1.54 }
2636    
2637     ## "after head" insertion mode
2638    
2639 wakaba 1.1 {
2640     my $el;
2641    
2642     $el = $self->{document}->create_element_ns
2643 wakaba 1.128 ($HTML_NS, [undef, $token->{tag_name}]);
2644 wakaba 1.1
2645     for my $attr_name (keys %{ $token->{attributes}}) {
2646 wakaba 1.117 my $attr_t = $token->{attributes}->{$attr_name};
2647 wakaba 1.128 my $attr = $self->{document}->create_attribute_ns (undef, [undef, $attr_name]);
2648 wakaba 1.117 $attr->value ($attr_t->{value});
2649     $attr->set_user_data (manakai_source_line => $attr_t->{line});
2650     $attr->set_user_data (manakai_source_column => $attr_t->{column});
2651     $el->set_attribute_node_ns ($attr);
2652 wakaba 1.1 }
2653    
2654 wakaba 1.114 $el->set_user_data (manakai_source_line => $token->{line})
2655     if defined $token->{line};
2656     $el->set_user_data (manakai_source_column => $token->{column})
2657     if defined $token->{column};
2658    
2659 wakaba 1.54 $self->{open_elements}->[-1]->[0]->append_child ($el);
2660 wakaba 1.121 push @{$self->{open_elements}}, [$el, $el_category->{$token->{tag_name}} || 0];
2661 wakaba 1.1 }
2662    
2663 wakaba 1.56 if ($token->{tag_name} eq 'body') {
2664 wakaba 1.79
2665 wakaba 1.56 $self->{insertion_mode} = IN_BODY_IM;
2666     } elsif ($token->{tag_name} eq 'frameset') {
2667 wakaba 1.79
2668 wakaba 1.56 $self->{insertion_mode} = IN_FRAMESET_IM;
2669     } else {
2670     die "$0: tag name: $self->{tag_name}";
2671     }
2672 wakaba 1.122
2673 wakaba 1.54 $token = $self->_get_next_token;
2674 wakaba 1.123 next B;
2675 wakaba 1.54 } else {
2676 wakaba 1.79
2677 wakaba 1.54 #
2678 wakaba 1.8 }
2679 wakaba 1.54
2680 wakaba 1.56 if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2681 wakaba 1.79
2682 wakaba 1.54 ## As if </noscript>
2683     pop @{$self->{open_elements}};
2684 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'in noscript:/',
2685     text => $token->{tag_name}, token => $token);
2686 wakaba 1.54
2687     ## Reprocess in the "in head" insertion mode...
2688     ## As if </head>
2689     pop @{$self->{open_elements}};
2690    
2691     ## Reprocess in the "after head" insertion mode...
2692 wakaba 1.56 } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2693 wakaba 1.79
2694 wakaba 1.54 ## As if </head>
2695     pop @{$self->{open_elements}};
2696    
2697     ## Reprocess in the "after head" insertion mode...
2698 wakaba 1.79 } else {
2699    
2700 wakaba 1.54 }
2701    
2702     ## "after head" insertion mode
2703     ## As if <body>
2704    
2705 wakaba 1.1 {
2706     my $el;
2707    
2708     $el = $self->{document}->create_element_ns
2709 wakaba 1.128 ($HTML_NS, [undef, 'body']);
2710 wakaba 1.1
2711 wakaba 1.114 $el->set_user_data (manakai_source_line => $token->{line})
2712     if defined $token->{line};
2713     $el->set_user_data (manakai_source_column => $token->{column})
2714     if defined $token->{column};
2715    
2716 wakaba 1.54 $self->{open_elements}->[-1]->[0]->append_child ($el);
2717 wakaba 1.121 push @{$self->{open_elements}}, [$el, $el_category->{'body'} || 0];
2718 wakaba 1.1 }
2719    
2720 wakaba 1.56 $self->{insertion_mode} = IN_BODY_IM;
2721 wakaba 1.54 ## reprocess
2722 wakaba 1.122
2723 wakaba 1.123 next B;
2724 wakaba 1.57 } elsif ($token->{type} == END_TAG_TOKEN) {
2725 wakaba 1.54 if ($token->{tag_name} eq 'head') {
2726 wakaba 1.56 if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2727 wakaba 1.79
2728 wakaba 1.54 ## As if <head>
2729    
2730     $self->{head_element} = $self->{document}->create_element_ns
2731 wakaba 1.128 ($HTML_NS, [undef, 'head']);
2732 wakaba 1.54
2733 wakaba 1.114 $self->{head_element}->set_user_data (manakai_source_line => $token->{line})
2734     if defined $token->{line};
2735     $self->{head_element}->set_user_data (manakai_source_column => $token->{column})
2736     if defined $token->{column};
2737    
2738 wakaba 1.54 $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2739 wakaba 1.121 push @{$self->{open_elements}},
2740     [$self->{head_element}, $el_category->{head}];
2741 wakaba 1.54
2742     ## Reprocess in the "in head" insertion mode...
2743     pop @{$self->{open_elements}};
2744 wakaba 1.56 $self->{insertion_mode} = AFTER_HEAD_IM;
2745 wakaba 1.54 $token = $self->_get_next_token;
2746 wakaba 1.123 next B;
2747 wakaba 1.56 } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2748 wakaba 1.79
2749 wakaba 1.54 ## As if </noscript>
2750     pop @{$self->{open_elements}};
2751 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'in noscript:/',
2752     text => 'head', token => $token);
2753 wakaba 1.54
2754     ## Reprocess in the "in head" insertion mode...
2755     pop @{$self->{open_elements}};
2756 wakaba 1.56 $self->{insertion_mode} = AFTER_HEAD_IM;
2757 wakaba 1.54 $token = $self->_get_next_token;
2758 wakaba 1.123 next B;
2759 wakaba 1.56 } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2760 wakaba 1.79
2761 wakaba 1.54 pop @{$self->{open_elements}};
2762 wakaba 1.56 $self->{insertion_mode} = AFTER_HEAD_IM;
2763 wakaba 1.54 $token = $self->_get_next_token;
2764 wakaba 1.123 next B;
2765 wakaba 1.136 } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2766    
2767 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag', text => 'head',
2768     token => $token);
2769 wakaba 1.136 ## Ignore the token
2770     $token = $self->_get_next_token;
2771     next B;
2772 wakaba 1.54 } else {
2773 wakaba 1.136 die "$0: $self->{insertion_mode}: Unknown insertion mode";
2774 wakaba 1.54 }
2775     } elsif ($token->{tag_name} eq 'noscript') {
2776 wakaba 1.56 if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2777 wakaba 1.79
2778 wakaba 1.54 pop @{$self->{open_elements}};
2779 wakaba 1.56 $self->{insertion_mode} = IN_HEAD_IM;
2780 wakaba 1.54 $token = $self->_get_next_token;
2781 wakaba 1.123 next B;
2782 wakaba 1.136 } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
2783     $self->{insertion_mode} == AFTER_HEAD_IM) {
2784 wakaba 1.79
2785 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
2786     text => 'noscript', token => $token);
2787 wakaba 1.54 ## Ignore the token ## ISSUE: An issue in the spec.
2788     $token = $self->_get_next_token;
2789 wakaba 1.123 next B;
2790 wakaba 1.54 } else {
2791 wakaba 1.79
2792 wakaba 1.54 #
2793     }
2794     } elsif ({
2795     body => 1, html => 1,
2796     }->{$token->{tag_name}}) {
2797 wakaba 1.198 ## TODO: This branch is entirely redundant.
2798     if ($self->{insertion_mode} == BEFORE_HEAD_IM or
2799 wakaba 1.136 $self->{insertion_mode} == IN_HEAD_IM or
2800     $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2801 wakaba 1.79
2802 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
2803     text => $token->{tag_name}, token => $token);
2804 wakaba 1.136 ## Ignore the token
2805     $token = $self->_get_next_token;
2806     next B;
2807     } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2808 wakaba 1.79
2809 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
2810     text => $token->{tag_name}, token => $token);
2811 wakaba 1.54 ## Ignore the token
2812     $token = $self->_get_next_token;
2813 wakaba 1.123 next B;
2814 wakaba 1.79 } else {
2815 wakaba 1.136 die "$0: $self->{insertion_mode}: Unknown insertion mode";
2816 wakaba 1.54 }
2817 wakaba 1.136 } elsif ($token->{tag_name} eq 'p') {
2818    
2819 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
2820     text => $token->{tag_name}, token => $token);
2821 wakaba 1.136 ## Ignore the token
2822     $token = $self->_get_next_token;
2823     next B;
2824     } elsif ($token->{tag_name} eq 'br') {
2825 wakaba 1.56 if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2826 wakaba 1.79
2827 wakaba 1.136 ## (before head) as if <head>, (in head) as if </head>
2828 wakaba 1.54
2829     $self->{head_element} = $self->{document}->create_element_ns
2830 wakaba 1.128 ($HTML_NS, [undef, 'head']);
2831 wakaba 1.54
2832 wakaba 1.114 $self->{head_element}->set_user_data (manakai_source_line => $token->{line})
2833     if defined $token->{line};
2834     $self->{head_element}->set_user_data (manakai_source_column => $token->{column})
2835     if defined $token->{column};
2836    
2837 wakaba 1.54 $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2838 wakaba 1.136 $self->{insertion_mode} = AFTER_HEAD_IM;
2839    
2840     ## Reprocess in the "after head" insertion mode...
2841     } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2842    
2843     ## As if </head>
2844     pop @{$self->{open_elements}};
2845     $self->{insertion_mode} = AFTER_HEAD_IM;
2846    
2847     ## Reprocess in the "after head" insertion mode...
2848     } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2849    
2850 wakaba 1.207 ## NOTE: Two parse errors for <head><noscript></br>
2851 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
2852     text => 'br', token => $token);
2853 wakaba 1.136 ## As if </noscript>
2854     pop @{$self->{open_elements}};
2855     $self->{insertion_mode} = IN_HEAD_IM;
2856 wakaba 1.54
2857     ## Reprocess in the "in head" insertion mode...
2858 wakaba 1.136 ## As if </head>
2859     pop @{$self->{open_elements}};
2860     $self->{insertion_mode} = AFTER_HEAD_IM;
2861 wakaba 1.54
2862 wakaba 1.136 ## Reprocess in the "after head" insertion mode...
2863     } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2864 wakaba 1.79
2865 wakaba 1.56 #
2866     } else {
2867 wakaba 1.136 die "$0: $self->{insertion_mode}: Unknown insertion mode";
2868 wakaba 1.54 }
2869 wakaba 1.136
2870     ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
2871 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
2872     text => 'br', token => $token);
2873 wakaba 1.136 ## Ignore the token
2874     $token = $self->_get_next_token;
2875     next B;
2876     } else {
2877    
2878 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
2879     text => $token->{tag_name}, token => $token);
2880 wakaba 1.136 ## Ignore the token
2881     $token = $self->_get_next_token;
2882     next B;
2883 wakaba 1.54 }
2884    
2885 wakaba 1.56 if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2886 wakaba 1.79
2887 wakaba 1.54 ## As if </noscript>
2888     pop @{$self->{open_elements}};
2889 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'in noscript:/',
2890     text => $token->{tag_name}, token => $token);
2891 wakaba 1.54
2892     ## Reprocess in the "in head" insertion mode...
2893     ## As if </head>
2894     pop @{$self->{open_elements}};
2895    
2896     ## Reprocess in the "after head" insertion mode...
2897 wakaba 1.56 } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2898 wakaba 1.79
2899 wakaba 1.54 ## As if </head>
2900     pop @{$self->{open_elements}};
2901    
2902     ## Reprocess in the "after head" insertion mode...
2903 wakaba 1.56 } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2904 wakaba 1.82 ## ISSUE: This case cannot be reached?
2905 wakaba 1.79
2906 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
2907     text => $token->{tag_name}, token => $token);
2908 wakaba 1.54 ## Ignore the token ## ISSUE: An issue in the spec.
2909     $token = $self->_get_next_token;
2910 wakaba 1.123 next B;
2911 wakaba 1.79 } else {
2912    
2913 wakaba 1.8 }
2914 wakaba 1.54
2915     ## "after head" insertion mode
2916     ## As if <body>
2917    
2918 wakaba 1.1 {
2919     my $el;
2920    
2921     $el = $self->{document}->create_element_ns
2922 wakaba 1.128 ($HTML_NS, [undef, 'body']);
2923 wakaba 1.1
2924 wakaba 1.114 $el->set_user_data (manakai_source_line => $token->{line})
2925     if defined $token->{line};
2926     $el->set_user_data (manakai_source_column => $token->{column})
2927     if defined $token->{column};
2928    
2929 wakaba 1.54 $self->{open_elements}->[-1]->[0]->append_child ($el);
2930 wakaba 1.121 push @{$self->{open_elements}}, [$el, $el_category->{'body'} || 0];
2931 wakaba 1.1 }
2932    
2933 wakaba 1.56 $self->{insertion_mode} = IN_BODY_IM;
2934 wakaba 1.54 ## reprocess
2935 wakaba 1.123 next B;
2936 wakaba 1.103 } elsif ($token->{type} == END_OF_FILE_TOKEN) {
2937     if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2938    
2939    
2940     ## NOTE: As if <head>
2941    
2942     $self->{head_element} = $self->{document}->create_element_ns
2943 wakaba 1.128 ($HTML_NS, [undef, 'head']);
2944 wakaba 1.103
2945 wakaba 1.114 $self->{head_element}->set_user_data (manakai_source_line => $token->{line})
2946     if defined $token->{line};
2947     $self->{head_element}->set_user_data (manakai_source_column => $token->{column})
2948     if defined $token->{column};
2949    
2950 wakaba 1.103 $self->{open_elements}->[-1]->[0]->append_child
2951     ($self->{head_element});
2952 wakaba 1.121 #push @{$self->{open_elements}},
2953     # [$self->{head_element}, $el_category->{head}];
2954 wakaba 1.103 #$self->{insertion_mode} = IN_HEAD_IM;
2955     ## NOTE: Reprocess.
2956    
2957     ## NOTE: As if </head>
2958     #pop @{$self->{open_elements}};
2959     #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
2960     ## NOTE: Reprocess.
2961    
2962     #
2963     } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2964    
2965    
2966     ## NOTE: As if </head>
2967     pop @{$self->{open_elements}};
2968     #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
2969     ## NOTE: Reprocess.
2970    
2971     #
2972     } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2973    
2974    
2975 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'in noscript:#eof', token => $token);
2976 wakaba 1.103
2977     ## As if </noscript>
2978     pop @{$self->{open_elements}};
2979     #$self->{insertion_mode} = IN_HEAD_IM;
2980     ## NOTE: Reprocess.
2981    
2982     ## NOTE: As if </head>
2983     pop @{$self->{open_elements}};
2984     #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
2985     ## NOTE: Reprocess.
2986    
2987     #
2988     } else {
2989    
2990     #
2991     }
2992    
2993     ## NOTE: As if <body>
2994    
2995     {
2996     my $el;
2997    
2998     $el = $self->{document}->create_element_ns
2999 wakaba 1.128 ($HTML_NS, [undef, 'body']);
3000 wakaba 1.103
3001 wakaba 1.114 $el->set_user_data (manakai_source_line => $token->{line})
3002     if defined $token->{line};
3003     $el->set_user_data (manakai_source_column => $token->{column})
3004     if defined $token->{column};
3005    
3006 wakaba 1.103 $self->{open_elements}->[-1]->[0]->append_child ($el);
3007 wakaba 1.121 push @{$self->{open_elements}}, [$el, $el_category->{'body'} || 0];
3008 wakaba 1.103 }
3009    
3010     $self->{insertion_mode} = IN_BODY_IM;
3011     ## NOTE: Reprocess.
3012 wakaba 1.123 next B;
3013 wakaba 1.103 } else {
3014     die "$0: $token->{type}: Unknown token type";
3015     }
3016 wakaba 1.58 } elsif ($self->{insertion_mode} & BODY_IMS) {
3017 wakaba 1.57 if ($token->{type} == CHARACTER_TOKEN) {
3018 wakaba 1.79
3019 wakaba 1.54 ## NOTE: There is a code clone of "character in body".
3020     $reconstruct_active_formatting_elements->($insert_to_current);
3021    
3022     $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3023    
3024     $token = $self->_get_next_token;
3025 wakaba 1.123 next B;
3026 wakaba 1.57 } elsif ($token->{type} == START_TAG_TOKEN) {
3027 wakaba 1.54 if ({
3028     caption => 1, col => 1, colgroup => 1, tbody => 1,
3029     td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3030     }->{$token->{tag_name}}) {
3031 wakaba 1.206 if (($self->{insertion_mode} & IM_MASK) == IN_CELL_IM) {
3032 wakaba 1.54 ## have an element in table scope
3033 wakaba 1.106 for (reverse 0..$#{$self->{open_elements}}) {
3034 wakaba 1.54 my $node = $self->{open_elements}->[$_];
3035 wakaba 1.201 if ($node->[1] == TABLE_CELL_EL) {
3036 wakaba 1.79
3037 wakaba 1.106
3038     ## Close the cell
3039 wakaba 1.122
3040     $token->{self_closing} = $self->{self_closing};
3041     unshift @{$self->{token}}, $token;
3042     delete $self->{self_closing};
3043     # <x>
3044 wakaba 1.120 $token = {type => END_TAG_TOKEN,
3045     tag_name => $node->[0]->manakai_local_name,
3046 wakaba 1.112 line => $token->{line},
3047     column => $token->{column}};
3048 wakaba 1.123 next B;
3049 wakaba 1.121 } elsif ($node->[1] & TABLE_SCOPING_EL) {
3050 wakaba 1.79
3051 wakaba 1.106 ## ISSUE: This case can never be reached, maybe.
3052     last;
3053 wakaba 1.54 }
3054 wakaba 1.106 }
3055    
3056 wakaba 1.54
3057 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'start tag not allowed',
3058     text => $token->{tag_name}, token => $token);
3059 wakaba 1.106 ## Ignore the token
3060 wakaba 1.122
3061 wakaba 1.106 $token = $self->_get_next_token;
3062 wakaba 1.123 next B;
3063 wakaba 1.206 } elsif (($self->{insertion_mode} & IM_MASK) == IN_CAPTION_IM) {
3064 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'not closed', text => 'caption',
3065     token => $token);
3066 wakaba 1.54
3067 wakaba 1.106 ## NOTE: As if </caption>.
3068 wakaba 1.54 ## have a table element in table scope
3069     my $i;
3070 wakaba 1.106 INSCOPE: {
3071     for (reverse 0..$#{$self->{open_elements}}) {
3072     my $node = $self->{open_elements}->[$_];
3073 wakaba 1.201 if ($node->[1] == CAPTION_EL) {
3074 wakaba 1.106
3075     $i = $_;
3076     last INSCOPE;
3077 wakaba 1.121 } elsif ($node->[1] & TABLE_SCOPING_EL) {
3078 wakaba 1.106
3079     last;
3080     }
3081 wakaba 1.54 }
3082 wakaba 1.106
3083    
3084 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'start tag not allowed',
3085     text => $token->{tag_name}, token => $token);
3086 wakaba 1.106 ## Ignore the token
3087 wakaba 1.122
3088 wakaba 1.106 $token = $self->_get_next_token;
3089 wakaba 1.123 next B;
3090 wakaba 1.54 } # INSCOPE
3091    
3092     ## generate implied end tags
3093 wakaba 1.121 while ($self->{open_elements}->[-1]->[1]
3094     & END_TAG_OPTIONAL_EL) {
3095 wakaba 1.79
3096 wakaba 1.86 pop @{$self->{open_elements}};
3097 wakaba 1.54 }
3098    
3099 wakaba 1.201 unless ($self->{open_elements}->[-1]->[1] == CAPTION_EL) {
3100 wakaba 1.79
3101 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'not closed',
3102     text => $self->{open_elements}->[-1]->[0]
3103 wakaba 1.120 ->manakai_local_name,
3104     token => $token);
3105 wakaba 1.79 } else {
3106    
3107 wakaba 1.54 }
3108    
3109     splice @{$self->{open_elements}}, $i;
3110    
3111     $clear_up_to_marker->();
3112    
3113 wakaba 1.56 $self->{insertion_mode} = IN_TABLE_IM;
3114 wakaba 1.54
3115     ## reprocess
3116 wakaba 1.122
3117 wakaba 1.123 next B;
3118 wakaba 1.54 } else {
3119 wakaba 1.79
3120 wakaba 1.54 #
3121     }
3122     } else {
3123 wakaba 1.79
3124 wakaba 1.54 #
3125     }
3126 wakaba 1.57 } elsif ($token->{type} == END_TAG_TOKEN) {
3127 wakaba 1.54 if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3128 wakaba 1.206 if (($self->{insertion_mode} & IM_MASK) == IN_CELL_IM) {
3129 wakaba 1.54 ## have an element in table scope
3130     my $i;
3131     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3132     my $node = $self->{open_elements}->[$_];
3133 wakaba 1.121 if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
3134 wakaba 1.79
3135 wakaba 1.54 $i = $_;
3136     last INSCOPE;
3137 wakaba 1.121 } elsif ($node->[1] & TABLE_SCOPING_EL) {
3138 wakaba 1.79
3139 wakaba 1.54 last INSCOPE;
3140     }
3141     } # INSCOPE
3142     unless (defined $i) {
3143 wakaba 1.79
3144 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
3145     text => $token->{tag_name},
3146     token => $token);
3147 wakaba 1.54 ## Ignore the token
3148     $token = $self->_get_next_token;
3149 wakaba 1.123 next B;
3150 wakaba 1.54 }
3151    
3152     ## generate implied end tags
3153 wakaba 1.121 while ($self->{open_elements}->[-1]->[1]
3154     & END_TAG_OPTIONAL_EL) {
3155 wakaba 1.79
3156 wakaba 1.86 pop @{$self->{open_elements}};
3157 wakaba 1.54 }
3158 wakaba 1.86
3159 wakaba 1.121 if ($self->{open_elements}->[-1]->[0]->manakai_local_name
3160     ne $token->{tag_name}) {
3161 wakaba 1.79
3162 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'not closed',
3163     text => $self->{open_elements}->[-1]->[0]
3164 wakaba 1.120 ->manakai_local_name,
3165     token => $token);
3166 wakaba 1.79 } else {
3167    
3168 wakaba 1.54 }
3169    
3170     splice @{$self->{open_elements}}, $i;
3171    
3172     $clear_up_to_marker->();
3173    
3174 wakaba 1.56 $self->{insertion_mode} = IN_ROW_IM;
3175 wakaba 1.54
3176     $token = $self->_get_next_token;
3177 wakaba 1.123 next B;
3178 wakaba 1.206 } elsif (($self->{insertion_mode} & IM_MASK) == IN_CAPTION_IM) {
3179 wakaba 1.79
3180 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
3181     text => $token->{tag_name}, token => $token);
3182 wakaba 1.54 ## Ignore the token
3183     $token = $self->_get_next_token;
3184 wakaba 1.123 next B;
3185 wakaba 1.54 } else {
3186 wakaba 1.79
3187 wakaba 1.54 #
3188     }
3189     } elsif ($token->{tag_name} eq 'caption') {
3190 wakaba 1.206 if (($self->{insertion_mode} & IM_MASK) == IN_CAPTION_IM) {
3191 wakaba 1.54 ## have a table element in table scope
3192     my $i;
3193 wakaba 1.106 INSCOPE: {
3194     for (reverse 0..$#{$self->{open_elements}}) {
3195     my $node = $self->{open_elements}->[$_];
3196 wakaba 1.201 if ($node->[1] == CAPTION_EL) {
3197 wakaba 1.106
3198     $i = $_;
3199     last INSCOPE;
3200 wakaba 1.121 } elsif ($node->[1] & TABLE_SCOPING_EL) {
3201 wakaba 1.106
3202     last;
3203     }
3204 wakaba 1.54 }
3205 wakaba 1.106
3206    
3207 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
3208     text => $token->{tag_name}, token => $token);
3209 wakaba 1.106 ## Ignore the token
3210     $token = $self->_get_next_token;
3211 wakaba 1.123 next B;
3212 wakaba 1.54 } # INSCOPE
3213    
3214     ## generate implied end tags
3215 wakaba 1.121 while ($self->{open_elements}->[-1]->[1]
3216     & END_TAG_OPTIONAL_EL) {
3217 wakaba 1.79
3218 wakaba 1.86 pop @{$self->{open_elements}};
3219 wakaba 1.54 }
3220    
3221 wakaba 1.201 unless ($self->{open_elements}->[-1]->[1] == CAPTION_EL) {
3222 wakaba 1.79
3223 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'not closed',
3224     text => $self->{open_elements}->[-1]->[0]
3225 wakaba 1.120 ->manakai_local_name,
3226     token => $token);
3227 wakaba 1.79 } else {
3228    
3229 wakaba 1.54 }
3230    
3231     splice @{$self->{open_elements}}, $i;
3232    
3233     $clear_up_to_marker->();
3234    
3235 wakaba 1.56 $self->{insertion_mode} = IN_TABLE_IM;
3236 wakaba 1.54
3237     $token = $self->_get_next_token;
3238 wakaba 1.123 next B;
3239 wakaba 1.206 } elsif (($self->{insertion_mode} & IM_MASK) == IN_CELL_IM) {
3240 wakaba 1.79
3241 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
3242     text => $token->{tag_name}, token => $token);
3243 wakaba 1.54 ## Ignore the token
3244     $token = $self->_get_next_token;
3245 wakaba 1.123 next B;
3246 wakaba 1.54 } else {
3247 wakaba 1.79
3248 wakaba 1.54 #
3249 wakaba 1.1 }
3250 wakaba 1.54 } elsif ({
3251     table => 1, tbody => 1, tfoot => 1,
3252     thead => 1, tr => 1,
3253     }->{$token->{tag_name}} and
3254 wakaba 1.206 ($self->{insertion_mode} & IM_MASK) == IN_CELL_IM) {
3255 wakaba 1.54 ## have an element in table scope
3256     my $i;
3257     my $tn;
3258 wakaba 1.106 INSCOPE: {
3259     for (reverse 0..$#{$self->{open_elements}}) {
3260     my $node = $self->{open_elements}->[$_];
3261 wakaba 1.121 if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
3262 wakaba 1.106
3263     $i = $_;
3264    
3265     ## Close the cell
3266 wakaba 1.122
3267     $token->{self_closing} = $self->{self_closing};
3268     unshift @{$self->{token}}, $token;
3269     delete $self->{self_closing};
3270     # </x>
3271 wakaba 1.112 $token = {type => END_TAG_TOKEN, tag_name => $tn,
3272     line => $token->{line},
3273     column => $token->{column}};
3274 wakaba 1.123 next B;
3275 wakaba 1.201 } elsif ($node->[1] == TABLE_CELL_EL) {
3276 wakaba 1.106
3277 wakaba 1.121 $tn = $node->[0]->manakai_local_name;
3278 wakaba 1.106 ## NOTE: There is exactly one |td| or |th| element
3279     ## in scope in the stack of open elements by definition.
3280 wakaba 1.121 } elsif ($node->[1] & TABLE_SCOPING_EL) {
3281 wakaba 1.106 ## ISSUE: Can this be reached?
3282    
3283     last;
3284     }
3285 wakaba 1.54 }
3286 wakaba 1.106
3287 wakaba 1.79
3288 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
3289     text => $token->{tag_name}, token => $token);
3290 wakaba 1.54 ## Ignore the token
3291     $token = $self->_get_next_token;
3292 wakaba 1.123 next B;
3293 wakaba 1.106 } # INSCOPE
3294 wakaba 1.54 } elsif ($token->{tag_name} eq 'table' and
3295 wakaba 1.206 ($self->{insertion_mode} & IM_MASK) == IN_CAPTION_IM) {
3296 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'not closed', text => 'caption',
3297     token => $token);
3298 wakaba 1.31
3299 wakaba 1.54 ## As if </caption>
3300     ## have a table element in table scope
3301     my $i;
3302     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3303     my $node = $self->{open_elements}->[$_];
3304 wakaba 1.201 if ($node->[1] == CAPTION_EL) {
3305 wakaba 1.79
3306 wakaba 1.54 $i = $_;
3307     last INSCOPE;
3308 wakaba 1.121 } elsif ($node->[1] & TABLE_SCOPING_EL) {
3309 wakaba 1.79
3310 wakaba 1.54 last INSCOPE;
3311     }
3312     } # INSCOPE
3313     unless (defined $i) {
3314 wakaba 1.79
3315 wakaba 1.204 ## TODO: Wrong error type?
3316 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
3317     text => 'caption', token => $token);
3318 wakaba 1.54 ## Ignore the token
3319     $token = $self->_get_next_token;
3320 wakaba 1.123 next B;
3321 wakaba 1.54 }
3322    
3323     ## generate implied end tags
3324 wakaba 1.121 while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
3325 wakaba 1.79
3326 wakaba 1.86 pop @{$self->{open_elements}};
3327 wakaba 1.54 }
3328 wakaba 1.20
3329 wakaba 1.201 unless ($self->{open_elements}->[-1]->[1] == CAPTION_EL) {
3330 wakaba 1.79
3331 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'not closed',
3332     text => $self->{open_elements}->[-1]->[0]
3333 wakaba 1.120 ->manakai_local_name,
3334     token => $token);
3335 wakaba 1.79 } else {
3336    
3337 wakaba 1.54 }
3338 wakaba 1.12
3339 wakaba 1.54 splice @{$self->{open_elements}}, $i;
3340 wakaba 1.31
3341 wakaba 1.54 $clear_up_to_marker->();
3342 wakaba 1.1
3343 wakaba 1.56 $self->{insertion_mode} = IN_TABLE_IM;
3344 wakaba 1.3
3345 wakaba 1.54 ## reprocess
3346 wakaba 1.123 next B;
3347 wakaba 1.54 } elsif ({
3348     body => 1, col => 1, colgroup => 1, html => 1,
3349     }->{$token->{tag_name}}) {
3350 wakaba 1.58 if ($self->{insertion_mode} & BODY_TABLE_IMS) {
3351 wakaba 1.79
3352 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
3353     text => $token->{tag_name}, token => $token);
3354 wakaba 1.54 ## Ignore the token
3355     $token = $self->_get_next_token;
3356 wakaba 1.123 next B;
3357 wakaba 1.54 } else {
3358 wakaba 1.79
3359 wakaba 1.54 #
3360     }
3361 wakaba 1.206 } elsif ({
3362     tbody => 1, tfoot => 1,
3363     thead => 1, tr => 1,
3364     }->{$token->{tag_name}} and
3365     ($self->{insertion_mode} & IM_MASK) == IN_CAPTION_IM) {
3366    
3367     $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
3368     text => $token->{tag_name}, token => $token);
3369     ## Ignore the token
3370     $token = $self->_get_next_token;
3371     next B;
3372     } else {
3373    
3374     #
3375     }
3376 wakaba 1.103 } elsif ($token->{type} == END_OF_FILE_TOKEN) {
3377     for my $entry (@{$self->{open_elements}}) {
3378 wakaba 1.121 unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
3379 wakaba 1.103
3380 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'in body:#eof', token => $token);
3381 wakaba 1.103 last;
3382     }
3383     }
3384    
3385     ## Stop parsing.
3386     last B;
3387 wakaba 1.54 } else {
3388     die "$0: $token->{type}: Unknown token type";
3389 wakaba 1.1 }
3390    
3391 wakaba 1.54 $insert = $insert_to_current;
3392     #
3393 wakaba 1.58 } elsif ($self->{insertion_mode} & TABLE_IMS) {
3394 wakaba 1.60 if ($token->{type} == CHARACTER_TOKEN) {
3395 wakaba 1.95 if (not $open_tables->[-1]->[1] and # tainted
3396 wakaba 1.184 $token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
3397 wakaba 1.95 $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3398 wakaba 1.54
3399 wakaba 1.95 unless (length $token->{data}) {
3400    
3401     $token = $self->_get_next_token;
3402 wakaba 1.123 next B;
3403 wakaba 1.95 } else {
3404    
3405     }
3406     }
3407 wakaba 1.1
3408 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'in table:#text', token => $token);
3409 wakaba 1.1
3410 wakaba 1.197 ## NOTE: As if in body, but insert into the foster parent element.
3411     $reconstruct_active_formatting_elements->($insert_to_foster);
3412    
3413     if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3414     # MUST
3415     my $foster_parent_element;
3416     my $next_sibling;
3417     my $prev_sibling;
3418     OE: for (reverse 0..$#{$self->{open_elements}}) {
3419 wakaba 1.201 if ($self->{open_elements}->[$_]->[1] == TABLE_EL) {
3420 wakaba 1.197 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3421     if (defined $parent and $parent->node_type == 1) {
3422     $foster_parent_element = $parent;
3423 wakaba 1.79
3424 wakaba 1.197 $next_sibling = $self->{open_elements}->[$_]->[0];
3425     $prev_sibling = $next_sibling->previous_sibling;
3426     #
3427 wakaba 1.54 } else {
3428 wakaba 1.79
3429 wakaba 1.197 $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
3430     $prev_sibling = $foster_parent_element->last_child;
3431     #
3432 wakaba 1.54 }
3433 wakaba 1.197 last OE;
3434     }
3435     } # OE
3436     $foster_parent_element = $self->{open_elements}->[0]->[0] and
3437     $prev_sibling = $foster_parent_element->last_child
3438     unless defined $foster_parent_element;
3439     undef $prev_sibling unless $open_tables->[-1]->[2]; # ~node inserted
3440     if (defined $prev_sibling and
3441     $prev_sibling->node_type == 3) {
3442    
3443     $prev_sibling->manakai_append_text ($token->{data});
3444     } else {
3445    
3446     $foster_parent_element->insert_before
3447     ($self->{document}->create_text_node ($token->{data}),
3448     $next_sibling);
3449     }
3450 wakaba 1.95 $open_tables->[-1]->[1] = 1; # tainted
3451 wakaba 1.197 $open_tables->[-1]->[2] = 1; # ~node inserted
3452 wakaba 1.95 } else {
3453 wakaba 1.197 ## NOTE: Fragment case or in a foster parent'ed element
3454     ## (e.g. |<table><span>a|). In fragment case, whether the
3455     ## character is appended to existing node or a new node is
3456     ## created is irrelevant, since the foster parent'ed nodes
3457     ## are discarded and fragment parsing does not invoke any
3458     ## script.
3459 wakaba 1.95
3460 wakaba 1.197 $self->{open_elements}->[-1]->[0]->manakai_append_text
3461     ($token->{data});
3462 wakaba 1.95 }
3463 wakaba 1.54
3464 wakaba 1.95 $token = $self->_get_next_token;
3465 wakaba 1.123 next B;
3466 wakaba 1.60 } elsif ($token->{type} == START_TAG_TOKEN) {
3467 wakaba 1.150 if ({
3468 wakaba 1.206 tr => (($self->{insertion_mode} & IM_MASK) != IN_ROW_IM),
3469 wakaba 1.150 th => 1, td => 1,
3470     }->{$token->{tag_name}}) {
3471 wakaba 1.206 if (($self->{insertion_mode} & IM_MASK) == IN_TABLE_IM) {
3472 wakaba 1.150 ## Clear back to table context
3473     while (not ($self->{open_elements}->[-1]->[1]
3474     & TABLE_SCOPING_EL)) {
3475    
3476     pop @{$self->{open_elements}};
3477     }
3478    
3479    
3480 wakaba 1.54 {
3481     my $el;
3482    
3483     $el = $self->{document}->create_element_ns
3484 wakaba 1.128 ($HTML_NS, [undef, 'tbody']);
3485 wakaba 1.54
3486 wakaba 1.114 $el->set_user_data (manakai_source_line => $token->{line})
3487     if defined $token->{line};
3488     $el->set_user_data (manakai_source_column => $token->{column})
3489     if defined $token->{column};
3490    
3491 wakaba 1.54 $self->{open_elements}->[-1]->[0]->append_child ($el);
3492 wakaba 1.121 push @{$self->{open_elements}}, [$el, $el_category->{'tbody'} || 0];
3493 wakaba 1.54 }
3494    
3495 wakaba 1.150 $self->{insertion_mode} = IN_TABLE_BODY_IM;
3496     ## reprocess in the "in table body" insertion mode...
3497     }
3498    
3499 wakaba 1.206 if (($self->{insertion_mode} & IM_MASK) == IN_TABLE_BODY_IM) {
3500 wakaba 1.150 unless ($token->{tag_name} eq 'tr') {
3501    
3502     $self->{parse_error}->(level => $self->{level}->{must}, type => 'missing start tag:tr', token => $token);
3503     }
3504 wakaba 1.54
3505 wakaba 1.150 ## Clear back to table body context
3506     while (not ($self->{open_elements}->[-1]->[1]
3507     & TABLE_ROWS_SCOPING_EL)) {
3508    
3509     ## ISSUE: Can this case be reached?
3510     pop @{$self->{open_elements}};
3511     }
3512 wakaba 1.54
3513 wakaba 1.197 $self->{insertion_mode} = IN_ROW_IM;
3514     if ($token->{tag_name} eq 'tr') {
3515    
3516    
3517 wakaba 1.54 {
3518     my $el;
3519    
3520     $el = $self->{document}->create_element_ns
3521 wakaba 1.128 ($HTML_NS, [undef, $token->{tag_name}]);
3522 wakaba 1.54
3523     for my $attr_name (keys %{ $token->{attributes}}) {
3524 wakaba 1.117 my $attr_t = $token->{attributes}->{$attr_name};
3525 wakaba 1.128 my $attr = $self->{document}->create_attribute_ns (undef, [undef, $attr_name]);
3526 wakaba 1.117 $attr->value ($attr_t->{value});
3527     $attr->set_user_data (manakai_source_line => $attr_t->{line});
3528     $attr->set_user_data (manakai_source_column => $attr_t->{column});
3529     $el->set_attribute_node_ns ($attr);
3530 wakaba 1.1 }
3531 wakaba 1.54
3532 wakaba 1.114 $el->set_user_data (manakai_source_line => $token->{line})
3533     if defined $token->{line};
3534     $el->set_user_data (manakai_source_column => $token->{column})
3535     if defined $token->{column};
3536    
3537 wakaba 1.54 $self->{open_elements}->[-1]->[0]->append_child ($el);
3538 wakaba 1.121 push @{$self->{open_elements}}, [$el, $el_category->{$token->{tag_name}} || 0];
3539 wakaba 1.54 }
3540    
3541 wakaba 1.197 $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
3542    
3543     $token = $self->_get_next_token;
3544     next B;
3545     } else {
3546    
3547    
3548 wakaba 1.54 {
3549     my $el;
3550    
3551     $el = $self->{document}->create_element_ns
3552 wakaba 1.128 ($HTML_NS, [undef, 'tr']);
3553 wakaba 1.54
3554 wakaba 1.114 $el->set_user_data (manakai_source_line => $token->{line})
3555     if defined $token->{line};
3556     $el->set_user_data (manakai_source_column => $token->{column})
3557     if defined $token->{column};
3558    
3559 wakaba 1.54 $self->{open_elements}->[-1]->[0]->append_child ($el);
3560 wakaba 1.121 push @{$self->{open_elements}}, [$el, $el_category->{'tr'} || 0];
3561 wakaba 1.54 }
3562    
3563 wakaba 1.197 ## reprocess in the "in row" insertion mode
3564     }
3565     } else {
3566    
3567     }
3568 wakaba 1.52
3569 wakaba 1.54 ## Clear back to table row context
3570 wakaba 1.121 while (not ($self->{open_elements}->[-1]->[1]
3571     & TABLE_ROW_SCOPING_EL)) {
3572 wakaba 1.79
3573 wakaba 1.54 pop @{$self->{open_elements}};
3574     }
3575    
3576 wakaba 1.197
3577 wakaba 1.54 {
3578     my $el;
3579    
3580     $el = $self->{document}->create_element_ns
3581 wakaba 1.128 ($HTML_NS, [undef, $token->{tag_name}]);
3582 wakaba 1.1
3583 wakaba 1.54 for my $attr_name (keys %{ $token->{attributes}}) {
3584 wakaba 1.117 my $attr_t = $token->{attributes}->{$attr_name};
3585 wakaba 1.128 my $attr = $self->{document}->create_attribute_ns (undef, [undef, $attr_name]);
3586 wakaba 1.117 $attr->value ($attr_t->{value});
3587     $attr->set_user_data (manakai_source_line => $attr_t->{line});
3588     $attr->set_user_data (manakai_source_column => $attr_t->{column});
3589     $el->set_attribute_node_ns ($attr);
3590 wakaba 1.54 }
3591    
3592 wakaba 1.114 $el->set_user_data (manakai_source_line => $token->{line})
3593     if defined $token->{line};
3594     $el->set_user_data (manakai_source_column => $token->{column})
3595     if defined $token->{column};
3596    
3597 wakaba 1.54 $self->{open_elements}->[-1]->[0]->append_child ($el);
3598 wakaba 1.121 push @{$self->{open_elements}}, [$el, $el_category->{$token->{tag_name}} || 0];
3599 wakaba 1.54 }
3600    
3601 wakaba 1.197 $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
3602     $self->{insertion_mode} = IN_CELL_IM;
3603 wakaba 1.54
3604 wakaba 1.197 push @$active_formatting_elements, ['#marker', ''];
3605 wakaba 1.54
3606 wakaba 1.197
3607     $token = $self->_get_next_token;
3608     next B;
3609     } elsif ({
3610     caption => 1, col => 1, colgroup => 1,
3611     tbody => 1, tfoot => 1, thead => 1,
3612     tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3613     }->{$token->{tag_name}}) {
3614 wakaba 1.206 if (($self->{insertion_mode} & IM_MASK) == IN_ROW_IM) {
3615 wakaba 1.197 ## As if </tr>
3616     ## have an element in table scope
3617     my $i;
3618     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3619     my $node = $self->{open_elements}->[$_];
3620 wakaba 1.201 if ($node->[1] == TABLE_ROW_EL) {
3621 wakaba 1.197
3622     $i = $_;
3623     last INSCOPE;
3624     } elsif ($node->[1] & TABLE_SCOPING_EL) {
3625    
3626     last INSCOPE;
3627     }
3628     } # INSCOPE
3629     unless (defined $i) {
3630    
3631     ## TODO: This type is wrong.
3632     $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmacthed end tag',
3633     text => $token->{tag_name}, token => $token);
3634     ## Ignore the token
3635 wakaba 1.122
3636 wakaba 1.54 $token = $self->_get_next_token;
3637 wakaba 1.123 next B;
3638 wakaba 1.197 }
3639 wakaba 1.54
3640     ## Clear back to table row context
3641 wakaba 1.121 while (not ($self->{open_elements}->[-1]->[1]
3642     & TABLE_ROW_SCOPING_EL)) {
3643 wakaba 1.79
3644 wakaba 1.83 ## ISSUE: Can this case be reached?
3645 wakaba 1.54 pop @{$self->{open_elements}};
3646     }
3647    
3648     pop @{$self->{open_elements}}; # tr
3649 wakaba 1.56 $self->{insertion_mode} = IN_TABLE_BODY_IM;
3650 wakaba 1.54 if ($token->{tag_name} eq 'tr') {
3651 wakaba 1.79
3652 wakaba 1.54 ## reprocess
3653 wakaba 1.122
3654 wakaba 1.123 next B;
3655 wakaba 1.54 } else {
3656 wakaba 1.79
3657 wakaba 1.54 ## reprocess in the "in table body" insertion mode...
3658     }
3659     }
3660 wakaba 1.52
3661 wakaba 1.206 if (($self->{insertion_mode} & IM_MASK) == IN_TABLE_BODY_IM) {
3662 wakaba 1.54 ## have an element in table scope
3663     my $i;
3664     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3665     my $node = $self->{open_elements}->[$_];
3666 wakaba 1.201 if ($node->[1] == TABLE_ROW_GROUP_EL) {
3667 wakaba 1.79
3668 wakaba 1.54 $i = $_;
3669     last INSCOPE;
3670 wakaba 1.121 } elsif ($node->[1] & TABLE_SCOPING_EL) {
3671 wakaba 1.79
3672 wakaba 1.54 last INSCOPE;
3673     }
3674     } # INSCOPE
3675     unless (defined $i) {
3676 wakaba 1.79
3677 wakaba 1.150 ## TODO: This erorr type is wrong.
3678     $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
3679     text => $token->{tag_name}, token => $token);
3680 wakaba 1.54 ## Ignore the token
3681 wakaba 1.122
3682 wakaba 1.54 $token = $self->_get_next_token;
3683 wakaba 1.123 next B;
3684 wakaba 1.54 }
3685 wakaba 1.52
3686 wakaba 1.54 ## Clear back to table body context
3687 wakaba 1.121 while (not ($self->{open_elements}->[-1]->[1]
3688     & TABLE_ROWS_SCOPING_EL)) {
3689 wakaba 1.79
3690 wakaba 1.83 ## ISSUE: Can this state be reached?
3691 wakaba 1.54 pop @{$self->{open_elements}};
3692     }
3693    
3694     ## As if <{current node}>
3695     ## have an element in table scope
3696     ## true by definition
3697    
3698     ## Clear back to table body context
3699     ## nop by definition
3700    
3701     pop @{$self->{open_elements}};
3702 wakaba 1.56 $self->{insertion_mode} = IN_TABLE_IM;
3703 wakaba 1.54 ## reprocess in "in table" insertion mode...
3704 wakaba 1.79 } else {
3705    
3706 wakaba 1.54 }
3707 wakaba 1.51
3708 wakaba 1.197 if ($token->{tag_name} eq 'col') {
3709     ## Clear back to table context
3710     while (not ($self->{open_elements}->[-1]->[1]
3711     & TABLE_SCOPING_EL)) {
3712    
3713     ## ISSUE: Can this state be reached?
3714     pop @{$self->{open_elements}};
3715     }
3716    
3717    
3718 wakaba 1.51 {
3719     my $el;
3720    
3721     $el = $self->{document}->create_element_ns
3722 wakaba 1.128 ($HTML_NS, [undef, 'colgroup']);
3723 wakaba 1.51
3724 wakaba 1.114 $el->set_user_data (manakai_source_line => $token->{line})
3725     if defined $token->{line};
3726     $el->set_user_data (manakai_source_column => $token->{column})
3727     if defined $token->{column};
3728    
3729 wakaba 1.51 $self->{open_elements}->[-1]->[0]->append_child ($el);
3730 wakaba 1.121 push @{$self->{open_elements}}, [$el, $el_category->{'colgroup'} || 0];
3731 wakaba 1.51 }
3732    
3733 wakaba 1.197 $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
3734     ## reprocess
3735     $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
3736    
3737     next B;
3738     } elsif ({
3739     caption => 1,
3740     colgroup => 1,
3741     tbody => 1, tfoot => 1, thead => 1,
3742     }->{$token->{tag_name}}) {
3743     ## Clear back to table context
3744 wakaba 1.121 while (not ($self->{open_elements}->[-1]->[1]
3745     & TABLE_SCOPING_EL)) {
3746 wakaba 1.79
3747 wakaba 1.83 ## ISSUE: Can this state be reached?
3748 wakaba 1.54 pop @{$self->{open_elements}};
3749     }
3750    
3751 wakaba 1.197 push @$active_formatting_elements, ['#marker', '']
3752     if $token->{tag_name} eq 'caption';
3753 wakaba 1.52
3754 wakaba 1.197
3755 wakaba 1.54 {
3756     my $el;
3757    
3758     $el = $self->{document}->create_element_ns
3759 wakaba 1.128 ($HTML_NS, [undef, $token->{tag_name}]);
3760 wakaba 1.52
3761 wakaba 1.54 for my $attr_name (keys %{ $token->{attributes}}) {
3762 wakaba 1.117 my $attr_t = $token->{attributes}->{$attr_name};
3763 wakaba 1.128 my $attr = $self->{document}->create_attribute_ns (undef, [undef, $attr_name]);
3764 wakaba 1.117 $attr->value ($attr_t->{value});
3765     $attr->set_user_data (manakai_source_line => $attr_t->{line});
3766     $attr->set_user_data (manakai_source_column => $attr_t->{column});
3767     $el->set_attribute_node_ns ($attr);
3768 wakaba 1.52 }
3769    
3770 wakaba 1.114 $el->set_user_data (manakai_source_line => $token->{line})
3771     if defined $token->{line};
3772     $el->set_user_data (manakai_source_column => $token->{column})
3773     if defined $token->{column};
3774    
3775 wakaba 1.54 $self->{open_elements}->[-1]->[0]->append_child ($el);
3776 wakaba 1.121 push @{$self->{open_elements}}, [$el, $el_category->{$token->{tag_name}} || 0];
3777 wakaba 1.54 }
3778    
3779 wakaba 1.197 $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
3780     $self->{insertion_mode} = {
3781     caption => IN_CAPTION_IM,
3782     colgroup => IN_COLUMN_GROUP_IM,
3783     tbody => IN_TABLE_BODY_IM,
3784     tfoot => IN_TABLE_BODY_IM,
3785     thead => IN_TABLE_BODY_IM,
3786     }->{$token->{tag_name}};
3787     $token = $self->_get_next_token;
3788    
3789     next B;
3790     } else {
3791     die "$0: in table: <>: $token->{tag_name}";
3792     }
3793 wakaba 1.54 } elsif ($token->{tag_name} eq 'table') {
3794 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'not closed',
3795     text => $self->{open_elements}->[-1]->[0]
3796 wakaba 1.120 ->manakai_local_name,
3797     token => $token);
3798 wakaba 1.54
3799     ## As if </table>
3800     ## have a table element in table scope
3801     my $i;
3802     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3803     my $node = $self->{open_elements}->[$_];
3804 wakaba 1.201 if ($node->[1] == TABLE_EL) {
3805 wakaba 1.79
3806 wakaba 1.54 $i = $_;
3807     last INSCOPE;
3808 wakaba 1.121 } elsif ($node->[1] & TABLE_SCOPING_EL) {
3809 wakaba 1.79
3810 wakaba 1.54 last INSCOPE;
3811     }
3812     } # INSCOPE
3813     unless (defined $i) {
3814 wakaba 1.79
3815 wakaba 1.83 ## TODO: The following is wrong, maybe.
3816 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag', text => 'table',
3817     token => $token);
3818 wakaba 1.54 ## Ignore tokens </table><table>
3819 wakaba 1.122
3820 wakaba 1.52 $token = $self->_get_next_token;
3821 wakaba 1.123 next B;
3822 wakaba 1.52 }
3823    
3824 wakaba 1.149 ## TODO: Followings are removed from the latest spec.
3825 wakaba 1.54 ## generate implied end tags
3826 wakaba 1.121 while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
3827 wakaba 1.79
3828 wakaba 1.86 pop @{$self->{open_elements}};
3829 wakaba 1.54 }
3830    
3831 wakaba 1.201 unless ($self->{open_elements}->[-1]->[1] == TABLE_EL) {
3832 wakaba 1.79
3833 wakaba 1.120 ## NOTE: |<table><tr><table>|
3834 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'not closed',
3835     text => $self->{open_elements}->[-1]->[0]
3836 wakaba 1.120 ->manakai_local_name,
3837     token => $token);
3838 wakaba 1.79 } else {
3839    
3840 wakaba 1.54 }
3841    
3842     splice @{$self->{open_elements}}, $i;
3843 wakaba 1.95 pop @{$open_tables};
3844 wakaba 1.54
3845     $self->_reset_insertion_mode;
3846 wakaba 1.52
3847 wakaba 1.122 ## reprocess
3848    
3849 wakaba 1.123 next B;
3850 wakaba 1.100 } elsif ($token->{tag_name} eq 'style') {
3851     if (not $open_tables->[-1]->[1]) { # tainted
3852    
3853     ## NOTE: This is a "as if in head" code clone.
3854     $parse_rcdata->(CDATA_CONTENT_MODEL);
3855 wakaba 1.197 $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
3856 wakaba 1.123 next B;
3857 wakaba 1.100 } else {
3858    
3859     #
3860     }
3861     } elsif ($token->{tag_name} eq 'script') {
3862     if (not $open_tables->[-1]->[1]) { # tainted
3863    
3864     ## NOTE: This is a "as if in head" code clone.
3865     $script_start_tag->();
3866 wakaba 1.197 $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
3867 wakaba 1.123 next B;
3868 wakaba 1.100 } else {
3869    
3870     #
3871     }
3872 wakaba 1.98 } elsif ($token->{tag_name} eq 'input') {
3873     if (not $open_tables->[-1]->[1]) { # tainted
3874     if ($token->{attributes}->{type}) { ## TODO: case
3875     my $type = lc $token->{attributes}->{type}->{value};
3876     if ($type eq 'hidden') {
3877    
3878 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'in table',
3879     text => $token->{tag_name}, token => $token);
3880 wakaba 1.98
3881    
3882     {
3883     my $el;
3884    
3885     $el = $self->{document}->create_element_ns
3886 wakaba 1.128 ($HTML_NS, [undef, $token->{tag_name}]);
3887 wakaba 1.98
3888     for my $attr_name (keys %{ $token->{attributes}}) {
3889 wakaba 1.117 my $attr_t = $token->{attributes}->{$attr_name};
3890 wakaba 1.128 my $attr = $self->{document}->create_attribute_ns (undef, [undef, $attr_name]);
3891 wakaba 1.117 $attr->value ($attr_t->{value});
3892     $attr->set_user_data (manakai_source_line => $attr_t->{line});
3893     $attr->set_user_data (manakai_source_column => $attr_t->{column});
3894     $el->set_attribute_node_ns ($attr);
3895 wakaba 1.98 }
3896    
3897 wakaba 1.114 $el->set_user_data (manakai_source_line => $token->{line})
3898     if defined $token->{line};
3899     $el->set_user_data (manakai_source_column => $token->{column})
3900     if defined $token->{column};
3901    
3902 wakaba 1.98 $self->{open_elements}->[-1]->[0]->append_child ($el);
3903 wakaba 1.121 push @{$self->{open_elements}}, [$el, $el_category->{$token->{tag_name}} || 0];
3904 wakaba 1.98 }
3905    
3906 wakaba 1.197 $open_tables->[-1]->[2] = 0 if @$open_tables; # ~node inserted
3907 wakaba 1.98
3908     ## TODO: form element pointer
3909    
3910     pop @{$self->{open_elements}};
3911    
3912     $token = $self->_get_next_token;
3913 wakaba 1.122 delete $self->{self_closing};
3914 wakaba 1.123 next B;
3915 wakaba 1.98 } else {
3916    
3917     #
3918     }
3919     } else {
3920    
3921     #
3922     }
3923     } else {
3924    
3925     #
3926     }
3927 wakaba 1.60 } else {
3928 wakaba 1.79
3929 wakaba 1.60 #
3930     }
3931 wakaba 1.98
3932 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'in table', text => $token->{tag_name},
3933     token => $token);
3934 wakaba 1.98
3935     $insert = $insert_to_foster;
3936     #
3937 wakaba 1.60 } elsif ($token->{type} == END_TAG_TOKEN) {
3938 wakaba 1.206 if ($token->{tag_name} eq 'tr' and
3939     ($self->{insertion_mode} & IM_MASK) == IN_ROW_IM) {
3940     ## have an element in table scope
3941 wakaba 1.54 my $i;
3942     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3943     my $node = $self->{open_elements}->[$_];
3944 wakaba 1.201 if ($node->[1] == TABLE_ROW_EL) {
3945 wakaba 1.79
3946 wakaba 1.54 $i = $_;
3947     last INSCOPE;
3948 wakaba 1.121 } elsif ($node->[1] & TABLE_SCOPING_EL) {
3949 wakaba 1.79
3950 wakaba 1.54 last INSCOPE;
3951     }
3952     } # INSCOPE
3953     unless (defined $i) {
3954 wakaba 1.79
3955 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
3956     text => $token->{tag_name}, token => $token);
3957 wakaba 1.54 ## Ignore the token
3958 wakaba 1.122
3959 wakaba 1.54 $token = $self->_get_next_token;
3960 wakaba 1.123 next B;
3961 wakaba 1.79 } else {
3962    
3963 wakaba 1.54 }
3964    
3965     ## Clear back to table row context
3966 wakaba 1.121 while (not ($self->{open_elements}->[-1]->[1]
3967     & TABLE_ROW_SCOPING_EL)) {
3968 wakaba 1.79
3969 wakaba 1.83 ## ISSUE: Can this state be reached?
3970 wakaba 1.54 pop @{$self->{open_elements}};
3971     }
3972    
3973     pop @{$self->{open_elements}}; # tr
3974 wakaba 1.56 $self->{insertion_mode} = IN_TABLE_BODY_IM;
3975 wakaba 1.54 $token = $self->_get_next_token;
3976 wakaba 1.122
3977 wakaba 1.123 next B;
3978 wakaba 1.54 } elsif ($token->{tag_name} eq 'table') {
3979 wakaba 1.206 if (($self->{insertion_mode} & IM_MASK) == IN_ROW_IM) {
3980 wakaba 1.54 ## As if </tr>
3981     ## have an element in table scope
3982     my $i;
3983     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3984     my $node = $self->{open_elements}->[$_];
3985 wakaba 1.201 if ($node->[1] == TABLE_ROW_EL) {
3986 wakaba 1.79
3987 wakaba 1.54 $i = $_;
3988     last INSCOPE;
3989 wakaba 1.121 } elsif ($node->[1] & TABLE_SCOPING_EL) {
3990 wakaba 1.79
3991 wakaba 1.54 last INSCOPE;
3992     }
3993     } # INSCOPE
3994     unless (defined $i) {
3995 wakaba 1.79
3996 wakaba 1.83 ## TODO: The following is wrong.
3997 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
3998     text => $token->{type}, token => $token);
3999 wakaba 1.54 ## Ignore the token
4000 wakaba 1.122
4001 wakaba 1.54 $token = $self->_get_next_token;
4002 wakaba 1.123 next B;
4003 wakaba 1.54 }
4004    
4005     ## Clear back to table row context
4006 wakaba 1.121 while (not ($self->{open_elements}->[-1]->[1]
4007     & TABLE_ROW_SCOPING_EL)) {
4008 wakaba 1.79
4009 wakaba 1.83 ## ISSUE: Can this state be reached?
4010 wakaba 1.54 pop @{$self->{open_elements}};
4011     }
4012    
4013     pop @{$self->{open_elements}}; # tr
4014 wakaba 1.56 $self->{insertion_mode} = IN_TABLE_BODY_IM;
4015 wakaba 1.54 ## reprocess in the "in table body" insertion mode...
4016     }
4017    
4018 wakaba 1.206 if (($self->{insertion_mode} & IM_MASK) == IN_TABLE_BODY_IM) {
4019 wakaba 1.54 ## have an element in table scope
4020     my $i;
4021     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4022     my $node = $self->{open_elements}->[$_];
4023 wakaba 1.201 if ($node->[1] == TABLE_ROW_GROUP_EL) {
4024 wakaba 1.79
4025 wakaba 1.54 $i = $_;
4026     last INSCOPE;
4027 wakaba 1.121 } elsif ($node->[1] & TABLE_SCOPING_EL) {
4028 wakaba 1.79
4029 wakaba 1.54 last INSCOPE;
4030     }
4031     } # INSCOPE
4032     unless (defined $i) {
4033 wakaba 1.79
4034 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
4035     text => $token->{tag_name}, token => $token);
4036 wakaba 1.54 ## Ignore the token
4037 wakaba 1.122
4038 wakaba 1.54 $token = $self->_get_next_token;
4039 wakaba 1.123 next B;
4040 wakaba 1.54 }
4041    
4042     ## Clear back to table body context
4043 wakaba 1.121 while (not ($self->{open_elements}->[-1]->[1]
4044     & TABLE_ROWS_SCOPING_EL)) {
4045 wakaba 1.79
4046 wakaba 1.54 pop @{$self->{open_elements}};
4047     }
4048    
4049     ## As if <{current node}>
4050     ## have an element in table scope
4051     ## true by definition
4052    
4053     ## Clear back to table body context
4054     ## nop by definition
4055    
4056     pop @{$self->{open_elements}};
4057 wakaba 1.56 $self->{insertion_mode} = IN_TABLE_IM;
4058 wakaba 1.54 ## reprocess in the "in table" insertion mode...
4059     }
4060 wakaba 1.52
4061 wakaba 1.94 ## NOTE: </table> in the "in table" insertion mode.
4062     ## When you edit the code fragment below, please ensure that
4063     ## the code for <table> in the "in table" insertion mode
4064     ## is synced with it.
4065    
4066 wakaba 1.54 ## have a table element in table scope
4067     my $i;
4068     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4069     my $node = $self->{open_elements}->[$_];
4070 wakaba 1.201 if ($node->[1] == TABLE_EL) {
4071 wakaba 1.79
4072 wakaba 1.54 $i = $_;
4073     last INSCOPE;
4074 wakaba 1.121 } elsif ($node->[1] & TABLE_SCOPING_EL) {
4075 wakaba 1.79
4076 wakaba 1.54 last INSCOPE;
4077     }
4078     } # INSCOPE
4079     unless (defined $i) {
4080 wakaba 1.79
4081 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
4082     text => $token->{tag_name}, token => $token);
4083 wakaba 1.54 ## Ignore the token
4084 wakaba 1.122
4085 wakaba 1.54 $token = $self->_get_next_token;
4086 wakaba 1.123 next B;
4087 wakaba 1.51 }
4088 wakaba 1.54
4089     splice @{$self->{open_elements}}, $i;
4090 wakaba 1.95 pop @{$open_tables};
4091 wakaba 1.54
4092     $self->_reset_insertion_mode;
4093 wakaba 1.1
4094     $token = $self->_get_next_token;
4095 wakaba 1.123 next B;
4096 wakaba 1.54 } elsif ({
4097     tbody => 1, tfoot => 1, thead => 1,
4098     }->{$token->{tag_name}} and
4099 wakaba 1.58 $self->{insertion_mode} & ROW_IMS) {
4100 wakaba 1.206 if (($self->{insertion_mode} & IM_MASK) == IN_ROW_IM) {
4101 wakaba 1.54 ## have an element in table scope
4102     my $i;
4103     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4104     my $node = $self->{open_elements}->[$_];
4105 wakaba 1.121 if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4106 wakaba 1.79
4107 wakaba 1.54 $i = $_;
4108     last INSCOPE;
4109 wakaba 1.121 } elsif ($node->[1] & TABLE_SCOPING_EL) {
4110 wakaba 1.79
4111 wakaba 1.54 last INSCOPE;
4112     }
4113     } # INSCOPE
4114     unless (defined $i) {
4115 wakaba 1.79
4116 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
4117     text => $token->{tag_name}, token => $token);
4118 wakaba 1.54 ## Ignore the token
4119 wakaba 1.122
4120 wakaba 1.54 $token = $self->_get_next_token;
4121 wakaba 1.123 next B;
4122 wakaba 1.54 }
4123    
4124     ## As if </tr>
4125     ## have an element in table scope
4126     my $i;
4127     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4128     my $node = $self->{open_elements}->[$_];
4129 wakaba 1.201 if ($node->[1] == TABLE_ROW_EL) {
4130 wakaba 1.79
4131 wakaba 1.54 $i = $_;
4132     last INSCOPE;
4133 wakaba 1.121 } elsif ($node->[1] & TABLE_SCOPING_EL) {
4134 wakaba 1.79
4135 wakaba 1.54 last INSCOPE;
4136     }
4137     } # INSCOPE
4138     unless (defined $i) {
4139 wakaba 1.79
4140 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
4141     text => 'tr', token => $token);
4142 wakaba 1.54 ## Ignore the token
4143 wakaba 1.122
4144 wakaba 1.54 $token = $self->_get_next_token;
4145 wakaba 1.123 next B;
4146 wakaba 1.54 }
4147    
4148     ## Clear back to table row context
4149 wakaba 1.121 while (not ($self->{open_elements}->[-1]->[1]
4150     & TABLE_ROW_SCOPING_EL)) {
4151 wakaba 1.79
4152 wakaba 1.83 ## ISSUE: Can this case be reached?
4153 wakaba 1.54 pop @{$self->{open_elements}};
4154     }
4155    
4156     pop @{$self->{open_elements}}; # tr
4157 wakaba 1.56 $self->{insertion_mode} = IN_TABLE_BODY_IM;
4158 wakaba 1.54 ## reprocess in the "in table body" insertion mode...
4159 wakaba 1.34 }
4160    
4161 wakaba 1.54 ## have an element in table scope
4162     my $i;
4163     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4164     my $node = $self->{open_elements}->[$_];
4165 wakaba 1.121 if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4166 wakaba 1.79
4167 wakaba 1.54 $i = $_;
4168     last INSCOPE;
4169 wakaba 1.121 } elsif ($node->[1] & TABLE_SCOPING_EL) {
4170 wakaba 1.79
4171 wakaba 1.54 last INSCOPE;
4172 wakaba 1.34 }
4173 wakaba 1.54 } # INSCOPE
4174     unless (defined $i) {
4175 wakaba 1.79
4176 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
4177     text => $token->{tag_name}, token => $token);
4178 wakaba 1.54 ## Ignore the token
4179 wakaba 1.122
4180 wakaba 1.54 $token = $self->_get_next_token;
4181 wakaba 1.123 next B;
4182 wakaba 1.34 }
4183    
4184 wakaba 1.54 ## Clear back to table body context
4185 wakaba 1.121 while (not ($self->{open_elements}->[-1]->[1]
4186     & TABLE_ROWS_SCOPING_EL)) {
4187 wakaba 1.79
4188 wakaba 1.83 ## ISSUE: Can this case be reached?
4189 wakaba 1.51 pop @{$self->{open_elements}};
4190 wakaba 1.25 }
4191 wakaba 1.51
4192 wakaba 1.54 pop @{$self->{open_elements}};
4193 wakaba 1.56 $self->{insertion_mode} = IN_TABLE_IM;
4194 wakaba 1.122
4195 wakaba 1.54 $token = $self->_get_next_token;
4196 wakaba 1.123 next B;
4197 wakaba 1.54 } elsif ({
4198     body => 1, caption => 1, col => 1, colgroup => 1,
4199     html => 1, td => 1, th => 1,
4200 wakaba 1.56 tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4201     tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
4202 wakaba 1.54 }->{$token->{tag_name}}) {
4203 wakaba 1.122
4204 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
4205     text => $token->{tag_name}, token => $token);
4206 wakaba 1.122 ## Ignore the token
4207    
4208     $token = $self->_get_next_token;
4209 wakaba 1.123 next B;
4210 wakaba 1.60 } else {
4211 wakaba 1.79
4212 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'in table:/',
4213     text => $token->{tag_name}, token => $token);
4214 wakaba 1.54
4215 wakaba 1.60 $insert = $insert_to_foster;
4216     #
4217     }
4218 wakaba 1.103 } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4219 wakaba 1.201 unless ($self->{open_elements}->[-1]->[1] == HTML_EL and
4220 wakaba 1.103 @{$self->{open_elements}} == 1) { # redundant, maybe
4221 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'in body:#eof', token => $token);
4222 wakaba 1.103
4223 wakaba 1.104 #
4224 wakaba 1.103 } else {
4225    
4226 wakaba 1.104 #
4227 wakaba 1.103 }
4228    
4229     ## Stop parsing
4230     last B;
4231 wakaba 1.60 } else {
4232     die "$0: $token->{type}: Unknown token type";
4233     }
4234 wakaba 1.206 } elsif (($self->{insertion_mode} & IM_MASK) == IN_COLUMN_GROUP_IM) {
4235 wakaba 1.57 if ($token->{type} == CHARACTER_TOKEN) {
4236 wakaba 1.184 if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
4237 wakaba 1.54 $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4238     unless (length $token->{data}) {
4239 wakaba 1.79
4240 wakaba 1.54 $token = $self->_get_next_token;
4241 wakaba 1.123 next B;
4242 wakaba 1.25 }
4243 wakaba 1.54 }
4244    
4245 wakaba 1.79
4246 wakaba 1.54 #
4247 wakaba 1.57 } elsif ($token->{type} == START_TAG_TOKEN) {
4248 wakaba 1.54 if ($token->{tag_name} eq 'col') {
4249    
4250 wakaba 1.79
4251 wakaba 1.25 {
4252     my $el;
4253    
4254 wakaba 1.1 $el = $self->{document}->create_element_ns
4255 wakaba 1.128 ($HTML_NS, [undef, $token->{tag_name}]);
4256 wakaba 1.1
4257 wakaba 1.25 for my $attr_name (keys %{ $token->{attributes}}) {
4258 wakaba 1.117 my $attr_t = $token->{attributes}->{$attr_name};
4259 wakaba 1.128 my $attr = $self->{document}->create_attribute_ns (undef, [undef, $attr_name]);
4260 wakaba 1.117 $attr->value ($attr_t->{value});
4261     $attr->set_user_data (manakai_source_line => $attr_t->{line});
4262     $attr->set_user_data (manakai_source_column => $attr_t->{column});
4263     $el->set_attribute_node_ns ($attr);
4264 wakaba 1.1 }
4265    
4266 wakaba 1.114 $el->set_user_data (manakai_source_line => $token->{line})
4267     if defined $token->{line};
4268     $el->set_user_data (manakai_source_column => $token->{column})
4269     if defined $token->{column};
4270    
4271 wakaba 1.25 $self->{open_elements}->[-1]->[0]->append_child ($el);
4272 wakaba 1.121 push @{$self->{open_elements}}, [$el, $el_category->{$token->{tag_name}} || 0];
4273 wakaba 1.25 }
4274    
4275 wakaba 1.54 pop @{$self->{open_elements}};
4276 wakaba 1.122 delete $self->{self_closing};
4277 wakaba 1.54 $token = $self->_get_next_token;
4278 wakaba 1.123 next B;
4279 wakaba 1.54 } else {
4280 wakaba 1.79
4281 wakaba 1.54 #
4282     }
4283 wakaba 1.57 } elsif ($token->{type} == END_TAG_TOKEN) {
4284 wakaba 1.54 if ($token->{tag_name} eq 'colgroup') {
4285 wakaba 1.201 if ($self->{open_elements}->[-1]->[1] == HTML_EL) {
4286 wakaba 1.79
4287 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
4288     text => 'colgroup', token => $token);
4289 wakaba 1.25 ## Ignore the token
4290 wakaba 1.42 $token = $self->_get_next_token;
4291 wakaba 1.123 next B;
4292 wakaba 1.24 } else {
4293 wakaba 1.79
4294 wakaba 1.54 pop @{$self->{open_elements}}; # colgroup
4295 wakaba 1.56 $self->{insertion_mode} = IN_TABLE_IM;
4296 wakaba 1.54 $token = $self->_get_next_token;
4297 wakaba 1.123 next B;
4298 wakaba 1.25 }
4299 wakaba 1.54 } elsif ($token->{tag_name} eq 'col') {
4300 wakaba 1.79
4301 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
4302     text => 'col', token => $token);
4303 wakaba 1.54 ## Ignore the token
4304     $token = $self->_get_next_token;
4305 wakaba 1.123 next B;
4306 wakaba 1.54 } else {
4307 wakaba 1.79
4308 wakaba 1.54 #
4309     }
4310 wakaba 1.103 } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4311 wakaba 1.201 if ($self->{open_elements}->[-1]->[1] == HTML_EL and
4312 wakaba 1.103 @{$self->{open_elements}} == 1) { # redundant, maybe
4313    
4314     ## Stop parsing.
4315     last B;
4316     } else {
4317     ## NOTE: As if </colgroup>.
4318    
4319     pop @{$self->{open_elements}}; # colgroup
4320     $self->{insertion_mode} = IN_TABLE_IM;
4321     ## Reprocess.
4322 wakaba 1.123 next B;
4323 wakaba 1.103 }
4324     } else {
4325     die "$0: $token->{type}: Unknown token type";
4326     }
4327 wakaba 1.51
4328 wakaba 1.54 ## As if </colgroup>
4329 wakaba 1.201 if ($self->{open_elements}->[-1]->[1] == HTML_EL) {
4330 wakaba 1.79
4331 wakaba 1.103 ## TODO: Wrong error type?
4332 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
4333     text => 'colgroup', token => $token);
4334 wakaba 1.54 ## Ignore the token
4335 wakaba 1.122
4336 wakaba 1.54 $token = $self->_get_next_token;
4337 wakaba 1.123 next B;
4338 wakaba 1.54 } else {
4339 wakaba 1.79
4340 wakaba 1.54 pop @{$self->{open_elements}}; # colgroup
4341 wakaba 1.56 $self->{insertion_mode} = IN_TABLE_IM;
4342 wakaba 1.122
4343 wakaba 1.54 ## reprocess
4344 wakaba 1.123 next B;
4345 wakaba 1.54 }
4346 wakaba 1.101 } elsif ($self->{insertion_mode} & SELECT_IMS) {
4347 wakaba 1.60 if ($token->{type} == CHARACTER_TOKEN) {
4348 wakaba 1.79
4349 wakaba 1.60 $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4350     $token = $self->_get_next_token;
4351 wakaba 1.123 next B;
4352 wakaba 1.60 } elsif ($token->{type} == START_TAG_TOKEN) {
4353 wakaba 1.121 if ($token->{tag_name} eq 'option') {
4354 wakaba 1.201 if ($self->{open_elements}->[-1]->[1] == OPTION_EL) {
4355 wakaba 1.121
4356     ## As if </option>
4357     pop @{$self->{open_elements}};
4358     } else {
4359    
4360     }
4361 wakaba 1.51
4362 wakaba 1.121
4363 wakaba 1.1 {
4364     my $el;
4365    
4366     $el = $self->{document}->create_element_ns
4367 wakaba 1.128 ($HTML_NS, [undef, $token->{tag_name}]);
4368 wakaba 1.1
4369     for my $attr_name (keys %{ $token->{attributes}}) {
4370 wakaba 1.117 my $attr_t = $token->{attributes}->{$attr_name};
4371 wakaba 1.128 my $attr = $self->{document}->create_attribute_ns (undef, [undef, $attr_name]);
4372 wakaba 1.117 $attr->value ($attr_t->{value});
4373     $attr->set_user_data (manakai_source_line => $attr_t->{line});
4374     $attr->set_user_data (manakai_source_column => $attr_t->{column});
4375     $el->set_attribute_node_ns ($attr);
4376 wakaba 1.1 }
4377    
4378 wakaba 1.114 $el->set_user_data (manakai_source_line => $token->{line})
4379     if defined $token->{line};
4380     $el->set_user_data (manakai_source_column => $token->{column})
4381     if defined $token->{column};
4382    
4383 wakaba 1.3 $self->{open_elements}->[-1]->[0]->append_child ($el);
4384 wakaba 1.121 push @{$self->{open_elements}}, [$el, $el_category->{$token->{tag_name}} || 0];
4385 wakaba 1.1 }
4386    
4387 wakaba 1.122
4388 wakaba 1.121 $token = $self->_get_next_token;
4389 wakaba 1.123 next B;
4390 wakaba 1.121 } elsif ($token->{tag_name} eq 'optgroup') {
4391 wakaba 1.201 if ($self->{open_elements}->[-1]->[1] == OPTION_EL) {
4392 wakaba 1.121
4393     ## As if </option>
4394     pop @{$self->{open_elements}};
4395     } else {
4396    
4397     }
4398 wakaba 1.54
4399 wakaba 1.201 if ($self->{open_elements}->[-1]->[1] == OPTGROUP_EL) {
4400 wakaba 1.121
4401     ## As if </optgroup>
4402     pop @{$self->{open_elements}};
4403     } else {
4404    
4405     }
4406 wakaba 1.51
4407 wakaba 1.121
4408 wakaba 1.1 {
4409     my $el;
4410    
4411     $el = $self->{document}->create_element_ns
4412 wakaba 1.128 ($HTML_NS, [undef, $token->{tag_name}]);
4413 wakaba 1.1
4414 wakaba 1.54 for my $attr_name (keys %{ $token->{attributes}}) {
4415 wakaba 1.117 my $attr_t = $token->{attributes}->{$attr_name};
4416 wakaba 1.128 my $attr = $self->{document}->create_attribute_ns (undef, [undef, $attr_name]);
4417 wakaba 1.117 $attr->value ($attr_t->{value});
4418     $attr->set_user_data (manakai_source_line => $attr_t->{line});
4419     $attr->set_user_data (manakai_source_column => $attr_t->{column});
4420     $el->set_attribute_node_ns ($attr);
4421 wakaba 1.54 }
4422    
4423 wakaba 1.114 $el->set_user_data (manakai_source_line => $token->{line})
4424     if defined $token->{line};
4425     $el->set_user_data (manakai_source_column => $token->{column})
4426     if defined $token->{column};
4427    
4428 wakaba 1.3 $self->{open_elements}->[-1]->[0]->append_child ($el);
4429 wakaba 1.121 push @{$self->{open_elements}}, [$el, $el_category->{$token->{tag_name}} || 0];
4430 wakaba 1.1 }
4431    
4432 wakaba 1.122
4433 wakaba 1.121 $token = $self->_get_next_token;
4434 wakaba 1.123 next B;
4435 wakaba 1.143 } elsif ({
4436 wakaba 1.210 select => 1, input => 1, textarea => 1, keygen => 1,
4437 wakaba 1.143 }->{$token->{tag_name}} or
4438 wakaba 1.206 (($self->{insertion_mode} & IM_MASK)
4439     == IN_SELECT_IN_TABLE_IM and
4440 wakaba 1.101 {
4441     caption => 1, table => 1,
4442     tbody => 1, tfoot => 1, thead => 1,
4443     tr => 1, td => 1, th => 1,
4444     }->{$token->{tag_name}})) {
4445     ## TODO: The type below is not good - <select> is replaced by </select>
4446 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'not closed', text => 'select',
4447     token => $token);
4448 wakaba 1.101 ## NOTE: As if the token were </select> (<select> case) or
4449     ## as if there were </select> (otherwise).
4450 wakaba 1.121 ## have an element in table scope
4451     my $i;
4452     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4453     my $node = $self->{open_elements}->[$_];
4454 wakaba 1.201 if ($node->[1] == SELECT_EL) {
4455 wakaba 1.121
4456     $i = $_;
4457     last INSCOPE;
4458     } elsif ($node->[1] & TABLE_SCOPING_EL) {
4459 wakaba 1.54
4460 wakaba 1.121 last INSCOPE;
4461     }
4462     } # INSCOPE
4463     unless (defined $i) {
4464    
4465 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
4466     text => 'select', token => $token);
4467 wakaba 1.121 ## Ignore the token
4468 wakaba 1.122
4469 wakaba 1.121 $token = $self->_get_next_token;
4470 wakaba 1.123 next B;
4471 wakaba 1.121 }
4472 wakaba 1.79
4473 wakaba 1.121
4474     splice @{$self->{open_elements}}, $i;
4475 wakaba 1.54
4476 wakaba 1.121 $self->_reset_insertion_mode;
4477 wakaba 1.54
4478 wakaba 1.101 if ($token->{tag_name} eq 'select') {
4479    
4480     $token = $self->_get_next_token;
4481 wakaba 1.123 next B;
4482 wakaba 1.101 } else {
4483    
4484 wakaba 1.122
4485 wakaba 1.101 ## Reprocess the token.
4486 wakaba 1.123 next B;
4487 wakaba 1.101 }
4488 wakaba 1.60 } else {
4489 wakaba 1.79
4490 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'in select',
4491     text => $token->{tag_name}, token => $token);
4492 wakaba 1.60 ## Ignore the token
4493 wakaba 1.122
4494 wakaba 1.60 $token = $self->_get_next_token;
4495 wakaba 1.123 next B;
4496 wakaba 1.60 }
4497     } elsif ($token->{type} == END_TAG_TOKEN) {
4498 wakaba 1.121 if ($token->{tag_name} eq 'optgroup') {
4499 wakaba 1.201 if ($self->{open_elements}->[-1]->[1] == OPTION_EL and
4500     $self->{open_elements}->[-2]->[1] == OPTGROUP_EL) {
4501 wakaba 1.121
4502     ## As if </option>
4503     splice @{$self->{open_elements}}, -2;
4504 wakaba 1.201 } elsif ($self->{open_elements}->[-1]->[1] == OPTGROUP_EL) {
4505 wakaba 1.121
4506     pop @{$self->{open_elements}};
4507     } else {
4508    
4509 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
4510     text => $token->{tag_name}, token => $token);
4511 wakaba 1.121 ## Ignore the token
4512     }
4513 wakaba 1.122
4514 wakaba 1.121 $token = $self->_get_next_token;
4515 wakaba 1.123 next B;
4516 wakaba 1.121 } elsif ($token->{tag_name} eq 'option') {
4517 wakaba 1.201 if ($self->{open_elements}->[-1]->[1] == OPTION_EL) {
4518 wakaba 1.121
4519     pop @{$self->{open_elements}};
4520     } else {
4521    
4522 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
4523     text => $token->{tag_name}, token => $token);
4524 wakaba 1.121 ## Ignore the token
4525     }
4526 wakaba 1.122
4527 wakaba 1.121 $token = $self->_get_next_token;
4528 wakaba 1.123 next B;
4529 wakaba 1.121 } elsif ($token->{tag_name} eq 'select') {
4530     ## have an element in table scope
4531     my $i;
4532     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4533     my $node = $self->{open_elements}->[$_];
4534 wakaba 1.201 if ($node->[1] == SELECT_EL) {
4535 wakaba 1.121
4536     $i = $_;
4537     last INSCOPE;
4538     } elsif ($node->[1] & TABLE_SCOPING_EL) {
4539 wakaba 1.54
4540 wakaba 1.121 last INSCOPE;
4541     }
4542     } # INSCOPE
4543     unless (defined $i) {
4544    
4545 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
4546     text => $token->{tag_name}, token => $token);
4547 wakaba 1.121 ## Ignore the token
4548 wakaba 1.122
4549 wakaba 1.121 $token = $self->_get_next_token;
4550 wakaba 1.123 next B;
4551 wakaba 1.121 }
4552 wakaba 1.79
4553 wakaba 1.121
4554     splice @{$self->{open_elements}}, $i;
4555 wakaba 1.54
4556 wakaba 1.121 $self->_reset_insertion_mode;
4557 wakaba 1.54
4558 wakaba 1.122
4559 wakaba 1.121 $token = $self->_get_next_token;
4560 wakaba 1.123 next B;
4561 wakaba 1.206 } elsif (($self->{insertion_mode} & IM_MASK)
4562     == IN_SELECT_IN_TABLE_IM and
4563 wakaba 1.101 {
4564     caption => 1, table => 1, tbody => 1,
4565     tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
4566     }->{$token->{tag_name}}) {
4567 wakaba 1.83 ## TODO: The following is wrong?
4568 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
4569     text => $token->{tag_name}, token => $token);
4570 wakaba 1.121
4571     ## have an element in table scope
4572     my $i;
4573     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4574     my $node = $self->{open_elements}->[$_];
4575     if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4576    
4577     $i = $_;
4578     last INSCOPE;
4579     } elsif ($node->[1] & TABLE_SCOPING_EL) {
4580    
4581     last INSCOPE;
4582     }
4583     } # INSCOPE
4584     unless (defined $i) {
4585    
4586     ## Ignore the token
4587 wakaba 1.122
4588 wakaba 1.121 $token = $self->_get_next_token;
4589 wakaba 1.123 next B;
4590 wakaba 1.121 }
4591 wakaba 1.54
4592 wakaba 1.121 ## As if </select>
4593     ## have an element in table scope
4594     undef $i;
4595     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4596     my $node = $self->{open_elements}->[$_];
4597 wakaba 1.201 if ($node->[1] == SELECT_EL) {
4598 wakaba 1.54
4599 wakaba 1.121 $i = $_;
4600     last INSCOPE;
4601     } elsif ($node->[1] & TABLE_SCOPING_EL) {
4602 wakaba 1.83 ## ISSUE: Can this state be reached?
4603 wakaba 1.121
4604     last INSCOPE;
4605     }
4606     } # INSCOPE
4607     unless (defined $i) {
4608    
4609 wakaba 1.83 ## TODO: The following error type is correct?
4610 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
4611     text => 'select', token => $token);
4612 wakaba 1.121 ## Ignore the </select> token
4613 wakaba 1.122
4614 wakaba 1.121 $token = $self->_get_next_token; ## TODO: ok?
4615 wakaba 1.123 next B;
4616 wakaba 1.121 }
4617 wakaba 1.54
4618 wakaba 1.121
4619     splice @{$self->{open_elements}}, $i;
4620 wakaba 1.54
4621 wakaba 1.121 $self->_reset_insertion_mode;
4622 wakaba 1.54
4623 wakaba 1.122
4624 wakaba 1.121 ## reprocess
4625 wakaba 1.123 next B;
4626 wakaba 1.60 } else {
4627 wakaba 1.79
4628 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'in select:/',
4629     text => $token->{tag_name}, token => $token);
4630 wakaba 1.54 ## Ignore the token
4631 wakaba 1.122
4632 wakaba 1.54 $token = $self->_get_next_token;
4633 wakaba 1.123 next B;
4634 wakaba 1.60 }
4635 wakaba 1.103 } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4636 wakaba 1.201 unless ($self->{open_elements}->[-1]->[1] == HTML_EL and
4637 wakaba 1.103 @{$self->{open_elements}} == 1) { # redundant, maybe
4638    
4639 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'in body:#eof', token => $token);
4640 wakaba 1.103 } else {
4641    
4642     }
4643    
4644     ## Stop parsing.
4645     last B;
4646 wakaba 1.60 } else {
4647     die "$0: $token->{type}: Unknown token type";
4648     }
4649 wakaba 1.58 } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
4650 wakaba 1.57 if ($token->{type} == CHARACTER_TOKEN) {
4651 wakaba 1.184 if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
4652 wakaba 1.54 my $data = $1;
4653     ## As if in body
4654     $reconstruct_active_formatting_elements->($insert_to_current);
4655    
4656     $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4657    
4658     unless (length $token->{data}) {
4659 wakaba 1.79
4660 wakaba 1.54 $token = $self->_get_next_token;
4661 wakaba 1.123 next B;
4662 wakaba 1.54 }
4663     }
4664    
4665 wakaba 1.56 if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4666 wakaba 1.79
4667 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'after html:#text', token => $token);
4668 wakaba 1.184 #
4669 wakaba 1.79 } else {
4670    
4671 wakaba 1.184 ## "after body" insertion mode
4672     $self->{parse_error}->(level => $self->{level}->{must}, type => 'after body:#text', token => $token);
4673     #
4674 wakaba 1.54 }
4675    
4676 wakaba 1.56 $self->{insertion_mode} = IN_BODY_IM;
4677 wakaba 1.54 ## reprocess
4678 wakaba 1.123 next B;
4679 wakaba 1.57 } elsif ($token->{type} == START_TAG_TOKEN) {
4680 wakaba 1.56 if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4681 wakaba 1.79
4682 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'after html',
4683     text => $token->{tag_name}, token => $token);
4684 wakaba 1.184 #
4685 wakaba 1.79 } else {
4686    
4687 wakaba 1.184 ## "after body" insertion mode
4688     $self->{parse_error}->(level => $self->{level}->{must}, type => 'after body',
4689     text => $token->{tag_name}, token => $token);
4690     #
4691 wakaba 1.54 }
4692    
4693 wakaba 1.56 $self->{insertion_mode} = IN_BODY_IM;
4694 wakaba 1.122
4695 wakaba 1.54 ## reprocess
4696 wakaba 1.123 next B;
4697 wakaba 1.57 } elsif ($token->{type} == END_TAG_TOKEN) {
4698 wakaba 1.56 if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4699 wakaba 1.79
4700 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'after html:/',
4701     text => $token->{tag_name}, token => $token);
4702 wakaba 1.54
4703 wakaba 1.184 $self->{insertion_mode} = IN_BODY_IM;
4704     ## Reprocess.
4705     next B;
4706 wakaba 1.79 } else {
4707    
4708 wakaba 1.54 }
4709    
4710     ## "after body" insertion mode
4711     if ($token->{tag_name} eq 'html') {
4712     if (defined $self->{inner_html_node}) {
4713 wakaba 1.79
4714 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
4715     text => 'html', token => $token);
4716 wakaba 1.54 ## Ignore the token
4717     $token = $self->_get_next_token;
4718 wakaba 1.123 next B;
4719 wakaba 1.54 } else {
4720 wakaba 1.79
4721 wakaba 1.56 $self->{insertion_mode} = AFTER_HTML_BODY_IM;
4722 wakaba 1.54 $token = $self->_get_next_token;
4723 wakaba 1.123 next B;
4724 wakaba 1.54 }
4725     } else {
4726 wakaba 1.79
4727 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'after body:/',
4728     text => $token->{tag_name}, token => $token);
4729 wakaba 1.54
4730 wakaba 1.56 $self->{insertion_mode} = IN_BODY_IM;
4731 wakaba 1.54 ## reprocess
4732 wakaba 1.123 next B;
4733 wakaba 1.54 }
4734 wakaba 1.103 } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4735    
4736     ## Stop parsing
4737     last B;
4738 wakaba 1.54 } else {
4739     die "$0: $token->{type}: Unknown token type";
4740     }
4741 wakaba 1.58 } elsif ($self->{insertion_mode} & FRAME_IMS) {
4742 wakaba 1.57 if ($token->{type} == CHARACTER_TOKEN) {
4743 wakaba 1.184 if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
4744 wakaba 1.54 $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4745    
4746     unless (length $token->{data}) {
4747 wakaba 1.79
4748 wakaba 1.54 $token = $self->_get_next_token;
4749 wakaba 1.123 next B;
4750 wakaba 1.54 }
4751     }
4752    
4753 wakaba 1.184 if ($token->{data} =~ s/^[^\x09\x0A\x0C\x20]+//) {
4754 wakaba 1.56 if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4755 wakaba 1.79
4756 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'in frameset:#text', token => $token);
4757 wakaba 1.56 } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4758 wakaba 1.79
4759 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'after frameset:#text', token => $token);
4760 wakaba 1.154 } else { # "after after frameset"
4761 wakaba 1.79
4762 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'after html:#text', token => $token);
4763 wakaba 1.54 }
4764    
4765     ## Ignore the token.
4766     if (length $token->{data}) {
4767 wakaba 1.79
4768 wakaba 1.54 ## reprocess the rest of characters
4769     } else {
4770 wakaba 1.79
4771 wakaba 1.54 $token = $self->_get_next_token;
4772     }
4773 wakaba 1.123 next B;
4774 wakaba 1.54 }
4775    
4776     die qq[$0: Character "$token->{data}"];
4777 wakaba 1.57 } elsif ($token->{type} == START_TAG_TOKEN) {
4778 wakaba 1.54 if ($token->{tag_name} eq 'frameset' and
4779 wakaba 1.56 $self->{insertion_mode} == IN_FRAMESET_IM) {
4780 wakaba 1.54
4781 wakaba 1.79
4782 wakaba 1.54 {
4783     my $el;
4784    
4785     $el = $self->{document}->create_element_ns
4786 wakaba 1.128 ($HTML_NS, [undef, $token->{tag_name}]);
4787 wakaba 1.54
4788     for my $attr_name (keys %{ $token->{attributes}}) {
4789 wakaba 1.117 my $attr_t = $token->{attributes}->{$attr_name};
4790 wakaba 1.128 my $attr = $self->{document}->create_attribute_ns (undef, [undef, $attr_name]);
4791 wakaba 1.117 $attr->value ($attr_t->{value});
4792     $attr->set_user_data (manakai_source_line => $attr_t->{line});
4793     $attr->set_user_data (manakai_source_column => $attr_t->{column});
4794     $el->set_attribute_node_ns ($attr);
4795 wakaba 1.54 }
4796    
4797 wakaba 1.114 $el->set_user_data (manakai_source_line => $token->{line})
4798     if defined $token->{line};
4799     $el->set_user_data (manakai_source_column => $token->{column})
4800     if defined $token->{column};
4801    
4802 wakaba 1.54 $self->{open_elements}->[-1]->[0]->append_child ($el);
4803 wakaba 1.121 push @{$self->{open_elements}}, [$el, $el_category->{$token->{tag_name}} || 0];
4804 wakaba 1.54 }
4805    
4806 wakaba 1.122
4807 wakaba 1.54 $token = $self->_get_next_token;
4808 wakaba 1.123 next B;
4809 wakaba 1.54 } elsif ($token->{tag_name} eq 'frame' and
4810 wakaba 1.56 $self->{insertion_mode} == IN_FRAMESET_IM) {
4811 wakaba 1.54
4812 wakaba 1.79
4813 wakaba 1.54 {
4814     my $el;
4815    
4816     $el = $self->{document}->create_element_ns
4817 wakaba 1.128 ($HTML_NS, [undef, $token->{tag_name}]);
4818 wakaba 1.54
4819     for my $attr_name (keys %{ $token->{attributes}}) {
4820 wakaba 1.117 my $attr_t = $token->{attributes}->{$attr_name};
4821 wakaba 1.128 my $attr = $self->{document}->create_attribute_ns (undef, [undef, $attr_name]);
4822 wakaba 1.117 $attr->value ($attr_t->{value});
4823     $attr->set_user_data (manakai_source_line => $attr_t->{line});
4824     $attr->set_user_data (manakai_source_column => $attr_t->{column});
4825     $el->set_attribute_node_ns ($attr);
4826 wakaba 1.54 }
4827    
4828 wakaba 1.114 $el->set_user_data (manakai_source_line => $token->{line})
4829     if defined $token->{line};
4830     $el->set_user_data (manakai_source_column => $token->{column})
4831     if defined $token->{column};
4832    
4833 wakaba 1.54 $self->{open_elements}->[-1]->[0]->append_child ($el);
4834 wakaba 1.121 push @{$self->{open_elements}}, [$el, $el_category->{$token->{tag_name}} || 0];
4835 wakaba 1.54 }
4836    
4837     pop @{$self->{open_elements}};
4838 wakaba 1.122 delete $self->{self_closing};
4839 wakaba 1.54 $token = $self->_get_next_token;
4840 wakaba 1.123 next B;
4841 wakaba 1.54 } elsif ($token->{tag_name} eq 'noframes') {
4842 wakaba 1.79
4843 wakaba 1.145 ## NOTE: As if in head.
4844 wakaba 1.96 $parse_rcdata->(CDATA_CONTENT_MODEL);
4845 wakaba 1.123 next B;
4846 wakaba 1.155
4847     ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
4848     ## has no parse error.
4849 wakaba 1.54 } else {
4850 wakaba 1.56 if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4851 wakaba 1.79
4852 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'in frameset',
4853     text => $token->{tag_name}, token => $token);
4854 wakaba 1.154 } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4855 wakaba 1.79
4856 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'after frameset',
4857     text => $token->{tag_name}, token => $token);
4858 wakaba 1.154 } else { # "after after frameset"
4859    
4860     $self->{parse_error}->(level => $self->{level}->{must}, type => 'after after frameset',
4861     text => $token->{tag_name}, token => $token);
4862 wakaba 1.54 }
4863     ## Ignore the token
4864 wakaba 1.122
4865 wakaba 1.54 $token = $self->_get_next_token;
4866 wakaba 1.123 next B;
4867 wakaba 1.54 }
4868 wakaba 1.57 } elsif ($token->{type} == END_TAG_TOKEN) {
4869 wakaba 1.54 if ($token->{tag_name} eq 'frameset' and
4870 wakaba 1.56 $self->{insertion_mode} == IN_FRAMESET_IM) {
4871 wakaba 1.201 if ($self->{open_elements}->[-1]->[1] == HTML_EL and
4872 wakaba 1.54 @{$self->{open_elements}} == 1) {
4873 wakaba 1.79
4874 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
4875     text => $token->{tag_name}, token => $token);
4876 wakaba 1.54 ## Ignore the token
4877     $token = $self->_get_next_token;
4878     } else {
4879 wakaba 1.79
4880 wakaba 1.54 pop @{$self->{open_elements}};
4881     $token = $self->_get_next_token;
4882     }
4883 wakaba 1.43
4884 wakaba 1.54 if (not defined $self->{inner_html_node} and
4885 wakaba 1.201 not ($self->{open_elements}->[-1]->[1] == FRAMESET_EL)) {
4886 wakaba 1.79
4887 wakaba 1.56 $self->{insertion_mode} = AFTER_FRAMESET_IM;
4888 wakaba 1.79 } else {
4889    
4890 wakaba 1.54 }
4891 wakaba 1.123 next B;
4892 wakaba 1.54 } elsif ($token->{tag_name} eq 'html' and
4893 wakaba 1.56 $self->{insertion_mode} == AFTER_FRAMESET_IM) {
4894 wakaba 1.79
4895 wakaba 1.56 $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
4896 wakaba 1.54 $token = $self->_get_next_token;
4897 wakaba 1.123 next B;
4898 wakaba 1.54 } else {
4899 wakaba 1.56 if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4900 wakaba 1.79
4901 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'in frameset:/',
4902     text => $token->{tag_name}, token => $token);
4903 wakaba 1.154 } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4904 wakaba 1.79
4905 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'after frameset:/',
4906     text => $token->{tag_name}, token => $token);
4907 wakaba 1.154 } else { # "after after html"
4908    
4909     $self->{parse_error}->(level => $self->{level}->{must}, type => 'after after frameset:/',
4910     text => $token->{tag_name}, token => $token);
4911 wakaba 1.54 }
4912     ## Ignore the token
4913     $token = $self->_get_next_token;
4914 wakaba 1.123 next B;
4915 wakaba 1.54 }
4916 wakaba 1.103 } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4917 wakaba 1.201 unless ($self->{open_elements}->[-1]->[1] == HTML_EL and
4918 wakaba 1.103 @{$self->{open_elements}} == 1) { # redundant, maybe
4919    
4920 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'in body:#eof', token => $token);
4921 wakaba 1.103 } else {
4922    
4923     }
4924    
4925     ## Stop parsing
4926     last B;
4927 wakaba 1.54 } else {
4928     die "$0: $token->{type}: Unknown token type";
4929     }
4930     } else {
4931     die "$0: $self->{insertion_mode}: Unknown insertion mode";
4932     }
4933 wakaba 1.43
4934 wakaba 1.54 ## "in body" insertion mode
4935 wakaba 1.57 if ($token->{type} == START_TAG_TOKEN) {
4936 wakaba 1.54 if ($token->{tag_name} eq 'script') {
4937 wakaba 1.79
4938 wakaba 1.54 ## NOTE: This is an "as if in head" code clone
4939 wakaba 1.100 $script_start_tag->();
4940 wakaba 1.123 next B;
4941 wakaba 1.54 } elsif ($token->{tag_name} eq 'style') {
4942 wakaba 1.79
4943 wakaba 1.54 ## NOTE: This is an "as if in head" code clone
4944 wakaba 1.96 $parse_rcdata->(CDATA_CONTENT_MODEL);
4945 wakaba 1.123 next B;
4946 wakaba 1.54 } elsif ({
4947 wakaba 1.189 base => 1, command => 1, eventsource => 1, link => 1,
4948 wakaba 1.54 }->{$token->{tag_name}}) {
4949 wakaba 1.79
4950 wakaba 1.54 ## NOTE: This is an "as if in head" code clone, only "-t" differs
4951    
4952     {
4953     my $el;
4954    
4955     $el = $self->{document}->create_element_ns
4956 wakaba 1.128 ($HTML_NS, [undef, $token->{tag_name}]);
4957 wakaba 1.54
4958     for my $attr_name (keys %{ $token->{attributes}}) {
4959 wakaba 1.117 my $attr_t = $token->{attributes}->{$attr_name};
4960 wakaba 1.128 my $attr = $self->{document}->create_attribute_ns (undef, [undef, $attr_name]);
4961 wakaba 1.117 $attr->value ($attr_t->{value});
4962     $attr->set_user_data (manakai_source_line => $attr_t->{line});
4963     $attr->set_user_data (manakai_source_column => $attr_t->{column});
4964     $el->set_attribute_node_ns ($attr);
4965 wakaba 1.54 }
4966    
4967 wakaba 1.114 $el->set_user_data (manakai_source_line => $token->{line})
4968     if defined $token->{line};
4969     $el->set_user_data (manakai_source_column => $token->{column})
4970     if defined $token->{column};
4971    
4972 wakaba 1.54 $insert->($el);
4973 wakaba 1.121 push @{$self->{open_elements}}, [$el, $el_category->{$token->{tag_name}} || 0];
4974 wakaba 1.54 }
4975    
4976 wakaba 1.189 pop @{$self->{open_elements}};
4977 wakaba 1.122 delete $self->{self_closing};
4978 wakaba 1.54 $token = $self->_get_next_token;
4979 wakaba 1.123 next B;
4980 wakaba 1.54 } elsif ($token->{tag_name} eq 'meta') {
4981     ## NOTE: This is an "as if in head" code clone, only "-t" differs
4982    
4983     {
4984     my $el;
4985    
4986     $el = $self->{document}->create_element_ns
4987 wakaba 1.128 ($HTML_NS, [undef, $token->{tag_name}]);
4988 wakaba 1.54
4989     for my $attr_name (keys %{ $token->{attributes}}) {
4990 wakaba 1.117 my $attr_t = $token->{attributes}->{$attr_name};
4991 wakaba 1.128 my $attr = $self->{document}->create_attribute_ns (undef, [undef, $attr_name]);
4992 wakaba 1.117 $attr->value ($attr_t->{value});
4993     $attr->set_user_data (manakai_source_line => $attr_t->{line});
4994     $attr->set_user_data (manakai_source_column => $attr_t->{column});
4995     $el->set_attribute_node_ns ($attr);
4996 wakaba 1.54 }
4997    
4998 wakaba 1.114 $el->set_user_data (manakai_source_line => $token->{line})
4999     if defined $token->{line};
5000     $el->set_user_data (manakai_source_column => $token->{column})
5001     if defined $token->{column};
5002    
5003 wakaba 1.54 $insert->($el);
5004 wakaba 1.121 push @{$self->{open_elements}}, [$el, $el_category->{$token->{tag_name}} || 0];
5005 wakaba 1.54 }
5006    
5007 wakaba 1.189 my $meta_el = pop @{$self->{open_elements}};
5008 wakaba 1.43
5009 wakaba 1.54 unless ($self->{confident}) {
5010 wakaba 1.131 if ($token->{attributes}->{charset}) {
5011 wakaba 1.79
5012 wakaba 1.131 ## NOTE: Whether the encoding is supported or not is handled
5013     ## in the {change_encoding} callback.
5014 wakaba 1.65 $self->{change_encoding}
5015 wakaba 1.112 ->($self, $token->{attributes}->{charset}->{value}, $token);
5016 wakaba 1.68
5017     $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5018     ->set_user_data (manakai_has_reference =>
5019     $token->{attributes}->{charset}
5020     ->{has_reference});
5021 wakaba 1.65 } elsif ($token->{attributes}->{content}) {
5022     if ($token->{attributes}->{content}->{value}
5023 wakaba 1.141 =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
5024 wakaba 1.185 [\x09\x0A\x0C\x0D\x20]*=
5025     [\x09\x0A\x0C\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
5026     ([^"'\x09\x0A\x0C\x0D\x20][^\x09\x0A\x0C\x0D\x20\x3B]*))
5027     /x) {
5028 wakaba 1.79
5029 wakaba 1.131 ## NOTE: Whether the encoding is supported or not is handled
5030     ## in the {change_encoding} callback.
5031 wakaba 1.65 $self->{change_encoding}
5032 wakaba 1.112 ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
5033 wakaba 1.69 $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5034     ->set_user_data (manakai_has_reference =>
5035     $token->{attributes}->{content}
5036     ->{has_reference});
5037 wakaba 1.65 }
5038 wakaba 1.54 }
5039 wakaba 1.68 } else {
5040     if ($token->{attributes}->{charset}) {
5041 wakaba 1.79
5042 wakaba 1.68 $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5043     ->set_user_data (manakai_has_reference =>
5044     $token->{attributes}->{charset}
5045     ->{has_reference});
5046     }
5047 wakaba 1.69 if ($token->{attributes}->{content}) {
5048 wakaba 1.79
5049 wakaba 1.69 $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5050     ->set_user_data (manakai_has_reference =>
5051     $token->{attributes}->{content}
5052     ->{has_reference});
5053     }
5054 wakaba 1.54 }
5055 wakaba 1.43
5056 wakaba 1.122 delete $self->{self_closing};
5057 wakaba 1.54 $token = $self->_get_next_token;
5058 wakaba 1.123 next B;
5059 wakaba 1.54 } elsif ($token->{tag_name} eq 'title') {
5060 wakaba 1.79
5061 wakaba 1.54 ## NOTE: This is an "as if in head" code clone
5062 wakaba 1.96 $parse_rcdata->(RCDATA_CONTENT_MODEL);
5063 wakaba 1.123 next B;
5064 wakaba 1.54 } elsif ($token->{tag_name} eq 'body') {
5065 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'in body', text => 'body', token => $token);
5066 wakaba 1.1
5067 wakaba 1.54 if (@{$self->{open_elements}} == 1 or
5068 wakaba 1.201 not ($self->{open_elements}->[1]->[1] == BODY_EL)) {
5069 wakaba 1.79
5070 wakaba 1.54 ## Ignore the token
5071     } else {
5072     my $body_el = $self->{open_elements}->[1]->[0];
5073     for my $attr_name (keys %{$token->{attributes}}) {
5074     unless ($body_el->has_attribute_ns (undef, $attr_name)) {
5075 wakaba 1.79
5076 wakaba 1.54 $body_el->set_attribute_ns
5077     (undef, [undef, $attr_name],
5078     $token->{attributes}->{$attr_name}->{value});
5079 wakaba 1.1 }
5080 wakaba 1.54 }
5081     }
5082 wakaba 1.122
5083 wakaba 1.54 $token = $self->_get_next_token;
5084 wakaba 1.123 next B;
5085 wakaba 1.54 } elsif ({
5086 wakaba 1.190 ## NOTE: Start tags for non-phrasing flow content elements
5087    
5088     ## NOTE: The normal one
5089     address => 1, article => 1, aside => 1, blockquote => 1,
5090     center => 1, datagrid => 1, details => 1, dialog => 1,
5091     dir => 1, div => 1, dl => 1, fieldset => 1, figure => 1,
5092     footer => 1, h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1,
5093     h6 => 1, header => 1, menu => 1, nav => 1, ol => 1, p => 1,
5094     section => 1, ul => 1,
5095     ## NOTE: As normal, but drops leading newline
5096 wakaba 1.97 pre => 1, listing => 1,
5097 wakaba 1.190 ## NOTE: As normal, but interacts with the form element pointer
5098 wakaba 1.107 form => 1,
5099 wakaba 1.190
5100 wakaba 1.107 table => 1,
5101     hr => 1,
5102 wakaba 1.54 }->{$token->{tag_name}}) {
5103 wakaba 1.107 if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
5104    
5105 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'in form:form', token => $token);
5106 wakaba 1.107 ## Ignore the token
5107 wakaba 1.122
5108 wakaba 1.107 $token = $self->_get_next_token;
5109 wakaba 1.123 next B;
5110 wakaba 1.107 }
5111    
5112 wakaba 1.211 if ($token->{tag_name} ne 'table' or # The Hixie Quirk
5113     $self->{document}->manakai_compat_mode ne 'quirks') {
5114     ## has a p element in scope
5115     INSCOPE: for (reverse @{$self->{open_elements}}) {
5116     if ($_->[1] == P_EL) {
5117    
5118    
5119 wakaba 1.122 $token->{self_closing} = $self->{self_closing};
5120     unshift @{$self->{token}}, $token;
5121     delete $self->{self_closing};
5122     # <form>
5123 wakaba 1.211 $token = {type => END_TAG_TOKEN, tag_name => 'p',
5124     line => $token->{line}, column => $token->{column}};
5125     next B;
5126     } elsif ($_->[1] & SCOPING_EL) {
5127    
5128     last INSCOPE;
5129     }
5130     } # INSCOPE
5131     }
5132 wakaba 1.54
5133    
5134 wakaba 1.48 {
5135     my $el;
5136    
5137     $el = $self->{document}->create_element_ns
5138 wakaba 1.128 ($HTML_NS, [undef, $token->{tag_name}]);
5139 wakaba 1.48
5140 wakaba 1.54 for my $attr_name (keys %{ $token->{attributes}}) {
5141 wakaba 1.117 my $attr_t = $token->{attributes}->{$attr_name};
5142 wakaba 1.128 my $attr = $self->{document}->create_attribute_ns (undef, [undef, $attr_name]);
5143 wakaba 1.117 $attr->value ($attr_t->{value});
5144     $attr->set_user_data (manakai_source_line => $attr_t->{line});
5145     $attr->set_user_data (manakai_source_column => $attr_t->{column});
5146     $el->set_attribute_node_ns ($attr);
5147 wakaba 1.54 }
5148    
5149 wakaba 1.114 $el->set_user_data (manakai_source_line => $token->{line})
5150     if defined $token->{line};
5151     $el->set_user_data (manakai_source_column => $token->{column})
5152     if defined $token->{column};
5153    
5154 wakaba 1.54 $insert->($el);
5155 wakaba 1.121 push @{$self->{open_elements}}, [$el, $el_category->{$token->{tag_name}} || 0];
5156 wakaba 1.48 }
5157    
5158 wakaba 1.97 if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
5159 wakaba 1.122
5160 wakaba 1.54 $token = $self->_get_next_token;
5161 wakaba 1.57 if ($token->{type} == CHARACTER_TOKEN) {
5162 wakaba 1.54 $token->{data} =~ s/^\x0A//;
5163     unless (length $token->{data}) {
5164 wakaba 1.79
5165 wakaba 1.54 $token = $self->_get_next_token;
5166 wakaba 1.79 } else {
5167    
5168 wakaba 1.54 }
5169 wakaba 1.79 } else {
5170    
5171 wakaba 1.54 }
5172 wakaba 1.107 } elsif ($token->{tag_name} eq 'form') {
5173    
5174     $self->{form_element} = $self->{open_elements}->[-1]->[0];
5175    
5176 wakaba 1.122
5177 wakaba 1.107 $token = $self->_get_next_token;
5178     } elsif ($token->{tag_name} eq 'table') {
5179    
5180     push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
5181 wakaba 1.79
5182 wakaba 1.107 $self->{insertion_mode} = IN_TABLE_IM;
5183    
5184 wakaba 1.122
5185 wakaba 1.54 $token = $self->_get_next_token;
5186 wakaba 1.107 } elsif ($token->{tag_name} eq 'hr') {
5187 wakaba 1.79
5188 wakaba 1.107 pop @{$self->{open_elements}};
5189    
5190 wakaba 1.122
5191 wakaba 1.54 $token = $self->_get_next_token;
5192     } else {
5193    
5194     $token = $self->_get_next_token;
5195     }
5196 wakaba 1.123 next B;
5197 wakaba 1.191 } elsif ($token->{tag_name} eq 'li') {
5198     ## NOTE: As normal, but imply </li> when there's another <li> ...
5199    
5200     ## NOTE: Special, Scope (<li><foo><li> == <li><foo><li/></foo></li>)
5201     ## Interpreted as <li><foo/></li><li/> (non-conforming)
5202     ## blockquote (O9.27), center (O), dd (Fx3, O, S3.1.2, IE7),
5203     ## dt (Fx, O, S, IE), dl (O), fieldset (O, S, IE), form (Fx, O, S),
5204     ## hn (O), pre (O), applet (O, S), button (O, S), marquee (Fx, O, S),
5205     ## object (Fx)
5206     ## Generate non-tree (non-conforming)
5207     ## basefont (IE7 (where basefont is non-void)), center (IE),
5208     ## form (IE), hn (IE)
5209     ## address, div, p (<li><foo><li> == <li><foo/></li><li/>)
5210     ## Interpreted as <li><foo><li/></foo></li> (non-conforming)
5211     ## div (Fx, S)
5212    
5213     my $non_optional;
5214     my $i = -1;
5215    
5216     ## 1.
5217     for my $node (reverse @{$self->{open_elements}}) {
5218 wakaba 1.201 if ($node->[1] == LI_EL) {
5219 wakaba 1.191 ## 2. (a) As if </li>
5220     {
5221     ## If no </li> - not applied
5222     #
5223    
5224     ## Otherwise
5225    
5226     ## 1. generate implied end tags, except for </li>
5227     #
5228    
5229     ## 2. If current node != "li", parse error
5230     if ($non_optional) {
5231     $self->{parse_error}->(level => $self->{level}->{must}, type => 'not closed',
5232     text => $non_optional->[0]->manakai_local_name,
5233     token => $token);
5234    
5235     } else {
5236    
5237     }
5238    
5239     ## 3. Pop
5240     splice @{$self->{open_elements}}, $i;
5241     }
5242    
5243     last; ## 2. (b) goto 5.
5244     } elsif (
5245     ## NOTE: not "formatting" and not "phrasing"
5246     ($node->[1] & SPECIAL_EL or
5247     $node->[1] & SCOPING_EL) and
5248     ## NOTE: "li", "dt", and "dd" are in |SPECIAL_EL|.
5249 wakaba 1.201 (not $node->[1] & ADDRESS_DIV_P_EL)
5250     ) {
5251 wakaba 1.191 ## 3.
5252    
5253     last; ## goto 5.
5254     } elsif ($node->[1] & END_TAG_OPTIONAL_EL) {
5255    
5256     #
5257     } else {
5258    
5259     $non_optional ||= $node;
5260     #
5261     }
5262     ## 4.
5263     ## goto 2.
5264     $i--;
5265     }
5266    
5267     ## 5. (a) has a |p| element in scope
5268 wakaba 1.54 INSCOPE: for (reverse @{$self->{open_elements}}) {
5269 wakaba 1.201 if ($_->[1] == P_EL) {
5270 wakaba 1.79
5271 wakaba 1.193
5272     ## NOTE: |<p><li>|, for example.
5273    
5274 wakaba 1.122
5275     $token->{self_closing} = $self->{self_closing};
5276     unshift @{$self->{token}}, $token;
5277     delete $self->{self_closing};
5278     # <x>
5279 wakaba 1.112 $token = {type => END_TAG_TOKEN, tag_name => 'p',
5280     line => $token->{line}, column => $token->{column}};
5281 wakaba 1.123 next B;
5282 wakaba 1.121 } elsif ($_->[1] & SCOPING_EL) {
5283 wakaba 1.79
5284 wakaba 1.54 last INSCOPE;
5285     }
5286     } # INSCOPE
5287 wakaba 1.188
5288 wakaba 1.191 ## 5. (b) insert
5289    
5290     {
5291     my $el;
5292    
5293     $el = $self->{document}->create_element_ns
5294     ($HTML_NS, [undef, $token->{tag_name}]);
5295    
5296     for my $attr_name (keys %{ $token->{attributes}}) {
5297     my $attr_t = $token->{attributes}->{$attr_name};
5298     my $attr = $self->{document}->create_attribute_ns (undef, [undef, $attr_name]);
5299     $attr->value ($attr_t->{value});
5300     $attr->set_user_data (manakai_source_line => $attr_t->{line});
5301     $attr->set_user_data (manakai_source_column => $attr_t->{column});
5302     $el->set_attribute_node_ns ($attr);
5303     }
5304    
5305     $el->set_user_data (manakai_source_line => $token->{line})
5306     if defined $token->{line};
5307     $el->set_user_data (manakai_source_column => $token->{column})
5308     if defined $token->{column};
5309    
5310     $insert->($el);
5311     push @{$self->{open_elements}}, [$el, $el_category->{$token->{tag_name}} || 0];
5312     }
5313    
5314    
5315     $token = $self->_get_next_token;
5316     next B;
5317     } elsif ($token->{tag_name} eq 'dt' or
5318     $token->{tag_name} eq 'dd') {
5319     ## NOTE: As normal, but imply </dt> or </dd> when ...
5320    
5321     my $non_optional;
5322 wakaba 1.54 my $i = -1;
5323 wakaba 1.191
5324     ## 1.
5325     for my $node (reverse @{$self->{open_elements}}) {
5326 wakaba 1.202 if ($node->[1] == DTDD_EL) {
5327 wakaba 1.191 ## 2. (a) As if </li>
5328     {
5329     ## If no </li> - not applied
5330     #
5331    
5332     ## Otherwise
5333    
5334     ## 1. generate implied end tags, except for </dt> or </dd>
5335     #
5336    
5337     ## 2. If current node != "dt"|"dd", parse error
5338     if ($non_optional) {
5339     $self->{parse_error}->(level => $self->{level}->{must}, type => 'not closed',
5340     text => $non_optional->[0]->manakai_local_name,
5341     token => $token);
5342    
5343     } else {
5344    
5345     }
5346    
5347     ## 3. Pop
5348     splice @{$self->{open_elements}}, $i;
5349 wakaba 1.54 }
5350 wakaba 1.191
5351     last; ## 2. (b) goto 5.
5352     } elsif (
5353     ## NOTE: not "formatting" and not "phrasing"
5354     ($node->[1] & SPECIAL_EL or
5355     $node->[1] & SCOPING_EL) and
5356     ## NOTE: "li", "dt", and "dd" are in |SPECIAL_EL|.
5357    
5358 wakaba 1.201 (not $node->[1] & ADDRESS_DIV_P_EL)
5359     ) {
5360 wakaba 1.191 ## 3.
5361    
5362     last; ## goto 5.
5363     } elsif ($node->[1] & END_TAG_OPTIONAL_EL) {
5364    
5365     #
5366 wakaba 1.79 } else {
5367    
5368 wakaba 1.191 $non_optional ||= $node;
5369     #
5370 wakaba 1.54 }
5371 wakaba 1.191 ## 4.
5372     ## goto 2.
5373     $i--;
5374     }
5375    
5376     ## 5. (a) has a |p| element in scope
5377     INSCOPE: for (reverse @{$self->{open_elements}}) {
5378 wakaba 1.201 if ($_->[1] == P_EL) {
5379 wakaba 1.191
5380    
5381     $token->{self_closing} = $self->{self_closing};
5382     unshift @{$self->{token}}, $token;
5383     delete $self->{self_closing};
5384     # <x>
5385     $token = {type => END_TAG_TOKEN, tag_name => 'p',
5386     line => $token->{line}, column => $token->{column}};
5387     next B;
5388     } elsif ($_->[1] & SCOPING_EL) {
5389 wakaba 1.79
5390 wakaba 1.191 last INSCOPE;
5391 wakaba 1.54 }
5392 wakaba 1.191 } # INSCOPE
5393    
5394     ## 5. (b) insert
5395 wakaba 1.54
5396 wakaba 1.49 {
5397     my $el;
5398    
5399     $el = $self->{document}->create_element_ns
5400 wakaba 1.128 ($HTML_NS, [undef, $token->{tag_name}]);
5401 wakaba 1.49
5402     for my $attr_name (keys %{ $token->{attributes}}) {
5403 wakaba 1.117 my $attr_t = $token->{attributes}->{$attr_name};
5404 wakaba 1.128 my $attr = $self->{document}->create_attribute_ns (undef, [undef, $attr_name]);
5405 wakaba 1.117 $attr->value ($attr_t->{value});
5406     $attr->set_user_data (manakai_source_line => $attr_t->{line});
5407     $attr->set_user_data (manakai_source_column => $attr_t->{column});
5408     $el->set_attribute_node_ns ($attr);
5409 wakaba 1.49 }
5410    
5411 wakaba 1.114 $el->set_user_data (manakai_source_line => $token->{line})
5412     if defined $token->{line};
5413     $el->set_user_data (manakai_source_column => $token->{column})
5414     if defined $token->{column};
5415    
5416 wakaba 1.54 $insert->($el);
5417 wakaba 1.121 push @{$self->{open_elements}}, [$el, $el_category->{$token->{tag_name}} || 0];
5418 wakaba 1.49 }
5419    
5420 wakaba 1.122
5421 wakaba 1.54 $token = $self->_get_next_token;
5422 wakaba 1.123 next B;
5423 wakaba 1.54 } elsif ($token->{tag_name} eq 'plaintext') {
5424 wakaba 1.190 ## NOTE: As normal, but effectively ends parsing
5425    
5426 wakaba 1.54 ## has a p element in scope
5427     INSCOPE: for (reverse @{$self->{open_elements}}) {
5428 wakaba 1.201 if ($_->[1] == P_EL) {
5429 wakaba 1.79
5430 wakaba 1.122
5431     $token->{self_closing} = $self->{self_closing};
5432     unshift @{$self->{token}}, $token;
5433     delete $self->{self_closing};
5434     # <plaintext>
5435 wakaba 1.112 $token = {type => END_TAG_TOKEN, tag_name => 'p',
5436     line => $token->{line}, column => $token->{column}};
5437 wakaba 1.123 next B;
5438 wakaba 1.121 } elsif ($_->[1] & SCOPING_EL) {
5439 wakaba 1.79
5440 wakaba 1.54 last INSCOPE;
5441     }
5442     } # INSCOPE
5443    
5444    
5445 wakaba 1.1 {
5446     my $el;
5447    
5448     $el = $self->{document}->create_element_ns
5449 wakaba 1.128 ($HTML_NS, [undef, $token->{tag_name}]);
5450 wakaba 1.1
5451 wakaba 1.54 for my $attr_name (keys %{ $token->{attributes}}) {
5452 wakaba 1.117 my $attr_t = $token->{attributes}->{$attr_name};
5453 wakaba 1.128 my $attr = $self->{document}->create_attribute_ns (undef, [undef, $attr_name]);
5454 wakaba 1.117 $attr->value ($attr_t->{value});
5455     $attr->set_user_data (manakai_source_line => $attr_t->{line});
5456     $attr->set_user_data (manakai_source_column => $attr_t->{column});
5457     $el->set_attribute_node_ns ($attr);
5458 wakaba 1.54 }
5459    
5460 wakaba 1.114 $el->set_user_data (manakai_source_line => $token->{line})
5461     if defined $token->{line};
5462     $el->set_user_data (manakai_source_column => $token->{column})
5463     if defined $token->{column};
5464    
5465 wakaba 1.54 $insert->($el);
5466 wakaba 1.121 push @{$self->{open_elements}}, [$el, $el_category->{$token->{tag_name}} || 0];
5467 wakaba 1.1 }
5468    
5469 wakaba 1.54
5470     $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
5471    
5472 wakaba 1.122
5473 wakaba 1.54 $token = $self->_get_next_token;
5474 wakaba 1.123 next B;
5475 wakaba 1.54 } elsif ($token->{tag_name} eq 'a') {
5476     AFE: for my $i (reverse 0..$#$active_formatting_elements) {
5477     my $node = $active_formatting_elements->[$i];
5478 wakaba 1.201 if ($node->[1] == A_EL) {
5479 wakaba 1.79
5480 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'in a:a', token => $token);
5481 wakaba 1.54
5482 wakaba 1.122
5483     $token->{self_closing} = $self->{self_closing};
5484     unshift @{$self->{token}}, $token;
5485     delete $self->{self_closing};
5486     # <a>
5487 wakaba 1.112 $token = {type => END_TAG_TOKEN, tag_name => 'a',
5488     line => $token->{line}, column => $token->{column}};
5489 wakaba 1.111 $formatting_end_tag->($token);
5490 wakaba 1.54
5491     AFE2: for (reverse 0..$#$active_formatting_elements) {
5492     if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
5493 wakaba 1.79
5494 wakaba 1.54 splice @$active_formatting_elements, $_, 1;
5495     last AFE2;
5496 wakaba 1.49 }
5497 wakaba 1.54 } # AFE2
5498     OE: for (reverse 0..$#{$self->{open_elements}}) {
5499     if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
5500 wakaba 1.79
5501 wakaba 1.54 splice @{$self->{open_elements}}, $_, 1;
5502     last OE;
5503 wakaba 1.49 }
5504 wakaba 1.54 } # OE
5505     last AFE;
5506     } elsif ($node->[0] eq '#marker') {
5507 wakaba 1.79
5508 wakaba 1.54 last AFE;
5509     }
5510     } # AFE
5511    
5512     $reconstruct_active_formatting_elements->($insert_to_current);
5513 wakaba 1.49
5514 wakaba 1.54
5515     {
5516     my $el;
5517    
5518     $el = $self->{document}->create_element_ns
5519 wakaba 1.128 ($HTML_NS, [undef, $token->{tag_name}]);
5520 wakaba 1.54
5521     for my $attr_name (keys %{ $token->{attributes}}) {
5522 wakaba 1.117 my $attr_t = $token->{attributes}->{$attr_name};
5523 wakaba 1.128 my $attr = $self->{document}->create_attribute_ns (undef, [undef, $attr_name]);
5524 wakaba 1.117 $attr->value ($attr_t->{value});
5525     $attr->set_user_data (manakai_source_line => $attr_t->{line});
5526     $attr->set_user_data (manakai_source_column => $attr_t->{column});
5527     $el->set_attribute_node_ns ($attr);
5528 wakaba 1.54 }
5529    
5530 wakaba 1.114 $el->set_user_data (manakai_source_line => $token->{line})
5531     if defined $token->{line};
5532     $el->set_user_data (manakai_source_column => $token->{column})
5533     if defined $token->{column};
5534    
5535 wakaba 1.54 $insert->($el);
5536 wakaba 1.121 push @{$self->{open_elements}}, [$el, $el_category->{$token->{tag_name}} || 0];
5537 wakaba 1.54 }
5538    
5539     push @$active_formatting_elements, $self->{open_elements}->[-1];
5540 wakaba 1.48
5541 wakaba 1.122
5542 wakaba 1.54 $token = $self->_get_next_token;
5543 wakaba 1.123 next B;
5544 wakaba 1.54 } elsif ($token->{tag_name} eq 'nobr') {
5545     $reconstruct_active_formatting_elements->($insert_to_current);
5546 wakaba 1.1
5547 wakaba 1.54 ## has a |nobr| element in scope
5548     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5549     my $node = $self->{open_elements}->[$_];
5550 wakaba 1.201 if ($node->[1] == NOBR_EL) {
5551 wakaba 1.79
5552 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'in nobr:nobr', token => $token);
5553 wakaba 1.122
5554     $token->{self_closing} = $self->{self_closing};
5555     unshift @{$self->{token}}, $token;
5556     delete $self->{self_closing};
5557     # <nobr>
5558 wakaba 1.112 $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
5559     line => $token->{line}, column => $token->{column}};
5560 wakaba 1.123 next B;
5561 wakaba 1.121 } elsif ($node->[1] & SCOPING_EL) {
5562 wakaba 1.79
5563 wakaba 1.54 last INSCOPE;
5564     }
5565     } # INSCOPE
5566    
5567    
5568     {
5569     my $el;
5570    
5571     $el = $self->{document}->create_element_ns
5572 wakaba 1.128 ($HTML_NS, [undef, $token->{tag_name}]);
5573 wakaba 1.54
5574     for my $attr_name (keys %{ $token->{attributes}}) {
5575 wakaba 1.117 my $attr_t = $token->{attributes}->{$attr_name};
5576 wakaba 1.128 my $attr = $self->{document}->create_attribute_ns (undef, [undef, $attr_name]);
5577 wakaba 1.117 $attr->value ($attr_t->{value});
5578     $attr->set_user_data (manakai_source_line => $attr_t->{line});
5579     $attr->set_user_data (manakai_source_column => $attr_t->{column});
5580     $el->set_attribute_node_ns ($attr);
5581 wakaba 1.54 }
5582    
5583 wakaba 1.114 $el->set_user_data (manakai_source_line => $token->{line})
5584     if defined $token->{line};
5585     $el->set_user_data (manakai_source_column => $token->{column})
5586     if defined $token->{column};
5587    
5588 wakaba 1.54 $insert->($el);
5589 wakaba 1.121 push @{$self->{open_elements}}, [$el, $el_category->{$token->{tag_name}} || 0];
5590 wakaba 1.54 }
5591    
5592     push @$active_formatting_elements, $self->{open_elements}->[-1];
5593    
5594 wakaba 1.122
5595 wakaba 1.54 $token = $self->_get_next_token;
5596 wakaba 1.123 next B;
5597 wakaba 1.54 } elsif ($token->{tag_name} eq 'button') {
5598     ## has a button element in scope
5599     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5600     my $node = $self->{open_elements}->[$_];
5601 wakaba 1.201 if ($node->[1] == BUTTON_EL) {
5602 wakaba 1.79
5603 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'in button:button', token => $token);
5604 wakaba 1.122
5605     $token->{self_closing} = $self->{self_closing};
5606     unshift @{$self->{token}}, $token;
5607     delete $self->{self_closing};
5608     # <button>
5609 wakaba 1.112 $token = {type => END_TAG_TOKEN, tag_name => 'button',
5610     line => $token->{line}, column => $token->{column}};
5611 wakaba 1.123 next B;
5612 wakaba 1.121 } elsif ($node->[1] & SCOPING_EL) {
5613 wakaba 1.79
5614 wakaba 1.54 last INSCOPE;
5615 wakaba 1.1 }
5616 wakaba 1.54 } # INSCOPE
5617    
5618     $reconstruct_active_formatting_elements->($insert_to_current);
5619    
5620    
5621     {
5622     my $el;
5623    
5624     $el = $self->{document}->create_element_ns
5625 wakaba 1.128 ($HTML_NS, [undef, $token->{tag_name}]);
5626 wakaba 1.54
5627     for my $attr_name (keys %{ $token->{attributes}}) {
5628 wakaba 1.117 my $attr_t = $token->{attributes}->{$attr_name};
5629 wakaba 1.128 my $attr = $self->{document}->create_attribute_ns (undef, [undef, $attr_name]);
5630 wakaba 1.117 $attr->value ($attr_t->{value});
5631     $attr->set_user_data (manakai_source_line => $attr_t->{line});
5632     $attr->set_user_data (manakai_source_column => $attr_t->{column});
5633     $el->set_attribute_node_ns ($attr);
5634 wakaba 1.54 }
5635    
5636 wakaba 1.114 $el->set_user_data (manakai_source_line => $token->{line})
5637     if defined $token->{line};
5638     $el->set_user_data (manakai_source_column => $token->{column})
5639     if defined $token->{column};
5640    
5641 wakaba 1.54 $insert->($el);
5642 wakaba 1.121 push @{$self->{open_elements}}, [$el, $el_category->{$token->{tag_name}} || 0];
5643 wakaba 1.54 }
5644    
5645 wakaba 1.86
5646     ## TODO: associate with $self->{form_element} if defined
5647    
5648 wakaba 1.54 push @$active_formatting_elements, ['#marker', ''];
5649 wakaba 1.1
5650 wakaba 1.122
5651 wakaba 1.54 $token = $self->_get_next_token;
5652 wakaba 1.123 next B;
5653 wakaba 1.102 } elsif ({
5654 wakaba 1.107 xmp => 1,
5655     iframe => 1,
5656     noembed => 1,
5657 wakaba 1.145 noframes => 1, ## NOTE: This is an "as if in head" code clone.
5658 wakaba 1.107 noscript => 0, ## TODO: 1 if scripting is enabled
5659 wakaba 1.102 }->{$token->{tag_name}}) {
5660 wakaba 1.107 if ($token->{tag_name} eq 'xmp') {
5661    
5662     $reconstruct_active_formatting_elements->($insert_to_current);
5663     } else {
5664    
5665 wakaba 1.1 }
5666 wakaba 1.107 ## NOTE: There is an "as if in body" code clone.
5667 wakaba 1.96 $parse_rcdata->(CDATA_CONTENT_MODEL);
5668 wakaba 1.123 next B;
5669 wakaba 1.54 } elsif ($token->{tag_name} eq 'isindex') {
5670 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'isindex', token => $token);
5671 wakaba 1.54
5672     if (defined $self->{form_element}) {
5673 wakaba 1.79
5674 wakaba 1.54 ## Ignore the token
5675 wakaba 1.122 ## NOTE: Not acknowledged.
5676 wakaba 1.54 $token = $self->_get_next_token;
5677 wakaba 1.123 next B;
5678 wakaba 1.54 } else {
5679 wakaba 1.144 delete $self->{self_closing};
5680    
5681 wakaba 1.54 my $at = $token->{attributes};
5682     my $form_attrs;
5683     $form_attrs->{action} = $at->{action} if $at->{action};
5684     my $prompt_attr = $at->{prompt};
5685     $at->{name} = {name => 'name', value => 'isindex'};
5686     delete $at->{action};
5687     delete $at->{prompt};
5688     my @tokens = (
5689 wakaba 1.57 {type => START_TAG_TOKEN, tag_name => 'form',
5690 wakaba 1.112 attributes => $form_attrs,
5691     line => $token->{line}, column => $token->{column}},
5692     {type => START_TAG_TOKEN, tag_name => 'hr',
5693     line => $token->{line}, column => $token->{column}},
5694     {type => START_TAG_TOKEN, tag_name => 'label',
5695     line => $token->{line}, column => $token->{column}},
5696 wakaba 1.54 );
5697     if ($prompt_attr) {
5698 wakaba 1.79
5699 wakaba 1.112 push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
5700 wakaba 1.116 #line => $token->{line}, column => $token->{column},
5701     };
5702 wakaba 1.1 } else {
5703 wakaba 1.79
5704 wakaba 1.57 push @tokens, {type => CHARACTER_TOKEN,
5705 wakaba 1.112 data => 'This is a searchable index. Insert your search keywords here: ',
5706 wakaba 1.116 #line => $token->{line}, column => $token->{column},
5707     }; # SHOULD
5708 wakaba 1.54 ## TODO: make this configurable
5709 wakaba 1.1 }
5710 wakaba 1.54 push @tokens,
5711 wakaba 1.112 {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
5712     line => $token->{line}, column => $token->{column}},
5713 wakaba 1.57 #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
5714 wakaba 1.112 {type => END_TAG_TOKEN, tag_name => 'label',
5715     line => $token->{line}, column => $token->{column}},
5716     {type => START_TAG_TOKEN, tag_name => 'hr',
5717     line => $token->{line}, column => $token->{column}},
5718     {type => END_TAG_TOKEN, tag_name => 'form',
5719     line => $token->{line}, column => $token->{column}};
5720 wakaba 1.54 unshift @{$self->{token}}, (@tokens);
5721 wakaba 1.122 $token = $self->_get_next_token;
5722 wakaba 1.123 next B;
5723 wakaba 1.54 }
5724     } elsif ($token->{tag_name} eq 'textarea') {
5725 wakaba 1.200 ## Step 1
5726 wakaba 1.54
5727 wakaba 1.200 {
5728     my $el;
5729    
5730 wakaba 1.54 $el = $self->{document}->create_element_ns
5731 wakaba 1.128 ($HTML_NS, [undef, $token->{tag_name}]);
5732 wakaba 1.54
5733 wakaba 1.200 for my $attr_name (keys %{ $token->{attributes}}) {
5734     my $attr_t = $token->{attributes}->{$attr_name};
5735 wakaba 1.128 my $attr = $self->{document}->create_attribute_ns (undef, [undef, $attr_name]);
5736 wakaba 1.117 $attr->value ($attr_t->{value});
5737     $attr->set_user_data (manakai_source_line => $attr_t->{line});
5738     $attr->set_user_data (manakai_source_column => $attr_t->{column});
5739     $el->set_attribute_node_ns ($attr);
5740 wakaba 1.54 }
5741    
5742 wakaba 1.114 $el->set_user_data (manakai_source_line => $token->{line})
5743     if defined $token->{line};
5744     $el->set_user_data (manakai_source_column => $token->{column})
5745     if defined $token->{column};
5746    
5747 wakaba 1.200 $insert->($el);
5748     push @{$self->{open_elements}}, [$el, $el_category->{$token->{tag_name}} || 0];
5749     }
5750    
5751 wakaba 1.54
5752 wakaba 1.200 ## Step 2
5753 wakaba 1.54 ## TODO: $self->{form_element} if defined
5754 wakaba 1.200
5755     ## Step 3
5756     $self->{ignore_newline} = 1;
5757    
5758     ## Step 4
5759     ## ISSUE: This step is wrong. (r2302 enbugged)
5760    
5761     ## Step 5
5762 wakaba 1.54 $self->{content_model} = RCDATA_CONTENT_MODEL;
5763     delete $self->{escape}; # MUST
5764 wakaba 1.200
5765     ## Step 6-7
5766     $self->{insertion_mode} |= IN_CDATA_RCDATA_IM;
5767    
5768 wakaba 1.54
5769     $token = $self->_get_next_token;
5770 wakaba 1.123 next B;
5771 wakaba 1.196 } elsif ($token->{tag_name} eq 'optgroup' or
5772     $token->{tag_name} eq 'option') {
5773     ## has an |option| element in scope
5774     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5775     my $node = $self->{open_elements}->[$_];
5776 wakaba 1.201 if ($node->[1] == OPTION_EL) {
5777 wakaba 1.196
5778     ## NOTE: As if </option>
5779    
5780     $token->{self_closing} = $self->{self_closing};
5781     unshift @{$self->{token}}, $token;
5782     delete $self->{self_closing};
5783     # <option> or <optgroup>
5784     $token = {type => END_TAG_TOKEN, tag_name => 'option',
5785     line => $token->{line}, column => $token->{column}};
5786     next B;
5787     } elsif ($node->[1] & SCOPING_EL) {
5788    
5789     last INSCOPE;
5790     }
5791     } # INSCOPE
5792    
5793     $reconstruct_active_formatting_elements->($insert_to_current);
5794    
5795    
5796     {
5797     my $el;
5798    
5799     $el = $self->{document}->create_element_ns
5800     ($HTML_NS, [undef, $token->{tag_name}]);
5801    
5802     for my $attr_name (keys %{ $token->{attributes}}) {
5803     my $attr_t = $token->{attributes}->{$attr_name};
5804     my $attr = $self->{document}->create_attribute_ns (undef, [undef, $attr_name]);
5805     $attr->value ($attr_t->{value});
5806     $attr->set_user_data (manakai_source_line => $attr_t->{line});
5807     $attr->set_user_data (manakai_source_column => $attr_t->{column});
5808     $el->set_attribute_node_ns ($attr);
5809     }
5810    
5811     $el->set_user_data (manakai_source_line => $token->{line})
5812     if defined $token->{line};
5813     $el->set_user_data (manakai_source_column => $token->{column})
5814     if defined $token->{column};
5815    
5816     $insert->($el);
5817     push @{$self->{open_elements}}, [$el, $el_category->{$token->{tag_name}} || 0];
5818     }
5819    
5820    
5821    
5822     $token = $self->_get_next_token;
5823     redo B;
5824 wakaba 1.149 } elsif ($token->{tag_name} eq 'rt' or
5825     $token->{tag_name} eq 'rp') {
5826     ## has a |ruby| element in scope
5827     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5828     my $node = $self->{open_elements}->[$_];
5829 wakaba 1.201 if ($node->[1] == RUBY_EL) {
5830 wakaba 1.149
5831     ## generate implied end tags
5832     while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5833    
5834     pop @{$self->{open_elements}};
5835     }
5836 wakaba 1.201 unless ($self->{open_elements}->[-1]->[1] == RUBY_EL) {
5837 wakaba 1.149
5838 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'not closed',
5839     text => $self->{open_elements}->[-1]->[0]
5840 wakaba 1.149 ->manakai_local_name,
5841     token => $token);
5842     pop @{$self->{open_elements}}
5843 wakaba 1.201 while not $self->{open_elements}->[-1]->[1] == RUBY_EL;
5844 wakaba 1.149 }
5845     last INSCOPE;
5846     } elsif ($node->[1] & SCOPING_EL) {
5847    
5848     last INSCOPE;
5849     }
5850     } # INSCOPE
5851 wakaba 1.207
5852     ## TODO: <non-ruby><rt> is not allowed.
5853 wakaba 1.149
5854    
5855     {
5856     my $el;
5857    
5858     $el = $self->{document}->create_element_ns
5859     ($HTML_NS, [undef, $token->{tag_name}]);
5860    
5861     for my $attr_name (keys %{ $token->{attributes}}) {
5862     my $attr_t = $token->{attributes}->{$attr_name};
5863     my $attr = $self->{document}->create_attribute_ns (undef, [undef, $attr_name]);
5864     $attr->value ($attr_t->{value});
5865     $attr->set_user_data (manakai_source_line => $attr_t->{line});
5866     $attr->set_user_data (manakai_source_column => $attr_t->{column});
5867     $el->set_attribute_node_ns ($attr);
5868     }
5869    
5870     $el->set_user_data (manakai_source_line => $token->{line})
5871     if defined $token->{line};
5872     $el->set_user_data (manakai_source_column => $token->{column})
5873     if defined $token->{column};
5874    
5875     $insert->($el);
5876     push @{$self->{open_elements}}, [$el, $el_category->{$token->{tag_name}} || 0];
5877     }
5878    
5879    
5880    
5881     $token = $self->_get_next_token;
5882     redo B;
5883 wakaba 1.123 } elsif ($token->{tag_name} eq 'math' or
5884     $token->{tag_name} eq 'svg') {
5885     $reconstruct_active_formatting_elements->($insert_to_current);
5886 wakaba 1.128
5887 wakaba 1.152 ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
5888    
5889 wakaba 1.128 ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
5890    
5891     ## "adjust foreign attributes" - done in insert-element-f
5892 wakaba 1.123
5893    
5894     {
5895     my $el;
5896    
5897     $el = $self->{document}->create_element_ns
5898 wakaba 1.128 ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, [undef, $token->{tag_name}]);
5899 wakaba 1.123
5900     for my $attr_name (keys %{ $token->{attributes}}) {
5901     my $attr_t = $token->{attributes}->{$attr_name};
5902 wakaba 1.128 my $attr = $self->{document}->create_attribute_ns (
5903     @{
5904     $foreign_attr_xname->{$attr_name} ||
5905     [undef, [undef,
5906 wakaba 1.152 ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS) eq $SVG_NS ?
5907 wakaba 1.128 ($svg_attr_name->{$attr_name} || $attr_name) :
5908 wakaba 1.152 ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS) eq $MML_NS ?
5909     ($attr_name eq 'definitionurl' ?
5910     'definitionURL' : $attr_name) :
5911 wakaba 1.128 $attr_name]]
5912     }
5913     );
5914 wakaba 1.123 $attr->value ($attr_t->{value});
5915     $attr->set_user_data (manakai_source_line => $attr_t->{line});
5916     $attr->set_user_data (manakai_source_column => $attr_t->{column});
5917     $el->set_attribute_node_ns ($attr);
5918     }
5919    
5920     $el->set_user_data (manakai_source_line => $token->{line})
5921     if defined $token->{line};
5922     $el->set_user_data (manakai_source_column => $token->{column})
5923     if defined $token->{column};
5924    
5925     $insert->($el);
5926     push @{$self->{open_elements}}, [$el, ($el_category_f->{$token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS}->{ $token->{tag_name}} || 0) | FOREIGN_EL];
5927 wakaba 1.128
5928     if ( $token->{attributes}->{xmlns} and $token->{attributes}->{xmlns}->{value} ne ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS)) {
5929 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'bad namespace', token => $token);
5930 wakaba 1.128 ## TODO: Error type documentation
5931     }
5932 wakaba 1.217 if ( $token->{attributes}->{'xmlns:xlink'} and
5933     $token->{attributes}->{'xmlns:xlink'}->{value} ne q<http://www.w3.org/1999/xlink>) {
5934     $self->{parse_error}->(level => $self->{level}->{must}, type => 'bad namespace', token => $token);
5935     }
5936 wakaba 1.123 }
5937    
5938    
5939     if ($self->{self_closing}) {
5940     pop @{$self->{open_elements}};
5941     delete $self->{self_closing};
5942     } else {
5943    
5944     $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
5945     ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
5946     ## mode, "in body" (not "in foreign content") secondary insertion
5947     ## mode, maybe.
5948     }
5949    
5950     $token = $self->_get_next_token;
5951     next B;
5952 wakaba 1.54 } elsif ({
5953     caption => 1, col => 1, colgroup => 1, frame => 1,
5954 wakaba 1.196 frameset => 1, head => 1,
5955 wakaba 1.54 tbody => 1, td => 1, tfoot => 1, th => 1,
5956     thead => 1, tr => 1,
5957     }->{$token->{tag_name}}) {
5958 wakaba 1.79
5959 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'in body',
5960     text => $token->{tag_name}, token => $token);
5961 wakaba 1.54 ## Ignore the token
5962 wakaba 1.122 ## NOTE: |<col/>| or |<frame/>| here is an error.
5963 wakaba 1.54 $token = $self->_get_next_token;
5964 wakaba 1.123 next B;
5965 wakaba 1.193 } elsif ($token->{tag_name} eq 'param' or
5966     $token->{tag_name} eq 'source') {
5967 wakaba 1.54
5968 wakaba 1.193 {
5969     my $el;
5970    
5971     $el = $self->{document}->create_element_ns
5972     ($HTML_NS, [undef, $token->{tag_name}]);
5973    
5974     for my $attr_name (keys %{ $token->{attributes}}) {
5975     my $attr_t = $token->{attributes}->{$attr_name};
5976     my $attr = $self->{document}->create_attribute_ns (undef, [undef, $attr_name]);
5977     $attr->value ($attr_t->{value});
5978     $attr->set_user_data (manakai_source_line => $attr_t->{line});
5979     $attr->set_user_data (manakai_source_column => $attr_t->{column});
5980     $el->set_attribute_node_ns ($attr);
5981     }
5982    
5983     $el->set_user_data (manakai_source_line => $token->{line})
5984     if defined $token->{line};
5985     $el->set_user_data (manakai_source_column => $token->{column})
5986     if defined $token->{column};
5987    
5988     $insert->($el);
5989     push @{$self->{open_elements}}, [$el, $el_category->{$token->{tag_name}} || 0];
5990     }
5991    
5992     pop @{$self->{open_elements}};
5993    
5994     delete $self->{self_closing};
5995     $token = $self->_get_next_token;
5996     redo B;
5997 wakaba 1.54 } else {
5998 wakaba 1.108 if ($token->{tag_name} eq 'image') {
5999    
6000 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'image', token => $token);
6001 wakaba 1.108 $token->{tag_name} = 'img';
6002     } else {
6003    
6004     }
6005    
6006     ## NOTE: There is an "as if <br>" code clone.
6007 wakaba 1.54 $reconstruct_active_formatting_elements->($insert_to_current);
6008 wakaba 1.53
6009 wakaba 1.54
6010     {
6011     my $el;
6012    
6013     $el = $self->{document}->create_element_ns
6014 wakaba 1.128 ($HTML_NS, [undef, $token->{tag_name}]);
6015 wakaba 1.54
6016     for my $attr_name (keys %{ $token->{attributes}}) {
6017 wakaba 1.117 my $attr_t = $token->{attributes}->{$attr_name};
6018 wakaba 1.128 my $attr = $self->{document}->create_attribute_ns (undef, [undef, $attr_name]);
6019 wakaba 1.117 $attr->value ($attr_t->{value});
6020     $attr->set_user_data (manakai_source_line => $attr_t->{line});
6021     $attr->set_user_data (manakai_source_column => $attr_t->{column});
6022     $el->set_attribute_node_ns ($attr);
6023 wakaba 1.53 }
6024 wakaba 1.54
6025 wakaba 1.114 $el->set_user_data (manakai_source_line => $token->{line})
6026     if defined $token->{line};
6027     $el->set_user_data (manakai_source_column => $token->{column})
6028     if defined $token->{column};
6029    
6030 wakaba 1.54 $insert->($el);
6031 wakaba 1.121 push @{$self->{open_elements}}, [$el, $el_category->{$token->{tag_name}} || 0];
6032 wakaba 1.54 }
6033    
6034 wakaba 1.107
6035 wakaba 1.108 if ({
6036     applet => 1, marquee => 1, object => 1,
6037     }->{$token->{tag_name}}) {
6038    
6039     push @$active_formatting_elements, ['#marker', ''];
6040 wakaba 1.122
6041 wakaba 1.108 } elsif ({
6042     b => 1, big => 1, em => 1, font => 1, i => 1,
6043 wakaba 1.188 s => 1, small => 1, strike => 1,
6044 wakaba 1.108 strong => 1, tt => 1, u => 1,
6045     }->{$token->{tag_name}}) {
6046    
6047     push @$active_formatting_elements, $self->{open_elements}->[-1];
6048 wakaba 1.122
6049 wakaba 1.108 } elsif ($token->{tag_name} eq 'input') {
6050    
6051     ## TODO: associate with $self->{form_element} if defined
6052     pop @{$self->{open_elements}};
6053 wakaba 1.122 delete $self->{self_closing};
6054 wakaba 1.108 } elsif ({
6055     area => 1, basefont => 1, bgsound => 1, br => 1,
6056 wakaba 1.193 embed => 1, img => 1, spacer => 1, wbr => 1,
6057 wakaba 1.108 }->{$token->{tag_name}}) {
6058    
6059     pop @{$self->{open_elements}};
6060 wakaba 1.122 delete $self->{self_closing};
6061 wakaba 1.108 } elsif ($token->{tag_name} eq 'select') {
6062 wakaba 1.107 ## TODO: associate with $self->{form_element} if defined
6063    
6064     if ($self->{insertion_mode} & TABLE_IMS or
6065     $self->{insertion_mode} & BODY_TABLE_IMS or
6066 wakaba 1.206 ($self->{insertion_mode} & IM_MASK) == IN_COLUMN_GROUP_IM) {
6067 wakaba 1.107
6068     $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6069     } else {
6070    
6071     $self->{insertion_mode} = IN_SELECT_IM;
6072     }
6073 wakaba 1.122
6074 wakaba 1.108 } else {
6075    
6076 wakaba 1.107 }
6077 wakaba 1.53
6078 wakaba 1.54 $token = $self->_get_next_token;
6079 wakaba 1.123 next B;
6080 wakaba 1.54 }
6081 wakaba 1.57 } elsif ($token->{type} == END_TAG_TOKEN) {
6082 wakaba 1.54 if ($token->{tag_name} eq 'body') {
6083 wakaba 1.105 ## has a |body| element in scope
6084     my $i;
6085 wakaba 1.109 INSCOPE: {
6086     for (reverse @{$self->{open_elements}}) {
6087 wakaba 1.201 if ($_->[1] == BODY_EL) {
6088 wakaba 1.109
6089     $i = $_;
6090     last INSCOPE;
6091 wakaba 1.121 } elsif ($_->[1] & SCOPING_EL) {
6092 wakaba 1.109
6093     last;
6094     }
6095 wakaba 1.54 }
6096 wakaba 1.109
6097 wakaba 1.195 ## NOTE: |<marquee></body>|, |<svg><foreignobject></body>|
6098    
6099     $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
6100 wakaba 1.150 text => $token->{tag_name}, token => $token);
6101 wakaba 1.105 ## NOTE: Ignore the token.
6102 wakaba 1.54 $token = $self->_get_next_token;
6103 wakaba 1.123 next B;
6104 wakaba 1.109 } # INSCOPE
6105 wakaba 1.105
6106     for (@{$self->{open_elements}}) {
6107 wakaba 1.216 unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL ||
6108     $_->[1] == OPTGROUP_EL ||
6109     $_->[1] == OPTION_EL ||
6110     $_->[1] == RUBY_COMPONENT_EL) {
6111 wakaba 1.105
6112 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'not closed',
6113     text => $_->[0]->manakai_local_name,
6114 wakaba 1.120 token => $token);
6115 wakaba 1.105 last;
6116     } else {
6117    
6118     }
6119     }
6120    
6121     $self->{insertion_mode} = AFTER_BODY_IM;
6122     $token = $self->_get_next_token;
6123 wakaba 1.123 next B;
6124 wakaba 1.54 } elsif ($token->{tag_name} eq 'html') {
6125 wakaba 1.120 ## TODO: Update this code. It seems that the code below is not
6126     ## up-to-date, though it has same effect as speced.
6127 wakaba 1.121 if (@{$self->{open_elements}} > 1 and
6128 wakaba 1.201 $self->{open_elements}->[1]->[1] == BODY_EL) {
6129     unless ($self->{open_elements}->[-1]->[1] == BODY_EL) {
6130 wakaba 1.79
6131 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'not closed',
6132     text => $self->{open_elements}->[1]->[0]
6133 wakaba 1.120 ->manakai_local_name,
6134     token => $token);
6135 wakaba 1.79 } else {
6136    
6137 wakaba 1.54 }
6138 wakaba 1.56 $self->{insertion_mode} = AFTER_BODY_IM;
6139 wakaba 1.54 ## reprocess
6140 wakaba 1.123 next B;
6141 wakaba 1.54 } else {
6142 wakaba 1.79
6143 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
6144     text => $token->{tag_name}, token => $token);
6145 wakaba 1.54 ## Ignore the token
6146     $token = $self->_get_next_token;
6147 wakaba 1.123 next B;
6148 wakaba 1.53 }
6149 wakaba 1.54 } elsif ({
6150 wakaba 1.190 ## NOTE: End tags for non-phrasing flow content elements
6151    
6152     ## NOTE: The normal ones
6153     address => 1, article => 1, aside => 1, blockquote => 1,
6154     center => 1, datagrid => 1, details => 1, dialog => 1,
6155     dir => 1, div => 1, dl => 1, fieldset => 1, figure => 1,
6156     footer => 1, header => 1, listing => 1, menu => 1, nav => 1,
6157     ol => 1, pre => 1, section => 1, ul => 1,
6158    
6159     ## NOTE: As normal, but ... optional tags
6160 wakaba 1.54 dd => 1, dt => 1, li => 1,
6161 wakaba 1.190
6162 wakaba 1.102 applet => 1, button => 1, marquee => 1, object => 1,
6163 wakaba 1.54 }->{$token->{tag_name}}) {
6164 wakaba 1.192 ## NOTE: Code for <li> start tags includes "as if </li>" code.
6165     ## Code for <dt> or <dd> start tags includes "as if </dt> or
6166     ## </dd>" code.
6167    
6168 wakaba 1.54 ## has an element in scope
6169     my $i;
6170     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6171     my $node = $self->{open_elements}->[$_];
6172 wakaba 1.121 if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6173 wakaba 1.79
6174 wakaba 1.54 $i = $_;
6175 wakaba 1.87 last INSCOPE;
6176 wakaba 1.121 } elsif ($node->[1] & SCOPING_EL) {
6177 wakaba 1.79
6178 wakaba 1.54 last INSCOPE;
6179     }
6180     } # INSCOPE
6181 wakaba 1.89
6182     unless (defined $i) { # has an element in scope
6183    
6184 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
6185     text => $token->{tag_name}, token => $token);
6186 wakaba 1.154 ## NOTE: Ignore the token.
6187 wakaba 1.89 } else {
6188     ## Step 1. generate implied end tags
6189     while ({
6190 wakaba 1.149 ## END_TAG_OPTIONAL_EL
6191 wakaba 1.89 dd => ($token->{tag_name} ne 'dd'),
6192     dt => ($token->{tag_name} ne 'dt'),
6193     li => ($token->{tag_name} ne 'li'),
6194 wakaba 1.189 option => 1,
6195     optgroup => 1,
6196 wakaba 1.89 p => 1,
6197 wakaba 1.149 rt => 1,
6198     rp => 1,
6199 wakaba 1.121 }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6200 wakaba 1.89
6201     pop @{$self->{open_elements}};
6202     }
6203    
6204     ## Step 2.
6205 wakaba 1.121 if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6206     ne $token->{tag_name}) {
6207 wakaba 1.79
6208 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'not closed',
6209     text => $self->{open_elements}->[-1]->[0]
6210 wakaba 1.120 ->manakai_local_name,
6211     token => $token);
6212 wakaba 1.1 } else {
6213 wakaba 1.79
6214 wakaba 1.1 }
6215 wakaba 1.89
6216     ## Step 3.
6217 wakaba 1.54 splice @{$self->{open_elements}}, $i;
6218 wakaba 1.89
6219     ## Step 4.
6220     $clear_up_to_marker->()
6221     if {
6222 wakaba 1.102 applet => 1, button => 1, marquee => 1, object => 1,
6223 wakaba 1.89 }->{$token->{tag_name}};
6224 wakaba 1.54 }
6225     $token = $self->_get_next_token;
6226 wakaba 1.123 next B;
6227 wakaba 1.54 } elsif ($token->{tag_name} eq 'form') {
6228 wakaba 1.190 ## NOTE: As normal, but interacts with the form element pointer
6229    
6230 wakaba 1.92 undef $self->{form_element};
6231    
6232 wakaba 1.54 ## has an element in scope
6233 wakaba 1.92 my $i;
6234 wakaba 1.54 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6235     my $node = $self->{open_elements}->[$_];
6236 wakaba 1.201 if ($node->[1] == FORM_EL) {
6237 wakaba 1.79
6238 wakaba 1.92 $i = $_;
6239 wakaba 1.54 last INSCOPE;
6240 wakaba 1.121 } elsif ($node->[1] & SCOPING_EL) {
6241 wakaba 1.79
6242 wakaba 1.54 last INSCOPE;
6243 wakaba 1.36 }
6244 wakaba 1.54 } # INSCOPE
6245 wakaba 1.92
6246     unless (defined $i) { # has an element in scope
6247 wakaba 1.79
6248 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
6249     text => $token->{tag_name}, token => $token);
6250 wakaba 1.154 ## NOTE: Ignore the token.
6251 wakaba 1.54 } else {
6252 wakaba 1.92 ## Step 1. generate implied end tags
6253 wakaba 1.121 while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6254 wakaba 1.92
6255     pop @{$self->{open_elements}};
6256     }
6257    
6258     ## Step 2.
6259 wakaba 1.121 if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6260     ne $token->{tag_name}) {
6261 wakaba 1.92
6262 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'not closed',
6263     text => $self->{open_elements}->[-1]->[0]
6264 wakaba 1.120 ->manakai_local_name,
6265     token => $token);
6266 wakaba 1.92 } else {
6267    
6268     }
6269 wakaba 1.79
6270 wakaba 1.92 ## Step 3.
6271     splice @{$self->{open_elements}}, $i;
6272 wakaba 1.36 }
6273    
6274 wakaba 1.54 $token = $self->_get_next_token;
6275 wakaba 1.123 next B;
6276 wakaba 1.54 } elsif ({
6277 wakaba 1.190 ## NOTE: As normal, except acts as a closer for any ...
6278 wakaba 1.54 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6279     }->{$token->{tag_name}}) {
6280     ## has an element in scope
6281     my $i;
6282     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6283     my $node = $self->{open_elements}->[$_];
6284 wakaba 1.201 if ($node->[1] == HEADING_EL) {
6285 wakaba 1.79
6286 wakaba 1.54 $i = $_;
6287     last INSCOPE;
6288 wakaba 1.121 } elsif ($node->[1] & SCOPING_EL) {
6289 wakaba 1.79
6290 wakaba 1.54 last INSCOPE;
6291 wakaba 1.53 }
6292 wakaba 1.54 } # INSCOPE
6293 wakaba 1.93
6294     unless (defined $i) { # has an element in scope
6295 wakaba 1.79
6296 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
6297     text => $token->{tag_name}, token => $token);
6298 wakaba 1.154 ## NOTE: Ignore the token.
6299 wakaba 1.79 } else {
6300 wakaba 1.93 ## Step 1. generate implied end tags
6301 wakaba 1.121 while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6302 wakaba 1.93
6303     pop @{$self->{open_elements}};
6304     }
6305 wakaba 1.79
6306 wakaba 1.93 ## Step 2.
6307 wakaba 1.121 if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6308     ne $token->{tag_name}) {
6309 wakaba 1.93
6310 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
6311     text => $token->{tag_name}, token => $token);
6312 wakaba 1.93 } else {
6313    
6314     }
6315    
6316     ## Step 3.
6317     splice @{$self->{open_elements}}, $i;
6318 wakaba 1.53 }
6319    
6320 wakaba 1.54 $token = $self->_get_next_token;
6321 wakaba 1.123 next B;
6322 wakaba 1.87 } elsif ($token->{tag_name} eq 'p') {
6323 wakaba 1.190 ## NOTE: As normal, except </p> implies <p> and ...
6324    
6325 wakaba 1.87 ## has an element in scope
6326 wakaba 1.192 my $non_optional;
6327 wakaba 1.87 my $i;
6328     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6329     my $node = $self->{open_elements}->[$_];
6330 wakaba 1.201 if ($node->[1] == P_EL) {
6331 wakaba 1.87
6332     $i = $_;
6333 wakaba 1.88 last INSCOPE;
6334 wakaba 1.121 } elsif ($node->[1] & SCOPING_EL) {
6335 wakaba 1.87
6336     last INSCOPE;
6337 wakaba 1.192 } elsif ($node->[1] & END_TAG_OPTIONAL_EL) {
6338     ## NOTE: |END_TAG_OPTIONAL_EL| includes "p"
6339    
6340     #
6341     } else {
6342    
6343     $non_optional ||= $node;
6344     #
6345 wakaba 1.87 }
6346     } # INSCOPE
6347 wakaba 1.91
6348     if (defined $i) {
6349 wakaba 1.192 ## 1. Generate implied end tags
6350     #
6351    
6352     ## 2. If current node != "p", parse error
6353     if ($non_optional) {
6354 wakaba 1.87
6355 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'not closed',
6356 wakaba 1.192 text => $non_optional->[0]->manakai_local_name,
6357 wakaba 1.120 token => $token);
6358 wakaba 1.87 } else {
6359    
6360     }
6361 wakaba 1.91
6362 wakaba 1.192 ## 3. Pop
6363 wakaba 1.87 splice @{$self->{open_elements}}, $i;
6364     } else {
6365    
6366 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
6367     text => $token->{tag_name}, token => $token);
6368 wakaba 1.91
6369    
6370 wakaba 1.87 ## As if <p>, then reprocess the current token
6371     my $el;
6372    
6373     $el = $self->{document}->create_element_ns
6374 wakaba 1.128 ($HTML_NS, [undef, 'p']);
6375 wakaba 1.87
6376 wakaba 1.114 $el->set_user_data (manakai_source_line => $token->{line})
6377     if defined $token->{line};
6378     $el->set_user_data (manakai_source_column => $token->{column})
6379     if defined $token->{column};
6380    
6381 wakaba 1.87 $insert->($el);
6382 wakaba 1.91 ## NOTE: Not inserted into |$self->{open_elements}|.
6383 wakaba 1.87 }
6384 wakaba 1.91
6385 wakaba 1.87 $token = $self->_get_next_token;
6386 wakaba 1.123 next B;
6387 wakaba 1.54 } elsif ({
6388     a => 1,
6389     b => 1, big => 1, em => 1, font => 1, i => 1,
6390 wakaba 1.188 nobr => 1, s => 1, small => 1, strike => 1,
6391 wakaba 1.54 strong => 1, tt => 1, u => 1,
6392     }->{$token->{tag_name}}) {
6393 wakaba 1.79
6394 wakaba 1.111 $formatting_end_tag->($token);
6395 wakaba 1.123 next B;
6396 wakaba 1.54 } elsif ($token->{tag_name} eq 'br') {
6397 wakaba 1.79
6398 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
6399     text => 'br', token => $token);
6400 wakaba 1.53
6401 wakaba 1.54 ## As if <br>
6402     $reconstruct_active_formatting_elements->($insert_to_current);
6403    
6404     my $el;
6405    
6406 wakaba 1.1 $el = $self->{document}->create_element_ns
6407 wakaba 1.128 ($HTML_NS, [undef, 'br']);
6408 wakaba 1.1
6409 wakaba 1.114 $el->set_user_data (manakai_source_line => $token->{line})
6410     if defined $token->{line};
6411     $el->set_user_data (manakai_source_column => $token->{column})
6412     if defined $token->{column};
6413    
6414 wakaba 1.54 $insert->($el);
6415    
6416     ## Ignore the token.
6417     $token = $self->_get_next_token;
6418 wakaba 1.123 next B;
6419 wakaba 1.54 } else {
6420 wakaba 1.190 if ($token->{tag_name} eq 'sarcasm') {
6421     sleep 0.001; # take a deep breath
6422     }
6423    
6424 wakaba 1.54 ## Step 1
6425     my $node_i = -1;
6426     my $node = $self->{open_elements}->[$node_i];
6427    
6428     ## Step 2
6429     S2: {
6430 wakaba 1.195 my $node_tag_name = $node->[0]->manakai_local_name;
6431     $node_tag_name =~ tr/A-Z/a-z/; # for SVG camelCase tag names
6432     if ($node_tag_name eq $token->{tag_name}) {
6433 wakaba 1.54 ## Step 1
6434     ## generate implied end tags
6435 wakaba 1.121 while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6436 wakaba 1.79
6437 wakaba 1.149 ## NOTE: |<ruby><rt></ruby>|.
6438     ## ISSUE: <ruby><rt></rt> will also take this code path,
6439     ## which seems wrong.
6440 wakaba 1.86 pop @{$self->{open_elements}};
6441 wakaba 1.149 $node_i++;
6442 wakaba 1.54 }
6443    
6444     ## Step 2
6445 wakaba 1.195 my $current_tag_name
6446     = $self->{open_elements}->[-1]->[0]->manakai_local_name;
6447     $current_tag_name =~ tr/A-Z/a-z/;
6448     if ($current_tag_name ne $token->{tag_name}) {
6449 wakaba 1.79
6450 wakaba 1.60 ## NOTE: <x><y></x>
6451 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'not closed',
6452     text => $self->{open_elements}->[-1]->[0]
6453 wakaba 1.120 ->manakai_local_name,
6454     token => $token);
6455 wakaba 1.79 } else {
6456    
6457 wakaba 1.54 }
6458    
6459     ## Step 3
6460 wakaba 1.149 splice @{$self->{open_elements}}, $node_i if $node_i < 0;
6461 wakaba 1.53
6462 wakaba 1.36 $token = $self->_get_next_token;
6463 wakaba 1.54 last S2;
6464 wakaba 1.1 } else {
6465 wakaba 1.54 ## Step 3
6466 wakaba 1.121 if (not ($node->[1] & FORMATTING_EL) and
6467 wakaba 1.54 #not $phrasing_category->{$node->[1]} and
6468 wakaba 1.121 ($node->[1] & SPECIAL_EL or
6469     $node->[1] & SCOPING_EL)) {
6470 wakaba 1.79
6471 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'unmatched end tag',
6472     text => $token->{tag_name}, token => $token);
6473 wakaba 1.54 ## Ignore the token
6474     $token = $self->_get_next_token;
6475     last S2;
6476 wakaba 1.188
6477     ## NOTE: |<span><dd></span>a|: In Safari 3.1.2 and Opera
6478     ## 9.27, "a" is a child of <dd> (conforming). In
6479     ## Firefox 3.0.2, "a" is a child of <body>. In WinIE 7,
6480     ## "a" is a child of both <body> and <dd>.
6481 wakaba 1.54 }
6482 wakaba 1.188
6483 wakaba 1.79
6484 wakaba 1.1 }
6485 wakaba 1.54
6486     ## Step 4
6487     $node_i--;
6488     $node = $self->{open_elements}->[$node_i];
6489    
6490     ## Step 5;
6491     redo S2;
6492     } # S2
6493 wakaba 1.123 next B;
6494 wakaba 1.1 }
6495     }
6496 wakaba 1.123 next B;
6497     } continue { # B
6498     if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
6499     ## NOTE: The code below is executed in cases where it does not have
6500     ## to be, but it it is harmless even in those cases.
6501     ## has an element in scope
6502     INSCOPE: {
6503     for (reverse 0..$#{$self->{open_elements}}) {
6504     my $node = $self->{open_elements}->[$_];
6505     if ($node->[1] & FOREIGN_EL) {
6506     last INSCOPE;
6507     } elsif ($node->[1] & SCOPING_EL) {
6508     last;
6509     }
6510     }
6511    
6512     ## NOTE: No foreign element in scope.
6513     $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
6514     } # INSCOPE
6515     }
6516 wakaba 1.1 } # B
6517    
6518     ## Stop parsing # MUST
6519    
6520     ## TODO: script stuffs
6521 wakaba 1.3 } # _tree_construct_main
6522    
6523 wakaba 1.214 ## XXX: How this method is organized is somewhat out of date, although
6524     ## it still does what the current spec documents.
6525 wakaba 1.173 sub set_inner_html ($$$$;$) {
6526 wakaba 1.3 my $class = shift;
6527 wakaba 1.213 my $node = shift; # /context/
6528 wakaba 1.173 #my $s = \$_[0];
6529 wakaba 1.3 my $onerror = $_[1];
6530 wakaba 1.158 my $get_wrapper = $_[2] || sub ($) { return $_[0] };
6531 wakaba 1.3
6532 wakaba 1.65 ## ISSUE: Should {confident} be true?
6533    
6534 wakaba 1.3 my $nt = $node->node_type;
6535 wakaba 1.213 if ($nt == 9) { # Document (invoke the algorithm with no /context/ element)
6536 wakaba 1.3 # MUST
6537    
6538     ## Step 1 # MUST
6539     ## TODO: If the document has an active parser, ...
6540     ## ISSUE: There is an issue in the spec.
6541    
6542     ## Step 2 # MUST
6543     my @cn = @{$node->child_nodes};
6544     for (@cn) {
6545     $node->remove_child ($_);
6546     }
6547    
6548     ## Step 3, 4, 5 # MUST
6549 wakaba 1.173 $class->parse_char_string ($_[0] => $node, $onerror, $get_wrapper);
6550 wakaba 1.213 } elsif ($nt == 1) { # Element (invoke the algorithm with /context/ element)
6551 wakaba 1.3 ## TODO: If non-html element
6552    
6553     ## NOTE: Most of this code is copied from |parse_string|
6554    
6555 wakaba 1.158 ## TODO: Support for $get_wrapper
6556    
6557 wakaba 1.213 ## F1. Create an HTML document.
6558 wakaba 1.14 my $this_doc = $node->owner_document;
6559     my $doc = $this_doc->implementation->create_document;
6560 wakaba 1.18 $doc->manakai_is_html (1);
6561 wakaba 1.213
6562     ## F2. Propagate quirkness flag
6563     my $node_doc = $node->owner_document;
6564     $doc->manakai_compat_mode ($node_doc->manakai_compat_mode);
6565    
6566     ## F3. Create an HTML parser
6567 wakaba 1.3 my $p = $class->new;
6568     $p->{document} = $doc;
6569    
6570 wakaba 1.84 ## Step 8 # MUST
6571 wakaba 1.3 my $i = 0;
6572 wakaba 1.119 $p->{line_prev} = $p->{line} = 1;
6573     $p->{column_prev} = $p->{column} = 0;
6574 wakaba 1.173 require Whatpm::Charset::DecodeHandle;
6575     my $input = Whatpm::Charset::DecodeHandle::CharString->new (\($_[0]));
6576     $input = $get_wrapper->($input);
6577 wakaba 1.179 $p->{set_nc} = sub {
6578 wakaba 1.3 my $self = shift;
6579 wakaba 1.14
6580 wakaba 1.174 my $char = '';
6581 wakaba 1.179 if (defined $self->{next_nc}) {
6582     $char = $self->{next_nc};
6583     delete $self->{next_nc};
6584     $self->{nc} = ord $char;
6585 wakaba 1.173 } else {
6586 wakaba 1.176 $self->{char_buffer} = '';
6587     $self->{char_buffer_pos} = 0;
6588    
6589     my $count = $input->manakai_read_until
6590 wakaba 1.178 ($self->{char_buffer}, qr/[^\x00\x0A\x0D]/,
6591     $self->{char_buffer_pos});
6592 wakaba 1.176 if ($count) {
6593     $self->{line_prev} = $self->{line};
6594     $self->{column_prev} = $self->{column};
6595     $self->{column}++;
6596 wakaba 1.179 $self->{nc}
6597 wakaba 1.176 = ord substr ($self->{char_buffer},
6598     $self->{char_buffer_pos}++, 1);
6599     return;
6600     }
6601    
6602 wakaba 1.174 if ($input->read ($char, 1)) {
6603 wakaba 1.179 $self->{nc} = ord $char;
6604 wakaba 1.174 } else {
6605 wakaba 1.179 $self->{nc} = -1;
6606 wakaba 1.174 return;
6607     }
6608 wakaba 1.173 }
6609 wakaba 1.119
6610     ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
6611     $p->{column}++;
6612 wakaba 1.4
6613 wakaba 1.179 if ($self->{nc} == 0x000A) { # LF
6614 wakaba 1.119 $p->{line}++;
6615     $p->{column} = 0;
6616 wakaba 1.79
6617 wakaba 1.179 } elsif ($self->{nc} == 0x000D) { # CR
6618 wakaba 1.173 ## TODO: support for abort/streaming
6619 wakaba 1.174 my $next = '';
6620     if ($input->read ($next, 1) and $next ne "\x0A") {
6621 wakaba 1.179 $self->{next_nc} = $next;
6622 wakaba 1.173 }
6623 wakaba 1.179 $self->{nc} = 0x000A; # LF # MUST
6624 wakaba 1.119 $p->{line}++;
6625     $p->{column} = 0;
6626 wakaba 1.79
6627 wakaba 1.179 } elsif ($self->{nc} == 0x0000) { # NULL
6628 wakaba 1.79
6629 wakaba 1.150 $self->{parse_error}->(level => $self->{level}->{must}, type => 'NULL');
6630 wakaba 1.179 $self->{nc} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
6631 wakaba 1.3 }
6632     };
6633 wakaba 1.167
6634 wakaba 1.168 $p->{read_until} = sub {
6635 wakaba 1.173 #my ($scalar, $specials_range, $offset) = @_;
6636 wakaba 1.179 return 0 if defined $p->{next_nc};
6637 wakaba 1.176
6638 wakaba 1.178 my $pattern = qr/[^$_[1]\x00\x0A\x0D]/;
6639 wakaba 1.176 my $offset = $_[2] || 0;
6640    
6641     if ($p->{char_buffer_pos} < length $p->{char_buffer}) {
6642     pos ($p->{char_buffer}) = $p->{char_buffer_pos};
6643     if ($p->{char_buffer} =~ /\G(?>$pattern)+/) {
6644     substr ($_[0], $offset)
6645     = substr ($p->{char_buffer}, $-[0], $+[0] - $-[0]);
6646     my $count = $+[0] - $-[0];
6647     if ($count) {
6648     $p->{column} += $count;
6649     $p->{char_buffer_pos} += $count;
6650     $p->{line_prev} = $p->{line};
6651     $p->{column_prev} = $p->{column} - 1;
6652 wakaba 1.179 $p->{nc} = -1;
6653 wakaba 1.176 }
6654     return $count;
6655     } else {
6656     return 0;
6657     }
6658     } else {
6659     my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
6660     if ($count) {
6661     $p->{column} += $count;
6662     $p->{column_prev} += $count;
6663 wakaba 1.179 $p->{nc} = -1;
6664 wakaba 1.176 }
6665     return $count;
6666 wakaba 1.173 }
6667     }; # $p->{read_until}
6668 wakaba 1.167
6669 wakaba 1.3 my $ponerror = $onerror || sub {
6670     my (%opt) = @_;
6671 wakaba 1.119 my $line = $opt{line};
6672     my $column = $opt{column};
6673     if (defined $opt{token} and defined $opt{token}->{line}) {
6674     $line = $opt{token}->{line};
6675     $column = $opt{token}->{column};
6676     }
6677     warn "Parse error ($opt{type}) at line $line column $column\n";
6678 wakaba 1.3 };
6679     $p->{parse_error} = sub {
6680 wakaba 1.119 $ponerror->(line => $p->{line}, column => $p->{column}, @_);
6681 wakaba 1.3 };
6682    
6683 wakaba 1.174 my $char_onerror = sub {
6684     my (undef, $type, %opt) = @_;
6685     $ponerror->(layer => 'encode',
6686     line => $p->{line}, column => $p->{column} + 1,
6687     %opt, type => $type);
6688     }; # $char_onerror
6689     $input->onerror ($char_onerror);
6690    
6691 wakaba 1.3 $p->_initialize_tokenizer;
6692     $p->_initialize_tree_constructor;
6693    
6694 wakaba 1.213 ## F4. If /context/ is not undef...
6695    
6696     ## F4.1. content model flag
6697 wakaba 1.71 my $node_ln = $node->manakai_local_name;
6698 wakaba 1.41 $p->{content_model} = {
6699     title => RCDATA_CONTENT_MODEL,
6700     textarea => RCDATA_CONTENT_MODEL,
6701     style => CDATA_CONTENT_MODEL,
6702     script => CDATA_CONTENT_MODEL,
6703     xmp => CDATA_CONTENT_MODEL,
6704     iframe => CDATA_CONTENT_MODEL,
6705     noembed => CDATA_CONTENT_MODEL,
6706     noframes => CDATA_CONTENT_MODEL,
6707     noscript => CDATA_CONTENT_MODEL,
6708     plaintext => PLAINTEXT_CONTENT_MODEL,
6709     }->{$node_ln};
6710     $p->{content_model} = PCDATA_CONTENT_MODEL
6711     unless defined $p->{content_model};
6712 wakaba 1.3
6713 wakaba 1.121 $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
6714     ## TODO: Foreign element OK?
6715 wakaba 1.3
6716 wakaba 1.213 ## F4.2. Root |html| element
6717 wakaba 1.3 my $root = $doc->create_element_ns
6718     ('http://www.w3.org/1999/xhtml', [undef, 'html']);
6719    
6720 wakaba 1.213 ## F4.3.
6721 wakaba 1.3 $doc->append_child ($root);
6722    
6723 wakaba 1.213 ## F4.4.
6724 wakaba 1.121 push @{$p->{open_elements}}, [$root, $el_category->{html}];
6725 wakaba 1.3
6726     undef $p->{head_element};
6727 wakaba 1.197 undef $p->{head_element_inserted};
6728 wakaba 1.3
6729 wakaba 1.213 ## F4.5.
6730 wakaba 1.3 $p->_reset_insertion_mode;
6731    
6732 wakaba 1.213 ## F4.6.
6733 wakaba 1.3 my $anode = $node;
6734     AN: while (defined $anode) {
6735     if ($anode->node_type == 1) {
6736     my $nsuri = $anode->namespace_uri;
6737     if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
6738 wakaba 1.71 if ($anode->manakai_local_name eq 'form') {
6739 wakaba 1.79
6740 wakaba 1.3 $p->{form_element} = $anode;
6741     last AN;
6742     }
6743     }
6744     }
6745     $anode = $anode->parent_node;
6746     } # AN
6747 wakaba 1.213
6748     ## F.6. Start the parser.
6749 wakaba 1.3 {
6750     my $self = $p;
6751     $token = $self->_get_next_token;
6752     }
6753     $p->_tree_construction_main;
6754    
6755 wakaba 1.213 ## F.7.
6756 wakaba 1.3 my @cn = @{$node->child_nodes};
6757     for (@cn) {
6758     $node->remove_child ($_);
6759     }
6760     ## ISSUE: mutation events? read-only?
6761    
6762 wakaba 1.84 ## Step 11 # MUST
6763 wakaba 1.3 @cn = @{$root->child_nodes};
6764     for (@cn) {
6765 wakaba 1.14 $this_doc->adopt_node ($_);
6766 wakaba 1.3 $node->append_child ($_);
6767     }
6768 wakaba 1.14 ## ISSUE: mutation events?
6769 wakaba 1.3
6770     $p->_terminate_tree_constructor;
6771 wakaba 1.119
6772     delete $p->{parse_error}; # delete loop
6773 wakaba 1.3 } else {
6774     die "$0: |set_inner_html| is not defined for node of type $nt";
6775     }
6776     } # set_inner_html
6777    
6778     } # tree construction stage
6779 wakaba 1.1
6780 wakaba 1.65 package Whatpm::HTML::RestartParser;
6781     push our @ISA, 'Error';
6782    
6783 wakaba 1.1 1;
6784 wakaba 1.217 # $Date: 2009/07/26 09:11:18 $

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24