/[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.69 - (hide annotations) (download) (as text)
Sun Feb 17 12:39:32 2008 UTC (18 years, 7 months ago) by wakaba
Branch: MAIN
Changes since 1.68: +42 -2 lines
File MIME type: application/x-wais-source
++ whatpm/Whatpm/ChangeLog	17 Feb 2008 12:38:38 -0000
	* HTML.pm.src: |>| in public or system literal closes the DOCTYPE
	token (HTML5 revision 1225).

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

1 wakaba 1.2 package Whatpm::HTML;
2 wakaba 1.1 use strict;
3 wakaba 1.69 our $VERSION=do{my @r=(q$Revision: 1.68 $=~/\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 wakaba 1.69 } elsif ($self->{next_input_character} == 0x003E) { # >
1559     !!!parse-error (type => 'unclosed PUBLIC literal');
1560    
1561     $self->{state} = DATA_STATE;
1562     !!!next-input-character;
1563    
1564     delete $self->{current_token}->{correct};
1565     !!!emit ($self->{current_token}); # DOCTYPE
1566    
1567     redo A;
1568 wakaba 1.18 } elsif ($self->{next_input_character} == -1) {
1569     !!!parse-error (type => 'unclosed PUBLIC literal');
1570    
1571 wakaba 1.57 $self->{state} = DATA_STATE;
1572 wakaba 1.18 ## reconsume
1573    
1574     delete $self->{current_token}->{correct};
1575     !!!emit ($self->{current_token}); # DOCTYPE
1576    
1577     redo A;
1578     } else {
1579     $self->{current_token}->{public_identifier} # DOCTYPE
1580     .= chr $self->{next_input_character};
1581     ## Stay in the state
1582     !!!next-input-character;
1583     redo A;
1584     }
1585 wakaba 1.57 } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
1586 wakaba 1.18 if ($self->{next_input_character} == 0x0027) { # '
1587 wakaba 1.57 $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1588 wakaba 1.18 !!!next-input-character;
1589     redo A;
1590 wakaba 1.69 } elsif ($self->{next_input_character} == 0x003E) { # >
1591     !!!parse-error (type => 'unclosed PUBLIC literal');
1592    
1593     $self->{state} = DATA_STATE;
1594     !!!next-input-character;
1595    
1596     delete $self->{current_token}->{correct};
1597     !!!emit ($self->{current_token}); # DOCTYPE
1598    
1599     redo A;
1600 wakaba 1.18 } elsif ($self->{next_input_character} == -1) {
1601     !!!parse-error (type => 'unclosed PUBLIC literal');
1602    
1603 wakaba 1.57 $self->{state} = DATA_STATE;
1604 wakaba 1.18 ## reconsume
1605    
1606     delete $self->{current_token}->{correct};
1607     !!!emit ($self->{current_token}); # DOCTYPE
1608    
1609     redo A;
1610     } else {
1611     $self->{current_token}->{public_identifier} # DOCTYPE
1612     .= chr $self->{next_input_character};
1613     ## Stay in the state
1614     !!!next-input-character;
1615     redo A;
1616     }
1617 wakaba 1.57 } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1618 wakaba 1.18 if ({
1619     0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1620     #0x000D => 1, # HT, LF, VT, FF, SP, CR
1621     }->{$self->{next_input_character}}) {
1622     ## Stay in the state
1623     !!!next-input-character;
1624     redo A;
1625     } elsif ($self->{next_input_character} == 0x0022) { # "
1626     $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1627 wakaba 1.57 $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1628 wakaba 1.18 !!!next-input-character;
1629     redo A;
1630     } elsif ($self->{next_input_character} == 0x0027) { # '
1631     $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1632 wakaba 1.57 $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1633 wakaba 1.18 !!!next-input-character;
1634     redo A;
1635     } elsif ($self->{next_input_character} == 0x003E) { # >
1636 wakaba 1.57 $self->{state} = DATA_STATE;
1637 wakaba 1.18 !!!next-input-character;
1638    
1639     !!!emit ($self->{current_token}); # DOCTYPE
1640    
1641     redo A;
1642     } elsif ($self->{next_input_character} == -1) {
1643     !!!parse-error (type => 'unclosed DOCTYPE');
1644    
1645 wakaba 1.57 $self->{state} = DATA_STATE;
1646 wakaba 1.26 ## reconsume
1647 wakaba 1.18
1648     delete $self->{current_token}->{correct};
1649     !!!emit ($self->{current_token}); # DOCTYPE
1650    
1651     redo A;
1652     } else {
1653     !!!parse-error (type => 'string after PUBLIC literal');
1654 wakaba 1.57 $self->{state} = BOGUS_DOCTYPE_STATE;
1655 wakaba 1.18 !!!next-input-character;
1656     redo A;
1657     }
1658 wakaba 1.57 } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1659 wakaba 1.18 if ({
1660     0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1661     #0x000D => 1, # HT, LF, VT, FF, SP, CR
1662     }->{$self->{next_input_character}}) {
1663     ## Stay in the state
1664     !!!next-input-character;
1665     redo A;
1666     } elsif ($self->{next_input_character} == 0x0022) { # "
1667     $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1668 wakaba 1.57 $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1669 wakaba 1.18 !!!next-input-character;
1670     redo A;
1671     } elsif ($self->{next_input_character} == 0x0027) { # '
1672     $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1673 wakaba 1.57 $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1674 wakaba 1.18 !!!next-input-character;
1675     redo A;
1676     } elsif ($self->{next_input_character} == 0x003E) { # >
1677     !!!parse-error (type => 'no SYSTEM literal');
1678 wakaba 1.57 $self->{state} = DATA_STATE;
1679 wakaba 1.18 !!!next-input-character;
1680    
1681     delete $self->{current_token}->{correct};
1682     !!!emit ($self->{current_token}); # DOCTYPE
1683    
1684     redo A;
1685     } elsif ($self->{next_input_character} == -1) {
1686     !!!parse-error (type => 'unclosed DOCTYPE');
1687    
1688 wakaba 1.57 $self->{state} = DATA_STATE;
1689 wakaba 1.26 ## reconsume
1690 wakaba 1.18
1691     delete $self->{current_token}->{correct};
1692     !!!emit ($self->{current_token}); # DOCTYPE
1693    
1694     redo A;
1695     } else {
1696 wakaba 1.30 !!!parse-error (type => 'string after SYSTEM');
1697 wakaba 1.57 $self->{state} = BOGUS_DOCTYPE_STATE;
1698 wakaba 1.18 !!!next-input-character;
1699     redo A;
1700     }
1701 wakaba 1.57 } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1702 wakaba 1.18 if ($self->{next_input_character} == 0x0022) { # "
1703 wakaba 1.57 $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1704 wakaba 1.18 !!!next-input-character;
1705     redo A;
1706 wakaba 1.69 } elsif ($self->{next_input_character} == 0x003E) { # >
1707     !!!parse-error (type => 'unclosed PUBLIC literal');
1708    
1709     $self->{state} = DATA_STATE;
1710     !!!next-input-character;
1711    
1712     delete $self->{current_token}->{correct};
1713     !!!emit ($self->{current_token}); # DOCTYPE
1714    
1715     redo A;
1716 wakaba 1.18 } elsif ($self->{next_input_character} == -1) {
1717     !!!parse-error (type => 'unclosed SYSTEM literal');
1718    
1719 wakaba 1.57 $self->{state} = DATA_STATE;
1720 wakaba 1.18 ## reconsume
1721    
1722     delete $self->{current_token}->{correct};
1723     !!!emit ($self->{current_token}); # DOCTYPE
1724    
1725     redo A;
1726     } else {
1727     $self->{current_token}->{system_identifier} # DOCTYPE
1728     .= chr $self->{next_input_character};
1729     ## Stay in the state
1730     !!!next-input-character;
1731     redo A;
1732     }
1733 wakaba 1.57 } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
1734 wakaba 1.18 if ($self->{next_input_character} == 0x0027) { # '
1735 wakaba 1.57 $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1736 wakaba 1.18 !!!next-input-character;
1737     redo A;
1738 wakaba 1.69 } elsif ($self->{next_input_character} == 0x003E) { # >
1739     !!!parse-error (type => 'unclosed PUBLIC literal');
1740    
1741     $self->{state} = DATA_STATE;
1742     !!!next-input-character;
1743    
1744     delete $self->{current_token}->{correct};
1745     !!!emit ($self->{current_token}); # DOCTYPE
1746    
1747     redo A;
1748 wakaba 1.18 } elsif ($self->{next_input_character} == -1) {
1749     !!!parse-error (type => 'unclosed SYSTEM literal');
1750    
1751 wakaba 1.57 $self->{state} = DATA_STATE;
1752 wakaba 1.18 ## reconsume
1753    
1754     delete $self->{current_token}->{correct};
1755 wakaba 1.1 !!!emit ($self->{current_token}); # DOCTYPE
1756    
1757     redo A;
1758     } else {
1759 wakaba 1.18 $self->{current_token}->{system_identifier} # DOCTYPE
1760     .= chr $self->{next_input_character};
1761     ## Stay in the state
1762     !!!next-input-character;
1763     redo A;
1764     }
1765 wakaba 1.57 } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1766 wakaba 1.18 if ({
1767     0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1768     #0x000D => 1, # HT, LF, VT, FF, SP, CR
1769     }->{$self->{next_input_character}}) {
1770     ## Stay in the state
1771     !!!next-input-character;
1772     redo A;
1773     } elsif ($self->{next_input_character} == 0x003E) { # >
1774 wakaba 1.57 $self->{state} = DATA_STATE;
1775 wakaba 1.18 !!!next-input-character;
1776    
1777     !!!emit ($self->{current_token}); # DOCTYPE
1778    
1779     redo A;
1780     } elsif ($self->{next_input_character} == -1) {
1781     !!!parse-error (type => 'unclosed DOCTYPE');
1782    
1783 wakaba 1.57 $self->{state} = DATA_STATE;
1784 wakaba 1.26 ## reconsume
1785 wakaba 1.18
1786     delete $self->{current_token}->{correct};
1787     !!!emit ($self->{current_token}); # DOCTYPE
1788    
1789     redo A;
1790     } else {
1791     !!!parse-error (type => 'string after SYSTEM literal');
1792 wakaba 1.57 $self->{state} = BOGUS_DOCTYPE_STATE;
1793 wakaba 1.1 !!!next-input-character;
1794     redo A;
1795     }
1796 wakaba 1.57 } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
1797 wakaba 1.1 if ($self->{next_input_character} == 0x003E) { # >
1798 wakaba 1.57 $self->{state} = DATA_STATE;
1799 wakaba 1.1 !!!next-input-character;
1800    
1801 wakaba 1.18 delete $self->{current_token}->{correct};
1802 wakaba 1.1 !!!emit ($self->{current_token}); # DOCTYPE
1803    
1804     redo A;
1805     } elsif ($self->{next_input_character} == -1) {
1806 wakaba 1.3 !!!parse-error (type => 'unclosed DOCTYPE');
1807 wakaba 1.57 $self->{state} = DATA_STATE;
1808 wakaba 1.1 ## reconsume
1809    
1810 wakaba 1.18 delete $self->{current_token}->{correct};
1811 wakaba 1.1 !!!emit ($self->{current_token}); # DOCTYPE
1812    
1813     redo A;
1814     } else {
1815     ## Stay in the state
1816     !!!next-input-character;
1817     redo A;
1818     }
1819     } else {
1820     die "$0: $self->{state}: Unknown state";
1821     }
1822     } # A
1823    
1824     die "$0: _get_next_token: unexpected case";
1825     } # _get_next_token
1826    
1827 wakaba 1.26 sub _tokenize_attempt_to_consume_an_entity ($$) {
1828     my ($self, $in_attr) = @_;
1829 wakaba 1.20
1830     if ({
1831     0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
1832     0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
1833     }->{$self->{next_input_character}}) {
1834     ## Don't consume
1835     ## No error
1836     return undef;
1837     } elsif ($self->{next_input_character} == 0x0023) { # #
1838 wakaba 1.1 !!!next-input-character;
1839     if ($self->{next_input_character} == 0x0078 or # x
1840     $self->{next_input_character} == 0x0058) { # X
1841 wakaba 1.26 my $code;
1842 wakaba 1.1 X: {
1843     my $x_char = $self->{next_input_character};
1844     !!!next-input-character;
1845     if (0x0030 <= $self->{next_input_character} and
1846     $self->{next_input_character} <= 0x0039) { # 0..9
1847 wakaba 1.26 $code ||= 0;
1848     $code *= 0x10;
1849     $code += $self->{next_input_character} - 0x0030;
1850 wakaba 1.1 redo X;
1851     } elsif (0x0061 <= $self->{next_input_character} and
1852     $self->{next_input_character} <= 0x0066) { # a..f
1853 wakaba 1.26 $code ||= 0;
1854     $code *= 0x10;
1855     $code += $self->{next_input_character} - 0x0060 + 9;
1856 wakaba 1.1 redo X;
1857     } elsif (0x0041 <= $self->{next_input_character} and
1858     $self->{next_input_character} <= 0x0046) { # A..F
1859 wakaba 1.26 $code ||= 0;
1860     $code *= 0x10;
1861     $code += $self->{next_input_character} - 0x0040 + 9;
1862 wakaba 1.1 redo X;
1863 wakaba 1.26 } elsif (not defined $code) { # no hexadecimal digit
1864 wakaba 1.3 !!!parse-error (type => 'bare hcro');
1865 wakaba 1.37 !!!back-next-input-character ($x_char, $self->{next_input_character});
1866 wakaba 1.1 $self->{next_input_character} = 0x0023; # #
1867     return undef;
1868     } elsif ($self->{next_input_character} == 0x003B) { # ;
1869     !!!next-input-character;
1870     } else {
1871 wakaba 1.3 !!!parse-error (type => 'no refc');
1872 wakaba 1.1 }
1873    
1874 wakaba 1.26 if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1875     !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1876     $code = 0xFFFD;
1877     } elsif ($code > 0x10FFFF) {
1878     !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1879     $code = 0xFFFD;
1880     } elsif ($code == 0x000D) {
1881     !!!parse-error (type => 'CR character reference');
1882     $code = 0x000A;
1883     } elsif (0x80 <= $code and $code <= 0x9F) {
1884 wakaba 1.30 !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
1885 wakaba 1.26 $code = $c1_entity_char->{$code};
1886 wakaba 1.1 }
1887    
1888 wakaba 1.66 return {type => CHARACTER_TOKEN, data => chr $code,
1889     has_reference => 1};
1890 wakaba 1.1 } # X
1891     } elsif (0x0030 <= $self->{next_input_character} and
1892     $self->{next_input_character} <= 0x0039) { # 0..9
1893     my $code = $self->{next_input_character} - 0x0030;
1894     !!!next-input-character;
1895    
1896     while (0x0030 <= $self->{next_input_character} and
1897     $self->{next_input_character} <= 0x0039) { # 0..9
1898     $code *= 10;
1899     $code += $self->{next_input_character} - 0x0030;
1900    
1901     !!!next-input-character;
1902     }
1903    
1904     if ($self->{next_input_character} == 0x003B) { # ;
1905     !!!next-input-character;
1906     } else {
1907 wakaba 1.3 !!!parse-error (type => 'no refc');
1908 wakaba 1.1 }
1909    
1910 wakaba 1.26 if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1911     !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1912     $code = 0xFFFD;
1913     } elsif ($code > 0x10FFFF) {
1914     !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1915     $code = 0xFFFD;
1916     } elsif ($code == 0x000D) {
1917     !!!parse-error (type => 'CR character reference');
1918     $code = 0x000A;
1919 wakaba 1.4 } elsif (0x80 <= $code and $code <= 0x9F) {
1920 wakaba 1.30 !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
1921 wakaba 1.4 $code = $c1_entity_char->{$code};
1922 wakaba 1.1 }
1923    
1924 wakaba 1.66 return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};
1925 wakaba 1.1 } else {
1926 wakaba 1.3 !!!parse-error (type => 'bare nero');
1927 wakaba 1.1 !!!back-next-input-character ($self->{next_input_character});
1928     $self->{next_input_character} = 0x0023; # #
1929     return undef;
1930     }
1931     } elsif ((0x0041 <= $self->{next_input_character} and
1932     $self->{next_input_character} <= 0x005A) or
1933     (0x0061 <= $self->{next_input_character} and
1934     $self->{next_input_character} <= 0x007A)) {
1935     my $entity_name = chr $self->{next_input_character};
1936     !!!next-input-character;
1937    
1938     my $value = $entity_name;
1939 wakaba 1.37 my $match = 0;
1940 wakaba 1.16 require Whatpm::_NamedEntityList;
1941     our $EntityChar;
1942 wakaba 1.1
1943     while (length $entity_name < 10 and
1944     ## NOTE: Some number greater than the maximum length of entity name
1945 wakaba 1.16 ((0x0041 <= $self->{next_input_character} and # a
1946     $self->{next_input_character} <= 0x005A) or # x
1947     (0x0061 <= $self->{next_input_character} and # a
1948     $self->{next_input_character} <= 0x007A) or # z
1949     (0x0030 <= $self->{next_input_character} and # 0
1950     $self->{next_input_character} <= 0x0039) or # 9
1951     $self->{next_input_character} == 0x003B)) { # ;
1952 wakaba 1.1 $entity_name .= chr $self->{next_input_character};
1953 wakaba 1.16 if (defined $EntityChar->{$entity_name}) {
1954     if ($self->{next_input_character} == 0x003B) { # ;
1955 wakaba 1.26 $value = $EntityChar->{$entity_name};
1956 wakaba 1.16 $match = 1;
1957     !!!next-input-character;
1958     last;
1959 wakaba 1.37 } else {
1960 wakaba 1.26 $value = $EntityChar->{$entity_name};
1961     $match = -1;
1962 wakaba 1.37 !!!next-input-character;
1963 wakaba 1.16 }
1964 wakaba 1.1 } else {
1965     $value .= chr $self->{next_input_character};
1966 wakaba 1.37 $match *= 2;
1967     !!!next-input-character;
1968 wakaba 1.1 }
1969     }
1970    
1971 wakaba 1.16 if ($match > 0) {
1972 wakaba 1.66 return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
1973 wakaba 1.16 } elsif ($match < 0) {
1974 wakaba 1.30 !!!parse-error (type => 'no refc');
1975 wakaba 1.37 if ($in_attr and $match < -1) {
1976 wakaba 1.55 return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
1977 wakaba 1.37 } else {
1978 wakaba 1.66 return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
1979 wakaba 1.37 }
1980 wakaba 1.1 } else {
1981 wakaba 1.3 !!!parse-error (type => 'bare ero');
1982 wakaba 1.66 ## NOTE: "No characters are consumed" in the spec.
1983 wakaba 1.55 return {type => CHARACTER_TOKEN, data => '&'.$value};
1984 wakaba 1.1 }
1985     } else {
1986     ## no characters are consumed
1987 wakaba 1.3 !!!parse-error (type => 'bare ero');
1988 wakaba 1.1 return undef;
1989     }
1990     } # _tokenize_attempt_to_consume_an_entity
1991    
1992     sub _initialize_tree_constructor ($) {
1993     my $self = shift;
1994     ## NOTE: $self->{document} MUST be specified before this method is called
1995     $self->{document}->strict_error_checking (0);
1996     ## TODO: Turn mutation events off # MUST
1997     ## TODO: Turn loose Document option (manakai extension) on
1998 wakaba 1.18 $self->{document}->manakai_is_html (1); # MUST
1999 wakaba 1.1 } # _initialize_tree_constructor
2000    
2001     sub _terminate_tree_constructor ($) {
2002     my $self = shift;
2003     $self->{document}->strict_error_checking (1);
2004     ## TODO: Turn mutation events on
2005     } # _terminate_tree_constructor
2006    
2007     ## ISSUE: Should append_child (for example) in script executed in tree construction stage fire mutation events?
2008    
2009 wakaba 1.3 { # tree construction stage
2010     my $token;
2011    
2012 wakaba 1.1 sub _construct_tree ($) {
2013     my ($self) = @_;
2014    
2015     ## When an interactive UA render the $self->{document} available
2016     ## to the user, or when it begin accepting user input, are
2017     ## not defined.
2018    
2019     ## Append a character: collect it and all subsequent consecutive
2020     ## characters and insert one Text node whose data is concatenation
2021     ## of all those characters. # MUST
2022    
2023     !!!next-token;
2024    
2025 wakaba 1.54 $self->{insertion_mode} = BEFORE_HEAD_IM;
2026 wakaba 1.3 undef $self->{form_element};
2027     undef $self->{head_element};
2028     $self->{open_elements} = [];
2029     undef $self->{inner_html_node};
2030    
2031     $self->_tree_construction_initial; # MUST
2032     $self->_tree_construction_root_element;
2033     $self->_tree_construction_main;
2034     } # _construct_tree
2035    
2036     sub _tree_construction_initial ($) {
2037     my $self = shift;
2038 wakaba 1.18 INITIAL: {
2039 wakaba 1.55 if ($token->{type} == DOCTYPE_TOKEN) {
2040 wakaba 1.18 ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
2041     ## error, switch to a conformance checking mode for another
2042     ## language.
2043     my $doctype_name = $token->{name};
2044     $doctype_name = '' unless defined $doctype_name;
2045     $doctype_name =~ tr/a-z/A-Z/;
2046     if (not defined $token->{name} or # <!DOCTYPE>
2047     defined $token->{public_identifier} or
2048     defined $token->{system_identifier}) {
2049     !!!parse-error (type => 'not HTML5');
2050     } elsif ($doctype_name ne 'HTML') {
2051     ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
2052     !!!parse-error (type => 'not HTML5');
2053     }
2054    
2055     my $doctype = $self->{document}->create_document_type_definition
2056     ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
2057     $doctype->public_id ($token->{public_identifier})
2058     if defined $token->{public_identifier};
2059     $doctype->system_id ($token->{system_identifier})
2060     if defined $token->{system_identifier};
2061     ## NOTE: Other DocumentType attributes are null or empty lists.
2062     ## ISSUE: internalSubset = null??
2063     $self->{document}->append_child ($doctype);
2064    
2065     if (not $token->{correct} or $doctype_name ne 'HTML') {
2066     $self->{document}->manakai_compat_mode ('quirks');
2067     } elsif (defined $token->{public_identifier}) {
2068     my $pubid = $token->{public_identifier};
2069     $pubid =~ tr/a-z/A-z/;
2070     if ({
2071     "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,
2072     "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
2073     "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,
2074     "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,
2075     "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,
2076     "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,
2077     "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,
2078     "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,
2079     "-//IETF//DTD HTML 2.0//EN" => 1,
2080     "-//IETF//DTD HTML 2.1E//EN" => 1,
2081     "-//IETF//DTD HTML 3.0//EN" => 1,
2082     "-//IETF//DTD HTML 3.0//EN//" => 1,
2083     "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,
2084     "-//IETF//DTD HTML 3.2//EN" => 1,
2085     "-//IETF//DTD HTML 3//EN" => 1,
2086     "-//IETF//DTD HTML LEVEL 0//EN" => 1,
2087     "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,
2088     "-//IETF//DTD HTML LEVEL 1//EN" => 1,
2089     "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,
2090     "-//IETF//DTD HTML LEVEL 2//EN" => 1,
2091     "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,
2092     "-//IETF//DTD HTML LEVEL 3//EN" => 1,
2093     "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,
2094     "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,
2095     "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,
2096     "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,
2097     "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,
2098     "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,
2099     "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,
2100     "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,
2101     "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,
2102     "-//IETF//DTD HTML STRICT//EN" => 1,
2103     "-//IETF//DTD HTML STRICT//EN//2.0" => 1,
2104     "-//IETF//DTD HTML STRICT//EN//3.0" => 1,
2105     "-//IETF//DTD HTML//EN" => 1,
2106     "-//IETF//DTD HTML//EN//2.0" => 1,
2107     "-//IETF//DTD HTML//EN//3.0" => 1,
2108     "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,
2109     "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,
2110     "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,
2111     "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,
2112     "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,
2113     "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,
2114     "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,
2115     "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,
2116     "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
2117     "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
2118     "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
2119     "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
2120     "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
2121     "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
2122     "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,
2123     "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,
2124     "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,
2125     "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,
2126     "-//W3C//DTD HTML 3.2//EN" => 1,
2127     "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,
2128     "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,
2129     "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,
2130     "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,
2131     "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,
2132     "-//W3C//DTD W3 HTML//EN" => 1,
2133     "-//W3O//DTD W3 HTML 3.0//EN" => 1,
2134     "-//W3O//DTD W3 HTML 3.0//EN//" => 1,
2135     "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,
2136     "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,
2137     "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,
2138     "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
2139     "HTML" => 1,
2140     }->{$pubid}) {
2141     $self->{document}->manakai_compat_mode ('quirks');
2142     } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
2143     $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
2144     if (defined $token->{system_identifier}) {
2145     $self->{document}->manakai_compat_mode ('quirks');
2146     } else {
2147     $self->{document}->manakai_compat_mode ('limited quirks');
2148 wakaba 1.3 }
2149 wakaba 1.18 } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or
2150     $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {
2151     $self->{document}->manakai_compat_mode ('limited quirks');
2152     }
2153     }
2154     if (defined $token->{system_identifier}) {
2155     my $sysid = $token->{system_identifier};
2156     $sysid =~ tr/A-Z/a-z/;
2157     if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
2158     $self->{document}->manakai_compat_mode ('quirks');
2159     }
2160     }
2161    
2162     ## Go to the root element phase.
2163     !!!next-token;
2164     return;
2165     } elsif ({
2166 wakaba 1.55 START_TAG_TOKEN, 1,
2167     END_TAG_TOKEN, 1,
2168     END_OF_FILE_TOKEN, 1,
2169 wakaba 1.18 }->{$token->{type}}) {
2170     !!!parse-error (type => 'no DOCTYPE');
2171     $self->{document}->manakai_compat_mode ('quirks');
2172     ## Go to the root element phase
2173     ## reprocess
2174     return;
2175 wakaba 1.55 } elsif ($token->{type} == CHARACTER_TOKEN) {
2176 wakaba 1.18 if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2177     ## Ignore the token
2178 wakaba 1.26
2179 wakaba 1.18 unless (length $token->{data}) {
2180     ## Stay in the phase
2181     !!!next-token;
2182     redo INITIAL;
2183 wakaba 1.3 }
2184     }
2185 wakaba 1.18
2186     !!!parse-error (type => 'no DOCTYPE');
2187     $self->{document}->manakai_compat_mode ('quirks');
2188     ## Go to the root element phase
2189     ## reprocess
2190     return;
2191 wakaba 1.55 } elsif ($token->{type} == COMMENT_TOKEN) {
2192 wakaba 1.18 my $comment = $self->{document}->create_comment ($token->{data});
2193     $self->{document}->append_child ($comment);
2194    
2195     ## Stay in the phase.
2196     !!!next-token;
2197     redo INITIAL;
2198     } else {
2199 wakaba 1.55 die "$0: $token->{type}: Unknown token type";
2200 wakaba 1.18 }
2201     } # INITIAL
2202 wakaba 1.3 } # _tree_construction_initial
2203    
2204     sub _tree_construction_root_element ($) {
2205     my $self = shift;
2206    
2207     B: {
2208 wakaba 1.55 if ($token->{type} == DOCTYPE_TOKEN) {
2209 wakaba 1.3 !!!parse-error (type => 'in html:#DOCTYPE');
2210     ## Ignore the token
2211     ## Stay in the phase
2212     !!!next-token;
2213     redo B;
2214 wakaba 1.55 } elsif ($token->{type} == COMMENT_TOKEN) {
2215 wakaba 1.3 my $comment = $self->{document}->create_comment ($token->{data});
2216     $self->{document}->append_child ($comment);
2217     ## Stay in the phase
2218     !!!next-token;
2219     redo B;
2220 wakaba 1.55 } elsif ($token->{type} == CHARACTER_TOKEN) {
2221 wakaba 1.26 if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2222     ## Ignore the token.
2223    
2224 wakaba 1.3 unless (length $token->{data}) {
2225     ## Stay in the phase
2226     !!!next-token;
2227     redo B;
2228     }
2229     }
2230 wakaba 1.61
2231     $self->{application_cache_selection}->(undef);
2232    
2233     #
2234     } elsif ($token->{type} == START_TAG_TOKEN) {
2235     if ($token->{tag_name} eq 'html' and
2236 wakaba 1.67 $token->{attributes}->{manifest}) {
2237 wakaba 1.61 $self->{application_cache_selection}
2238     ->($token->{attributes}->{manifest}->{value});
2239     ## ISSUE: No relative reference resolution?
2240     } else {
2241     $self->{application_cache_selection}->(undef);
2242     }
2243    
2244     ## ISSUE: There is an issue in the spec
2245 wakaba 1.3 #
2246     } elsif ({
2247 wakaba 1.55 END_TAG_TOKEN, 1,
2248     END_OF_FILE_TOKEN, 1,
2249 wakaba 1.3 }->{$token->{type}}) {
2250 wakaba 1.61 $self->{application_cache_selection}->(undef);
2251    
2252 wakaba 1.3 ## ISSUE: There is an issue in the spec
2253     #
2254     } else {
2255 wakaba 1.55 die "$0: $token->{type}: Unknown token type";
2256 wakaba 1.3 }
2257 wakaba 1.61
2258 wakaba 1.3 my $root_element; !!!create-element ($root_element, 'html');
2259     $self->{document}->append_child ($root_element);
2260     push @{$self->{open_elements}}, [$root_element, 'html'];
2261     ## reprocess
2262     #redo B;
2263 wakaba 1.35 return; ## Go to the main phase.
2264 wakaba 1.3 } # B
2265     } # _tree_construction_root_element
2266    
2267     sub _reset_insertion_mode ($) {
2268     my $self = shift;
2269    
2270     ## Step 1
2271     my $last;
2272    
2273     ## Step 2
2274     my $i = -1;
2275     my $node = $self->{open_elements}->[$i];
2276    
2277     ## Step 3
2278     S3: {
2279 wakaba 1.29 ## ISSUE: Oops! "If node is the first node in the stack of open
2280     ## elements, then set last to true. If the context element of the
2281     ## HTML fragment parsing algorithm is neither a td element nor a
2282     ## th element, then set node to the context element. (fragment case)":
2283     ## The second "if" is in the scope of the first "if"!?
2284     if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2285     $last = 1;
2286     if (defined $self->{inner_html_node}) {
2287     if ($self->{inner_html_node}->[1] eq 'td' or
2288     $self->{inner_html_node}->[1] eq 'th') {
2289     #
2290     } else {
2291     $node = $self->{inner_html_node};
2292     }
2293 wakaba 1.3 }
2294     }
2295    
2296     ## Step 4..13
2297     my $new_mode = {
2298 wakaba 1.54 select => IN_SELECT_IM,
2299     td => IN_CELL_IM,
2300     th => IN_CELL_IM,
2301     tr => IN_ROW_IM,
2302     tbody => IN_TABLE_BODY_IM,
2303     thead => IN_TABLE_BODY_IM,
2304     tfoot => IN_TABLE_BODY_IM,
2305     caption => IN_CAPTION_IM,
2306     colgroup => IN_COLUMN_GROUP_IM,
2307     table => IN_TABLE_IM,
2308     head => IN_BODY_IM, # not in head!
2309     body => IN_BODY_IM,
2310     frameset => IN_FRAMESET_IM,
2311 wakaba 1.3 }->{$node->[1]};
2312     $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2313    
2314     ## Step 14
2315     if ($node->[1] eq 'html') {
2316     unless (defined $self->{head_element}) {
2317 wakaba 1.54 $self->{insertion_mode} = BEFORE_HEAD_IM;
2318 wakaba 1.3 } else {
2319 wakaba 1.54 $self->{insertion_mode} = AFTER_HEAD_IM;
2320 wakaba 1.3 }
2321     return;
2322     }
2323    
2324     ## Step 15
2325 wakaba 1.54 $self->{insertion_mode} = IN_BODY_IM and return if $last;
2326 wakaba 1.3
2327     ## Step 16
2328     $i--;
2329     $node = $self->{open_elements}->[$i];
2330    
2331     ## Step 17
2332     redo S3;
2333     } # S3
2334     } # _reset_insertion_mode
2335    
2336     sub _tree_construction_main ($) {
2337     my $self = shift;
2338    
2339 wakaba 1.1 my $active_formatting_elements = [];
2340    
2341     my $reconstruct_active_formatting_elements = sub { # MUST
2342     my $insert = shift;
2343    
2344     ## Step 1
2345     return unless @$active_formatting_elements;
2346    
2347     ## Step 3
2348     my $i = -1;
2349     my $entry = $active_formatting_elements->[$i];
2350    
2351     ## Step 2
2352     return if $entry->[0] eq '#marker';
2353 wakaba 1.3 for (@{$self->{open_elements}}) {
2354 wakaba 1.1 if ($entry->[0] eq $_->[0]) {
2355     return;
2356     }
2357     }
2358    
2359     S4: {
2360     ## Step 4
2361     last S4 if $active_formatting_elements->[0]->[0] eq $entry->[0];
2362    
2363     ## Step 5
2364     $i--;
2365     $entry = $active_formatting_elements->[$i];
2366    
2367     ## Step 6
2368     if ($entry->[0] eq '#marker') {
2369     #
2370     } else {
2371     my $in_open_elements;
2372 wakaba 1.3 OE: for (@{$self->{open_elements}}) {
2373 wakaba 1.1 if ($entry->[0] eq $_->[0]) {
2374     $in_open_elements = 1;
2375     last OE;
2376     }
2377     }
2378     if ($in_open_elements) {
2379     #
2380     } else {
2381     redo S4;
2382     }
2383     }
2384    
2385     ## Step 7
2386     $i++;
2387     $entry = $active_formatting_elements->[$i];
2388     } # S4
2389    
2390     S7: {
2391     ## Step 8
2392     my $clone = [$entry->[0]->clone_node (0), $entry->[1]];
2393    
2394     ## Step 9
2395     $insert->($clone->[0]);
2396 wakaba 1.3 push @{$self->{open_elements}}, $clone;
2397 wakaba 1.1
2398     ## Step 10
2399 wakaba 1.3 $active_formatting_elements->[$i] = $self->{open_elements}->[-1];
2400 wakaba 1.1
2401     ## Step 11
2402     unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
2403     ## Step 7'
2404     $i++;
2405     $entry = $active_formatting_elements->[$i];
2406    
2407     redo S7;
2408     }
2409     } # S7
2410     }; # $reconstruct_active_formatting_elements
2411    
2412     my $clear_up_to_marker = sub {
2413     for (reverse 0..$#$active_formatting_elements) {
2414     if ($active_formatting_elements->[$_]->[0] eq '#marker') {
2415     splice @$active_formatting_elements, $_;
2416     return;
2417     }
2418     }
2419     }; # $clear_up_to_marker
2420    
2421 wakaba 1.25 my $parse_rcdata = sub ($$) {
2422     my ($content_model_flag, $insert) = @_;
2423    
2424     ## Step 1
2425     my $start_tag_name = $token->{tag_name};
2426     my $el;
2427     !!!create-element ($el, $start_tag_name, $token->{attributes});
2428    
2429     ## Step 2
2430     $insert->($el); # /context node/->append_child ($el)
2431    
2432     ## Step 3
2433 wakaba 1.40 $self->{content_model} = $content_model_flag; # CDATA or RCDATA
2434 wakaba 1.13 delete $self->{escape}; # MUST
2435 wakaba 1.25
2436     ## Step 4
2437 wakaba 1.1 my $text = '';
2438     !!!next-token;
2439 wakaba 1.55 while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2440 wakaba 1.1 $text .= $token->{data};
2441     !!!next-token;
2442 wakaba 1.25 }
2443    
2444     ## Step 5
2445 wakaba 1.1 if (length $text) {
2446 wakaba 1.25 my $text = $self->{document}->create_text_node ($text);
2447     $el->append_child ($text);
2448 wakaba 1.1 }
2449 wakaba 1.25
2450     ## Step 6
2451 wakaba 1.40 $self->{content_model} = PCDATA_CONTENT_MODEL;
2452 wakaba 1.25
2453     ## Step 7
2454 wakaba 1.55 if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {
2455 wakaba 1.1 ## Ignore the token
2456 wakaba 1.40 } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2457     !!!parse-error (type => 'in CDATA:#'.$token->{type});
2458     } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
2459     !!!parse-error (type => 'in RCDATA:#'.$token->{type});
2460 wakaba 1.1 } else {
2461 wakaba 1.40 die "$0: $content_model_flag in parse_rcdata";
2462 wakaba 1.1 }
2463     !!!next-token;
2464 wakaba 1.25 }; # $parse_rcdata
2465 wakaba 1.1
2466 wakaba 1.25 my $script_start_tag = sub ($) {
2467     my $insert = $_[0];
2468 wakaba 1.1 my $script_el;
2469     !!!create-element ($script_el, 'script', $token->{attributes});
2470     ## TODO: mark as "parser-inserted"
2471    
2472 wakaba 1.40 $self->{content_model} = CDATA_CONTENT_MODEL;
2473 wakaba 1.13 delete $self->{escape}; # MUST
2474 wakaba 1.1
2475     my $text = '';
2476     !!!next-token;
2477 wakaba 1.55 while ($token->{type} == CHARACTER_TOKEN) {
2478 wakaba 1.1 $text .= $token->{data};
2479     !!!next-token;
2480     } # stop if non-character token or tokenizer stops tokenising
2481     if (length $text) {
2482     $script_el->manakai_append_text ($text);
2483     }
2484    
2485 wakaba 1.40 $self->{content_model} = PCDATA_CONTENT_MODEL;
2486 wakaba 1.1
2487 wakaba 1.55 if ($token->{type} == END_TAG_TOKEN and
2488 wakaba 1.1 $token->{tag_name} eq 'script') {
2489     ## Ignore the token
2490     } else {
2491 wakaba 1.3 !!!parse-error (type => 'in CDATA:#'.$token->{type});
2492 wakaba 1.1 ## ISSUE: And ignore?
2493     ## TODO: mark as "already executed"
2494     }
2495    
2496 wakaba 1.3 if (defined $self->{inner_html_node}) {
2497     ## TODO: mark as "already executed"
2498     } else {
2499 wakaba 1.1 ## TODO: $old_insertion_point = current insertion point
2500     ## TODO: insertion point = just before the next input character
2501 wakaba 1.25
2502     $insert->($script_el);
2503 wakaba 1.1
2504     ## TODO: insertion point = $old_insertion_point (might be "undefined")
2505    
2506     ## TODO: if there is a script that will execute as soon as the parser resume, then...
2507     }
2508    
2509     !!!next-token;
2510     }; # $script_start_tag
2511    
2512     my $formatting_end_tag = sub {
2513     my $tag_name = shift;
2514    
2515     FET: {
2516     ## Step 1
2517     my $formatting_element;
2518     my $formatting_element_i_in_active;
2519     AFE: for (reverse 0..$#$active_formatting_elements) {
2520     if ($active_formatting_elements->[$_]->[1] eq $tag_name) {
2521     $formatting_element = $active_formatting_elements->[$_];
2522     $formatting_element_i_in_active = $_;
2523     last AFE;
2524     } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {
2525     last AFE;
2526     }
2527     } # AFE
2528     unless (defined $formatting_element) {
2529 wakaba 1.3 !!!parse-error (type => 'unmatched end tag:'.$tag_name);
2530 wakaba 1.1 ## Ignore the token
2531     !!!next-token;
2532     return;
2533     }
2534     ## has an element in scope
2535     my $in_scope = 1;
2536     my $formatting_element_i_in_open;
2537 wakaba 1.3 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
2538     my $node = $self->{open_elements}->[$_];
2539 wakaba 1.1 if ($node->[0] eq $formatting_element->[0]) {
2540     if ($in_scope) {
2541     $formatting_element_i_in_open = $_;
2542     last INSCOPE;
2543     } else { # in open elements but not in scope
2544 wakaba 1.4 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2545 wakaba 1.1 ## Ignore the token
2546     !!!next-token;
2547     return;
2548     }
2549     } elsif ({
2550     table => 1, caption => 1, td => 1, th => 1,
2551     button => 1, marquee => 1, object => 1, html => 1,
2552     }->{$node->[1]}) {
2553     $in_scope = 0;
2554     }
2555     } # INSCOPE
2556     unless (defined $formatting_element_i_in_open) {
2557 wakaba 1.4 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2558 wakaba 1.1 pop @$active_formatting_elements; # $formatting_element
2559     !!!next-token; ## TODO: ok?
2560     return;
2561     }
2562 wakaba 1.3 if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
2563 wakaba 1.4 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
2564 wakaba 1.1 }
2565    
2566     ## Step 2
2567     my $furthest_block;
2568     my $furthest_block_i_in_open;
2569 wakaba 1.3 OE: for (reverse 0..$#{$self->{open_elements}}) {
2570     my $node = $self->{open_elements}->[$_];
2571 wakaba 1.1 if (not $formatting_category->{$node->[1]} and
2572     #not $phrasing_category->{$node->[1]} and
2573     ($special_category->{$node->[1]} or
2574     $scoping_category->{$node->[1]})) {
2575     $furthest_block = $node;
2576     $furthest_block_i_in_open = $_;
2577     } elsif ($node->[0] eq $formatting_element->[0]) {
2578     last OE;
2579     }
2580     } # OE
2581    
2582     ## Step 3
2583     unless (defined $furthest_block) { # MUST
2584 wakaba 1.3 splice @{$self->{open_elements}}, $formatting_element_i_in_open;
2585 wakaba 1.1 splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
2586     !!!next-token;
2587     return;
2588     }
2589    
2590     ## Step 4
2591 wakaba 1.3 my $common_ancestor_node = $self->{open_elements}->[$formatting_element_i_in_open - 1];
2592 wakaba 1.1
2593     ## Step 5
2594     my $furthest_block_parent = $furthest_block->[0]->parent_node;
2595     if (defined $furthest_block_parent) {
2596     $furthest_block_parent->remove_child ($furthest_block->[0]);
2597     }
2598    
2599     ## Step 6
2600     my $bookmark_prev_el
2601     = $active_formatting_elements->[$formatting_element_i_in_active - 1]
2602     ->[0];
2603    
2604     ## Step 7
2605     my $node = $furthest_block;
2606     my $node_i_in_open = $furthest_block_i_in_open;
2607     my $last_node = $furthest_block;
2608     S7: {
2609     ## Step 1
2610     $node_i_in_open--;
2611 wakaba 1.3 $node = $self->{open_elements}->[$node_i_in_open];
2612 wakaba 1.1
2613     ## Step 2
2614     my $node_i_in_active;
2615     S7S2: {
2616     for (reverse 0..$#$active_formatting_elements) {
2617     if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
2618     $node_i_in_active = $_;
2619     last S7S2;
2620     }
2621     }
2622 wakaba 1.3 splice @{$self->{open_elements}}, $node_i_in_open, 1;
2623 wakaba 1.1 redo S7;
2624     } # S7S2
2625    
2626     ## Step 3
2627     last S7 if $node->[0] eq $formatting_element->[0];
2628    
2629     ## Step 4
2630     if ($last_node->[0] eq $furthest_block->[0]) {
2631     $bookmark_prev_el = $node->[0];
2632     }
2633    
2634     ## Step 5
2635     if ($node->[0]->has_child_nodes ()) {
2636     my $clone = [$node->[0]->clone_node (0), $node->[1]];
2637     $active_formatting_elements->[$node_i_in_active] = $clone;
2638 wakaba 1.3 $self->{open_elements}->[$node_i_in_open] = $clone;
2639 wakaba 1.1 $node = $clone;
2640     }
2641    
2642     ## Step 6
2643     $node->[0]->append_child ($last_node->[0]);
2644    
2645     ## Step 7
2646     $last_node = $node;
2647    
2648     ## Step 8
2649     redo S7;
2650     } # S7
2651    
2652     ## Step 8
2653     $common_ancestor_node->[0]->append_child ($last_node->[0]);
2654    
2655     ## Step 9
2656     my $clone = [$formatting_element->[0]->clone_node (0),
2657     $formatting_element->[1]];
2658    
2659     ## Step 10
2660     my @cn = @{$furthest_block->[0]->child_nodes};
2661     $clone->[0]->append_child ($_) for @cn;
2662    
2663     ## Step 11
2664     $furthest_block->[0]->append_child ($clone->[0]);
2665    
2666     ## Step 12
2667     my $i;
2668     AFE: for (reverse 0..$#$active_formatting_elements) {
2669     if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
2670     splice @$active_formatting_elements, $_, 1;
2671     $i-- and last AFE if defined $i;
2672     } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
2673     $i = $_;
2674     }
2675     } # AFE
2676     splice @$active_formatting_elements, $i + 1, 0, $clone;
2677    
2678     ## Step 13
2679     undef $i;
2680 wakaba 1.3 OE: for (reverse 0..$#{$self->{open_elements}}) {
2681     if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
2682     splice @{$self->{open_elements}}, $_, 1;
2683 wakaba 1.1 $i-- and last OE if defined $i;
2684 wakaba 1.3 } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
2685 wakaba 1.1 $i = $_;
2686     }
2687     } # OE
2688 wakaba 1.3 splice @{$self->{open_elements}}, $i + 1, 1, $clone;
2689 wakaba 1.1
2690     ## Step 14
2691     redo FET;
2692     } # FET
2693     }; # $formatting_end_tag
2694    
2695     my $insert_to_current = sub {
2696 wakaba 1.25 $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
2697 wakaba 1.1 }; # $insert_to_current
2698    
2699     my $insert_to_foster = sub {
2700     my $child = shift;
2701     if ({
2702     table => 1, tbody => 1, tfoot => 1,
2703     thead => 1, tr => 1,
2704 wakaba 1.3 }->{$self->{open_elements}->[-1]->[1]}) {
2705 wakaba 1.1 # MUST
2706     my $foster_parent_element;
2707     my $next_sibling;
2708 wakaba 1.3 OE: for (reverse 0..$#{$self->{open_elements}}) {
2709     if ($self->{open_elements}->[$_]->[1] eq 'table') {
2710     my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
2711 wakaba 1.1 if (defined $parent and $parent->node_type == 1) {
2712     $foster_parent_element = $parent;
2713 wakaba 1.3 $next_sibling = $self->{open_elements}->[$_]->[0];
2714 wakaba 1.1 } else {
2715     $foster_parent_element
2716 wakaba 1.3 = $self->{open_elements}->[$_ - 1]->[0];
2717 wakaba 1.1 }
2718     last OE;
2719     }
2720     } # OE
2721 wakaba 1.3 $foster_parent_element = $self->{open_elements}->[0]->[0]
2722 wakaba 1.1 unless defined $foster_parent_element;
2723     $foster_parent_element->insert_before
2724     ($child, $next_sibling);
2725     } else {
2726 wakaba 1.3 $self->{open_elements}->[-1]->[0]->append_child ($child);
2727 wakaba 1.1 }
2728     }; # $insert_to_foster
2729    
2730 wakaba 1.52 my $insert;
2731 wakaba 1.34
2732 wakaba 1.52 B: {
2733 wakaba 1.55 if ($token->{type} == DOCTYPE_TOKEN) {
2734 wakaba 1.52 !!!parse-error (type => 'DOCTYPE in the middle');
2735     ## Ignore the token
2736     ## Stay in the phase
2737     !!!next-token;
2738     redo B;
2739 wakaba 1.55 } elsif ($token->{type} == END_OF_FILE_TOKEN) {
2740 wakaba 1.56 if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2741 wakaba 1.52 #
2742     } else {
2743     ## Generate implied end tags
2744     if ({
2745     dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,
2746     tbody => 1, tfoot=> 1, thead => 1,
2747     }->{$self->{open_elements}->[-1]->[1]}) {
2748     !!!back-token;
2749 wakaba 1.55 $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};
2750 wakaba 1.52 redo B;
2751     }
2752    
2753     if (@{$self->{open_elements}} > 2 or
2754     (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {
2755     !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
2756     } elsif (defined $self->{inner_html_node} and
2757     @{$self->{open_elements}} > 1 and
2758     $self->{open_elements}->[1]->[1] ne 'body') {
2759     !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
2760 wakaba 1.34 }
2761    
2762 wakaba 1.52 ## ISSUE: There is an issue in the spec.
2763     }
2764    
2765     ## Stop parsing
2766     last B;
2767 wakaba 1.55 } elsif ($token->{type} == START_TAG_TOKEN and
2768 wakaba 1.52 $token->{tag_name} eq 'html') {
2769 wakaba 1.54 if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
2770 wakaba 1.52 ## Turn into the main phase
2771     !!!parse-error (type => 'after html:html');
2772 wakaba 1.54 $self->{insertion_mode} = AFTER_BODY_IM;
2773     } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
2774 wakaba 1.52 ## Turn into the main phase
2775     !!!parse-error (type => 'after html:html');
2776 wakaba 1.54 $self->{insertion_mode} = AFTER_FRAMESET_IM;
2777 wakaba 1.52 }
2778    
2779     ## ISSUE: "aa<html>" is not a parse error.
2780     ## ISSUE: "<html>" in fragment is not a parse error.
2781     unless ($token->{first_start_tag}) {
2782     !!!parse-error (type => 'not first start tag');
2783     }
2784     my $top_el = $self->{open_elements}->[0]->[0];
2785     for my $attr_name (keys %{$token->{attributes}}) {
2786     unless ($top_el->has_attribute_ns (undef, $attr_name)) {
2787     $top_el->set_attribute_ns
2788     (undef, [undef, $attr_name],
2789     $token->{attributes}->{$attr_name}->{value});
2790     }
2791     }
2792     !!!next-token;
2793     redo B;
2794 wakaba 1.55 } elsif ($token->{type} == COMMENT_TOKEN) {
2795 wakaba 1.52 my $comment = $self->{document}->create_comment ($token->{data});
2796 wakaba 1.56 if ($self->{insertion_mode} & AFTER_HTML_IMS) {
2797 wakaba 1.52 $self->{document}->append_child ($comment);
2798 wakaba 1.54 } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
2799 wakaba 1.52 $self->{open_elements}->[0]->[0]->append_child ($comment);
2800     } else {
2801     $self->{open_elements}->[-1]->[0]->append_child ($comment);
2802     }
2803     !!!next-token;
2804     redo B;
2805 wakaba 1.56 } elsif ($self->{insertion_mode} & HEAD_IMS) {
2806 wakaba 1.55 if ($token->{type} == CHARACTER_TOKEN) {
2807 wakaba 1.52 if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
2808     $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
2809     unless (length $token->{data}) {
2810     !!!next-token;
2811     redo B;
2812 wakaba 1.1 }
2813     }
2814 wakaba 1.52
2815 wakaba 1.54 if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2816 wakaba 1.52 ## As if <head>
2817     !!!create-element ($self->{head_element}, 'head');
2818     $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2819     push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2820    
2821     ## Reprocess in the "in head" insertion mode...
2822     pop @{$self->{open_elements}};
2823    
2824     ## Reprocess in the "after head" insertion mode...
2825 wakaba 1.54 } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2826 wakaba 1.52 ## As if </noscript>
2827     pop @{$self->{open_elements}};
2828     !!!parse-error (type => 'in noscript:#character');
2829 wakaba 1.1
2830 wakaba 1.52 ## Reprocess in the "in head" insertion mode...
2831     ## As if </head>
2832     pop @{$self->{open_elements}};
2833    
2834     ## Reprocess in the "after head" insertion mode...
2835 wakaba 1.54 } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2836 wakaba 1.52 pop @{$self->{open_elements}};
2837    
2838     ## Reprocess in the "after head" insertion mode...
2839 wakaba 1.1 }
2840 wakaba 1.52
2841     ## "after head" insertion mode
2842     ## As if <body>
2843     !!!insert-element ('body');
2844 wakaba 1.54 $self->{insertion_mode} = IN_BODY_IM;
2845 wakaba 1.52 ## reprocess
2846     redo B;
2847 wakaba 1.55 } elsif ($token->{type} == START_TAG_TOKEN) {
2848 wakaba 1.52 if ($token->{tag_name} eq 'head') {
2849 wakaba 1.54 if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2850 wakaba 1.52 !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
2851     $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2852     push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
2853 wakaba 1.54 $self->{insertion_mode} = IN_HEAD_IM;
2854 wakaba 1.52 !!!next-token;
2855     redo B;
2856 wakaba 1.54 } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2857     #
2858     } else {
2859 wakaba 1.52 !!!parse-error (type => 'in head:head'); # or in head noscript
2860     ## Ignore the token
2861     !!!next-token;
2862     redo B;
2863     }
2864 wakaba 1.54 } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2865 wakaba 1.52 ## As if <head>
2866     !!!create-element ($self->{head_element}, 'head');
2867     $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2868     push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2869    
2870 wakaba 1.54 $self->{insertion_mode} = IN_HEAD_IM;
2871 wakaba 1.52 ## Reprocess in the "in head" insertion mode...
2872 wakaba 1.1 }
2873 wakaba 1.52
2874 wakaba 1.49 if ($token->{tag_name} eq 'base') {
2875 wakaba 1.54 if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2876 wakaba 1.49 ## As if </noscript>
2877     pop @{$self->{open_elements}};
2878     !!!parse-error (type => 'in noscript:base');
2879    
2880 wakaba 1.54 $self->{insertion_mode} = IN_HEAD_IM;
2881 wakaba 1.49 ## Reprocess in the "in head" insertion mode...
2882     }
2883    
2884     ## NOTE: There is a "as if in head" code clone.
2885 wakaba 1.54 if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2886 wakaba 1.49 !!!parse-error (type => 'after head:'.$token->{tag_name});
2887     push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2888     }
2889     !!!insert-element ($token->{tag_name}, $token->{attributes});
2890     pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2891     pop @{$self->{open_elements}}
2892 wakaba 1.54 if $self->{insertion_mode} == AFTER_HEAD_IM;
2893 wakaba 1.49 !!!next-token;
2894     redo B;
2895     } elsif ($token->{tag_name} eq 'link') {
2896 wakaba 1.25 ## NOTE: There is a "as if in head" code clone.
2897 wakaba 1.54 if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2898 wakaba 1.25 !!!parse-error (type => 'after head:'.$token->{tag_name});
2899     push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2900     }
2901     !!!insert-element ($token->{tag_name}, $token->{attributes});
2902     pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2903     pop @{$self->{open_elements}}
2904 wakaba 1.54 if $self->{insertion_mode} == AFTER_HEAD_IM;
2905 wakaba 1.1 !!!next-token;
2906 wakaba 1.25 redo B;
2907 wakaba 1.34 } elsif ($token->{tag_name} eq 'meta') {
2908     ## NOTE: There is a "as if in head" code clone.
2909 wakaba 1.54 if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2910 wakaba 1.34 !!!parse-error (type => 'after head:'.$token->{tag_name});
2911     push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2912     }
2913     !!!insert-element ($token->{tag_name}, $token->{attributes});
2914 wakaba 1.66 my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2915 wakaba 1.34
2916     unless ($self->{confident}) {
2917     if ($token->{attributes}->{charset}) { ## TODO: And if supported
2918 wakaba 1.63 $self->{change_encoding}
2919     ->($self, $token->{attributes}->{charset}->{value});
2920 wakaba 1.66
2921     $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
2922     ->set_user_data (manakai_has_reference =>
2923     $token->{attributes}->{charset}
2924     ->{has_reference});
2925 wakaba 1.63 } elsif ($token->{attributes}->{content}) {
2926 wakaba 1.35 ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
2927 wakaba 1.63 if ($token->{attributes}->{content}->{value}
2928 wakaba 1.34 =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
2929     [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2930     ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
2931 wakaba 1.63 $self->{change_encoding}
2932     ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
2933 wakaba 1.68 $meta_el->[0]->get_attribute_node_ns (undef, 'content')
2934     ->set_user_data (manakai_has_reference =>
2935     $token->{attributes}->{content}
2936     ->{has_reference});
2937 wakaba 1.63 }
2938 wakaba 1.34 }
2939 wakaba 1.66 } else {
2940     if ($token->{attributes}->{charset}) {
2941     $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
2942     ->set_user_data (manakai_has_reference =>
2943     $token->{attributes}->{charset}
2944     ->{has_reference});
2945     }
2946 wakaba 1.68 if ($token->{attributes}->{content}) {
2947     $meta_el->[0]->get_attribute_node_ns (undef, 'content')
2948     ->set_user_data (manakai_has_reference =>
2949     $token->{attributes}->{content}
2950     ->{has_reference});
2951     }
2952 wakaba 1.34 }
2953    
2954     pop @{$self->{open_elements}}
2955 wakaba 1.54 if $self->{insertion_mode} == AFTER_HEAD_IM;
2956 wakaba 1.34 !!!next-token;
2957     redo B;
2958 wakaba 1.49 } elsif ($token->{tag_name} eq 'title') {
2959 wakaba 1.54 if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2960 wakaba 1.49 ## As if </noscript>
2961     pop @{$self->{open_elements}};
2962     !!!parse-error (type => 'in noscript:title');
2963    
2964 wakaba 1.54 $self->{insertion_mode} = IN_HEAD_IM;
2965 wakaba 1.49 ## Reprocess in the "in head" insertion mode...
2966 wakaba 1.54 } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2967 wakaba 1.25 !!!parse-error (type => 'after head:'.$token->{tag_name});
2968     push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2969     }
2970 wakaba 1.49
2971     ## NOTE: There is a "as if in head" code clone.
2972 wakaba 1.31 my $parent = defined $self->{head_element} ? $self->{head_element}
2973     : $self->{open_elements}->[-1]->[0];
2974 wakaba 1.40 $parse_rcdata->(RCDATA_CONTENT_MODEL,
2975     sub { $parent->append_child ($_[0]) });
2976 wakaba 1.25 pop @{$self->{open_elements}}
2977 wakaba 1.54 if $self->{insertion_mode} == AFTER_HEAD_IM;
2978 wakaba 1.25 redo B;
2979     } elsif ($token->{tag_name} eq 'style') {
2980     ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
2981 wakaba 1.54 ## insertion mode IN_HEAD_IM)
2982 wakaba 1.25 ## NOTE: There is a "as if in head" code clone.
2983 wakaba 1.54 if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2984 wakaba 1.25 !!!parse-error (type => 'after head:'.$token->{tag_name});
2985     push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2986     }
2987 wakaba 1.40 $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
2988 wakaba 1.25 pop @{$self->{open_elements}}
2989 wakaba 1.54 if $self->{insertion_mode} == AFTER_HEAD_IM;
2990 wakaba 1.25 redo B;
2991     } elsif ($token->{tag_name} eq 'noscript') {
2992 wakaba 1.54 if ($self->{insertion_mode} == IN_HEAD_IM) {
2993 wakaba 1.25 ## NOTE: and scripting is disalbed
2994     !!!insert-element ($token->{tag_name}, $token->{attributes});
2995 wakaba 1.54 $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
2996 wakaba 1.1 !!!next-token;
2997 wakaba 1.25 redo B;
2998 wakaba 1.54 } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2999 wakaba 1.30 !!!parse-error (type => 'in noscript:noscript');
3000 wakaba 1.1 ## Ignore the token
3001 wakaba 1.41 !!!next-token;
3002 wakaba 1.25 redo B;
3003 wakaba 1.1 } else {
3004 wakaba 1.25 #
3005 wakaba 1.1 }
3006 wakaba 1.49 } elsif ($token->{tag_name} eq 'script') {
3007 wakaba 1.54 if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3008 wakaba 1.49 ## As if </noscript>
3009     pop @{$self->{open_elements}};
3010     !!!parse-error (type => 'in noscript:script');
3011    
3012 wakaba 1.54 $self->{insertion_mode} = IN_HEAD_IM;
3013 wakaba 1.49 ## Reprocess in the "in head" insertion mode...
3014 wakaba 1.54 } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3015 wakaba 1.25 !!!parse-error (type => 'after head:'.$token->{tag_name});
3016     push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3017     }
3018 wakaba 1.49
3019 wakaba 1.25 ## NOTE: There is a "as if in head" code clone.
3020     $script_start_tag->($insert_to_current);
3021     pop @{$self->{open_elements}}
3022 wakaba 1.54 if $self->{insertion_mode} == AFTER_HEAD_IM;
3023 wakaba 1.1 redo B;
3024 wakaba 1.49 } elsif ($token->{tag_name} eq 'body' or
3025 wakaba 1.25 $token->{tag_name} eq 'frameset') {
3026 wakaba 1.54 if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3027 wakaba 1.49 ## As if </noscript>
3028     pop @{$self->{open_elements}};
3029     !!!parse-error (type => 'in noscript:'.$token->{tag_name});
3030    
3031     ## Reprocess in the "in head" insertion mode...
3032     ## As if </head>
3033     pop @{$self->{open_elements}};
3034    
3035     ## Reprocess in the "after head" insertion mode...
3036 wakaba 1.54 } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3037 wakaba 1.49 pop @{$self->{open_elements}};
3038    
3039     ## Reprocess in the "after head" insertion mode...
3040     }
3041    
3042     ## "after head" insertion mode
3043     !!!insert-element ($token->{tag_name}, $token->{attributes});
3044 wakaba 1.54 if ($token->{tag_name} eq 'body') {
3045     $self->{insertion_mode} = IN_BODY_IM;
3046     } elsif ($token->{tag_name} eq 'frameset') {
3047     $self->{insertion_mode} = IN_FRAMESET_IM;
3048     } else {
3049     die "$0: tag name: $self->{tag_name}";
3050     }
3051 wakaba 1.1 !!!next-token;
3052     redo B;
3053     } else {
3054     #
3055     }
3056 wakaba 1.49
3057 wakaba 1.54 if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3058 wakaba 1.49 ## As if </noscript>
3059     pop @{$self->{open_elements}};
3060     !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3061    
3062     ## Reprocess in the "in head" insertion mode...
3063     ## As if </head>
3064 wakaba 1.25 pop @{$self->{open_elements}};
3065 wakaba 1.49
3066     ## Reprocess in the "after head" insertion mode...
3067 wakaba 1.54 } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3068 wakaba 1.49 ## As if </head>
3069 wakaba 1.25 pop @{$self->{open_elements}};
3070 wakaba 1.49
3071     ## Reprocess in the "after head" insertion mode...
3072     }
3073    
3074     ## "after head" insertion mode
3075     ## As if <body>
3076     !!!insert-element ('body');
3077 wakaba 1.54 $self->{insertion_mode} = IN_BODY_IM;
3078 wakaba 1.49 ## reprocess
3079     redo B;
3080 wakaba 1.55 } elsif ($token->{type} == END_TAG_TOKEN) {
3081 wakaba 1.49 if ($token->{tag_name} eq 'head') {
3082 wakaba 1.54 if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3083 wakaba 1.50 ## As if <head>
3084     !!!create-element ($self->{head_element}, 'head');
3085     $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3086     push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3087    
3088     ## Reprocess in the "in head" insertion mode...
3089     pop @{$self->{open_elements}};
3090 wakaba 1.54 $self->{insertion_mode} = AFTER_HEAD_IM;
3091 wakaba 1.50 !!!next-token;
3092     redo B;
3093 wakaba 1.54 } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3094 wakaba 1.49 ## As if </noscript>
3095     pop @{$self->{open_elements}};
3096     !!!parse-error (type => 'in noscript:script');
3097    
3098     ## Reprocess in the "in head" insertion mode...
3099 wakaba 1.50 pop @{$self->{open_elements}};
3100 wakaba 1.54 $self->{insertion_mode} = AFTER_HEAD_IM;
3101 wakaba 1.50 !!!next-token;
3102     redo B;
3103 wakaba 1.54 } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3104 wakaba 1.49 pop @{$self->{open_elements}};
3105 wakaba 1.54 $self->{insertion_mode} = AFTER_HEAD_IM;
3106 wakaba 1.49 !!!next-token;
3107     redo B;
3108     } else {
3109     #
3110     }
3111     } elsif ($token->{tag_name} eq 'noscript') {
3112 wakaba 1.54 if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3113 wakaba 1.49 pop @{$self->{open_elements}};
3114 wakaba 1.54 $self->{insertion_mode} = IN_HEAD_IM;
3115 wakaba 1.49 !!!next-token;
3116     redo B;
3117 wakaba 1.54 } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3118 wakaba 1.50 !!!parse-error (type => 'unmatched end tag:noscript');
3119     ## Ignore the token ## ISSUE: An issue in the spec.
3120     !!!next-token;
3121     redo B;
3122 wakaba 1.49 } else {
3123     #
3124     }
3125     } elsif ({
3126 wakaba 1.31 body => 1, html => 1,
3127     }->{$token->{tag_name}}) {
3128 wakaba 1.54 if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3129 wakaba 1.50 ## As if <head>
3130     !!!create-element ($self->{head_element}, 'head');
3131     $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3132     push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3133    
3134 wakaba 1.54 $self->{insertion_mode} = IN_HEAD_IM;
3135 wakaba 1.50 ## Reprocess in the "in head" insertion mode...
3136 wakaba 1.54 } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3137 wakaba 1.49 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3138     ## Ignore the token
3139     !!!next-token;
3140     redo B;
3141     }
3142 wakaba 1.50
3143     #
3144 wakaba 1.49 } elsif ({
3145 wakaba 1.31 p => 1, br => 1,
3146     }->{$token->{tag_name}}) {
3147 wakaba 1.54 if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3148 wakaba 1.50 ## As if <head>
3149     !!!create-element ($self->{head_element}, 'head');
3150     $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3151     push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3152    
3153 wakaba 1.54 $self->{insertion_mode} = IN_HEAD_IM;
3154 wakaba 1.50 ## Reprocess in the "in head" insertion mode...
3155     }
3156    
3157 wakaba 1.1 #
3158 wakaba 1.25 } else {
3159 wakaba 1.54 if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3160     #
3161     } else {
3162 wakaba 1.49 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3163     ## Ignore the token
3164     !!!next-token;
3165     redo B;
3166     }
3167     }
3168    
3169 wakaba 1.54 if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3170 wakaba 1.49 ## As if </noscript>
3171     pop @{$self->{open_elements}};
3172     !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3173    
3174     ## Reprocess in the "in head" insertion mode...
3175     ## As if </head>
3176     pop @{$self->{open_elements}};
3177    
3178     ## Reprocess in the "after head" insertion mode...
3179 wakaba 1.54 } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3180 wakaba 1.49 ## As if </head>
3181     pop @{$self->{open_elements}};
3182    
3183     ## Reprocess in the "after head" insertion mode...
3184 wakaba 1.54 } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3185 wakaba 1.50 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3186     ## Ignore the token ## ISSUE: An issue in the spec.
3187     !!!next-token;
3188     redo B;
3189 wakaba 1.1 }
3190    
3191 wakaba 1.49 ## "after head" insertion mode
3192     ## As if <body>
3193 wakaba 1.52 !!!insert-element ('body');
3194 wakaba 1.54 $self->{insertion_mode} = IN_BODY_IM;
3195 wakaba 1.52 ## reprocess
3196     redo B;
3197     } else {
3198     die "$0: $token->{type}: Unknown token type";
3199     }
3200    
3201     ## ISSUE: An issue in the spec.
3202 wakaba 1.56 } elsif ($self->{insertion_mode} & BODY_IMS) {
3203 wakaba 1.55 if ($token->{type} == CHARACTER_TOKEN) {
3204 wakaba 1.52 ## NOTE: There is a code clone of "character in body".
3205     $reconstruct_active_formatting_elements->($insert_to_current);
3206    
3207     $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3208    
3209     !!!next-token;
3210     redo B;
3211 wakaba 1.55 } elsif ($token->{type} == START_TAG_TOKEN) {
3212 wakaba 1.52 if ({
3213     caption => 1, col => 1, colgroup => 1, tbody => 1,
3214     td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3215     }->{$token->{tag_name}}) {
3216 wakaba 1.54 if ($self->{insertion_mode} == IN_CELL_IM) {
3217 wakaba 1.52 ## have an element in table scope
3218     my $tn;
3219     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3220     my $node = $self->{open_elements}->[$_];
3221     if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3222     $tn = $node->[1];
3223     last INSCOPE;
3224     } elsif ({
3225     table => 1, html => 1,
3226     }->{$node->[1]}) {
3227     last INSCOPE;
3228     }
3229     } # INSCOPE
3230     unless (defined $tn) {
3231     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3232     ## Ignore the token
3233     !!!next-token;
3234     redo B;
3235     }
3236    
3237     ## Close the cell
3238     !!!back-token; # <?>
3239 wakaba 1.55 $token = {type => END_TAG_TOKEN, tag_name => $tn};
3240 wakaba 1.52 redo B;
3241 wakaba 1.54 } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3242 wakaba 1.52 !!!parse-error (type => 'not closed:caption');
3243    
3244     ## As if </caption>
3245     ## have a table element in table scope
3246     my $i;
3247     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3248     my $node = $self->{open_elements}->[$_];
3249     if ($node->[1] eq 'caption') {
3250     $i = $_;
3251     last INSCOPE;
3252     } elsif ({
3253     table => 1, html => 1,
3254     }->{$node->[1]}) {
3255     last INSCOPE;
3256     }
3257     } # INSCOPE
3258     unless (defined $i) {
3259     !!!parse-error (type => 'unmatched end tag:caption');
3260     ## Ignore the token
3261     !!!next-token;
3262     redo B;
3263     }
3264    
3265     ## generate implied end tags
3266     if ({
3267     dd => 1, dt => 1, li => 1, p => 1,
3268     td => 1, th => 1, tr => 1,
3269     tbody => 1, tfoot=> 1, thead => 1,
3270     }->{$self->{open_elements}->[-1]->[1]}) {
3271     !!!back-token; # <?>
3272 wakaba 1.55 $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3273 wakaba 1.52 !!!back-token;
3274 wakaba 1.55 $token = {type => END_TAG_TOKEN,
3275 wakaba 1.52 tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3276     redo B;
3277     }
3278    
3279     if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3280     !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3281     }
3282    
3283     splice @{$self->{open_elements}}, $i;
3284    
3285     $clear_up_to_marker->();
3286    
3287 wakaba 1.54 $self->{insertion_mode} = IN_TABLE_IM;
3288 wakaba 1.52
3289     ## reprocess
3290     redo B;
3291     } else {
3292     #
3293     }
3294     } else {
3295     #
3296     }
3297 wakaba 1.55 } elsif ($token->{type} == END_TAG_TOKEN) {
3298 wakaba 1.52 if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3299 wakaba 1.54 if ($self->{insertion_mode} == IN_CELL_IM) {
3300 wakaba 1.43 ## have an element in table scope
3301 wakaba 1.52 my $i;
3302 wakaba 1.43 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3303     my $node = $self->{open_elements}->[$_];
3304 wakaba 1.52 if ($node->[1] eq $token->{tag_name}) {
3305     $i = $_;
3306 wakaba 1.43 last INSCOPE;
3307     } elsif ({
3308     table => 1, html => 1,
3309     }->{$node->[1]}) {
3310     last INSCOPE;
3311     }
3312     } # INSCOPE
3313 wakaba 1.52 unless (defined $i) {
3314 wakaba 1.43 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3315     ## Ignore the token
3316     !!!next-token;
3317     redo B;
3318     }
3319    
3320 wakaba 1.52 ## generate implied end tags
3321     if ({
3322     dd => 1, dt => 1, li => 1, p => 1,
3323     td => ($token->{tag_name} eq 'th'),
3324     th => ($token->{tag_name} eq 'td'),
3325     tr => 1,
3326     tbody => 1, tfoot=> 1, thead => 1,
3327     }->{$self->{open_elements}->[-1]->[1]}) {
3328     !!!back-token;
3329 wakaba 1.55 $token = {type => END_TAG_TOKEN,
3330 wakaba 1.52 tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3331     redo B;
3332     }
3333    
3334     if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3335     !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3336     }
3337    
3338     splice @{$self->{open_elements}}, $i;
3339    
3340     $clear_up_to_marker->();
3341    
3342 wakaba 1.54 $self->{insertion_mode} = IN_ROW_IM;
3343 wakaba 1.52
3344     !!!next-token;
3345 wakaba 1.43 redo B;
3346 wakaba 1.54 } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3347 wakaba 1.52 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3348     ## Ignore the token
3349     !!!next-token;
3350     redo B;
3351     } else {
3352     #
3353     }
3354     } elsif ($token->{tag_name} eq 'caption') {
3355 wakaba 1.54 if ($self->{insertion_mode} == IN_CAPTION_IM) {
3356 wakaba 1.43 ## have a table element in table scope
3357     my $i;
3358     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3359     my $node = $self->{open_elements}->[$_];
3360 wakaba 1.52 if ($node->[1] eq $token->{tag_name}) {
3361 wakaba 1.43 $i = $_;
3362     last INSCOPE;
3363     } elsif ({
3364     table => 1, html => 1,
3365     }->{$node->[1]}) {
3366     last INSCOPE;
3367     }
3368     } # INSCOPE
3369     unless (defined $i) {
3370 wakaba 1.52 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3371 wakaba 1.43 ## Ignore the token
3372     !!!next-token;
3373     redo B;
3374     }
3375    
3376     ## generate implied end tags
3377     if ({
3378     dd => 1, dt => 1, li => 1, p => 1,
3379     td => 1, th => 1, tr => 1,
3380     tbody => 1, tfoot=> 1, thead => 1,
3381     }->{$self->{open_elements}->[-1]->[1]}) {
3382     !!!back-token;
3383 wakaba 1.55 $token = {type => END_TAG_TOKEN,
3384 wakaba 1.43 tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3385     redo B;
3386     }
3387 wakaba 1.52
3388     if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3389     !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3390     }
3391    
3392     splice @{$self->{open_elements}}, $i;
3393    
3394     $clear_up_to_marker->();
3395    
3396 wakaba 1.54 $self->{insertion_mode} = IN_TABLE_IM;
3397 wakaba 1.52
3398     !!!next-token;
3399     redo B;
3400 wakaba 1.54 } elsif ($self->{insertion_mode} == IN_CELL_IM) {
3401 wakaba 1.52 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3402     ## Ignore the token
3403     !!!next-token;
3404     redo B;
3405     } else {
3406     #
3407     }
3408     } elsif ({
3409     table => 1, tbody => 1, tfoot => 1,
3410     thead => 1, tr => 1,
3411     }->{$token->{tag_name}} and
3412 wakaba 1.54 $self->{insertion_mode} == IN_CELL_IM) {
3413 wakaba 1.52 ## have an element in table scope
3414     my $i;
3415     my $tn;
3416     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3417     my $node = $self->{open_elements}->[$_];
3418     if ($node->[1] eq $token->{tag_name}) {
3419     $i = $_;
3420     last INSCOPE;
3421     } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
3422     $tn = $node->[1];
3423     ## NOTE: There is exactly one |td| or |th| element
3424     ## in scope in the stack of open elements by definition.
3425     } elsif ({
3426     table => 1, html => 1,
3427     }->{$node->[1]}) {
3428     last INSCOPE;
3429     }
3430     } # INSCOPE
3431     unless (defined $i) {
3432     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3433     ## Ignore the token
3434     !!!next-token;
3435     redo B;
3436     }
3437    
3438     ## Close the cell
3439     !!!back-token; # </?>
3440 wakaba 1.55 $token = {type => END_TAG_TOKEN, tag_name => $tn};
3441 wakaba 1.52 redo B;
3442     } elsif ($token->{tag_name} eq 'table' and
3443 wakaba 1.54 $self->{insertion_mode} == IN_CAPTION_IM) {
3444 wakaba 1.52 !!!parse-error (type => 'not closed:caption');
3445    
3446     ## As if </caption>
3447     ## have a table element in table scope
3448     my $i;
3449     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3450     my $node = $self->{open_elements}->[$_];
3451     if ($node->[1] eq 'caption') {
3452     $i = $_;
3453     last INSCOPE;
3454     } elsif ({
3455     table => 1, html => 1,
3456     }->{$node->[1]}) {
3457     last INSCOPE;
3458     }
3459     } # INSCOPE
3460     unless (defined $i) {
3461     !!!parse-error (type => 'unmatched end tag:caption');
3462     ## Ignore the token
3463     !!!next-token;
3464     redo B;
3465     }
3466    
3467     ## generate implied end tags
3468     if ({
3469     dd => 1, dt => 1, li => 1, p => 1,
3470     td => 1, th => 1, tr => 1,
3471     tbody => 1, tfoot=> 1, thead => 1,
3472     }->{$self->{open_elements}->[-1]->[1]}) {
3473     !!!back-token; # </table>
3474 wakaba 1.55 $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3475 wakaba 1.52 !!!back-token;
3476 wakaba 1.55 $token = {type => END_TAG_TOKEN,
3477 wakaba 1.52 tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3478     redo B;
3479     }
3480    
3481     if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3482     !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3483     }
3484    
3485     splice @{$self->{open_elements}}, $i;
3486    
3487     $clear_up_to_marker->();
3488    
3489 wakaba 1.54 $self->{insertion_mode} = IN_TABLE_IM;
3490 wakaba 1.52
3491     ## reprocess
3492     redo B;
3493     } elsif ({
3494     body => 1, col => 1, colgroup => 1, html => 1,
3495     }->{$token->{tag_name}}) {
3496 wakaba 1.56 if ($self->{insertion_mode} & BODY_TABLE_IMS) {
3497 wakaba 1.52 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3498     ## Ignore the token
3499     !!!next-token;
3500     redo B;
3501     } else {
3502     #
3503     }
3504     } elsif ({
3505     tbody => 1, tfoot => 1,
3506     thead => 1, tr => 1,
3507     }->{$token->{tag_name}} and
3508 wakaba 1.54 $self->{insertion_mode} == IN_CAPTION_IM) {
3509 wakaba 1.52 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3510     ## Ignore the token
3511     !!!next-token;
3512     redo B;
3513     } else {
3514     #
3515     }
3516     } else {
3517     die "$0: $token->{type}: Unknown token type";
3518     }
3519    
3520     $insert = $insert_to_current;
3521     #
3522 wakaba 1.56 } elsif ($self->{insertion_mode} & TABLE_IMS) {
3523 wakaba 1.58 if ($token->{type} == CHARACTER_TOKEN) {
3524 wakaba 1.52 if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3525     $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3526    
3527     unless (length $token->{data}) {
3528     !!!next-token;
3529     redo B;
3530     }
3531     }
3532    
3533     !!!parse-error (type => 'in table:#character');
3534    
3535     ## As if in body, but insert into foster parent element
3536     ## ISSUE: Spec says that "whenever a node would be inserted
3537     ## into the current node" while characters might not be
3538     ## result in a new Text node.
3539     $reconstruct_active_formatting_elements->($insert_to_foster);
3540    
3541     if ({
3542     table => 1, tbody => 1, tfoot => 1,
3543     thead => 1, tr => 1,
3544     }->{$self->{open_elements}->[-1]->[1]}) {
3545     # MUST
3546     my $foster_parent_element;
3547     my $next_sibling;
3548     my $prev_sibling;
3549     OE: for (reverse 0..$#{$self->{open_elements}}) {
3550     if ($self->{open_elements}->[$_]->[1] eq 'table') {
3551     my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3552     if (defined $parent and $parent->node_type == 1) {
3553     $foster_parent_element = $parent;
3554     $next_sibling = $self->{open_elements}->[$_]->[0];
3555     $prev_sibling = $next_sibling->previous_sibling;
3556     } else {
3557     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
3558     $prev_sibling = $foster_parent_element->last_child;
3559     }
3560     last OE;
3561     }
3562     } # OE
3563     $foster_parent_element = $self->{open_elements}->[0]->[0] and
3564     $prev_sibling = $foster_parent_element->last_child
3565     unless defined $foster_parent_element;
3566     if (defined $prev_sibling and
3567     $prev_sibling->node_type == 3) {
3568     $prev_sibling->manakai_append_text ($token->{data});
3569     } else {
3570     $foster_parent_element->insert_before
3571     ($self->{document}->create_text_node ($token->{data}),
3572     $next_sibling);
3573     }
3574     } else {
3575     $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3576     }
3577    
3578     !!!next-token;
3579     redo B;
3580 wakaba 1.58 } elsif ($token->{type} == START_TAG_TOKEN) {
3581 wakaba 1.52 if ({
3582 wakaba 1.54 tr => ($self->{insertion_mode} != IN_ROW_IM),
3583 wakaba 1.52 th => 1, td => 1,
3584     }->{$token->{tag_name}}) {
3585 wakaba 1.54 if ($self->{insertion_mode} == IN_TABLE_IM) {
3586 wakaba 1.52 ## Clear back to table context
3587     while ($self->{open_elements}->[-1]->[1] ne 'table' and
3588     $self->{open_elements}->[-1]->[1] ne 'html') {
3589 wakaba 1.43 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3590 wakaba 1.52 pop @{$self->{open_elements}};
3591 wakaba 1.43 }
3592    
3593 wakaba 1.52 !!!insert-element ('tbody');
3594 wakaba 1.54 $self->{insertion_mode} = IN_TABLE_BODY_IM;
3595 wakaba 1.52 ## reprocess in the "in table body" insertion mode...
3596     }
3597    
3598 wakaba 1.54 if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3599 wakaba 1.52 unless ($token->{tag_name} eq 'tr') {
3600     !!!parse-error (type => 'missing start tag:tr');
3601     }
3602 wakaba 1.43
3603 wakaba 1.52 ## Clear back to table body context
3604     while (not {
3605     tbody => 1, tfoot => 1, thead => 1, html => 1,
3606     }->{$self->{open_elements}->[-1]->[1]}) {
3607     !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3608     pop @{$self->{open_elements}};
3609     }
3610 wakaba 1.43
3611 wakaba 1.54 $self->{insertion_mode} = IN_ROW_IM;
3612 wakaba 1.52 if ($token->{tag_name} eq 'tr') {
3613     !!!insert-element ($token->{tag_name}, $token->{attributes});
3614     !!!next-token;
3615     redo B;
3616     } else {
3617     !!!insert-element ('tr');
3618     ## reprocess in the "in row" insertion mode
3619     }
3620     }
3621    
3622     ## Clear back to table row context
3623     while (not {
3624     tr => 1, html => 1,
3625     }->{$self->{open_elements}->[-1]->[1]}) {
3626     !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3627     pop @{$self->{open_elements}};
3628 wakaba 1.43 }
3629 wakaba 1.52
3630     !!!insert-element ($token->{tag_name}, $token->{attributes});
3631 wakaba 1.54 $self->{insertion_mode} = IN_CELL_IM;
3632 wakaba 1.52
3633     push @$active_formatting_elements, ['#marker', ''];
3634    
3635     !!!next-token;
3636     redo B;
3637     } elsif ({
3638     caption => 1, col => 1, colgroup => 1,
3639     tbody => 1, tfoot => 1, thead => 1,
3640 wakaba 1.54 tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3641 wakaba 1.52 }->{$token->{tag_name}}) {
3642 wakaba 1.54 if ($self->{insertion_mode} == IN_ROW_IM) {
3643 wakaba 1.52 ## As if </tr>
3644 wakaba 1.43 ## have an element in table scope
3645     my $i;
3646     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3647     my $node = $self->{open_elements}->[$_];
3648 wakaba 1.52 if ($node->[1] eq 'tr') {
3649 wakaba 1.43 $i = $_;
3650     last INSCOPE;
3651     } elsif ({
3652     table => 1, html => 1,
3653     }->{$node->[1]}) {
3654     last INSCOPE;
3655     }
3656     } # INSCOPE
3657 wakaba 1.52 unless (defined $i) {
3658     !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
3659     ## Ignore the token
3660     !!!next-token;
3661 wakaba 1.43 redo B;
3662     }
3663    
3664 wakaba 1.52 ## Clear back to table row context
3665     while (not {
3666     tr => 1, html => 1,
3667     }->{$self->{open_elements}->[-1]->[1]}) {
3668 wakaba 1.43 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3669 wakaba 1.52 pop @{$self->{open_elements}};
3670 wakaba 1.1 }
3671 wakaba 1.43
3672 wakaba 1.52 pop @{$self->{open_elements}}; # tr
3673 wakaba 1.54 $self->{insertion_mode} = IN_TABLE_BODY_IM;
3674 wakaba 1.52 if ($token->{tag_name} eq 'tr') {
3675     ## reprocess
3676     redo B;
3677     } else {
3678     ## reprocess in the "in table body" insertion mode...
3679     }
3680 wakaba 1.1 }
3681 wakaba 1.52
3682 wakaba 1.54 if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3683 wakaba 1.52 ## have an element in table scope
3684 wakaba 1.43 my $i;
3685     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3686     my $node = $self->{open_elements}->[$_];
3687 wakaba 1.52 if ({
3688     tbody => 1, thead => 1, tfoot => 1,
3689     }->{$node->[1]}) {
3690 wakaba 1.43 $i = $_;
3691     last INSCOPE;
3692     } elsif ({
3693     table => 1, html => 1,
3694     }->{$node->[1]}) {
3695     last INSCOPE;
3696     }
3697     } # INSCOPE
3698 wakaba 1.52 unless (defined $i) {
3699     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3700     ## Ignore the token
3701     !!!next-token;
3702 wakaba 1.43 redo B;
3703     }
3704 wakaba 1.52
3705     ## Clear back to table body context
3706     while (not {
3707     tbody => 1, tfoot => 1, thead => 1, html => 1,
3708     }->{$self->{open_elements}->[-1]->[1]}) {
3709 wakaba 1.43 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3710 wakaba 1.52 pop @{$self->{open_elements}};
3711 wakaba 1.43 }
3712    
3713 wakaba 1.52 ## As if <{current node}>
3714     ## have an element in table scope
3715     ## true by definition
3716 wakaba 1.43
3717 wakaba 1.52 ## Clear back to table body context
3718     ## nop by definition
3719 wakaba 1.43
3720 wakaba 1.52 pop @{$self->{open_elements}};
3721 wakaba 1.54 $self->{insertion_mode} = IN_TABLE_IM;
3722 wakaba 1.52 ## reprocess in "in table" insertion mode...
3723     }
3724    
3725     if ($token->{tag_name} eq 'col') {
3726     ## Clear back to table context
3727     while ($self->{open_elements}->[-1]->[1] ne 'table' and
3728     $self->{open_elements}->[-1]->[1] ne 'html') {
3729     !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3730     pop @{$self->{open_elements}};
3731     }
3732 wakaba 1.43
3733 wakaba 1.52 !!!insert-element ('colgroup');
3734 wakaba 1.54 $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
3735 wakaba 1.52 ## reprocess
3736 wakaba 1.43 redo B;
3737 wakaba 1.52 } elsif ({
3738     caption => 1,
3739     colgroup => 1,
3740     tbody => 1, tfoot => 1, thead => 1,
3741     }->{$token->{tag_name}}) {
3742     ## Clear back to table context
3743     while ($self->{open_elements}->[-1]->[1] ne 'table' and
3744     $self->{open_elements}->[-1]->[1] ne 'html') {
3745     !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3746     pop @{$self->{open_elements}};
3747 wakaba 1.1 }
3748 wakaba 1.52
3749     push @$active_formatting_elements, ['#marker', '']
3750     if $token->{tag_name} eq 'caption';
3751    
3752     !!!insert-element ($token->{tag_name}, $token->{attributes});
3753     $self->{insertion_mode} = {
3754 wakaba 1.54 caption => IN_CAPTION_IM,
3755     colgroup => IN_COLUMN_GROUP_IM,
3756     tbody => IN_TABLE_BODY_IM,
3757     tfoot => IN_TABLE_BODY_IM,
3758     thead => IN_TABLE_BODY_IM,
3759 wakaba 1.52 }->{$token->{tag_name}};
3760 wakaba 1.1 !!!next-token;
3761     redo B;
3762 wakaba 1.52 } else {
3763     die "$0: in table: <>: $token->{tag_name}";
3764 wakaba 1.1 }
3765 wakaba 1.52 } elsif ($token->{tag_name} eq 'table') {
3766     !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3767 wakaba 1.1
3768 wakaba 1.52 ## As if </table>
3769 wakaba 1.1 ## have a table element in table scope
3770     my $i;
3771 wakaba 1.3 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3772     my $node = $self->{open_elements}->[$_];
3773 wakaba 1.52 if ($node->[1] eq 'table') {
3774 wakaba 1.1 $i = $_;
3775     last INSCOPE;
3776     } elsif ({
3777     table => 1, html => 1,
3778     }->{$node->[1]}) {
3779     last INSCOPE;
3780     }
3781     } # INSCOPE
3782     unless (defined $i) {
3783 wakaba 1.52 !!!parse-error (type => 'unmatched end tag:table');
3784     ## Ignore tokens </table><table>
3785 wakaba 1.1 !!!next-token;
3786     redo B;
3787     }
3788    
3789     ## generate implied end tags
3790     if ({
3791     dd => 1, dt => 1, li => 1, p => 1,
3792     td => 1, th => 1, tr => 1,
3793 wakaba 1.31 tbody => 1, tfoot=> 1, thead => 1,
3794 wakaba 1.3 }->{$self->{open_elements}->[-1]->[1]}) {
3795 wakaba 1.52 !!!back-token; # <table>
3796 wakaba 1.55 $token = {type => END_TAG_TOKEN, tag_name => 'table'};
3797 wakaba 1.1 !!!back-token;
3798 wakaba 1.55 $token = {type => END_TAG_TOKEN,
3799 wakaba 1.3 tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3800 wakaba 1.1 redo B;
3801     }
3802    
3803 wakaba 1.52 if ($self->{open_elements}->[-1]->[1] ne 'table') {
3804 wakaba 1.3 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3805 wakaba 1.1 }
3806    
3807 wakaba 1.3 splice @{$self->{open_elements}}, $i;
3808 wakaba 1.1
3809 wakaba 1.52 $self->_reset_insertion_mode;
3810 wakaba 1.1
3811     ## reprocess
3812     redo B;
3813 wakaba 1.58 } else {
3814     !!!parse-error (type => 'in table:'.$token->{tag_name});
3815    
3816     $insert = $insert_to_foster;
3817     #
3818     }
3819     } elsif ($token->{type} == END_TAG_TOKEN) {
3820 wakaba 1.52 if ($token->{tag_name} eq 'tr' and
3821 wakaba 1.54 $self->{insertion_mode} == IN_ROW_IM) {
3822 wakaba 1.52 ## have an element in table scope
3823     my $i;
3824     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3825     my $node = $self->{open_elements}->[$_];
3826     if ($node->[1] eq $token->{tag_name}) {
3827     $i = $_;
3828     last INSCOPE;
3829     } elsif ({
3830     table => 1, html => 1,
3831     }->{$node->[1]}) {
3832     last INSCOPE;
3833     }
3834     } # INSCOPE
3835     unless (defined $i) {
3836     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3837     ## Ignore the token
3838 wakaba 1.42 !!!next-token;
3839     redo B;
3840     }
3841    
3842 wakaba 1.52 ## Clear back to table row context
3843     while (not {
3844     tr => 1, html => 1,
3845     }->{$self->{open_elements}->[-1]->[1]}) {
3846     !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3847     pop @{$self->{open_elements}};
3848     }
3849 wakaba 1.42
3850 wakaba 1.52 pop @{$self->{open_elements}}; # tr
3851 wakaba 1.54 $self->{insertion_mode} = IN_TABLE_BODY_IM;
3852 wakaba 1.52 !!!next-token;
3853     redo B;
3854     } elsif ($token->{tag_name} eq 'table') {
3855 wakaba 1.54 if ($self->{insertion_mode} == IN_ROW_IM) {
3856 wakaba 1.52 ## As if </tr>
3857     ## have an element in table scope
3858     my $i;
3859     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3860     my $node = $self->{open_elements}->[$_];
3861     if ($node->[1] eq 'tr') {
3862     $i = $_;
3863     last INSCOPE;
3864     } elsif ({
3865     table => 1, html => 1,
3866     }->{$node->[1]}) {
3867     last INSCOPE;
3868 wakaba 1.42 }
3869 wakaba 1.52 } # INSCOPE
3870     unless (defined $i) {
3871     !!!parse-error (type => 'unmatched end tag:'.$token->{type});
3872     ## Ignore the token
3873     !!!next-token;
3874     redo B;
3875 wakaba 1.42 }
3876 wakaba 1.52
3877     ## Clear back to table row context
3878     while (not {
3879     tr => 1, html => 1,
3880     }->{$self->{open_elements}->[-1]->[1]}) {
3881 wakaba 1.46 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3882     pop @{$self->{open_elements}};
3883 wakaba 1.1 }
3884 wakaba 1.46
3885 wakaba 1.52 pop @{$self->{open_elements}}; # tr
3886 wakaba 1.54 $self->{insertion_mode} = IN_TABLE_BODY_IM;
3887 wakaba 1.46 ## reprocess in the "in table body" insertion mode...
3888 wakaba 1.1 }
3889    
3890 wakaba 1.54 if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3891 wakaba 1.52 ## have an element in table scope
3892     my $i;
3893     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3894     my $node = $self->{open_elements}->[$_];
3895     if ({
3896     tbody => 1, thead => 1, tfoot => 1,
3897     }->{$node->[1]}) {
3898     $i = $_;
3899     last INSCOPE;
3900     } elsif ({
3901     table => 1, html => 1,
3902     }->{$node->[1]}) {
3903     last INSCOPE;
3904     }
3905     } # 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.47 }
3912    
3913     ## Clear back to table body context
3914     while (not {
3915     tbody => 1, tfoot => 1, thead => 1, html => 1,
3916     }->{$self->{open_elements}->[-1]->[1]}) {
3917     !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3918     pop @{$self->{open_elements}};
3919     }
3920    
3921 wakaba 1.52 ## As if <{current node}>
3922     ## have an element in table scope
3923     ## true by definition
3924    
3925     ## Clear back to table body context
3926     ## nop by definition
3927    
3928     pop @{$self->{open_elements}};
3929 wakaba 1.54 $self->{insertion_mode} = IN_TABLE_IM;
3930 wakaba 1.52 ## reprocess in the "in table" insertion mode...
3931     }
3932    
3933     ## have a table element in table scope
3934     my $i;
3935     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3936     my $node = $self->{open_elements}->[$_];
3937     if ($node->[1] eq $token->{tag_name}) {
3938     $i = $_;
3939     last INSCOPE;
3940     } elsif ({
3941     table => 1, html => 1,
3942     }->{$node->[1]}) {
3943     last INSCOPE;
3944 wakaba 1.47 }
3945 wakaba 1.52 } # INSCOPE
3946     unless (defined $i) {
3947     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3948     ## Ignore the token
3949     !!!next-token;
3950     redo B;
3951 wakaba 1.3 }
3952    
3953 wakaba 1.52 ## generate implied end tags
3954     if ({
3955     dd => 1, dt => 1, li => 1, p => 1,
3956     td => 1, th => 1, tr => 1,
3957     tbody => 1, tfoot=> 1, thead => 1,
3958     }->{$self->{open_elements}->[-1]->[1]}) {
3959     !!!back-token;
3960 wakaba 1.55 $token = {type => END_TAG_TOKEN,
3961 wakaba 1.52 tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3962     redo B;
3963     }
3964    
3965     if ($self->{open_elements}->[-1]->[1] ne 'table') {
3966 wakaba 1.3 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3967 wakaba 1.1 }
3968 wakaba 1.52
3969     splice @{$self->{open_elements}}, $i;
3970 wakaba 1.1
3971 wakaba 1.52 $self->_reset_insertion_mode;
3972 wakaba 1.47
3973     !!!next-token;
3974     redo B;
3975     } elsif ({
3976 wakaba 1.48 tbody => 1, tfoot => 1, thead => 1,
3977 wakaba 1.52 }->{$token->{tag_name}} and
3978 wakaba 1.56 $self->{insertion_mode} & ROW_IMS) {
3979 wakaba 1.54 if ($self->{insertion_mode} == IN_ROW_IM) {
3980 wakaba 1.52 ## have an element in table scope
3981     my $i;
3982     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3983     my $node = $self->{open_elements}->[$_];
3984     if ($node->[1] eq $token->{tag_name}) {
3985     $i = $_;
3986     last INSCOPE;
3987     } elsif ({
3988     table => 1, html => 1,
3989     }->{$node->[1]}) {
3990     last INSCOPE;
3991     }
3992     } # INSCOPE
3993     unless (defined $i) {
3994     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3995     ## Ignore the token
3996     !!!next-token;
3997     redo B;
3998     }
3999    
4000 wakaba 1.48 ## As if </tr>
4001     ## have an element in table scope
4002     my $i;
4003     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4004     my $node = $self->{open_elements}->[$_];
4005     if ($node->[1] eq 'tr') {
4006     $i = $_;
4007     last INSCOPE;
4008     } elsif ({
4009     table => 1, html => 1,
4010     }->{$node->[1]}) {
4011     last INSCOPE;
4012     }
4013     } # INSCOPE
4014 wakaba 1.52 unless (defined $i) {
4015     !!!parse-error (type => 'unmatched end tag:tr');
4016     ## Ignore the token
4017     !!!next-token;
4018     redo B;
4019     }
4020 wakaba 1.48
4021     ## Clear back to table row context
4022     while (not {
4023     tr => 1, html => 1,
4024     }->{$self->{open_elements}->[-1]->[1]}) {
4025     !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4026     pop @{$self->{open_elements}};
4027     }
4028    
4029     pop @{$self->{open_elements}}; # tr
4030 wakaba 1.54 $self->{insertion_mode} = IN_TABLE_BODY_IM;
4031 wakaba 1.52 ## reprocess in the "in table body" insertion mode...
4032     }
4033    
4034     ## have an element in table scope
4035     my $i;
4036     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4037     my $node = $self->{open_elements}->[$_];
4038     if ($node->[1] eq $token->{tag_name}) {
4039     $i = $_;
4040     last INSCOPE;
4041     } elsif ({
4042     table => 1, html => 1,
4043     }->{$node->[1]}) {
4044     last INSCOPE;
4045     }
4046     } # INSCOPE
4047     unless (defined $i) {
4048     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4049     ## Ignore the token
4050     !!!next-token;
4051     redo B;
4052     }
4053    
4054     ## Clear back to table body context
4055     while (not {
4056     tbody => 1, tfoot => 1, thead => 1, html => 1,
4057     }->{$self->{open_elements}->[-1]->[1]}) {
4058     !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4059     pop @{$self->{open_elements}};
4060     }
4061    
4062     pop @{$self->{open_elements}};
4063 wakaba 1.54 $self->{insertion_mode} = IN_TABLE_IM;
4064 wakaba 1.52 !!!next-token;
4065     redo B;
4066     } elsif ({
4067     body => 1, caption => 1, col => 1, colgroup => 1,
4068     html => 1, td => 1, th => 1,
4069 wakaba 1.54 tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4070     tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
4071 wakaba 1.52 }->{$token->{tag_name}}) {
4072     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4073     ## Ignore the token
4074     !!!next-token;
4075     redo B;
4076 wakaba 1.58 } else {
4077     !!!parse-error (type => 'in table:/'.$token->{tag_name});
4078 wakaba 1.52
4079 wakaba 1.58 $insert = $insert_to_foster;
4080     #
4081     }
4082     } else {
4083     die "$0: $token->{type}: Unknown token type";
4084     }
4085 wakaba 1.54 } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
4086 wakaba 1.55 if ($token->{type} == CHARACTER_TOKEN) {
4087 wakaba 1.52 if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4088     $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4089     unless (length $token->{data}) {
4090     !!!next-token;
4091     redo B;
4092     }
4093     }
4094    
4095     #
4096 wakaba 1.55 } elsif ($token->{type} == START_TAG_TOKEN) {
4097 wakaba 1.52 if ($token->{tag_name} eq 'col') {
4098     !!!insert-element ($token->{tag_name}, $token->{attributes});
4099     pop @{$self->{open_elements}};
4100     !!!next-token;
4101     redo B;
4102     } else {
4103     #
4104     }
4105 wakaba 1.55 } elsif ($token->{type} == END_TAG_TOKEN) {
4106 wakaba 1.52 if ($token->{tag_name} eq 'colgroup') {
4107     if ($self->{open_elements}->[-1]->[1] eq 'html') {
4108     !!!parse-error (type => 'unmatched end tag:colgroup');
4109     ## Ignore the token
4110     !!!next-token;
4111     redo B;
4112     } else {
4113     pop @{$self->{open_elements}}; # colgroup
4114 wakaba 1.54 $self->{insertion_mode} = IN_TABLE_IM;
4115 wakaba 1.52 !!!next-token;
4116     redo B;
4117     }
4118     } elsif ($token->{tag_name} eq 'col') {
4119     !!!parse-error (type => 'unmatched end tag:col');
4120     ## Ignore the token
4121     !!!next-token;
4122     redo B;
4123     } else {
4124     #
4125     }
4126     } else {
4127     #
4128     }
4129    
4130     ## As if </colgroup>
4131     if ($self->{open_elements}->[-1]->[1] eq 'html') {
4132     !!!parse-error (type => 'unmatched end tag:colgroup');
4133     ## Ignore the token
4134     !!!next-token;
4135     redo B;
4136     } else {
4137     pop @{$self->{open_elements}}; # colgroup
4138 wakaba 1.54 $self->{insertion_mode} = IN_TABLE_IM;
4139 wakaba 1.52 ## reprocess
4140     redo B;
4141     }
4142 wakaba 1.54 } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
4143 wakaba 1.58 if ($token->{type} == CHARACTER_TOKEN) {
4144     $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4145     !!!next-token;
4146     redo B;
4147     } elsif ($token->{type} == START_TAG_TOKEN) {
4148 wakaba 1.52 if ($token->{tag_name} eq 'option') {
4149     if ($self->{open_elements}->[-1]->[1] eq 'option') {
4150     ## As if </option>
4151     pop @{$self->{open_elements}};
4152     }
4153    
4154     !!!insert-element ($token->{tag_name}, $token->{attributes});
4155     !!!next-token;
4156     redo B;
4157     } elsif ($token->{tag_name} eq 'optgroup') {
4158     if ($self->{open_elements}->[-1]->[1] eq 'option') {
4159     ## As if </option>
4160     pop @{$self->{open_elements}};
4161     }
4162    
4163     if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
4164     ## As if </optgroup>
4165     pop @{$self->{open_elements}};
4166     }
4167    
4168     !!!insert-element ($token->{tag_name}, $token->{attributes});
4169     !!!next-token;
4170     redo B;
4171     } elsif ($token->{tag_name} eq 'select') {
4172     !!!parse-error (type => 'not closed:select');
4173     ## As if </select> instead
4174     ## have an element in table scope
4175     my $i;
4176     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4177     my $node = $self->{open_elements}->[$_];
4178     if ($node->[1] eq $token->{tag_name}) {
4179     $i = $_;
4180     last INSCOPE;
4181     } elsif ({
4182     table => 1, html => 1,
4183     }->{$node->[1]}) {
4184     last INSCOPE;
4185 wakaba 1.47 }
4186 wakaba 1.52 } # INSCOPE
4187     unless (defined $i) {
4188     !!!parse-error (type => 'unmatched end tag:select');
4189     ## Ignore the token
4190     !!!next-token;
4191     redo B;
4192 wakaba 1.47 }
4193 wakaba 1.52
4194     splice @{$self->{open_elements}}, $i;
4195    
4196     $self->_reset_insertion_mode;
4197 wakaba 1.47
4198 wakaba 1.52 !!!next-token;
4199     redo B;
4200 wakaba 1.58 } else {
4201     !!!parse-error (type => 'in select:'.$token->{tag_name});
4202     ## Ignore the token
4203     !!!next-token;
4204     redo B;
4205     }
4206     } elsif ($token->{type} == END_TAG_TOKEN) {
4207 wakaba 1.52 if ($token->{tag_name} eq 'optgroup') {
4208     if ($self->{open_elements}->[-1]->[1] eq 'option' and
4209     $self->{open_elements}->[-2]->[1] eq 'optgroup') {
4210     ## As if </option>
4211     splice @{$self->{open_elements}}, -2;
4212     } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
4213     pop @{$self->{open_elements}};
4214     } else {
4215     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4216     ## Ignore the token
4217     }
4218     !!!next-token;
4219     redo B;
4220     } elsif ($token->{tag_name} eq 'option') {
4221     if ($self->{open_elements}->[-1]->[1] eq 'option') {
4222 wakaba 1.47 pop @{$self->{open_elements}};
4223 wakaba 1.52 } else {
4224     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4225     ## Ignore the token
4226 wakaba 1.1 }
4227 wakaba 1.52 !!!next-token;
4228     redo B;
4229     } elsif ($token->{tag_name} eq 'select') {
4230     ## have an element in table scope
4231     my $i;
4232     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4233     my $node = $self->{open_elements}->[$_];
4234     if ($node->[1] eq $token->{tag_name}) {
4235     $i = $_;
4236     last INSCOPE;
4237     } elsif ({
4238     table => 1, html => 1,
4239     }->{$node->[1]}) {
4240     last INSCOPE;
4241 wakaba 1.48 }
4242 wakaba 1.52 } # INSCOPE
4243     unless (defined $i) {
4244     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4245     ## Ignore the token
4246     !!!next-token;
4247 wakaba 1.48 redo B;
4248 wakaba 1.52 }
4249    
4250     splice @{$self->{open_elements}}, $i;
4251    
4252     $self->_reset_insertion_mode;
4253    
4254     !!!next-token;
4255     redo B;
4256     } elsif ({
4257     caption => 1, table => 1, tbody => 1,
4258     tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
4259     }->{$token->{tag_name}}) {
4260     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4261    
4262     ## have an element in table scope
4263     my $i;
4264     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4265     my $node = $self->{open_elements}->[$_];
4266     if ($node->[1] eq $token->{tag_name}) {
4267     $i = $_;
4268     last INSCOPE;
4269     } elsif ({
4270     table => 1, html => 1,
4271     }->{$node->[1]}) {
4272     last INSCOPE;
4273 wakaba 1.1 }
4274 wakaba 1.52 } # INSCOPE
4275     unless (defined $i) {
4276     ## Ignore the token
4277 wakaba 1.1 !!!next-token;
4278     redo B;
4279     }
4280 wakaba 1.52
4281     ## As if </select>
4282     ## have an element in table scope
4283     undef $i;
4284 wakaba 1.3 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4285     my $node = $self->{open_elements}->[$_];
4286 wakaba 1.52 if ($node->[1] eq 'select') {
4287 wakaba 1.1 $i = $_;
4288     last INSCOPE;
4289     } elsif ({
4290     table => 1, html => 1,
4291 wakaba 1.52 }->{$node->[1]}) {
4292     last INSCOPE;
4293     }
4294     } # INSCOPE
4295     unless (defined $i) {
4296     !!!parse-error (type => 'unmatched end tag:select');
4297     ## Ignore the </select> token
4298     !!!next-token; ## TODO: ok?
4299     redo B;
4300     }
4301    
4302     splice @{$self->{open_elements}}, $i;
4303    
4304     $self->_reset_insertion_mode;
4305    
4306     ## reprocess
4307     redo B;
4308 wakaba 1.58 } else {
4309     !!!parse-error (type => 'in select:/'.$token->{tag_name});
4310 wakaba 1.52 ## Ignore the token
4311     !!!next-token;
4312     redo B;
4313 wakaba 1.58 }
4314     } else {
4315     die "$0: $token->{type}: Unknown token type";
4316     }
4317 wakaba 1.56 } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
4318 wakaba 1.55 if ($token->{type} == CHARACTER_TOKEN) {
4319 wakaba 1.52 if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4320     my $data = $1;
4321     ## As if in body
4322     $reconstruct_active_formatting_elements->($insert_to_current);
4323    
4324     $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4325    
4326     unless (length $token->{data}) {
4327     !!!next-token;
4328     redo B;
4329     }
4330     }
4331    
4332 wakaba 1.54 if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4333 wakaba 1.52 !!!parse-error (type => 'after html:#character');
4334    
4335     ## Reprocess in the "main" phase, "after body" insertion mode...
4336     }
4337    
4338     ## "after body" insertion mode
4339     !!!parse-error (type => 'after body:#character');
4340    
4341 wakaba 1.54 $self->{insertion_mode} = IN_BODY_IM;
4342 wakaba 1.52 ## reprocess
4343     redo B;
4344 wakaba 1.55 } elsif ($token->{type} == START_TAG_TOKEN) {
4345 wakaba 1.54 if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4346 wakaba 1.52 !!!parse-error (type => 'after html:'.$token->{tag_name});
4347    
4348     ## Reprocess in the "main" phase, "after body" insertion mode...
4349     }
4350    
4351     ## "after body" insertion mode
4352     !!!parse-error (type => 'after body:'.$token->{tag_name});
4353    
4354 wakaba 1.54 $self->{insertion_mode} = IN_BODY_IM;
4355 wakaba 1.52 ## reprocess
4356     redo B;
4357 wakaba 1.55 } elsif ($token->{type} == END_TAG_TOKEN) {
4358 wakaba 1.54 if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4359 wakaba 1.52 !!!parse-error (type => 'after html:/'.$token->{tag_name});
4360    
4361 wakaba 1.54 $self->{insertion_mode} = AFTER_BODY_IM;
4362 wakaba 1.52 ## Reprocess in the "main" phase, "after body" insertion mode...
4363     }
4364    
4365     ## "after body" insertion mode
4366     if ($token->{tag_name} eq 'html') {
4367     if (defined $self->{inner_html_node}) {
4368     !!!parse-error (type => 'unmatched end tag:html');
4369     ## Ignore the token
4370     !!!next-token;
4371     redo B;
4372     } else {
4373 wakaba 1.54 $self->{insertion_mode} = AFTER_HTML_BODY_IM;
4374 wakaba 1.52 !!!next-token;
4375     redo B;
4376     }
4377     } else {
4378     !!!parse-error (type => 'after body:/'.$token->{tag_name});
4379    
4380 wakaba 1.54 $self->{insertion_mode} = IN_BODY_IM;
4381 wakaba 1.52 ## reprocess
4382     redo B;
4383     }
4384     } else {
4385     die "$0: $token->{type}: Unknown token type";
4386     }
4387 wakaba 1.56 } elsif ($self->{insertion_mode} & FRAME_IMS) {
4388 wakaba 1.55 if ($token->{type} == CHARACTER_TOKEN) {
4389 wakaba 1.52 if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4390     $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4391    
4392     unless (length $token->{data}) {
4393     !!!next-token;
4394     redo B;
4395     }
4396     }
4397    
4398     if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
4399 wakaba 1.54 if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4400 wakaba 1.52 !!!parse-error (type => 'in frameset:#character');
4401 wakaba 1.54 } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4402 wakaba 1.52 !!!parse-error (type => 'after frameset:#character');
4403     } else { # "after html frameset"
4404     !!!parse-error (type => 'after html:#character');
4405    
4406 wakaba 1.54 $self->{insertion_mode} = AFTER_FRAMESET_IM;
4407 wakaba 1.52 ## Reprocess in the "main" phase, "after frameset"...
4408     !!!parse-error (type => 'after frameset:#character');
4409     }
4410    
4411     ## Ignore the token.
4412     if (length $token->{data}) {
4413     ## reprocess the rest of characters
4414     } else {
4415     !!!next-token;
4416     }
4417     redo B;
4418     }
4419    
4420     die qq[$0: Character "$token->{data}"];
4421 wakaba 1.55 } elsif ($token->{type} == START_TAG_TOKEN) {
4422 wakaba 1.54 if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4423 wakaba 1.52 !!!parse-error (type => 'after html:'.$token->{tag_name});
4424 wakaba 1.1
4425 wakaba 1.54 $self->{insertion_mode} = AFTER_FRAMESET_IM;
4426 wakaba 1.52 ## Process in the "main" phase, "after frameset" insertion mode...
4427     }
4428 wakaba 1.1
4429 wakaba 1.52 if ($token->{tag_name} eq 'frameset' and
4430 wakaba 1.54 $self->{insertion_mode} == IN_FRAMESET_IM) {
4431 wakaba 1.52 !!!insert-element ($token->{tag_name}, $token->{attributes});
4432     !!!next-token;
4433     redo B;
4434     } elsif ($token->{tag_name} eq 'frame' and
4435 wakaba 1.54 $self->{insertion_mode} == IN_FRAMESET_IM) {
4436 wakaba 1.52 !!!insert-element ($token->{tag_name}, $token->{attributes});
4437     pop @{$self->{open_elements}};
4438     !!!next-token;
4439     redo B;
4440     } elsif ($token->{tag_name} eq 'noframes') {
4441     ## NOTE: As if in body.
4442     $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
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 wakaba 1.55 } elsif ($token->{type} == END_TAG_TOKEN) {
4455 wakaba 1.54 if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4456 wakaba 1.52 !!!parse-error (type => 'after html:/'.$token->{tag_name});
4457 wakaba 1.1
4458 wakaba 1.54 $self->{insertion_mode} = AFTER_FRAMESET_IM;
4459 wakaba 1.52 ## Process in the "main" phase, "after frameset" insertion mode...
4460     }
4461 wakaba 1.1
4462 wakaba 1.52 if ($token->{tag_name} eq 'frameset' and
4463 wakaba 1.54 $self->{insertion_mode} == IN_FRAMESET_IM) {
4464 wakaba 1.52 if ($self->{open_elements}->[-1]->[1] eq 'html' and
4465     @{$self->{open_elements}} == 1) {
4466     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4467     ## Ignore the token
4468     !!!next-token;
4469     } else {
4470     pop @{$self->{open_elements}};
4471     !!!next-token;
4472     }
4473 wakaba 1.47
4474 wakaba 1.52 if (not defined $self->{inner_html_node} and
4475     $self->{open_elements}->[-1]->[1] ne 'frameset') {
4476 wakaba 1.54 $self->{insertion_mode} = AFTER_FRAMESET_IM;
4477 wakaba 1.52 }
4478     redo B;
4479     } elsif ($token->{tag_name} eq 'html' and
4480 wakaba 1.54 $self->{insertion_mode} == AFTER_FRAMESET_IM) {
4481     $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
4482 wakaba 1.52 !!!next-token;
4483     redo B;
4484     } else {
4485 wakaba 1.54 if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4486 wakaba 1.52 !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
4487     } else {
4488     !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
4489     }
4490     ## Ignore the token
4491     !!!next-token;
4492     redo B;
4493     }
4494     } else {
4495     die "$0: $token->{type}: Unknown token type";
4496     }
4497 wakaba 1.47
4498 wakaba 1.52 ## ISSUE: An issue in spec here
4499     } else {
4500     die "$0: $self->{insertion_mode}: Unknown insertion mode";
4501     }
4502 wakaba 1.47
4503 wakaba 1.52 ## "in body" insertion mode
4504 wakaba 1.55 if ($token->{type} == START_TAG_TOKEN) {
4505 wakaba 1.52 if ($token->{tag_name} eq 'script') {
4506     ## NOTE: This is an "as if in head" code clone
4507     $script_start_tag->($insert);
4508 wakaba 1.53 redo B;
4509 wakaba 1.52 } elsif ($token->{tag_name} eq 'style') {
4510     ## NOTE: This is an "as if in head" code clone
4511     $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4512 wakaba 1.53 redo B;
4513 wakaba 1.52 } elsif ({
4514     base => 1, link => 1,
4515     }->{$token->{tag_name}}) {
4516     ## NOTE: This is an "as if in head" code clone, only "-t" differs
4517     !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4518     pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4519     !!!next-token;
4520 wakaba 1.53 redo B;
4521 wakaba 1.52 } elsif ($token->{tag_name} eq 'meta') {
4522     ## NOTE: This is an "as if in head" code clone, only "-t" differs
4523     !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4524 wakaba 1.66 my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4525 wakaba 1.46
4526 wakaba 1.52 unless ($self->{confident}) {
4527     if ($token->{attributes}->{charset}) { ## TODO: And if supported
4528 wakaba 1.63 $self->{change_encoding}
4529     ->($self, $token->{attributes}->{charset}->{value});
4530 wakaba 1.66
4531     $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4532     ->set_user_data (manakai_has_reference =>
4533     $token->{attributes}->{charset}
4534     ->{has_reference});
4535 wakaba 1.63 } elsif ($token->{attributes}->{content}) {
4536 wakaba 1.52 ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
4537 wakaba 1.63 if ($token->{attributes}->{content}->{value}
4538 wakaba 1.52 =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
4539     [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4540     ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4541 wakaba 1.63 $self->{change_encoding}
4542     ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
4543 wakaba 1.68 $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4544     ->set_user_data (manakai_has_reference =>
4545     $token->{attributes}->{content}
4546     ->{has_reference});
4547 wakaba 1.63 }
4548 wakaba 1.52 }
4549 wakaba 1.66 } else {
4550     if ($token->{attributes}->{charset}) {
4551     $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4552     ->set_user_data (manakai_has_reference =>
4553     $token->{attributes}->{charset}
4554     ->{has_reference});
4555     }
4556 wakaba 1.68 if ($token->{attributes}->{content}) {
4557     $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4558     ->set_user_data (manakai_has_reference =>
4559     $token->{attributes}->{content}
4560     ->{has_reference});
4561     }
4562 wakaba 1.52 }
4563 wakaba 1.1
4564 wakaba 1.52 !!!next-token;
4565 wakaba 1.53 redo B;
4566 wakaba 1.52 } elsif ($token->{tag_name} eq 'title') {
4567     !!!parse-error (type => 'in body:title');
4568     ## NOTE: This is an "as if in head" code clone
4569     $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
4570     if (defined $self->{head_element}) {
4571     $self->{head_element}->append_child ($_[0]);
4572     } else {
4573     $insert->($_[0]);
4574     }
4575     });
4576 wakaba 1.53 redo B;
4577 wakaba 1.52 } elsif ($token->{tag_name} eq 'body') {
4578     !!!parse-error (type => 'in body:body');
4579 wakaba 1.46
4580 wakaba 1.52 if (@{$self->{open_elements}} == 1 or
4581     $self->{open_elements}->[1]->[1] ne 'body') {
4582     ## Ignore the token
4583     } else {
4584     my $body_el = $self->{open_elements}->[1]->[0];
4585     for my $attr_name (keys %{$token->{attributes}}) {
4586     unless ($body_el->has_attribute_ns (undef, $attr_name)) {
4587     $body_el->set_attribute_ns
4588     (undef, [undef, $attr_name],
4589     $token->{attributes}->{$attr_name}->{value});
4590     }
4591     }
4592     }
4593     !!!next-token;
4594 wakaba 1.53 redo B;
4595 wakaba 1.52 } elsif ({
4596     address => 1, blockquote => 1, center => 1, dir => 1,
4597     div => 1, dl => 1, fieldset => 1, listing => 1,
4598     menu => 1, ol => 1, p => 1, ul => 1,
4599     pre => 1,
4600     }->{$token->{tag_name}}) {
4601     ## has a p element in scope
4602     INSCOPE: for (reverse @{$self->{open_elements}}) {
4603     if ($_->[1] eq 'p') {
4604     !!!back-token;
4605 wakaba 1.55 $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4606 wakaba 1.53 redo B;
4607 wakaba 1.52 } elsif ({
4608     table => 1, caption => 1, td => 1, th => 1,
4609     button => 1, marquee => 1, object => 1, html => 1,
4610     }->{$_->[1]}) {
4611     last INSCOPE;
4612     }
4613     } # INSCOPE
4614    
4615     !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4616     if ($token->{tag_name} eq 'pre') {
4617     !!!next-token;
4618 wakaba 1.55 if ($token->{type} == CHARACTER_TOKEN) {
4619 wakaba 1.52 $token->{data} =~ s/^\x0A//;
4620     unless (length $token->{data}) {
4621 wakaba 1.1 !!!next-token;
4622 wakaba 1.52 }
4623     }
4624     } else {
4625     !!!next-token;
4626     }
4627 wakaba 1.53 redo B;
4628 wakaba 1.52 } elsif ($token->{tag_name} eq 'form') {
4629     if (defined $self->{form_element}) {
4630     !!!parse-error (type => 'in form:form');
4631     ## Ignore the token
4632     !!!next-token;
4633 wakaba 1.53 redo B;
4634 wakaba 1.52 } else {
4635     ## has a p element in scope
4636     INSCOPE: for (reverse @{$self->{open_elements}}) {
4637     if ($_->[1] eq 'p') {
4638     !!!back-token;
4639 wakaba 1.55 $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4640 wakaba 1.53 redo B;
4641 wakaba 1.46 } elsif ({
4642 wakaba 1.52 table => 1, caption => 1, td => 1, th => 1,
4643     button => 1, marquee => 1, object => 1, html => 1,
4644     }->{$_->[1]}) {
4645     last INSCOPE;
4646     }
4647     } # INSCOPE
4648    
4649     !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4650     $self->{form_element} = $self->{open_elements}->[-1]->[0];
4651     !!!next-token;
4652 wakaba 1.53 redo B;
4653 wakaba 1.52 }
4654     } elsif ($token->{tag_name} eq 'li') {
4655     ## has a p element in scope
4656     INSCOPE: for (reverse @{$self->{open_elements}}) {
4657     if ($_->[1] eq 'p') {
4658     !!!back-token;
4659 wakaba 1.55 $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4660 wakaba 1.53 redo B;
4661 wakaba 1.52 } elsif ({
4662     table => 1, caption => 1, td => 1, th => 1,
4663     button => 1, marquee => 1, object => 1, html => 1,
4664     }->{$_->[1]}) {
4665     last INSCOPE;
4666     }
4667     } # INSCOPE
4668    
4669     ## Step 1
4670     my $i = -1;
4671     my $node = $self->{open_elements}->[$i];
4672     LI: {
4673     ## Step 2
4674     if ($node->[1] eq 'li') {
4675     if ($i != -1) {
4676     !!!parse-error (type => 'end tag missing:'.
4677     $self->{open_elements}->[-1]->[1]);
4678     }
4679     splice @{$self->{open_elements}}, $i;
4680     last LI;
4681     }
4682    
4683     ## Step 3
4684     if (not $formatting_category->{$node->[1]} and
4685     #not $phrasing_category->{$node->[1]} and
4686     ($special_category->{$node->[1]} or
4687     $scoping_category->{$node->[1]}) and
4688     $node->[1] ne 'address' and $node->[1] ne 'div') {
4689     last LI;
4690     }
4691    
4692     ## Step 4
4693     $i--;
4694     $node = $self->{open_elements}->[$i];
4695     redo LI;
4696     } # LI
4697    
4698     !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4699     !!!next-token;
4700 wakaba 1.53 redo B;
4701 wakaba 1.52 } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {
4702     ## has a p element in scope
4703     INSCOPE: for (reverse @{$self->{open_elements}}) {
4704     if ($_->[1] eq 'p') {
4705     !!!back-token;
4706 wakaba 1.55 $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4707 wakaba 1.53 redo B;
4708 wakaba 1.52 } elsif ({
4709     table => 1, caption => 1, td => 1, th => 1,
4710     button => 1, marquee => 1, object => 1, html => 1,
4711     }->{$_->[1]}) {
4712     last INSCOPE;
4713     }
4714     } # INSCOPE
4715    
4716     ## Step 1
4717     my $i = -1;
4718     my $node = $self->{open_elements}->[$i];
4719     LI: {
4720     ## Step 2
4721     if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {
4722     if ($i != -1) {
4723     !!!parse-error (type => 'end tag missing:'.
4724     $self->{open_elements}->[-1]->[1]);
4725 wakaba 1.1 }
4726 wakaba 1.52 splice @{$self->{open_elements}}, $i;
4727     last LI;
4728     }
4729    
4730     ## Step 3
4731     if (not $formatting_category->{$node->[1]} and
4732     #not $phrasing_category->{$node->[1]} and
4733     ($special_category->{$node->[1]} or
4734     $scoping_category->{$node->[1]}) and
4735     $node->[1] ne 'address' and $node->[1] ne 'div') {
4736     last LI;
4737 wakaba 1.1 }
4738 wakaba 1.52
4739     ## Step 4
4740     $i--;
4741     $node = $self->{open_elements}->[$i];
4742     redo LI;
4743     } # LI
4744    
4745     !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4746     !!!next-token;
4747 wakaba 1.53 redo B;
4748 wakaba 1.52 } elsif ($token->{tag_name} eq 'plaintext') {
4749     ## has a p element in scope
4750     INSCOPE: for (reverse @{$self->{open_elements}}) {
4751     if ($_->[1] eq 'p') {
4752     !!!back-token;
4753 wakaba 1.55 $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4754 wakaba 1.53 redo B;
4755 wakaba 1.52 } elsif ({
4756     table => 1, caption => 1, td => 1, th => 1,
4757     button => 1, marquee => 1, object => 1, html => 1,
4758     }->{$_->[1]}) {
4759     last INSCOPE;
4760 wakaba 1.46 }
4761 wakaba 1.52 } # INSCOPE
4762    
4763     !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4764    
4765     $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
4766    
4767     !!!next-token;
4768 wakaba 1.53 redo B;
4769 wakaba 1.52 } elsif ({
4770     h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4771     }->{$token->{tag_name}}) {
4772     ## has a p element in scope
4773     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4774     my $node = $self->{open_elements}->[$_];
4775     if ($node->[1] eq 'p') {
4776     !!!back-token;
4777 wakaba 1.55 $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4778 wakaba 1.53 redo B;
4779 wakaba 1.52 } elsif ({
4780     table => 1, caption => 1, td => 1, th => 1,
4781     button => 1, marquee => 1, object => 1, html => 1,
4782     }->{$node->[1]}) {
4783     last INSCOPE;
4784 wakaba 1.46 }
4785 wakaba 1.52 } # INSCOPE
4786    
4787     ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>
4788     ## has an element in scope
4789     #my $i;
4790     #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4791     # my $node = $self->{open_elements}->[$_];
4792     # if ({
4793     # h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4794     # }->{$node->[1]}) {
4795     # $i = $_;
4796     # last INSCOPE;
4797     # } elsif ({
4798     # table => 1, caption => 1, td => 1, th => 1,
4799     # button => 1, marquee => 1, object => 1, html => 1,
4800     # }->{$node->[1]}) {
4801     # last INSCOPE;
4802     # }
4803     #} # INSCOPE
4804     #
4805     #if (defined $i) {
4806     # !!! parse-error (type => 'in hn:hn');
4807     # splice @{$self->{open_elements}}, $i;
4808     #}
4809    
4810     !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4811    
4812     !!!next-token;
4813 wakaba 1.53 redo B;
4814 wakaba 1.52 } elsif ($token->{tag_name} eq 'a') {
4815     AFE: for my $i (reverse 0..$#$active_formatting_elements) {
4816     my $node = $active_formatting_elements->[$i];
4817     if ($node->[1] eq 'a') {
4818     !!!parse-error (type => 'in a:a');
4819    
4820     !!!back-token;
4821 wakaba 1.55 $token = {type => END_TAG_TOKEN, tag_name => 'a'};
4822 wakaba 1.52 $formatting_end_tag->($token->{tag_name});
4823    
4824     AFE2: for (reverse 0..$#$active_formatting_elements) {
4825     if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
4826     splice @$active_formatting_elements, $_, 1;
4827     last AFE2;
4828 wakaba 1.1 }
4829 wakaba 1.52 } # AFE2
4830     OE: for (reverse 0..$#{$self->{open_elements}}) {
4831     if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
4832     splice @{$self->{open_elements}}, $_, 1;
4833     last OE;
4834 wakaba 1.1 }
4835 wakaba 1.52 } # OE
4836     last AFE;
4837     } elsif ($node->[0] eq '#marker') {
4838     last AFE;
4839     }
4840     } # AFE
4841    
4842     $reconstruct_active_formatting_elements->($insert_to_current);
4843 wakaba 1.1
4844 wakaba 1.52 !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4845     push @$active_formatting_elements, $self->{open_elements}->[-1];
4846 wakaba 1.1
4847 wakaba 1.52 !!!next-token;
4848 wakaba 1.53 redo B;
4849 wakaba 1.52 } elsif ({
4850     b => 1, big => 1, em => 1, font => 1, i => 1,
4851     s => 1, small => 1, strile => 1,
4852     strong => 1, tt => 1, u => 1,
4853     }->{$token->{tag_name}}) {
4854     $reconstruct_active_formatting_elements->($insert_to_current);
4855    
4856     !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4857     push @$active_formatting_elements, $self->{open_elements}->[-1];
4858    
4859     !!!next-token;
4860 wakaba 1.53 redo B;
4861 wakaba 1.52 } elsif ($token->{tag_name} eq 'nobr') {
4862     $reconstruct_active_formatting_elements->($insert_to_current);
4863 wakaba 1.1
4864 wakaba 1.52 ## has a |nobr| element in scope
4865     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4866     my $node = $self->{open_elements}->[$_];
4867     if ($node->[1] eq 'nobr') {
4868 wakaba 1.58 !!!parse-error (type => 'in nobr:nobr');
4869 wakaba 1.52 !!!back-token;
4870 wakaba 1.55 $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
4871 wakaba 1.53 redo B;
4872 wakaba 1.52 } elsif ({
4873     table => 1, caption => 1, td => 1, th => 1,
4874     button => 1, marquee => 1, object => 1, html => 1,
4875     }->{$node->[1]}) {
4876     last INSCOPE;
4877     }
4878     } # INSCOPE
4879    
4880     !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4881     push @$active_formatting_elements, $self->{open_elements}->[-1];
4882    
4883     !!!next-token;
4884 wakaba 1.53 redo B;
4885 wakaba 1.52 } elsif ($token->{tag_name} eq 'button') {
4886     ## has a button element in scope
4887     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4888     my $node = $self->{open_elements}->[$_];
4889     if ($node->[1] eq 'button') {
4890     !!!parse-error (type => 'in button:button');
4891     !!!back-token;
4892 wakaba 1.55 $token = {type => END_TAG_TOKEN, tag_name => 'button'};
4893 wakaba 1.53 redo B;
4894 wakaba 1.52 } elsif ({
4895     table => 1, caption => 1, td => 1, th => 1,
4896     button => 1, marquee => 1, object => 1, html => 1,
4897     }->{$node->[1]}) {
4898     last INSCOPE;
4899     }
4900     } # INSCOPE
4901    
4902     $reconstruct_active_formatting_elements->($insert_to_current);
4903    
4904     !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4905     push @$active_formatting_elements, ['#marker', ''];
4906 wakaba 1.1
4907 wakaba 1.52 !!!next-token;
4908 wakaba 1.53 redo B;
4909 wakaba 1.52 } elsif ($token->{tag_name} eq 'marquee' or
4910     $token->{tag_name} eq 'object') {
4911     $reconstruct_active_formatting_elements->($insert_to_current);
4912    
4913     !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4914     push @$active_formatting_elements, ['#marker', ''];
4915    
4916     !!!next-token;
4917 wakaba 1.53 redo B;
4918 wakaba 1.52 } elsif ($token->{tag_name} eq 'xmp') {
4919     $reconstruct_active_formatting_elements->($insert_to_current);
4920     $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4921 wakaba 1.53 redo B;
4922 wakaba 1.52 } elsif ($token->{tag_name} eq 'table') {
4923     ## has a p element in scope
4924     INSCOPE: for (reverse @{$self->{open_elements}}) {
4925     if ($_->[1] eq 'p') {
4926     !!!back-token;
4927 wakaba 1.55 $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4928 wakaba 1.53 redo B;
4929 wakaba 1.52 } elsif ({
4930     table => 1, caption => 1, td => 1, th => 1,
4931     button => 1, marquee => 1, object => 1, html => 1,
4932     }->{$_->[1]}) {
4933     last INSCOPE;
4934     }
4935     } # INSCOPE
4936    
4937     !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4938    
4939 wakaba 1.54 $self->{insertion_mode} = IN_TABLE_IM;
4940 wakaba 1.52
4941     !!!next-token;
4942 wakaba 1.53 redo B;
4943 wakaba 1.52 } elsif ({
4944     area => 1, basefont => 1, bgsound => 1, br => 1,
4945     embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
4946     image => 1,
4947     }->{$token->{tag_name}}) {
4948     if ($token->{tag_name} eq 'image') {
4949     !!!parse-error (type => 'image');
4950     $token->{tag_name} = 'img';
4951     }
4952 wakaba 1.1
4953 wakaba 1.52 ## NOTE: There is an "as if <br>" code clone.
4954     $reconstruct_active_formatting_elements->($insert_to_current);
4955    
4956     !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4957     pop @{$self->{open_elements}};
4958    
4959     !!!next-token;
4960 wakaba 1.53 redo B;
4961 wakaba 1.52 } elsif ($token->{tag_name} eq 'hr') {
4962     ## has a p element in scope
4963     INSCOPE: for (reverse @{$self->{open_elements}}) {
4964     if ($_->[1] eq 'p') {
4965     !!!back-token;
4966 wakaba 1.55 $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4967 wakaba 1.53 redo B;
4968 wakaba 1.52 } elsif ({
4969     table => 1, caption => 1, td => 1, th => 1,
4970     button => 1, marquee => 1, object => 1, html => 1,
4971     }->{$_->[1]}) {
4972     last INSCOPE;
4973     }
4974     } # INSCOPE
4975    
4976     !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4977     pop @{$self->{open_elements}};
4978    
4979     !!!next-token;
4980 wakaba 1.53 redo B;
4981 wakaba 1.52 } elsif ($token->{tag_name} eq 'input') {
4982     $reconstruct_active_formatting_elements->($insert_to_current);
4983    
4984     !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4985     ## TODO: associate with $self->{form_element} if defined
4986     pop @{$self->{open_elements}};
4987    
4988     !!!next-token;
4989 wakaba 1.53 redo B;
4990 wakaba 1.52 } elsif ($token->{tag_name} eq 'isindex') {
4991     !!!parse-error (type => 'isindex');
4992    
4993     if (defined $self->{form_element}) {
4994     ## Ignore the token
4995     !!!next-token;
4996 wakaba 1.53 redo B;
4997 wakaba 1.52 } else {
4998     my $at = $token->{attributes};
4999     my $form_attrs;
5000     $form_attrs->{action} = $at->{action} if $at->{action};
5001     my $prompt_attr = $at->{prompt};
5002     $at->{name} = {name => 'name', value => 'isindex'};
5003     delete $at->{action};
5004     delete $at->{prompt};
5005     my @tokens = (
5006 wakaba 1.55 {type => START_TAG_TOKEN, tag_name => 'form',
5007 wakaba 1.52 attributes => $form_attrs},
5008 wakaba 1.55 {type => START_TAG_TOKEN, tag_name => 'hr'},
5009     {type => START_TAG_TOKEN, tag_name => 'p'},
5010     {type => START_TAG_TOKEN, tag_name => 'label'},
5011 wakaba 1.52 );
5012     if ($prompt_attr) {
5013 wakaba 1.55 push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
5014 wakaba 1.1 } else {
5015 wakaba 1.55 push @tokens, {type => CHARACTER_TOKEN,
5016 wakaba 1.52 data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
5017     ## TODO: make this configurable
5018 wakaba 1.1 }
5019 wakaba 1.52 push @tokens,
5020 wakaba 1.55 {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},
5021     #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
5022     {type => END_TAG_TOKEN, tag_name => 'label'},
5023     {type => END_TAG_TOKEN, tag_name => 'p'},
5024     {type => START_TAG_TOKEN, tag_name => 'hr'},
5025     {type => END_TAG_TOKEN, tag_name => 'form'};
5026 wakaba 1.52 $token = shift @tokens;
5027     !!!back-token (@tokens);
5028 wakaba 1.53 redo B;
5029 wakaba 1.52 }
5030     } elsif ($token->{tag_name} eq 'textarea') {
5031     my $tag_name = $token->{tag_name};
5032     my $el;
5033     !!!create-element ($el, $token->{tag_name}, $token->{attributes});
5034    
5035     ## TODO: $self->{form_element} if defined
5036     $self->{content_model} = RCDATA_CONTENT_MODEL;
5037     delete $self->{escape}; # MUST
5038    
5039     $insert->($el);
5040    
5041     my $text = '';
5042     !!!next-token;
5043 wakaba 1.55 if ($token->{type} == CHARACTER_TOKEN) {
5044 wakaba 1.52 $token->{data} =~ s/^\x0A//;
5045 wakaba 1.51 unless (length $token->{data}) {
5046     !!!next-token;
5047     }
5048     }
5049 wakaba 1.55 while ($token->{type} == CHARACTER_TOKEN) {
5050 wakaba 1.52 $text .= $token->{data};
5051     !!!next-token;
5052     }
5053     if (length $text) {
5054     $el->manakai_append_text ($text);
5055     }
5056    
5057     $self->{content_model} = PCDATA_CONTENT_MODEL;
5058 wakaba 1.51
5059 wakaba 1.55 if ($token->{type} == END_TAG_TOKEN and
5060 wakaba 1.52 $token->{tag_name} eq $tag_name) {
5061     ## Ignore the token
5062     } else {
5063     !!!parse-error (type => 'in RCDATA:#'.$token->{type});
5064 wakaba 1.51 }
5065 wakaba 1.52 !!!next-token;
5066 wakaba 1.53 redo B;
5067 wakaba 1.52 } elsif ({
5068     iframe => 1,
5069     noembed => 1,
5070     noframes => 1,
5071     noscript => 0, ## TODO: 1 if scripting is enabled
5072     }->{$token->{tag_name}}) {
5073 wakaba 1.58 ## NOTE: There is an "as if in body" code clone.
5074 wakaba 1.52 $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
5075 wakaba 1.53 redo B;
5076 wakaba 1.52 } elsif ($token->{tag_name} eq 'select') {
5077     $reconstruct_active_formatting_elements->($insert_to_current);
5078    
5079     !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5080    
5081 wakaba 1.54 $self->{insertion_mode} = IN_SELECT_IM;
5082 wakaba 1.52 !!!next-token;
5083 wakaba 1.53 redo B;
5084 wakaba 1.52 } elsif ({
5085     caption => 1, col => 1, colgroup => 1, frame => 1,
5086     frameset => 1, head => 1, option => 1, optgroup => 1,
5087     tbody => 1, td => 1, tfoot => 1, th => 1,
5088     thead => 1, tr => 1,
5089     }->{$token->{tag_name}}) {
5090     !!!parse-error (type => 'in body:'.$token->{tag_name});
5091     ## Ignore the token
5092     !!!next-token;
5093 wakaba 1.53 redo B;
5094 wakaba 1.52
5095     ## ISSUE: An issue on HTML5 new elements in the spec.
5096     } else {
5097     $reconstruct_active_formatting_elements->($insert_to_current);
5098    
5099     !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5100 wakaba 1.51
5101 wakaba 1.52 !!!next-token;
5102 wakaba 1.53 redo B;
5103 wakaba 1.52 }
5104 wakaba 1.55 } elsif ($token->{type} == END_TAG_TOKEN) {
5105 wakaba 1.52 if ($token->{tag_name} eq 'body') {
5106     if (@{$self->{open_elements}} > 1 and
5107     $self->{open_elements}->[1]->[1] eq 'body') {
5108     for (@{$self->{open_elements}}) {
5109     unless ({
5110     dd => 1, dt => 1, li => 1, p => 1, td => 1,
5111     th => 1, tr => 1, body => 1, html => 1,
5112     tbody => 1, tfoot => 1, thead => 1,
5113     }->{$_->[1]}) {
5114     !!!parse-error (type => 'not closed:'.$_->[1]);
5115     }
5116     }
5117 wakaba 1.51
5118 wakaba 1.54 $self->{insertion_mode} = AFTER_BODY_IM;
5119 wakaba 1.52 !!!next-token;
5120 wakaba 1.53 redo B;
5121 wakaba 1.52 } else {
5122     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5123     ## Ignore the token
5124     !!!next-token;
5125 wakaba 1.53 redo B;
5126 wakaba 1.51 }
5127 wakaba 1.52 } elsif ($token->{tag_name} eq 'html') {
5128     if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
5129     ## ISSUE: There is an issue in the spec.
5130     if ($self->{open_elements}->[-1]->[1] ne 'body') {
5131     !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
5132 wakaba 1.1 }
5133 wakaba 1.54 $self->{insertion_mode} = AFTER_BODY_IM;
5134 wakaba 1.52 ## reprocess
5135 wakaba 1.53 redo B;
5136 wakaba 1.51 } else {
5137 wakaba 1.52 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5138     ## Ignore the token
5139     !!!next-token;
5140 wakaba 1.53 redo B;
5141 wakaba 1.51 }
5142 wakaba 1.52 } elsif ({
5143     address => 1, blockquote => 1, center => 1, dir => 1,
5144     div => 1, dl => 1, fieldset => 1, listing => 1,
5145     menu => 1, ol => 1, pre => 1, ul => 1,
5146     p => 1,
5147     dd => 1, dt => 1, li => 1,
5148     button => 1, marquee => 1, object => 1,
5149     }->{$token->{tag_name}}) {
5150     ## has an element in scope
5151     my $i;
5152     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5153     my $node = $self->{open_elements}->[$_];
5154     if ($node->[1] eq $token->{tag_name}) {
5155     ## generate implied end tags
5156     if ({
5157     dd => ($token->{tag_name} ne 'dd'),
5158     dt => ($token->{tag_name} ne 'dt'),
5159     li => ($token->{tag_name} ne 'li'),
5160     p => ($token->{tag_name} ne 'p'),
5161     td => 1, th => 1, tr => 1,
5162     tbody => 1, tfoot=> 1, thead => 1,
5163     }->{$self->{open_elements}->[-1]->[1]}) {
5164     !!!back-token;
5165 wakaba 1.55 $token = {type => END_TAG_TOKEN,
5166 wakaba 1.52 tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5167 wakaba 1.53 redo B;
5168 wakaba 1.52 }
5169     $i = $_;
5170     last INSCOPE unless $token->{tag_name} eq 'p';
5171     } elsif ({
5172     table => 1, caption => 1, td => 1, th => 1,
5173     button => 1, marquee => 1, object => 1, html => 1,
5174     }->{$node->[1]}) {
5175     last INSCOPE;
5176 wakaba 1.51 }
5177 wakaba 1.52 } # INSCOPE
5178    
5179     if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5180     if (defined $i) {
5181     !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5182 wakaba 1.51 } else {
5183 wakaba 1.52 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5184 wakaba 1.51 }
5185     }
5186    
5187 wakaba 1.52 if (defined $i) {
5188     splice @{$self->{open_elements}}, $i;
5189     } elsif ($token->{tag_name} eq 'p') {
5190     ## As if <p>, then reprocess the current token
5191     my $el;
5192     !!!create-element ($el, 'p');
5193     $insert->($el);
5194 wakaba 1.51 }
5195 wakaba 1.52 $clear_up_to_marker->()
5196     if {
5197     button => 1, marquee => 1, object => 1,
5198     }->{$token->{tag_name}};
5199     !!!next-token;
5200 wakaba 1.53 redo B;
5201 wakaba 1.52 } elsif ($token->{tag_name} eq 'form') {
5202     ## has an element in scope
5203     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5204     my $node = $self->{open_elements}->[$_];
5205     if ($node->[1] eq $token->{tag_name}) {
5206     ## generate implied end tags
5207     if ({
5208     dd => 1, dt => 1, li => 1, p => 1,
5209     td => 1, th => 1, tr => 1,
5210     tbody => 1, tfoot=> 1, thead => 1,
5211     }->{$self->{open_elements}->[-1]->[1]}) {
5212     !!!back-token;
5213 wakaba 1.55 $token = {type => END_TAG_TOKEN,
5214 wakaba 1.52 tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5215 wakaba 1.53 redo B;
5216 wakaba 1.52 }
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     }
5224     } # INSCOPE
5225    
5226     if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
5227 wakaba 1.36 pop @{$self->{open_elements}};
5228     } else {
5229 wakaba 1.58 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5230 wakaba 1.52 }
5231    
5232     undef $self->{form_element};
5233     !!!next-token;
5234 wakaba 1.53 redo B;
5235 wakaba 1.52 } elsif ({
5236     h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5237     }->{$token->{tag_name}}) {
5238     ## has an element in scope
5239     my $i;
5240     INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5241     my $node = $self->{open_elements}->[$_];
5242     if ({
5243     h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5244     }->{$node->[1]}) {
5245     ## generate implied end tags
5246     if ({
5247     dd => 1, dt => 1, li => 1, p => 1,
5248     td => 1, th => 1, tr => 1,
5249     tbody => 1, tfoot=> 1, thead => 1,
5250     }->{$self->{open_elements}->[-1]->[1]}) {
5251     !!!back-token;
5252 wakaba 1.55 $token = {type => END_TAG_TOKEN,
5253 wakaba 1.52 tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5254 wakaba 1.53 redo B;
5255 wakaba 1.52 }
5256     $i = $_;
5257     last INSCOPE;
5258     } elsif ({
5259     table => 1, caption => 1, td => 1, th => 1,
5260     button => 1, marquee => 1, object => 1, html => 1,
5261     }->{$node->[1]}) {
5262     last INSCOPE;
5263 wakaba 1.51 }
5264 wakaba 1.52 } # INSCOPE
5265    
5266     if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5267 wakaba 1.58 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5268 wakaba 1.36 }
5269 wakaba 1.52
5270     splice @{$self->{open_elements}}, $i if defined $i;
5271     !!!next-token;
5272 wakaba 1.53 redo B;
5273 wakaba 1.52 } elsif ({
5274     a => 1,
5275     b => 1, big => 1, em => 1, font => 1, i => 1,
5276     nobr => 1, s => 1, small => 1, strile => 1,
5277     strong => 1, tt => 1, u => 1,
5278     }->{$token->{tag_name}}) {
5279     $formatting_end_tag->($token->{tag_name});
5280 wakaba 1.53 redo B;
5281 wakaba 1.52 } elsif ($token->{tag_name} eq 'br') {
5282     !!!parse-error (type => 'unmatched end tag:br');
5283    
5284     ## As if <br>
5285     $reconstruct_active_formatting_elements->($insert_to_current);
5286    
5287     my $el;
5288     !!!create-element ($el, 'br');
5289     $insert->($el);
5290    
5291     ## Ignore the token.
5292     !!!next-token;
5293 wakaba 1.53 redo B;
5294 wakaba 1.52 } elsif ({
5295     caption => 1, col => 1, colgroup => 1, frame => 1,
5296     frameset => 1, head => 1, option => 1, optgroup => 1,
5297     tbody => 1, td => 1, tfoot => 1, th => 1,
5298     thead => 1, tr => 1,
5299     area => 1, basefont => 1, bgsound => 1,
5300     embed => 1, hr => 1, iframe => 1, image => 1,
5301     img => 1, input => 1, isindex => 1, noembed => 1,
5302     noframes => 1, param => 1, select => 1, spacer => 1,
5303     table => 1, textarea => 1, wbr => 1,
5304     noscript => 0, ## TODO: if scripting is enabled
5305     }->{$token->{tag_name}}) {
5306     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5307     ## Ignore the token
5308     !!!next-token;
5309 wakaba 1.53 redo B;
5310 wakaba 1.52
5311     ## ISSUE: Issue on HTML5 new elements in spec
5312    
5313     } else {
5314     ## Step 1
5315     my $node_i = -1;
5316     my $node = $self->{open_elements}->[$node_i];
5317 wakaba 1.51
5318 wakaba 1.52 ## Step 2
5319     S2: {
5320     if ($node->[1] eq $token->{tag_name}) {
5321     ## Step 1
5322     ## generate implied end tags
5323     if ({
5324     dd => 1, dt => 1, li => 1, p => 1,
5325     td => 1, th => 1, tr => 1,
5326 wakaba 1.55 tbody => 1, tfoot => 1, thead => 1,
5327 wakaba 1.52 }->{$self->{open_elements}->[-1]->[1]}) {
5328     !!!back-token;
5329 wakaba 1.55 $token = {type => END_TAG_TOKEN,
5330 wakaba 1.52 tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5331 wakaba 1.53 redo B;
5332 wakaba 1.52 }
5333    
5334     ## Step 2
5335     if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
5336 wakaba 1.58 ## NOTE: <x><y></x>
5337 wakaba 1.52 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5338     }
5339    
5340     ## Step 3
5341     splice @{$self->{open_elements}}, $node_i;
5342 wakaba 1.51
5343 wakaba 1.1 !!!next-token;
5344 wakaba 1.52 last S2;
5345 wakaba 1.1 } else {
5346 wakaba 1.52 ## Step 3
5347     if (not $formatting_category->{$node->[1]} and
5348     #not $phrasing_category->{$node->[1]} and
5349     ($special_category->{$node->[1]} or
5350     $scoping_category->{$node->[1]})) {
5351     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5352     ## Ignore the token
5353     !!!next-token;
5354     last S2;
5355     }
5356 wakaba 1.1 }
5357 wakaba 1.52
5358     ## Step 4
5359     $node_i--;
5360     $node = $self->{open_elements}->[$node_i];
5361    
5362     ## Step 5;
5363     redo S2;
5364     } # S2
5365 wakaba 1.53 redo B;
5366 wakaba 1.1 }
5367     }
5368 wakaba 1.52 redo B;
5369 wakaba 1.1 } # B
5370    
5371 wakaba 1.51 ## NOTE: The "trailing end" phase in HTML5 is split into
5372     ## two insertion modes: "after html body" and "after html frameset".
5373     ## NOTE: States in the main stage is preserved while
5374     ## the parser stays in the trailing end phase. # MUST
5375    
5376 wakaba 1.1 ## Stop parsing # MUST
5377    
5378     ## TODO: script stuffs
5379 wakaba 1.3 } # _tree_construct_main
5380    
5381     sub set_inner_html ($$$) {
5382     my $class = shift;
5383     my $node = shift;
5384     my $s = \$_[0];
5385     my $onerror = $_[1];
5386    
5387 wakaba 1.63 ## ISSUE: Should {confident} be true?
5388    
5389 wakaba 1.3 my $nt = $node->node_type;
5390     if ($nt == 9) {
5391     # MUST
5392    
5393     ## Step 1 # MUST
5394     ## TODO: If the document has an active parser, ...
5395     ## ISSUE: There is an issue in the spec.
5396    
5397     ## Step 2 # MUST
5398     my @cn = @{$node->child_nodes};
5399     for (@cn) {
5400     $node->remove_child ($_);
5401     }
5402    
5403     ## Step 3, 4, 5 # MUST
5404     $class->parse_string ($$s => $node, $onerror);
5405     } elsif ($nt == 1) {
5406     ## TODO: If non-html element
5407    
5408     ## NOTE: Most of this code is copied from |parse_string|
5409    
5410     ## Step 1 # MUST
5411 wakaba 1.14 my $this_doc = $node->owner_document;
5412     my $doc = $this_doc->implementation->create_document;
5413 wakaba 1.18 $doc->manakai_is_html (1);
5414 wakaba 1.3 my $p = $class->new;
5415     $p->{document} = $doc;
5416    
5417     ## Step 9 # MUST
5418     my $i = 0;
5419     my $line = 1;
5420     my $column = 0;
5421     $p->{set_next_input_character} = sub {
5422     my $self = shift;
5423 wakaba 1.14
5424     pop @{$self->{prev_input_character}};
5425     unshift @{$self->{prev_input_character}}, $self->{next_input_character};
5426    
5427 wakaba 1.3 $self->{next_input_character} = -1 and return if $i >= length $$s;
5428     $self->{next_input_character} = ord substr $$s, $i++, 1;
5429     $column++;
5430 wakaba 1.4
5431     if ($self->{next_input_character} == 0x000A) { # LF
5432     $line++;
5433     $column = 0;
5434     } elsif ($self->{next_input_character} == 0x000D) { # CR
5435 wakaba 1.15 $i++ if substr ($$s, $i, 1) eq "\x0A";
5436 wakaba 1.3 $self->{next_input_character} = 0x000A; # LF # MUST
5437     $line++;
5438 wakaba 1.4 $column = 0;
5439 wakaba 1.3 } elsif ($self->{next_input_character} > 0x10FFFF) {
5440     $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
5441     } elsif ($self->{next_input_character} == 0x0000) { # NULL
5442 wakaba 1.14 !!!parse-error (type => 'NULL');
5443 wakaba 1.3 $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
5444     }
5445     };
5446 wakaba 1.14 $p->{prev_input_character} = [-1, -1, -1];
5447     $p->{next_input_character} = -1;
5448 wakaba 1.3
5449     my $ponerror = $onerror || sub {
5450     my (%opt) = @_;
5451     warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";
5452     };
5453     $p->{parse_error} = sub {
5454     $ponerror->(@_, line => $line, column => $column);
5455     };
5456    
5457     $p->_initialize_tokenizer;
5458     $p->_initialize_tree_constructor;
5459    
5460     ## Step 2
5461     my $node_ln = $node->local_name;
5462 wakaba 1.40 $p->{content_model} = {
5463     title => RCDATA_CONTENT_MODEL,
5464     textarea => RCDATA_CONTENT_MODEL,
5465     style => CDATA_CONTENT_MODEL,
5466     script => CDATA_CONTENT_MODEL,
5467     xmp => CDATA_CONTENT_MODEL,
5468     iframe => CDATA_CONTENT_MODEL,
5469     noembed => CDATA_CONTENT_MODEL,
5470     noframes => CDATA_CONTENT_MODEL,
5471     noscript => CDATA_CONTENT_MODEL,
5472     plaintext => PLAINTEXT_CONTENT_MODEL,
5473     }->{$node_ln};
5474     $p->{content_model} = PCDATA_CONTENT_MODEL
5475     unless defined $p->{content_model};
5476     ## ISSUE: What is "the name of the element"? local name?
5477 wakaba 1.3
5478     $p->{inner_html_node} = [$node, $node_ln];
5479    
5480     ## Step 4
5481     my $root = $doc->create_element_ns
5482     ('http://www.w3.org/1999/xhtml', [undef, 'html']);
5483    
5484     ## Step 5 # MUST
5485     $doc->append_child ($root);
5486    
5487     ## Step 6 # MUST
5488     push @{$p->{open_elements}}, [$root, 'html'];
5489    
5490     undef $p->{head_element};
5491    
5492     ## Step 7 # MUST
5493     $p->_reset_insertion_mode;
5494    
5495     ## Step 8 # MUST
5496     my $anode = $node;
5497     AN: while (defined $anode) {
5498     if ($anode->node_type == 1) {
5499     my $nsuri = $anode->namespace_uri;
5500     if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
5501     if ($anode->local_name eq 'form') { ## TODO: case?
5502     $p->{form_element} = $anode;
5503     last AN;
5504     }
5505     }
5506     }
5507     $anode = $anode->parent_node;
5508     } # AN
5509    
5510     ## Step 3 # MUST
5511     ## Step 10 # MUST
5512     {
5513     my $self = $p;
5514     !!!next-token;
5515     }
5516     $p->_tree_construction_main;
5517    
5518     ## Step 11 # MUST
5519     my @cn = @{$node->child_nodes};
5520     for (@cn) {
5521     $node->remove_child ($_);
5522     }
5523     ## ISSUE: mutation events? read-only?
5524    
5525     ## Step 12 # MUST
5526     @cn = @{$root->child_nodes};
5527     for (@cn) {
5528 wakaba 1.14 $this_doc->adopt_node ($_);
5529 wakaba 1.3 $node->append_child ($_);
5530     }
5531 wakaba 1.14 ## ISSUE: mutation events?
5532 wakaba 1.3
5533     $p->_terminate_tree_constructor;
5534     } else {
5535     die "$0: |set_inner_html| is not defined for node of type $nt";
5536     }
5537     } # set_inner_html
5538    
5539     } # tree construction stage
5540 wakaba 1.1
5541 wakaba 1.63 package Whatpm::HTML::RestartParser;
5542     push our @ISA, 'Error';
5543    
5544 wakaba 1.1 1;
5545 wakaba 1.69 # $Date: 2008/02/17 12:18:06 $

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24