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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.71 - (hide annotations) (download) (as text)
Sun Mar 2 03:39:41 2008 UTC (16 years, 8 months ago) by wakaba
Branch: MAIN
Changes since 1.70: +14 -11 lines
File MIME type: application/x-wais-source
++ whatpm/t/ChangeLog	2 Mar 2008 03:39:34 -0000
2008-03-02  Wakaba  <wakaba@suika.fam.cx>

	* content-model-1.dat: Test data for |<area>| are added.

	* content-model-2.dat: Test data for |<img ismap>| are added.

++ whatpm/Whatpm/ChangeLog	2 Mar 2008 03:36:45 -0000
2008-03-02  Wakaba  <wakaba@suika.fam.cx>

	* HTML.pm.src: s/local_name/manakai_local_name/g.

++ whatpm/Whatpm/HTML/ChangeLog	2 Mar 2008 03:28:10 -0000
2008-03-02  Wakaba  <wakaba@suika.fam.cx>

	* Serializer.pm (get_inner_html): Typo fixed.

++ whatpm/Whatpm/ContentChecker/ChangeLog	2 Mar 2008 03:37:30 -0000
2008-03-02  Wakaba  <wakaba@suika.fam.cx>

	* HTML.pm: |in_a_href| flag is not reset after the </a>.
	Raise an error if |area| is not a descendant of |map|.

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24