/[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.101 - (show annotations) (download) (as text)
Sun Mar 9 05:12:05 2008 UTC (18 years, 7 months ago) by wakaba
Branch: MAIN
Changes since 1.100: +42 -16 lines
File MIME type: application/x-wais-source
++ whatpm/t/ChangeLog	9 Mar 2008 05:02:14 -0000
	* tree-test-1.dat: Test data on <input> in <select> and <select>
	in table are added (HTML5 revision 1342).

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

++ whatpm/Whatpm/ChangeLog	9 Mar 2008 05:02:26 -0000
	* HTML.pm.src: Support for |<input>| in the "in select" insertion
	mode and support for the "in select in table" insertion mode (HTML5
	revision 1342).

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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24