/[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.78 - (hide annotations) (download) (as text)
Mon Mar 3 11:56:18 2008 UTC (16 years, 8 months ago) by wakaba
Branch: MAIN
Changes since 1.77: +51 -12 lines
File MIME type: application/x-wais-source
++ whatpm/t/ChangeLog	3 Mar 2008 11:49:36 -0000
	* tokenizer-test-1.test: New test data are added to cover
	all possible cases.

	* HTML-tree.t: Support for test coverage.

2008-03-03  Wakaba  <wakaba@suika.fam.cx>

++ whatpm/Whatpm/ChangeLog	3 Mar 2008 11:56:09 -0000
	* HTML.pm.src (_tokenize_attempt_to_consume_an_entity): Checkpoints
	are set.  Cases that are unlikely reached are noted as so.

2008-03-03  Wakaba  <wakaba@suika.fam.cx>

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24