/[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.160 - (show annotations) (download) (as text)
Wed Sep 10 10:27:08 2008 UTC (18 years ago) by wakaba
Branch: MAIN
Changes since 1.159: +18 -6 lines
File MIME type: application/x-wais-source
++ whatpm/Whatpm/ChangeLog	10 Sep 2008 10:25:19 -0000
2008-09-10  Wakaba  <wakaba@suika.fam.cx>

	* ContentChecker.pm: Support for charset-layer error levels.

	* HTML.pm.src: Don't specify |text| argument for the
	|chardecode:fallback| error, since it is not the encoding
	being used alternatively.

++ whatpm/Whatpm/Charset/ChangeLog	10 Sep 2008 10:26:52 -0000
2008-09-10  Wakaba  <wakaba@suika.fam.cx>

	* DecodeHandle.pm: Set error levels.

	* WebLatin1.pm: Support for |us-ascii| and |iso-8859-5|
	charsets (this module no longer for Latin1, but for Latin*
	encodings).

	* WebThai.pm: Support for |tis-620| charset.

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24