/[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.232 - (hide annotations) (download)
Sun Sep 6 08:29:32 2009 UTC (16 years, 11 months ago) by wakaba
Branch: MAIN
Changes since 1.231: +7 -4 lines
++ whatpm/t/ChangeLog	6 Sep 2009 08:29:19 -0000
	* tree-test-flow.dat: Added some test cases on the |hgroup|
	element (HTML5 revision 3040).

2009-09-06  Wakaba  <wakaba@suika.fam.cx>

++ whatpm/Whatpm/ChangeLog	6 Sep 2009 08:27:35 -0000
	* HTML.pm.src: Added the |hgroup| element (HTML5 revision 3039 and
	HTML5 revision 3040).

2009-09-06  Wakaba  <wakaba@suika.fam.cx>

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24