/[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.74 - (hide annotations) (download)
Sun Mar 2 23:51:00 2008 UTC (18 years, 5 months ago) by wakaba
Branch: MAIN
Changes since 1.73: +3 -3 lines
++ whatpm/t/ChangeLog	2 Mar 2008 23:42:50 -0000
2008-03-03  Wakaba  <wakaba@suika.fam.cx>

	* tokenizer-test-1.test: New tests on trailing garbage
	after SYSTEM literal (HTML5 revision 1306) are added.

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24