/[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.28 - (hide annotations) (download) (as text)
Mon Jun 25 00:14:40 2007 UTC (17 years, 4 months ago) by wakaba
Branch: MAIN
Changes since 1.27: +35 -5 lines
File MIME type: application/x-wais-source
++ whatpm/t/ChangeLog	24 Jun 2007 23:47:54 -0000
2007-06-25  Wakaba  <wakaba@suika.fam.cx>

	* tree-test-1.dat: Tests for end tag strings
	in CDATA elements are added.

++ whatpm/Whatpm/ChangeLog	25 Jun 2007 00:14:32 -0000
2007-06-25  Wakaba  <wakaba@suika.fam.cx>

	* HTML.pm.src: Late |<html>| parse error is implemented.

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24