/[suikacvs]/markup/html/whatpm/Whatpm/HTML.pm
Suika

Contents of /markup/html/whatpm/Whatpm/HTML.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.69 - (hide annotations) (download)
Sun Feb 17 12:18:06 2008 UTC (18 years, 5 months ago) by wakaba
Branch: MAIN
Changes since 1.68: +23 -3 lines
++ whatpm/t/ChangeLog	17 Feb 2008 12:18:01 -0000
	* content-model-1.dat, content-model-2.dat: Updated.

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

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

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

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

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

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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24