/[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.82 - (hide annotations) (download) (as text)
Wed Mar 5 02:55:08 2008 UTC (16 years, 8 months ago) by wakaba
Branch: MAIN
Changes since 1.81: +6 -3 lines
File MIME type: application/x-wais-source
++ whatpm/t/ChangeLog	5 Mar 2008 02:55:01 -0000
2008-03-05  Wakaba  <wakaba@suika.fam.cx>

	* tree-test-1.dat: More test data for previously uncovered
	cases are added.

++ whatpm/Whatpm/ChangeLog	5 Mar 2008 02:54:27 -0000
2008-03-05  Wakaba  <wakaba@suika.fam.cx>

	* HTML.pm.src: Typo fixed.

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24