/[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.77 - (hide annotations) (download) (as text)
Mon Mar 3 10:20:19 2008 UTC (16 years, 8 months ago) by wakaba
Branch: MAIN
Changes since 1.76: +270 -4 lines
File MIME type: application/x-wais-source
++ whatpm/t/ChangeLog	3 Mar 2008 10:20:07 -0000
	* HTML-tokenizer.t: Support for test coverage caluclation.

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

++ whatpm/Whatpm/ChangeLog	3 Mar 2008 10:19:33 -0000
	* HTML.pm.src: Checkpoints for debugging are added.

	* mkhtmlparser.pl: Support for |!!!cp| syntax.

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

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24