/[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.118 - (hide annotations) (download) (as text)
Thu Mar 20 01:34:00 2008 UTC (16 years, 7 months ago) by wakaba
Branch: MAIN
Changes since 1.117: +58 -32 lines
File MIME type: application/x-wais-source
++ whatpm/Whatpm/ChangeLog	20 Mar 2008 01:33:45 -0000
	* HTML.pm.src: Unused line/column markings are removed.

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

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24