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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.166 - (show annotations) (download) (as text)
Sat Sep 13 08:21:35 2008 UTC (18 years ago) by wakaba
Branch: MAIN
Changes since 1.165: +91 -78 lines
File MIME type: application/x-wais-source
++ whatpm/Whatpm/ChangeLog	13 Sep 2008 08:21:31 -0000
	* HTML.pm: Make |PUBLIC| and |SYSTEM| keyword tokenizing
	into their own tokenizer states.

2008-09-13  Wakaba  <wakaba@suika.fam.cx>

1 package Whatpm::HTML;
2 use strict;
3 our $VERSION=do{my @r=(q$Revision: 1.165 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4 use Error qw(:try);
5
6 ## ISSUE:
7 ## var doc = implementation.createDocument (null, null, null);
8 ## doc.write ('');
9 ## alert (doc.compatMode);
10
11 require IO::Handle;
12
13 my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
14 my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
15 my $SVG_NS = q<http://www.w3.org/2000/svg>;
16 my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
17 my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
18 my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
19
20 sub A_EL () { 0b1 }
21 sub ADDRESS_EL () { 0b10 }
22 sub BODY_EL () { 0b100 }
23 sub BUTTON_EL () { 0b1000 }
24 sub CAPTION_EL () { 0b10000 }
25 sub DD_EL () { 0b100000 }
26 sub DIV_EL () { 0b1000000 }
27 sub DT_EL () { 0b10000000 }
28 sub FORM_EL () { 0b100000000 }
29 sub FORMATTING_EL () { 0b1000000000 }
30 sub FRAMESET_EL () { 0b10000000000 }
31 sub HEADING_EL () { 0b100000000000 }
32 sub HTML_EL () { 0b1000000000000 }
33 sub LI_EL () { 0b10000000000000 }
34 sub NOBR_EL () { 0b100000000000000 }
35 sub OPTION_EL () { 0b1000000000000000 }
36 sub OPTGROUP_EL () { 0b10000000000000000 }
37 sub P_EL () { 0b100000000000000000 }
38 sub SELECT_EL () { 0b1000000000000000000 }
39 sub TABLE_EL () { 0b10000000000000000000 }
40 sub TABLE_CELL_EL () { 0b100000000000000000000 }
41 sub TABLE_ROW_EL () { 0b1000000000000000000000 }
42 sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
43 sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
44 sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
45 sub FOREIGN_EL () { 0b10000000000000000000000000 }
46 sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
47 sub MML_AXML_EL () { 0b1000000000000000000000000000 }
48 sub RUBY_EL () { 0b10000000000000000000000000000 }
49 sub RUBY_COMPONENT_EL () { 0b100000000000000000000000000000 }
50
51 sub TABLE_ROWS_EL () {
52 TABLE_EL |
53 TABLE_ROW_EL |
54 TABLE_ROW_GROUP_EL
55 }
56
57 ## NOTE: Used in "generate implied end tags" algorithm.
58 ## NOTE: There is a code where a modified version of END_TAG_OPTIONAL_EL
59 ## is used in "generate implied end tags" implementation (search for the
60 ## function mae).
61 sub END_TAG_OPTIONAL_EL () {
62 DD_EL |
63 DT_EL |
64 LI_EL |
65 P_EL |
66 RUBY_COMPONENT_EL
67 }
68
69 ## NOTE: Used in </body> and EOF algorithms.
70 sub ALL_END_TAG_OPTIONAL_EL () {
71 DD_EL |
72 DT_EL |
73 LI_EL |
74 P_EL |
75
76 BODY_EL |
77 HTML_EL |
78 TABLE_CELL_EL |
79 TABLE_ROW_EL |
80 TABLE_ROW_GROUP_EL
81 }
82
83 sub SCOPING_EL () {
84 BUTTON_EL |
85 CAPTION_EL |
86 HTML_EL |
87 TABLE_EL |
88 TABLE_CELL_EL |
89 MISC_SCOPING_EL
90 }
91
92 sub TABLE_SCOPING_EL () {
93 HTML_EL |
94 TABLE_EL
95 }
96
97 sub TABLE_ROWS_SCOPING_EL () {
98 HTML_EL |
99 TABLE_ROW_GROUP_EL
100 }
101
102 sub TABLE_ROW_SCOPING_EL () {
103 HTML_EL |
104 TABLE_ROW_EL
105 }
106
107 sub SPECIAL_EL () {
108 ADDRESS_EL |
109 BODY_EL |
110 DIV_EL |
111
112 DD_EL |
113 DT_EL |
114 LI_EL |
115 P_EL |
116
117 FORM_EL |
118 FRAMESET_EL |
119 HEADING_EL |
120 OPTION_EL |
121 OPTGROUP_EL |
122 SELECT_EL |
123 TABLE_ROW_EL |
124 TABLE_ROW_GROUP_EL |
125 MISC_SPECIAL_EL
126 }
127
128 my $el_category = {
129 a => A_EL | FORMATTING_EL,
130 address => ADDRESS_EL,
131 applet => MISC_SCOPING_EL,
132 area => MISC_SPECIAL_EL,
133 b => FORMATTING_EL,
134 base => MISC_SPECIAL_EL,
135 basefont => MISC_SPECIAL_EL,
136 bgsound => MISC_SPECIAL_EL,
137 big => FORMATTING_EL,
138 blockquote => MISC_SPECIAL_EL,
139 body => BODY_EL,
140 br => MISC_SPECIAL_EL,
141 button => BUTTON_EL,
142 caption => CAPTION_EL,
143 center => MISC_SPECIAL_EL,
144 col => MISC_SPECIAL_EL,
145 colgroup => MISC_SPECIAL_EL,
146 dd => DD_EL,
147 dir => MISC_SPECIAL_EL,
148 div => DIV_EL,
149 dl => MISC_SPECIAL_EL,
150 dt => DT_EL,
151 em => FORMATTING_EL,
152 embed => MISC_SPECIAL_EL,
153 fieldset => MISC_SPECIAL_EL,
154 font => FORMATTING_EL,
155 form => FORM_EL,
156 frame => MISC_SPECIAL_EL,
157 frameset => FRAMESET_EL,
158 h1 => HEADING_EL,
159 h2 => HEADING_EL,
160 h3 => HEADING_EL,
161 h4 => HEADING_EL,
162 h5 => HEADING_EL,
163 h6 => HEADING_EL,
164 head => MISC_SPECIAL_EL,
165 hr => MISC_SPECIAL_EL,
166 html => HTML_EL,
167 i => FORMATTING_EL,
168 iframe => MISC_SPECIAL_EL,
169 img => MISC_SPECIAL_EL,
170 input => MISC_SPECIAL_EL,
171 isindex => MISC_SPECIAL_EL,
172 li => LI_EL,
173 link => MISC_SPECIAL_EL,
174 listing => MISC_SPECIAL_EL,
175 marquee => MISC_SCOPING_EL,
176 menu => MISC_SPECIAL_EL,
177 meta => MISC_SPECIAL_EL,
178 nobr => NOBR_EL | FORMATTING_EL,
179 noembed => MISC_SPECIAL_EL,
180 noframes => MISC_SPECIAL_EL,
181 noscript => MISC_SPECIAL_EL,
182 object => MISC_SCOPING_EL,
183 ol => MISC_SPECIAL_EL,
184 optgroup => OPTGROUP_EL,
185 option => OPTION_EL,
186 p => P_EL,
187 param => MISC_SPECIAL_EL,
188 plaintext => MISC_SPECIAL_EL,
189 pre => MISC_SPECIAL_EL,
190 rp => RUBY_COMPONENT_EL,
191 rt => RUBY_COMPONENT_EL,
192 ruby => RUBY_EL,
193 s => FORMATTING_EL,
194 script => MISC_SPECIAL_EL,
195 select => SELECT_EL,
196 small => FORMATTING_EL,
197 spacer => MISC_SPECIAL_EL,
198 strike => FORMATTING_EL,
199 strong => FORMATTING_EL,
200 style => MISC_SPECIAL_EL,
201 table => TABLE_EL,
202 tbody => TABLE_ROW_GROUP_EL,
203 td => TABLE_CELL_EL,
204 textarea => MISC_SPECIAL_EL,
205 tfoot => TABLE_ROW_GROUP_EL,
206 th => TABLE_CELL_EL,
207 thead => TABLE_ROW_GROUP_EL,
208 title => MISC_SPECIAL_EL,
209 tr => TABLE_ROW_EL,
210 tt => FORMATTING_EL,
211 u => FORMATTING_EL,
212 ul => MISC_SPECIAL_EL,
213 wbr => MISC_SPECIAL_EL,
214 };
215
216 my $el_category_f = {
217 $MML_NS => {
218 'annotation-xml' => MML_AXML_EL,
219 mi => FOREIGN_FLOW_CONTENT_EL,
220 mo => FOREIGN_FLOW_CONTENT_EL,
221 mn => FOREIGN_FLOW_CONTENT_EL,
222 ms => FOREIGN_FLOW_CONTENT_EL,
223 mtext => FOREIGN_FLOW_CONTENT_EL,
224 },
225 $SVG_NS => {
226 foreignObject => FOREIGN_FLOW_CONTENT_EL,
227 desc => FOREIGN_FLOW_CONTENT_EL,
228 title => FOREIGN_FLOW_CONTENT_EL,
229 },
230 ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
231 };
232
233 my $svg_attr_name = {
234 attributename => 'attributeName',
235 attributetype => 'attributeType',
236 basefrequency => 'baseFrequency',
237 baseprofile => 'baseProfile',
238 calcmode => 'calcMode',
239 clippathunits => 'clipPathUnits',
240 contentscripttype => 'contentScriptType',
241 contentstyletype => 'contentStyleType',
242 diffuseconstant => 'diffuseConstant',
243 edgemode => 'edgeMode',
244 externalresourcesrequired => 'externalResourcesRequired',
245 filterres => 'filterRes',
246 filterunits => 'filterUnits',
247 glyphref => 'glyphRef',
248 gradienttransform => 'gradientTransform',
249 gradientunits => 'gradientUnits',
250 kernelmatrix => 'kernelMatrix',
251 kernelunitlength => 'kernelUnitLength',
252 keypoints => 'keyPoints',
253 keysplines => 'keySplines',
254 keytimes => 'keyTimes',
255 lengthadjust => 'lengthAdjust',
256 limitingconeangle => 'limitingConeAngle',
257 markerheight => 'markerHeight',
258 markerunits => 'markerUnits',
259 markerwidth => 'markerWidth',
260 maskcontentunits => 'maskContentUnits',
261 maskunits => 'maskUnits',
262 numoctaves => 'numOctaves',
263 pathlength => 'pathLength',
264 patterncontentunits => 'patternContentUnits',
265 patterntransform => 'patternTransform',
266 patternunits => 'patternUnits',
267 pointsatx => 'pointsAtX',
268 pointsaty => 'pointsAtY',
269 pointsatz => 'pointsAtZ',
270 preservealpha => 'preserveAlpha',
271 preserveaspectratio => 'preserveAspectRatio',
272 primitiveunits => 'primitiveUnits',
273 refx => 'refX',
274 refy => 'refY',
275 repeatcount => 'repeatCount',
276 repeatdur => 'repeatDur',
277 requiredextensions => 'requiredExtensions',
278 requiredfeatures => 'requiredFeatures',
279 specularconstant => 'specularConstant',
280 specularexponent => 'specularExponent',
281 spreadmethod => 'spreadMethod',
282 startoffset => 'startOffset',
283 stddeviation => 'stdDeviation',
284 stitchtiles => 'stitchTiles',
285 surfacescale => 'surfaceScale',
286 systemlanguage => 'systemLanguage',
287 tablevalues => 'tableValues',
288 targetx => 'targetX',
289 targety => 'targetY',
290 textlength => 'textLength',
291 viewbox => 'viewBox',
292 viewtarget => 'viewTarget',
293 xchannelselector => 'xChannelSelector',
294 ychannelselector => 'yChannelSelector',
295 zoomandpan => 'zoomAndPan',
296 };
297
298 my $foreign_attr_xname = {
299 'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
300 'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
301 'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
302 'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
303 'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
304 'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
305 'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
306 'xml:base' => [$XML_NS, ['xml', 'base']],
307 'xml:lang' => [$XML_NS, ['xml', 'lang']],
308 'xml:space' => [$XML_NS, ['xml', 'space']],
309 'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
310 'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
311 };
312
313 ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
314
315 my $c1_entity_char = {
316 0x80 => 0x20AC,
317 0x81 => 0xFFFD,
318 0x82 => 0x201A,
319 0x83 => 0x0192,
320 0x84 => 0x201E,
321 0x85 => 0x2026,
322 0x86 => 0x2020,
323 0x87 => 0x2021,
324 0x88 => 0x02C6,
325 0x89 => 0x2030,
326 0x8A => 0x0160,
327 0x8B => 0x2039,
328 0x8C => 0x0152,
329 0x8D => 0xFFFD,
330 0x8E => 0x017D,
331 0x8F => 0xFFFD,
332 0x90 => 0xFFFD,
333 0x91 => 0x2018,
334 0x92 => 0x2019,
335 0x93 => 0x201C,
336 0x94 => 0x201D,
337 0x95 => 0x2022,
338 0x96 => 0x2013,
339 0x97 => 0x2014,
340 0x98 => 0x02DC,
341 0x99 => 0x2122,
342 0x9A => 0x0161,
343 0x9B => 0x203A,
344 0x9C => 0x0153,
345 0x9D => 0xFFFD,
346 0x9E => 0x017E,
347 0x9F => 0x0178,
348 }; # $c1_entity_char
349
350 sub parse_byte_string ($$$$;$) {
351 my $self = shift;
352 my $charset_name = shift;
353 open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
354 return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
355 } # parse_byte_string
356
357 sub parse_byte_stream ($$$$;$$) {
358 # my ($self, $charset_name, $byte_stream, $doc, $onerror, $get_wrapper) = @_;
359 my $self = ref $_[0] ? shift : shift->new;
360 my $charset_name = shift;
361 my $byte_stream = $_[0];
362
363 my $onerror = $_[2] || sub {
364 my (%opt) = @_;
365 warn "Parse error ($opt{type})\n";
366 };
367 $self->{parse_error} = $onerror; # updated later by parse_char_string
368
369 my $get_wrapper = $_[3] || sub ($) {
370 return $_[0]; # $_[0] = byte stream handle, returned = arg to char handle
371 };
372
373 ## HTML5 encoding sniffing algorithm
374 require Message::Charset::Info;
375 my $charset;
376 my $buffer;
377 my ($char_stream, $e_status);
378
379 SNIFFING: {
380 ## NOTE: By setting |allow_fallback| option true when the
381 ## |get_decode_handle| method is invoked, we ignore what the HTML5
382 ## spec requires, i.e. unsupported encoding should be ignored.
383 ## TODO: We should not do this unless the parser is invoked
384 ## in the conformance checking mode, in which this behavior
385 ## would be useful.
386
387 ## Step 1
388 if (defined $charset_name) {
389 $charset = Message::Charset::Info->get_by_html_name ($charset_name);
390 ## TODO: Is this ok? Transfer protocol's parameter should be
391 ## interpreted in its semantics?
392
393 ## ISSUE: Unsupported encoding is not ignored according to the spec.
394 ($char_stream, $e_status) = $charset->get_decode_handle
395 ($byte_stream, allow_error_reporting => 1,
396 allow_fallback => 1);
397 if ($char_stream) {
398 $self->{confident} = 1;
399 last SNIFFING;
400 } else {
401 ## TODO: unsupported error
402 }
403 }
404
405 ## Step 2
406 my $byte_buffer = '';
407 for (1..1024) {
408 my $char = $byte_stream->getc;
409 last unless defined $char;
410 $byte_buffer .= $char;
411 } ## TODO: timeout
412
413 ## Step 3
414 if ($byte_buffer =~ /^\xFE\xFF/) {
415 $charset = Message::Charset::Info->get_by_html_name ('utf-16be');
416 ($char_stream, $e_status) = $charset->get_decode_handle
417 ($byte_stream, allow_error_reporting => 1,
418 allow_fallback => 1, byte_buffer => \$byte_buffer);
419 $self->{confident} = 1;
420 last SNIFFING;
421 } elsif ($byte_buffer =~ /^\xFF\xFE/) {
422 $charset = Message::Charset::Info->get_by_html_name ('utf-16le');
423 ($char_stream, $e_status) = $charset->get_decode_handle
424 ($byte_stream, allow_error_reporting => 1,
425 allow_fallback => 1, byte_buffer => \$byte_buffer);
426 $self->{confident} = 1;
427 last SNIFFING;
428 } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
429 $charset = Message::Charset::Info->get_by_html_name ('utf-8');
430 ($char_stream, $e_status) = $charset->get_decode_handle
431 ($byte_stream, allow_error_reporting => 1,
432 allow_fallback => 1, byte_buffer => \$byte_buffer);
433 $self->{confident} = 1;
434 last SNIFFING;
435 }
436
437 ## Step 4
438 ## TODO: <meta charset>
439
440 ## Step 5
441 ## TODO: from history
442
443 ## Step 6
444 require Whatpm::Charset::UniversalCharDet;
445 $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
446 ($byte_buffer);
447 if (defined $charset_name) {
448 $charset = Message::Charset::Info->get_by_html_name ($charset_name);
449
450 ## ISSUE: Unsupported encoding is not ignored according to the spec.
451 require Whatpm::Charset::DecodeHandle;
452 $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
453 ($byte_stream);
454 ($char_stream, $e_status) = $charset->get_decode_handle
455 ($buffer, allow_error_reporting => 1,
456 allow_fallback => 1, byte_buffer => \$byte_buffer);
457 if ($char_stream) {
458 $buffer->{buffer} = $byte_buffer;
459 !!!parse-error (type => 'sniffing:chardet',
460 text => $charset_name,
461 level => $self->{level}->{info},
462 layer => 'encode',
463 line => 1, column => 1);
464 $self->{confident} = 0;
465 last SNIFFING;
466 }
467 }
468
469 ## Step 7: default
470 ## TODO: Make this configurable.
471 $charset = Message::Charset::Info->get_by_html_name ('windows-1252');
472 ## NOTE: We choose |windows-1252| here, since |utf-8| should be
473 ## detectable in the step 6.
474 require Whatpm::Charset::DecodeHandle;
475 $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
476 ($byte_stream);
477 ($char_stream, $e_status)
478 = $charset->get_decode_handle ($buffer,
479 allow_error_reporting => 1,
480 allow_fallback => 1,
481 byte_buffer => \$byte_buffer);
482 $buffer->{buffer} = $byte_buffer;
483 !!!parse-error (type => 'sniffing:default',
484 text => 'windows-1252',
485 level => $self->{level}->{info},
486 line => 1, column => 1,
487 layer => 'encode');
488 $self->{confident} = 0;
489 } # SNIFFING
490
491 if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
492 $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
493 !!!parse-error (type => 'chardecode:fallback',
494 #text => $self->{input_encoding},
495 level => $self->{level}->{uncertain},
496 line => 1, column => 1,
497 layer => 'encode');
498 } elsif (not ($e_status &
499 Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
500 $self->{input_encoding} = $charset->get_iana_name;
501 !!!parse-error (type => 'chardecode:no error',
502 text => $self->{input_encoding},
503 level => $self->{level}->{uncertain},
504 line => 1, column => 1,
505 layer => 'encode');
506 } else {
507 $self->{input_encoding} = $charset->get_iana_name;
508 }
509
510 $self->{change_encoding} = sub {
511 my $self = shift;
512 $charset_name = shift;
513 my $token = shift;
514
515 $charset = Message::Charset::Info->get_by_html_name ($charset_name);
516 ($char_stream, $e_status) = $charset->get_decode_handle
517 ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
518 byte_buffer => \ $buffer->{buffer});
519
520 if ($char_stream) { # if supported
521 ## "Change the encoding" algorithm:
522
523 ## Step 1
524 if ($charset->{category} &
525 Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
526 $charset = Message::Charset::Info->get_by_html_name ('utf-8');
527 ($char_stream, $e_status) = $charset->get_decode_handle
528 ($byte_stream,
529 byte_buffer => \ $buffer->{buffer});
530 }
531 $charset_name = $charset->get_iana_name;
532
533 ## Step 2
534 if (defined $self->{input_encoding} and
535 $self->{input_encoding} eq $charset_name) {
536 !!!parse-error (type => 'charset label:matching',
537 text => $charset_name,
538 level => $self->{level}->{info});
539 $self->{confident} = 1;
540 return;
541 }
542
543 !!!parse-error (type => 'charset label detected',
544 text => $self->{input_encoding},
545 value => $charset_name,
546 level => $self->{level}->{warn},
547 token => $token);
548
549 ## Step 3
550 # if (can) {
551 ## change the encoding on the fly.
552 #$self->{confident} = 1;
553 #return;
554 # }
555
556 ## Step 4
557 throw Whatpm::HTML::RestartParser ();
558 }
559 }; # $self->{change_encoding}
560
561 my $char_onerror = sub {
562 my (undef, $type, %opt) = @_;
563 !!!parse-error (layer => 'encode',
564 %opt, type => $type,
565 line => $self->{line}, column => $self->{column} + 1);
566 if ($opt{octets}) {
567 ${$opt{octets}} = "\x{FFFD}"; # relacement character
568 }
569 };
570
571 my $wrapped_char_stream = $get_wrapper->($char_stream);
572 $wrapped_char_stream->onerror ($char_onerror);
573
574 my @args = @_; shift @args; # $s
575 my $return;
576 try {
577 $return = $self->parse_char_stream ($wrapped_char_stream, @args);
578 } catch Whatpm::HTML::RestartParser with {
579 ## NOTE: Invoked after {change_encoding}.
580
581 if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
582 $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
583 !!!parse-error (type => 'chardecode:fallback',
584 level => $self->{level}->{uncertain},
585 #text => $self->{input_encoding},
586 line => 1, column => 1,
587 layer => 'encode');
588 } elsif (not ($e_status &
589 Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
590 $self->{input_encoding} = $charset->get_iana_name;
591 !!!parse-error (type => 'chardecode:no error',
592 text => $self->{input_encoding},
593 level => $self->{level}->{uncertain},
594 line => 1, column => 1,
595 layer => 'encode');
596 } else {
597 $self->{input_encoding} = $charset->get_iana_name;
598 }
599 $self->{confident} = 1;
600
601 $wrapped_char_stream = $get_wrapper->($char_stream);
602 $wrapped_char_stream->onerror ($char_onerror);
603
604 $return = $self->parse_char_stream ($wrapped_char_stream, @args);
605 };
606 return $return;
607 } # parse_byte_stream
608
609 ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
610 ## and the HTML layer MUST ignore it. However, we does strip BOM in
611 ## the encoding layer and the HTML layer does not ignore any U+FEFF,
612 ## because the core part of our HTML parser expects a string of character,
613 ## not a string of bytes or code units or anything which might contain a BOM.
614 ## Therefore, any parser interface that accepts a string of bytes,
615 ## such as |parse_byte_string| in this module, must ensure that it does
616 ## strip the BOM and never strip any ZWNBSP.
617
618 sub parse_char_string ($$$;$$) {
619 #my ($self, $s, $doc, $onerror, $get_wrapper) = @_;
620 my $self = shift;
621 require utf8;
622 my $s = ref $_[0] ? $_[0] : \($_[0]);
623 open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;
624 if ($_[3]) {
625 $input = $_[3]->($input);
626 }
627 return $self->parse_char_stream ($input, @_[1..$#_]);
628 } # parse_char_string
629 *parse_string = \&parse_char_string; ## NOTE: Alias for backward compatibility.
630
631 sub parse_char_stream ($$$;$) {
632 my $self = ref $_[0] ? shift : shift->new;
633 my $input = $_[0];
634 $self->{document} = $_[1];
635 @{$self->{document}->child_nodes} = ();
636
637 ## NOTE: |set_inner_html| copies most of this method's code
638
639 $self->{confident} = 1 unless exists $self->{confident};
640 $self->{document}->input_encoding ($self->{input_encoding})
641 if defined $self->{input_encoding};
642
643 my $i = 0;
644 $self->{line_prev} = $self->{line} = 1;
645 $self->{column_prev} = $self->{column} = 0;
646 $self->{set_next_char} = sub {
647 my $self = shift;
648
649 pop @{$self->{prev_char}};
650 unshift @{$self->{prev_char}}, $self->{next_char};
651
652 my $char;
653 if (defined $self->{next_next_char}) {
654 $char = $self->{next_next_char};
655 delete $self->{next_next_char};
656 } else {
657 $char = $input->getc;
658 }
659 $self->{next_char} = -1 and return unless defined $char;
660 $self->{next_char} = ord $char;
661
662 ($self->{line_prev}, $self->{column_prev})
663 = ($self->{line}, $self->{column});
664 $self->{column}++;
665
666 if ($self->{next_char} == 0x000A) { # LF
667 !!!cp ('j1');
668 $self->{line}++;
669 $self->{column} = 0;
670 } elsif ($self->{next_char} == 0x000D) { # CR
671 !!!cp ('j2');
672 my $next = $input->getc;
673 if (defined $next and $next ne "\x0A") {
674 $self->{next_next_char} = $next;
675 }
676 $self->{next_char} = 0x000A; # LF # MUST
677 $self->{line}++;
678 $self->{column} = 0;
679 } elsif ($self->{next_char} > 0x10FFFF) {
680 !!!cp ('j3');
681 $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
682 } elsif ($self->{next_char} == 0x0000) { # NULL
683 !!!cp ('j4');
684 !!!parse-error (type => 'NULL');
685 $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
686 } elsif ($self->{next_char} <= 0x0008 or
687 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
688 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
689 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
690 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
691 {
692 0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
693 0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
694 0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
695 0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
696 0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
697 0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
698 0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
699 0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
700 0x10FFFE => 1, 0x10FFFF => 1,
701 }->{$self->{next_char}}) {
702 !!!cp ('j5');
703 if ($self->{next_char} < 0x10000) {
704 !!!parse-error (type => 'control char',
705 text => (sprintf 'U+%04X', $self->{next_char}));
706 } else {
707 !!!parse-error (type => 'control char',
708 text => (sprintf 'U-%08X', $self->{next_char}));
709 }
710 }
711 };
712 $self->{prev_char} = [-1, -1, -1];
713 $self->{next_char} = -1;
714
715 my $onerror = $_[2] || sub {
716 my (%opt) = @_;
717 my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
718 my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
719 warn "Parse error ($opt{type}) at line $line column $column\n";
720 };
721 $self->{parse_error} = sub {
722 $onerror->(line => $self->{line}, column => $self->{column}, @_);
723 };
724
725 $self->_initialize_tokenizer;
726 $self->_initialize_tree_constructor;
727 $self->_construct_tree;
728 $self->_terminate_tree_constructor;
729
730 delete $self->{parse_error}; # remove loop
731
732 return $self->{document};
733 } # parse_char_stream
734
735 sub new ($) {
736 my $class = shift;
737 my $self = bless {
738 level => {must => 'm',
739 should => 's',
740 warn => 'w',
741 info => 'i',
742 uncertain => 'u'},
743 }, $class;
744 $self->{set_next_char} = sub {
745 $self->{next_char} = -1;
746 };
747 $self->{parse_error} = sub {
748 #
749 };
750 $self->{change_encoding} = sub {
751 # if ($_[0] is a supported encoding) {
752 # run "change the encoding" algorithm;
753 # throw Whatpm::HTML::RestartParser (charset => $new_encoding);
754 # }
755 };
756 $self->{application_cache_selection} = sub {
757 #
758 };
759 return $self;
760 } # new
761
762 sub CM_ENTITY () { 0b001 } # & markup in data
763 sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
764 sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
765
766 sub PLAINTEXT_CONTENT_MODEL () { 0 }
767 sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
768 sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
769 sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
770
771 sub DATA_STATE () { 0 }
772 sub ENTITY_DATA_STATE () { 1 }
773 sub TAG_OPEN_STATE () { 2 }
774 sub CLOSE_TAG_OPEN_STATE () { 3 }
775 sub TAG_NAME_STATE () { 4 }
776 sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
777 sub ATTRIBUTE_NAME_STATE () { 6 }
778 sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
779 sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
780 sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
781 sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
782 sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
783 sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
784 sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
785 sub COMMENT_START_STATE () { 14 }
786 sub COMMENT_START_DASH_STATE () { 15 }
787 sub COMMENT_STATE () { 16 }
788 sub COMMENT_END_STATE () { 17 }
789 sub COMMENT_END_DASH_STATE () { 18 }
790 sub BOGUS_COMMENT_STATE () { 19 }
791 sub DOCTYPE_STATE () { 20 }
792 sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
793 sub DOCTYPE_NAME_STATE () { 22 }
794 sub AFTER_DOCTYPE_NAME_STATE () { 23 }
795 sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
796 sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
797 sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
798 sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
799 sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
800 sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
801 sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
802 sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
803 sub BOGUS_DOCTYPE_STATE () { 32 }
804 sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
805 sub SELF_CLOSING_START_TAG_STATE () { 34 }
806 sub CDATA_SECTION_STATE () { 35 }
807 sub MD_HYPHEN_STATE () { 36 } # "markup declaration open state" in the spec
808 sub MD_DOCTYPE_STATE () { 37 } # "markup declaration open state" in the spec
809 sub MD_CDATA_STATE () { 38 } # "markup declaration open state" in the spec
810 sub CDATA_PCDATA_CLOSE_TAG_STATE () { 39 } # "close tag open state" in the spec
811 sub CDATA_SECTION_MSE1_STATE () { 40 } # "CDATA section state" in the spec
812 sub CDATA_SECTION_MSE2_STATE () { 41 } # "CDATA section state" in the spec
813 sub PUBLIC_STATE () { 42 } # "after DOCTYPE name state" in the spec
814 sub SYSTEM_STATE () { 43 } # "after DOCTYPE name state" in the spec
815
816 sub DOCTYPE_TOKEN () { 1 }
817 sub COMMENT_TOKEN () { 2 }
818 sub START_TAG_TOKEN () { 3 }
819 sub END_TAG_TOKEN () { 4 }
820 sub END_OF_FILE_TOKEN () { 5 }
821 sub CHARACTER_TOKEN () { 6 }
822
823 sub AFTER_HTML_IMS () { 0b100 }
824 sub HEAD_IMS () { 0b1000 }
825 sub BODY_IMS () { 0b10000 }
826 sub BODY_TABLE_IMS () { 0b100000 }
827 sub TABLE_IMS () { 0b1000000 }
828 sub ROW_IMS () { 0b10000000 }
829 sub BODY_AFTER_IMS () { 0b100000000 }
830 sub FRAME_IMS () { 0b1000000000 }
831 sub SELECT_IMS () { 0b10000000000 }
832 sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
833 ## NOTE: "in foreign content" insertion mode is special; it is combined
834 ## with the secondary insertion mode. In this parser, they are stored
835 ## together in the bit-or'ed form.
836
837 ## NOTE: "initial" and "before html" insertion modes have no constants.
838
839 ## NOTE: "after after body" insertion mode.
840 sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
841
842 ## NOTE: "after after frameset" insertion mode.
843 sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
844
845 sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
846 sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
847 sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
848 sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
849 sub IN_BODY_IM () { BODY_IMS }
850 sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
851 sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
852 sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
853 sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
854 sub IN_TABLE_IM () { TABLE_IMS }
855 sub AFTER_BODY_IM () { BODY_AFTER_IMS }
856 sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
857 sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
858 sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
859 sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
860 sub IN_COLUMN_GROUP_IM () { 0b10 }
861
862 ## Implementations MUST act as if state machine in the spec
863
864 sub _initialize_tokenizer ($) {
865 my $self = shift;
866 $self->{state} = DATA_STATE; # MUST
867 #$self->{state_keyword}; # initialized when used
868 $self->{content_model} = PCDATA_CONTENT_MODEL; # be
869 undef $self->{current_token};
870 undef $self->{current_attribute};
871 undef $self->{last_emitted_start_tag_name};
872 undef $self->{last_attribute_value_state};
873 delete $self->{self_closing};
874 $self->{char} = [];
875 # $self->{next_char}
876 !!!next-input-character;
877 $self->{token} = [];
878 # $self->{escape}
879 } # _initialize_tokenizer
880
881 ## A token has:
882 ## ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
883 ## CHARACTER_TOKEN, or END_OF_FILE_TOKEN
884 ## ->{name} (DOCTYPE_TOKEN)
885 ## ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
886 ## ->{public_identifier} (DOCTYPE_TOKEN)
887 ## ->{system_identifier} (DOCTYPE_TOKEN)
888 ## ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
889 ## ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
890 ## ->{name}
891 ## ->{value}
892 ## ->{has_reference} == 1 or 0
893 ## ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
894 ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
895 ## |->{self_closing}| is used to save the value of |$self->{self_closing}|
896 ## while the token is pushed back to the stack.
897
898 ## Emitted token MUST immediately be handled by the tree construction state.
899
900 ## Before each step, UA MAY check to see if either one of the scripts in
901 ## "list of scripts that will execute as soon as possible" or the first
902 ## script in the "list of scripts that will execute asynchronously",
903 ## has completed loading. If one has, then it MUST be executed
904 ## and removed from the list.
905
906 ## NOTE: HTML5 "Writing HTML documents" section, applied to
907 ## documents and not to user agents and conformance checkers,
908 ## contains some requirements that are not detected by the
909 ## parsing algorithm:
910 ## - Some requirements on character encoding declarations. ## TODO
911 ## - "Elements MUST NOT contain content that their content model disallows."
912 ## ... Some are parse error, some are not (will be reported by c.c.).
913 ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
914 ## - Text (in elements, attributes, and comments) SHOULD NOT contain
915 ## control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL? Unicode control character?)
916
917 ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
918 ## be detected by the HTML5 parsing algorithm:
919 ## - Text,
920
921 sub _get_next_token ($) {
922 my $self = shift;
923
924 if ($self->{self_closing}) {
925 !!!parse-error (type => 'nestc', token => $self->{current_token});
926 ## NOTE: The |self_closing| flag is only set by start tag token.
927 ## In addition, when a start tag token is emitted, it is always set to
928 ## |current_token|.
929 delete $self->{self_closing};
930 }
931
932 if (@{$self->{token}}) {
933 $self->{self_closing} = $self->{token}->[0]->{self_closing};
934 return shift @{$self->{token}};
935 }
936
937 A: {
938 if ($self->{state} == DATA_STATE) {
939 if ($self->{next_char} == 0x0026) { # &
940 if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
941 not $self->{escape}) {
942 !!!cp (1);
943 $self->{state} = ENTITY_DATA_STATE;
944 !!!next-input-character;
945 redo A;
946 } else {
947 !!!cp (2);
948 #
949 }
950 } elsif ($self->{next_char} == 0x002D) { # -
951 if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
952 unless ($self->{escape}) {
953 if ($self->{prev_char}->[0] == 0x002D and # -
954 $self->{prev_char}->[1] == 0x0021 and # !
955 $self->{prev_char}->[2] == 0x003C) { # <
956 !!!cp (3);
957 $self->{escape} = 1;
958 } else {
959 !!!cp (4);
960 }
961 } else {
962 !!!cp (5);
963 }
964 }
965
966 #
967 } elsif ($self->{next_char} == 0x003C) { # <
968 if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
969 (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
970 not $self->{escape})) {
971 !!!cp (6);
972 $self->{state} = TAG_OPEN_STATE;
973 !!!next-input-character;
974 redo A;
975 } else {
976 !!!cp (7);
977 #
978 }
979 } elsif ($self->{next_char} == 0x003E) { # >
980 if ($self->{escape} and
981 ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
982 if ($self->{prev_char}->[0] == 0x002D and # -
983 $self->{prev_char}->[1] == 0x002D) { # -
984 !!!cp (8);
985 delete $self->{escape};
986 } else {
987 !!!cp (9);
988 }
989 } else {
990 !!!cp (10);
991 }
992
993 #
994 } elsif ($self->{next_char} == -1) {
995 !!!cp (11);
996 !!!emit ({type => END_OF_FILE_TOKEN,
997 line => $self->{line}, column => $self->{column}});
998 last A; ## TODO: ok?
999 } else {
1000 !!!cp (12);
1001 }
1002 # Anything else
1003 my $token = {type => CHARACTER_TOKEN,
1004 data => chr $self->{next_char},
1005 line => $self->{line}, column => $self->{column},
1006 };
1007 ## Stay in the data state
1008 !!!next-input-character;
1009
1010 !!!emit ($token);
1011
1012 redo A;
1013 } elsif ($self->{state} == ENTITY_DATA_STATE) {
1014 ## (cannot happen in CDATA state)
1015
1016 my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
1017
1018 my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
1019
1020 $self->{state} = DATA_STATE;
1021 # next-input-character is already done
1022
1023 unless (defined $token) {
1024 !!!cp (13);
1025 !!!emit ({type => CHARACTER_TOKEN, data => '&',
1026 line => $l, column => $c,
1027 });
1028 } else {
1029 !!!cp (14);
1030 !!!emit ($token);
1031 }
1032
1033 redo A;
1034 } elsif ($self->{state} == TAG_OPEN_STATE) {
1035 if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1036 if ($self->{next_char} == 0x002F) { # /
1037 !!!cp (15);
1038 !!!next-input-character;
1039 $self->{state} = CLOSE_TAG_OPEN_STATE;
1040 redo A;
1041 } else {
1042 !!!cp (16);
1043 ## reconsume
1044 $self->{state} = DATA_STATE;
1045
1046 !!!emit ({type => CHARACTER_TOKEN, data => '<',
1047 line => $self->{line_prev},
1048 column => $self->{column_prev},
1049 });
1050
1051 redo A;
1052 }
1053 } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
1054 if ($self->{next_char} == 0x0021) { # !
1055 !!!cp (17);
1056 $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
1057 !!!next-input-character;
1058 redo A;
1059 } elsif ($self->{next_char} == 0x002F) { # /
1060 !!!cp (18);
1061 $self->{state} = CLOSE_TAG_OPEN_STATE;
1062 !!!next-input-character;
1063 redo A;
1064 } elsif (0x0041 <= $self->{next_char} and
1065 $self->{next_char} <= 0x005A) { # A..Z
1066 !!!cp (19);
1067 $self->{current_token}
1068 = {type => START_TAG_TOKEN,
1069 tag_name => chr ($self->{next_char} + 0x0020),
1070 line => $self->{line_prev},
1071 column => $self->{column_prev}};
1072 $self->{state} = TAG_NAME_STATE;
1073 !!!next-input-character;
1074 redo A;
1075 } elsif (0x0061 <= $self->{next_char} and
1076 $self->{next_char} <= 0x007A) { # a..z
1077 !!!cp (20);
1078 $self->{current_token} = {type => START_TAG_TOKEN,
1079 tag_name => chr ($self->{next_char}),
1080 line => $self->{line_prev},
1081 column => $self->{column_prev}};
1082 $self->{state} = TAG_NAME_STATE;
1083 !!!next-input-character;
1084 redo A;
1085 } elsif ($self->{next_char} == 0x003E) { # >
1086 !!!cp (21);
1087 !!!parse-error (type => 'empty start tag',
1088 line => $self->{line_prev},
1089 column => $self->{column_prev});
1090 $self->{state} = DATA_STATE;
1091 !!!next-input-character;
1092
1093 !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1094 line => $self->{line_prev},
1095 column => $self->{column_prev},
1096 });
1097
1098 redo A;
1099 } elsif ($self->{next_char} == 0x003F) { # ?
1100 !!!cp (22);
1101 !!!parse-error (type => 'pio',
1102 line => $self->{line_prev},
1103 column => $self->{column_prev});
1104 $self->{state} = BOGUS_COMMENT_STATE;
1105 $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1106 line => $self->{line_prev},
1107 column => $self->{column_prev},
1108 };
1109 ## $self->{next_char} is intentionally left as is
1110 redo A;
1111 } else {
1112 !!!cp (23);
1113 !!!parse-error (type => 'bare stago',
1114 line => $self->{line_prev},
1115 column => $self->{column_prev});
1116 $self->{state} = DATA_STATE;
1117 ## reconsume
1118
1119 !!!emit ({type => CHARACTER_TOKEN, data => '<',
1120 line => $self->{line_prev},
1121 column => $self->{column_prev},
1122 });
1123
1124 redo A;
1125 }
1126 } else {
1127 die "$0: $self->{content_model} in tag open";
1128 }
1129 } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1130 ## NOTE: The "close tag open state" in the spec is implemented as
1131 ## |CLOSE_TAG_OPEN_STATE| and |CDATA_PCDATA_CLOSE_TAG_STATE|.
1132
1133 my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1134 if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1135 if (defined $self->{last_emitted_start_tag_name}) {
1136 $self->{state} = CDATA_PCDATA_CLOSE_TAG_STATE;
1137 $self->{state_keyword} = '';
1138 ## Reconsume.
1139 redo A;
1140 } else {
1141 ## No start tag token has ever been emitted
1142 ## NOTE: See <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>.
1143 !!!cp (28);
1144 $self->{state} = DATA_STATE;
1145 ## Reconsume.
1146 !!!emit ({type => CHARACTER_TOKEN, data => '</',
1147 line => $l, column => $c,
1148 });
1149 redo A;
1150 }
1151 }
1152
1153 if (0x0041 <= $self->{next_char} and
1154 $self->{next_char} <= 0x005A) { # A..Z
1155 !!!cp (29);
1156 $self->{current_token}
1157 = {type => END_TAG_TOKEN,
1158 tag_name => chr ($self->{next_char} + 0x0020),
1159 line => $l, column => $c};
1160 $self->{state} = TAG_NAME_STATE;
1161 !!!next-input-character;
1162 redo A;
1163 } elsif (0x0061 <= $self->{next_char} and
1164 $self->{next_char} <= 0x007A) { # a..z
1165 !!!cp (30);
1166 $self->{current_token} = {type => END_TAG_TOKEN,
1167 tag_name => chr ($self->{next_char}),
1168 line => $l, column => $c};
1169 $self->{state} = TAG_NAME_STATE;
1170 !!!next-input-character;
1171 redo A;
1172 } elsif ($self->{next_char} == 0x003E) { # >
1173 !!!cp (31);
1174 !!!parse-error (type => 'empty end tag',
1175 line => $self->{line_prev}, ## "<" in "</>"
1176 column => $self->{column_prev} - 1);
1177 $self->{state} = DATA_STATE;
1178 !!!next-input-character;
1179 redo A;
1180 } elsif ($self->{next_char} == -1) {
1181 !!!cp (32);
1182 !!!parse-error (type => 'bare etago');
1183 $self->{state} = DATA_STATE;
1184 # reconsume
1185
1186 !!!emit ({type => CHARACTER_TOKEN, data => '</',
1187 line => $l, column => $c,
1188 });
1189
1190 redo A;
1191 } else {
1192 !!!cp (33);
1193 !!!parse-error (type => 'bogus end tag');
1194 $self->{state} = BOGUS_COMMENT_STATE;
1195 $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1196 line => $self->{line_prev}, # "<" of "</"
1197 column => $self->{column_prev} - 1,
1198 };
1199 ## NOTE: $self->{next_char} is intentionally left as is.
1200 ## Although the "anything else" case of the spec not explicitly
1201 ## states that the next input character is to be reconsumed,
1202 ## it will be included to the |data| of the comment token
1203 ## generated from the bogus end tag, as defined in the
1204 ## "bogus comment state" entry.
1205 redo A;
1206 }
1207 } elsif ($self->{state} == CDATA_PCDATA_CLOSE_TAG_STATE) {
1208 my $ch = substr $self->{last_emitted_start_tag_name}, length $self->{state_keyword}, 1;
1209 if (length $ch) {
1210 my $CH = $ch;
1211 $ch =~ tr/a-z/A-Z/;
1212 my $nch = chr $self->{next_char};
1213 if ($nch eq $ch or $nch eq $CH) {
1214 !!!cp (24);
1215 ## Stay in the state.
1216 $self->{state_keyword} .= $nch;
1217 !!!next-input-character;
1218 redo A;
1219 } else {
1220 !!!cp (25);
1221 $self->{state} = DATA_STATE;
1222 ## Reconsume.
1223 !!!emit ({type => CHARACTER_TOKEN,
1224 data => '</' . $self->{state_keyword},
1225 line => $self->{line_prev},
1226 column => $self->{column_prev} - 1 - length $self->{state_keyword},
1227 });
1228 redo A;
1229 }
1230 } else { # after "<{tag-name}"
1231 unless ({
1232 0x0009 => 1, # HT
1233 0x000A => 1, # LF
1234 0x000B => 1, # VT
1235 0x000C => 1, # FF
1236 0x0020 => 1, # SP
1237 0x003E => 1, # >
1238 0x002F => 1, # /
1239 -1 => 1, # EOF
1240 }->{$self->{next_char}}) {
1241 !!!cp (26);
1242 ## Reconsume.
1243 $self->{state} = DATA_STATE;
1244 !!!emit ({type => CHARACTER_TOKEN,
1245 data => '</' . $self->{state_keyword},
1246 line => $self->{line_prev},
1247 column => $self->{column_prev} - 1 - length $self->{state_keyword},
1248 });
1249 redo A;
1250 } else {
1251 !!!cp (27);
1252 $self->{current_token}
1253 = {type => END_TAG_TOKEN,
1254 tag_name => $self->{last_emitted_start_tag_name},
1255 line => $self->{line_prev},
1256 column => $self->{column_prev} - 1 - length $self->{state_keyword}};
1257 $self->{state} = TAG_NAME_STATE;
1258 ## Reconsume.
1259 redo A;
1260 }
1261 }
1262 } elsif ($self->{state} == TAG_NAME_STATE) {
1263 if ($self->{next_char} == 0x0009 or # HT
1264 $self->{next_char} == 0x000A or # LF
1265 $self->{next_char} == 0x000B or # VT
1266 $self->{next_char} == 0x000C or # FF
1267 $self->{next_char} == 0x0020) { # SP
1268 !!!cp (34);
1269 $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1270 !!!next-input-character;
1271 redo A;
1272 } elsif ($self->{next_char} == 0x003E) { # >
1273 if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1274 !!!cp (35);
1275 $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1276 } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1277 $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1278 #if ($self->{current_token}->{attributes}) {
1279 # ## NOTE: This should never be reached.
1280 # !!! cp (36);
1281 # !!! parse-error (type => 'end tag attribute');
1282 #} else {
1283 !!!cp (37);
1284 #}
1285 } else {
1286 die "$0: $self->{current_token}->{type}: Unknown token type";
1287 }
1288 $self->{state} = DATA_STATE;
1289 !!!next-input-character;
1290
1291 !!!emit ($self->{current_token}); # start tag or end tag
1292
1293 redo A;
1294 } elsif (0x0041 <= $self->{next_char} and
1295 $self->{next_char} <= 0x005A) { # A..Z
1296 !!!cp (38);
1297 $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
1298 # start tag or end tag
1299 ## Stay in this state
1300 !!!next-input-character;
1301 redo A;
1302 } elsif ($self->{next_char} == -1) {
1303 !!!parse-error (type => 'unclosed tag');
1304 if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1305 !!!cp (39);
1306 $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1307 } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1308 $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1309 #if ($self->{current_token}->{attributes}) {
1310 # ## NOTE: This state should never be reached.
1311 # !!! cp (40);
1312 # !!! parse-error (type => 'end tag attribute');
1313 #} else {
1314 !!!cp (41);
1315 #}
1316 } else {
1317 die "$0: $self->{current_token}->{type}: Unknown token type";
1318 }
1319 $self->{state} = DATA_STATE;
1320 # reconsume
1321
1322 !!!emit ($self->{current_token}); # start tag or end tag
1323
1324 redo A;
1325 } elsif ($self->{next_char} == 0x002F) { # /
1326 !!!cp (42);
1327 $self->{state} = SELF_CLOSING_START_TAG_STATE;
1328 !!!next-input-character;
1329 redo A;
1330 } else {
1331 !!!cp (44);
1332 $self->{current_token}->{tag_name} .= chr $self->{next_char};
1333 # start tag or end tag
1334 ## Stay in the state
1335 !!!next-input-character;
1336 redo A;
1337 }
1338 } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1339 if ($self->{next_char} == 0x0009 or # HT
1340 $self->{next_char} == 0x000A or # LF
1341 $self->{next_char} == 0x000B or # VT
1342 $self->{next_char} == 0x000C or # FF
1343 $self->{next_char} == 0x0020) { # SP
1344 !!!cp (45);
1345 ## Stay in the state
1346 !!!next-input-character;
1347 redo A;
1348 } elsif ($self->{next_char} == 0x003E) { # >
1349 if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1350 !!!cp (46);
1351 $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1352 } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1353 $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1354 if ($self->{current_token}->{attributes}) {
1355 !!!cp (47);
1356 !!!parse-error (type => 'end tag attribute');
1357 } else {
1358 !!!cp (48);
1359 }
1360 } else {
1361 die "$0: $self->{current_token}->{type}: Unknown token type";
1362 }
1363 $self->{state} = DATA_STATE;
1364 !!!next-input-character;
1365
1366 !!!emit ($self->{current_token}); # start tag or end tag
1367
1368 redo A;
1369 } elsif (0x0041 <= $self->{next_char} and
1370 $self->{next_char} <= 0x005A) { # A..Z
1371 !!!cp (49);
1372 $self->{current_attribute}
1373 = {name => chr ($self->{next_char} + 0x0020),
1374 value => '',
1375 line => $self->{line}, column => $self->{column}};
1376 $self->{state} = ATTRIBUTE_NAME_STATE;
1377 !!!next-input-character;
1378 redo A;
1379 } elsif ($self->{next_char} == 0x002F) { # /
1380 !!!cp (50);
1381 $self->{state} = SELF_CLOSING_START_TAG_STATE;
1382 !!!next-input-character;
1383 redo A;
1384 } elsif ($self->{next_char} == -1) {
1385 !!!parse-error (type => 'unclosed tag');
1386 if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1387 !!!cp (52);
1388 $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1389 } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1390 $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1391 if ($self->{current_token}->{attributes}) {
1392 !!!cp (53);
1393 !!!parse-error (type => 'end tag attribute');
1394 } else {
1395 !!!cp (54);
1396 }
1397 } else {
1398 die "$0: $self->{current_token}->{type}: Unknown token type";
1399 }
1400 $self->{state} = DATA_STATE;
1401 # reconsume
1402
1403 !!!emit ($self->{current_token}); # start tag or end tag
1404
1405 redo A;
1406 } else {
1407 if ({
1408 0x0022 => 1, # "
1409 0x0027 => 1, # '
1410 0x003D => 1, # =
1411 }->{$self->{next_char}}) {
1412 !!!cp (55);
1413 !!!parse-error (type => 'bad attribute name');
1414 } else {
1415 !!!cp (56);
1416 }
1417 $self->{current_attribute}
1418 = {name => chr ($self->{next_char}),
1419 value => '',
1420 line => $self->{line}, column => $self->{column}};
1421 $self->{state} = ATTRIBUTE_NAME_STATE;
1422 !!!next-input-character;
1423 redo A;
1424 }
1425 } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
1426 my $before_leave = sub {
1427 if (exists $self->{current_token}->{attributes} # start tag or end tag
1428 ->{$self->{current_attribute}->{name}}) { # MUST
1429 !!!cp (57);
1430 !!!parse-error (type => 'duplicate attribute', text => $self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1431 ## Discard $self->{current_attribute} # MUST
1432 } else {
1433 !!!cp (58);
1434 $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
1435 = $self->{current_attribute};
1436 }
1437 }; # $before_leave
1438
1439 if ($self->{next_char} == 0x0009 or # HT
1440 $self->{next_char} == 0x000A or # LF
1441 $self->{next_char} == 0x000B or # VT
1442 $self->{next_char} == 0x000C or # FF
1443 $self->{next_char} == 0x0020) { # SP
1444 !!!cp (59);
1445 $before_leave->();
1446 $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1447 !!!next-input-character;
1448 redo A;
1449 } elsif ($self->{next_char} == 0x003D) { # =
1450 !!!cp (60);
1451 $before_leave->();
1452 $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1453 !!!next-input-character;
1454 redo A;
1455 } elsif ($self->{next_char} == 0x003E) { # >
1456 $before_leave->();
1457 if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1458 !!!cp (61);
1459 $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1460 } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1461 !!!cp (62);
1462 $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1463 if ($self->{current_token}->{attributes}) {
1464 !!!parse-error (type => 'end tag attribute');
1465 }
1466 } else {
1467 die "$0: $self->{current_token}->{type}: Unknown token type";
1468 }
1469 $self->{state} = DATA_STATE;
1470 !!!next-input-character;
1471
1472 !!!emit ($self->{current_token}); # start tag or end tag
1473
1474 redo A;
1475 } elsif (0x0041 <= $self->{next_char} and
1476 $self->{next_char} <= 0x005A) { # A..Z
1477 !!!cp (63);
1478 $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
1479 ## Stay in the state
1480 !!!next-input-character;
1481 redo A;
1482 } elsif ($self->{next_char} == 0x002F) { # /
1483 !!!cp (64);
1484 $before_leave->();
1485 $self->{state} = SELF_CLOSING_START_TAG_STATE;
1486 !!!next-input-character;
1487 redo A;
1488 } elsif ($self->{next_char} == -1) {
1489 !!!parse-error (type => 'unclosed tag');
1490 $before_leave->();
1491 if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1492 !!!cp (66);
1493 $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1494 } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1495 $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1496 if ($self->{current_token}->{attributes}) {
1497 !!!cp (67);
1498 !!!parse-error (type => 'end tag attribute');
1499 } else {
1500 ## NOTE: This state should never be reached.
1501 !!!cp (68);
1502 }
1503 } else {
1504 die "$0: $self->{current_token}->{type}: Unknown token type";
1505 }
1506 $self->{state} = DATA_STATE;
1507 # reconsume
1508
1509 !!!emit ($self->{current_token}); # start tag or end tag
1510
1511 redo A;
1512 } else {
1513 if ($self->{next_char} == 0x0022 or # "
1514 $self->{next_char} == 0x0027) { # '
1515 !!!cp (69);
1516 !!!parse-error (type => 'bad attribute name');
1517 } else {
1518 !!!cp (70);
1519 }
1520 $self->{current_attribute}->{name} .= chr ($self->{next_char});
1521 ## Stay in the state
1522 !!!next-input-character;
1523 redo A;
1524 }
1525 } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1526 if ($self->{next_char} == 0x0009 or # HT
1527 $self->{next_char} == 0x000A or # LF
1528 $self->{next_char} == 0x000B or # VT
1529 $self->{next_char} == 0x000C or # FF
1530 $self->{next_char} == 0x0020) { # SP
1531 !!!cp (71);
1532 ## Stay in the state
1533 !!!next-input-character;
1534 redo A;
1535 } elsif ($self->{next_char} == 0x003D) { # =
1536 !!!cp (72);
1537 $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1538 !!!next-input-character;
1539 redo A;
1540 } elsif ($self->{next_char} == 0x003E) { # >
1541 if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1542 !!!cp (73);
1543 $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1544 } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1545 $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1546 if ($self->{current_token}->{attributes}) {
1547 !!!cp (74);
1548 !!!parse-error (type => 'end tag attribute');
1549 } else {
1550 ## NOTE: This state should never be reached.
1551 !!!cp (75);
1552 }
1553 } else {
1554 die "$0: $self->{current_token}->{type}: Unknown token type";
1555 }
1556 $self->{state} = DATA_STATE;
1557 !!!next-input-character;
1558
1559 !!!emit ($self->{current_token}); # start tag or end tag
1560
1561 redo A;
1562 } elsif (0x0041 <= $self->{next_char} and
1563 $self->{next_char} <= 0x005A) { # A..Z
1564 !!!cp (76);
1565 $self->{current_attribute}
1566 = {name => chr ($self->{next_char} + 0x0020),
1567 value => '',
1568 line => $self->{line}, column => $self->{column}};
1569 $self->{state} = ATTRIBUTE_NAME_STATE;
1570 !!!next-input-character;
1571 redo A;
1572 } elsif ($self->{next_char} == 0x002F) { # /
1573 !!!cp (77);
1574 $self->{state} = SELF_CLOSING_START_TAG_STATE;
1575 !!!next-input-character;
1576 redo A;
1577 } elsif ($self->{next_char} == -1) {
1578 !!!parse-error (type => 'unclosed tag');
1579 if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1580 !!!cp (79);
1581 $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1582 } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1583 $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1584 if ($self->{current_token}->{attributes}) {
1585 !!!cp (80);
1586 !!!parse-error (type => 'end tag attribute');
1587 } else {
1588 ## NOTE: This state should never be reached.
1589 !!!cp (81);
1590 }
1591 } else {
1592 die "$0: $self->{current_token}->{type}: Unknown token type";
1593 }
1594 $self->{state} = DATA_STATE;
1595 # reconsume
1596
1597 !!!emit ($self->{current_token}); # start tag or end tag
1598
1599 redo A;
1600 } else {
1601 if ($self->{next_char} == 0x0022 or # "
1602 $self->{next_char} == 0x0027) { # '
1603 !!!cp (78);
1604 !!!parse-error (type => 'bad attribute name');
1605 } else {
1606 !!!cp (82);
1607 }
1608 $self->{current_attribute}
1609 = {name => chr ($self->{next_char}),
1610 value => '',
1611 line => $self->{line}, column => $self->{column}};
1612 $self->{state} = ATTRIBUTE_NAME_STATE;
1613 !!!next-input-character;
1614 redo A;
1615 }
1616 } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1617 if ($self->{next_char} == 0x0009 or # HT
1618 $self->{next_char} == 0x000A or # LF
1619 $self->{next_char} == 0x000B or # VT
1620 $self->{next_char} == 0x000C or # FF
1621 $self->{next_char} == 0x0020) { # SP
1622 !!!cp (83);
1623 ## Stay in the state
1624 !!!next-input-character;
1625 redo A;
1626 } elsif ($self->{next_char} == 0x0022) { # "
1627 !!!cp (84);
1628 $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1629 !!!next-input-character;
1630 redo A;
1631 } elsif ($self->{next_char} == 0x0026) { # &
1632 !!!cp (85);
1633 $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1634 ## reconsume
1635 redo A;
1636 } elsif ($self->{next_char} == 0x0027) { # '
1637 !!!cp (86);
1638 $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1639 !!!next-input-character;
1640 redo A;
1641 } elsif ($self->{next_char} == 0x003E) { # >
1642 !!!parse-error (type => 'empty unquoted attribute value');
1643 if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1644 !!!cp (87);
1645 $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1646 } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1647 $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1648 if ($self->{current_token}->{attributes}) {
1649 !!!cp (88);
1650 !!!parse-error (type => 'end tag attribute');
1651 } else {
1652 ## NOTE: This state should never be reached.
1653 !!!cp (89);
1654 }
1655 } else {
1656 die "$0: $self->{current_token}->{type}: Unknown token type";
1657 }
1658 $self->{state} = DATA_STATE;
1659 !!!next-input-character;
1660
1661 !!!emit ($self->{current_token}); # start tag or end tag
1662
1663 redo A;
1664 } elsif ($self->{next_char} == -1) {
1665 !!!parse-error (type => 'unclosed tag');
1666 if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1667 !!!cp (90);
1668 $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1669 } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1670 $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1671 if ($self->{current_token}->{attributes}) {
1672 !!!cp (91);
1673 !!!parse-error (type => 'end tag attribute');
1674 } else {
1675 ## NOTE: This state should never be reached.
1676 !!!cp (92);
1677 }
1678 } else {
1679 die "$0: $self->{current_token}->{type}: Unknown token type";
1680 }
1681 $self->{state} = DATA_STATE;
1682 ## reconsume
1683
1684 !!!emit ($self->{current_token}); # start tag or end tag
1685
1686 redo A;
1687 } else {
1688 if ($self->{next_char} == 0x003D) { # =
1689 !!!cp (93);
1690 !!!parse-error (type => 'bad attribute value');
1691 } else {
1692 !!!cp (94);
1693 }
1694 $self->{current_attribute}->{value} .= chr ($self->{next_char});
1695 $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1696 !!!next-input-character;
1697 redo A;
1698 }
1699 } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1700 if ($self->{next_char} == 0x0022) { # "
1701 !!!cp (95);
1702 $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1703 !!!next-input-character;
1704 redo A;
1705 } elsif ($self->{next_char} == 0x0026) { # &
1706 !!!cp (96);
1707 $self->{last_attribute_value_state} = $self->{state};
1708 $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1709 !!!next-input-character;
1710 redo A;
1711 } elsif ($self->{next_char} == -1) {
1712 !!!parse-error (type => 'unclosed attribute value');
1713 if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1714 !!!cp (97);
1715 $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1716 } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1717 $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1718 if ($self->{current_token}->{attributes}) {
1719 !!!cp (98);
1720 !!!parse-error (type => 'end tag attribute');
1721 } else {
1722 ## NOTE: This state should never be reached.
1723 !!!cp (99);
1724 }
1725 } else {
1726 die "$0: $self->{current_token}->{type}: Unknown token type";
1727 }
1728 $self->{state} = DATA_STATE;
1729 ## reconsume
1730
1731 !!!emit ($self->{current_token}); # start tag or end tag
1732
1733 redo A;
1734 } else {
1735 !!!cp (100);
1736 $self->{current_attribute}->{value} .= chr ($self->{next_char});
1737 ## Stay in the state
1738 !!!next-input-character;
1739 redo A;
1740 }
1741 } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1742 if ($self->{next_char} == 0x0027) { # '
1743 !!!cp (101);
1744 $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1745 !!!next-input-character;
1746 redo A;
1747 } elsif ($self->{next_char} == 0x0026) { # &
1748 !!!cp (102);
1749 $self->{last_attribute_value_state} = $self->{state};
1750 $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1751 !!!next-input-character;
1752 redo A;
1753 } elsif ($self->{next_char} == -1) {
1754 !!!parse-error (type => 'unclosed attribute value');
1755 if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1756 !!!cp (103);
1757 $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1758 } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1759 $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1760 if ($self->{current_token}->{attributes}) {
1761 !!!cp (104);
1762 !!!parse-error (type => 'end tag attribute');
1763 } else {
1764 ## NOTE: This state should never be reached.
1765 !!!cp (105);
1766 }
1767 } else {
1768 die "$0: $self->{current_token}->{type}: Unknown token type";
1769 }
1770 $self->{state} = DATA_STATE;
1771 ## reconsume
1772
1773 !!!emit ($self->{current_token}); # start tag or end tag
1774
1775 redo A;
1776 } else {
1777 !!!cp (106);
1778 $self->{current_attribute}->{value} .= chr ($self->{next_char});
1779 ## Stay in the state
1780 !!!next-input-character;
1781 redo A;
1782 }
1783 } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1784 if ($self->{next_char} == 0x0009 or # HT
1785 $self->{next_char} == 0x000A or # LF
1786 $self->{next_char} == 0x000B or # HT
1787 $self->{next_char} == 0x000C or # FF
1788 $self->{next_char} == 0x0020) { # SP
1789 !!!cp (107);
1790 $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1791 !!!next-input-character;
1792 redo A;
1793 } elsif ($self->{next_char} == 0x0026) { # &
1794 !!!cp (108);
1795 $self->{last_attribute_value_state} = $self->{state};
1796 $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1797 !!!next-input-character;
1798 redo A;
1799 } elsif ($self->{next_char} == 0x003E) { # >
1800 if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1801 !!!cp (109);
1802 $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1803 } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1804 $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1805 if ($self->{current_token}->{attributes}) {
1806 !!!cp (110);
1807 !!!parse-error (type => 'end tag attribute');
1808 } else {
1809 ## NOTE: This state should never be reached.
1810 !!!cp (111);
1811 }
1812 } else {
1813 die "$0: $self->{current_token}->{type}: Unknown token type";
1814 }
1815 $self->{state} = DATA_STATE;
1816 !!!next-input-character;
1817
1818 !!!emit ($self->{current_token}); # start tag or end tag
1819
1820 redo A;
1821 } elsif ($self->{next_char} == -1) {
1822 !!!parse-error (type => 'unclosed tag');
1823 if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1824 !!!cp (112);
1825 $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1826 } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1827 $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1828 if ($self->{current_token}->{attributes}) {
1829 !!!cp (113);
1830 !!!parse-error (type => 'end tag attribute');
1831 } else {
1832 ## NOTE: This state should never be reached.
1833 !!!cp (114);
1834 }
1835 } else {
1836 die "$0: $self->{current_token}->{type}: Unknown token type";
1837 }
1838 $self->{state} = DATA_STATE;
1839 ## reconsume
1840
1841 !!!emit ($self->{current_token}); # start tag or end tag
1842
1843 redo A;
1844 } else {
1845 if ({
1846 0x0022 => 1, # "
1847 0x0027 => 1, # '
1848 0x003D => 1, # =
1849 }->{$self->{next_char}}) {
1850 !!!cp (115);
1851 !!!parse-error (type => 'bad attribute value');
1852 } else {
1853 !!!cp (116);
1854 }
1855 $self->{current_attribute}->{value} .= chr ($self->{next_char});
1856 ## Stay in the state
1857 !!!next-input-character;
1858 redo A;
1859 }
1860 } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1861 my $token = $self->_tokenize_attempt_to_consume_an_entity
1862 (1,
1863 $self->{last_attribute_value_state}
1864 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1865 $self->{last_attribute_value_state}
1866 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1867 -1);
1868
1869 unless (defined $token) {
1870 !!!cp (117);
1871 $self->{current_attribute}->{value} .= '&';
1872 } else {
1873 !!!cp (118);
1874 $self->{current_attribute}->{value} .= $token->{data};
1875 $self->{current_attribute}->{has_reference} = $token->{has_reference};
1876 ## ISSUE: spec says "append the returned character token to the current attribute's value"
1877 }
1878
1879 $self->{state} = $self->{last_attribute_value_state};
1880 # next-input-character is already done
1881 redo A;
1882 } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1883 if ($self->{next_char} == 0x0009 or # HT
1884 $self->{next_char} == 0x000A or # LF
1885 $self->{next_char} == 0x000B or # VT
1886 $self->{next_char} == 0x000C or # FF
1887 $self->{next_char} == 0x0020) { # SP
1888 !!!cp (118);
1889 $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1890 !!!next-input-character;
1891 redo A;
1892 } elsif ($self->{next_char} == 0x003E) { # >
1893 if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1894 !!!cp (119);
1895 $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1896 } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1897 $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1898 if ($self->{current_token}->{attributes}) {
1899 !!!cp (120);
1900 !!!parse-error (type => 'end tag attribute');
1901 } else {
1902 ## NOTE: This state should never be reached.
1903 !!!cp (121);
1904 }
1905 } else {
1906 die "$0: $self->{current_token}->{type}: Unknown token type";
1907 }
1908 $self->{state} = DATA_STATE;
1909 !!!next-input-character;
1910
1911 !!!emit ($self->{current_token}); # start tag or end tag
1912
1913 redo A;
1914 } elsif ($self->{next_char} == 0x002F) { # /
1915 !!!cp (122);
1916 $self->{state} = SELF_CLOSING_START_TAG_STATE;
1917 !!!next-input-character;
1918 redo A;
1919 } elsif ($self->{next_char} == -1) {
1920 !!!parse-error (type => 'unclosed tag');
1921 if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1922 !!!cp (122.3);
1923 $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1924 } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1925 if ($self->{current_token}->{attributes}) {
1926 !!!cp (122.1);
1927 !!!parse-error (type => 'end tag attribute');
1928 } else {
1929 ## NOTE: This state should never be reached.
1930 !!!cp (122.2);
1931 }
1932 } else {
1933 die "$0: $self->{current_token}->{type}: Unknown token type";
1934 }
1935 $self->{state} = DATA_STATE;
1936 ## Reconsume.
1937 !!!emit ($self->{current_token}); # start tag or end tag
1938 redo A;
1939 } else {
1940 !!!cp ('124.1');
1941 !!!parse-error (type => 'no space between attributes');
1942 $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1943 ## reconsume
1944 redo A;
1945 }
1946 } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1947 if ($self->{next_char} == 0x003E) { # >
1948 if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1949 !!!cp ('124.2');
1950 !!!parse-error (type => 'nestc', token => $self->{current_token});
1951 ## TODO: Different type than slash in start tag
1952 $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1953 if ($self->{current_token}->{attributes}) {
1954 !!!cp ('124.4');
1955 !!!parse-error (type => 'end tag attribute');
1956 } else {
1957 !!!cp ('124.5');
1958 }
1959 ## TODO: Test |<title></title/>|
1960 } else {
1961 !!!cp ('124.3');
1962 $self->{self_closing} = 1;
1963 }
1964
1965 $self->{state} = DATA_STATE;
1966 !!!next-input-character;
1967
1968 !!!emit ($self->{current_token}); # start tag or end tag
1969
1970 redo A;
1971 } elsif ($self->{next_char} == -1) {
1972 !!!parse-error (type => 'unclosed tag');
1973 if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1974 !!!cp (124.7);
1975 $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1976 } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1977 if ($self->{current_token}->{attributes}) {
1978 !!!cp (124.5);
1979 !!!parse-error (type => 'end tag attribute');
1980 } else {
1981 ## NOTE: This state should never be reached.
1982 !!!cp (124.6);
1983 }
1984 } else {
1985 die "$0: $self->{current_token}->{type}: Unknown token type";
1986 }
1987 $self->{state} = DATA_STATE;
1988 ## Reconsume.
1989 !!!emit ($self->{current_token}); # start tag or end tag
1990 redo A;
1991 } else {
1992 !!!cp ('124.4');
1993 !!!parse-error (type => 'nestc');
1994 ## TODO: This error type is wrong.
1995 $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1996 ## Reconsume.
1997 redo A;
1998 }
1999 } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
2000 ## (only happen if PCDATA state)
2001
2002 ## NOTE: Set by the previous state
2003 #my $token = {type => COMMENT_TOKEN, data => ''};
2004
2005 BC: {
2006 if ($self->{next_char} == 0x003E) { # >
2007 !!!cp (124);
2008 $self->{state} = DATA_STATE;
2009 !!!next-input-character;
2010
2011 !!!emit ($self->{current_token}); # comment
2012
2013 redo A;
2014 } elsif ($self->{next_char} == -1) {
2015 !!!cp (125);
2016 $self->{state} = DATA_STATE;
2017 ## reconsume
2018
2019 !!!emit ($self->{current_token}); # comment
2020
2021 redo A;
2022 } else {
2023 !!!cp (126);
2024 $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2025 !!!next-input-character;
2026 redo BC;
2027 }
2028 } # BC
2029
2030 die "$0: _get_next_token: unexpected case [BC]";
2031 } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2032 ## (only happen if PCDATA state)
2033
2034 if ($self->{next_char} == 0x002D) { # -
2035 !!!cp (133);
2036 $self->{state} = MD_HYPHEN_STATE;
2037 !!!next-input-character;
2038 redo A;
2039 } elsif ($self->{next_char} == 0x0044 or # D
2040 $self->{next_char} == 0x0064) { # d
2041 ## ASCII case-insensitive.
2042 !!!cp (130);
2043 $self->{state} = MD_DOCTYPE_STATE;
2044 $self->{state_keyword} = chr $self->{next_char};
2045 !!!next-input-character;
2046 redo A;
2047 } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2048 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2049 $self->{next_char} == 0x005B) { # [
2050 !!!cp (135.4);
2051 $self->{state} = MD_CDATA_STATE;
2052 $self->{state_keyword} = '[';
2053 !!!next-input-character;
2054 redo A;
2055 } else {
2056 !!!cp (136);
2057 }
2058
2059 !!!parse-error (type => 'bogus comment',
2060 line => $self->{line_prev},
2061 column => $self->{column_prev} - 1);
2062 ## Reconsume.
2063 $self->{state} = BOGUS_COMMENT_STATE;
2064 $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2065 line => $self->{line_prev},
2066 column => $self->{column_prev} - 1,
2067 };
2068 redo A;
2069 } elsif ($self->{state} == MD_HYPHEN_STATE) {
2070 if ($self->{next_char} == 0x002D) { # -
2071 !!!cp (127);
2072 $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2073 line => $self->{line_prev},
2074 column => $self->{column_prev} - 2,
2075 };
2076 $self->{state} = COMMENT_START_STATE;
2077 !!!next-input-character;
2078 redo A;
2079 } else {
2080 !!!cp (128);
2081 !!!parse-error (type => 'bogus comment',
2082 line => $self->{line_prev},
2083 column => $self->{column_prev} - 2);
2084 $self->{state} = BOGUS_COMMENT_STATE;
2085 ## Reconsume.
2086 $self->{current_token} = {type => COMMENT_TOKEN,
2087 data => '-',
2088 line => $self->{line_prev},
2089 column => $self->{column_prev} - 2,
2090 };
2091 redo A;
2092 }
2093 } elsif ($self->{state} == MD_DOCTYPE_STATE) {
2094 ## ASCII case-insensitive.
2095 if ($self->{next_char} == [
2096 undef,
2097 0x004F, # O
2098 0x0043, # C
2099 0x0054, # T
2100 0x0059, # Y
2101 0x0050, # P
2102 ]->[length $self->{state_keyword}] or
2103 $self->{next_char} == [
2104 undef,
2105 0x006F, # o
2106 0x0063, # c
2107 0x0074, # t
2108 0x0079, # y
2109 0x0070, # p
2110 ]->[length $self->{state_keyword}]) {
2111 !!!cp (131);
2112 ## Stay in the state.
2113 $self->{state_keyword} .= chr $self->{next_char};
2114 !!!next-input-character;
2115 redo A;
2116 } elsif ((length $self->{state_keyword}) == 6 and
2117 ($self->{next_char} == 0x0045 or # E
2118 $self->{next_char} == 0x0065)) { # e
2119 !!!cp (129);
2120 $self->{state} = DOCTYPE_STATE;
2121 $self->{current_token} = {type => DOCTYPE_TOKEN,
2122 quirks => 1,
2123 line => $self->{line_prev},
2124 column => $self->{column_prev} - 7,
2125 };
2126 !!!next-input-character;
2127 redo A;
2128 } else {
2129 !!!cp (132);
2130 !!!parse-error (type => 'bogus comment',
2131 line => $self->{line_prev},
2132 column => $self->{column_prev} - 1 - length $self->{state_keyword});
2133 $self->{state} = BOGUS_COMMENT_STATE;
2134 ## Reconsume.
2135 $self->{current_token} = {type => COMMENT_TOKEN,
2136 data => $self->{state_keyword},
2137 line => $self->{line_prev},
2138 column => $self->{column_prev} - 1 - length $self->{state_keyword},
2139 };
2140 redo A;
2141 }
2142 } elsif ($self->{state} == MD_CDATA_STATE) {
2143 if ($self->{next_char} == {
2144 '[' => 0x0043, # C
2145 '[C' => 0x0044, # D
2146 '[CD' => 0x0041, # A
2147 '[CDA' => 0x0054, # T
2148 '[CDAT' => 0x0041, # A
2149 }->{$self->{state_keyword}}) {
2150 !!!cp (135.1);
2151 ## Stay in the state.
2152 $self->{state_keyword} .= chr $self->{next_char};
2153 !!!next-input-character;
2154 redo A;
2155 } elsif ($self->{state_keyword} eq '[CDATA' and
2156 $self->{next_char} == 0x005B) { # [
2157 !!!cp (135.2);
2158 $self->{current_token} = {type => CHARACTER_TOKEN,
2159 data => '',
2160 line => $self->{line_prev},
2161 column => $self->{column_prev} - 7};
2162 $self->{state} = CDATA_SECTION_STATE;
2163 !!!next-input-character;
2164 redo A;
2165 } else {
2166 !!!cp (135.3);
2167 !!!parse-error (type => 'bogus comment',
2168 line => $self->{line_prev},
2169 column => $self->{column_prev} - 1 - length $self->{state_keyword});
2170 $self->{state} = BOGUS_COMMENT_STATE;
2171 ## Reconsume.
2172 $self->{current_token} = {type => COMMENT_TOKEN,
2173 data => $self->{state_keyword},
2174 line => $self->{line_prev},
2175 column => $self->{column_prev} - 1 - length $self->{state_keyword},
2176 };
2177 redo A;
2178 }
2179 } elsif ($self->{state} == COMMENT_START_STATE) {
2180 if ($self->{next_char} == 0x002D) { # -
2181 !!!cp (137);
2182 $self->{state} = COMMENT_START_DASH_STATE;
2183 !!!next-input-character;
2184 redo A;
2185 } elsif ($self->{next_char} == 0x003E) { # >
2186 !!!cp (138);
2187 !!!parse-error (type => 'bogus comment');
2188 $self->{state} = DATA_STATE;
2189 !!!next-input-character;
2190
2191 !!!emit ($self->{current_token}); # comment
2192
2193 redo A;
2194 } elsif ($self->{next_char} == -1) {
2195 !!!cp (139);
2196 !!!parse-error (type => 'unclosed comment');
2197 $self->{state} = DATA_STATE;
2198 ## reconsume
2199
2200 !!!emit ($self->{current_token}); # comment
2201
2202 redo A;
2203 } else {
2204 !!!cp (140);
2205 $self->{current_token}->{data} # comment
2206 .= chr ($self->{next_char});
2207 $self->{state} = COMMENT_STATE;
2208 !!!next-input-character;
2209 redo A;
2210 }
2211 } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2212 if ($self->{next_char} == 0x002D) { # -
2213 !!!cp (141);
2214 $self->{state} = COMMENT_END_STATE;
2215 !!!next-input-character;
2216 redo A;
2217 } elsif ($self->{next_char} == 0x003E) { # >
2218 !!!cp (142);
2219 !!!parse-error (type => 'bogus comment');
2220 $self->{state} = DATA_STATE;
2221 !!!next-input-character;
2222
2223 !!!emit ($self->{current_token}); # comment
2224
2225 redo A;
2226 } elsif ($self->{next_char} == -1) {
2227 !!!cp (143);
2228 !!!parse-error (type => 'unclosed comment');
2229 $self->{state} = DATA_STATE;
2230 ## reconsume
2231
2232 !!!emit ($self->{current_token}); # comment
2233
2234 redo A;
2235 } else {
2236 !!!cp (144);
2237 $self->{current_token}->{data} # comment
2238 .= '-' . chr ($self->{next_char});
2239 $self->{state} = COMMENT_STATE;
2240 !!!next-input-character;
2241 redo A;
2242 }
2243 } elsif ($self->{state} == COMMENT_STATE) {
2244 if ($self->{next_char} == 0x002D) { # -
2245 !!!cp (145);
2246 $self->{state} = COMMENT_END_DASH_STATE;
2247 !!!next-input-character;
2248 redo A;
2249 } elsif ($self->{next_char} == -1) {
2250 !!!cp (146);
2251 !!!parse-error (type => 'unclosed comment');
2252 $self->{state} = DATA_STATE;
2253 ## reconsume
2254
2255 !!!emit ($self->{current_token}); # comment
2256
2257 redo A;
2258 } else {
2259 !!!cp (147);
2260 $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2261 ## Stay in the state
2262 !!!next-input-character;
2263 redo A;
2264 }
2265 } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2266 if ($self->{next_char} == 0x002D) { # -
2267 !!!cp (148);
2268 $self->{state} = COMMENT_END_STATE;
2269 !!!next-input-character;
2270 redo A;
2271 } elsif ($self->{next_char} == -1) {
2272 !!!cp (149);
2273 !!!parse-error (type => 'unclosed comment');
2274 $self->{state} = DATA_STATE;
2275 ## reconsume
2276
2277 !!!emit ($self->{current_token}); # comment
2278
2279 redo A;
2280 } else {
2281 !!!cp (150);
2282 $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
2283 $self->{state} = COMMENT_STATE;
2284 !!!next-input-character;
2285 redo A;
2286 }
2287 } elsif ($self->{state} == COMMENT_END_STATE) {
2288 if ($self->{next_char} == 0x003E) { # >
2289 !!!cp (151);
2290 $self->{state} = DATA_STATE;
2291 !!!next-input-character;
2292
2293 !!!emit ($self->{current_token}); # comment
2294
2295 redo A;
2296 } elsif ($self->{next_char} == 0x002D) { # -
2297 !!!cp (152);
2298 !!!parse-error (type => 'dash in comment',
2299 line => $self->{line_prev},
2300 column => $self->{column_prev});
2301 $self->{current_token}->{data} .= '-'; # comment
2302 ## Stay in the state
2303 !!!next-input-character;
2304 redo A;
2305 } elsif ($self->{next_char} == -1) {
2306 !!!cp (153);
2307 !!!parse-error (type => 'unclosed comment');
2308 $self->{state} = DATA_STATE;
2309 ## reconsume
2310
2311 !!!emit ($self->{current_token}); # comment
2312
2313 redo A;
2314 } else {
2315 !!!cp (154);
2316 !!!parse-error (type => 'dash in comment',
2317 line => $self->{line_prev},
2318 column => $self->{column_prev});
2319 $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2320 $self->{state} = COMMENT_STATE;
2321 !!!next-input-character;
2322 redo A;
2323 }
2324 } elsif ($self->{state} == DOCTYPE_STATE) {
2325 if ($self->{next_char} == 0x0009 or # HT
2326 $self->{next_char} == 0x000A or # LF
2327 $self->{next_char} == 0x000B or # VT
2328 $self->{next_char} == 0x000C or # FF
2329 $self->{next_char} == 0x0020) { # SP
2330 !!!cp (155);
2331 $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2332 !!!next-input-character;
2333 redo A;
2334 } else {
2335 !!!cp (156);
2336 !!!parse-error (type => 'no space before DOCTYPE name');
2337 $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2338 ## reconsume
2339 redo A;
2340 }
2341 } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2342 if ($self->{next_char} == 0x0009 or # HT
2343 $self->{next_char} == 0x000A or # LF
2344 $self->{next_char} == 0x000B or # VT
2345 $self->{next_char} == 0x000C or # FF
2346 $self->{next_char} == 0x0020) { # SP
2347 !!!cp (157);
2348 ## Stay in the state
2349 !!!next-input-character;
2350 redo A;
2351 } elsif ($self->{next_char} == 0x003E) { # >
2352 !!!cp (158);
2353 !!!parse-error (type => 'no DOCTYPE name');
2354 $self->{state} = DATA_STATE;
2355 !!!next-input-character;
2356
2357 !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2358
2359 redo A;
2360 } elsif ($self->{next_char} == -1) {
2361 !!!cp (159);
2362 !!!parse-error (type => 'no DOCTYPE name');
2363 $self->{state} = DATA_STATE;
2364 ## reconsume
2365
2366 !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2367
2368 redo A;
2369 } else {
2370 !!!cp (160);
2371 $self->{current_token}->{name} = chr $self->{next_char};
2372 delete $self->{current_token}->{quirks};
2373 ## ISSUE: "Set the token's name name to the" in the spec
2374 $self->{state} = DOCTYPE_NAME_STATE;
2375 !!!next-input-character;
2376 redo A;
2377 }
2378 } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2379 ## ISSUE: Redundant "First," in the spec.
2380 if ($self->{next_char} == 0x0009 or # HT
2381 $self->{next_char} == 0x000A or # LF
2382 $self->{next_char} == 0x000B or # VT
2383 $self->{next_char} == 0x000C or # FF
2384 $self->{next_char} == 0x0020) { # SP
2385 !!!cp (161);
2386 $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2387 !!!next-input-character;
2388 redo A;
2389 } elsif ($self->{next_char} == 0x003E) { # >
2390 !!!cp (162);
2391 $self->{state} = DATA_STATE;
2392 !!!next-input-character;
2393
2394 !!!emit ($self->{current_token}); # DOCTYPE
2395
2396 redo A;
2397 } elsif ($self->{next_char} == -1) {
2398 !!!cp (163);
2399 !!!parse-error (type => 'unclosed DOCTYPE');
2400 $self->{state} = DATA_STATE;
2401 ## reconsume
2402
2403 $self->{current_token}->{quirks} = 1;
2404 !!!emit ($self->{current_token}); # DOCTYPE
2405
2406 redo A;
2407 } else {
2408 !!!cp (164);
2409 $self->{current_token}->{name}
2410 .= chr ($self->{next_char}); # DOCTYPE
2411 ## Stay in the state
2412 !!!next-input-character;
2413 redo A;
2414 }
2415 } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2416 if ($self->{next_char} == 0x0009 or # HT
2417 $self->{next_char} == 0x000A or # LF
2418 $self->{next_char} == 0x000B or # VT
2419 $self->{next_char} == 0x000C or # FF
2420 $self->{next_char} == 0x0020) { # SP
2421 !!!cp (165);
2422 ## Stay in the state
2423 !!!next-input-character;
2424 redo A;
2425 } elsif ($self->{next_char} == 0x003E) { # >
2426 !!!cp (166);
2427 $self->{state} = DATA_STATE;
2428 !!!next-input-character;
2429
2430 !!!emit ($self->{current_token}); # DOCTYPE
2431
2432 redo A;
2433 } elsif ($self->{next_char} == -1) {
2434 !!!cp (167);
2435 !!!parse-error (type => 'unclosed DOCTYPE');
2436 $self->{state} = DATA_STATE;
2437 ## reconsume
2438
2439 $self->{current_token}->{quirks} = 1;
2440 !!!emit ($self->{current_token}); # DOCTYPE
2441
2442 redo A;
2443 } elsif ($self->{next_char} == 0x0050 or # P
2444 $self->{next_char} == 0x0070) { # p
2445 $self->{state} = PUBLIC_STATE;
2446 $self->{state_keyword} = chr $self->{next_char};
2447 !!!next-input-character;
2448 redo A;
2449 } elsif ($self->{next_char} == 0x0053 or # S
2450 $self->{next_char} == 0x0073) { # s
2451 $self->{state} = SYSTEM_STATE;
2452 $self->{state_keyword} = chr $self->{next_char};
2453 !!!next-input-character;
2454 redo A;
2455 } else {
2456 !!!cp (180);
2457 !!!parse-error (type => 'string after DOCTYPE name');
2458 $self->{current_token}->{quirks} = 1;
2459
2460 $self->{state} = BOGUS_DOCTYPE_STATE;
2461 !!!next-input-character;
2462 redo A;
2463 }
2464 } elsif ($self->{state} == PUBLIC_STATE) {
2465 ## ASCII case-insensitive
2466 if ($self->{next_char} == [
2467 undef,
2468 0x0055, # U
2469 0x0042, # B
2470 0x004C, # L
2471 0x0049, # I
2472 ]->[length $self->{state_keyword}] or
2473 $self->{next_char} == [
2474 undef,
2475 0x0075, # u
2476 0x0062, # b
2477 0x006C, # l
2478 0x0069, # i
2479 ]->[length $self->{state_keyword}]) {
2480 !!!cp (175);
2481 ## Stay in the state.
2482 $self->{state_keyword} .= chr $self->{next_char};
2483 !!!next-input-character;
2484 redo A;
2485 } elsif ((length $self->{state_keyword}) == 5 and
2486 ($self->{next_char} == 0x0043 or # C
2487 $self->{next_char} == 0x0063)) { # c
2488 !!!cp (168);
2489 $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2490 !!!next-input-character;
2491 redo A;
2492 } else {
2493 !!!cp (169);
2494 !!!parse-error (type => 'string after DOCTYPE name',
2495 line => $self->{line_prev},
2496 column => $self->{column_prev} + 1 - length $self->{state_keyword});
2497 $self->{current_token}->{quirks} = 1;
2498
2499 $self->{state} = BOGUS_DOCTYPE_STATE;
2500 ## Reconsume.
2501 redo A;
2502 }
2503 } elsif ($self->{state} == SYSTEM_STATE) {
2504 ## ASCII case-insensitive
2505 if ($self->{next_char} == [
2506 undef,
2507 0x0059, # Y
2508 0x0053, # S
2509 0x0054, # T
2510 0x0045, # E
2511 ]->[length $self->{state_keyword}] or
2512 $self->{next_char} == [
2513 undef,
2514 0x0079, # y
2515 0x0073, # s
2516 0x0074, # t
2517 0x0065, # e
2518 ]->[length $self->{state_keyword}]) {
2519 !!!cp (170);
2520 ## Stay in the state.
2521 $self->{state_keyword} .= chr $self->{next_char};
2522 !!!next-input-character;
2523 redo A;
2524 } elsif ((length $self->{state_keyword}) == 5 and
2525 ($self->{next_char} == 0x004D or # M
2526 $self->{next_char} == 0x006D)) { # m
2527 !!!cp (171);
2528 $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2529 !!!next-input-character;
2530 redo A;
2531 } else {
2532 !!!cp (172);
2533 !!!parse-error (type => 'string after DOCTYPE name',
2534 line => $self->{line_prev},
2535 column => $self->{column_prev} + 1 - length $self->{state_keyword});
2536 $self->{current_token}->{quirks} = 1;
2537
2538 $self->{state} = BOGUS_DOCTYPE_STATE;
2539 ## Reconsume.
2540 redo A;
2541 }
2542 } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2543 if ({
2544 0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2545 #0x000D => 1, # HT, LF, VT, FF, SP, CR
2546 }->{$self->{next_char}}) {
2547 !!!cp (181);
2548 ## Stay in the state
2549 !!!next-input-character;
2550 redo A;
2551 } elsif ($self->{next_char} eq 0x0022) { # "
2552 !!!cp (182);
2553 $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2554 $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2555 !!!next-input-character;
2556 redo A;
2557 } elsif ($self->{next_char} eq 0x0027) { # '
2558 !!!cp (183);
2559 $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2560 $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2561 !!!next-input-character;
2562 redo A;
2563 } elsif ($self->{next_char} eq 0x003E) { # >
2564 !!!cp (184);
2565 !!!parse-error (type => 'no PUBLIC literal');
2566
2567 $self->{state} = DATA_STATE;
2568 !!!next-input-character;
2569
2570 $self->{current_token}->{quirks} = 1;
2571 !!!emit ($self->{current_token}); # DOCTYPE
2572
2573 redo A;
2574 } elsif ($self->{next_char} == -1) {
2575 !!!cp (185);
2576 !!!parse-error (type => 'unclosed DOCTYPE');
2577
2578 $self->{state} = DATA_STATE;
2579 ## reconsume
2580
2581 $self->{current_token}->{quirks} = 1;
2582 !!!emit ($self->{current_token}); # DOCTYPE
2583
2584 redo A;
2585 } else {
2586 !!!cp (186);
2587 !!!parse-error (type => 'string after PUBLIC');
2588 $self->{current_token}->{quirks} = 1;
2589
2590 $self->{state} = BOGUS_DOCTYPE_STATE;
2591 !!!next-input-character;
2592 redo A;
2593 }
2594 } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2595 if ($self->{next_char} == 0x0022) { # "
2596 !!!cp (187);
2597 $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2598 !!!next-input-character;
2599 redo A;
2600 } elsif ($self->{next_char} == 0x003E) { # >
2601 !!!cp (188);
2602 !!!parse-error (type => 'unclosed PUBLIC literal');
2603
2604 $self->{state} = DATA_STATE;
2605 !!!next-input-character;
2606
2607 $self->{current_token}->{quirks} = 1;
2608 !!!emit ($self->{current_token}); # DOCTYPE
2609
2610 redo A;
2611 } elsif ($self->{next_char} == -1) {
2612 !!!cp (189);
2613 !!!parse-error (type => 'unclosed PUBLIC literal');
2614
2615 $self->{state} = DATA_STATE;
2616 ## reconsume
2617
2618 $self->{current_token}->{quirks} = 1;
2619 !!!emit ($self->{current_token}); # DOCTYPE
2620
2621 redo A;
2622 } else {
2623 !!!cp (190);
2624 $self->{current_token}->{public_identifier} # DOCTYPE
2625 .= chr $self->{next_char};
2626 ## Stay in the state
2627 !!!next-input-character;
2628 redo A;
2629 }
2630 } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2631 if ($self->{next_char} == 0x0027) { # '
2632 !!!cp (191);
2633 $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2634 !!!next-input-character;
2635 redo A;
2636 } elsif ($self->{next_char} == 0x003E) { # >
2637 !!!cp (192);
2638 !!!parse-error (type => 'unclosed PUBLIC literal');
2639
2640 $self->{state} = DATA_STATE;
2641 !!!next-input-character;
2642
2643 $self->{current_token}->{quirks} = 1;
2644 !!!emit ($self->{current_token}); # DOCTYPE
2645
2646 redo A;
2647 } elsif ($self->{next_char} == -1) {
2648 !!!cp (193);
2649 !!!parse-error (type => 'unclosed PUBLIC literal');
2650
2651 $self->{state} = DATA_STATE;
2652 ## reconsume
2653
2654 $self->{current_token}->{quirks} = 1;
2655 !!!emit ($self->{current_token}); # DOCTYPE
2656
2657 redo A;
2658 } else {
2659 !!!cp (194);
2660 $self->{current_token}->{public_identifier} # DOCTYPE
2661 .= chr $self->{next_char};
2662 ## Stay in the state
2663 !!!next-input-character;
2664 redo A;
2665 }
2666 } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2667 if ({
2668 0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2669 #0x000D => 1, # HT, LF, VT, FF, SP, CR
2670 }->{$self->{next_char}}) {
2671 !!!cp (195);
2672 ## Stay in the state
2673 !!!next-input-character;
2674 redo A;
2675 } elsif ($self->{next_char} == 0x0022) { # "
2676 !!!cp (196);
2677 $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2678 $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2679 !!!next-input-character;
2680 redo A;
2681 } elsif ($self->{next_char} == 0x0027) { # '
2682 !!!cp (197);
2683 $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2684 $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2685 !!!next-input-character;
2686 redo A;
2687 } elsif ($self->{next_char} == 0x003E) { # >
2688 !!!cp (198);
2689 $self->{state} = DATA_STATE;
2690 !!!next-input-character;
2691
2692 !!!emit ($self->{current_token}); # DOCTYPE
2693
2694 redo A;
2695 } elsif ($self->{next_char} == -1) {
2696 !!!cp (199);
2697 !!!parse-error (type => 'unclosed DOCTYPE');
2698
2699 $self->{state} = DATA_STATE;
2700 ## reconsume
2701
2702 $self->{current_token}->{quirks} = 1;
2703 !!!emit ($self->{current_token}); # DOCTYPE
2704
2705 redo A;
2706 } else {
2707 !!!cp (200);
2708 !!!parse-error (type => 'string after PUBLIC literal');
2709 $self->{current_token}->{quirks} = 1;
2710
2711 $self->{state} = BOGUS_DOCTYPE_STATE;
2712 !!!next-input-character;
2713 redo A;
2714 }
2715 } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2716 if ({
2717 0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2718 #0x000D => 1, # HT, LF, VT, FF, SP, CR
2719 }->{$self->{next_char}}) {
2720 !!!cp (201);
2721 ## Stay in the state
2722 !!!next-input-character;
2723 redo A;
2724 } elsif ($self->{next_char} == 0x0022) { # "
2725 !!!cp (202);
2726 $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2727 $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2728 !!!next-input-character;
2729 redo A;
2730 } elsif ($self->{next_char} == 0x0027) { # '
2731 !!!cp (203);
2732 $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2733 $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2734 !!!next-input-character;
2735 redo A;
2736 } elsif ($self->{next_char} == 0x003E) { # >
2737 !!!cp (204);
2738 !!!parse-error (type => 'no SYSTEM literal');
2739 $self->{state} = DATA_STATE;
2740 !!!next-input-character;
2741
2742 $self->{current_token}->{quirks} = 1;
2743 !!!emit ($self->{current_token}); # DOCTYPE
2744
2745 redo A;
2746 } elsif ($self->{next_char} == -1) {
2747 !!!cp (205);
2748 !!!parse-error (type => 'unclosed DOCTYPE');
2749
2750 $self->{state} = DATA_STATE;
2751 ## reconsume
2752
2753 $self->{current_token}->{quirks} = 1;
2754 !!!emit ($self->{current_token}); # DOCTYPE
2755
2756 redo A;
2757 } else {
2758 !!!cp (206);
2759 !!!parse-error (type => 'string after SYSTEM');
2760 $self->{current_token}->{quirks} = 1;
2761
2762 $self->{state} = BOGUS_DOCTYPE_STATE;
2763 !!!next-input-character;
2764 redo A;
2765 }
2766 } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2767 if ($self->{next_char} == 0x0022) { # "
2768 !!!cp (207);
2769 $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2770 !!!next-input-character;
2771 redo A;
2772 } elsif ($self->{next_char} == 0x003E) { # >
2773 !!!cp (208);
2774 !!!parse-error (type => 'unclosed SYSTEM literal');
2775
2776 $self->{state} = DATA_STATE;
2777 !!!next-input-character;
2778
2779 $self->{current_token}->{quirks} = 1;
2780 !!!emit ($self->{current_token}); # DOCTYPE
2781
2782 redo A;
2783 } elsif ($self->{next_char} == -1) {
2784 !!!cp (209);
2785 !!!parse-error (type => 'unclosed SYSTEM literal');
2786
2787 $self->{state} = DATA_STATE;
2788 ## reconsume
2789
2790 $self->{current_token}->{quirks} = 1;
2791 !!!emit ($self->{current_token}); # DOCTYPE
2792
2793 redo A;
2794 } else {
2795 !!!cp (210);
2796 $self->{current_token}->{system_identifier} # DOCTYPE
2797 .= chr $self->{next_char};
2798 ## Stay in the state
2799 !!!next-input-character;
2800 redo A;
2801 }
2802 } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2803 if ($self->{next_char} == 0x0027) { # '
2804 !!!cp (211);
2805 $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2806 !!!next-input-character;
2807 redo A;
2808 } elsif ($self->{next_char} == 0x003E) { # >
2809 !!!cp (212);
2810 !!!parse-error (type => 'unclosed SYSTEM literal');
2811
2812 $self->{state} = DATA_STATE;
2813 !!!next-input-character;
2814
2815 $self->{current_token}->{quirks} = 1;
2816 !!!emit ($self->{current_token}); # DOCTYPE
2817
2818 redo A;
2819 } elsif ($self->{next_char} == -1) {
2820 !!!cp (213);
2821 !!!parse-error (type => 'unclosed SYSTEM literal');
2822
2823 $self->{state} = DATA_STATE;
2824 ## reconsume
2825
2826 $self->{current_token}->{quirks} = 1;
2827 !!!emit ($self->{current_token}); # DOCTYPE
2828
2829 redo A;
2830 } else {
2831 !!!cp (214);
2832 $self->{current_token}->{system_identifier} # DOCTYPE
2833 .= chr $self->{next_char};
2834 ## Stay in the state
2835 !!!next-input-character;
2836 redo A;
2837 }
2838 } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2839 if ({
2840 0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2841 #0x000D => 1, # HT, LF, VT, FF, SP, CR
2842 }->{$self->{next_char}}) {
2843 !!!cp (215);
2844 ## Stay in the state
2845 !!!next-input-character;
2846 redo A;
2847 } elsif ($self->{next_char} == 0x003E) { # >
2848 !!!cp (216);
2849 $self->{state} = DATA_STATE;
2850 !!!next-input-character;
2851
2852 !!!emit ($self->{current_token}); # DOCTYPE
2853
2854 redo A;
2855 } elsif ($self->{next_char} == -1) {
2856 !!!cp (217);
2857 !!!parse-error (type => 'unclosed DOCTYPE');
2858 $self->{state} = DATA_STATE;
2859 ## reconsume
2860
2861 $self->{current_token}->{quirks} = 1;
2862 !!!emit ($self->{current_token}); # DOCTYPE
2863
2864 redo A;
2865 } else {
2866 !!!cp (218);
2867 !!!parse-error (type => 'string after SYSTEM literal');
2868 #$self->{current_token}->{quirks} = 1;
2869
2870 $self->{state} = BOGUS_DOCTYPE_STATE;
2871 !!!next-input-character;
2872 redo A;
2873 }
2874 } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2875 if ($self->{next_char} == 0x003E) { # >
2876 !!!cp (219);
2877 $self->{state} = DATA_STATE;
2878 !!!next-input-character;
2879
2880 !!!emit ($self->{current_token}); # DOCTYPE
2881
2882 redo A;
2883 } elsif ($self->{next_char} == -1) {
2884 !!!cp (220);
2885 !!!parse-error (type => 'unclosed DOCTYPE');
2886 $self->{state} = DATA_STATE;
2887 ## reconsume
2888
2889 !!!emit ($self->{current_token}); # DOCTYPE
2890
2891 redo A;
2892 } else {
2893 !!!cp (221);
2894 ## Stay in the state
2895 !!!next-input-character;
2896 redo A;
2897 }
2898 } elsif ($self->{state} == CDATA_SECTION_STATE) {
2899 ## NOTE: "CDATA section state" in the state is jointly implemented
2900 ## by three states, |CDATA_SECTION_STATE|, |CDATA_SECTION_MSE1_STATE|,
2901 ## and |CDATA_SECTION_MSE2_STATE|.
2902
2903 if ($self->{next_char} == 0x005D) { # ]
2904 !!!cp (221.1);
2905 $self->{state} = CDATA_SECTION_MSE1_STATE;
2906 !!!next-input-character;
2907 redo A;
2908 } elsif ($self->{next_char} == -1) {
2909 $self->{state} = DATA_STATE;
2910 !!!next-input-character;
2911 if (length $self->{current_token}->{data}) { # character
2912 !!!cp (221.2);
2913 !!!emit ($self->{current_token}); # character
2914 } else {
2915 !!!cp (221.3);
2916 ## No token to emit. $self->{current_token} is discarded.
2917 }
2918 redo A;
2919 } else {
2920 !!!cp (221.4);
2921 $self->{current_token}->{data} .= chr $self->{next_char};
2922 ## Stay in the state.
2923 !!!next-input-character;
2924 redo A;
2925 }
2926
2927 ## ISSUE: "text tokens" in spec.
2928 } elsif ($self->{state} == CDATA_SECTION_MSE1_STATE) {
2929 if ($self->{next_char} == 0x005D) { # ]
2930 !!!cp (221.5);
2931 $self->{state} = CDATA_SECTION_MSE2_STATE;
2932 !!!next-input-character;
2933 redo A;
2934 } else {
2935 !!!cp (221.6);
2936 $self->{current_token}->{data} .= ']';
2937 $self->{state} = CDATA_SECTION_STATE;
2938 ## Reconsume.
2939 redo A;
2940 }
2941 } elsif ($self->{state} == CDATA_SECTION_MSE2_STATE) {
2942 if ($self->{next_char} == 0x003E) { # >
2943 $self->{state} = DATA_STATE;
2944 !!!next-input-character;
2945 if (length $self->{current_token}->{data}) { # character
2946 !!!cp (221.7);
2947 !!!emit ($self->{current_token}); # character
2948 } else {
2949 !!!cp (221.8);
2950 ## No token to emit. $self->{current_token} is discarded.
2951 }
2952 redo A;
2953 } elsif ($self->{next_char} == 0x005D) { # ]
2954 !!!cp (221.9); # character
2955 $self->{current_token}->{data} .= ']'; ## Add first "]" of "]]]".
2956 ## Stay in the state.
2957 !!!next-input-character;
2958 redo A;
2959 } else {
2960 !!!cp (221.11);
2961 $self->{current_token}->{data} .= ']]'; # character
2962 $self->{state} = CDATA_SECTION_STATE;
2963 ## Reconsume.
2964 redo A;
2965 }
2966 } else {
2967 die "$0: $self->{state}: Unknown state";
2968 }
2969 } # A
2970
2971 die "$0: _get_next_token: unexpected case";
2972 } # _get_next_token
2973
2974 sub _tokenize_attempt_to_consume_an_entity ($$$) {
2975 my ($self, $in_attr, $additional) = @_;
2976
2977 my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2978
2979 if ({
2980 0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2981 0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2982 $additional => 1,
2983 }->{$self->{next_char}}) {
2984 !!!cp (1001);
2985 ## Don't consume
2986 ## No error
2987 return undef;
2988 } elsif ($self->{next_char} == 0x0023) { # #
2989 !!!next-input-character;
2990 if ($self->{next_char} == 0x0078 or # x
2991 $self->{next_char} == 0x0058) { # X
2992 my $code;
2993 X: {
2994 my $x_char = $self->{next_char};
2995 !!!next-input-character;
2996 if (0x0030 <= $self->{next_char} and
2997 $self->{next_char} <= 0x0039) { # 0..9
2998 !!!cp (1002);
2999 $code ||= 0;
3000 $code *= 0x10;
3001 $code += $self->{next_char} - 0x0030;
3002 redo X;
3003 } elsif (0x0061 <= $self->{next_char} and
3004 $self->{next_char} <= 0x0066) { # a..f
3005 !!!cp (1003);
3006 $code ||= 0;
3007 $code *= 0x10;
3008 $code += $self->{next_char} - 0x0060 + 9;
3009 redo X;
3010 } elsif (0x0041 <= $self->{next_char} and
3011 $self->{next_char} <= 0x0046) { # A..F
3012 !!!cp (1004);
3013 $code ||= 0;
3014 $code *= 0x10;
3015 $code += $self->{next_char} - 0x0040 + 9;
3016 redo X;
3017 } elsif (not defined $code) { # no hexadecimal digit
3018 !!!cp (1005);
3019 !!!parse-error (type => 'bare hcro', line => $l, column => $c);
3020 !!!back-next-input-character ($x_char, $self->{next_char});
3021 $self->{next_char} = 0x0023; # #
3022 return undef;
3023 } elsif ($self->{next_char} == 0x003B) { # ;
3024 !!!cp (1006);
3025 !!!next-input-character;
3026 } else {
3027 !!!cp (1007);
3028 !!!parse-error (type => 'no refc', line => $l, column => $c);
3029 }
3030
3031 if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3032 !!!cp (1008);
3033 !!!parse-error (type => 'invalid character reference',
3034 text => (sprintf 'U+%04X', $code),
3035 line => $l, column => $c);
3036 $code = 0xFFFD;
3037 } elsif ($code > 0x10FFFF) {
3038 !!!cp (1009);
3039 !!!parse-error (type => 'invalid character reference',
3040 text => (sprintf 'U-%08X', $code),
3041 line => $l, column => $c);
3042 $code = 0xFFFD;
3043 } elsif ($code == 0x000D) {
3044 !!!cp (1010);
3045 !!!parse-error (type => 'CR character reference', line => $l, column => $c);
3046 $code = 0x000A;
3047 } elsif (0x80 <= $code and $code <= 0x9F) {
3048 !!!cp (1011);
3049 !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
3050 $code = $c1_entity_char->{$code};
3051 }
3052
3053 return {type => CHARACTER_TOKEN, data => chr $code,
3054 has_reference => 1,
3055 line => $l, column => $c,
3056 };
3057 } # X
3058 } elsif (0x0030 <= $self->{next_char} and
3059 $self->{next_char} <= 0x0039) { # 0..9
3060 my $code = $self->{next_char} - 0x0030;
3061 !!!next-input-character;
3062
3063 while (0x0030 <= $self->{next_char} and
3064 $self->{next_char} <= 0x0039) { # 0..9
3065 !!!cp (1012);
3066 $code *= 10;
3067 $code += $self->{next_char} - 0x0030;
3068
3069 !!!next-input-character;
3070 }
3071
3072 if ($self->{next_char} == 0x003B) { # ;
3073 !!!cp (1013);
3074 !!!next-input-character;
3075 } else {
3076 !!!cp (1014);
3077 !!!parse-error (type => 'no refc', line => $l, column => $c);
3078 }
3079
3080 if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3081 !!!cp (1015);
3082 !!!parse-error (type => 'invalid character reference',
3083 text => (sprintf 'U+%04X', $code),
3084 line => $l, column => $c);
3085 $code = 0xFFFD;
3086 } elsif ($code > 0x10FFFF) {
3087 !!!cp (1016);
3088 !!!parse-error (type => 'invalid character reference',
3089 text => (sprintf 'U-%08X', $code),
3090 line => $l, column => $c);
3091 $code = 0xFFFD;
3092 } elsif ($code == 0x000D) {
3093 !!!cp (1017);
3094 !!!parse-error (type => 'CR character reference',
3095 line => $l, column => $c);
3096 $code = 0x000A;
3097 } elsif (0x80 <= $code and $code <= 0x9F) {
3098 !!!cp (1018);
3099 !!!parse-error (type => 'C1 character reference',
3100 text => (sprintf 'U+%04X', $code),
3101 line => $l, column => $c);
3102 $code = $c1_entity_char->{$code};
3103 }
3104
3105 return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
3106 line => $l, column => $c,
3107 };
3108 } else {
3109 !!!cp (1019);
3110 !!!parse-error (type => 'bare nero', line => $l, column => $c);
3111 !!!back-next-input-character ($self->{next_char});
3112 $self->{next_char} = 0x0023; # #
3113 return undef;
3114 }
3115 } elsif ((0x0041 <= $self->{next_char} and
3116 $self->{next_char} <= 0x005A) or
3117 (0x0061 <= $self->{next_char} and
3118 $self->{next_char} <= 0x007A)) {
3119 my $entity_name = chr $self->{next_char};
3120 !!!next-input-character;
3121
3122 my $value = $entity_name;
3123 my $match = 0;
3124 require Whatpm::_NamedEntityList;
3125 our $EntityChar;
3126
3127 while (length $entity_name < 30 and
3128 ## NOTE: Some number greater than the maximum length of entity name
3129 ((0x0041 <= $self->{next_char} and # a
3130 $self->{next_char} <= 0x005A) or # x
3131 (0x0061 <= $self->{next_char} and # a
3132 $self->{next_char} <= 0x007A) or # z
3133 (0x0030 <= $self->{next_char} and # 0
3134 $self->{next_char} <= 0x0039) or # 9
3135 $self->{next_char} == 0x003B)) { # ;
3136 $entity_name .= chr $self->{next_char};
3137 if (defined $EntityChar->{$entity_name}) {
3138 if ($self->{next_char} == 0x003B) { # ;
3139 !!!cp (1020);
3140 $value = $EntityChar->{$entity_name};
3141 $match = 1;
3142 !!!next-input-character;
3143 last;
3144 } else {
3145 !!!cp (1021);
3146 $value = $EntityChar->{$entity_name};
3147 $match = -1;
3148 !!!next-input-character;
3149 }
3150 } else {
3151 !!!cp (1022);
3152 $value .= chr $self->{next_char};
3153 $match *= 2;
3154 !!!next-input-character;
3155 }
3156 }
3157
3158 if ($match > 0) {
3159 !!!cp (1023);
3160 return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3161 line => $l, column => $c,
3162 };
3163 } elsif ($match < 0) {
3164 !!!parse-error (type => 'no refc', line => $l, column => $c);
3165 if ($in_attr and $match < -1) {
3166 !!!cp (1024);
3167 return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
3168 line => $l, column => $c,
3169 };
3170 } else {
3171 !!!cp (1025);
3172 return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3173 line => $l, column => $c,
3174 };
3175 }
3176 } else {
3177 !!!cp (1026);
3178 !!!parse-error (type => 'bare ero', line => $l, column => $c);
3179 ## NOTE: "No characters are consumed" in the spec.
3180 return {type => CHARACTER_TOKEN, data => '&'.$value,
3181 line => $l, column => $c,
3182 };
3183 }
3184 } else {
3185 !!!cp (1027);
3186 ## no characters are consumed
3187 !!!parse-error (type => 'bare ero', line => $l, column => $c);
3188 return undef;
3189 }
3190 } # _tokenize_attempt_to_consume_an_entity
3191
3192 sub _initialize_tree_constructor ($) {
3193 my $self = shift;
3194 ## NOTE: $self->{document} MUST be specified before this method is called
3195 $self->{document}->strict_error_checking (0);
3196 ## TODO: Turn mutation events off # MUST
3197 ## TODO: Turn loose Document option (manakai extension) on
3198 $self->{document}->manakai_is_html (1); # MUST
3199 $self->{document}->set_user_data (manakai_source_line => 1);
3200 $self->{document}->set_user_data (manakai_source_column => 1);
3201 } # _initialize_tree_constructor
3202
3203 sub _terminate_tree_constructor ($) {
3204 my $self = shift;
3205 $self->{document}->strict_error_checking (1);
3206 ## TODO: Turn mutation events on
3207 } # _terminate_tree_constructor
3208
3209 ## ISSUE: Should append_child (for example) in script executed in tree construction stage fire mutation events?
3210
3211 { # tree construction stage
3212 my $token;
3213
3214 sub _construct_tree ($) {
3215 my ($self) = @_;
3216
3217 ## When an interactive UA render the $self->{document} available
3218 ## to the user, or when it begin accepting user input, are
3219 ## not defined.
3220
3221 ## Append a character: collect it and all subsequent consecutive
3222 ## characters and insert one Text node whose data is concatenation
3223 ## of all those characters. # MUST
3224
3225 !!!next-token;
3226
3227 undef $self->{form_element};
3228 undef $self->{head_element};
3229 $self->{open_elements} = [];
3230 undef $self->{inner_html_node};
3231
3232 ## NOTE: The "initial" insertion mode.
3233 $self->_tree_construction_initial; # MUST
3234
3235 ## NOTE: The "before html" insertion mode.
3236 $self->_tree_construction_root_element;
3237 $self->{insertion_mode} = BEFORE_HEAD_IM;
3238
3239 ## NOTE: The "before head" insertion mode and so on.
3240 $self->_tree_construction_main;
3241 } # _construct_tree
3242
3243 sub _tree_construction_initial ($) {
3244 my $self = shift;
3245
3246 ## NOTE: "initial" insertion mode
3247
3248 INITIAL: {
3249 if ($token->{type} == DOCTYPE_TOKEN) {
3250 ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
3251 ## error, switch to a conformance checking mode for another
3252 ## language.
3253 my $doctype_name = $token->{name};
3254 $doctype_name = '' unless defined $doctype_name;
3255 $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3256 if (not defined $token->{name} or # <!DOCTYPE>
3257 defined $token->{system_identifier}) {
3258 !!!cp ('t1');
3259 !!!parse-error (type => 'not HTML5', token => $token);
3260 } elsif ($doctype_name ne 'HTML') {
3261 !!!cp ('t2');
3262 !!!parse-error (type => 'not HTML5', token => $token);
3263 } elsif (defined $token->{public_identifier}) {
3264 if ($token->{public_identifier} eq 'XSLT-compat') {
3265 !!!cp ('t1.2');
3266 !!!parse-error (type => 'XSLT-compat', token => $token,
3267 level => $self->{level}->{should});
3268 } else {
3269 !!!parse-error (type => 'not HTML5', token => $token);
3270 }
3271 } else {
3272 !!!cp ('t3');
3273 #
3274 }
3275
3276 my $doctype = $self->{document}->create_document_type_definition
3277 ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3278 ## NOTE: Default value for both |public_id| and |system_id| attributes
3279 ## are empty strings, so that we don't set any value in missing cases.
3280 $doctype->public_id ($token->{public_identifier})
3281 if defined $token->{public_identifier};
3282 $doctype->system_id ($token->{system_identifier})
3283 if defined $token->{system_identifier};
3284 ## NOTE: Other DocumentType attributes are null or empty lists.
3285 ## ISSUE: internalSubset = null??
3286 $self->{document}->append_child ($doctype);
3287
3288 if ($token->{quirks} or $doctype_name ne 'HTML') {
3289 !!!cp ('t4');
3290 $self->{document}->manakai_compat_mode ('quirks');
3291 } elsif (defined $token->{public_identifier}) {
3292 my $pubid = $token->{public_identifier};
3293 $pubid =~ tr/a-z/A-z/;
3294 my $prefix = [
3295 "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3296 "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3297 "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3298 "-//IETF//DTD HTML 2.0 LEVEL 1//",
3299 "-//IETF//DTD HTML 2.0 LEVEL 2//",
3300 "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3301 "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3302 "-//IETF//DTD HTML 2.0 STRICT//",
3303 "-//IETF//DTD HTML 2.0//",
3304 "-//IETF//DTD HTML 2.1E//",
3305 "-//IETF//DTD HTML 3.0//",
3306 "-//IETF//DTD HTML 3.2 FINAL//",
3307 "-//IETF//DTD HTML 3.2//",
3308 "-//IETF//DTD HTML 3//",
3309 "-//IETF//DTD HTML LEVEL 0//",
3310 "-//IETF//DTD HTML LEVEL 1//",
3311 "-//IETF//DTD HTML LEVEL 2//",
3312 "-//IETF//DTD HTML LEVEL 3//",
3313 "-//IETF//DTD HTML STRICT LEVEL 0//",
3314 "-//IETF//DTD HTML STRICT LEVEL 1//",
3315 "-//IETF//DTD HTML STRICT LEVEL 2//",
3316 "-//IETF//DTD HTML STRICT LEVEL 3//",
3317 "-//IETF//DTD HTML STRICT//",
3318 "-//IETF//DTD HTML//",
3319 "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3320 "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3321 "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3322 "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3323 "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3324 "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3325 "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3326 "-//NETSCAPE COMM. CORP.//DTD HTML//",
3327 "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3328 "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3329 "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3330 "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3331 "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3332 "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3333 "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3334 "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3335 "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3336 "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3337 "-//W3C//DTD HTML 3 1995-03-24//",
3338 "-//W3C//DTD HTML 3.2 DRAFT//",
3339 "-//W3C//DTD HTML 3.2 FINAL//",
3340 "-//W3C//DTD HTML 3.2//",
3341 "-//W3C//DTD HTML 3.2S DRAFT//",
3342 "-//W3C//DTD HTML 4.0 FRAMESET//",
3343 "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3344 "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3345 "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3346 "-//W3C//DTD W3 HTML//",
3347 "-//W3O//DTD W3 HTML 3.0//",
3348 "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3349 "-//WEBTECHS//DTD MOZILLA HTML//",
3350 ]; # $prefix
3351 my $match;
3352 for (@$prefix) {
3353 if (substr ($prefix, 0, length $_) eq $_) {
3354 $match = 1;
3355 last;
3356 }
3357 }
3358 if ($match or
3359 $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3360 $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3361 $pubid eq "HTML") {
3362 !!!cp ('t5');
3363 $self->{document}->manakai_compat_mode ('quirks');
3364 } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3365 $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3366 if (defined $token->{system_identifier}) {
3367 !!!cp ('t6');
3368 $self->{document}->manakai_compat_mode ('quirks');
3369 } else {
3370 !!!cp ('t7');
3371 $self->{document}->manakai_compat_mode ('limited quirks');
3372 }
3373 } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3374 $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3375 !!!cp ('t8');
3376 $self->{document}->manakai_compat_mode ('limited quirks');
3377 } else {
3378 !!!cp ('t9');
3379 }
3380 } else {
3381 !!!cp ('t10');
3382 }
3383 if (defined $token->{system_identifier}) {
3384 my $sysid = $token->{system_identifier};
3385 $sysid =~ tr/A-Z/a-z/;
3386 if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
3387 ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3388 ## marked as quirks.
3389 $self->{document}->manakai_compat_mode ('quirks');
3390 !!!cp ('t11');
3391 } else {
3392 !!!cp ('t12');
3393 }
3394 } else {
3395 !!!cp ('t13');
3396 }
3397
3398 ## Go to the "before html" insertion mode.
3399 !!!next-token;
3400 return;
3401 } elsif ({
3402 START_TAG_TOKEN, 1,
3403 END_TAG_TOKEN, 1,
3404 END_OF_FILE_TOKEN, 1,
3405 }->{$token->{type}}) {
3406 !!!cp ('t14');
3407 !!!parse-error (type => 'no DOCTYPE', token => $token);
3408 $self->{document}->manakai_compat_mode ('quirks');
3409 ## Go to the "before html" insertion mode.
3410 ## reprocess
3411 !!!ack-later;
3412 return;
3413 } elsif ($token->{type} == CHARACTER_TOKEN) {
3414 if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3415 ## Ignore the token
3416
3417 unless (length $token->{data}) {
3418 !!!cp ('t15');
3419 ## Stay in the insertion mode.
3420 !!!next-token;
3421 redo INITIAL;
3422 } else {
3423 !!!cp ('t16');
3424 }
3425 } else {
3426 !!!cp ('t17');
3427 }
3428
3429 !!!parse-error (type => 'no DOCTYPE', token => $token);
3430 $self->{document}->manakai_compat_mode ('quirks');
3431 ## Go to the "before html" insertion mode.
3432 ## reprocess
3433 return;
3434 } elsif ($token->{type} == COMMENT_TOKEN) {
3435 !!!cp ('t18');
3436 my $comment = $self->{document}->create_comment ($token->{data});
3437 $self->{document}->append_child ($comment);
3438
3439 ## Stay in the insertion mode.
3440 !!!next-token;
3441 redo INITIAL;
3442 } else {
3443 die "$0: $token->{type}: Unknown token type";
3444 }
3445 } # INITIAL
3446
3447 die "$0: _tree_construction_initial: This should be never reached";
3448 } # _tree_construction_initial
3449
3450 sub _tree_construction_root_element ($) {
3451 my $self = shift;
3452
3453 ## NOTE: "before html" insertion mode.
3454
3455 B: {
3456 if ($token->{type} == DOCTYPE_TOKEN) {
3457 !!!cp ('t19');
3458 !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3459 ## Ignore the token
3460 ## Stay in the insertion mode.
3461 !!!next-token;
3462 redo B;
3463 } elsif ($token->{type} == COMMENT_TOKEN) {
3464 !!!cp ('t20');
3465 my $comment = $self->{document}->create_comment ($token->{data});
3466 $self->{document}->append_child ($comment);
3467 ## Stay in the insertion mode.
3468 !!!next-token;
3469 redo B;
3470 } elsif ($token->{type} == CHARACTER_TOKEN) {
3471 if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3472 ## Ignore the token.
3473
3474 unless (length $token->{data}) {
3475 !!!cp ('t21');
3476 ## Stay in the insertion mode.
3477 !!!next-token;
3478 redo B;
3479 } else {
3480 !!!cp ('t22');
3481 }
3482 } else {
3483 !!!cp ('t23');
3484 }
3485
3486 $self->{application_cache_selection}->(undef);
3487
3488 #
3489 } elsif ($token->{type} == START_TAG_TOKEN) {
3490 if ($token->{tag_name} eq 'html') {
3491 my $root_element;
3492 !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3493 $self->{document}->append_child ($root_element);
3494 push @{$self->{open_elements}},
3495 [$root_element, $el_category->{html}];
3496
3497 if ($token->{attributes}->{manifest}) {
3498 !!!cp ('t24');
3499 $self->{application_cache_selection}
3500 ->($token->{attributes}->{manifest}->{value});
3501 ## ISSUE: Spec is unclear on relative references.
3502 ## According to Hixie (#whatwg 2008-03-19), it should be
3503 ## resolved against the base URI of the document in HTML
3504 ## or xml:base of the element in XHTML.
3505 } else {
3506 !!!cp ('t25');
3507 $self->{application_cache_selection}->(undef);
3508 }
3509
3510 !!!nack ('t25c');
3511
3512 !!!next-token;
3513 return; ## Go to the "before head" insertion mode.
3514 } else {
3515 !!!cp ('t25.1');
3516 #
3517 }
3518 } elsif ({
3519 END_TAG_TOKEN, 1,
3520 END_OF_FILE_TOKEN, 1,
3521 }->{$token->{type}}) {
3522 !!!cp ('t26');
3523 #
3524 } else {
3525 die "$0: $token->{type}: Unknown token type";
3526 }
3527
3528 my $root_element;
3529 !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3530 $self->{document}->append_child ($root_element);
3531 push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3532
3533 $self->{application_cache_selection}->(undef);
3534
3535 ## NOTE: Reprocess the token.
3536 !!!ack-later;
3537 return; ## Go to the "before head" insertion mode.
3538
3539 ## ISSUE: There is an issue in the spec
3540 } # B
3541
3542 die "$0: _tree_construction_root_element: This should never be reached";
3543 } # _tree_construction_root_element
3544
3545 sub _reset_insertion_mode ($) {
3546 my $self = shift;
3547
3548 ## Step 1
3549 my $last;
3550
3551 ## Step 2
3552 my $i = -1;
3553 my $node = $self->{open_elements}->[$i];
3554
3555 ## Step 3
3556 S3: {
3557 if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3558 $last = 1;
3559 if (defined $self->{inner_html_node}) {
3560 !!!cp ('t28');
3561 $node = $self->{inner_html_node};
3562 } else {
3563 die "_reset_insertion_mode: t27";
3564 }
3565 }
3566
3567 ## Step 4..14
3568 my $new_mode;
3569 if ($node->[1] & FOREIGN_EL) {
3570 !!!cp ('t28.1');
3571 ## NOTE: Strictly spaking, the line below only applies to MathML and
3572 ## SVG elements. Currently the HTML syntax supports only MathML and
3573 ## SVG elements as foreigners.
3574 $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3575 } elsif ($node->[1] & TABLE_CELL_EL) {
3576 if ($last) {
3577 !!!cp ('t28.2');
3578 #
3579 } else {
3580 !!!cp ('t28.3');
3581 $new_mode = IN_CELL_IM;
3582 }
3583 } else {
3584 !!!cp ('t28.4');
3585 $new_mode = {
3586 select => IN_SELECT_IM,
3587 ## NOTE: |option| and |optgroup| do not set
3588 ## insertion mode to "in select" by themselves.
3589 tr => IN_ROW_IM,
3590 tbody => IN_TABLE_BODY_IM,
3591 thead => IN_TABLE_BODY_IM,
3592 tfoot => IN_TABLE_BODY_IM,
3593 caption => IN_CAPTION_IM,
3594 colgroup => IN_COLUMN_GROUP_IM,
3595 table => IN_TABLE_IM,
3596 head => IN_BODY_IM, # not in head!
3597 body => IN_BODY_IM,
3598 frameset => IN_FRAMESET_IM,
3599 }->{$node->[0]->manakai_local_name};
3600 }
3601 $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3602
3603 ## Step 15
3604 if ($node->[1] & HTML_EL) {
3605 unless (defined $self->{head_element}) {
3606 !!!cp ('t29');
3607 $self->{insertion_mode} = BEFORE_HEAD_IM;
3608 } else {
3609 ## ISSUE: Can this state be reached?
3610 !!!cp ('t30');
3611 $self->{insertion_mode} = AFTER_HEAD_IM;
3612 }
3613 return;
3614 } else {
3615 !!!cp ('t31');
3616 }
3617
3618 ## Step 16
3619 $self->{insertion_mode} = IN_BODY_IM and return if $last;
3620
3621 ## Step 17
3622 $i--;
3623 $node = $self->{open_elements}->[$i];
3624
3625 ## Step 18
3626 redo S3;
3627 } # S3
3628
3629 die "$0: _reset_insertion_mode: This line should never be reached";
3630 } # _reset_insertion_mode
3631
3632 sub _tree_construction_main ($) {
3633 my $self = shift;
3634
3635 my $active_formatting_elements = [];
3636
3637 my $reconstruct_active_formatting_elements = sub { # MUST
3638 my $insert = shift;
3639
3640 ## Step 1
3641 return unless @$active_formatting_elements;
3642
3643 ## Step 3
3644 my $i = -1;
3645 my $entry = $active_formatting_elements->[$i];
3646
3647 ## Step 2
3648 return if $entry->[0] eq '#marker';
3649 for (@{$self->{open_elements}}) {
3650 if ($entry->[0] eq $_->[0]) {
3651 !!!cp ('t32');
3652 return;
3653 }
3654 }
3655
3656 S4: {
3657 ## Step 4
3658 last S4 if $active_formatting_elements->[0]->[0] eq $entry->[0];
3659
3660 ## Step 5
3661 $i--;
3662 $entry = $active_formatting_elements->[$i];
3663
3664 ## Step 6
3665 if ($entry->[0] eq '#marker') {
3666 !!!cp ('t33_1');
3667 #
3668 } else {
3669 my $in_open_elements;
3670 OE: for (@{$self->{open_elements}}) {
3671 if ($entry->[0] eq $_->[0]) {
3672 !!!cp ('t33');
3673 $in_open_elements = 1;
3674 last OE;
3675 }
3676 }
3677 if ($in_open_elements) {
3678 !!!cp ('t34');
3679 #
3680 } else {
3681 ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3682 !!!cp ('t35');
3683 redo S4;
3684 }
3685 }
3686
3687 ## Step 7
3688 $i++;
3689 $entry = $active_formatting_elements->[$i];
3690 } # S4
3691
3692 S7: {
3693 ## Step 8
3694 my $clone = [$entry->[0]->clone_node (0), $entry->[1]];
3695
3696 ## Step 9
3697 $insert->($clone->[0]);
3698 push @{$self->{open_elements}}, $clone;
3699
3700 ## Step 10
3701 $active_formatting_elements->[$i] = $self->{open_elements}->[-1];
3702
3703 ## Step 11
3704 unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3705 !!!cp ('t36');
3706 ## Step 7'
3707 $i++;
3708 $entry = $active_formatting_elements->[$i];
3709
3710 redo S7;
3711 }
3712
3713 !!!cp ('t37');
3714 } # S7
3715 }; # $reconstruct_active_formatting_elements
3716
3717 my $clear_up_to_marker = sub {
3718 for (reverse 0..$#$active_formatting_elements) {
3719 if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3720 !!!cp ('t38');
3721 splice @$active_formatting_elements, $_;
3722 return;
3723 }
3724 }
3725
3726 !!!cp ('t39');
3727 }; # $clear_up_to_marker
3728
3729 my $insert;
3730
3731 my $parse_rcdata = sub ($) {
3732 my ($content_model_flag) = @_;
3733
3734 ## Step 1
3735 my $start_tag_name = $token->{tag_name};
3736 my $el;
3737 !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3738
3739 ## Step 2
3740 $insert->($el);
3741
3742 ## Step 3
3743 $self->{content_model} = $content_model_flag; # CDATA or RCDATA
3744 delete $self->{escape}; # MUST
3745
3746 ## Step 4
3747 my $text = '';
3748 !!!nack ('t40.1');
3749 !!!next-token;
3750 while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3751 !!!cp ('t40');
3752 $text .= $token->{data};
3753 !!!next-token;
3754 }
3755
3756 ## Step 5
3757 if (length $text) {
3758 !!!cp ('t41');
3759 my $text = $self->{document}->create_text_node ($text);
3760 $el->append_child ($text);
3761 }
3762
3763 ## Step 6
3764 $self->{content_model} = PCDATA_CONTENT_MODEL;
3765
3766 ## Step 7
3767 if ($token->{type} == END_TAG_TOKEN and
3768 $token->{tag_name} eq $start_tag_name) {
3769 !!!cp ('t42');
3770 ## Ignore the token
3771 } else {
3772 ## NOTE: An end-of-file token.
3773 if ($content_model_flag == CDATA_CONTENT_MODEL) {
3774 !!!cp ('t43');
3775 !!!parse-error (type => 'in CDATA:#eof', token => $token);
3776 } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3777 !!!cp ('t44');
3778 !!!parse-error (type => 'in RCDATA:#eof', token => $token);
3779 } else {
3780 die "$0: $content_model_flag in parse_rcdata";
3781 }
3782 }
3783 !!!next-token;
3784 }; # $parse_rcdata
3785
3786 my $script_start_tag = sub () {
3787 my $script_el;
3788 !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3789 ## TODO: mark as "parser-inserted"
3790
3791 $self->{content_model} = CDATA_CONTENT_MODEL;
3792 delete $self->{escape}; # MUST
3793
3794 my $text = '';
3795 !!!nack ('t45.1');
3796 !!!next-token;
3797 while ($token->{type} == CHARACTER_TOKEN) {
3798 !!!cp ('t45');
3799 $text .= $token->{data};
3800 !!!next-token;
3801 } # stop if non-character token or tokenizer stops tokenising
3802 if (length $text) {
3803 !!!cp ('t46');
3804 $script_el->manakai_append_text ($text);
3805 }
3806
3807 $self->{content_model} = PCDATA_CONTENT_MODEL;
3808
3809 if ($token->{type} == END_TAG_TOKEN and
3810 $token->{tag_name} eq 'script') {
3811 !!!cp ('t47');
3812 ## Ignore the token
3813 } else {
3814 !!!cp ('t48');
3815 !!!parse-error (type => 'in CDATA:#eof', token => $token);
3816 ## ISSUE: And ignore?
3817 ## TODO: mark as "already executed"
3818 }
3819
3820 if (defined $self->{inner_html_node}) {
3821 !!!cp ('t49');
3822 ## TODO: mark as "already executed"
3823 } else {
3824 !!!cp ('t50');
3825 ## TODO: $old_insertion_point = current insertion point
3826 ## TODO: insertion point = just before the next input character
3827
3828 $insert->($script_el);
3829
3830 ## TODO: insertion point = $old_insertion_point (might be "undefined")
3831
3832 ## TODO: if there is a script that will execute as soon as the parser resume, then...
3833 }
3834
3835 !!!next-token;
3836 }; # $script_start_tag
3837
3838 ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3839 ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3840 my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3841
3842 my $formatting_end_tag = sub {
3843 my $end_tag_token = shift;
3844 my $tag_name = $end_tag_token->{tag_name};
3845
3846 ## NOTE: The adoption agency algorithm (AAA).
3847
3848 FET: {
3849 ## Step 1
3850 my $formatting_element;
3851 my $formatting_element_i_in_active;
3852 AFE: for (reverse 0..$#$active_formatting_elements) {
3853 if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3854 !!!cp ('t52');
3855 last AFE;
3856 } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3857 eq $tag_name) {
3858 !!!cp ('t51');
3859 $formatting_element = $active_formatting_elements->[$_];
3860 $formatting_element_i_in_active = $_;
3861 last AFE;
3862 }
3863 } # AFE
3864 unless (defined $formatting_element) {
3865 !!!cp ('t53');
3866 !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
3867 ## Ignore the token
3868 !!!next-token;
3869 return;
3870 }
3871 ## has an element in scope
3872 my $in_scope = 1;
3873 my $formatting_element_i_in_open;
3874 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3875 my $node = $self->{open_elements}->[$_];
3876 if ($node->[0] eq $formatting_element->[0]) {
3877 if ($in_scope) {
3878 !!!cp ('t54');
3879 $formatting_element_i_in_open = $_;
3880 last INSCOPE;
3881 } else { # in open elements but not in scope
3882 !!!cp ('t55');
3883 !!!parse-error (type => 'unmatched end tag',
3884 text => $token->{tag_name},
3885 token => $end_tag_token);
3886 ## Ignore the token
3887 !!!next-token;
3888 return;
3889 }
3890 } elsif ($node->[1] & SCOPING_EL) {
3891 !!!cp ('t56');
3892 $in_scope = 0;
3893 }
3894 } # INSCOPE
3895 unless (defined $formatting_element_i_in_open) {
3896 !!!cp ('t57');
3897 !!!parse-error (type => 'unmatched end tag',
3898 text => $token->{tag_name},
3899 token => $end_tag_token);
3900 pop @$active_formatting_elements; # $formatting_element
3901 !!!next-token; ## TODO: ok?
3902 return;
3903 }
3904 if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3905 !!!cp ('t58');
3906 !!!parse-error (type => 'not closed',
3907 text => $self->{open_elements}->[-1]->[0]
3908 ->manakai_local_name,
3909 token => $end_tag_token);
3910 }
3911
3912 ## Step 2
3913 my $furthest_block;
3914 my $furthest_block_i_in_open;
3915 OE: for (reverse 0..$#{$self->{open_elements}}) {
3916 my $node = $self->{open_elements}->[$_];
3917 if (not ($node->[1] & FORMATTING_EL) and
3918 #not $phrasing_category->{$node->[1]} and
3919 ($node->[1] & SPECIAL_EL or
3920 $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3921 !!!cp ('t59');
3922 $furthest_block = $node;
3923 $furthest_block_i_in_open = $_;
3924 } elsif ($node->[0] eq $formatting_element->[0]) {
3925 !!!cp ('t60');
3926 last OE;
3927 }
3928 } # OE
3929
3930 ## Step 3
3931 unless (defined $furthest_block) { # MUST
3932 !!!cp ('t61');
3933 splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3934 splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3935 !!!next-token;
3936 return;
3937 }
3938
3939 ## Step 4
3940 my $common_ancestor_node = $self->{open_elements}->[$formatting_element_i_in_open - 1];
3941
3942 ## Step 5
3943 my $furthest_block_parent = $furthest_block->[0]->parent_node;
3944 if (defined $furthest_block_parent) {
3945 !!!cp ('t62');
3946 $furthest_block_parent->remove_child ($furthest_block->[0]);
3947 }
3948
3949 ## Step 6
3950 my $bookmark_prev_el
3951 = $active_formatting_elements->[$formatting_element_i_in_active - 1]
3952 ->[0];
3953
3954 ## Step 7
3955 my $node = $furthest_block;
3956 my $node_i_in_open = $furthest_block_i_in_open;
3957 my $last_node = $furthest_block;
3958 S7: {
3959 ## Step 1
3960 $node_i_in_open--;
3961 $node = $self->{open_elements}->[$node_i_in_open];
3962
3963 ## Step 2
3964 my $node_i_in_active;
3965 S7S2: {
3966 for (reverse 0..$#$active_formatting_elements) {
3967 if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3968 !!!cp ('t63');
3969 $node_i_in_active = $_;
3970 last S7S2;
3971 }
3972 }
3973 splice @{$self->{open_elements}}, $node_i_in_open, 1;
3974 redo S7;
3975 } # S7S2
3976
3977 ## Step 3
3978 last S7 if $node->[0] eq $formatting_element->[0];
3979
3980 ## Step 4
3981 if ($last_node->[0] eq $furthest_block->[0]) {
3982 !!!cp ('t64');
3983 $bookmark_prev_el = $node->[0];
3984 }
3985
3986 ## Step 5
3987 if ($node->[0]->has_child_nodes ()) {
3988 !!!cp ('t65');
3989 my $clone = [$node->[0]->clone_node (0), $node->[1]];
3990 $active_formatting_elements->[$node_i_in_active] = $clone;
3991 $self->{open_elements}->[$node_i_in_open] = $clone;
3992 $node = $clone;
3993 }
3994
3995 ## Step 6
3996 $node->[0]->append_child ($last_node->[0]);
3997
3998 ## Step 7
3999 $last_node = $node;
4000
4001 ## Step 8
4002 redo S7;
4003 } # S7
4004
4005 ## Step 8
4006 if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
4007 my $foster_parent_element;
4008 my $next_sibling;
4009 OE: for (reverse 0..$#{$self->{open_elements}}) {
4010 if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4011 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4012 if (defined $parent and $parent->node_type == 1) {
4013 !!!cp ('t65.1');
4014 $foster_parent_element = $parent;
4015 $next_sibling = $self->{open_elements}->[$_]->[0];
4016 } else {
4017 !!!cp ('t65.2');
4018 $foster_parent_element
4019 = $self->{open_elements}->[$_ - 1]->[0];
4020 }
4021 last OE;
4022 }
4023 } # OE
4024 $foster_parent_element = $self->{open_elements}->[0]->[0]
4025 unless defined $foster_parent_element;
4026 $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
4027 $open_tables->[-1]->[1] = 1; # tainted
4028 } else {
4029 !!!cp ('t65.3');
4030 $common_ancestor_node->[0]->append_child ($last_node->[0]);
4031 }
4032
4033 ## Step 9
4034 my $clone = [$formatting_element->[0]->clone_node (0),
4035 $formatting_element->[1]];
4036
4037 ## Step 10
4038 my @cn = @{$furthest_block->[0]->child_nodes};
4039 $clone->[0]->append_child ($_) for @cn;
4040
4041 ## Step 11
4042 $furthest_block->[0]->append_child ($clone->[0]);
4043
4044 ## Step 12
4045 my $i;
4046 AFE: for (reverse 0..$#$active_formatting_elements) {
4047 if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
4048 !!!cp ('t66');
4049 splice @$active_formatting_elements, $_, 1;
4050 $i-- and last AFE if defined $i;
4051 } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
4052 !!!cp ('t67');
4053 $i = $_;
4054 }
4055 } # AFE
4056 splice @$active_formatting_elements, $i + 1, 0, $clone;
4057
4058 ## Step 13
4059 undef $i;
4060 OE: for (reverse 0..$#{$self->{open_elements}}) {
4061 if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
4062 !!!cp ('t68');
4063 splice @{$self->{open_elements}}, $_, 1;
4064 $i-- and last OE if defined $i;
4065 } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
4066 !!!cp ('t69');
4067 $i = $_;
4068 }
4069 } # OE
4070 splice @{$self->{open_elements}}, $i + 1, 1, $clone;
4071
4072 ## Step 14
4073 redo FET;
4074 } # FET
4075 }; # $formatting_end_tag
4076
4077 $insert = my $insert_to_current = sub {
4078 $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
4079 }; # $insert_to_current
4080
4081 my $insert_to_foster = sub {
4082 my $child = shift;
4083 if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
4084 # MUST
4085 my $foster_parent_element;
4086 my $next_sibling;
4087 OE: for (reverse 0..$#{$self->{open_elements}}) {
4088 if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4089 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4090 if (defined $parent and $parent->node_type == 1) {
4091 !!!cp ('t70');
4092 $foster_parent_element = $parent;
4093 $next_sibling = $self->{open_elements}->[$_]->[0];
4094 } else {
4095 !!!cp ('t71');
4096 $foster_parent_element
4097 = $self->{open_elements}->[$_ - 1]->[0];
4098 }
4099 last OE;
4100 }
4101 } # OE
4102 $foster_parent_element = $self->{open_elements}->[0]->[0]
4103 unless defined $foster_parent_element;
4104 $foster_parent_element->insert_before
4105 ($child, $next_sibling);
4106 $open_tables->[-1]->[1] = 1; # tainted
4107 } else {
4108 !!!cp ('t72');
4109 $self->{open_elements}->[-1]->[0]->append_child ($child);
4110 }
4111 }; # $insert_to_foster
4112
4113 B: while (1) {
4114 if ($token->{type} == DOCTYPE_TOKEN) {
4115 !!!cp ('t73');
4116 !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4117 ## Ignore the token
4118 ## Stay in the phase
4119 !!!next-token;
4120 next B;
4121 } elsif ($token->{type} == START_TAG_TOKEN and
4122 $token->{tag_name} eq 'html') {
4123 if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4124 !!!cp ('t79');
4125 !!!parse-error (type => 'after html', text => 'html', token => $token);
4126 $self->{insertion_mode} = AFTER_BODY_IM;
4127 } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4128 !!!cp ('t80');
4129 !!!parse-error (type => 'after html', text => 'html', token => $token);
4130 $self->{insertion_mode} = AFTER_FRAMESET_IM;
4131 } else {
4132 !!!cp ('t81');
4133 }
4134
4135 !!!cp ('t82');
4136 !!!parse-error (type => 'not first start tag', token => $token);
4137 my $top_el = $self->{open_elements}->[0]->[0];
4138 for my $attr_name (keys %{$token->{attributes}}) {
4139 unless ($top_el->has_attribute_ns (undef, $attr_name)) {
4140 !!!cp ('t84');
4141 $top_el->set_attribute_ns
4142 (undef, [undef, $attr_name],
4143 $token->{attributes}->{$attr_name}->{value});
4144 }
4145 }
4146 !!!nack ('t84.1');
4147 !!!next-token;
4148 next B;
4149 } elsif ($token->{type} == COMMENT_TOKEN) {
4150 my $comment = $self->{document}->create_comment ($token->{data});
4151 if ($self->{insertion_mode} & AFTER_HTML_IMS) {
4152 !!!cp ('t85');
4153 $self->{document}->append_child ($comment);
4154 } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
4155 !!!cp ('t86');
4156 $self->{open_elements}->[0]->[0]->append_child ($comment);
4157 } else {
4158 !!!cp ('t87');
4159 $self->{open_elements}->[-1]->[0]->append_child ($comment);
4160 }
4161 !!!next-token;
4162 next B;
4163 } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4164 if ($token->{type} == CHARACTER_TOKEN) {
4165 !!!cp ('t87.1');
4166 $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4167 !!!next-token;
4168 next B;
4169 } elsif ($token->{type} == START_TAG_TOKEN) {
4170 if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4171 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4172 not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4173 ($token->{tag_name} eq 'svg' and
4174 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4175 ## NOTE: "using the rules for secondary insertion mode"then"continue"
4176 !!!cp ('t87.2');
4177 #
4178 } elsif ({
4179 b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4180 center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4181 em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4182 h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4183 img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4184 nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4185 small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4186 sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4187 }->{$token->{tag_name}}) {
4188 !!!cp ('t87.2');
4189 !!!parse-error (type => 'not closed',
4190 text => $self->{open_elements}->[-1]->[0]
4191 ->manakai_local_name,
4192 token => $token);
4193
4194 pop @{$self->{open_elements}}
4195 while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4196
4197 $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4198 ## Reprocess.
4199 next B;
4200 } else {
4201 my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4202 my $tag_name = $token->{tag_name};
4203 if ($nsuri eq $SVG_NS) {
4204 $tag_name = {
4205 altglyph => 'altGlyph',
4206 altglyphdef => 'altGlyphDef',
4207 altglyphitem => 'altGlyphItem',
4208 animatecolor => 'animateColor',
4209 animatemotion => 'animateMotion',
4210 animatetransform => 'animateTransform',
4211 clippath => 'clipPath',
4212 feblend => 'feBlend',
4213 fecolormatrix => 'feColorMatrix',
4214 fecomponenttransfer => 'feComponentTransfer',
4215 fecomposite => 'feComposite',
4216 feconvolvematrix => 'feConvolveMatrix',
4217 fediffuselighting => 'feDiffuseLighting',
4218 fedisplacementmap => 'feDisplacementMap',
4219 fedistantlight => 'feDistantLight',
4220 feflood => 'feFlood',
4221 fefunca => 'feFuncA',
4222 fefuncb => 'feFuncB',
4223 fefuncg => 'feFuncG',
4224 fefuncr => 'feFuncR',
4225 fegaussianblur => 'feGaussianBlur',
4226 feimage => 'feImage',
4227 femerge => 'feMerge',
4228 femergenode => 'feMergeNode',
4229 femorphology => 'feMorphology',
4230 feoffset => 'feOffset',
4231 fepointlight => 'fePointLight',
4232 fespecularlighting => 'feSpecularLighting',
4233 fespotlight => 'feSpotLight',
4234 fetile => 'feTile',
4235 feturbulence => 'feTurbulence',
4236 foreignobject => 'foreignObject',
4237 glyphref => 'glyphRef',
4238 lineargradient => 'linearGradient',
4239 radialgradient => 'radialGradient',
4240 #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4241 textpath => 'textPath',
4242 }->{$tag_name} || $tag_name;
4243 }
4244
4245 ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4246
4247 ## "adjust foreign attributes" - done in insert-element-f
4248
4249 !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4250
4251 if ($self->{self_closing}) {
4252 pop @{$self->{open_elements}};
4253 !!!ack ('t87.3');
4254 } else {
4255 !!!cp ('t87.4');
4256 }
4257
4258 !!!next-token;
4259 next B;
4260 }
4261 } elsif ($token->{type} == END_TAG_TOKEN) {
4262 ## NOTE: "using the rules for secondary insertion mode" then "continue"
4263 !!!cp ('t87.5');
4264 #
4265 } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4266 !!!cp ('t87.6');
4267 !!!parse-error (type => 'not closed',
4268 text => $self->{open_elements}->[-1]->[0]
4269 ->manakai_local_name,
4270 token => $token);
4271
4272 pop @{$self->{open_elements}}
4273 while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4274
4275 $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4276 ## Reprocess.
4277 next B;
4278 } else {
4279 die "$0: $token->{type}: Unknown token type";
4280 }
4281 }
4282
4283 if ($self->{insertion_mode} & HEAD_IMS) {
4284 if ($token->{type} == CHARACTER_TOKEN) {
4285 if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4286 unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4287 !!!cp ('t88.2');
4288 $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4289 } else {
4290 !!!cp ('t88.1');
4291 ## Ignore the token.
4292 !!!next-token;
4293 next B;
4294 }
4295 unless (length $token->{data}) {
4296 !!!cp ('t88');
4297 !!!next-token;
4298 next B;
4299 }
4300 }
4301
4302 if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4303 !!!cp ('t89');
4304 ## As if <head>
4305 !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4306 $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4307 push @{$self->{open_elements}},
4308 [$self->{head_element}, $el_category->{head}];
4309
4310 ## Reprocess in the "in head" insertion mode...
4311 pop @{$self->{open_elements}};
4312
4313 ## Reprocess in the "after head" insertion mode...
4314 } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4315 !!!cp ('t90');
4316 ## As if </noscript>
4317 pop @{$self->{open_elements}};
4318 !!!parse-error (type => 'in noscript:#text', token => $token);
4319
4320 ## Reprocess in the "in head" insertion mode...
4321 ## As if </head>
4322 pop @{$self->{open_elements}};
4323
4324 ## Reprocess in the "after head" insertion mode...
4325 } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4326 !!!cp ('t91');
4327 pop @{$self->{open_elements}};
4328
4329 ## Reprocess in the "after head" insertion mode...
4330 } else {
4331 !!!cp ('t92');
4332 }
4333
4334 ## "after head" insertion mode
4335 ## As if <body>
4336 !!!insert-element ('body',, $token);
4337 $self->{insertion_mode} = IN_BODY_IM;
4338 ## reprocess
4339 next B;
4340 } elsif ($token->{type} == START_TAG_TOKEN) {
4341 if ($token->{tag_name} eq 'head') {
4342 if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4343 !!!cp ('t93');
4344 !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4345 $self->{open_elements}->[-1]->[0]->append_child
4346 ($self->{head_element});
4347 push @{$self->{open_elements}},
4348 [$self->{head_element}, $el_category->{head}];
4349 $self->{insertion_mode} = IN_HEAD_IM;
4350 !!!nack ('t93.1');
4351 !!!next-token;
4352 next B;
4353 } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4354 !!!cp ('t93.2');
4355 !!!parse-error (type => 'after head', text => 'head',
4356 token => $token);
4357 ## Ignore the token
4358 !!!nack ('t93.3');
4359 !!!next-token;
4360 next B;
4361 } else {
4362 !!!cp ('t95');
4363 !!!parse-error (type => 'in head:head',
4364 token => $token); # or in head noscript
4365 ## Ignore the token
4366 !!!nack ('t95.1');
4367 !!!next-token;
4368 next B;
4369 }
4370 } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4371 !!!cp ('t96');
4372 ## As if <head>
4373 !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4374 $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4375 push @{$self->{open_elements}},
4376 [$self->{head_element}, $el_category->{head}];
4377
4378 $self->{insertion_mode} = IN_HEAD_IM;
4379 ## Reprocess in the "in head" insertion mode...
4380 } else {
4381 !!!cp ('t97');
4382 }
4383
4384 if ($token->{tag_name} eq 'base') {
4385 if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4386 !!!cp ('t98');
4387 ## As if </noscript>
4388 pop @{$self->{open_elements}};
4389 !!!parse-error (type => 'in noscript', text => 'base',
4390 token => $token);
4391
4392 $self->{insertion_mode} = IN_HEAD_IM;
4393 ## Reprocess in the "in head" insertion mode...
4394 } else {
4395 !!!cp ('t99');
4396 }
4397
4398 ## NOTE: There is a "as if in head" code clone.
4399 if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4400 !!!cp ('t100');
4401 !!!parse-error (type => 'after head',
4402 text => $token->{tag_name}, token => $token);
4403 push @{$self->{open_elements}},
4404 [$self->{head_element}, $el_category->{head}];
4405 } else {
4406 !!!cp ('t101');
4407 }
4408 !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4409 pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4410 pop @{$self->{open_elements}} # <head>
4411 if $self->{insertion_mode} == AFTER_HEAD_IM;
4412 !!!nack ('t101.1');
4413 !!!next-token;
4414 next B;
4415 } elsif ($token->{tag_name} eq 'link') {
4416 ## NOTE: There is a "as if in head" code clone.
4417 if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4418 !!!cp ('t102');
4419 !!!parse-error (type => 'after head',
4420 text => $token->{tag_name}, token => $token);
4421 push @{$self->{open_elements}},
4422 [$self->{head_element}, $el_category->{head}];
4423 } else {
4424 !!!cp ('t103');
4425 }
4426 !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4427 pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4428 pop @{$self->{open_elements}} # <head>
4429 if $self->{insertion_mode} == AFTER_HEAD_IM;
4430 !!!ack ('t103.1');
4431 !!!next-token;
4432 next B;
4433 } elsif ($token->{tag_name} eq 'meta') {
4434 ## NOTE: There is a "as if in head" code clone.
4435 if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4436 !!!cp ('t104');
4437 !!!parse-error (type => 'after head',
4438 text => $token->{tag_name}, token => $token);
4439 push @{$self->{open_elements}},
4440 [$self->{head_element}, $el_category->{head}];
4441 } else {
4442 !!!cp ('t105');
4443 }
4444 !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4445 my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4446
4447 unless ($self->{confident}) {
4448 if ($token->{attributes}->{charset}) {
4449 !!!cp ('t106');
4450 ## NOTE: Whether the encoding is supported or not is handled
4451 ## in the {change_encoding} callback.
4452 $self->{change_encoding}
4453 ->($self, $token->{attributes}->{charset}->{value},
4454 $token);
4455
4456 $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4457 ->set_user_data (manakai_has_reference =>
4458 $token->{attributes}->{charset}
4459 ->{has_reference});
4460 } elsif ($token->{attributes}->{content}) {
4461 if ($token->{attributes}->{content}->{value}
4462 =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4463 [\x09-\x0D\x20]*=
4464 [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4465 ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4466 !!!cp ('t107');
4467 ## NOTE: Whether the encoding is supported or not is handled
4468 ## in the {change_encoding} callback.
4469 $self->{change_encoding}
4470 ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4471 $token);
4472 $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4473 ->set_user_data (manakai_has_reference =>
4474 $token->{attributes}->{content}
4475 ->{has_reference});
4476 } else {
4477 !!!cp ('t108');
4478 }
4479 }
4480 } else {
4481 if ($token->{attributes}->{charset}) {
4482 !!!cp ('t109');
4483 $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4484 ->set_user_data (manakai_has_reference =>
4485 $token->{attributes}->{charset}
4486 ->{has_reference});
4487 }
4488 if ($token->{attributes}->{content}) {
4489 !!!cp ('t110');
4490 $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4491 ->set_user_data (manakai_has_reference =>
4492 $token->{attributes}->{content}
4493 ->{has_reference});
4494 }
4495 }
4496
4497 pop @{$self->{open_elements}} # <head>
4498 if $self->{insertion_mode} == AFTER_HEAD_IM;
4499 !!!ack ('t110.1');
4500 !!!next-token;
4501 next B;
4502 } elsif ($token->{tag_name} eq 'title') {
4503 if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4504 !!!cp ('t111');
4505 ## As if </noscript>
4506 pop @{$self->{open_elements}};
4507 !!!parse-error (type => 'in noscript', text => 'title',
4508 token => $token);
4509
4510 $self->{insertion_mode} = IN_HEAD_IM;
4511 ## Reprocess in the "in head" insertion mode...
4512 } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4513 !!!cp ('t112');
4514 !!!parse-error (type => 'after head',
4515 text => $token->{tag_name}, token => $token);
4516 push @{$self->{open_elements}},
4517 [$self->{head_element}, $el_category->{head}];
4518 } else {
4519 !!!cp ('t113');
4520 }
4521
4522 ## NOTE: There is a "as if in head" code clone.
4523 my $parent = defined $self->{head_element} ? $self->{head_element}
4524 : $self->{open_elements}->[-1]->[0];
4525 $parse_rcdata->(RCDATA_CONTENT_MODEL);
4526 pop @{$self->{open_elements}} # <head>
4527 if $self->{insertion_mode} == AFTER_HEAD_IM;
4528 next B;
4529 } elsif ($token->{tag_name} eq 'style' or
4530 $token->{tag_name} eq 'noframes') {
4531 ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4532 ## insertion mode IN_HEAD_IM)
4533 ## NOTE: There is a "as if in head" code clone.
4534 if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4535 !!!cp ('t114');
4536 !!!parse-error (type => 'after head',
4537 text => $token->{tag_name}, token => $token);
4538 push @{$self->{open_elements}},
4539 [$self->{head_element}, $el_category->{head}];
4540 } else {
4541 !!!cp ('t115');
4542 }
4543 $parse_rcdata->(CDATA_CONTENT_MODEL);
4544 pop @{$self->{open_elements}} # <head>
4545 if $self->{insertion_mode} == AFTER_HEAD_IM;
4546 next B;
4547 } elsif ($token->{tag_name} eq 'noscript') {
4548 if ($self->{insertion_mode} == IN_HEAD_IM) {
4549 !!!cp ('t116');
4550 ## NOTE: and scripting is disalbed
4551 !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4552 $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4553 !!!nack ('t116.1');
4554 !!!next-token;
4555 next B;
4556 } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4557 !!!cp ('t117');
4558 !!!parse-error (type => 'in noscript', text => 'noscript',
4559 token => $token);
4560 ## Ignore the token
4561 !!!nack ('t117.1');
4562 !!!next-token;
4563 next B;
4564 } else {
4565 !!!cp ('t118');
4566 #
4567 }
4568 } elsif ($token->{tag_name} eq 'script') {
4569 if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4570 !!!cp ('t119');
4571 ## As if </noscript>
4572 pop @{$self->{open_elements}};
4573 !!!parse-error (type => 'in noscript', text => 'script',
4574 token => $token);
4575
4576 $self->{insertion_mode} = IN_HEAD_IM;
4577 ## Reprocess in the "in head" insertion mode...
4578 } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4579 !!!cp ('t120');
4580 !!!parse-error (type => 'after head',
4581 text => $token->{tag_name}, token => $token);
4582 push @{$self->{open_elements}},
4583 [$self->{head_element}, $el_category->{head}];
4584 } else {
4585 !!!cp ('t121');
4586 }
4587
4588 ## NOTE: There is a "as if in head" code clone.
4589 $script_start_tag->();
4590 pop @{$self->{open_elements}} # <head>
4591 if $self->{insertion_mode} == AFTER_HEAD_IM;
4592 next B;
4593 } elsif ($token->{tag_name} eq 'body' or
4594 $token->{tag_name} eq 'frameset') {
4595 if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4596 !!!cp ('t122');
4597 ## As if </noscript>
4598 pop @{$self->{open_elements}};
4599 !!!parse-error (type => 'in noscript',
4600 text => $token->{tag_name}, token => $token);
4601
4602 ## Reprocess in the "in head" insertion mode...
4603 ## As if </head>
4604 pop @{$self->{open_elements}};
4605
4606 ## Reprocess in the "after head" insertion mode...
4607 } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4608 !!!cp ('t124');
4609 pop @{$self->{open_elements}};
4610
4611 ## Reprocess in the "after head" insertion mode...
4612 } else {
4613 !!!cp ('t125');
4614 }
4615
4616 ## "after head" insertion mode
4617 !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4618 if ($token->{tag_name} eq 'body') {
4619 !!!cp ('t126');
4620 $self->{insertion_mode} = IN_BODY_IM;
4621 } elsif ($token->{tag_name} eq 'frameset') {
4622 !!!cp ('t127');
4623 $self->{insertion_mode} = IN_FRAMESET_IM;
4624 } else {
4625 die "$0: tag name: $self->{tag_name}";
4626 }
4627 !!!nack ('t127.1');
4628 !!!next-token;
4629 next B;
4630 } else {
4631 !!!cp ('t128');
4632 #
4633 }
4634
4635 if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4636 !!!cp ('t129');
4637 ## As if </noscript>
4638 pop @{$self->{open_elements}};
4639 !!!parse-error (type => 'in noscript:/',
4640 text => $token->{tag_name}, token => $token);
4641
4642 ## Reprocess in the "in head" insertion mode...
4643 ## As if </head>
4644 pop @{$self->{open_elements}};
4645
4646 ## Reprocess in the "after head" insertion mode...
4647 } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4648 !!!cp ('t130');
4649 ## As if </head>
4650 pop @{$self->{open_elements}};
4651
4652 ## Reprocess in the "after head" insertion mode...
4653 } else {
4654 !!!cp ('t131');
4655 }
4656
4657 ## "after head" insertion mode
4658 ## As if <body>
4659 !!!insert-element ('body',, $token);
4660 $self->{insertion_mode} = IN_BODY_IM;
4661 ## reprocess
4662 !!!ack-later;
4663 next B;
4664 } elsif ($token->{type} == END_TAG_TOKEN) {
4665 if ($token->{tag_name} eq 'head') {
4666 if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4667 !!!cp ('t132');
4668 ## As if <head>
4669 !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4670 $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4671 push @{$self->{open_elements}},
4672 [$self->{head_element}, $el_category->{head}];
4673
4674 ## Reprocess in the "in head" insertion mode...
4675 pop @{$self->{open_elements}};
4676 $self->{insertion_mode} = AFTER_HEAD_IM;
4677 !!!next-token;
4678 next B;
4679 } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4680 !!!cp ('t133');
4681 ## As if </noscript>
4682 pop @{$self->{open_elements}};
4683 !!!parse-error (type => 'in noscript:/',
4684 text => 'head', token => $token);
4685
4686 ## Reprocess in the "in head" insertion mode...
4687 pop @{$self->{open_elements}};
4688 $self->{insertion_mode} = AFTER_HEAD_IM;
4689 !!!next-token;
4690 next B;
4691 } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4692 !!!cp ('t134');
4693 pop @{$self->{open_elements}};
4694 $self->{insertion_mode} = AFTER_HEAD_IM;
4695 !!!next-token;
4696 next B;
4697 } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4698 !!!cp ('t134.1');
4699 !!!parse-error (type => 'unmatched end tag', text => 'head',
4700 token => $token);
4701 ## Ignore the token
4702 !!!next-token;
4703 next B;
4704 } else {
4705 die "$0: $self->{insertion_mode}: Unknown insertion mode";
4706 }
4707 } elsif ($token->{tag_name} eq 'noscript') {
4708 if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4709 !!!cp ('t136');
4710 pop @{$self->{open_elements}};
4711 $self->{insertion_mode} = IN_HEAD_IM;
4712 !!!next-token;
4713 next B;
4714 } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4715 $self->{insertion_mode} == AFTER_HEAD_IM) {
4716 !!!cp ('t137');
4717 !!!parse-error (type => 'unmatched end tag',
4718 text => 'noscript', token => $token);
4719 ## Ignore the token ## ISSUE: An issue in the spec.
4720 !!!next-token;
4721 next B;
4722 } else {
4723 !!!cp ('t138');
4724 #
4725 }
4726 } elsif ({
4727 body => 1, html => 1,
4728 }->{$token->{tag_name}}) {
4729 if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4730 $self->{insertion_mode} == IN_HEAD_IM or
4731 $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4732 !!!cp ('t140');
4733 !!!parse-error (type => 'unmatched end tag',
4734 text => $token->{tag_name}, token => $token);
4735 ## Ignore the token
4736 !!!next-token;
4737 next B;
4738 } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4739 !!!cp ('t140.1');
4740 !!!parse-error (type => 'unmatched end tag',
4741 text => $token->{tag_name}, token => $token);
4742 ## Ignore the token
4743 !!!next-token;
4744 next B;
4745 } else {
4746 die "$0: $self->{insertion_mode}: Unknown insertion mode";
4747 }
4748 } elsif ($token->{tag_name} eq 'p') {
4749 !!!cp ('t142');
4750 !!!parse-error (type => 'unmatched end tag',
4751 text => $token->{tag_name}, token => $token);
4752 ## Ignore the token
4753 !!!next-token;
4754 next B;
4755 } elsif ($token->{tag_name} eq 'br') {
4756 if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4757 !!!cp ('t142.2');
4758 ## (before head) as if <head>, (in head) as if </head>
4759 !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4760 $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4761 $self->{insertion_mode} = AFTER_HEAD_IM;
4762
4763 ## Reprocess in the "after head" insertion mode...
4764 } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4765 !!!cp ('t143.2');
4766 ## As if </head>
4767 pop @{$self->{open_elements}};
4768 $self->{insertion_mode} = AFTER_HEAD_IM;
4769
4770 ## Reprocess in the "after head" insertion mode...
4771 } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4772 !!!cp ('t143.3');
4773 ## ISSUE: Two parse errors for <head><noscript></br>
4774 !!!parse-error (type => 'unmatched end tag',
4775 text => 'br', token => $token);
4776 ## As if </noscript>
4777 pop @{$self->{open_elements}};
4778 $self->{insertion_mode} = IN_HEAD_IM;
4779
4780 ## Reprocess in the "in head" insertion mode...
4781 ## As if </head>
4782 pop @{$self->{open_elements}};
4783 $self->{insertion_mode} = AFTER_HEAD_IM;
4784
4785 ## Reprocess in the "after head" insertion mode...
4786 } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4787 !!!cp ('t143.4');
4788 #
4789 } else {
4790 die "$0: $self->{insertion_mode}: Unknown insertion mode";
4791 }
4792
4793 ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4794 !!!parse-error (type => 'unmatched end tag',
4795 text => 'br', token => $token);
4796 ## Ignore the token
4797 !!!next-token;
4798 next B;
4799 } else {
4800 !!!cp ('t145');
4801 !!!parse-error (type => 'unmatched end tag',
4802 text => $token->{tag_name}, token => $token);
4803 ## Ignore the token
4804 !!!next-token;
4805 next B;
4806 }
4807
4808 if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4809 !!!cp ('t146');
4810 ## As if </noscript>
4811 pop @{$self->{open_elements}};
4812 !!!parse-error (type => 'in noscript:/',
4813 text => $token->{tag_name}, token => $token);
4814
4815 ## Reprocess in the "in head" insertion mode...
4816 ## As if </head>
4817 pop @{$self->{open_elements}};
4818
4819 ## Reprocess in the "after head" insertion mode...
4820 } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4821 !!!cp ('t147');
4822 ## As if </head>
4823 pop @{$self->{open_elements}};
4824
4825 ## Reprocess in the "after head" insertion mode...
4826 } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4827 ## ISSUE: This case cannot be reached?
4828 !!!cp ('t148');
4829 !!!parse-error (type => 'unmatched end tag',
4830 text => $token->{tag_name}, token => $token);
4831 ## Ignore the token ## ISSUE: An issue in the spec.
4832 !!!next-token;
4833 next B;
4834 } else {
4835 !!!cp ('t149');
4836 }
4837
4838 ## "after head" insertion mode
4839 ## As if <body>
4840 !!!insert-element ('body',, $token);
4841 $self->{insertion_mode} = IN_BODY_IM;
4842 ## reprocess
4843 next B;
4844 } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4845 if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4846 !!!cp ('t149.1');
4847
4848 ## NOTE: As if <head>
4849 !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4850 $self->{open_elements}->[-1]->[0]->append_child
4851 ($self->{head_element});
4852 #push @{$self->{open_elements}},
4853 # [$self->{head_element}, $el_category->{head}];
4854 #$self->{insertion_mode} = IN_HEAD_IM;
4855 ## NOTE: Reprocess.
4856
4857 ## NOTE: As if </head>
4858 #pop @{$self->{open_elements}};
4859 #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4860 ## NOTE: Reprocess.
4861
4862 #
4863 } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4864 !!!cp ('t149.2');
4865
4866 ## NOTE: As if </head>
4867 pop @{$self->{open_elements}};
4868 #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4869 ## NOTE: Reprocess.
4870
4871 #
4872 } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4873 !!!cp ('t149.3');
4874
4875 !!!parse-error (type => 'in noscript:#eof', token => $token);
4876
4877 ## As if </noscript>
4878 pop @{$self->{open_elements}};
4879 #$self->{insertion_mode} = IN_HEAD_IM;
4880 ## NOTE: Reprocess.
4881
4882 ## NOTE: As if </head>
4883 pop @{$self->{open_elements}};
4884 #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4885 ## NOTE: Reprocess.
4886
4887 #
4888 } else {
4889 !!!cp ('t149.4');
4890 #
4891 }
4892
4893 ## NOTE: As if <body>
4894 !!!insert-element ('body',, $token);
4895 $self->{insertion_mode} = IN_BODY_IM;
4896 ## NOTE: Reprocess.
4897 next B;
4898 } else {
4899 die "$0: $token->{type}: Unknown token type";
4900 }
4901
4902 ## ISSUE: An issue in the spec.
4903 } elsif ($self->{insertion_mode} & BODY_IMS) {
4904 if ($token->{type} == CHARACTER_TOKEN) {
4905 !!!cp ('t150');
4906 ## NOTE: There is a code clone of "character in body".
4907 $reconstruct_active_formatting_elements->($insert_to_current);
4908
4909 $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4910
4911 !!!next-token;
4912 next B;
4913 } elsif ($token->{type} == START_TAG_TOKEN) {
4914 if ({
4915 caption => 1, col => 1, colgroup => 1, tbody => 1,
4916 td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
4917 }->{$token->{tag_name}}) {
4918 if ($self->{insertion_mode} == IN_CELL_IM) {
4919 ## have an element in table scope
4920 for (reverse 0..$#{$self->{open_elements}}) {
4921 my $node = $self->{open_elements}->[$_];
4922 if ($node->[1] & TABLE_CELL_EL) {
4923 !!!cp ('t151');
4924
4925 ## Close the cell
4926 !!!back-token; # <x>
4927 $token = {type => END_TAG_TOKEN,
4928 tag_name => $node->[0]->manakai_local_name,
4929 line => $token->{line},
4930 column => $token->{column}};
4931 next B;
4932 } elsif ($node->[1] & TABLE_SCOPING_EL) {
4933 !!!cp ('t152');
4934 ## ISSUE: This case can never be reached, maybe.
4935 last;
4936 }
4937 }
4938
4939 !!!cp ('t153');
4940 !!!parse-error (type => 'start tag not allowed',
4941 text => $token->{tag_name}, token => $token);
4942 ## Ignore the token
4943 !!!nack ('t153.1');
4944 !!!next-token;
4945 next B;
4946 } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4947 !!!parse-error (type => 'not closed', text => 'caption',
4948 token => $token);
4949
4950 ## NOTE: As if </caption>.
4951 ## have a table element in table scope
4952 my $i;
4953 INSCOPE: {
4954 for (reverse 0..$#{$self->{open_elements}}) {
4955 my $node = $self->{open_elements}->[$_];
4956 if ($node->[1] & CAPTION_EL) {
4957 !!!cp ('t155');
4958 $i = $_;
4959 last INSCOPE;
4960 } elsif ($node->[1] & TABLE_SCOPING_EL) {
4961 !!!cp ('t156');
4962 last;
4963 }
4964 }
4965
4966 !!!cp ('t157');
4967 !!!parse-error (type => 'start tag not allowed',
4968 text => $token->{tag_name}, token => $token);
4969 ## Ignore the token
4970 !!!nack ('t157.1');
4971 !!!next-token;
4972 next B;
4973 } # INSCOPE
4974
4975 ## generate implied end tags
4976 while ($self->{open_elements}->[-1]->[1]
4977 & END_TAG_OPTIONAL_EL) {
4978 !!!cp ('t158');
4979 pop @{$self->{open_elements}};
4980 }
4981
4982 unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4983 !!!cp ('t159');
4984 !!!parse-error (type => 'not closed',
4985 text => $self->{open_elements}->[-1]->[0]
4986 ->manakai_local_name,
4987 token => $token);
4988 } else {
4989 !!!cp ('t160');
4990 }
4991
4992 splice @{$self->{open_elements}}, $i;
4993
4994 $clear_up_to_marker->();
4995
4996 $self->{insertion_mode} = IN_TABLE_IM;
4997
4998 ## reprocess
4999 !!!ack-later;
5000 next B;
5001 } else {
5002 !!!cp ('t161');
5003 #
5004 }
5005 } else {
5006 !!!cp ('t162');
5007 #
5008 }
5009 } elsif ($token->{type} == END_TAG_TOKEN) {
5010 if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
5011 if ($self->{insertion_mode} == IN_CELL_IM) {
5012 ## have an element in table scope
5013 my $i;
5014 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5015 my $node = $self->{open_elements}->[$_];
5016 if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5017 !!!cp ('t163');
5018 $i = $_;
5019 last INSCOPE;
5020 } elsif ($node->[1] & TABLE_SCOPING_EL) {
5021 !!!cp ('t164');
5022 last INSCOPE;
5023 }
5024 } # INSCOPE
5025 unless (defined $i) {
5026 !!!cp ('t165');
5027 !!!parse-error (type => 'unmatched end tag',
5028 text => $token->{tag_name},
5029 token => $token);
5030 ## Ignore the token
5031 !!!next-token;
5032 next B;
5033 }
5034
5035 ## generate implied end tags
5036 while ($self->{open_elements}->[-1]->[1]
5037 & END_TAG_OPTIONAL_EL) {
5038 !!!cp ('t166');
5039 pop @{$self->{open_elements}};
5040 }
5041
5042 if ($self->{open_elements}->[-1]->[0]->manakai_local_name
5043 ne $token->{tag_name}) {
5044 !!!cp ('t167');
5045 !!!parse-error (type => 'not closed',
5046 text => $self->{open_elements}->[-1]->[0]
5047 ->manakai_local_name,
5048 token => $token);
5049 } else {
5050 !!!cp ('t168');
5051 }
5052
5053 splice @{$self->{open_elements}}, $i;
5054
5055 $clear_up_to_marker->();
5056
5057 $self->{insertion_mode} = IN_ROW_IM;
5058
5059 !!!next-token;
5060 next B;
5061 } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5062 !!!cp ('t169');
5063 !!!parse-error (type => 'unmatched end tag',
5064 text => $token->{tag_name}, token => $token);
5065 ## Ignore the token
5066 !!!next-token;
5067 next B;
5068 } else {
5069 !!!cp ('t170');
5070 #
5071 }
5072 } elsif ($token->{tag_name} eq 'caption') {
5073 if ($self->{insertion_mode} == IN_CAPTION_IM) {
5074 ## have a table element in table scope
5075 my $i;
5076 INSCOPE: {
5077 for (reverse 0..$#{$self->{open_elements}}) {
5078 my $node = $self->{open_elements}->[$_];
5079 if ($node->[1] & CAPTION_EL) {
5080 !!!cp ('t171');
5081 $i = $_;
5082 last INSCOPE;
5083 } elsif ($node->[1] & TABLE_SCOPING_EL) {
5084 !!!cp ('t172');
5085 last;
5086 }
5087 }
5088
5089 !!!cp ('t173');
5090 !!!parse-error (type => 'unmatched end tag',
5091 text => $token->{tag_name}, token => $token);
5092 ## Ignore the token
5093 !!!next-token;
5094 next B;
5095 } # INSCOPE
5096
5097 ## generate implied end tags
5098 while ($self->{open_elements}->[-1]->[1]
5099 & END_TAG_OPTIONAL_EL) {
5100 !!!cp ('t174');
5101 pop @{$self->{open_elements}};
5102 }
5103
5104 unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5105 !!!cp ('t175');
5106 !!!parse-error (type => 'not closed',
5107 text => $self->{open_elements}->[-1]->[0]
5108 ->manakai_local_name,
5109 token => $token);
5110 } else {
5111 !!!cp ('t176');
5112 }
5113
5114 splice @{$self->{open_elements}}, $i;
5115
5116 $clear_up_to_marker->();
5117
5118 $self->{insertion_mode} = IN_TABLE_IM;
5119
5120 !!!next-token;
5121 next B;
5122 } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5123 !!!cp ('t177');
5124 !!!parse-error (type => 'unmatched end tag',
5125 text => $token->{tag_name}, token => $token);
5126 ## Ignore the token
5127 !!!next-token;
5128 next B;
5129 } else {
5130 !!!cp ('t178');
5131 #
5132 }
5133 } elsif ({
5134 table => 1, tbody => 1, tfoot => 1,
5135 thead => 1, tr => 1,
5136 }->{$token->{tag_name}} and
5137 $self->{insertion_mode} == IN_CELL_IM) {
5138 ## have an element in table scope
5139 my $i;
5140 my $tn;
5141 INSCOPE: {
5142 for (reverse 0..$#{$self->{open_elements}}) {
5143 my $node = $self->{open_elements}->[$_];
5144 if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5145 !!!cp ('t179');
5146 $i = $_;
5147
5148 ## Close the cell
5149 !!!back-token; # </x>
5150 $token = {type => END_TAG_TOKEN, tag_name => $tn,
5151 line => $token->{line},
5152 column => $token->{column}};
5153 next B;
5154 } elsif ($node->[1] & TABLE_CELL_EL) {
5155 !!!cp ('t180');
5156 $tn = $node->[0]->manakai_local_name;
5157 ## NOTE: There is exactly one |td| or |th| element
5158 ## in scope in the stack of open elements by definition.
5159 } elsif ($node->[1] & TABLE_SCOPING_EL) {
5160 ## ISSUE: Can this be reached?
5161 !!!cp ('t181');
5162 last;
5163 }
5164 }
5165
5166 !!!cp ('t182');
5167 !!!parse-error (type => 'unmatched end tag',
5168 text => $token->{tag_name}, token => $token);
5169 ## Ignore the token
5170 !!!next-token;
5171 next B;
5172 } # INSCOPE
5173 } elsif ($token->{tag_name} eq 'table' and
5174 $self->{insertion_mode} == IN_CAPTION_IM) {
5175 !!!parse-error (type => 'not closed', text => 'caption',
5176 token => $token);
5177
5178 ## As if </caption>
5179 ## have a table element in table scope
5180 my $i;
5181 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5182 my $node = $self->{open_elements}->[$_];
5183 if ($node->[1] & CAPTION_EL) {
5184 !!!cp ('t184');
5185 $i = $_;
5186 last INSCOPE;
5187 } elsif ($node->[1] & TABLE_SCOPING_EL) {
5188 !!!cp ('t185');
5189 last INSCOPE;
5190 }
5191 } # INSCOPE
5192 unless (defined $i) {
5193 !!!cp ('t186');
5194 !!!parse-error (type => 'unmatched end tag',
5195 text => 'caption', token => $token);
5196 ## Ignore the token
5197 !!!next-token;
5198 next B;
5199 }
5200
5201 ## generate implied end tags
5202 while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5203 !!!cp ('t187');
5204 pop @{$self->{open_elements}};
5205 }
5206
5207 unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5208 !!!cp ('t188');
5209 !!!parse-error (type => 'not closed',
5210 text => $self->{open_elements}->[-1]->[0]
5211 ->manakai_local_name,
5212 token => $token);
5213 } else {
5214 !!!cp ('t189');
5215 }
5216
5217 splice @{$self->{open_elements}}, $i;
5218
5219 $clear_up_to_marker->();
5220
5221 $self->{insertion_mode} = IN_TABLE_IM;
5222
5223 ## reprocess
5224 next B;
5225 } elsif ({
5226 body => 1, col => 1, colgroup => 1, html => 1,
5227 }->{$token->{tag_name}}) {
5228 if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5229 !!!cp ('t190');
5230 !!!parse-error (type => 'unmatched end tag',
5231 text => $token->{tag_name}, token => $token);
5232 ## Ignore the token
5233 !!!next-token;
5234 next B;
5235 } else {
5236 !!!cp ('t191');
5237 #
5238 }
5239 } elsif ({
5240 tbody => 1, tfoot => 1,
5241 thead => 1, tr => 1,
5242 }->{$token->{tag_name}} and
5243 $self->{insertion_mode} == IN_CAPTION_IM) {
5244 !!!cp ('t192');
5245 !!!parse-error (type => 'unmatched end tag',
5246 text => $token->{tag_name}, token => $token);
5247 ## Ignore the token
5248 !!!next-token;
5249 next B;
5250 } else {
5251 !!!cp ('t193');
5252 #
5253 }
5254 } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5255 for my $entry (@{$self->{open_elements}}) {
5256 unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5257 !!!cp ('t75');
5258 !!!parse-error (type => 'in body:#eof', token => $token);
5259 last;
5260 }
5261 }
5262
5263 ## Stop parsing.
5264 last B;
5265 } else {
5266 die "$0: $token->{type}: Unknown token type";
5267 }
5268
5269 $insert = $insert_to_current;
5270 #
5271 } elsif ($self->{insertion_mode} & TABLE_IMS) {
5272 if ($token->{type} == CHARACTER_TOKEN) {
5273 if (not $open_tables->[-1]->[1] and # tainted
5274 $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5275 $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5276
5277 unless (length $token->{data}) {
5278 !!!cp ('t194');
5279 !!!next-token;
5280 next B;
5281 } else {
5282 !!!cp ('t195');
5283 }
5284 }
5285
5286 !!!parse-error (type => 'in table:#text', token => $token);
5287
5288 ## As if in body, but insert into foster parent element
5289 ## ISSUE: Spec says that "whenever a node would be inserted
5290 ## into the current node" while characters might not be
5291 ## result in a new Text node.
5292 $reconstruct_active_formatting_elements->($insert_to_foster);
5293
5294 if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
5295 # MUST
5296 my $foster_parent_element;
5297 my $next_sibling;
5298 my $prev_sibling;
5299 OE: for (reverse 0..$#{$self->{open_elements}}) {
5300 if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5301 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5302 if (defined $parent and $parent->node_type == 1) {
5303 !!!cp ('t196');
5304 $foster_parent_element = $parent;
5305 $next_sibling = $self->{open_elements}->[$_]->[0];
5306 $prev_sibling = $next_sibling->previous_sibling;
5307 } else {
5308 !!!cp ('t197');
5309 $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
5310 $prev_sibling = $foster_parent_element->last_child;
5311 }
5312 last OE;
5313 }
5314 } # OE
5315 $foster_parent_element = $self->{open_elements}->[0]->[0] and
5316 $prev_sibling = $foster_parent_element->last_child
5317 unless defined $foster_parent_element;
5318 if (defined $prev_sibling and
5319 $prev_sibling->node_type == 3) {
5320 !!!cp ('t198');
5321 $prev_sibling->manakai_append_text ($token->{data});
5322 } else {
5323 !!!cp ('t199');
5324 $foster_parent_element->insert_before
5325 ($self->{document}->create_text_node ($token->{data}),
5326 $next_sibling);
5327 }
5328 $open_tables->[-1]->[1] = 1; # tainted
5329 } else {
5330 !!!cp ('t200');
5331 $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5332 }
5333
5334 !!!next-token;
5335 next B;
5336 } elsif ($token->{type} == START_TAG_TOKEN) {
5337 if ({
5338 tr => ($self->{insertion_mode} != IN_ROW_IM),
5339 th => 1, td => 1,
5340 }->{$token->{tag_name}}) {
5341 if ($self->{insertion_mode} == IN_TABLE_IM) {
5342 ## Clear back to table context
5343 while (not ($self->{open_elements}->[-1]->[1]
5344 & TABLE_SCOPING_EL)) {
5345 !!!cp ('t201');
5346 pop @{$self->{open_elements}};
5347 }
5348
5349 !!!insert-element ('tbody',, $token);
5350 $self->{insertion_mode} = IN_TABLE_BODY_IM;
5351 ## reprocess in the "in table body" insertion mode...
5352 }
5353
5354 if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5355 unless ($token->{tag_name} eq 'tr') {
5356 !!!cp ('t202');
5357 !!!parse-error (type => 'missing start tag:tr', token => $token);
5358 }
5359
5360 ## Clear back to table body context
5361 while (not ($self->{open_elements}->[-1]->[1]
5362 & TABLE_ROWS_SCOPING_EL)) {
5363 !!!cp ('t203');
5364 ## ISSUE: Can this case be reached?
5365 pop @{$self->{open_elements}};
5366 }
5367
5368 $self->{insertion_mode} = IN_ROW_IM;
5369 if ($token->{tag_name} eq 'tr') {
5370 !!!cp ('t204');
5371 !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5372 !!!nack ('t204');
5373 !!!next-token;
5374 next B;
5375 } else {
5376 !!!cp ('t205');
5377 !!!insert-element ('tr',, $token);
5378 ## reprocess in the "in row" insertion mode
5379 }
5380 } else {
5381 !!!cp ('t206');
5382 }
5383
5384 ## Clear back to table row context
5385 while (not ($self->{open_elements}->[-1]->[1]
5386 & TABLE_ROW_SCOPING_EL)) {
5387 !!!cp ('t207');
5388 pop @{$self->{open_elements}};
5389 }
5390
5391 !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5392 $self->{insertion_mode} = IN_CELL_IM;
5393
5394 push @$active_formatting_elements, ['#marker', ''];
5395
5396 !!!nack ('t207.1');
5397 !!!next-token;
5398 next B;
5399 } elsif ({
5400 caption => 1, col => 1, colgroup => 1,
5401 tbody => 1, tfoot => 1, thead => 1,
5402 tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5403 }->{$token->{tag_name}}) {
5404 if ($self->{insertion_mode} == IN_ROW_IM) {
5405 ## As if </tr>
5406 ## have an element in table scope
5407 my $i;
5408 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5409 my $node = $self->{open_elements}->[$_];
5410 if ($node->[1] & TABLE_ROW_EL) {
5411 !!!cp ('t208');
5412 $i = $_;
5413 last INSCOPE;
5414 } elsif ($node->[1] & TABLE_SCOPING_EL) {
5415 !!!cp ('t209');
5416 last INSCOPE;
5417 }
5418 } # INSCOPE
5419 unless (defined $i) {
5420 !!!cp ('t210');
5421 ## TODO: This type is wrong.
5422 !!!parse-error (type => 'unmacthed end tag',
5423 text => $token->{tag_name}, token => $token);
5424 ## Ignore the token
5425 !!!nack ('t210.1');
5426 !!!next-token;
5427 next B;
5428 }
5429
5430 ## Clear back to table row context
5431 while (not ($self->{open_elements}->[-1]->[1]
5432 & TABLE_ROW_SCOPING_EL)) {
5433 !!!cp ('t211');
5434 ## ISSUE: Can this case be reached?
5435 pop @{$self->{open_elements}};
5436 }
5437
5438 pop @{$self->{open_elements}}; # tr
5439 $self->{insertion_mode} = IN_TABLE_BODY_IM;
5440 if ($token->{tag_name} eq 'tr') {
5441 !!!cp ('t212');
5442 ## reprocess
5443 !!!ack-later;
5444 next B;
5445 } else {
5446 !!!cp ('t213');
5447 ## reprocess in the "in table body" insertion mode...
5448 }
5449 }
5450
5451 if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5452 ## have an element in table scope
5453 my $i;
5454 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5455 my $node = $self->{open_elements}->[$_];
5456 if ($node->[1] & TABLE_ROW_GROUP_EL) {
5457 !!!cp ('t214');
5458 $i = $_;
5459 last INSCOPE;
5460 } elsif ($node->[1] & TABLE_SCOPING_EL) {
5461 !!!cp ('t215');
5462 last INSCOPE;
5463 }
5464 } # INSCOPE
5465 unless (defined $i) {
5466 !!!cp ('t216');
5467 ## TODO: This erorr type is wrong.
5468 !!!parse-error (type => 'unmatched end tag',
5469 text => $token->{tag_name}, token => $token);
5470 ## Ignore the token
5471 !!!nack ('t216.1');
5472 !!!next-token;
5473 next B;
5474 }
5475
5476 ## Clear back to table body context
5477 while (not ($self->{open_elements}->[-1]->[1]
5478 & TABLE_ROWS_SCOPING_EL)) {
5479 !!!cp ('t217');
5480 ## ISSUE: Can this state be reached?
5481 pop @{$self->{open_elements}};
5482 }
5483
5484 ## As if <{current node}>
5485 ## have an element in table scope
5486 ## true by definition
5487
5488 ## Clear back to table body context
5489 ## nop by definition
5490
5491 pop @{$self->{open_elements}};
5492 $self->{insertion_mode} = IN_TABLE_IM;
5493 ## reprocess in "in table" insertion mode...
5494 } else {
5495 !!!cp ('t218');
5496 }
5497
5498 if ($token->{tag_name} eq 'col') {
5499 ## Clear back to table context
5500 while (not ($self->{open_elements}->[-1]->[1]
5501 & TABLE_SCOPING_EL)) {
5502 !!!cp ('t219');
5503 ## ISSUE: Can this state be reached?
5504 pop @{$self->{open_elements}};
5505 }
5506
5507 !!!insert-element ('colgroup',, $token);
5508 $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5509 ## reprocess
5510 !!!ack-later;
5511 next B;
5512 } elsif ({
5513 caption => 1,
5514 colgroup => 1,
5515 tbody => 1, tfoot => 1, thead => 1,
5516 }->{$token->{tag_name}}) {
5517 ## Clear back to table context
5518 while (not ($self->{open_elements}->[-1]->[1]
5519 & TABLE_SCOPING_EL)) {
5520 !!!cp ('t220');
5521 ## ISSUE: Can this state be reached?
5522 pop @{$self->{open_elements}};
5523 }
5524
5525 push @$active_formatting_elements, ['#marker', '']
5526 if $token->{tag_name} eq 'caption';
5527
5528 !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5529 $self->{insertion_mode} = {
5530 caption => IN_CAPTION_IM,
5531 colgroup => IN_COLUMN_GROUP_IM,
5532 tbody => IN_TABLE_BODY_IM,
5533 tfoot => IN_TABLE_BODY_IM,
5534 thead => IN_TABLE_BODY_IM,
5535 }->{$token->{tag_name}};
5536 !!!next-token;
5537 !!!nack ('t220.1');
5538 next B;
5539 } else {
5540 die "$0: in table: <>: $token->{tag_name}";
5541 }
5542 } elsif ($token->{tag_name} eq 'table') {
5543 !!!parse-error (type => 'not closed',
5544 text => $self->{open_elements}->[-1]->[0]
5545 ->manakai_local_name,
5546 token => $token);
5547
5548 ## As if </table>
5549 ## have a table element in table scope
5550 my $i;
5551 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5552 my $node = $self->{open_elements}->[$_];
5553 if ($node->[1] & TABLE_EL) {
5554 !!!cp ('t221');
5555 $i = $_;
5556 last INSCOPE;
5557 } elsif ($node->[1] & TABLE_SCOPING_EL) {
5558 !!!cp ('t222');
5559 last INSCOPE;
5560 }
5561 } # INSCOPE
5562 unless (defined $i) {
5563 !!!cp ('t223');
5564 ## TODO: The following is wrong, maybe.
5565 !!!parse-error (type => 'unmatched end tag', text => 'table',
5566 token => $token);
5567 ## Ignore tokens </table><table>
5568 !!!nack ('t223.1');
5569 !!!next-token;
5570 next B;
5571 }
5572
5573 ## TODO: Followings are removed from the latest spec.
5574 ## generate implied end tags
5575 while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5576 !!!cp ('t224');
5577 pop @{$self->{open_elements}};
5578 }
5579
5580 unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5581 !!!cp ('t225');
5582 ## NOTE: |<table><tr><table>|
5583 !!!parse-error (type => 'not closed',
5584 text => $self->{open_elements}->[-1]->[0]
5585 ->manakai_local_name,
5586 token => $token);
5587 } else {
5588 !!!cp ('t226');
5589 }
5590
5591 splice @{$self->{open_elements}}, $i;
5592 pop @{$open_tables};
5593
5594 $self->_reset_insertion_mode;
5595
5596 ## reprocess
5597 !!!ack-later;
5598 next B;
5599 } elsif ($token->{tag_name} eq 'style') {
5600 if (not $open_tables->[-1]->[1]) { # tainted
5601 !!!cp ('t227.8');
5602 ## NOTE: This is a "as if in head" code clone.
5603 $parse_rcdata->(CDATA_CONTENT_MODEL);
5604 next B;
5605 } else {
5606 !!!cp ('t227.7');
5607 #
5608 }
5609 } elsif ($token->{tag_name} eq 'script') {
5610 if (not $open_tables->[-1]->[1]) { # tainted
5611 !!!cp ('t227.6');
5612 ## NOTE: This is a "as if in head" code clone.
5613 $script_start_tag->();
5614 next B;
5615 } else {
5616 !!!cp ('t227.5');
5617 #
5618 }
5619 } elsif ($token->{tag_name} eq 'input') {
5620 if (not $open_tables->[-1]->[1]) { # tainted
5621 if ($token->{attributes}->{type}) { ## TODO: case
5622 my $type = lc $token->{attributes}->{type}->{value};
5623 if ($type eq 'hidden') {
5624 !!!cp ('t227.3');
5625 !!!parse-error (type => 'in table',
5626 text => $token->{tag_name}, token => $token);
5627
5628 !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5629
5630 ## TODO: form element pointer
5631
5632 pop @{$self->{open_elements}};
5633
5634 !!!next-token;
5635 !!!ack ('t227.2.1');
5636 next B;
5637 } else {
5638 !!!cp ('t227.2');
5639 #
5640 }
5641 } else {
5642 !!!cp ('t227.1');
5643 #
5644 }
5645 } else {
5646 !!!cp ('t227.4');
5647 #
5648 }
5649 } else {
5650 !!!cp ('t227');
5651 #
5652 }
5653
5654 !!!parse-error (type => 'in table', text => $token->{tag_name},
5655 token => $token);
5656
5657 $insert = $insert_to_foster;
5658 #
5659 } elsif ($token->{type} == END_TAG_TOKEN) {
5660 if ($token->{tag_name} eq 'tr' and
5661 $self->{insertion_mode} == IN_ROW_IM) {
5662 ## have an element in table scope
5663 my $i;
5664 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5665 my $node = $self->{open_elements}->[$_];
5666 if ($node->[1] & TABLE_ROW_EL) {
5667 !!!cp ('t228');
5668 $i = $_;
5669 last INSCOPE;
5670 } elsif ($node->[1] & TABLE_SCOPING_EL) {
5671 !!!cp ('t229');
5672 last INSCOPE;
5673 }
5674 } # INSCOPE
5675 unless (defined $i) {
5676 !!!cp ('t230');
5677 !!!parse-error (type => 'unmatched end tag',
5678 text => $token->{tag_name}, token => $token);
5679 ## Ignore the token
5680 !!!nack ('t230.1');
5681 !!!next-token;
5682 next B;
5683 } else {
5684 !!!cp ('t232');
5685 }
5686
5687 ## Clear back to table row context
5688 while (not ($self->{open_elements}->[-1]->[1]
5689 & TABLE_ROW_SCOPING_EL)) {
5690 !!!cp ('t231');
5691 ## ISSUE: Can this state be reached?
5692 pop @{$self->{open_elements}};
5693 }
5694
5695 pop @{$self->{open_elements}}; # tr
5696 $self->{insertion_mode} = IN_TABLE_BODY_IM;
5697 !!!next-token;
5698 !!!nack ('t231.1');
5699 next B;
5700 } elsif ($token->{tag_name} eq 'table') {
5701 if ($self->{insertion_mode} == IN_ROW_IM) {
5702 ## As if </tr>
5703 ## have an element in table scope
5704 my $i;
5705 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5706 my $node = $self->{open_elements}->[$_];
5707 if ($node->[1] & TABLE_ROW_EL) {
5708 !!!cp ('t233');
5709 $i = $_;
5710 last INSCOPE;
5711 } elsif ($node->[1] & TABLE_SCOPING_EL) {
5712 !!!cp ('t234');
5713 last INSCOPE;
5714 }
5715 } # INSCOPE
5716 unless (defined $i) {
5717 !!!cp ('t235');
5718 ## TODO: The following is wrong.
5719 !!!parse-error (type => 'unmatched end tag',
5720 text => $token->{type}, token => $token);
5721 ## Ignore the token
5722 !!!nack ('t236.1');
5723 !!!next-token;
5724 next B;
5725 }
5726
5727 ## Clear back to table row context
5728 while (not ($self->{open_elements}->[-1]->[1]
5729 & TABLE_ROW_SCOPING_EL)) {
5730 !!!cp ('t236');
5731 ## ISSUE: Can this state be reached?
5732 pop @{$self->{open_elements}};
5733 }
5734
5735 pop @{$self->{open_elements}}; # tr
5736 $self->{insertion_mode} = IN_TABLE_BODY_IM;
5737 ## reprocess in the "in table body" insertion mode...
5738 }
5739
5740 if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5741 ## have an element in table scope
5742 my $i;
5743 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5744 my $node = $self->{open_elements}->[$_];
5745 if ($node->[1] & TABLE_ROW_GROUP_EL) {
5746 !!!cp ('t237');
5747 $i = $_;
5748 last INSCOPE;
5749 } elsif ($node->[1] & TABLE_SCOPING_EL) {
5750 !!!cp ('t238');
5751 last INSCOPE;
5752 }
5753 } # INSCOPE
5754 unless (defined $i) {
5755 !!!cp ('t239');
5756 !!!parse-error (type => 'unmatched end tag',
5757 text => $token->{tag_name}, token => $token);
5758 ## Ignore the token
5759 !!!nack ('t239.1');
5760 !!!next-token;
5761 next B;
5762 }
5763
5764 ## Clear back to table body context
5765 while (not ($self->{open_elements}->[-1]->[1]
5766 & TABLE_ROWS_SCOPING_EL)) {
5767 !!!cp ('t240');
5768 pop @{$self->{open_elements}};
5769 }
5770
5771 ## As if <{current node}>
5772 ## have an element in table scope
5773 ## true by definition
5774
5775 ## Clear back to table body context
5776 ## nop by definition
5777
5778 pop @{$self->{open_elements}};
5779 $self->{insertion_mode} = IN_TABLE_IM;
5780 ## reprocess in the "in table" insertion mode...
5781 }
5782
5783 ## NOTE: </table> in the "in table" insertion mode.
5784 ## When you edit the code fragment below, please ensure that
5785 ## the code for <table> in the "in table" insertion mode
5786 ## is synced with it.
5787
5788 ## have a table element in table scope
5789 my $i;
5790 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5791 my $node = $self->{open_elements}->[$_];
5792 if ($node->[1] & TABLE_EL) {
5793 !!!cp ('t241');
5794 $i = $_;
5795 last INSCOPE;
5796 } elsif ($node->[1] & TABLE_SCOPING_EL) {
5797 !!!cp ('t242');
5798 last INSCOPE;
5799 }
5800 } # INSCOPE
5801 unless (defined $i) {
5802 !!!cp ('t243');
5803 !!!parse-error (type => 'unmatched end tag',
5804 text => $token->{tag_name}, token => $token);
5805 ## Ignore the token
5806 !!!nack ('t243.1');
5807 !!!next-token;
5808 next B;
5809 }
5810
5811 splice @{$self->{open_elements}}, $i;
5812 pop @{$open_tables};
5813
5814 $self->_reset_insertion_mode;
5815
5816 !!!next-token;
5817 next B;
5818 } elsif ({
5819 tbody => 1, tfoot => 1, thead => 1,
5820 }->{$token->{tag_name}} and
5821 $self->{insertion_mode} & ROW_IMS) {
5822 if ($self->{insertion_mode} == IN_ROW_IM) {
5823 ## have an element in table scope
5824 my $i;
5825 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5826 my $node = $self->{open_elements}->[$_];
5827 if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5828 !!!cp ('t247');
5829 $i = $_;
5830 last INSCOPE;
5831 } elsif ($node->[1] & TABLE_SCOPING_EL) {
5832 !!!cp ('t248');
5833 last INSCOPE;
5834 }
5835 } # INSCOPE
5836 unless (defined $i) {
5837 !!!cp ('t249');
5838 !!!parse-error (type => 'unmatched end tag',
5839 text => $token->{tag_name}, token => $token);
5840 ## Ignore the token
5841 !!!nack ('t249.1');
5842 !!!next-token;
5843 next B;
5844 }
5845
5846 ## As if </tr>
5847 ## have an element in table scope
5848 my $i;
5849 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5850 my $node = $self->{open_elements}->[$_];
5851 if ($node->[1] & TABLE_ROW_EL) {
5852 !!!cp ('t250');
5853 $i = $_;
5854 last INSCOPE;
5855 } elsif ($node->[1] & TABLE_SCOPING_EL) {
5856 !!!cp ('t251');
5857 last INSCOPE;
5858 }
5859 } # INSCOPE
5860 unless (defined $i) {
5861 !!!cp ('t252');
5862 !!!parse-error (type => 'unmatched end tag',
5863 text => 'tr', token => $token);
5864 ## Ignore the token
5865 !!!nack ('t252.1');
5866 !!!next-token;
5867 next B;
5868 }
5869
5870 ## Clear back to table row context
5871 while (not ($self->{open_elements}->[-1]->[1]
5872 & TABLE_ROW_SCOPING_EL)) {
5873 !!!cp ('t253');
5874 ## ISSUE: Can this case be reached?
5875 pop @{$self->{open_elements}};
5876 }
5877
5878 pop @{$self->{open_elements}}; # tr
5879 $self->{insertion_mode} = IN_TABLE_BODY_IM;
5880 ## reprocess in the "in table body" insertion mode...
5881 }
5882
5883 ## have an element in table scope
5884 my $i;
5885 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5886 my $node = $self->{open_elements}->[$_];
5887 if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5888 !!!cp ('t254');
5889 $i = $_;
5890 last INSCOPE;
5891 } elsif ($node->[1] & TABLE_SCOPING_EL) {
5892 !!!cp ('t255');
5893 last INSCOPE;
5894 }
5895 } # INSCOPE
5896 unless (defined $i) {
5897 !!!cp ('t256');
5898 !!!parse-error (type => 'unmatched end tag',
5899 text => $token->{tag_name}, token => $token);
5900 ## Ignore the token
5901 !!!nack ('t256.1');
5902 !!!next-token;
5903 next B;
5904 }
5905
5906 ## Clear back to table body context
5907 while (not ($self->{open_elements}->[-1]->[1]
5908 & TABLE_ROWS_SCOPING_EL)) {
5909 !!!cp ('t257');
5910 ## ISSUE: Can this case be reached?
5911 pop @{$self->{open_elements}};
5912 }
5913
5914 pop @{$self->{open_elements}};
5915 $self->{insertion_mode} = IN_TABLE_IM;
5916 !!!nack ('t257.1');
5917 !!!next-token;
5918 next B;
5919 } elsif ({
5920 body => 1, caption => 1, col => 1, colgroup => 1,
5921 html => 1, td => 1, th => 1,
5922 tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5923 tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5924 }->{$token->{tag_name}}) {
5925 !!!cp ('t258');
5926 !!!parse-error (type => 'unmatched end tag',
5927 text => $token->{tag_name}, token => $token);
5928 ## Ignore the token
5929 !!!nack ('t258.1');
5930 !!!next-token;
5931 next B;
5932 } else {
5933 !!!cp ('t259');
5934 !!!parse-error (type => 'in table:/',
5935 text => $token->{tag_name}, token => $token);
5936
5937 $insert = $insert_to_foster;
5938 #
5939 }
5940 } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5941 unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5942 @{$self->{open_elements}} == 1) { # redundant, maybe
5943 !!!parse-error (type => 'in body:#eof', token => $token);
5944 !!!cp ('t259.1');
5945 #
5946 } else {
5947 !!!cp ('t259.2');
5948 #
5949 }
5950
5951 ## Stop parsing
5952 last B;
5953 } else {
5954 die "$0: $token->{type}: Unknown token type";
5955 }
5956 } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
5957 if ($token->{type} == CHARACTER_TOKEN) {
5958 if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5959 $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5960 unless (length $token->{data}) {
5961 !!!cp ('t260');
5962 !!!next-token;
5963 next B;
5964 }
5965 }
5966
5967 !!!cp ('t261');
5968 #
5969 } elsif ($token->{type} == START_TAG_TOKEN) {
5970 if ($token->{tag_name} eq 'col') {
5971 !!!cp ('t262');
5972 !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5973 pop @{$self->{open_elements}};
5974 !!!ack ('t262.1');
5975 !!!next-token;
5976 next B;
5977 } else {
5978 !!!cp ('t263');
5979 #
5980 }
5981 } elsif ($token->{type} == END_TAG_TOKEN) {
5982 if ($token->{tag_name} eq 'colgroup') {
5983 if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5984 !!!cp ('t264');
5985 !!!parse-error (type => 'unmatched end tag',
5986 text => 'colgroup', token => $token);
5987 ## Ignore the token
5988 !!!next-token;
5989 next B;
5990 } else {
5991 !!!cp ('t265');
5992 pop @{$self->{open_elements}}; # colgroup
5993 $self->{insertion_mode} = IN_TABLE_IM;
5994 !!!next-token;
5995 next B;
5996 }
5997 } elsif ($token->{tag_name} eq 'col') {
5998 !!!cp ('t266');
5999 !!!parse-error (type => 'unmatched end tag',
6000 text => 'col', token => $token);
6001 ## Ignore the token
6002 !!!next-token;
6003 next B;
6004 } else {
6005 !!!cp ('t267');
6006 #
6007 }
6008 } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6009 if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6010 @{$self->{open_elements}} == 1) { # redundant, maybe
6011 !!!cp ('t270.2');
6012 ## Stop parsing.
6013 last B;
6014 } else {
6015 ## NOTE: As if </colgroup>.
6016 !!!cp ('t270.1');
6017 pop @{$self->{open_elements}}; # colgroup
6018 $self->{insertion_mode} = IN_TABLE_IM;
6019 ## Reprocess.
6020 next B;
6021 }
6022 } else {
6023 die "$0: $token->{type}: Unknown token type";
6024 }
6025
6026 ## As if </colgroup>
6027 if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6028 !!!cp ('t269');
6029 ## TODO: Wrong error type?
6030 !!!parse-error (type => 'unmatched end tag',
6031 text => 'colgroup', token => $token);
6032 ## Ignore the token
6033 !!!nack ('t269.1');
6034 !!!next-token;
6035 next B;
6036 } else {
6037 !!!cp ('t270');
6038 pop @{$self->{open_elements}}; # colgroup
6039 $self->{insertion_mode} = IN_TABLE_IM;
6040 !!!ack-later;
6041 ## reprocess
6042 next B;
6043 }
6044 } elsif ($self->{insertion_mode} & SELECT_IMS) {
6045 if ($token->{type} == CHARACTER_TOKEN) {
6046 !!!cp ('t271');
6047 $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
6048 !!!next-token;
6049 next B;
6050 } elsif ($token->{type} == START_TAG_TOKEN) {
6051 if ($token->{tag_name} eq 'option') {
6052 if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6053 !!!cp ('t272');
6054 ## As if </option>
6055 pop @{$self->{open_elements}};
6056 } else {
6057 !!!cp ('t273');
6058 }
6059
6060 !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6061 !!!nack ('t273.1');
6062 !!!next-token;
6063 next B;
6064 } elsif ($token->{tag_name} eq 'optgroup') {
6065 if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6066 !!!cp ('t274');
6067 ## As if </option>
6068 pop @{$self->{open_elements}};
6069 } else {
6070 !!!cp ('t275');
6071 }
6072
6073 if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6074 !!!cp ('t276');
6075 ## As if </optgroup>
6076 pop @{$self->{open_elements}};
6077 } else {
6078 !!!cp ('t277');
6079 }
6080
6081 !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6082 !!!nack ('t277.1');
6083 !!!next-token;
6084 next B;
6085 } elsif ({
6086 select => 1, input => 1, textarea => 1,
6087 }->{$token->{tag_name}} or
6088 ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6089 {
6090 caption => 1, table => 1,
6091 tbody => 1, tfoot => 1, thead => 1,
6092 tr => 1, td => 1, th => 1,
6093 }->{$token->{tag_name}})) {
6094 ## TODO: The type below is not good - <select> is replaced by </select>
6095 !!!parse-error (type => 'not closed', text => 'select',
6096 token => $token);
6097 ## NOTE: As if the token were </select> (<select> case) or
6098 ## as if there were </select> (otherwise).
6099 ## have an element in table scope
6100 my $i;
6101 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6102 my $node = $self->{open_elements}->[$_];
6103 if ($node->[1] & SELECT_EL) {
6104 !!!cp ('t278');
6105 $i = $_;
6106 last INSCOPE;
6107 } elsif ($node->[1] & TABLE_SCOPING_EL) {
6108 !!!cp ('t279');
6109 last INSCOPE;
6110 }
6111 } # INSCOPE
6112 unless (defined $i) {
6113 !!!cp ('t280');
6114 !!!parse-error (type => 'unmatched end tag',
6115 text => 'select', token => $token);
6116 ## Ignore the token
6117 !!!nack ('t280.1');
6118 !!!next-token;
6119 next B;
6120 }
6121
6122 !!!cp ('t281');
6123 splice @{$self->{open_elements}}, $i;
6124
6125 $self->_reset_insertion_mode;
6126
6127 if ($token->{tag_name} eq 'select') {
6128 !!!nack ('t281.2');
6129 !!!next-token;
6130 next B;
6131 } else {
6132 !!!cp ('t281.1');
6133 !!!ack-later;
6134 ## Reprocess the token.
6135 next B;
6136 }
6137 } else {
6138 !!!cp ('t282');
6139 !!!parse-error (type => 'in select',
6140 text => $token->{tag_name}, token => $token);
6141 ## Ignore the token
6142 !!!nack ('t282.1');
6143 !!!next-token;
6144 next B;
6145 }
6146 } elsif ($token->{type} == END_TAG_TOKEN) {
6147 if ($token->{tag_name} eq 'optgroup') {
6148 if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6149 $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6150 !!!cp ('t283');
6151 ## As if </option>
6152 splice @{$self->{open_elements}}, -2;
6153 } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6154 !!!cp ('t284');
6155 pop @{$self->{open_elements}};
6156 } else {
6157 !!!cp ('t285');
6158 !!!parse-error (type => 'unmatched end tag',
6159 text => $token->{tag_name}, token => $token);
6160 ## Ignore the token
6161 }
6162 !!!nack ('t285.1');
6163 !!!next-token;
6164 next B;
6165 } elsif ($token->{tag_name} eq 'option') {
6166 if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6167 !!!cp ('t286');
6168 pop @{$self->{open_elements}};
6169 } else {
6170 !!!cp ('t287');
6171 !!!parse-error (type => 'unmatched end tag',
6172 text => $token->{tag_name}, token => $token);
6173 ## Ignore the token
6174 }
6175 !!!nack ('t287.1');
6176 !!!next-token;
6177 next B;
6178 } elsif ($token->{tag_name} eq 'select') {
6179 ## have an element in table scope
6180 my $i;
6181 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6182 my $node = $self->{open_elements}->[$_];
6183 if ($node->[1] & SELECT_EL) {
6184 !!!cp ('t288');
6185 $i = $_;
6186 last INSCOPE;
6187 } elsif ($node->[1] & TABLE_SCOPING_EL) {
6188 !!!cp ('t289');
6189 last INSCOPE;
6190 }
6191 } # INSCOPE
6192 unless (defined $i) {
6193 !!!cp ('t290');
6194 !!!parse-error (type => 'unmatched end tag',
6195 text => $token->{tag_name}, token => $token);
6196 ## Ignore the token
6197 !!!nack ('t290.1');
6198 !!!next-token;
6199 next B;
6200 }
6201
6202 !!!cp ('t291');
6203 splice @{$self->{open_elements}}, $i;
6204
6205 $self->_reset_insertion_mode;
6206
6207 !!!nack ('t291.1');
6208 !!!next-token;
6209 next B;
6210 } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6211 {
6212 caption => 1, table => 1, tbody => 1,
6213 tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6214 }->{$token->{tag_name}}) {
6215 ## TODO: The following is wrong?
6216 !!!parse-error (type => 'unmatched end tag',
6217 text => $token->{tag_name}, token => $token);
6218
6219 ## have an element in table scope
6220 my $i;
6221 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6222 my $node = $self->{open_elements}->[$_];
6223 if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6224 !!!cp ('t292');
6225 $i = $_;
6226 last INSCOPE;
6227 } elsif ($node->[1] & TABLE_SCOPING_EL) {
6228 !!!cp ('t293');
6229 last INSCOPE;
6230 }
6231 } # INSCOPE
6232 unless (defined $i) {
6233 !!!cp ('t294');
6234 ## Ignore the token
6235 !!!nack ('t294.1');
6236 !!!next-token;
6237 next B;
6238 }
6239
6240 ## As if </select>
6241 ## have an element in table scope
6242 undef $i;
6243 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6244 my $node = $self->{open_elements}->[$_];
6245 if ($node->[1] & SELECT_EL) {
6246 !!!cp ('t295');
6247 $i = $_;
6248 last INSCOPE;
6249 } elsif ($node->[1] & TABLE_SCOPING_EL) {
6250 ## ISSUE: Can this state be reached?
6251 !!!cp ('t296');
6252 last INSCOPE;
6253 }
6254 } # INSCOPE
6255 unless (defined $i) {
6256 !!!cp ('t297');
6257 ## TODO: The following error type is correct?
6258 !!!parse-error (type => 'unmatched end tag',
6259 text => 'select', token => $token);
6260 ## Ignore the </select> token
6261 !!!nack ('t297.1');
6262 !!!next-token; ## TODO: ok?
6263 next B;
6264 }
6265
6266 !!!cp ('t298');
6267 splice @{$self->{open_elements}}, $i;
6268
6269 $self->_reset_insertion_mode;
6270
6271 !!!ack-later;
6272 ## reprocess
6273 next B;
6274 } else {
6275 !!!cp ('t299');
6276 !!!parse-error (type => 'in select:/',
6277 text => $token->{tag_name}, token => $token);
6278 ## Ignore the token
6279 !!!nack ('t299.3');
6280 !!!next-token;
6281 next B;
6282 }
6283 } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6284 unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6285 @{$self->{open_elements}} == 1) { # redundant, maybe
6286 !!!cp ('t299.1');
6287 !!!parse-error (type => 'in body:#eof', token => $token);
6288 } else {
6289 !!!cp ('t299.2');
6290 }
6291
6292 ## Stop parsing.
6293 last B;
6294 } else {
6295 die "$0: $token->{type}: Unknown token type";
6296 }
6297 } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
6298 if ($token->{type} == CHARACTER_TOKEN) {
6299 if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6300 my $data = $1;
6301 ## As if in body
6302 $reconstruct_active_formatting_elements->($insert_to_current);
6303
6304 $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6305
6306 unless (length $token->{data}) {
6307 !!!cp ('t300');
6308 !!!next-token;
6309 next B;
6310 }
6311 }
6312
6313 if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6314 !!!cp ('t301');
6315 !!!parse-error (type => 'after html:#text', token => $token);
6316
6317 ## Reprocess in the "after body" insertion mode.
6318 } else {
6319 !!!cp ('t302');
6320 }
6321
6322 ## "after body" insertion mode
6323 !!!parse-error (type => 'after body:#text', token => $token);
6324
6325 $self->{insertion_mode} = IN_BODY_IM;
6326 ## reprocess
6327 next B;
6328 } elsif ($token->{type} == START_TAG_TOKEN) {
6329 if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6330 !!!cp ('t303');
6331 !!!parse-error (type => 'after html',
6332 text => $token->{tag_name}, token => $token);
6333
6334 ## Reprocess in the "after body" insertion mode.
6335 } else {
6336 !!!cp ('t304');
6337 }
6338
6339 ## "after body" insertion mode
6340 !!!parse-error (type => 'after body',
6341 text => $token->{tag_name}, token => $token);
6342
6343 $self->{insertion_mode} = IN_BODY_IM;
6344 !!!ack-later;
6345 ## reprocess
6346 next B;
6347 } elsif ($token->{type} == END_TAG_TOKEN) {
6348 if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6349 !!!cp ('t305');
6350 !!!parse-error (type => 'after html:/',
6351 text => $token->{tag_name}, token => $token);
6352
6353 $self->{insertion_mode} = AFTER_BODY_IM;
6354 ## Reprocess in the "after body" insertion mode.
6355 } else {
6356 !!!cp ('t306');
6357 }
6358
6359 ## "after body" insertion mode
6360 if ($token->{tag_name} eq 'html') {
6361 if (defined $self->{inner_html_node}) {
6362 !!!cp ('t307');
6363 !!!parse-error (type => 'unmatched end tag',
6364 text => 'html', token => $token);
6365 ## Ignore the token
6366 !!!next-token;
6367 next B;
6368 } else {
6369 !!!cp ('t308');
6370 $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6371 !!!next-token;
6372 next B;
6373 }
6374 } else {
6375 !!!cp ('t309');
6376 !!!parse-error (type => 'after body:/',
6377 text => $token->{tag_name}, token => $token);
6378
6379 $self->{insertion_mode} = IN_BODY_IM;
6380 ## reprocess
6381 next B;
6382 }
6383 } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6384 !!!cp ('t309.2');
6385 ## Stop parsing
6386 last B;
6387 } else {
6388 die "$0: $token->{type}: Unknown token type";
6389 }
6390 } elsif ($self->{insertion_mode} & FRAME_IMS) {
6391 if ($token->{type} == CHARACTER_TOKEN) {
6392 if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6393 $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6394
6395 unless (length $token->{data}) {
6396 !!!cp ('t310');
6397 !!!next-token;
6398 next B;
6399 }
6400 }
6401
6402 if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6403 if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6404 !!!cp ('t311');
6405 !!!parse-error (type => 'in frameset:#text', token => $token);
6406 } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6407 !!!cp ('t312');
6408 !!!parse-error (type => 'after frameset:#text', token => $token);
6409 } else { # "after after frameset"
6410 !!!cp ('t313');
6411 !!!parse-error (type => 'after html:#text', token => $token);
6412 }
6413
6414 ## Ignore the token.
6415 if (length $token->{data}) {
6416 !!!cp ('t314');
6417 ## reprocess the rest of characters
6418 } else {
6419 !!!cp ('t315');
6420 !!!next-token;
6421 }
6422 next B;
6423 }
6424
6425 die qq[$0: Character "$token->{data}"];
6426 } elsif ($token->{type} == START_TAG_TOKEN) {
6427 if ($token->{tag_name} eq 'frameset' and
6428 $self->{insertion_mode} == IN_FRAMESET_IM) {
6429 !!!cp ('t318');
6430 !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6431 !!!nack ('t318.1');
6432 !!!next-token;
6433 next B;
6434 } elsif ($token->{tag_name} eq 'frame' and
6435 $self->{insertion_mode} == IN_FRAMESET_IM) {
6436 !!!cp ('t319');
6437 !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6438 pop @{$self->{open_elements}};
6439 !!!ack ('t319.1');
6440 !!!next-token;
6441 next B;
6442 } elsif ($token->{tag_name} eq 'noframes') {
6443 !!!cp ('t320');
6444 ## NOTE: As if in head.
6445 $parse_rcdata->(CDATA_CONTENT_MODEL);
6446 next B;
6447
6448 ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6449 ## has no parse error.
6450 } else {
6451 if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6452 !!!cp ('t321');
6453 !!!parse-error (type => 'in frameset',
6454 text => $token->{tag_name}, token => $token);
6455 } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6456 !!!cp ('t322');
6457 !!!parse-error (type => 'after frameset',
6458 text => $token->{tag_name}, token => $token);
6459 } else { # "after after frameset"
6460 !!!cp ('t322.2');
6461 !!!parse-error (type => 'after after frameset',
6462 text => $token->{tag_name}, token => $token);
6463 }
6464 ## Ignore the token
6465 !!!nack ('t322.1');
6466 !!!next-token;
6467 next B;
6468 }
6469 } elsif ($token->{type} == END_TAG_TOKEN) {
6470 if ($token->{tag_name} eq 'frameset' and
6471 $self->{insertion_mode} == IN_FRAMESET_IM) {
6472 if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6473 @{$self->{open_elements}} == 1) {
6474 !!!cp ('t325');
6475 !!!parse-error (type => 'unmatched end tag',
6476 text => $token->{tag_name}, token => $token);
6477 ## Ignore the token
6478 !!!next-token;
6479 } else {
6480 !!!cp ('t326');
6481 pop @{$self->{open_elements}};
6482 !!!next-token;
6483 }
6484
6485 if (not defined $self->{inner_html_node} and
6486 not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6487 !!!cp ('t327');
6488 $self->{insertion_mode} = AFTER_FRAMESET_IM;
6489 } else {
6490 !!!cp ('t328');
6491 }
6492 next B;
6493 } elsif ($token->{tag_name} eq 'html' and
6494 $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6495 !!!cp ('t329');
6496 $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6497 !!!next-token;
6498 next B;
6499 } else {
6500 if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6501 !!!cp ('t330');
6502 !!!parse-error (type => 'in frameset:/',
6503 text => $token->{tag_name}, token => $token);
6504 } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6505 !!!cp ('t330.1');
6506 !!!parse-error (type => 'after frameset:/',
6507 text => $token->{tag_name}, token => $token);
6508 } else { # "after after html"
6509 !!!cp ('t331');
6510 !!!parse-error (type => 'after after frameset:/',
6511 text => $token->{tag_name}, token => $token);
6512 }
6513 ## Ignore the token
6514 !!!next-token;
6515 next B;
6516 }
6517 } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6518 unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6519 @{$self->{open_elements}} == 1) { # redundant, maybe
6520 !!!cp ('t331.1');
6521 !!!parse-error (type => 'in body:#eof', token => $token);
6522 } else {
6523 !!!cp ('t331.2');
6524 }
6525
6526 ## Stop parsing
6527 last B;
6528 } else {
6529 die "$0: $token->{type}: Unknown token type";
6530 }
6531
6532 ## ISSUE: An issue in spec here
6533 } else {
6534 die "$0: $self->{insertion_mode}: Unknown insertion mode";
6535 }
6536
6537 ## "in body" insertion mode
6538 if ($token->{type} == START_TAG_TOKEN) {
6539 if ($token->{tag_name} eq 'script') {
6540 !!!cp ('t332');
6541 ## NOTE: This is an "as if in head" code clone
6542 $script_start_tag->();
6543 next B;
6544 } elsif ($token->{tag_name} eq 'style') {
6545 !!!cp ('t333');
6546 ## NOTE: This is an "as if in head" code clone
6547 $parse_rcdata->(CDATA_CONTENT_MODEL);
6548 next B;
6549 } elsif ({
6550 base => 1, link => 1,
6551 }->{$token->{tag_name}}) {
6552 !!!cp ('t334');
6553 ## NOTE: This is an "as if in head" code clone, only "-t" differs
6554 !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6555 pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6556 !!!ack ('t334.1');
6557 !!!next-token;
6558 next B;
6559 } elsif ($token->{tag_name} eq 'meta') {
6560 ## NOTE: This is an "as if in head" code clone, only "-t" differs
6561 !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6562 my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6563
6564 unless ($self->{confident}) {
6565 if ($token->{attributes}->{charset}) {
6566 !!!cp ('t335');
6567 ## NOTE: Whether the encoding is supported or not is handled
6568 ## in the {change_encoding} callback.
6569 $self->{change_encoding}
6570 ->($self, $token->{attributes}->{charset}->{value}, $token);
6571
6572 $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6573 ->set_user_data (manakai_has_reference =>
6574 $token->{attributes}->{charset}
6575 ->{has_reference});
6576 } elsif ($token->{attributes}->{content}) {
6577 if ($token->{attributes}->{content}->{value}
6578 =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6579 [\x09-\x0D\x20]*=
6580 [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6581 ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6582 !!!cp ('t336');
6583 ## NOTE: Whether the encoding is supported or not is handled
6584 ## in the {change_encoding} callback.
6585 $self->{change_encoding}
6586 ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6587 $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6588 ->set_user_data (manakai_has_reference =>
6589 $token->{attributes}->{content}
6590 ->{has_reference});
6591 }
6592 }
6593 } else {
6594 if ($token->{attributes}->{charset}) {
6595 !!!cp ('t337');
6596 $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6597 ->set_user_data (manakai_has_reference =>
6598 $token->{attributes}->{charset}
6599 ->{has_reference});
6600 }
6601 if ($token->{attributes}->{content}) {
6602 !!!cp ('t338');
6603 $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6604 ->set_user_data (manakai_has_reference =>
6605 $token->{attributes}->{content}
6606 ->{has_reference});
6607 }
6608 }
6609
6610 !!!ack ('t338.1');
6611 !!!next-token;
6612 next B;
6613 } elsif ($token->{tag_name} eq 'title') {
6614 !!!cp ('t341');
6615 ## NOTE: This is an "as if in head" code clone
6616 $parse_rcdata->(RCDATA_CONTENT_MODEL);
6617 next B;
6618 } elsif ($token->{tag_name} eq 'body') {
6619 !!!parse-error (type => 'in body', text => 'body', token => $token);
6620
6621 if (@{$self->{open_elements}} == 1 or
6622 not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6623 !!!cp ('t342');
6624 ## Ignore the token
6625 } else {
6626 my $body_el = $self->{open_elements}->[1]->[0];
6627 for my $attr_name (keys %{$token->{attributes}}) {
6628 unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6629 !!!cp ('t343');
6630 $body_el->set_attribute_ns
6631 (undef, [undef, $attr_name],
6632 $token->{attributes}->{$attr_name}->{value});
6633 }
6634 }
6635 }
6636 !!!nack ('t343.1');
6637 !!!next-token;
6638 next B;
6639 } elsif ({
6640 address => 1, blockquote => 1, center => 1, dir => 1,
6641 div => 1, dl => 1, fieldset => 1,
6642 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6643 menu => 1, ol => 1, p => 1, ul => 1,
6644 pre => 1, listing => 1,
6645 form => 1,
6646 table => 1,
6647 hr => 1,
6648 }->{$token->{tag_name}}) {
6649 if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6650 !!!cp ('t350');
6651 !!!parse-error (type => 'in form:form', token => $token);
6652 ## Ignore the token
6653 !!!nack ('t350.1');
6654 !!!next-token;
6655 next B;
6656 }
6657
6658 ## has a p element in scope
6659 INSCOPE: for (reverse @{$self->{open_elements}}) {
6660 if ($_->[1] & P_EL) {
6661 !!!cp ('t344');
6662 !!!back-token; # <form>
6663 $token = {type => END_TAG_TOKEN, tag_name => 'p',
6664 line => $token->{line}, column => $token->{column}};
6665 next B;
6666 } elsif ($_->[1] & SCOPING_EL) {
6667 !!!cp ('t345');
6668 last INSCOPE;
6669 }
6670 } # INSCOPE
6671
6672 !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6673 if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6674 !!!nack ('t346.1');
6675 !!!next-token;
6676 if ($token->{type} == CHARACTER_TOKEN) {
6677 $token->{data} =~ s/^\x0A//;
6678 unless (length $token->{data}) {
6679 !!!cp ('t346');
6680 !!!next-token;
6681 } else {
6682 !!!cp ('t349');
6683 }
6684 } else {
6685 !!!cp ('t348');
6686 }
6687 } elsif ($token->{tag_name} eq 'form') {
6688 !!!cp ('t347.1');
6689 $self->{form_element} = $self->{open_elements}->[-1]->[0];
6690
6691 !!!nack ('t347.2');
6692 !!!next-token;
6693 } elsif ($token->{tag_name} eq 'table') {
6694 !!!cp ('t382');
6695 push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6696
6697 $self->{insertion_mode} = IN_TABLE_IM;
6698
6699 !!!nack ('t382.1');
6700 !!!next-token;
6701 } elsif ($token->{tag_name} eq 'hr') {
6702 !!!cp ('t386');
6703 pop @{$self->{open_elements}};
6704
6705 !!!nack ('t386.1');
6706 !!!next-token;
6707 } else {
6708 !!!nack ('t347.1');
6709 !!!next-token;
6710 }
6711 next B;
6712 } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6713 ## has a p element in scope
6714 INSCOPE: for (reverse @{$self->{open_elements}}) {
6715 if ($_->[1] & P_EL) {
6716 !!!cp ('t353');
6717 !!!back-token; # <x>
6718 $token = {type => END_TAG_TOKEN, tag_name => 'p',
6719 line => $token->{line}, column => $token->{column}};
6720 next B;
6721 } elsif ($_->[1] & SCOPING_EL) {
6722 !!!cp ('t354');
6723 last INSCOPE;
6724 }
6725 } # INSCOPE
6726
6727 ## Step 1
6728 my $i = -1;
6729 my $node = $self->{open_elements}->[$i];
6730 my $li_or_dtdd = {li => {li => 1},
6731 dt => {dt => 1, dd => 1},
6732 dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6733 LI: {
6734 ## Step 2
6735 if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6736 if ($i != -1) {
6737 !!!cp ('t355');
6738 !!!parse-error (type => 'not closed',
6739 text => $self->{open_elements}->[-1]->[0]
6740 ->manakai_local_name,
6741 token => $token);
6742 } else {
6743 !!!cp ('t356');
6744 }
6745 splice @{$self->{open_elements}}, $i;
6746 last LI;
6747 } else {
6748 !!!cp ('t357');
6749 }
6750
6751 ## Step 3
6752 if (not ($node->[1] & FORMATTING_EL) and
6753 #not $phrasing_category->{$node->[1]} and
6754 ($node->[1] & SPECIAL_EL or
6755 $node->[1] & SCOPING_EL) and
6756 not ($node->[1] & ADDRESS_EL) and
6757 not ($node->[1] & DIV_EL)) {
6758 !!!cp ('t358');
6759 last LI;
6760 }
6761
6762 !!!cp ('t359');
6763 ## Step 4
6764 $i--;
6765 $node = $self->{open_elements}->[$i];
6766 redo LI;
6767 } # LI
6768
6769 !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6770 !!!nack ('t359.1');
6771 !!!next-token;
6772 next B;
6773 } elsif ($token->{tag_name} eq 'plaintext') {
6774 ## has a p element in scope
6775 INSCOPE: for (reverse @{$self->{open_elements}}) {
6776 if ($_->[1] & P_EL) {
6777 !!!cp ('t367');
6778 !!!back-token; # <plaintext>
6779 $token = {type => END_TAG_TOKEN, tag_name => 'p',
6780 line => $token->{line}, column => $token->{column}};
6781 next B;
6782 } elsif ($_->[1] & SCOPING_EL) {
6783 !!!cp ('t368');
6784 last INSCOPE;
6785 }
6786 } # INSCOPE
6787
6788 !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6789
6790 $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6791
6792 !!!nack ('t368.1');
6793 !!!next-token;
6794 next B;
6795 } elsif ($token->{tag_name} eq 'a') {
6796 AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6797 my $node = $active_formatting_elements->[$i];
6798 if ($node->[1] & A_EL) {
6799 !!!cp ('t371');
6800 !!!parse-error (type => 'in a:a', token => $token);
6801
6802 !!!back-token; # <a>
6803 $token = {type => END_TAG_TOKEN, tag_name => 'a',
6804 line => $token->{line}, column => $token->{column}};
6805 $formatting_end_tag->($token);
6806
6807 AFE2: for (reverse 0..$#$active_formatting_elements) {
6808 if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
6809 !!!cp ('t372');
6810 splice @$active_formatting_elements, $_, 1;
6811 last AFE2;
6812 }
6813 } # AFE2
6814 OE: for (reverse 0..$#{$self->{open_elements}}) {
6815 if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
6816 !!!cp ('t373');
6817 splice @{$self->{open_elements}}, $_, 1;
6818 last OE;
6819 }
6820 } # OE
6821 last AFE;
6822 } elsif ($node->[0] eq '#marker') {
6823 !!!cp ('t374');
6824 last AFE;
6825 }
6826 } # AFE
6827
6828 $reconstruct_active_formatting_elements->($insert_to_current);
6829
6830 !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6831 push @$active_formatting_elements, $self->{open_elements}->[-1];
6832
6833 !!!nack ('t374.1');
6834 !!!next-token;
6835 next B;
6836 } elsif ($token->{tag_name} eq 'nobr') {
6837 $reconstruct_active_formatting_elements->($insert_to_current);
6838
6839 ## has a |nobr| element in scope
6840 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6841 my $node = $self->{open_elements}->[$_];
6842 if ($node->[1] & NOBR_EL) {
6843 !!!cp ('t376');
6844 !!!parse-error (type => 'in nobr:nobr', token => $token);
6845 !!!back-token; # <nobr>
6846 $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6847 line => $token->{line}, column => $token->{column}};
6848 next B;
6849 } elsif ($node->[1] & SCOPING_EL) {
6850 !!!cp ('t377');
6851 last INSCOPE;
6852 }
6853 } # INSCOPE
6854
6855 !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6856 push @$active_formatting_elements, $self->{open_elements}->[-1];
6857
6858 !!!nack ('t377.1');
6859 !!!next-token;
6860 next B;
6861 } elsif ($token->{tag_name} eq 'button') {
6862 ## has a button element in scope
6863 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6864 my $node = $self->{open_elements}->[$_];
6865 if ($node->[1] & BUTTON_EL) {
6866 !!!cp ('t378');
6867 !!!parse-error (type => 'in button:button', token => $token);
6868 !!!back-token; # <button>
6869 $token = {type => END_TAG_TOKEN, tag_name => 'button',
6870 line => $token->{line}, column => $token->{column}};
6871 next B;
6872 } elsif ($node->[1] & SCOPING_EL) {
6873 !!!cp ('t379');
6874 last INSCOPE;
6875 }
6876 } # INSCOPE
6877
6878 $reconstruct_active_formatting_elements->($insert_to_current);
6879
6880 !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6881
6882 ## TODO: associate with $self->{form_element} if defined
6883
6884 push @$active_formatting_elements, ['#marker', ''];
6885
6886 !!!nack ('t379.1');
6887 !!!next-token;
6888 next B;
6889 } elsif ({
6890 xmp => 1,
6891 iframe => 1,
6892 noembed => 1,
6893 noframes => 1, ## NOTE: This is an "as if in head" code clone.
6894 noscript => 0, ## TODO: 1 if scripting is enabled
6895 }->{$token->{tag_name}}) {
6896 if ($token->{tag_name} eq 'xmp') {
6897 !!!cp ('t381');
6898 $reconstruct_active_formatting_elements->($insert_to_current);
6899 } else {
6900 !!!cp ('t399');
6901 }
6902 ## NOTE: There is an "as if in body" code clone.
6903 $parse_rcdata->(CDATA_CONTENT_MODEL);
6904 next B;
6905 } elsif ($token->{tag_name} eq 'isindex') {
6906 !!!parse-error (type => 'isindex', token => $token);
6907
6908 if (defined $self->{form_element}) {
6909 !!!cp ('t389');
6910 ## Ignore the token
6911 !!!nack ('t389'); ## NOTE: Not acknowledged.
6912 !!!next-token;
6913 next B;
6914 } else {
6915 !!!ack ('t391.1');
6916
6917 my $at = $token->{attributes};
6918 my $form_attrs;
6919 $form_attrs->{action} = $at->{action} if $at->{action};
6920 my $prompt_attr = $at->{prompt};
6921 $at->{name} = {name => 'name', value => 'isindex'};
6922 delete $at->{action};
6923 delete $at->{prompt};
6924 my @tokens = (
6925 {type => START_TAG_TOKEN, tag_name => 'form',
6926 attributes => $form_attrs,
6927 line => $token->{line}, column => $token->{column}},
6928 {type => START_TAG_TOKEN, tag_name => 'hr',
6929 line => $token->{line}, column => $token->{column}},
6930 {type => START_TAG_TOKEN, tag_name => 'p',
6931 line => $token->{line}, column => $token->{column}},
6932 {type => START_TAG_TOKEN, tag_name => 'label',
6933 line => $token->{line}, column => $token->{column}},
6934 );
6935 if ($prompt_attr) {
6936 !!!cp ('t390');
6937 push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6938 #line => $token->{line}, column => $token->{column},
6939 };
6940 } else {
6941 !!!cp ('t391');
6942 push @tokens, {type => CHARACTER_TOKEN,
6943 data => 'This is a searchable index. Insert your search keywords here: ',
6944 #line => $token->{line}, column => $token->{column},
6945 }; # SHOULD
6946 ## TODO: make this configurable
6947 }
6948 push @tokens,
6949 {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6950 line => $token->{line}, column => $token->{column}},
6951 #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6952 {type => END_TAG_TOKEN, tag_name => 'label',
6953 line => $token->{line}, column => $token->{column}},
6954 {type => END_TAG_TOKEN, tag_name => 'p',
6955 line => $token->{line}, column => $token->{column}},
6956 {type => START_TAG_TOKEN, tag_name => 'hr',
6957 line => $token->{line}, column => $token->{column}},
6958 {type => END_TAG_TOKEN, tag_name => 'form',
6959 line => $token->{line}, column => $token->{column}};
6960 !!!back-token (@tokens);
6961 !!!next-token;
6962 next B;
6963 }
6964 } elsif ($token->{tag_name} eq 'textarea') {
6965 my $tag_name = $token->{tag_name};
6966 my $el;
6967 !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6968
6969 ## TODO: $self->{form_element} if defined
6970 $self->{content_model} = RCDATA_CONTENT_MODEL;
6971 delete $self->{escape}; # MUST
6972
6973 $insert->($el);
6974
6975 my $text = '';
6976 !!!nack ('t392.1');
6977 !!!next-token;
6978 if ($token->{type} == CHARACTER_TOKEN) {
6979 $token->{data} =~ s/^\x0A//;
6980 unless (length $token->{data}) {
6981 !!!cp ('t392');
6982 !!!next-token;
6983 } else {
6984 !!!cp ('t393');
6985 }
6986 } else {
6987 !!!cp ('t394');
6988 }
6989 while ($token->{type} == CHARACTER_TOKEN) {
6990 !!!cp ('t395');
6991 $text .= $token->{data};
6992 !!!next-token;
6993 }
6994 if (length $text) {
6995 !!!cp ('t396');
6996 $el->manakai_append_text ($text);
6997 }
6998
6999 $self->{content_model} = PCDATA_CONTENT_MODEL;
7000
7001 if ($token->{type} == END_TAG_TOKEN and
7002 $token->{tag_name} eq $tag_name) {
7003 !!!cp ('t397');
7004 ## Ignore the token
7005 } else {
7006 !!!cp ('t398');
7007 !!!parse-error (type => 'in RCDATA:#eof', token => $token);
7008 }
7009 !!!next-token;
7010 next B;
7011 } elsif ($token->{tag_name} eq 'rt' or
7012 $token->{tag_name} eq 'rp') {
7013 ## has a |ruby| element in scope
7014 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7015 my $node = $self->{open_elements}->[$_];
7016 if ($node->[1] & RUBY_EL) {
7017 !!!cp ('t398.1');
7018 ## generate implied end tags
7019 while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7020 !!!cp ('t398.2');
7021 pop @{$self->{open_elements}};
7022 }
7023 unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
7024 !!!cp ('t398.3');
7025 !!!parse-error (type => 'not closed',
7026 text => $self->{open_elements}->[-1]->[0]
7027 ->manakai_local_name,
7028 token => $token);
7029 pop @{$self->{open_elements}}
7030 while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
7031 }
7032 last INSCOPE;
7033 } elsif ($node->[1] & SCOPING_EL) {
7034 !!!cp ('t398.4');
7035 last INSCOPE;
7036 }
7037 } # INSCOPE
7038
7039 !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7040
7041 !!!nack ('t398.5');
7042 !!!next-token;
7043 redo B;
7044 } elsif ($token->{tag_name} eq 'math' or
7045 $token->{tag_name} eq 'svg') {
7046 $reconstruct_active_formatting_elements->($insert_to_current);
7047
7048 ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
7049
7050 ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
7051
7052 ## "adjust foreign attributes" - done in insert-element-f
7053
7054 !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
7055
7056 if ($self->{self_closing}) {
7057 pop @{$self->{open_elements}};
7058 !!!ack ('t398.1');
7059 } else {
7060 !!!cp ('t398.2');
7061 $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
7062 ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
7063 ## mode, "in body" (not "in foreign content") secondary insertion
7064 ## mode, maybe.
7065 }
7066
7067 !!!next-token;
7068 next B;
7069 } elsif ({
7070 caption => 1, col => 1, colgroup => 1, frame => 1,
7071 frameset => 1, head => 1, option => 1, optgroup => 1,
7072 tbody => 1, td => 1, tfoot => 1, th => 1,
7073 thead => 1, tr => 1,
7074 }->{$token->{tag_name}}) {
7075 !!!cp ('t401');
7076 !!!parse-error (type => 'in body',
7077 text => $token->{tag_name}, token => $token);
7078 ## Ignore the token
7079 !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7080 !!!next-token;
7081 next B;
7082
7083 ## ISSUE: An issue on HTML5 new elements in the spec.
7084 } else {
7085 if ($token->{tag_name} eq 'image') {
7086 !!!cp ('t384');
7087 !!!parse-error (type => 'image', token => $token);
7088 $token->{tag_name} = 'img';
7089 } else {
7090 !!!cp ('t385');
7091 }
7092
7093 ## NOTE: There is an "as if <br>" code clone.
7094 $reconstruct_active_formatting_elements->($insert_to_current);
7095
7096 !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7097
7098 if ({
7099 applet => 1, marquee => 1, object => 1,
7100 }->{$token->{tag_name}}) {
7101 !!!cp ('t380');
7102 push @$active_formatting_elements, ['#marker', ''];
7103 !!!nack ('t380.1');
7104 } elsif ({
7105 b => 1, big => 1, em => 1, font => 1, i => 1,
7106 s => 1, small => 1, strile => 1,
7107 strong => 1, tt => 1, u => 1,
7108 }->{$token->{tag_name}}) {
7109 !!!cp ('t375');
7110 push @$active_formatting_elements, $self->{open_elements}->[-1];
7111 !!!nack ('t375.1');
7112 } elsif ($token->{tag_name} eq 'input') {
7113 !!!cp ('t388');
7114 ## TODO: associate with $self->{form_element} if defined
7115 pop @{$self->{open_elements}};
7116 !!!ack ('t388.2');
7117 } elsif ({
7118 area => 1, basefont => 1, bgsound => 1, br => 1,
7119 embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
7120 #image => 1,
7121 }->{$token->{tag_name}}) {
7122 !!!cp ('t388.1');
7123 pop @{$self->{open_elements}};
7124 !!!ack ('t388.3');
7125 } elsif ($token->{tag_name} eq 'select') {
7126 ## TODO: associate with $self->{form_element} if defined
7127
7128 if ($self->{insertion_mode} & TABLE_IMS or
7129 $self->{insertion_mode} & BODY_TABLE_IMS or
7130 $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
7131 !!!cp ('t400.1');
7132 $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
7133 } else {
7134 !!!cp ('t400.2');
7135 $self->{insertion_mode} = IN_SELECT_IM;
7136 }
7137 !!!nack ('t400.3');
7138 } else {
7139 !!!nack ('t402');
7140 }
7141
7142 !!!next-token;
7143 next B;
7144 }
7145 } elsif ($token->{type} == END_TAG_TOKEN) {
7146 if ($token->{tag_name} eq 'body') {
7147 ## has a |body| element in scope
7148 my $i;
7149 INSCOPE: {
7150 for (reverse @{$self->{open_elements}}) {
7151 if ($_->[1] & BODY_EL) {
7152 !!!cp ('t405');
7153 $i = $_;
7154 last INSCOPE;
7155 } elsif ($_->[1] & SCOPING_EL) {
7156 !!!cp ('t405.1');
7157 last;
7158 }
7159 }
7160
7161 !!!parse-error (type => 'start tag not allowed',
7162 text => $token->{tag_name}, token => $token);
7163 ## NOTE: Ignore the token.
7164 !!!next-token;
7165 next B;
7166 } # INSCOPE
7167
7168 for (@{$self->{open_elements}}) {
7169 unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7170 !!!cp ('t403');
7171 !!!parse-error (type => 'not closed',
7172 text => $_->[0]->manakai_local_name,
7173 token => $token);
7174 last;
7175 } else {
7176 !!!cp ('t404');
7177 }
7178 }
7179
7180 $self->{insertion_mode} = AFTER_BODY_IM;
7181 !!!next-token;
7182 next B;
7183 } elsif ($token->{tag_name} eq 'html') {
7184 ## TODO: Update this code. It seems that the code below is not
7185 ## up-to-date, though it has same effect as speced.
7186 if (@{$self->{open_elements}} > 1 and
7187 $self->{open_elements}->[1]->[1] & BODY_EL) {
7188 ## ISSUE: There is an issue in the spec.
7189 unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7190 !!!cp ('t406');
7191 !!!parse-error (type => 'not closed',
7192 text => $self->{open_elements}->[1]->[0]
7193 ->manakai_local_name,
7194 token => $token);
7195 } else {
7196 !!!cp ('t407');
7197 }
7198 $self->{insertion_mode} = AFTER_BODY_IM;
7199 ## reprocess
7200 next B;
7201 } else {
7202 !!!cp ('t408');
7203 !!!parse-error (type => 'unmatched end tag',
7204 text => $token->{tag_name}, token => $token);
7205 ## Ignore the token
7206 !!!next-token;
7207 next B;
7208 }
7209 } elsif ({
7210 address => 1, blockquote => 1, center => 1, dir => 1,
7211 div => 1, dl => 1, fieldset => 1, listing => 1,
7212 menu => 1, ol => 1, pre => 1, ul => 1,
7213 dd => 1, dt => 1, li => 1,
7214 applet => 1, button => 1, marquee => 1, object => 1,
7215 }->{$token->{tag_name}}) {
7216 ## has an element in scope
7217 my $i;
7218 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7219 my $node = $self->{open_elements}->[$_];
7220 if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7221 !!!cp ('t410');
7222 $i = $_;
7223 last INSCOPE;
7224 } elsif ($node->[1] & SCOPING_EL) {
7225 !!!cp ('t411');
7226 last INSCOPE;
7227 }
7228 } # INSCOPE
7229
7230 unless (defined $i) { # has an element in scope
7231 !!!cp ('t413');
7232 !!!parse-error (type => 'unmatched end tag',
7233 text => $token->{tag_name}, token => $token);
7234 ## NOTE: Ignore the token.
7235 } else {
7236 ## Step 1. generate implied end tags
7237 while ({
7238 ## END_TAG_OPTIONAL_EL
7239 dd => ($token->{tag_name} ne 'dd'),
7240 dt => ($token->{tag_name} ne 'dt'),
7241 li => ($token->{tag_name} ne 'li'),
7242 p => 1,
7243 rt => 1,
7244 rp => 1,
7245 }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7246 !!!cp ('t409');
7247 pop @{$self->{open_elements}};
7248 }
7249
7250 ## Step 2.
7251 if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7252 ne $token->{tag_name}) {
7253 !!!cp ('t412');
7254 !!!parse-error (type => 'not closed',
7255 text => $self->{open_elements}->[-1]->[0]
7256 ->manakai_local_name,
7257 token => $token);
7258 } else {
7259 !!!cp ('t414');
7260 }
7261
7262 ## Step 3.
7263 splice @{$self->{open_elements}}, $i;
7264
7265 ## Step 4.
7266 $clear_up_to_marker->()
7267 if {
7268 applet => 1, button => 1, marquee => 1, object => 1,
7269 }->{$token->{tag_name}};
7270 }
7271 !!!next-token;
7272 next B;
7273 } elsif ($token->{tag_name} eq 'form') {
7274 undef $self->{form_element};
7275
7276 ## has an element in scope
7277 my $i;
7278 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7279 my $node = $self->{open_elements}->[$_];
7280 if ($node->[1] & FORM_EL) {
7281 !!!cp ('t418');
7282 $i = $_;
7283 last INSCOPE;
7284 } elsif ($node->[1] & SCOPING_EL) {
7285 !!!cp ('t419');
7286 last INSCOPE;
7287 }
7288 } # INSCOPE
7289
7290 unless (defined $i) { # has an element in scope
7291 !!!cp ('t421');
7292 !!!parse-error (type => 'unmatched end tag',
7293 text => $token->{tag_name}, token => $token);
7294 ## NOTE: Ignore the token.
7295 } else {
7296 ## Step 1. generate implied end tags
7297 while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7298 !!!cp ('t417');
7299 pop @{$self->{open_elements}};
7300 }
7301
7302 ## Step 2.
7303 if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7304 ne $token->{tag_name}) {
7305 !!!cp ('t417.1');
7306 !!!parse-error (type => 'not closed',
7307 text => $self->{open_elements}->[-1]->[0]
7308 ->manakai_local_name,
7309 token => $token);
7310 } else {
7311 !!!cp ('t420');
7312 }
7313
7314 ## Step 3.
7315 splice @{$self->{open_elements}}, $i;
7316 }
7317
7318 !!!next-token;
7319 next B;
7320 } elsif ({
7321 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7322 }->{$token->{tag_name}}) {
7323 ## has an element in scope
7324 my $i;
7325 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7326 my $node = $self->{open_elements}->[$_];
7327 if ($node->[1] & HEADING_EL) {
7328 !!!cp ('t423');
7329 $i = $_;
7330 last INSCOPE;
7331 } elsif ($node->[1] & SCOPING_EL) {
7332 !!!cp ('t424');
7333 last INSCOPE;
7334 }
7335 } # INSCOPE
7336
7337 unless (defined $i) { # has an element in scope
7338 !!!cp ('t425.1');
7339 !!!parse-error (type => 'unmatched end tag',
7340 text => $token->{tag_name}, token => $token);
7341 ## NOTE: Ignore the token.
7342 } else {
7343 ## Step 1. generate implied end tags
7344 while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7345 !!!cp ('t422');
7346 pop @{$self->{open_elements}};
7347 }
7348
7349 ## Step 2.
7350 if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7351 ne $token->{tag_name}) {
7352 !!!cp ('t425');
7353 !!!parse-error (type => 'unmatched end tag',
7354 text => $token->{tag_name}, token => $token);
7355 } else {
7356 !!!cp ('t426');
7357 }
7358
7359 ## Step 3.
7360 splice @{$self->{open_elements}}, $i;
7361 }
7362
7363 !!!next-token;
7364 next B;
7365 } elsif ($token->{tag_name} eq 'p') {
7366 ## has an element in scope
7367 my $i;
7368 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7369 my $node = $self->{open_elements}->[$_];
7370 if ($node->[1] & P_EL) {
7371 !!!cp ('t410.1');
7372 $i = $_;
7373 last INSCOPE;
7374 } elsif ($node->[1] & SCOPING_EL) {
7375 !!!cp ('t411.1');
7376 last INSCOPE;
7377 }
7378 } # INSCOPE
7379
7380 if (defined $i) {
7381 if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7382 ne $token->{tag_name}) {
7383 !!!cp ('t412.1');
7384 !!!parse-error (type => 'not closed',
7385 text => $self->{open_elements}->[-1]->[0]
7386 ->manakai_local_name,
7387 token => $token);
7388 } else {
7389 !!!cp ('t414.1');
7390 }
7391
7392 splice @{$self->{open_elements}}, $i;
7393 } else {
7394 !!!cp ('t413.1');
7395 !!!parse-error (type => 'unmatched end tag',
7396 text => $token->{tag_name}, token => $token);
7397
7398 !!!cp ('t415.1');
7399 ## As if <p>, then reprocess the current token
7400 my $el;
7401 !!!create-element ($el, $HTML_NS, 'p',, $token);
7402 $insert->($el);
7403 ## NOTE: Not inserted into |$self->{open_elements}|.
7404 }
7405
7406 !!!next-token;
7407 next B;
7408 } elsif ({
7409 a => 1,
7410 b => 1, big => 1, em => 1, font => 1, i => 1,
7411 nobr => 1, s => 1, small => 1, strile => 1,
7412 strong => 1, tt => 1, u => 1,
7413 }->{$token->{tag_name}}) {
7414 !!!cp ('t427');
7415 $formatting_end_tag->($token);
7416 next B;
7417 } elsif ($token->{tag_name} eq 'br') {
7418 !!!cp ('t428');
7419 !!!parse-error (type => 'unmatched end tag',
7420 text => 'br', token => $token);
7421
7422 ## As if <br>
7423 $reconstruct_active_formatting_elements->($insert_to_current);
7424
7425 my $el;
7426 !!!create-element ($el, $HTML_NS, 'br',, $token);
7427 $insert->($el);
7428
7429 ## Ignore the token.
7430 !!!next-token;
7431 next B;
7432 } elsif ({
7433 caption => 1, col => 1, colgroup => 1, frame => 1,
7434 frameset => 1, head => 1, option => 1, optgroup => 1,
7435 tbody => 1, td => 1, tfoot => 1, th => 1,
7436 thead => 1, tr => 1,
7437 area => 1, basefont => 1, bgsound => 1,
7438 embed => 1, hr => 1, iframe => 1, image => 1,
7439 img => 1, input => 1, isindex => 1, noembed => 1,
7440 noframes => 1, param => 1, select => 1, spacer => 1,
7441 table => 1, textarea => 1, wbr => 1,
7442 noscript => 0, ## TODO: if scripting is enabled
7443 }->{$token->{tag_name}}) {
7444 !!!cp ('t429');
7445 !!!parse-error (type => 'unmatched end tag',
7446 text => $token->{tag_name}, token => $token);
7447 ## Ignore the token
7448 !!!next-token;
7449 next B;
7450
7451 ## ISSUE: Issue on HTML5 new elements in spec
7452
7453 } else {
7454 ## Step 1
7455 my $node_i = -1;
7456 my $node = $self->{open_elements}->[$node_i];
7457
7458 ## Step 2
7459 S2: {
7460 if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7461 ## Step 1
7462 ## generate implied end tags
7463 while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7464 !!!cp ('t430');
7465 ## NOTE: |<ruby><rt></ruby>|.
7466 ## ISSUE: <ruby><rt></rt> will also take this code path,
7467 ## which seems wrong.
7468 pop @{$self->{open_elements}};
7469 $node_i++;
7470 }
7471
7472 ## Step 2
7473 if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7474 ne $token->{tag_name}) {
7475 !!!cp ('t431');
7476 ## NOTE: <x><y></x>
7477 !!!parse-error (type => 'not closed',
7478 text => $self->{open_elements}->[-1]->[0]
7479 ->manakai_local_name,
7480 token => $token);
7481 } else {
7482 !!!cp ('t432');
7483 }
7484
7485 ## Step 3
7486 splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7487
7488 !!!next-token;
7489 last S2;
7490 } else {
7491 ## Step 3
7492 if (not ($node->[1] & FORMATTING_EL) and
7493 #not $phrasing_category->{$node->[1]} and
7494 ($node->[1] & SPECIAL_EL or
7495 $node->[1] & SCOPING_EL)) {
7496 !!!cp ('t433');
7497 !!!parse-error (type => 'unmatched end tag',
7498 text => $token->{tag_name}, token => $token);
7499 ## Ignore the token
7500 !!!next-token;
7501 last S2;
7502 }
7503
7504 !!!cp ('t434');
7505 }
7506
7507 ## Step 4
7508 $node_i--;
7509 $node = $self->{open_elements}->[$node_i];
7510
7511 ## Step 5;
7512 redo S2;
7513 } # S2
7514 next B;
7515 }
7516 }
7517 next B;
7518 } continue { # B
7519 if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7520 ## NOTE: The code below is executed in cases where it does not have
7521 ## to be, but it it is harmless even in those cases.
7522 ## has an element in scope
7523 INSCOPE: {
7524 for (reverse 0..$#{$self->{open_elements}}) {
7525 my $node = $self->{open_elements}->[$_];
7526 if ($node->[1] & FOREIGN_EL) {
7527 last INSCOPE;
7528 } elsif ($node->[1] & SCOPING_EL) {
7529 last;
7530 }
7531 }
7532
7533 ## NOTE: No foreign element in scope.
7534 $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7535 } # INSCOPE
7536 }
7537 } # B
7538
7539 ## Stop parsing # MUST
7540
7541 ## TODO: script stuffs
7542 } # _tree_construct_main
7543
7544 sub set_inner_html ($$$;$) {
7545 my $class = shift;
7546 my $node = shift;
7547 my $s = \$_[0];
7548 my $onerror = $_[1];
7549 my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7550
7551 ## ISSUE: Should {confident} be true?
7552
7553 my $nt = $node->node_type;
7554 if ($nt == 9) {
7555 # MUST
7556
7557 ## Step 1 # MUST
7558 ## TODO: If the document has an active parser, ...
7559 ## ISSUE: There is an issue in the spec.
7560
7561 ## Step 2 # MUST
7562 my @cn = @{$node->child_nodes};
7563 for (@cn) {
7564 $node->remove_child ($_);
7565 }
7566
7567 ## Step 3, 4, 5 # MUST
7568 $class->parse_char_string ($$s => $node, $onerror, $get_wrapper);
7569 } elsif ($nt == 1) {
7570 ## TODO: If non-html element
7571
7572 ## NOTE: Most of this code is copied from |parse_string|
7573
7574 ## TODO: Support for $get_wrapper
7575
7576 ## Step 1 # MUST
7577 my $this_doc = $node->owner_document;
7578 my $doc = $this_doc->implementation->create_document;
7579 $doc->manakai_is_html (1);
7580 my $p = $class->new;
7581 $p->{document} = $doc;
7582
7583 ## Step 8 # MUST
7584 my $i = 0;
7585 $p->{line_prev} = $p->{line} = 1;
7586 $p->{column_prev} = $p->{column} = 0;
7587 $p->{set_next_char} = sub {
7588 my $self = shift;
7589
7590 pop @{$self->{prev_char}};
7591 unshift @{$self->{prev_char}}, $self->{next_char};
7592
7593 $self->{next_char} = -1 and return if $i >= length $$s;
7594 $self->{next_char} = ord substr $$s, $i++, 1;
7595
7596 ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7597 $p->{column}++;
7598
7599 if ($self->{next_char} == 0x000A) { # LF
7600 $p->{line}++;
7601 $p->{column} = 0;
7602 !!!cp ('i1');
7603 } elsif ($self->{next_char} == 0x000D) { # CR
7604 $i++ if substr ($$s, $i, 1) eq "\x0A";
7605 $self->{next_char} = 0x000A; # LF # MUST
7606 $p->{line}++;
7607 $p->{column} = 0;
7608 !!!cp ('i2');
7609 } elsif ($self->{next_char} > 0x10FFFF) {
7610 $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7611 !!!cp ('i3');
7612 } elsif ($self->{next_char} == 0x0000) { # NULL
7613 !!!cp ('i4');
7614 !!!parse-error (type => 'NULL');
7615 $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7616 } elsif ($self->{next_char} <= 0x0008 or
7617 (0x000E <= $self->{next_char} and
7618 $self->{next_char} <= 0x001F) or
7619 (0x007F <= $self->{next_char} and
7620 $self->{next_char} <= 0x009F) or
7621 (0xD800 <= $self->{next_char} and
7622 $self->{next_char} <= 0xDFFF) or
7623 (0xFDD0 <= $self->{next_char} and
7624 $self->{next_char} <= 0xFDDF) or
7625 {
7626 0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7627 0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7628 0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7629 0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7630 0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7631 0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7632 0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7633 0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7634 0x10FFFE => 1, 0x10FFFF => 1,
7635 }->{$self->{next_char}}) {
7636 !!!cp ('i4.1');
7637 if ($self->{next_char} < 0x10000) {
7638 !!!parse-error (type => 'control char',
7639 text => (sprintf 'U+%04X', $self->{next_char}));
7640 } else {
7641 !!!parse-error (type => 'control char',
7642 text => (sprintf 'U-%08X', $self->{next_char}));
7643 }
7644 }
7645 };
7646 $p->{prev_char} = [-1, -1, -1];
7647 $p->{next_char} = -1;
7648
7649 my $ponerror = $onerror || sub {
7650 my (%opt) = @_;
7651 my $line = $opt{line};
7652 my $column = $opt{column};
7653 if (defined $opt{token} and defined $opt{token}->{line}) {
7654 $line = $opt{token}->{line};
7655 $column = $opt{token}->{column};
7656 }
7657 warn "Parse error ($opt{type}) at line $line column $column\n";
7658 };
7659 $p->{parse_error} = sub {
7660 $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7661 };
7662
7663 $p->_initialize_tokenizer;
7664 $p->_initialize_tree_constructor;
7665
7666 ## Step 2
7667 my $node_ln = $node->manakai_local_name;
7668 $p->{content_model} = {
7669 title => RCDATA_CONTENT_MODEL,
7670 textarea => RCDATA_CONTENT_MODEL,
7671 style => CDATA_CONTENT_MODEL,
7672 script => CDATA_CONTENT_MODEL,
7673 xmp => CDATA_CONTENT_MODEL,
7674 iframe => CDATA_CONTENT_MODEL,
7675 noembed => CDATA_CONTENT_MODEL,
7676 noframes => CDATA_CONTENT_MODEL,
7677 noscript => CDATA_CONTENT_MODEL,
7678 plaintext => PLAINTEXT_CONTENT_MODEL,
7679 }->{$node_ln};
7680 $p->{content_model} = PCDATA_CONTENT_MODEL
7681 unless defined $p->{content_model};
7682 ## ISSUE: What is "the name of the element"? local name?
7683
7684 $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7685 ## TODO: Foreign element OK?
7686
7687 ## Step 3
7688 my $root = $doc->create_element_ns
7689 ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7690
7691 ## Step 4 # MUST
7692 $doc->append_child ($root);
7693
7694 ## Step 5 # MUST
7695 push @{$p->{open_elements}}, [$root, $el_category->{html}];
7696
7697 undef $p->{head_element};
7698
7699 ## Step 6 # MUST
7700 $p->_reset_insertion_mode;
7701
7702 ## Step 7 # MUST
7703 my $anode = $node;
7704 AN: while (defined $anode) {
7705 if ($anode->node_type == 1) {
7706 my $nsuri = $anode->namespace_uri;
7707 if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
7708 if ($anode->manakai_local_name eq 'form') {
7709 !!!cp ('i5');
7710 $p->{form_element} = $anode;
7711 last AN;
7712 }
7713 }
7714 }
7715 $anode = $anode->parent_node;
7716 } # AN
7717
7718 ## Step 9 # MUST
7719 {
7720 my $self = $p;
7721 !!!next-token;
7722 }
7723 $p->_tree_construction_main;
7724
7725 ## Step 10 # MUST
7726 my @cn = @{$node->child_nodes};
7727 for (@cn) {
7728 $node->remove_child ($_);
7729 }
7730 ## ISSUE: mutation events? read-only?
7731
7732 ## Step 11 # MUST
7733 @cn = @{$root->child_nodes};
7734 for (@cn) {
7735 $this_doc->adopt_node ($_);
7736 $node->append_child ($_);
7737 }
7738 ## ISSUE: mutation events?
7739
7740 $p->_terminate_tree_constructor;
7741
7742 delete $p->{parse_error}; # delete loop
7743 } else {
7744 die "$0: |set_inner_html| is not defined for node of type $nt";
7745 }
7746 } # set_inner_html
7747
7748 } # tree construction stage
7749
7750 package Whatpm::HTML::RestartParser;
7751 push our @ISA, 'Error';
7752
7753 1;
7754 # $Date: 2008/09/13 07:51:33 $

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24