/[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.112 - (show annotations) (download) (as text)
Sun Mar 16 06:39:57 2008 UTC (18 years, 6 months ago) by wakaba
Branch: MAIN
Changes since 1.111: +109 -59 lines
File MIME type: application/x-wais-source
++ whatpm/Whatpm/ChangeLog	16 Mar 2008 06:39:51 -0000
2008-03-16  Wakaba  <wakaba@suika.fam.cx>

	* HTML.pm.src: Preparation for more precious error point
	reporting.

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24