/[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.68 - (hide annotations) (download) (as text)
Sun Feb 17 12:18:06 2008 UTC (18 years, 7 months ago) by wakaba
Branch: MAIN
Changes since 1.67: +22 -2 lines
File MIME type: application/x-wais-source
++ whatpm/t/ChangeLog	17 Feb 2008 12:18:01 -0000
	* content-model-1.dat, content-model-2.dat: Updated.

2008-02-17  Wakaba  <wakaba@suika.fam.cx>

++ whatpm/Whatpm/ChangeLog	17 Feb 2008 12:12:56 -0000
	* ContentChecker.pm ({unsupported_level}): New value.

	* HTML.pm.src: Save whether |meta| |content| attribute
	contains character references or not.

2008-02-17  Wakaba  <wakaba@suika.fam.cx>

++ whatpm/Whatpm/ContentChecker/ChangeLog	17 Feb 2008 12:17:33 -0000
	* HTML.pm: |<meta http-equiv=Content-Type| support (HTML5 revision
	1180).

2008-02-17  Wakaba  <wakaba@suika.fam.cx>

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24