/[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.24 - (hide annotations) (download) (as text)
Sat Jun 23 16:42:43 2007 UTC (17 years, 4 months ago) by wakaba
Branch: MAIN
Changes since 1.23: +10 -6 lines
File MIME type: application/x-wais-source
++ whatpm/t/ChangeLog	23 Jun 2007 16:36:55 -0000
	* tree-test-1.dat: Some test results are changed
	since <base>, <link>, and <meta> in body
	are no longer appended to the head element pointer (HTML5
	revision 935).

	* content-model-2.dat: Tests for |scoped|
	attribute are added (HTML5 revision 938).

2007-06-24  Wakaba  <wakaba@suika.fam.cx>

++ whatpm/Whatpm/ChangeLog	23 Jun 2007 16:37:20 -0000
	* HTML.pm.src: HTML5 revision 935 (<base>, <link>, <meta>
	in body).

	* ContentChecker.pm: HTML5 revision 938 (scoped="").

2007-06-24  Wakaba  <wakaba@suika.fam.cx>

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24