/[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.69 - (show annotations) (download) (as text)
Sun Feb 17 12:39:32 2008 UTC (18 years, 7 months ago) by wakaba
Branch: MAIN
Changes since 1.68: +42 -2 lines
File MIME type: application/x-wais-source
++ whatpm/Whatpm/ChangeLog	17 Feb 2008 12:38:38 -0000
	* HTML.pm.src: |>| in public or system literal closes the DOCTYPE
	token (HTML5 revision 1225).

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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24