/[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.75 - (hide annotations) (download) (as text)
Mon Mar 3 00:13:22 2008 UTC (16 years, 8 months ago) by wakaba
Branch: MAIN
Changes since 1.74: +29 -28 lines
File MIME type: application/x-wais-source
++ whatpm/t/ChangeLog	3 Mar 2008 00:13:18 -0000
	* HTML-tokenizer.t: s/correct/force-quirks/.

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

++ whatpm/Whatpm/ChangeLog	2 Mar 2008 23:55:07 -0000
	* HTML.pm.src: Flag name changed: s/correct/force-quirks/g (HTML5
	revision 1307).

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

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24