/[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.93 - (show annotations) (download) (as text)
Sat Mar 8 03:43:48 2008 UTC (17 years, 10 months ago) by wakaba
Branch: MAIN
Changes since 1.92: +23 -15 lines
File MIME type: application/x-wais-source
++ whatpm/Whatpm/ChangeLog	8 Mar 2008 03:43:41 -0000
	* HTML.pm.src: |</h/n/>| case code rearranged to align with
	the spec (HTML5 revision 1320).  Note that we finally complete
	all of HTML5 revision 1320 changes.

2008-03-08  Wakaba  <wakaba@suika.fam.cx>

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24