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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.65 by wakaba, Mon Nov 19 12:18:26 2007 UTC revision 1.121 by wakaba, Thu Mar 20 08:04:58 2008 UTC
# Line 8  use Error qw(:try); Line 8  use Error qw(:try);
8  ## doc.write ('');  ## doc.write ('');
9  ## alert (doc.compatMode);  ## alert (doc.compatMode);
10    
11  ## ISSUE: HTML5 revision 967 says that the encoding layer MUST NOT  ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)
12  ## strip BOM and the HTML layer MUST ignore it.  Whether we can do it  ## TODO: 1252 parse error (revision 1264)
13  ## is not yet clear.  ## TODO: 8859-11 = 874 (revision 1271)
 ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?  
 ## "{U+FEFF}..." in GB18030?  
14    
15  my $permitted_slash_tag_name = {  my $permitted_slash_tag_name = {
16    base => 1,    base => 1,
# Line 20  my $permitted_slash_tag_name = { Line 18  my $permitted_slash_tag_name = {
18    meta => 1,    meta => 1,
19    hr => 1,    hr => 1,
20    br => 1,    br => 1,
21    img=> 1,    img => 1,
22    embed => 1,    embed => 1,
23    param => 1,    param => 1,
24    area => 1,    area => 1,
# Line 76  my $special_category = { Line 74  my $special_category = {
74    textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,    textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,
75  };  };
76  my $scoping_category = {  my $scoping_category = {
77    button => 1, caption => 1, html => 1, marquee => 1, object => 1,    applet => 1, button => 1, caption => 1, html => 1, marquee => 1, object => 1,
78    table => 1, td => 1, th => 1,    table => 1, td => 1, th => 1,
79  };  };
80  my $formatting_category = {  my $formatting_category = {
# Line 110  sub parse_byte_string ($$$$;$) { Line 108  sub parse_byte_string ($$$$;$) {
108    $self->{change_encoding} = sub {    $self->{change_encoding} = sub {
109      my $self = shift;      my $self = shift;
110      my $charset = lc shift;      my $charset = lc shift;
111        my $token = shift;
112      ## TODO: if $charset is supported      ## TODO: if $charset is supported
113      ## TODO: normalize charset name      ## TODO: normalize charset name
114    
# Line 128  sub parse_byte_string ($$$$;$) { Line 127  sub parse_byte_string ($$$$;$) {
127      }      }
128    
129      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
130          ':'.$charset, level => 'w');          ':'.$charset, level => 'w', token => $token);
131    
132      ## Step 3      ## Step 3
133      # if (can) {      # if (can) {
# Line 155  sub parse_byte_string ($$$$;$) { Line 154  sub parse_byte_string ($$$$;$) {
154    return $return;    return $return;
155  } # parse_byte_string  } # parse_byte_string
156    
157    ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
158    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
159    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
160    ## because the core part of our HTML parser expects a string of character,
161    ## not a string of bytes or code units or anything which might contain a BOM.
162    ## Therefore, any parser interface that accepts a string of bytes,
163    ## such as |parse_byte_string| in this module, must ensure that it does
164    ## strip the BOM and never strip any ZWNBSP.
165    
166  *parse_char_string = \&parse_string;  *parse_char_string = \&parse_string;
167    
168  sub parse_string ($$$;$) {  sub parse_string ($$$;$) {
# Line 170  sub parse_string ($$$;$) { Line 178  sub parse_string ($$$;$) {
178        if defined $self->{input_encoding};        if defined $self->{input_encoding};
179    
180    my $i = 0;    my $i = 0;
181    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
182    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
183    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
184      my $self = shift;      my $self = shift;
185    
186      pop @{$self->{prev_input_character}};      pop @{$self->{prev_char}};
187      unshift @{$self->{prev_input_character}}, $self->{next_input_character};      unshift @{$self->{prev_char}}, $self->{next_char};
188    
189        $self->{next_char} = -1 and return if $i >= length $$s;
190        $self->{next_char} = ord substr $$s, $i++, 1;
191    
192      $self->{next_input_character} = -1 and return if $i >= length $$s;      ($self->{line_prev}, $self->{column_prev})
193      $self->{next_input_character} = ord substr $$s, $i++, 1;          = ($self->{line}, $self->{column});
194      $column++;      $self->{column}++;
195            
196      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
197        $line++;        $self->{line}++;
198        $column = 0;        $self->{column} = 0;
199      } elsif ($self->{next_input_character} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
200        $i++ if substr ($$s, $i, 1) eq "\x0A";        $i++ if substr ($$s, $i, 1) eq "\x0A";
201        $self->{next_input_character} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
202        $line++;        $self->{line}++;
203        $column = 0;        $self->{column} = 0;
204      } elsif ($self->{next_input_character} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
205        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
206      } elsif ($self->{next_input_character} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
207        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
208        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
209      }      }
210    };    };
211    $self->{prev_input_character} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
212    $self->{next_input_character} = -1;    $self->{next_char} = -1;
213    
214    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
215      my (%opt) = @_;      my (%opt) = @_;
216      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
217        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
218        warn "Parse error ($opt{type}) at line $line column $column\n";
219    };    };
220    $self->{parse_error} = sub {    $self->{parse_error} = sub {
221      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
222    };    };
223    
224    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 213  sub parse_string ($$$;$) { Line 226  sub parse_string ($$$;$) {
226    $self->_construct_tree;    $self->_construct_tree;
227    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
228    
229      delete $self->{parse_error}; # remove loop
230    
231    return $self->{document};    return $self->{document};
232  } # parse_string  } # parse_string
233    
234  sub new ($) {  sub new ($) {
235    my $class = shift;    my $class = shift;
236    my $self = bless {}, $class;    my $self = bless {}, $class;
237    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
238      $self->{next_input_character} = -1;      $self->{next_char} = -1;
239    };    };
240    $self->{parse_error} = sub {    $self->{parse_error} = sub {
241      #      #
# Line 279  sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUO Line 294  sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUO
294  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
295  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
296  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
297    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
298    
299  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
300  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 295  sub TABLE_IMS ()      { 0b1000000 } Line 311  sub TABLE_IMS ()      { 0b1000000 }
311  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
312  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
313  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
314    sub SELECT_IMS ()     { 0b10000000000 }
315    
316    ## NOTE: "initial" and "before html" insertion modes have no constants.
317    
318    ## NOTE: "after after body" insertion mode.
319  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
320    
321    ## NOTE: "after after frameset" insertion mode.
322  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
323    
324  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
325  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
326  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
# Line 311  sub IN_TABLE_IM () { TABLE_IMS } Line 334  sub IN_TABLE_IM () { TABLE_IMS }
334  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
335  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
336  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
337  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
338    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
339  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
340    
341  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
# Line 325  sub _initialize_tokenizer ($) { Line 349  sub _initialize_tokenizer ($) {
349    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
350    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
351    $self->{char} = [];    $self->{char} = [];
352    # $self->{next_input_character}    # $self->{next_char}
353    !!!next-input-character;    !!!next-input-character;
354    $self->{token} = [];    $self->{token} = [];
355    # $self->{escape}    # $self->{escape}
# Line 338  sub _initialize_tokenizer ($) { Line 362  sub _initialize_tokenizer ($) {
362  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
363  ##   ->{public_identifier} (DOCTYPE_TOKEN)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
364  ##   ->{system_identifier} (DOCTYPE_TOKEN)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
365  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
366  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
367    ##        ->{name}
368    ##        ->{value}
369    ##        ->{has_reference} == 1 or 0
370  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
371    
372  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
# Line 373  sub _get_next_token ($) { Line 400  sub _get_next_token ($) {
400    
401    A: {    A: {
402      if ($self->{state} == DATA_STATE) {      if ($self->{state} == DATA_STATE) {
403        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
404          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
405                not $self->{escape}) {
406              !!!cp (1);
407            $self->{state} = ENTITY_DATA_STATE;            $self->{state} = ENTITY_DATA_STATE;
408            !!!next-input-character;            !!!next-input-character;
409            redo A;            redo A;
410          } else {          } else {
411              !!!cp (2);
412            #            #
413          }          }
414        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
415          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
416            unless ($self->{escape}) {            unless ($self->{escape}) {
417              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_char}->[0] == 0x002D and # -
418                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_char}->[1] == 0x0021 and # !
419                  $self->{prev_input_character}->[2] == 0x003C) { # <                  $self->{prev_char}->[2] == 0x003C) { # <
420                  !!!cp (3);
421                $self->{escape} = 1;                $self->{escape} = 1;
422                } else {
423                  !!!cp (4);
424              }              }
425              } else {
426                !!!cp (5);
427            }            }
428          }          }
429                    
430          #          #
431        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x003C) { # <
432          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
433              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
434               not $self->{escape})) {               not $self->{escape})) {
435              !!!cp (6);
436            $self->{state} = TAG_OPEN_STATE;            $self->{state} = TAG_OPEN_STATE;
437            !!!next-input-character;            !!!next-input-character;
438            redo A;            redo A;
439          } else {          } else {
440              !!!cp (7);
441            #            #
442          }          }
443        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
444          if ($self->{escape} and          if ($self->{escape} and
445              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
446            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_char}->[0] == 0x002D and # -
447                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_char}->[1] == 0x002D) { # -
448                !!!cp (8);
449              delete $self->{escape};              delete $self->{escape};
450              } else {
451                !!!cp (9);
452            }            }
453            } else {
454              !!!cp (10);
455          }          }
456                    
457          #          #
458        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
459          !!!emit ({type => END_OF_FILE_TOKEN});          !!!cp (11);
460            !!!emit ({type => END_OF_FILE_TOKEN,
461                      line => $self->{line}, column => $self->{column}});
462          last A; ## TODO: ok?          last A; ## TODO: ok?
463          } else {
464            !!!cp (12);
465        }        }
466        # Anything else        # Anything else
467        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
468                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
469                       line => $self->{line}, column => $self->{column},
470                      };
471        ## Stay in the data state        ## Stay in the data state
472        !!!next-input-character;        !!!next-input-character;
473    
# Line 428  sub _get_next_token ($) { Line 476  sub _get_next_token ($) {
476        redo A;        redo A;
477      } elsif ($self->{state} == ENTITY_DATA_STATE) {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
478        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
479    
480          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
481                
482        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
483    
484        $self->{state} = DATA_STATE;        $self->{state} = DATA_STATE;
485        # next-input-character is already done        # next-input-character is already done
486    
487        unless (defined $token) {        unless (defined $token) {
488          !!!emit ({type => CHARACTER_TOKEN, data => '&'});          !!!cp (13);
489            !!!emit ({type => CHARACTER_TOKEN, data => '&',
490                      line => $l, column => $c,
491                     });
492        } else {        } else {
493            !!!cp (14);
494          !!!emit ($token);          !!!emit ($token);
495        }        }
496    
497        redo A;        redo A;
498      } elsif ($self->{state} == TAG_OPEN_STATE) {      } elsif ($self->{state} == TAG_OPEN_STATE) {
499        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
500          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_char} == 0x002F) { # /
501              !!!cp (15);
502            !!!next-input-character;            !!!next-input-character;
503            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
504            redo A;            redo A;
505          } else {          } else {
506              !!!cp (16);
507            ## reconsume            ## reconsume
508            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
509    
510            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
511                        line => $self->{line_prev},
512                        column => $self->{column_prev},
513                       });
514    
515            redo A;            redo A;
516          }          }
517        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
518          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
519              !!!cp (17);
520            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
521            !!!next-input-character;            !!!next-input-character;
522            redo A;            redo A;
523          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
524              !!!cp (18);
525            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
526            !!!next-input-character;            !!!next-input-character;
527            redo A;            redo A;
528          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
529                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
530              !!!cp (19);
531            $self->{current_token}            $self->{current_token}
532              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
533                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
534                   line => $self->{line_prev},
535                   column => $self->{column_prev}};
536            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
537            !!!next-input-character;            !!!next-input-character;
538            redo A;            redo A;
539          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
540                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
541              !!!cp (20);
542            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
543                              tag_name => chr ($self->{next_input_character})};                                      tag_name => chr ($self->{next_char}),
544                                        line => $self->{line_prev},
545                                        column => $self->{column_prev}};
546            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
547            !!!next-input-character;            !!!next-input-character;
548            redo A;            redo A;
549          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
550            !!!parse-error (type => 'empty start tag');            !!!cp (21);
551              !!!parse-error (type => 'empty start tag',
552                              line => $self->{line_prev},
553                              column => $self->{column_prev});
554            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
555            !!!next-input-character;            !!!next-input-character;
556    
557            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
558                        line => $self->{line_prev},
559                        column => $self->{column_prev},
560                       });
561    
562            redo A;            redo A;
563          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
564            !!!parse-error (type => 'pio');            !!!cp (22);
565              !!!parse-error (type => 'pio',
566                              line => $self->{line_prev},
567                              column => $self->{column_prev});
568            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
569            ## $self->{next_input_character} is intentionally left as is            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
570                                        line => $self->{line_prev},
571                                        column => $self->{column_prev},
572                                       };
573              ## $self->{next_char} is intentionally left as is
574            redo A;            redo A;
575          } else {          } else {
576              !!!cp (23);
577            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
578            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
579            ## reconsume            ## reconsume
580    
581            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
582                        line => $self->{line_prev},
583                        column => $self->{column_prev},
584                       });
585    
586            redo A;            redo A;
587          }          }
# Line 505  sub _get_next_token ($) { Line 589  sub _get_next_token ($) {
589          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
590        }        }
591      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
592          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
593        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
594          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
595    
596            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
597            my @next_char;            my @next_char;
598            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
599              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
600              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
601              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
602              if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {              if ($self->{next_char} == $c or $self->{next_char} == $C) {
603                  !!!cp (24);
604                !!!next-input-character;                !!!next-input-character;
605                next TAGNAME;                next TAGNAME;
606              } else {              } else {
607                $self->{next_input_character} = shift @next_char; # reconsume                !!!cp (25);
608                  $self->{next_char} = shift @next_char; # reconsume
609                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
610                $self->{state} = DATA_STATE;                $self->{state} = DATA_STATE;
611    
612                !!!emit ({type => CHARACTER_TOKEN, data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
613                            line => $l, column => $c,
614                           });
615        
616                redo A;                redo A;
617              }              }
618            }            }
619            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
620                
621            unless ($self->{next_input_character} == 0x0009 or # HT            unless ($self->{next_char} == 0x0009 or # HT
622                    $self->{next_input_character} == 0x000A or # LF                    $self->{next_char} == 0x000A or # LF
623                    $self->{next_input_character} == 0x000B or # VT                    $self->{next_char} == 0x000B or # VT
624                    $self->{next_input_character} == 0x000C or # FF                    $self->{next_char} == 0x000C or # FF
625                    $self->{next_input_character} == 0x0020 or # SP                    $self->{next_char} == 0x0020 or # SP
626                    $self->{next_input_character} == 0x003E or # >                    $self->{next_char} == 0x003E or # >
627                    $self->{next_input_character} == 0x002F or # /                    $self->{next_char} == 0x002F or # /
628                    $self->{next_input_character} == -1) {                    $self->{next_char} == -1) {
629              $self->{next_input_character} = shift @next_char; # reconsume              !!!cp (26);
630                $self->{next_char} = shift @next_char; # reconsume
631              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
632              $self->{state} = DATA_STATE;              $self->{state} = DATA_STATE;
633              !!!emit ({type => CHARACTER_TOKEN, data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
634                          line => $l, column => $c,
635                         });
636              redo A;              redo A;
637            } else {            } else {
638              $self->{next_input_character} = shift @next_char;              !!!cp (27);
639                $self->{next_char} = shift @next_char;
640              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
641              # and consume...              # and consume...
642            }            }
643          } else {          } else {
644            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
645              !!!cp (28);
646            # next-input-character is already done            # next-input-character is already done
647            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
648            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
649                        line => $l, column => $c,
650                       });
651            redo A;            redo A;
652          }          }
653        }        }
654                
655        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
656            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
657          $self->{current_token} = {type => END_TAG_TOKEN,          !!!cp (29);
658                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
659                = {type => END_TAG_TOKEN,
660                   tag_name => chr ($self->{next_char} + 0x0020),
661                   line => $l, column => $c};
662          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
663          !!!next-input-character;          !!!next-input-character;
664          redo A;          redo A;
665        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_char} and
666                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
667            !!!cp (30);
668          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
669                            tag_name => chr ($self->{next_input_character})};                                    tag_name => chr ($self->{next_char}),
670                                      line => $l, column => $c};
671          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
672          !!!next-input-character;          !!!next-input-character;
673          redo A;          redo A;
674        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
675          !!!parse-error (type => 'empty end tag');          !!!cp (31);
676            !!!parse-error (type => 'empty end tag',
677                            line => $self->{line_prev}, ## "<" in "</>"
678                            column => $self->{column_prev} - 1);
679          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
680          !!!next-input-character;          !!!next-input-character;
681          redo A;          redo A;
682        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
683            !!!cp (32);
684          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
685          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
686          # reconsume          # reconsume
687    
688          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
689                      line => $l, column => $c,
690                     });
691    
692          redo A;          redo A;
693        } else {        } else {
694            !!!cp (33);
695          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
696          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
697          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
698                                      line => $self->{line_prev}, # "<" of "</"
699                                      column => $self->{column_prev} - 1,
700                                     };
701            ## $self->{next_char} is intentionally left as is
702          redo A;          redo A;
703        }        }
704      } elsif ($self->{state} == TAG_NAME_STATE) {      } elsif ($self->{state} == TAG_NAME_STATE) {
705        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
706            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
707            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
708            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
709            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
710            !!!cp (34);
711          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
712          !!!next-input-character;          !!!next-input-character;
713          redo A;          redo A;
714        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
715          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
716            $self->{current_token}->{first_start_tag}            !!!cp (35);
               = not defined $self->{last_emitted_start_tag_name};  
717            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
718          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
719            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
720            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
721              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
722            }            #  !!! cp (36);
723              #  !!! parse-error (type => 'end tag attribute');
724              #} else {
725                !!!cp (37);
726              #}
727          } else {          } else {
728            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
729          }          }
# Line 616  sub _get_next_token ($) { Line 733  sub _get_next_token ($) {
733          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
734    
735          redo A;          redo A;
736        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
737                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
738          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
739            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
740            # start tag or end tag            # start tag or end tag
741          ## Stay in this state          ## Stay in this state
742          !!!next-input-character;          !!!next-input-character;
743          redo A;          redo A;
744        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
745          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
746          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
747            $self->{current_token}->{first_start_tag}            !!!cp (39);
               = not defined $self->{last_emitted_start_tag_name};  
748            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
749          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
750            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
751            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
752              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
753            }            #  !!! cp (40);
754              #  !!! parse-error (type => 'end tag attribute');
755              #} else {
756                !!!cp (41);
757              #}
758          } else {          } else {
759            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
760          }          }
# Line 643  sub _get_next_token ($) { Line 764  sub _get_next_token ($) {
764          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
765    
766          redo A;          redo A;
767        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
768          !!!next-input-character;          !!!next-input-character;
769          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
770              $self->{current_token}->{type} == START_TAG_TOKEN and              $self->{current_token}->{type} == START_TAG_TOKEN and
771              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
772            # permitted slash            # permitted slash
773              !!!cp (42);
774            #            #
775          } else {          } else {
776              !!!cp (43);
777            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
778          }          }
779          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
780          # next-input-character is already done          # next-input-character is already done
781          redo A;          redo A;
782        } else {        } else {
783          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
784            $self->{current_token}->{tag_name} .= chr $self->{next_char};
785            # start tag or end tag            # start tag or end tag
786          ## Stay in the state          ## Stay in the state
787          !!!next-input-character;          !!!next-input-character;
788          redo A;          redo A;
789        }        }
790      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
791        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
792            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
793            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
794            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
795            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
796            !!!cp (45);
797          ## Stay in the state          ## Stay in the state
798          !!!next-input-character;          !!!next-input-character;
799          redo A;          redo A;
800        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
801          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
802            $self->{current_token}->{first_start_tag}            !!!cp (46);
               = not defined $self->{last_emitted_start_tag_name};  
803            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
804          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
805            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
806            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
807                !!!cp (47);
808              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
809              } else {
810                !!!cp (48);
811            }            }
812          } else {          } else {
813            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 691  sub _get_next_token ($) { Line 818  sub _get_next_token ($) {
818          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
819    
820          redo A;          redo A;
821        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
822                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
823          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
824                                value => ''};          $self->{current_attribute}
825                = {name => chr ($self->{next_char} + 0x0020),
826                   value => '',
827                   line => $self->{line}, column => $self->{column}};
828          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
829          !!!next-input-character;          !!!next-input-character;
830          redo A;          redo A;
831        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
832          !!!next-input-character;          !!!next-input-character;
833          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
834              $self->{current_token}->{type} == START_TAG_TOKEN and              $self->{current_token}->{type} == START_TAG_TOKEN and
835              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
836            # permitted slash            # permitted slash
837              !!!cp (50);
838            #            #
839          } else {          } else {
840              !!!cp (51);
841            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
842          }          }
843          ## Stay in the state          ## Stay in the state
844          # next-input-character is already done          # next-input-character is already done
845          redo A;          redo A;
846        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
847          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
848          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
849            $self->{current_token}->{first_start_tag}            !!!cp (52);
               = not defined $self->{last_emitted_start_tag_name};  
850            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
851          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
852            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
853            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
854                !!!cp (53);
855              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
856              } else {
857                !!!cp (54);
858            }            }
859          } else {          } else {
860            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 732  sub _get_next_token ($) { Line 866  sub _get_next_token ($) {
866    
867          redo A;          redo A;
868        } else {        } else {
869          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
870                                value => ''};               0x0022 => 1, # "
871                 0x0027 => 1, # '
872                 0x003D => 1, # =
873                }->{$self->{next_char}}) {
874              !!!cp (55);
875              !!!parse-error (type => 'bad attribute name');
876            } else {
877              !!!cp (56);
878            }
879            $self->{current_attribute}
880                = {name => chr ($self->{next_char}),
881                   value => '',
882                   line => $self->{line}, column => $self->{column}};
883          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
884          !!!next-input-character;          !!!next-input-character;
885          redo A;          redo A;
# Line 742  sub _get_next_token ($) { Line 888  sub _get_next_token ($) {
888        my $before_leave = sub {        my $before_leave = sub {
889          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
890              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
891            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!cp (57);
892              !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
893            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
894          } else {          } else {
895              !!!cp (58);
896            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
897              = $self->{current_attribute};              = $self->{current_attribute};
898          }          }
899        }; # $before_leave        }; # $before_leave
900    
901        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
902            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
903            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
904            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
905            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
906            !!!cp (59);
907          $before_leave->();          $before_leave->();
908          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
909          !!!next-input-character;          !!!next-input-character;
910          redo A;          redo A;
911        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
912            !!!cp (60);
913          $before_leave->();          $before_leave->();
914          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
915          !!!next-input-character;          !!!next-input-character;
916          redo A;          redo A;
917        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
918          $before_leave->();          $before_leave->();
919          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
920            $self->{current_token}->{first_start_tag}            !!!cp (61);
               = not defined $self->{last_emitted_start_tag_name};  
921            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
922          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
923              !!!cp (62);
924            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
925            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
926              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 784  sub _get_next_token ($) { Line 934  sub _get_next_token ($) {
934          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
935    
936          redo A;          redo A;
937        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
938                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
939          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
940            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
941          ## Stay in the state          ## Stay in the state
942          !!!next-input-character;          !!!next-input-character;
943          redo A;          redo A;
944        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
945          $before_leave->();          $before_leave->();
946          !!!next-input-character;          !!!next-input-character;
947          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
948              $self->{current_token}->{type} == START_TAG_TOKEN and              $self->{current_token}->{type} == START_TAG_TOKEN and
949              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
950            # permitted slash            # permitted slash
951              !!!cp (64);
952            #            #
953          } else {          } else {
954              !!!cp (65);
955            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
956          }          }
957          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
958          # next-input-character is already done          # next-input-character is already done
959          redo A;          redo A;
960        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
961          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
962          $before_leave->();          $before_leave->();
963          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
964            $self->{current_token}->{first_start_tag}            !!!cp (66);
               = not defined $self->{last_emitted_start_tag_name};  
965            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
966          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
967            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
968            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
969                !!!cp (67);
970              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
971              } else {
972                ## NOTE: This state should never be reached.
973                !!!cp (68);
974            }            }
975          } else {          } else {
976            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 826  sub _get_next_token ($) { Line 982  sub _get_next_token ($) {
982    
983          redo A;          redo A;
984        } else {        } else {
985          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
986                $self->{next_char} == 0x0027) { # '
987              !!!cp (69);
988              !!!parse-error (type => 'bad attribute name');
989            } else {
990              !!!cp (70);
991            }
992            $self->{current_attribute}->{name} .= chr ($self->{next_char});
993          ## Stay in the state          ## Stay in the state
994          !!!next-input-character;          !!!next-input-character;
995          redo A;          redo A;
996        }        }
997      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
998        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
999            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1000            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1001            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1002            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1003            !!!cp (71);
1004          ## Stay in the state          ## Stay in the state
1005          !!!next-input-character;          !!!next-input-character;
1006          redo A;          redo A;
1007        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1008            !!!cp (72);
1009          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1010          !!!next-input-character;          !!!next-input-character;
1011          redo A;          redo A;
1012        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1013          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1014            $self->{current_token}->{first_start_tag}            !!!cp (73);
               = not defined $self->{last_emitted_start_tag_name};  
1015            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1016          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1017            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1018            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1019                !!!cp (74);
1020              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1021              } else {
1022                ## NOTE: This state should never be reached.
1023                !!!cp (75);
1024            }            }
1025          } else {          } else {
1026            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 863  sub _get_next_token ($) { Line 1031  sub _get_next_token ($) {
1031          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1032    
1033          redo A;          redo A;
1034        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1035                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1036          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1037                                value => ''};          $self->{current_attribute}
1038                = {name => chr ($self->{next_char} + 0x0020),
1039                   value => '',
1040                   line => $self->{line}, column => $self->{column}};
1041          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1042          !!!next-input-character;          !!!next-input-character;
1043          redo A;          redo A;
1044        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1045          !!!next-input-character;          !!!next-input-character;
1046          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
1047              $self->{current_token}->{type} == START_TAG_TOKEN and              $self->{current_token}->{type} == START_TAG_TOKEN and
1048              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
1049            # permitted slash            # permitted slash
1050              !!!cp (77);
1051            #            #
1052          } else {          } else {
1053              !!!cp (78);
1054            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
1055            ## TODO: Different error type for <aa / bb> than <aa/>            ## TODO: Different error type for <aa / bb> than <aa/>
1056          }          }
1057          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1058          # next-input-character is already done          # next-input-character is already done
1059          redo A;          redo A;
1060        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1061          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1062          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1063            $self->{current_token}->{first_start_tag}            !!!cp (79);
               = not defined $self->{last_emitted_start_tag_name};  
1064            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1065          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1066            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1067            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1068                !!!cp (80);
1069              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1070              } else {
1071                ## NOTE: This state should never be reached.
1072                !!!cp (81);
1073            }            }
1074          } else {          } else {
1075            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 905  sub _get_next_token ($) { Line 1081  sub _get_next_token ($) {
1081    
1082          redo A;          redo A;
1083        } else {        } else {
1084          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          !!!cp (82);
1085                                value => ''};          $self->{current_attribute}
1086                = {name => chr ($self->{next_char}),
1087                   value => '',
1088                   line => $self->{line}, column => $self->{column}};
1089          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1090          !!!next-input-character;          !!!next-input-character;
1091          redo A;                  redo A;        
1092        }        }
1093      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1094        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1095            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1096            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1097            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1098            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1099            !!!cp (83);
1100          ## Stay in the state          ## Stay in the state
1101          !!!next-input-character;          !!!next-input-character;
1102          redo A;          redo A;
1103        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1104            !!!cp (84);
1105          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1106          !!!next-input-character;          !!!next-input-character;
1107          redo A;          redo A;
1108        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1109            !!!cp (85);
1110          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1111          ## reconsume          ## reconsume
1112          redo A;          redo A;
1113        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1114            !!!cp (86);
1115          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1116          !!!next-input-character;          !!!next-input-character;
1117          redo A;          redo A;
1118        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1119          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1120            $self->{current_token}->{first_start_tag}            !!!cp (87);
               = not defined $self->{last_emitted_start_tag_name};  
1121            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1122          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1123            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1124            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1125                !!!cp (88);
1126              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1127              } else {
1128                ## NOTE: This state should never be reached.
1129                !!!cp (89);
1130            }            }
1131          } else {          } else {
1132            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 951  sub _get_next_token ($) { Line 1137  sub _get_next_token ($) {
1137          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1138    
1139          redo A;          redo A;
1140        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1141          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1142          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1143            $self->{current_token}->{first_start_tag}            !!!cp (90);
               = not defined $self->{last_emitted_start_tag_name};  
1144            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1145          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1146            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1147            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1148                !!!cp (91);
1149              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1150              } else {
1151                ## NOTE: This state should never be reached.
1152                !!!cp (92);
1153            }            }
1154          } else {          } else {
1155            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 972  sub _get_next_token ($) { Line 1161  sub _get_next_token ($) {
1161    
1162          redo A;          redo A;
1163        } else {        } else {
1164          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1165              !!!cp (93);
1166              !!!parse-error (type => 'bad attribute value');
1167            } else {
1168              !!!cp (94);
1169            }
1170            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1171          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1172          !!!next-input-character;          !!!next-input-character;
1173          redo A;          redo A;
1174        }        }
1175      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1176        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1177          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          !!!cp (95);
1178            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1179          !!!next-input-character;          !!!next-input-character;
1180          redo A;          redo A;
1181        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1182            !!!cp (96);
1183          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1184          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1185          !!!next-input-character;          !!!next-input-character;
1186          redo A;          redo A;
1187        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1188          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1189          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1190            $self->{current_token}->{first_start_tag}            !!!cp (97);
               = not defined $self->{last_emitted_start_tag_name};  
1191            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1192          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1193            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1194            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1195                !!!cp (98);
1196              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1197              } else {
1198                ## NOTE: This state should never be reached.
1199                !!!cp (99);
1200            }            }
1201          } else {          } else {
1202            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1008  sub _get_next_token ($) { Line 1208  sub _get_next_token ($) {
1208    
1209          redo A;          redo A;
1210        } else {        } else {
1211          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1212            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1213          ## Stay in the state          ## Stay in the state
1214          !!!next-input-character;          !!!next-input-character;
1215          redo A;          redo A;
1216        }        }
1217      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1218        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1219          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          !!!cp (101);
1220            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1221          !!!next-input-character;          !!!next-input-character;
1222          redo A;          redo A;
1223        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1224            !!!cp (102);
1225          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1226          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1227          !!!next-input-character;          !!!next-input-character;
1228          redo A;          redo A;
1229        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1230          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1231          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1232            $self->{current_token}->{first_start_tag}            !!!cp (103);
               = not defined $self->{last_emitted_start_tag_name};  
1233            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1234          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1235            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1236            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1237                !!!cp (104);
1238              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1239              } else {
1240                ## NOTE: This state should never be reached.
1241                !!!cp (105);
1242            }            }
1243          } else {          } else {
1244            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1044  sub _get_next_token ($) { Line 1250  sub _get_next_token ($) {
1250    
1251          redo A;          redo A;
1252        } else {        } else {
1253          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1254            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1255          ## Stay in the state          ## Stay in the state
1256          !!!next-input-character;          !!!next-input-character;
1257          redo A;          redo A;
1258        }        }
1259      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1260        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1261            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1262            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1263            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1264            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1265            !!!cp (107);
1266          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1267          !!!next-input-character;          !!!next-input-character;
1268          redo A;          redo A;
1269        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1270            !!!cp (108);
1271          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1272          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1273          !!!next-input-character;          !!!next-input-character;
1274          redo A;          redo A;
1275        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1276          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1277            $self->{current_token}->{first_start_tag}            !!!cp (109);
               = not defined $self->{last_emitted_start_tag_name};  
1278            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1279          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1280            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1281            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1282                !!!cp (110);
1283              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1284              } else {
1285                ## NOTE: This state should never be reached.
1286                !!!cp (111);
1287            }            }
1288          } else {          } else {
1289            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1082  sub _get_next_token ($) { Line 1294  sub _get_next_token ($) {
1294          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1295    
1296          redo A;          redo A;
1297        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1298          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1299          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1300            $self->{current_token}->{first_start_tag}            !!!cp (112);
               = not defined $self->{last_emitted_start_tag_name};  
1301            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1302          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1303            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1304            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1305                !!!cp (113);
1306              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1307              } else {
1308                ## NOTE: This state should never be reached.
1309                !!!cp (114);
1310            }            }
1311          } else {          } else {
1312            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1103  sub _get_next_token ($) { Line 1318  sub _get_next_token ($) {
1318    
1319          redo A;          redo A;
1320        } else {        } else {
1321          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1322                 0x0022 => 1, # "
1323                 0x0027 => 1, # '
1324                 0x003D => 1, # =
1325                }->{$self->{next_char}}) {
1326              !!!cp (115);
1327              !!!parse-error (type => 'bad attribute value');
1328            } else {
1329              !!!cp (116);
1330            }
1331            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1332          ## Stay in the state          ## Stay in the state
1333          !!!next-input-character;          !!!next-input-character;
1334          redo A;          redo A;
1335        }        }
1336      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1337        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        my $token = $self->_tokenize_attempt_to_consume_an_entity
1338              (1,
1339               $self->{last_attribute_value_state}
1340                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1341               $self->{last_attribute_value_state}
1342                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1343               -1);
1344    
1345        unless (defined $token) {        unless (defined $token) {
1346            !!!cp (117);
1347          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1348        } else {        } else {
1349            !!!cp (118);
1350          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1351            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1352          ## ISSUE: spec says "append the returned character token to the current attribute's value"          ## ISSUE: spec says "append the returned character token to the current attribute's value"
1353        }        }
1354    
1355        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1356        # next-input-character is already done        # next-input-character is already done
1357        redo A;        redo A;
1358        } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1359          if ($self->{next_char} == 0x0009 or # HT
1360              $self->{next_char} == 0x000A or # LF
1361              $self->{next_char} == 0x000B or # VT
1362              $self->{next_char} == 0x000C or # FF
1363              $self->{next_char} == 0x0020) { # SP
1364            !!!cp (118);
1365            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1366            !!!next-input-character;
1367            redo A;
1368          } elsif ($self->{next_char} == 0x003E) { # >
1369            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1370              !!!cp (119);
1371              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1372            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1373              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1374              if ($self->{current_token}->{attributes}) {
1375                !!!cp (120);
1376                !!!parse-error (type => 'end tag attribute');
1377              } else {
1378                ## NOTE: This state should never be reached.
1379                !!!cp (121);
1380              }
1381            } else {
1382              die "$0: $self->{current_token}->{type}: Unknown token type";
1383            }
1384            $self->{state} = DATA_STATE;
1385            !!!next-input-character;
1386    
1387            !!!emit ($self->{current_token}); # start tag or end tag
1388    
1389            redo A;
1390          } elsif ($self->{next_char} == 0x002F) { # /
1391            !!!next-input-character;
1392            if ($self->{next_char} == 0x003E and # >
1393                $self->{current_token}->{type} == START_TAG_TOKEN and
1394                $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
1395              # permitted slash
1396              !!!cp (122);
1397              #
1398            } else {
1399              !!!cp (123);
1400              !!!parse-error (type => 'nestc');
1401            }
1402            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1403            # next-input-character is already done
1404            redo A;
1405          } else {
1406            !!!cp (124);
1407            !!!parse-error (type => 'no space between attributes');
1408            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1409            ## reconsume
1410            redo A;
1411          }
1412      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1413        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1414                
1415        my $token = {type => COMMENT_TOKEN, data => ''};        ## NOTE: Set by the previous state
1416          #my $token = {type => COMMENT_TOKEN, data => ''};
1417    
1418        BC: {        BC: {
1419          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1420              !!!cp (124);
1421            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1422            !!!next-input-character;            !!!next-input-character;
1423    
1424            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1425    
1426            redo A;            redo A;
1427          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1428              !!!cp (125);
1429            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1430            ## reconsume            ## reconsume
1431    
1432            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1433    
1434            redo A;            redo A;
1435          } else {          } else {
1436            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1437              $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1438            !!!next-input-character;            !!!next-input-character;
1439            redo BC;            redo BC;
1440          }          }
1441        } # BC        } # BC
1442    
1443          die "$0: _get_next_token: unexpected case [BC]";
1444      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1445        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1446    
1447          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1448    
1449        my @next_char;        my @next_char;
1450        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
1451                
1452        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1453          !!!next-input-character;          !!!next-input-character;
1454          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1455          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1456            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};            !!!cp (127);
1457              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1458                                        line => $l, column => $c,
1459                                       };
1460            $self->{state} = COMMENT_START_STATE;            $self->{state} = COMMENT_START_STATE;
1461            !!!next-input-character;            !!!next-input-character;
1462            redo A;            redo A;
1463            } else {
1464              !!!cp (128);
1465          }          }
1466        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
1467                 $self->{next_input_character} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
1468          !!!next-input-character;          !!!next-input-character;
1469          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1470          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x004F or # O
1471              $self->{next_input_character} == 0x006F) { # o              $self->{next_char} == 0x006F) { # o
1472            !!!next-input-character;            !!!next-input-character;
1473            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1474            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0043 or # C
1475                $self->{next_input_character} == 0x0063) { # c                $self->{next_char} == 0x0063) { # c
1476              !!!next-input-character;              !!!next-input-character;
1477              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1478              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
1479                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
1480                !!!next-input-character;                !!!next-input-character;
1481                push @next_char, $self->{next_input_character};                push @next_char, $self->{next_char};
1482                if ($self->{next_input_character} == 0x0059 or # Y                if ($self->{next_char} == 0x0059 or # Y
1483                    $self->{next_input_character} == 0x0079) { # y                    $self->{next_char} == 0x0079) { # y
1484                  !!!next-input-character;                  !!!next-input-character;
1485                  push @next_char, $self->{next_input_character};                  push @next_char, $self->{next_char};
1486                  if ($self->{next_input_character} == 0x0050 or # P                  if ($self->{next_char} == 0x0050 or # P
1487                      $self->{next_input_character} == 0x0070) { # p                      $self->{next_char} == 0x0070) { # p
1488                    !!!next-input-character;                    !!!next-input-character;
1489                    push @next_char, $self->{next_input_character};                    push @next_char, $self->{next_char};
1490                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_char} == 0x0045 or # E
1491                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_char} == 0x0065) { # e
1492                      ## ISSUE: What a stupid code this is!                      !!!cp (129);
1493                        ## TODO: What a stupid code this is!
1494                      $self->{state} = DOCTYPE_STATE;                      $self->{state} = DOCTYPE_STATE;
1495                        $self->{current_token} = {type => DOCTYPE_TOKEN,
1496                                                  quirks => 1,
1497                                                  line => $l, column => $c,
1498                                                 };
1499                      !!!next-input-character;                      !!!next-input-character;
1500                      redo A;                      redo A;
1501                      } else {
1502                        !!!cp (130);
1503                    }                    }
1504                    } else {
1505                      !!!cp (131);
1506                  }                  }
1507                  } else {
1508                    !!!cp (132);
1509                }                }
1510                } else {
1511                  !!!cp (133);
1512              }              }
1513              } else {
1514                !!!cp (134);
1515            }            }
1516            } else {
1517              !!!cp (135);
1518          }          }
1519          } else {
1520            !!!cp (136);
1521        }        }
1522    
1523        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
1524        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
1525        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1526        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
1527          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1528                                    line => $l, column => $c,
1529                                   };
1530        redo A;        redo A;
1531                
1532        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1533        ## 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?        ## 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?
1534      } elsif ($self->{state} == COMMENT_START_STATE) {      } elsif ($self->{state} == COMMENT_START_STATE) {
1535        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1536            !!!cp (137);
1537          $self->{state} = COMMENT_START_DASH_STATE;          $self->{state} = COMMENT_START_DASH_STATE;
1538          !!!next-input-character;          !!!next-input-character;
1539          redo A;          redo A;
1540        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1541            !!!cp (138);
1542          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1543          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1544          !!!next-input-character;          !!!next-input-character;
# Line 1221  sub _get_next_token ($) { Line 1546  sub _get_next_token ($) {
1546          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1547    
1548          redo A;          redo A;
1549        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1550            !!!cp (139);
1551          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1552          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1553          ## reconsume          ## reconsume
# Line 1230  sub _get_next_token ($) { Line 1556  sub _get_next_token ($) {
1556    
1557          redo A;          redo A;
1558        } else {        } else {
1559            !!!cp (140);
1560          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1561              .= chr ($self->{next_input_character});              .= chr ($self->{next_char});
1562          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
1563          !!!next-input-character;          !!!next-input-character;
1564          redo A;          redo A;
1565        }        }
1566      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1567        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1568            !!!cp (141);
1569          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
1570          !!!next-input-character;          !!!next-input-character;
1571          redo A;          redo A;
1572        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1573            !!!cp (142);
1574          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1575          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1576          !!!next-input-character;          !!!next-input-character;
# Line 1249  sub _get_next_token ($) { Line 1578  sub _get_next_token ($) {
1578          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1579    
1580          redo A;          redo A;
1581        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1582            !!!cp (143);
1583          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1584          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1585          ## reconsume          ## reconsume
# Line 1258  sub _get_next_token ($) { Line 1588  sub _get_next_token ($) {
1588    
1589          redo A;          redo A;
1590        } else {        } else {
1591            !!!cp (144);
1592          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1593              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_char});
1594          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
1595          !!!next-input-character;          !!!next-input-character;
1596          redo A;          redo A;
1597        }        }
1598      } elsif ($self->{state} == COMMENT_STATE) {      } elsif ($self->{state} == COMMENT_STATE) {
1599        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1600            !!!cp (145);
1601          $self->{state} = COMMENT_END_DASH_STATE;          $self->{state} = COMMENT_END_DASH_STATE;
1602          !!!next-input-character;          !!!next-input-character;
1603          redo A;          redo A;
1604        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1605            !!!cp (146);
1606          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1607          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1608          ## reconsume          ## reconsume
# Line 1278  sub _get_next_token ($) { Line 1611  sub _get_next_token ($) {
1611    
1612          redo A;          redo A;
1613        } else {        } else {
1614          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
1615            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1616          ## Stay in the state          ## Stay in the state
1617          !!!next-input-character;          !!!next-input-character;
1618          redo A;          redo A;
1619        }        }
1620      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1621        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1622            !!!cp (148);
1623          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
1624          !!!next-input-character;          !!!next-input-character;
1625          redo A;          redo A;
1626        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1627            !!!cp (149);
1628          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1629          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1630          ## reconsume          ## reconsume
# Line 1297  sub _get_next_token ($) { Line 1633  sub _get_next_token ($) {
1633    
1634          redo A;          redo A;
1635        } else {        } else {
1636          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
1637            $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
1638          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
1639          !!!next-input-character;          !!!next-input-character;
1640          redo A;          redo A;
1641        }        }
1642      } elsif ($self->{state} == COMMENT_END_STATE) {      } elsif ($self->{state} == COMMENT_END_STATE) {
1643        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
1644            !!!cp (151);
1645          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1646          !!!next-input-character;          !!!next-input-character;
1647    
1648          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1649    
1650          redo A;          redo A;
1651        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
1652          !!!parse-error (type => 'dash in comment');          !!!cp (152);
1653            !!!parse-error (type => 'dash in comment',
1654                            line => $self->{line_prev},
1655                            column => $self->{column_prev});
1656          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
1657          ## Stay in the state          ## Stay in the state
1658          !!!next-input-character;          !!!next-input-character;
1659          redo A;          redo A;
1660        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1661            !!!cp (153);
1662          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1663          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1664          ## reconsume          ## reconsume
# Line 1325  sub _get_next_token ($) { Line 1667  sub _get_next_token ($) {
1667    
1668          redo A;          redo A;
1669        } else {        } else {
1670          !!!parse-error (type => 'dash in comment');          !!!cp (154);
1671          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
1672                            line => $self->{line_prev},
1673                            column => $self->{column_prev});
1674            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
1675          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
1676          !!!next-input-character;          !!!next-input-character;
1677          redo A;          redo A;
1678        }        }
1679      } elsif ($self->{state} == DOCTYPE_STATE) {      } elsif ($self->{state} == DOCTYPE_STATE) {
1680        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1681            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1682            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1683            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1684            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1685            !!!cp (155);
1686          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1687          !!!next-input-character;          !!!next-input-character;
1688          redo A;          redo A;
1689        } else {        } else {
1690            !!!cp (156);
1691          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
1692          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1693          ## reconsume          ## reconsume
1694          redo A;          redo A;
1695        }        }
1696      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
1697        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1698            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1699            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1700            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1701            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1702            !!!cp (157);
1703          ## Stay in the state          ## Stay in the state
1704          !!!next-input-character;          !!!next-input-character;
1705          redo A;          redo A;
1706        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1707            !!!cp (158);
1708          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1709          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1710          !!!next-input-character;          !!!next-input-character;
1711    
1712          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
1713    
1714          redo A;          redo A;
1715        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1716            !!!cp (159);
1717          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1718          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1719          ## reconsume          ## reconsume
1720    
1721          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
1722    
1723          redo A;          redo A;
1724        } else {        } else {
1725          $self->{current_token}          !!!cp (160);
1726              = {type => DOCTYPE_TOKEN,          $self->{current_token}->{name} = chr $self->{next_char};
1727                 name => chr ($self->{next_input_character}),          delete $self->{current_token}->{quirks};
                correct => 1};  
1728  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1729          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
1730          !!!next-input-character;          !!!next-input-character;
# Line 1383  sub _get_next_token ($) { Line 1732  sub _get_next_token ($) {
1732        }        }
1733      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1734  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
1735        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1736            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1737            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1738            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1739            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1740            !!!cp (161);
1741          $self->{state} = AFTER_DOCTYPE_NAME_STATE;          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
1742          !!!next-input-character;          !!!next-input-character;
1743          redo A;          redo A;
1744        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1745            !!!cp (162);
1746          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1747          !!!next-input-character;          !!!next-input-character;
1748    
1749          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1750    
1751          redo A;          redo A;
1752        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1753            !!!cp (163);
1754          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1755          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1756          ## reconsume          ## reconsume
1757    
1758          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1759          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1760    
1761          redo A;          redo A;
1762        } else {        } else {
1763            !!!cp (164);
1764          $self->{current_token}->{name}          $self->{current_token}->{name}
1765            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_char}); # DOCTYPE
1766          ## Stay in the state          ## Stay in the state
1767          !!!next-input-character;          !!!next-input-character;
1768          redo A;          redo A;
1769        }        }
1770      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
1771        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1772            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1773            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1774            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1775            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1776            !!!cp (165);
1777          ## Stay in the state          ## Stay in the state
1778          !!!next-input-character;          !!!next-input-character;
1779          redo A;          redo A;
1780        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1781            !!!cp (166);
1782          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1783          !!!next-input-character;          !!!next-input-character;
1784    
1785          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1786    
1787          redo A;          redo A;
1788        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1789            !!!cp (167);
1790          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1791          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1792          ## reconsume          ## reconsume
1793    
1794          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1795          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1796    
1797          redo A;          redo A;
1798        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
1799                 $self->{next_input_character} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
1800          !!!next-input-character;          !!!next-input-character;
1801          if ($self->{next_input_character} == 0x0055 or # U          if ($self->{next_char} == 0x0055 or # U
1802              $self->{next_input_character} == 0x0075) { # u              $self->{next_char} == 0x0075) { # u
1803            !!!next-input-character;            !!!next-input-character;
1804            if ($self->{next_input_character} == 0x0042 or # B            if ($self->{next_char} == 0x0042 or # B
1805                $self->{next_input_character} == 0x0062) { # b                $self->{next_char} == 0x0062) { # b
1806              !!!next-input-character;              !!!next-input-character;
1807              if ($self->{next_input_character} == 0x004C or # L              if ($self->{next_char} == 0x004C or # L
1808                  $self->{next_input_character} == 0x006C) { # l                  $self->{next_char} == 0x006C) { # l
1809                !!!next-input-character;                !!!next-input-character;
1810                if ($self->{next_input_character} == 0x0049 or # I                if ($self->{next_char} == 0x0049 or # I
1811                    $self->{next_input_character} == 0x0069) { # i                    $self->{next_char} == 0x0069) { # i
1812                  !!!next-input-character;                  !!!next-input-character;
1813                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_char} == 0x0043 or # C
1814                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_char} == 0x0063) { # c
1815                      !!!cp (168);
1816                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1817                    !!!next-input-character;                    !!!next-input-character;
1818                    redo A;                    redo A;
1819                    } else {
1820                      !!!cp (169);
1821                  }                  }
1822                  } else {
1823                    !!!cp (170);
1824                }                }
1825                } else {
1826                  !!!cp (171);
1827              }              }
1828              } else {
1829                !!!cp (172);
1830            }            }
1831            } else {
1832              !!!cp (173);
1833          }          }
1834    
1835          #          #
1836        } elsif ($self->{next_input_character} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
1837                 $self->{next_input_character} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
1838          !!!next-input-character;          !!!next-input-character;
1839          if ($self->{next_input_character} == 0x0059 or # Y          if ($self->{next_char} == 0x0059 or # Y
1840              $self->{next_input_character} == 0x0079) { # y              $self->{next_char} == 0x0079) { # y
1841            !!!next-input-character;            !!!next-input-character;
1842            if ($self->{next_input_character} == 0x0053 or # S            if ($self->{next_char} == 0x0053 or # S
1843                $self->{next_input_character} == 0x0073) { # s                $self->{next_char} == 0x0073) { # s
1844              !!!next-input-character;              !!!next-input-character;
1845              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
1846                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
1847                !!!next-input-character;                !!!next-input-character;
1848                if ($self->{next_input_character} == 0x0045 or # E                if ($self->{next_char} == 0x0045 or # E
1849                    $self->{next_input_character} == 0x0065) { # e                    $self->{next_char} == 0x0065) { # e
1850                  !!!next-input-character;                  !!!next-input-character;
1851                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_char} == 0x004D or # M
1852                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_char} == 0x006D) { # m
1853                      !!!cp (174);
1854                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1855                    !!!next-input-character;                    !!!next-input-character;
1856                    redo A;                    redo A;
1857                    } else {
1858                      !!!cp (175);
1859                  }                  }
1860                  } else {
1861                    !!!cp (176);
1862                }                }
1863                } else {
1864                  !!!cp (177);
1865              }              }
1866              } else {
1867                !!!cp (178);
1868            }            }
1869            } else {
1870              !!!cp (179);
1871          }          }
1872    
1873          #          #
1874        } else {        } else {
1875            !!!cp (180);
1876          !!!next-input-character;          !!!next-input-character;
1877          #          #
1878        }        }
1879    
1880        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
1881          $self->{current_token}->{quirks} = 1;
1882    
1883        $self->{state} = BOGUS_DOCTYPE_STATE;        $self->{state} = BOGUS_DOCTYPE_STATE;
1884        # next-input-character is already done        # next-input-character is already done
1885        redo A;        redo A;
# Line 1506  sub _get_next_token ($) { Line 1887  sub _get_next_token ($) {
1887        if ({        if ({
1888              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1889              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
1890            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
1891            !!!cp (181);
1892          ## Stay in the state          ## Stay in the state
1893          !!!next-input-character;          !!!next-input-character;
1894          redo A;          redo A;
1895        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_char} eq 0x0022) { # "
1896            !!!cp (182);
1897          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1898          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
1899          !!!next-input-character;          !!!next-input-character;
1900          redo A;          redo A;
1901        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_char} eq 0x0027) { # '
1902            !!!cp (183);
1903          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1904          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
1905          !!!next-input-character;          !!!next-input-character;
1906          redo A;          redo A;
1907        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_char} eq 0x003E) { # >
1908            !!!cp (184);
1909          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
1910    
1911          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1912          !!!next-input-character;          !!!next-input-character;
1913    
1914          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1915          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1916    
1917          redo A;          redo A;
1918        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1919            !!!cp (185);
1920          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1921    
1922          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1923          ## reconsume          ## reconsume
1924    
1925          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1926          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1927    
1928          redo A;          redo A;
1929        } else {        } else {
1930            !!!cp (186);
1931          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
1932            $self->{current_token}->{quirks} = 1;
1933    
1934          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
1935          !!!next-input-character;          !!!next-input-character;
1936          redo A;          redo A;
1937        }        }
1938      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1939        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1940            !!!cp (187);
1941          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1942          !!!next-input-character;          !!!next-input-character;
1943          redo A;          redo A;
1944        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
1945            !!!cp (188);
1946            !!!parse-error (type => 'unclosed PUBLIC literal');
1947    
1948            $self->{state} = DATA_STATE;
1949            !!!next-input-character;
1950    
1951            $self->{current_token}->{quirks} = 1;
1952            !!!emit ($self->{current_token}); # DOCTYPE
1953    
1954            redo A;
1955          } elsif ($self->{next_char} == -1) {
1956            !!!cp (189);
1957          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1958    
1959          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1960          ## reconsume          ## reconsume
1961    
1962          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1963          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1964    
1965          redo A;          redo A;
1966        } else {        } else {
1967            !!!cp (190);
1968          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
1969              .= chr $self->{next_input_character};              .= chr $self->{next_char};
1970          ## Stay in the state          ## Stay in the state
1971          !!!next-input-character;          !!!next-input-character;
1972          redo A;          redo A;
1973        }        }
1974      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
1975        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1976            !!!cp (191);
1977          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1978          !!!next-input-character;          !!!next-input-character;
1979          redo A;          redo A;
1980        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
1981            !!!cp (192);
1982            !!!parse-error (type => 'unclosed PUBLIC literal');
1983    
1984            $self->{state} = DATA_STATE;
1985            !!!next-input-character;
1986    
1987            $self->{current_token}->{quirks} = 1;
1988            !!!emit ($self->{current_token}); # DOCTYPE
1989    
1990            redo A;
1991          } elsif ($self->{next_char} == -1) {
1992            !!!cp (193);
1993          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1994    
1995          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1996          ## reconsume          ## reconsume
1997    
1998          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1999          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2000    
2001          redo A;          redo A;
2002        } else {        } else {
2003            !!!cp (194);
2004          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2005              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2006          ## Stay in the state          ## Stay in the state
2007          !!!next-input-character;          !!!next-input-character;
2008          redo A;          redo A;
# Line 1594  sub _get_next_token ($) { Line 2011  sub _get_next_token ($) {
2011        if ({        if ({
2012              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2013              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2014            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2015            !!!cp (195);
2016          ## Stay in the state          ## Stay in the state
2017          !!!next-input-character;          !!!next-input-character;
2018          redo A;          redo A;
2019        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2020            !!!cp (196);
2021          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2022          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2023          !!!next-input-character;          !!!next-input-character;
2024          redo A;          redo A;
2025        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2026            !!!cp (197);
2027          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2028          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2029          !!!next-input-character;          !!!next-input-character;
2030          redo A;          redo A;
2031        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2032            !!!cp (198);
2033          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2034          !!!next-input-character;          !!!next-input-character;
2035    
2036          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2037    
2038          redo A;          redo A;
2039        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2040            !!!cp (199);
2041          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2042    
2043          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2044          ## reconsume          ## reconsume
2045    
2046          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2047          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2048    
2049          redo A;          redo A;
2050        } else {        } else {
2051            !!!cp (200);
2052          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2053            $self->{current_token}->{quirks} = 1;
2054    
2055          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2056          !!!next-input-character;          !!!next-input-character;
2057          redo A;          redo A;
# Line 1635  sub _get_next_token ($) { Line 2060  sub _get_next_token ($) {
2060        if ({        if ({
2061              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2062              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2063            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2064            !!!cp (201);
2065          ## Stay in the state          ## Stay in the state
2066          !!!next-input-character;          !!!next-input-character;
2067          redo A;          redo A;
2068        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2069            !!!cp (202);
2070          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2071          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2072          !!!next-input-character;          !!!next-input-character;
2073          redo A;          redo A;
2074        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2075            !!!cp (203);
2076          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2077          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2078          !!!next-input-character;          !!!next-input-character;
2079          redo A;          redo A;
2080        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2081            !!!cp (204);
2082          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2083          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2084          !!!next-input-character;          !!!next-input-character;
2085    
2086          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2087          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2088    
2089          redo A;          redo A;
2090        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2091            !!!cp (205);
2092          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2093    
2094          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2095          ## reconsume          ## reconsume
2096    
2097          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2098          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2099    
2100          redo A;          redo A;
2101        } else {        } else {
2102            !!!cp (206);
2103          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2104            $self->{current_token}->{quirks} = 1;
2105    
2106          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2107          !!!next-input-character;          !!!next-input-character;
2108          redo A;          redo A;
2109        }        }
2110      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2111        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2112            !!!cp (207);
2113          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2114          !!!next-input-character;          !!!next-input-character;
2115          redo A;          redo A;
2116        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2117            !!!cp (208);
2118            !!!parse-error (type => 'unclosed PUBLIC literal');
2119    
2120            $self->{state} = DATA_STATE;
2121            !!!next-input-character;
2122    
2123            $self->{current_token}->{quirks} = 1;
2124            !!!emit ($self->{current_token}); # DOCTYPE
2125    
2126            redo A;
2127          } elsif ($self->{next_char} == -1) {
2128            !!!cp (209);
2129          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2130    
2131          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2132          ## reconsume          ## reconsume
2133    
2134          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2135          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2136    
2137          redo A;          redo A;
2138        } else {        } else {
2139            !!!cp (210);
2140          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2141              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2142          ## Stay in the state          ## Stay in the state
2143          !!!next-input-character;          !!!next-input-character;
2144          redo A;          redo A;
2145        }        }
2146      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2147        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2148            !!!cp (211);
2149          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2150          !!!next-input-character;          !!!next-input-character;
2151          redo A;          redo A;
2152        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2153            !!!cp (212);
2154            !!!parse-error (type => 'unclosed PUBLIC literal');
2155    
2156            $self->{state} = DATA_STATE;
2157            !!!next-input-character;
2158    
2159            $self->{current_token}->{quirks} = 1;
2160            !!!emit ($self->{current_token}); # DOCTYPE
2161    
2162            redo A;
2163          } elsif ($self->{next_char} == -1) {
2164            !!!cp (213);
2165          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2166    
2167          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2168          ## reconsume          ## reconsume
2169    
2170          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2171          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2172    
2173          redo A;          redo A;
2174        } else {        } else {
2175            !!!cp (214);
2176          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2177              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2178          ## Stay in the state          ## Stay in the state
2179          !!!next-input-character;          !!!next-input-character;
2180          redo A;          redo A;
# Line 1722  sub _get_next_token ($) { Line 2183  sub _get_next_token ($) {
2183        if ({        if ({
2184              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2185              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2186            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2187            !!!cp (215);
2188          ## Stay in the state          ## Stay in the state
2189          !!!next-input-character;          !!!next-input-character;
2190          redo A;          redo A;
2191        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2192            !!!cp (216);
2193          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2194          !!!next-input-character;          !!!next-input-character;
2195    
2196          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2197    
2198          redo A;          redo A;
2199        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2200            !!!cp (217);
2201          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2202    
2203          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2204          ## reconsume          ## reconsume
2205    
2206          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2207          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2208    
2209          redo A;          redo A;
2210        } else {        } else {
2211            !!!cp (218);
2212          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2213            #$self->{current_token}->{quirks} = 1;
2214    
2215          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2216          !!!next-input-character;          !!!next-input-character;
2217          redo A;          redo A;
2218        }        }
2219      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2220        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2221            !!!cp (219);
2222          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2223          !!!next-input-character;          !!!next-input-character;
2224    
         delete $self->{current_token}->{correct};  
2225          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2226    
2227          redo A;          redo A;
2228        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2229            !!!cp (220);
2230          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2231          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2232          ## reconsume          ## reconsume
2233    
         delete $self->{current_token}->{correct};  
2234          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2235    
2236          redo A;          redo A;
2237        } else {        } else {
2238            !!!cp (221);
2239          ## Stay in the state          ## Stay in the state
2240          !!!next-input-character;          !!!next-input-character;
2241          redo A;          redo A;
# Line 1780  sub _get_next_token ($) { Line 2248  sub _get_next_token ($) {
2248    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
2249  } # _get_next_token  } # _get_next_token
2250    
2251  sub _tokenize_attempt_to_consume_an_entity ($$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2252    my ($self, $in_attr) = @_;    my ($self, $in_attr, $additional) = @_;
2253    
2254      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2255    
2256    if ({    if ({
2257         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2258         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2259        }->{$self->{next_input_character}}) {         $additional => 1,
2260          }->{$self->{next_char}}) {
2261        !!!cp (1001);
2262      ## Don't consume      ## Don't consume
2263      ## No error      ## No error
2264      return undef;      return undef;
2265    } elsif ($self->{next_input_character} == 0x0023) { # #    } elsif ($self->{next_char} == 0x0023) { # #
2266      !!!next-input-character;      !!!next-input-character;
2267      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_char} == 0x0078 or # x
2268          $self->{next_input_character} == 0x0058) { # X          $self->{next_char} == 0x0058) { # X
2269        my $code;        my $code;
2270        X: {        X: {
2271          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2272          !!!next-input-character;          !!!next-input-character;
2273          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2274              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2275              !!!cp (1002);
2276            $code ||= 0;            $code ||= 0;
2277            $code *= 0x10;            $code *= 0x10;
2278            $code += $self->{next_input_character} - 0x0030;            $code += $self->{next_char} - 0x0030;
2279            redo X;            redo X;
2280          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2281                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2282              !!!cp (1003);
2283            $code ||= 0;            $code ||= 0;
2284            $code *= 0x10;            $code *= 0x10;
2285            $code += $self->{next_input_character} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2286            redo X;            redo X;
2287          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2288                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2289              !!!cp (1004);
2290            $code ||= 0;            $code ||= 0;
2291            $code *= 0x10;            $code *= 0x10;
2292            $code += $self->{next_input_character} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2293            redo X;            redo X;
2294          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2295            !!!parse-error (type => 'bare hcro');            !!!cp (1005);
2296            !!!back-next-input-character ($x_char, $self->{next_input_character});            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2297            $self->{next_input_character} = 0x0023; # #            !!!back-next-input-character ($x_char, $self->{next_char});
2298              $self->{next_char} = 0x0023; # #
2299            return undef;            return undef;
2300          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2301              !!!cp (1006);
2302            !!!next-input-character;            !!!next-input-character;
2303          } else {          } else {
2304            !!!parse-error (type => 'no refc');            !!!cp (1007);
2305              !!!parse-error (type => 'no refc', line => $l, column => $c);
2306          }          }
2307    
2308          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2309            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!cp (1008);
2310              !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2311            $code = 0xFFFD;            $code = 0xFFFD;
2312          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2313            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!cp (1009);
2314              !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2315            $code = 0xFFFD;            $code = 0xFFFD;
2316          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2317            !!!parse-error (type => 'CR character reference');            !!!cp (1010);
2318              !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2319            $code = 0x000A;            $code = 0x000A;
2320          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2321            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!cp (1011);
2322              !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2323            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2324          }          }
2325    
2326          return {type => CHARACTER_TOKEN, data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code,
2327                    has_reference => 1,
2328                    line => $l, column => $c,
2329                   };
2330        } # X        } # X
2331      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
2332               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
2333        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
2334        !!!next-input-character;        !!!next-input-character;
2335                
2336        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
2337                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2338            !!!cp (1012);
2339          $code *= 10;          $code *= 10;
2340          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
2341                    
2342          !!!next-input-character;          !!!next-input-character;
2343        }        }
2344    
2345        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
2346            !!!cp (1013);
2347          !!!next-input-character;          !!!next-input-character;
2348        } else {        } else {
2349          !!!parse-error (type => 'no refc');          !!!cp (1014);
2350            !!!parse-error (type => 'no refc', line => $l, column => $c);
2351        }        }
2352    
2353        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2354          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!cp (1015);
2355            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2356          $code = 0xFFFD;          $code = 0xFFFD;
2357        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2358          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!cp (1016);
2359            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2360          $code = 0xFFFD;          $code = 0xFFFD;
2361        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2362          !!!parse-error (type => 'CR character reference');          !!!cp (1017);
2363            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2364          $code = 0x000A;          $code = 0x000A;
2365        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2366          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!cp (1018);
2367            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2368          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2369        }        }
2370                
2371        return {type => CHARACTER_TOKEN, data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2372                  line => $l, column => $c,
2373                 };
2374      } else {      } else {
2375        !!!parse-error (type => 'bare nero');        !!!cp (1019);
2376        !!!back-next-input-character ($self->{next_input_character});        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2377        $self->{next_input_character} = 0x0023; # #        !!!back-next-input-character ($self->{next_char});
2378          $self->{next_char} = 0x0023; # #
2379        return undef;        return undef;
2380      }      }
2381    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
2382              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
2383             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
2384              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
2385      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
2386      !!!next-input-character;      !!!next-input-character;
2387    
2388      my $value = $entity_name;      my $value = $entity_name;
# Line 1897  sub _tokenize_attempt_to_consume_an_enti Line 2392  sub _tokenize_attempt_to_consume_an_enti
2392    
2393      while (length $entity_name < 10 and      while (length $entity_name < 10 and
2394             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2395             ((0x0041 <= $self->{next_input_character} and # a             ((0x0041 <= $self->{next_char} and # a
2396               $self->{next_input_character} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
2397              (0x0061 <= $self->{next_input_character} and # a              (0x0061 <= $self->{next_char} and # a
2398               $self->{next_input_character} <= 0x007A) or # z               $self->{next_char} <= 0x007A) or # z
2399              (0x0030 <= $self->{next_input_character} and # 0              (0x0030 <= $self->{next_char} and # 0
2400               $self->{next_input_character} <= 0x0039) or # 9               $self->{next_char} <= 0x0039) or # 9
2401              $self->{next_input_character} == 0x003B)) { # ;              $self->{next_char} == 0x003B)) { # ;
2402        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_char};
2403        if (defined $EntityChar->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
2404          if ($self->{next_input_character} == 0x003B) { # ;          if ($self->{next_char} == 0x003B) { # ;
2405              !!!cp (1020);
2406            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2407            $match = 1;            $match = 1;
2408            !!!next-input-character;            !!!next-input-character;
2409            last;            last;
2410          } else {          } else {
2411              !!!cp (1021);
2412            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2413            $match = -1;            $match = -1;
2414            !!!next-input-character;            !!!next-input-character;
2415          }          }
2416        } else {        } else {
2417          $value .= chr $self->{next_input_character};          !!!cp (1022);
2418            $value .= chr $self->{next_char};
2419          $match *= 2;          $match *= 2;
2420          !!!next-input-character;          !!!next-input-character;
2421        }        }
2422      }      }
2423            
2424      if ($match > 0) {      if ($match > 0) {
2425        return {type => CHARACTER_TOKEN, data => $value};        !!!cp (1023);
2426          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2427                  line => $l, column => $c,
2428                 };
2429      } elsif ($match < 0) {      } elsif ($match < 0) {
2430        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
2431        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
2432          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          !!!cp (1024);
2433        } else {          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
2434          return {type => CHARACTER_TOKEN, data => $value};                  line => $l, column => $c,
2435                   };
2436          } else {
2437            !!!cp (1025);
2438            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2439                    line => $l, column => $c,
2440                   };
2441        }        }
2442      } else {      } else {
2443        !!!parse-error (type => 'bare ero');        !!!cp (1026);
2444        ## NOTE: No characters are consumed in the spec.        !!!parse-error (type => 'bare ero', line => $l, column => $c);
2445        return {type => CHARACTER_TOKEN, data => '&'.$value};        ## NOTE: "No characters are consumed" in the spec.
2446          return {type => CHARACTER_TOKEN, data => '&'.$value,
2447                  line => $l, column => $c,
2448                 };
2449      }      }
2450    } else {    } else {
2451        !!!cp (1027);
2452      ## no characters are consumed      ## no characters are consumed
2453      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
2454      return undef;      return undef;
2455    }    }
2456  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 1977  sub _construct_tree ($) { Line 2488  sub _construct_tree ($) {
2488        
2489    !!!next-token;    !!!next-token;
2490    
   $self->{insertion_mode} = BEFORE_HEAD_IM;  
2491    undef $self->{form_element};    undef $self->{form_element};
2492    undef $self->{head_element};    undef $self->{head_element};
2493    $self->{open_elements} = [];    $self->{open_elements} = [];
2494    undef $self->{inner_html_node};    undef $self->{inner_html_node};
2495    
2496      ## NOTE: The "initial" insertion mode.
2497    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
2498    
2499      ## NOTE: The "before html" insertion mode.
2500    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
2501      $self->{insertion_mode} = BEFORE_HEAD_IM;
2502    
2503      ## NOTE: The "before head" insertion mode and so on.
2504    $self->_tree_construction_main;    $self->_tree_construction_main;
2505  } # _construct_tree  } # _construct_tree
2506    
2507  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
2508    my $self = shift;    my $self = shift;
2509    
2510      ## NOTE: "initial" insertion mode
2511    
2512    INITIAL: {    INITIAL: {
2513      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
2514        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
# Line 2001  sub _tree_construction_initial ($) { Line 2520  sub _tree_construction_initial ($) {
2520        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
2521            defined $token->{public_identifier} or            defined $token->{public_identifier} or
2522            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
2523          !!!parse-error (type => 'not HTML5');          !!!cp ('t1');
2524            !!!parse-error (type => 'not HTML5', token => $token);
2525        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
2526            !!!cp ('t2');
2527          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
2528          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
2529          } else {
2530            !!!cp ('t3');
2531        }        }
2532                
2533        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
# Line 2017  sub _tree_construction_initial ($) { Line 2540  sub _tree_construction_initial ($) {
2540        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
2541        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
2542                
2543        if (not $token->{correct} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
2544            !!!cp ('t4');
2545          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
2546        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
2547          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
# Line 2071  sub _tree_construction_initial ($) { Line 2595  sub _tree_construction_initial ($) {
2595            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
2596            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
2597            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
2598              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,
2599              "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,
2600              "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,
2601            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
2602            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
2603            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
# Line 2093  sub _tree_construction_initial ($) { Line 2620  sub _tree_construction_initial ($) {
2620            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
2621            "HTML" => 1,            "HTML" => 1,
2622          }->{$pubid}) {          }->{$pubid}) {
2623              !!!cp ('t5');
2624            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
2625          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
2626                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
2627            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
2628                !!!cp ('t6');
2629              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
2630            } else {            } else {
2631                !!!cp ('t7');
2632              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
2633            }            }
2634          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or
2635                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {
2636              !!!cp ('t8');
2637            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
2638            } else {
2639              !!!cp ('t9');
2640          }          }
2641          } else {
2642            !!!cp ('t10');
2643        }        }
2644        if (defined $token->{system_identifier}) {        if (defined $token->{system_identifier}) {
2645          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
2646          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
2647          if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {          if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
2648              ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"
2649            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
2650              !!!cp ('t11');
2651            } else {
2652              !!!cp ('t12');
2653          }          }
2654          } else {
2655            !!!cp ('t13');
2656        }        }
2657                
2658        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
2659        !!!next-token;        !!!next-token;
2660        return;        return;
2661      } elsif ({      } elsif ({
# Line 2122  sub _tree_construction_initial ($) { Line 2663  sub _tree_construction_initial ($) {
2663                END_TAG_TOKEN, 1,                END_TAG_TOKEN, 1,
2664                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
2665               }->{$token->{type}}) {               }->{$token->{type}}) {
2666        !!!parse-error (type => 'no DOCTYPE');        !!!cp ('t14');
2667          !!!parse-error (type => 'no DOCTYPE', token => $token);
2668        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2669        ## Go to the root element phase        ## Go to the "before html" insertion mode.
2670        ## reprocess        ## reprocess
2671        return;        return;
2672      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
# Line 2132  sub _tree_construction_initial ($) { Line 2674  sub _tree_construction_initial ($) {
2674          ## Ignore the token          ## Ignore the token
2675    
2676          unless (length $token->{data}) {          unless (length $token->{data}) {
2677            ## Stay in the phase            !!!cp ('t15');
2678              ## Stay in the insertion mode.
2679            !!!next-token;            !!!next-token;
2680            redo INITIAL;            redo INITIAL;
2681            } else {
2682              !!!cp ('t16');
2683          }          }
2684          } else {
2685            !!!cp ('t17');
2686        }        }
2687    
2688        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
2689        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2690        ## Go to the root element phase        ## Go to the "before html" insertion mode.
2691        ## reprocess        ## reprocess
2692        return;        return;
2693      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
2694          !!!cp ('t18');
2695        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2696        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
2697                
2698        ## Stay in the phase.        ## Stay in the insertion mode.
2699        !!!next-token;        !!!next-token;
2700        redo INITIAL;        redo INITIAL;
2701      } else {      } else {
2702        die "$0: $token->{type}: Unknown token type";        die "$0: $token->{type}: Unknown token type";
2703      }      }
2704    } # INITIAL    } # INITIAL
2705    
2706      die "$0: _tree_construction_initial: This should be never reached";
2707  } # _tree_construction_initial  } # _tree_construction_initial
2708    
2709  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
2710    my $self = shift;    my $self = shift;
2711    
2712      ## NOTE: "before html" insertion mode.
2713        
2714    B: {    B: {
2715        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
2716          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
2717            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
2718          ## Ignore the token          ## Ignore the token
2719          ## Stay in the phase          ## Stay in the insertion mode.
2720          !!!next-token;          !!!next-token;
2721          redo B;          redo B;
2722        } elsif ($token->{type} == COMMENT_TOKEN) {        } elsif ($token->{type} == COMMENT_TOKEN) {
2723            !!!cp ('t20');
2724          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2725          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2726          ## Stay in the phase          ## Stay in the insertion mode.
2727          !!!next-token;          !!!next-token;
2728          redo B;          redo B;
2729        } elsif ($token->{type} == CHARACTER_TOKEN) {        } elsif ($token->{type} == CHARACTER_TOKEN) {
# Line 2177  sub _tree_construction_root_element ($) Line 2731  sub _tree_construction_root_element ($)
2731            ## Ignore the token.            ## Ignore the token.
2732    
2733            unless (length $token->{data}) {            unless (length $token->{data}) {
2734              ## Stay in the phase              !!!cp ('t21');
2735                ## Stay in the insertion mode.
2736              !!!next-token;              !!!next-token;
2737              redo B;              redo B;
2738              } else {
2739                !!!cp ('t22');
2740            }            }
2741            } else {
2742              !!!cp ('t23');
2743          }          }
2744    
2745          $self->{application_cache_selection}->(undef);          $self->{application_cache_selection}->(undef);
2746    
2747          #          #
2748        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
2749          if ($token->{tag_name} eq 'html' and          if ($token->{tag_name} eq 'html') {
2750              $token->{attributes}->{manifest}) { ## ISSUE: Spec spells as "application"            my $root_element;
2751            $self->{application_cache_selection}            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes}, $token);
2752                 ->($token->{attributes}->{manifest}->{value});            $self->{document}->append_child ($root_element);
2753            ## ISSUE: No relative reference resolution?            push @{$self->{open_elements}}, [$root_element, 'html'];
2754    
2755              if ($token->{attributes}->{manifest}) {
2756                !!!cp ('t24');
2757                $self->{application_cache_selection}
2758                    ->($token->{attributes}->{manifest}->{value});
2759                ## ISSUE: Spec is unclear on relative references.
2760                ## According to Hixie (#whatwg 2008-03-19), it should be
2761                ## resolved against the base URI of the document in HTML
2762                ## or xml:base of the element in XHTML.
2763              } else {
2764                !!!cp ('t25');
2765                $self->{application_cache_selection}->(undef);
2766              }
2767    
2768              !!!next-token;
2769              return; ## Go to the "before head" insertion mode.
2770          } else {          } else {
2771            $self->{application_cache_selection}->(undef);            !!!cp ('t25.1');
2772              #
2773          }          }
   
         ## ISSUE: There is an issue in the spec  
         #  
2774        } elsif ({        } elsif ({
2775                  END_TAG_TOKEN, 1,                  END_TAG_TOKEN, 1,
2776                  END_OF_FILE_TOKEN, 1,                  END_OF_FILE_TOKEN, 1,
2777                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2778          $self->{application_cache_selection}->(undef);          !!!cp ('t26');
   
         ## ISSUE: There is an issue in the spec  
2779          #          #
2780        } else {        } else {
2781          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
2782        }        }
2783    
2784        my $root_element; !!!create-element ($root_element, 'html');      my $root_element; !!!create-element ($root_element, 'html',, $token);
2785        $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
2786        push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, 'html'];
2787        ## reprocess  
2788        #redo B;      $self->{application_cache_selection}->(undef);
2789        return; ## Go to the main phase.  
2790        ## NOTE: Reprocess the token.
2791        return; ## Go to the "before head" insertion mode.
2792    
2793        ## ISSUE: There is an issue in the spec
2794    } # B    } # B
2795    
2796      die "$0: _tree_construction_root_element: This should never be reached";
2797  } # _tree_construction_root_element  } # _tree_construction_root_element
2798    
2799  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2231  sub _reset_insertion_mode ($) { Line 2808  sub _reset_insertion_mode ($) {
2808            
2809      ## Step 3      ## Step 3
2810      S3: {      S3: {
       ## ISSUE: Oops! "If node is the first node in the stack of open  
       ## elements, then set last to true. If the context element of the  
       ## HTML fragment parsing algorithm is neither a td element nor a  
       ## th element, then set node to the context element. (fragment case)":  
       ## The second "if" is in the scope of the first "if"!?  
2811        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2812          $last = 1;          $last = 1;
2813          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
2814            if ($self->{inner_html_node}->[1] eq 'td' or            if ($self->{inner_html_node}->[1] eq 'td' or
2815                $self->{inner_html_node}->[1] eq 'th') {                $self->{inner_html_node}->[1] eq 'th') {
2816                !!!cp ('t27');
2817              #              #
2818            } else {            } else {
2819                !!!cp ('t28');
2820              $node = $self->{inner_html_node};              $node = $self->{inner_html_node};
2821            }            }
2822          }          }
# Line 2251  sub _reset_insertion_mode ($) { Line 2825  sub _reset_insertion_mode ($) {
2825        ## Step 4..13        ## Step 4..13
2826        my $new_mode = {        my $new_mode = {
2827                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
2828                          ## NOTE: |option| and |optgroup| do not set
2829                          ## insertion mode to "in select" by themselves.
2830                        td => IN_CELL_IM,                        td => IN_CELL_IM,
2831                        th => IN_CELL_IM,                        th => IN_CELL_IM,
2832                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
# Line 2269  sub _reset_insertion_mode ($) { Line 2845  sub _reset_insertion_mode ($) {
2845        ## Step 14        ## Step 14
2846        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2847          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2848              !!!cp ('t29');
2849            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
2850          } else {          } else {
2851              ## ISSUE: Can this state be reached?
2852              !!!cp ('t30');
2853            $self->{insertion_mode} = AFTER_HEAD_IM;            $self->{insertion_mode} = AFTER_HEAD_IM;
2854          }          }
2855          return;          return;
2856          } else {
2857            !!!cp ('t31');
2858        }        }
2859                
2860        ## Step 15        ## Step 15
# Line 2286  sub _reset_insertion_mode ($) { Line 2867  sub _reset_insertion_mode ($) {
2867        ## Step 17        ## Step 17
2868        redo S3;        redo S3;
2869      } # S3      } # S3
2870    
2871      die "$0: _reset_insertion_mode: This line should never be reached";
2872  } # _reset_insertion_mode  } # _reset_insertion_mode
2873    
2874  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
# Line 2307  sub _tree_construction_main ($) { Line 2890  sub _tree_construction_main ($) {
2890      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
2891      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
2892        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
2893            !!!cp ('t32');
2894          return;          return;
2895        }        }
2896      }      }
# Line 2321  sub _tree_construction_main ($) { Line 2905  sub _tree_construction_main ($) {
2905    
2906        ## Step 6        ## Step 6
2907        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
2908            !!!cp ('t33_1');
2909          #          #
2910        } else {        } else {
2911          my $in_open_elements;          my $in_open_elements;
2912          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
2913            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
2914                !!!cp ('t33');
2915              $in_open_elements = 1;              $in_open_elements = 1;
2916              last OE;              last OE;
2917            }            }
2918          }          }
2919          if ($in_open_elements) {          if ($in_open_elements) {
2920              !!!cp ('t34');
2921            #            #
2922          } else {          } else {
2923              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
2924              !!!cp ('t35');
2925            redo S4;            redo S4;
2926          }          }
2927        }        }
# Line 2355  sub _tree_construction_main ($) { Line 2944  sub _tree_construction_main ($) {
2944    
2945        ## Step 11        ## Step 11
2946        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
2947            !!!cp ('t36');
2948          ## Step 7'          ## Step 7'
2949          $i++;          $i++;
2950          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
2951                    
2952          redo S7;          redo S7;
2953        }        }
2954    
2955          !!!cp ('t37');
2956      } # S7      } # S7
2957    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
2958    
2959    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
2960      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
2961        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
2962            !!!cp ('t38');
2963          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
2964          return;          return;
2965        }        }
2966      }      }
2967    
2968        !!!cp ('t39');
2969    }; # $clear_up_to_marker    }; # $clear_up_to_marker
2970    
2971    my $parse_rcdata = sub ($$) {    my $insert;
2972      my ($content_model_flag, $insert) = @_;  
2973      my $parse_rcdata = sub ($) {
2974        my ($content_model_flag) = @_;
2975    
2976      ## Step 1      ## Step 1
2977      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
2978      my $el;      my $el;
2979      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $start_tag_name, $token->{attributes}, $token);
2980    
2981      ## Step 2      ## Step 2
2982      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
2983    
2984      ## Step 3      ## Step 3
2985      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2392  sub _tree_construction_main ($) { Line 2989  sub _tree_construction_main ($) {
2989      my $text = '';      my $text = '';
2990      !!!next-token;      !!!next-token;
2991      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2992          !!!cp ('t40');
2993        $text .= $token->{data};        $text .= $token->{data};
2994        !!!next-token;        !!!next-token;
2995      }      }
2996    
2997      ## Step 5      ## Step 5
2998      if (length $text) {      if (length $text) {
2999          !!!cp ('t41');
3000        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
3001        $el->append_child ($text);        $el->append_child ($text);
3002      }      }
# Line 2406  sub _tree_construction_main ($) { Line 3005  sub _tree_construction_main ($) {
3005      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
3006    
3007      ## Step 7      ## Step 7
3008      if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
3009            $token->{tag_name} eq $start_tag_name) {
3010          !!!cp ('t42');
3011        ## Ignore the token        ## Ignore the token
     } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {  
       !!!parse-error (type => 'in CDATA:#'.$token->{type});  
     } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {  
       !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
3012      } else {      } else {
3013        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3014          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3015            !!!cp ('t43');
3016            !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3017          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3018            !!!cp ('t44');
3019            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3020          } else {
3021            die "$0: $content_model_flag in parse_rcdata";
3022          }
3023      }      }
3024      !!!next-token;      !!!next-token;
3025    }; # $parse_rcdata    }; # $parse_rcdata
3026    
3027    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3028      my $script_el;      my $script_el;
3029      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes}, $token);
3030      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3031    
3032      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
# Line 2430  sub _tree_construction_main ($) { Line 3035  sub _tree_construction_main ($) {
3035      my $text = '';      my $text = '';
3036      !!!next-token;      !!!next-token;
3037      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3038          !!!cp ('t45');
3039        $text .= $token->{data};        $text .= $token->{data};
3040        !!!next-token;        !!!next-token;
3041      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
3042      if (length $text) {      if (length $text) {
3043          !!!cp ('t46');
3044        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
3045      }      }
3046                                
# Line 2441  sub _tree_construction_main ($) { Line 3048  sub _tree_construction_main ($) {
3048    
3049      if ($token->{type} == END_TAG_TOKEN and      if ($token->{type} == END_TAG_TOKEN and
3050          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
3051          !!!cp ('t47');
3052        ## Ignore the token        ## Ignore the token
3053      } else {      } else {
3054        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
3055          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3056        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3057        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3058      }      }
3059            
3060      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
3061          !!!cp ('t49');
3062        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3063      } else {      } else {
3064          !!!cp ('t50');
3065        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3066        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3067    
# Line 2464  sub _tree_construction_main ($) { Line 3075  sub _tree_construction_main ($) {
3075      !!!next-token;      !!!next-token;
3076    }; # $script_start_tag    }; # $script_start_tag
3077    
3078      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3079      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3080      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3081    
3082    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3083      my $tag_name = shift;      my $end_tag_token = shift;
3084        my $tag_name = $end_tag_token->{tag_name};
3085    
3086        ## NOTE: The adoption agency algorithm (AAA).
3087    
3088      FET: {      FET: {
3089        ## Step 1        ## Step 1
# Line 2473  sub _tree_construction_main ($) { Line 3091  sub _tree_construction_main ($) {
3091        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3092        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3093          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {
3094              !!!cp ('t51');
3095            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3096            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3097            last AFE;            last AFE;
3098          } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {          } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {
3099              !!!cp ('t52');
3100            last AFE;            last AFE;
3101          }          }
3102        } # AFE        } # AFE
3103        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3104          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
3105            !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3106          ## Ignore the token          ## Ignore the token
3107          !!!next-token;          !!!next-token;
3108          return;          return;
# Line 2493  sub _tree_construction_main ($) { Line 3114  sub _tree_construction_main ($) {
3114          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3115          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3116            if ($in_scope) {            if ($in_scope) {
3117                !!!cp ('t54');
3118              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3119              last INSCOPE;              last INSCOPE;
3120            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3121              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
3122                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3123                                token => $end_tag_token);
3124              ## Ignore the token              ## Ignore the token
3125              !!!next-token;              !!!next-token;
3126              return;              return;
3127            }            }
3128          } elsif ({          } elsif ({
3129                    table => 1, caption => 1, td => 1, th => 1,                    applet => 1, table => 1, caption => 1, td => 1, th => 1,
3130                    button => 1, marquee => 1, object => 1, html => 1,                    button => 1, marquee => 1, object => 1, html => 1,
3131                   }->{$node->[1]}) {                   }->{$node->[1]}) {
3132              !!!cp ('t56');
3133            $in_scope = 0;            $in_scope = 0;
3134          }          }
3135        } # INSCOPE        } # INSCOPE
3136        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3137          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
3138            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3139                            token => $end_tag_token);
3140          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3141          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3142          return;          return;
3143        }        }
3144        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3145          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
3146            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1],
3147                            token => $end_tag_token);
3148        }        }
3149                
3150        ## Step 2        ## Step 2
# Line 2526  sub _tree_construction_main ($) { Line 3155  sub _tree_construction_main ($) {
3155          if (not $formatting_category->{$node->[1]} and          if (not $formatting_category->{$node->[1]} and
3156              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3157              ($special_category->{$node->[1]} or              ($special_category->{$node->[1]} or
3158               $scoping_category->{$node->[1]})) {               $scoping_category->{$node->[1]})) { ## Scoping is redundant, maybe
3159              !!!cp ('t59');
3160            $furthest_block = $node;            $furthest_block = $node;
3161            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3162          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3163              !!!cp ('t60');
3164            last OE;            last OE;
3165          }          }
3166        } # OE        } # OE
3167                
3168        ## Step 3        ## Step 3
3169        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3170            !!!cp ('t61');
3171          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3172          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3173          !!!next-token;          !!!next-token;
# Line 2548  sub _tree_construction_main ($) { Line 3180  sub _tree_construction_main ($) {
3180        ## Step 5        ## Step 5
3181        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3182        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3183            !!!cp ('t62');
3184          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3185        }        }
3186                
# Line 2570  sub _tree_construction_main ($) { Line 3203  sub _tree_construction_main ($) {
3203          S7S2: {          S7S2: {
3204            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3205              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3206                  !!!cp ('t63');
3207                $node_i_in_active = $_;                $node_i_in_active = $_;
3208                last S7S2;                last S7S2;
3209              }              }
# Line 2583  sub _tree_construction_main ($) { Line 3217  sub _tree_construction_main ($) {
3217                    
3218          ## Step 4          ## Step 4
3219          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3220              !!!cp ('t64');
3221            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3222          }          }
3223                    
3224          ## Step 5          ## Step 5
3225          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3226              !!!cp ('t65');
3227            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3228            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3229            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2605  sub _tree_construction_main ($) { Line 3241  sub _tree_construction_main ($) {
3241        } # S7          } # S7  
3242                
3243        ## Step 8        ## Step 8
3244        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ({
3245               table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,
3246              }->{$common_ancestor_node->[1]}) {
3247            my $foster_parent_element;
3248            my $next_sibling;
3249                             OE: for (reverse 0..$#{$self->{open_elements}}) {
3250                               if ($self->{open_elements}->[$_]->[1] eq 'table') {
3251                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3252                                 if (defined $parent and $parent->node_type == 1) {
3253                                   !!!cp ('t65.1');
3254                                   $foster_parent_element = $parent;
3255                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3256                                 } else {
3257                                   !!!cp ('t65.2');
3258                                   $foster_parent_element
3259                                     = $self->{open_elements}->[$_ - 1]->[0];
3260                                 }
3261                                 last OE;
3262                               }
3263                             } # OE
3264                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3265                               unless defined $foster_parent_element;
3266            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3267            $open_tables->[-1]->[1] = 1; # tainted
3268          } else {
3269            !!!cp ('t65.3');
3270            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3271          }
3272                
3273        ## Step 9        ## Step 9
3274        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2622  sub _tree_construction_main ($) { Line 3285  sub _tree_construction_main ($) {
3285        my $i;        my $i;
3286        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3287          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3288              !!!cp ('t66');
3289            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3290            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3291          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3292              !!!cp ('t67');
3293            $i = $_;            $i = $_;
3294          }          }
3295        } # AFE        } # AFE
# Line 2634  sub _tree_construction_main ($) { Line 3299  sub _tree_construction_main ($) {
3299        undef $i;        undef $i;
3300        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3301          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3302              !!!cp ('t68');
3303            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3304            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3305          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3306              !!!cp ('t69');
3307            $i = $_;            $i = $_;
3308          }          }
3309        } # OE        } # OE
# Line 2647  sub _tree_construction_main ($) { Line 3314  sub _tree_construction_main ($) {
3314      } # FET      } # FET
3315    }; # $formatting_end_tag    }; # $formatting_end_tag
3316    
3317    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3318      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3319    }; # $insert_to_current    }; # $insert_to_current
3320    
3321    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3322                         my $child = shift;      my $child = shift;
3323                         if ({      if ({
3324                              table => 1, tbody => 1, tfoot => 1,           table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,
3325                              thead => 1, tr => 1,          }->{$self->{open_elements}->[-1]->[1]}) {
3326                             }->{$self->{open_elements}->[-1]->[1]}) {        # MUST
3327                           # MUST        my $foster_parent_element;
3328                           my $foster_parent_element;        my $next_sibling;
                          my $next_sibling;  
3329                           OE: for (reverse 0..$#{$self->{open_elements}}) {                           OE: for (reverse 0..$#{$self->{open_elements}}) {
3330                             if ($self->{open_elements}->[$_]->[1] eq 'table') {                             if ($self->{open_elements}->[$_]->[1] eq 'table') {
3331                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3332                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3333                                   !!!cp ('t70');
3334                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
3335                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
3336                               } else {                               } else {
3337                                   !!!cp ('t71');
3338                                 $foster_parent_element                                 $foster_parent_element
3339                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
3340                               }                               }
# Line 2677  sub _tree_construction_main ($) { Line 3345  sub _tree_construction_main ($) {
3345                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3346                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3347                             ($child, $next_sibling);                             ($child, $next_sibling);
3348                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3349                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
3350                         }        !!!cp ('t72');
3351          $self->{open_elements}->[-1]->[0]->append_child ($child);
3352        }
3353    }; # $insert_to_foster    }; # $insert_to_foster
3354    
   my $insert;  
   
3355    B: {    B: {
3356      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3357        !!!parse-error (type => 'DOCTYPE in the middle');        !!!cp ('t73');
3358          !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3359        ## Ignore the token        ## Ignore the token
3360        ## Stay in the phase        ## Stay in the phase
3361        !!!next-token;        !!!next-token;
3362        redo B;        redo B;
     } elsif ($token->{type} == END_OF_FILE_TOKEN) {  
       if ($self->{insertion_mode} & AFTER_HTML_IMS) {  
         #  
       } else {  
         ## Generate implied end tags  
         if ({  
              dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,  
              tbody => 1, tfoot=> 1, thead => 1,  
             }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!back-token;  
           $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};  
           redo B;  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
   
         ## ISSUE: There is an issue in the spec.  
       }  
   
       ## Stop parsing  
       last B;  
3363      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3364               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3365        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3366          ## Turn into the main phase          !!!cp ('t79');
3367          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3368          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
3369        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3370          ## Turn into the main phase          !!!cp ('t80');
3371          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3372          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3373          } else {
3374            !!!cp ('t81');
3375        }        }
3376    
3377  ## ISSUE: "aa<html>" is not a parse error.        !!!cp ('t82');
3378  ## ISSUE: "<html>" in fragment is not a parse error.        !!!parse-error (type => 'not first start tag', token => $token);
       unless ($token->{first_start_tag}) {  
         !!!parse-error (type => 'not first start tag');  
       }  
3379        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
3380        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
3381          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3382              !!!cp ('t84');
3383            $top_el->set_attribute_ns            $top_el->set_attribute_ns
3384              (undef, [undef, $attr_name],              (undef, [undef, $attr_name],
3385               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
# Line 2749  sub _tree_construction_main ($) { Line 3390  sub _tree_construction_main ($) {
3390      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3391        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3392        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3393            !!!cp ('t85');
3394          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3395        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
3396            !!!cp ('t86');
3397          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
3398        } else {        } else {
3399            !!!cp ('t87');
3400          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
3401        }        }
3402        !!!next-token;        !!!next-token;
# Line 2760  sub _tree_construction_main ($) { Line 3404  sub _tree_construction_main ($) {
3404      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & HEAD_IMS) {
3405        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
3406          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3407            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3408                !!!cp ('t88.2');
3409                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3410              } else {
3411                !!!cp ('t88.1');
3412                ## Ignore the token.
3413                !!!next-token;
3414                redo B;
3415              }
3416            unless (length $token->{data}) {            unless (length $token->{data}) {
3417                !!!cp ('t88');
3418              !!!next-token;              !!!next-token;
3419              redo B;              redo B;
3420            }            }
3421          }          }
3422    
3423          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3424              !!!cp ('t89');
3425            ## As if <head>            ## As if <head>
3426            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, 'head',, $token);
3427            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3428            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3429    
# Line 2778  sub _tree_construction_main ($) { Line 3432  sub _tree_construction_main ($) {
3432    
3433            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
3434          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3435              !!!cp ('t90');
3436            ## As if </noscript>            ## As if </noscript>
3437            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
3438            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character', token => $token);
3439                        
3440            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
3441            ## As if </head>            ## As if </head>
# Line 2788  sub _tree_construction_main ($) { Line 3443  sub _tree_construction_main ($) {
3443    
3444            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
3445          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3446              !!!cp ('t91');
3447            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
3448    
3449            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
3450            } else {
3451              !!!cp ('t92');
3452          }          }
3453    
3454              ## "after head" insertion mode              ## "after head" insertion mode
3455              ## As if <body>              ## As if <body>
3456              !!!insert-element ('body');              !!!insert-element ('body',, $token);
3457              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
3458              ## reprocess              ## reprocess
3459              redo B;              redo B;
3460            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
3461              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
3462                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3463                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});                  !!!cp ('t93');
3464                    !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes}, $token);
3465                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3466                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
3467                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3468                  !!!next-token;                  !!!next-token;
3469                  redo B;                  redo B;
3470                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3471                    !!!cp ('t94');
3472                  #                  #
3473                } else {                } else {
3474                  !!!parse-error (type => 'in head:head'); # or in head noscript                  !!!cp ('t95');
3475                    !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
3476                  ## Ignore the token                  ## Ignore the token
3477                  !!!next-token;                  !!!next-token;
3478                  redo B;                  redo B;
3479                }                }
3480              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3481                  !!!cp ('t96');
3482                ## As if <head>                ## As if <head>
3483                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head',, $token);
3484                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3485                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3486    
3487                $self->{insertion_mode} = IN_HEAD_IM;                $self->{insertion_mode} = IN_HEAD_IM;
3488                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
3489                } else {
3490                  !!!cp ('t97');
3491              }              }
3492    
3493              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
3494                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3495                    !!!cp ('t98');
3496                  ## As if </noscript>                  ## As if </noscript>
3497                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3498                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base', token => $token);
3499                                
3500                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3501                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3502                  } else {
3503                    !!!cp ('t99');
3504                }                }
3505    
3506                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3507                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3508                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t100');
3509                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3510                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3511                  } else {
3512                    !!!cp ('t101');
3513                }                }
3514                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3515                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3516                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3517                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3518                !!!next-token;                !!!next-token;
3519                redo B;                redo B;
3520              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
3521                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3522                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3523                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t102');
3524                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3525                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3526                  } else {
3527                    !!!cp ('t103');
3528                }                }
3529                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3530                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3531                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3532                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3533                !!!next-token;                !!!next-token;
3534                redo B;                redo B;
3535              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
3536                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3537                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3538                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t104');
3539                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3540                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3541                  } else {
3542                    !!!cp ('t105');
3543                }                }
3544                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3545                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3546    
3547                unless ($self->{confident}) {                unless ($self->{confident}) {
3548                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
3549                      !!!cp ('t106');
3550                    $self->{change_encoding}                    $self->{change_encoding}
3551                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
3552                             $token);
3553                      
3554                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3555                          ->set_user_data (manakai_has_reference =>
3556                                               $token->{attributes}->{charset}
3557                                                   ->{has_reference});
3558                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
3559                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
3560                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
3561                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
3562                              [\x09-\x0D\x20]*=
3563                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
3564                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
3565                        !!!cp ('t107');
3566                      $self->{change_encoding}                      $self->{change_encoding}
3567                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
3568                               $token);
3569                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3570                            ->set_user_data (manakai_has_reference =>
3571                                                 $token->{attributes}->{content}
3572                                                       ->{has_reference});
3573                      } else {
3574                        !!!cp ('t108');
3575                    }                    }
3576                  }                  }
3577                  } else {
3578                    if ($token->{attributes}->{charset}) {
3579                      !!!cp ('t109');
3580                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3581                          ->set_user_data (manakai_has_reference =>
3582                                               $token->{attributes}->{charset}
3583                                                   ->{has_reference});
3584                    }
3585                    if ($token->{attributes}->{content}) {
3586                      !!!cp ('t110');
3587                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3588                          ->set_user_data (manakai_has_reference =>
3589                                               $token->{attributes}->{content}
3590                                                   ->{has_reference});
3591                    }
3592                }                }
3593    
3594                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3595                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3596                !!!next-token;                !!!next-token;
3597                redo B;                redo B;
3598              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
3599                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3600                    !!!cp ('t111');
3601                  ## As if </noscript>                  ## As if </noscript>
3602                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3603                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title', token => $token);
3604                                
3605                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3606                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3607                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3608                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t112');
3609                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3610                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3611                  } else {
3612                    !!!cp ('t113');
3613                }                }
3614    
3615                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3616                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
3617                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
3618                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
3619                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
3620                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3621                redo B;                redo B;
3622              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
# Line 2914  sub _tree_construction_main ($) { Line 3624  sub _tree_construction_main ($) {
3624                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
3625                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3626                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3627                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t114');
3628                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3629                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3630                  } else {
3631                    !!!cp ('t115');
3632                }                }
3633                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
3634                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3635                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3636                redo B;                redo B;
3637              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
3638                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
3639                    !!!cp ('t116');
3640                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
3641                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3642                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
3643                  !!!next-token;                  !!!next-token;
3644                  redo B;                  redo B;
3645                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3646                  !!!parse-error (type => 'in noscript:noscript');                  !!!cp ('t117');
3647                    !!!parse-error (type => 'in noscript:noscript', token => $token);
3648                  ## Ignore the token                  ## Ignore the token
3649                  !!!next-token;                  !!!next-token;
3650                  redo B;                  redo B;
3651                } else {                } else {
3652                    !!!cp ('t118');
3653                  #                  #
3654                }                }
3655              } elsif ($token->{tag_name} eq 'script') {              } elsif ($token->{tag_name} eq 'script') {
3656                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3657                    !!!cp ('t119');
3658                  ## As if </noscript>                  ## As if </noscript>
3659                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3660                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script', token => $token);
3661                                
3662                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3663                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3664                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3665                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t120');
3666                    !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3667                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3668                  } else {
3669                    !!!cp ('t121');
3670                }                }
3671    
3672                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3673                $script_start_tag->($insert_to_current);                $script_start_tag->();
3674                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3675                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3676                redo B;                redo B;
3677              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
3678                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
3679                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3680                    !!!cp ('t122');
3681                  ## As if </noscript>                  ## As if </noscript>
3682                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3683                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
3684                                    
3685                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3686                  ## As if </head>                  ## As if </head>
# Line 2967  sub _tree_construction_main ($) { Line 3688  sub _tree_construction_main ($) {
3688                                    
3689                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
3690                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3691                    !!!cp ('t124');
3692                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3693                                    
3694                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
3695                  } else {
3696                    !!!cp ('t125');
3697                }                }
3698    
3699                ## "after head" insertion mode                ## "after head" insertion mode
3700                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3701                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
3702                    !!!cp ('t126');
3703                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
3704                } elsif ($token->{tag_name} eq 'frameset') {                } elsif ($token->{tag_name} eq 'frameset') {
3705                    !!!cp ('t127');
3706                  $self->{insertion_mode} = IN_FRAMESET_IM;                  $self->{insertion_mode} = IN_FRAMESET_IM;
3707                } else {                } else {
3708                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
# Line 2984  sub _tree_construction_main ($) { Line 3710  sub _tree_construction_main ($) {
3710                !!!next-token;                !!!next-token;
3711                redo B;                redo B;
3712              } else {              } else {
3713                  !!!cp ('t128');
3714                #                #
3715              }              }
3716    
3717              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3718                  !!!cp ('t129');
3719                ## As if </noscript>                ## As if </noscript>
3720                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3721                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
3722                                
3723                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
3724                ## As if </head>                ## As if </head>
# Line 2998  sub _tree_construction_main ($) { Line 3726  sub _tree_construction_main ($) {
3726    
3727                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3728              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3729                  !!!cp ('t130');
3730                ## As if </head>                ## As if </head>
3731                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3732    
3733                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3734                } else {
3735                  !!!cp ('t131');
3736              }              }
3737    
3738              ## "after head" insertion mode              ## "after head" insertion mode
3739              ## As if <body>              ## As if <body>
3740              !!!insert-element ('body');              !!!insert-element ('body',, $token);
3741              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
3742              ## reprocess              ## reprocess
3743              redo B;              redo B;
3744            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
3745              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
3746                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3747                    !!!cp ('t132');
3748                  ## As if <head>                  ## As if <head>
3749                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head',, $token);
3750                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3751                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3752    
# Line 3024  sub _tree_construction_main ($) { Line 3756  sub _tree_construction_main ($) {
3756                  !!!next-token;                  !!!next-token;
3757                  redo B;                  redo B;
3758                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3759                    !!!cp ('t133');
3760                  ## As if </noscript>                  ## As if </noscript>
3761                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3762                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:/head', token => $token);
3763                                    
3764                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3765                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 3034  sub _tree_construction_main ($) { Line 3767  sub _tree_construction_main ($) {
3767                  !!!next-token;                  !!!next-token;
3768                  redo B;                  redo B;
3769                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3770                    !!!cp ('t134');
3771                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3772                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
3773                  !!!next-token;                  !!!next-token;
3774                  redo B;                  redo B;
3775                } else {                } else {
3776                    !!!cp ('t135');
3777                  #                  #
3778                }                }
3779              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
3780                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3781                    !!!cp ('t136');
3782                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3783                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3784                  !!!next-token;                  !!!next-token;
3785                  redo B;                  redo B;
3786                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3787                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!cp ('t137');
3788                    !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
3789                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
3790                  !!!next-token;                  !!!next-token;
3791                  redo B;                  redo B;
3792                } else {                } else {
3793                    !!!cp ('t138');
3794                  #                  #
3795                }                }
3796              } elsif ({              } elsif ({
3797                        body => 1, html => 1,                        body => 1, html => 1,
3798                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3799                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3800                    !!!cp ('t139');
3801                  ## As if <head>                  ## As if <head>
3802                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head',, $token);
3803                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3804                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3805    
3806                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3807                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3808                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3809                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t140');
3810                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
3811                  ## Ignore the token                  ## Ignore the token
3812                  !!!next-token;                  !!!next-token;
3813                  redo B;                  redo B;
3814                  } else {
3815                    !!!cp ('t141');
3816                }                }
3817                                
3818                #                #
# Line 3078  sub _tree_construction_main ($) { Line 3820  sub _tree_construction_main ($) {
3820                        p => 1, br => 1,                        p => 1, br => 1,
3821                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3822                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3823                    !!!cp ('t142');
3824                  ## As if <head>                  ## As if <head>
3825                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head',, $token);
3826                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3827                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3828    
3829                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3830                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3831                  } else {
3832                    !!!cp ('t143');
3833                }                }
3834    
3835                #                #
3836              } else {              } else {
3837                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3838                    !!!cp ('t144');
3839                  #                  #
3840                } else {                } else {
3841                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t145');
3842                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
3843                  ## Ignore the token                  ## Ignore the token
3844                  !!!next-token;                  !!!next-token;
3845                  redo B;                  redo B;
# Line 3100  sub _tree_construction_main ($) { Line 3847  sub _tree_construction_main ($) {
3847              }              }
3848    
3849              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3850                  !!!cp ('t146');
3851                ## As if </noscript>                ## As if </noscript>
3852                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3853                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
3854                                
3855                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
3856                ## As if </head>                ## As if </head>
# Line 3110  sub _tree_construction_main ($) { Line 3858  sub _tree_construction_main ($) {
3858    
3859                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3860              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3861                  !!!cp ('t147');
3862                ## As if </head>                ## As if </head>
3863                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3864    
3865                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3866              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3867                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  ## ISSUE: This case cannot be reached?
3868                  !!!cp ('t148');
3869                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
3870                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
3871                !!!next-token;                !!!next-token;
3872                redo B;                redo B;
3873                } else {
3874                  !!!cp ('t149');
3875              }              }
3876    
3877              ## "after head" insertion mode              ## "after head" insertion mode
3878              ## As if <body>              ## As if <body>
3879              !!!insert-element ('body');              !!!insert-element ('body',, $token);
3880              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
3881              ## reprocess              ## reprocess
3882              redo B;              redo B;
3883            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
3884              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3885            }            !!!cp ('t149.1');
3886    
3887              ## NOTE: As if <head>
3888              !!!create-element ($self->{head_element}, 'head',, $token);
3889              $self->{open_elements}->[-1]->[0]->append_child
3890                  ($self->{head_element});
3891              #push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3892              #$self->{insertion_mode} = IN_HEAD_IM;
3893              ## NOTE: Reprocess.
3894    
3895              ## NOTE: As if </head>
3896              #pop @{$self->{open_elements}};
3897              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
3898              ## NOTE: Reprocess.
3899              
3900              #
3901            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3902              !!!cp ('t149.2');
3903    
3904              ## NOTE: As if </head>
3905              pop @{$self->{open_elements}};
3906              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
3907              ## NOTE: Reprocess.
3908    
3909              #
3910            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3911              !!!cp ('t149.3');
3912    
3913              !!!parse-error (type => 'in noscript:#eof', token => $token);
3914    
3915              ## As if </noscript>
3916              pop @{$self->{open_elements}};
3917              #$self->{insertion_mode} = IN_HEAD_IM;
3918              ## NOTE: Reprocess.
3919    
3920              ## NOTE: As if </head>
3921              pop @{$self->{open_elements}};
3922              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
3923              ## NOTE: Reprocess.
3924    
3925              #
3926            } else {
3927              !!!cp ('t149.4');
3928              #
3929            }
3930    
3931            ## NOTE: As if <body>
3932            !!!insert-element ('body',, $token);
3933            $self->{insertion_mode} = IN_BODY_IM;
3934            ## NOTE: Reprocess.
3935            redo B;
3936          } else {
3937            die "$0: $token->{type}: Unknown token type";
3938          }
3939    
3940            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
3941      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
3942            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
3943                !!!cp ('t150');
3944              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
3945              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
3946                            
# Line 3148  sub _tree_construction_main ($) { Line 3955  sub _tree_construction_main ($) {
3955                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3956                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
3957                  ## have an element in table scope                  ## have an element in table scope
3958                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
3959                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
3960                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3961                      $tn = $node->[1];                      !!!cp ('t151');
3962                      last INSCOPE;  
3963                        ## Close the cell
3964                        !!!back-token; # <?>
3965                        $token = {type => END_TAG_TOKEN, tag_name => $node->[1],
3966                                  line => $token->{line},
3967                                  column => $token->{column}};
3968                        redo B;
3969                    } elsif ({                    } elsif ({
3970                              table => 1, html => 1,                              table => 1, html => 1,
3971                             }->{$node->[1]}) {                             }->{$node->[1]}) {
3972                      last INSCOPE;                      !!!cp ('t152');
3973                    }                      ## ISSUE: This case can never be reached, maybe.
3974                  } # INSCOPE                      last;
                   unless (defined $tn) {  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
3975                    }                    }
3976                                    }
3977                  ## Close the cell  
3978                  !!!back-token; # <?>                  !!!cp ('t153');
3979                  $token = {type => END_TAG_TOKEN, tag_name => $tn};                  !!!parse-error (type => 'start tag not allowed',
3980                        value => $token->{tag_name}, token => $token);
3981                    ## Ignore the token
3982                    !!!next-token;
3983                  redo B;                  redo B;
3984                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3985                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption', token => $token);
3986                                    
3987                  ## As if </caption>                  ## NOTE: As if </caption>.
3988                  ## have a table element in table scope                  ## have a table element in table scope
3989                  my $i;                  my $i;
3990                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
3991                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
3992                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
3993                      $i = $_;                      if ($node->[1] eq 'caption') {
3994                      last INSCOPE;                        !!!cp ('t155');
3995                    } elsif ({                        $i = $_;
3996                              table => 1, html => 1,                        last INSCOPE;
3997                             }->{$node->[1]}) {                      } elsif ({
3998                      last INSCOPE;                                table => 1, html => 1,
3999                                 }->{$node->[1]}) {
4000                          !!!cp ('t156');
4001                          last;
4002                        }
4003                    }                    }
4004    
4005                      !!!cp ('t157');
4006                      !!!parse-error (type => 'start tag not allowed',
4007                                      value => $token->{tag_name}, token => $token);
4008                      ## Ignore the token
4009                      !!!next-token;
4010                      redo B;
4011                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4012                                    
4013                  ## generate implied end tags                  ## generate implied end tags
4014                  if ({                  while ({
4015                       dd => 1, dt => 1, li => 1, p => 1,                          dd => 1, dt => 1, li => 1, p => 1,
4016                       td => 1, th => 1, tr => 1,                         }->{$self->{open_elements}->[-1]->[1]}) {
4017                       tbody => 1, tfoot=> 1, thead => 1,                    !!!cp ('t158');
4018                      }->{$self->{open_elements}->[-1]->[1]}) {                    pop @{$self->{open_elements}};
                   !!!back-token; # <?>  
                   $token = {type => END_TAG_TOKEN, tag_name => 'caption'};  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4019                  }                  }
4020    
4021                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4022                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t159');
4023                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
4024                    } else {
4025                      !!!cp ('t160');
4026                  }                  }
4027                                    
4028                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3222  sub _tree_construction_main ($) { Line 4034  sub _tree_construction_main ($) {
4034                  ## reprocess                  ## reprocess
4035                  redo B;                  redo B;
4036                } else {                } else {
4037                    !!!cp ('t161');
4038                  #                  #
4039                }                }
4040              } else {              } else {
4041                  !!!cp ('t162');
4042                #                #
4043              }              }
4044            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
# Line 3235  sub _tree_construction_main ($) { Line 4049  sub _tree_construction_main ($) {
4049                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4050                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4051                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[1] eq $token->{tag_name}) {
4052                        !!!cp ('t163');
4053                      $i = $_;                      $i = $_;
4054                      last INSCOPE;                      last INSCOPE;
4055                    } elsif ({                    } elsif ({
4056                              table => 1, html => 1,                              table => 1, html => 1,
4057                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4058                        !!!cp ('t164');
4059                      last INSCOPE;                      last INSCOPE;
4060                    }                    }
4061                  } # INSCOPE                  } # INSCOPE
4062                    unless (defined $i) {                    unless (defined $i) {
4063                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t165');
4064                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4065                      ## Ignore the token                      ## Ignore the token
4066                      !!!next-token;                      !!!next-token;
4067                      redo B;                      redo B;
4068                    }                    }
4069                                    
4070                  ## generate implied end tags                  ## generate implied end tags
4071                  if ({                  while ({
4072                       dd => 1, dt => 1, li => 1, p => 1,                          dd => 1, dt => 1, li => 1, p => 1,
4073                       td => ($token->{tag_name} eq 'th'),                         }->{$self->{open_elements}->[-1]->[1]}) {
4074                       th => ($token->{tag_name} eq 'td'),                    !!!cp ('t166');
4075                       tr => 1,                    pop @{$self->{open_elements}};
                      tbody => 1, tfoot=> 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4076                  }                  }
4077                    
4078                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
4079                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t167');
4080                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
4081                    } else {
4082                      !!!cp ('t168');
4083                  }                  }
4084                                    
4085                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3277  sub _tree_construction_main ($) { Line 4091  sub _tree_construction_main ($) {
4091                  !!!next-token;                  !!!next-token;
4092                  redo B;                  redo B;
4093                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4094                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t169');
4095                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4096                  ## Ignore the token                  ## Ignore the token
4097                  !!!next-token;                  !!!next-token;
4098                  redo B;                  redo B;
4099                } else {                } else {
4100                    !!!cp ('t170');
4101                  #                  #
4102                }                }
4103              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
4104                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
4105                  ## have a table element in table scope                  ## have a table element in table scope
4106                  my $i;                  my $i;
4107                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4108                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4109                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
4110                      $i = $_;                      if ($node->[1] eq $token->{tag_name}) {
4111                      last INSCOPE;                        !!!cp ('t171');
4112                    } elsif ({                        $i = $_;
4113                              table => 1, html => 1,                        last INSCOPE;
4114                             }->{$node->[1]}) {                      } elsif ({
4115                      last INSCOPE;                                table => 1, html => 1,
4116                                 }->{$node->[1]}) {
4117                          !!!cp ('t172');
4118                          last;
4119                        }
4120                    }                    }
4121    
4122                      !!!cp ('t173');
4123                      !!!parse-error (type => 'unmatched end tag',
4124                                      value => $token->{tag_name}, token => $token);
4125                      ## Ignore the token
4126                      !!!next-token;
4127                      redo B;
4128                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4129                                    
4130                  ## generate implied end tags                  ## generate implied end tags
4131                  if ({                  while ({
4132                       dd => 1, dt => 1, li => 1, p => 1,                          dd => 1, dt => 1, li => 1, p => 1,
4133                       td => 1, th => 1, tr => 1,                         }->{$self->{open_elements}->[-1]->[1]}) {
4134                       tbody => 1, tfoot=> 1, thead => 1,                    !!!cp ('t174');
4135                      }->{$self->{open_elements}->[-1]->[1]}) {                    pop @{$self->{open_elements}};
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4136                  }                  }
4137                                    
4138                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4139                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t175');
4140                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
4141                    } else {
4142                      !!!cp ('t176');
4143                  }                  }
4144                                    
4145                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3331  sub _tree_construction_main ($) { Line 4151  sub _tree_construction_main ($) {
4151                  !!!next-token;                  !!!next-token;
4152                  redo B;                  redo B;
4153                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4154                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t177');
4155                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4156                  ## Ignore the token                  ## Ignore the token
4157                  !!!next-token;                  !!!next-token;
4158                  redo B;                  redo B;
4159                } else {                } else {
4160                    !!!cp ('t178');
4161                  #                  #
4162                }                }
4163              } elsif ({              } elsif ({
# Line 3346  sub _tree_construction_main ($) { Line 4168  sub _tree_construction_main ($) {
4168                ## have an element in table scope                ## have an element in table scope
4169                my $i;                my $i;
4170                my $tn;                my $tn;
4171                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
4172                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
4173                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4174                    $i = $_;                    if ($node->[1] eq $token->{tag_name}) {
4175                    last INSCOPE;                      !!!cp ('t179');
4176                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                      $i = $_;
4177                    $tn = $node->[1];  
4178                    ## NOTE: There is exactly one |td| or |th| element                      ## Close the cell
4179                    ## in scope in the stack of open elements by definition.                      !!!back-token; # </?>
4180                  } elsif ({                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4181                            table => 1, html => 1,                                line => $token->{line},
4182                           }->{$node->[1]}) {                                column => $token->{column}};
4183                    last INSCOPE;                      redo B;
4184                      } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
4185                        !!!cp ('t180');
4186                        $tn = $node->[1];
4187                        ## NOTE: There is exactly one |td| or |th| element
4188                        ## in scope in the stack of open elements by definition.
4189                      } elsif ({
4190                                table => 1, html => 1,
4191                               }->{$node->[1]}) {
4192                        ## ISSUE: Can this be reached?
4193                        !!!cp ('t181');
4194                        last;
4195                      }
4196                  }                  }
4197                } # INSCOPE  
4198                unless (defined $i) {                  !!!cp ('t182');
4199                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4200                        value => $token->{tag_name}, token => $token);
4201                  ## Ignore the token                  ## Ignore the token
4202                  !!!next-token;                  !!!next-token;
4203                  redo B;                  redo B;
4204                }                } # INSCOPE
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
4205              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
4206                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4207                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4208    
4209                ## As if </caption>                ## As if </caption>
4210                ## have a table element in table scope                ## have a table element in table scope
# Line 3382  sub _tree_construction_main ($) { Line 4212  sub _tree_construction_main ($) {
4212                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4213                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4214                  if ($node->[1] eq 'caption') {                  if ($node->[1] eq 'caption') {
4215                      !!!cp ('t184');
4216                    $i = $_;                    $i = $_;
4217                    last INSCOPE;                    last INSCOPE;
4218                  } elsif ({                  } elsif ({
4219                            table => 1, html => 1,                            table => 1, html => 1,
4220                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4221                      !!!cp ('t185');
4222                    last INSCOPE;                    last INSCOPE;
4223                  }                  }
4224                } # INSCOPE                } # INSCOPE
4225                unless (defined $i) {                unless (defined $i) {
4226                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
4227                    !!!parse-error (type => 'unmatched end tag:caption', token => $token);
4228                  ## Ignore the token                  ## Ignore the token
4229                  !!!next-token;                  !!!next-token;
4230                  redo B;                  redo B;
4231                }                }
4232                                
4233                ## generate implied end tags                ## generate implied end tags
4234                if ({                while ({
4235                     dd => 1, dt => 1, li => 1, p => 1,                        dd => 1, dt => 1, li => 1, p => 1,
4236                     td => 1, th => 1, tr => 1,                       }->{$self->{open_elements}->[-1]->[1]}) {
4237                     tbody => 1, tfoot=> 1, thead => 1,                  !!!cp ('t187');
4238                    }->{$self->{open_elements}->[-1]->[1]}) {                  pop @{$self->{open_elements}};
                 !!!back-token; # </table>  
                 $token = {type => END_TAG_TOKEN, tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
4239                }                }
4240    
4241                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4242                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
4243                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
4244                  } else {
4245                    !!!cp ('t189');
4246                }                }
4247    
4248                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 3427  sub _tree_construction_main ($) { Line 4257  sub _tree_construction_main ($) {
4257                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
4258                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4259                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4260                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t190');
4261                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4262                  ## Ignore the token                  ## Ignore the token
4263                  !!!next-token;                  !!!next-token;
4264                  redo B;                  redo B;
4265                } else {                } else {
4266                    !!!cp ('t191');
4267                  #                  #
4268                }                }
4269              } elsif ({              } elsif ({
# Line 3439  sub _tree_construction_main ($) { Line 4271  sub _tree_construction_main ($) {
4271                        thead => 1, tr => 1,                        thead => 1, tr => 1,
4272                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
4273                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4274                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t192');
4275                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4276                ## Ignore the token                ## Ignore the token
4277                !!!next-token;                !!!next-token;
4278                redo B;                redo B;
4279              } else {              } else {
4280                  !!!cp ('t193');
4281                #                #
4282              }              }
4283          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4284            for my $entry (@{$self->{open_elements}}) {
4285              if (not {
4286                dd => 1, dt => 1, li => 1, p => 1, tbody => 1, td => 1, tfoot => 1,
4287                th => 1, thead => 1, tr => 1, body => 1, html => 1,
4288              }->{$entry->[1]}) {
4289                !!!cp ('t75');
4290                !!!parse-error (type => 'in body:#eof', token => $token);
4291                last;
4292              }
4293            }
4294    
4295            ## Stop parsing.
4296            last B;
4297        } else {        } else {
4298          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4299        }        }
# Line 3454  sub _tree_construction_main ($) { Line 4302  sub _tree_construction_main ($) {
4302        #        #
4303      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
4304        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4305              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
4306                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4307              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4308                                
4309                unless (length $token->{data}) {            unless (length $token->{data}) {
4310                  !!!next-token;              !!!cp ('t194');
4311                  redo B;              !!!next-token;
4312                }              redo B;
4313              }            } else {
4314                !!!cp ('t195');
4315              }
4316            }
4317    
4318              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
4319    
4320              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
4321              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 3483  sub _tree_construction_main ($) { Line 4335  sub _tree_construction_main ($) {
4335                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] eq 'table') {
4336                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4337                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
4338                        !!!cp ('t196');
4339                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
4340                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
4341                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
4342                    } else {                    } else {
4343                        !!!cp ('t197');
4344                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
4345                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
4346                    }                    }
# Line 3498  sub _tree_construction_main ($) { Line 4352  sub _tree_construction_main ($) {
4352                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
4353                if (defined $prev_sibling and                if (defined $prev_sibling and
4354                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
4355                    !!!cp ('t198');
4356                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
4357                } else {                } else {
4358                    !!!cp ('t199');
4359                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
4360                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
4361                     $next_sibling);                     $next_sibling);
4362                }                }
4363              } else {            $open_tables->[-1]->[1] = 1; # tainted
4364                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
4365              }            !!!cp ('t200');
4366              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4367            }
4368                            
4369              !!!next-token;          !!!next-token;
4370              redo B;          redo B;
4371        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4372              if ({              if ({
4373                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
# Line 3519  sub _tree_construction_main ($) { Line 4377  sub _tree_construction_main ($) {
4377                  ## Clear back to table context                  ## Clear back to table context
4378                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
4379                         $self->{open_elements}->[-1]->[1] ne 'html') {                         $self->{open_elements}->[-1]->[1] ne 'html') {
4380                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t201');
4381                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4382                  }                  }
4383                                    
4384                  !!!insert-element ('tbody');                  !!!insert-element ('tbody',, $token);
4385                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4386                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
4387                }                }
4388    
4389                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4390                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
4391                    !!!parse-error (type => 'missing start tag:tr');                    !!!cp ('t202');
4392                      !!!parse-error (type => 'missing start tag:tr', token => $token);
4393                  }                  }
4394                                    
4395                  ## Clear back to table body context                  ## Clear back to table body context
4396                  while (not {                  while (not {
4397                    tbody => 1, tfoot => 1, thead => 1, html => 1,                    tbody => 1, tfoot => 1, thead => 1, html => 1,
4398                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4399                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t203');
4400                      ## ISSUE: Can this case be reached?
4401                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4402                  }                  }
4403                                    
4404                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4405                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
4406                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!cp ('t204');
4407                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4408                    !!!next-token;                    !!!next-token;
4409                    redo B;                    redo B;
4410                  } else {                  } else {
4411                    !!!insert-element ('tr');                    !!!cp ('t205');
4412                      !!!insert-element ('tr',, $token);
4413                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
4414                  }                  }
4415                  } else {
4416                    !!!cp ('t206');
4417                }                }
4418    
4419                ## Clear back to table row context                ## Clear back to table row context
4420                while (not {                while (not {
4421                  tr => 1, html => 1,                  tr => 1, html => 1,
4422                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4423                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t207');
4424                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4425                }                }
4426                                
4427                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4428                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
4429    
4430                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
# Line 3579  sub _tree_construction_main ($) { Line 4443  sub _tree_construction_main ($) {
4443                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4444                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4445                    if ($node->[1] eq 'tr') {                    if ($node->[1] eq 'tr') {
4446                        !!!cp ('t208');
4447                      $i = $_;                      $i = $_;
4448                      last INSCOPE;                      last INSCOPE;
4449                    } elsif ({                    } elsif ({
4450                              table => 1, html => 1,                              html => 1,
4451    
4452                                ## NOTE: This element does not appear here, maybe.
4453                                table => 1,
4454                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4455                        !!!cp ('t209');
4456                      last INSCOPE;                      last INSCOPE;
4457                    }                    }
4458                  } # INSCOPE                  } # INSCOPE
4459                  unless (defined $i) {                  unless (defined $i) {
4460                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                   !!!cp ('t210');
4461    ## TODO: This type is wrong.
4462                     !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
4463                    ## Ignore the token                    ## Ignore the token
4464                    !!!next-token;                    !!!next-token;
4465                    redo B;                    redo B;
# Line 3598  sub _tree_construction_main ($) { Line 4469  sub _tree_construction_main ($) {
4469                  while (not {                  while (not {
4470                    tr => 1, html => 1,                    tr => 1, html => 1,
4471                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4472                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t211');
4473                      ## ISSUE: Can this case be reached?
4474                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4475                  }                  }
4476                                    
4477                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
4478                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4479                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
4480                      !!!cp ('t212');
4481                    ## reprocess                    ## reprocess
4482                    redo B;                    redo B;
4483                  } else {                  } else {
4484                      !!!cp ('t213');
4485                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
4486                  }                  }
4487                }                }
# Line 3620  sub _tree_construction_main ($) { Line 4494  sub _tree_construction_main ($) {
4494                    if ({                    if ({
4495                         tbody => 1, thead => 1, tfoot => 1,                         tbody => 1, thead => 1, tfoot => 1,
4496                        }->{$node->[1]}) {                        }->{$node->[1]}) {
4497                        !!!cp ('t214');
4498                      $i = $_;                      $i = $_;
4499                      last INSCOPE;                      last INSCOPE;
4500                    } elsif ({                    } elsif ({
4501                              table => 1, html => 1,                              table => 1, html => 1,
4502                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4503                        !!!cp ('t215');
4504                      last INSCOPE;                      last INSCOPE;
4505                    }                    }
4506                  } # INSCOPE                  } # INSCOPE
4507                  unless (defined $i) {                  unless (defined $i) {
4508                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t216');
4509    ## TODO: This erorr type ios wrong.
4510                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4511                    ## Ignore the token                    ## Ignore the token
4512                    !!!next-token;                    !!!next-token;
4513                    redo B;                    redo B;
# Line 3639  sub _tree_construction_main ($) { Line 4517  sub _tree_construction_main ($) {
4517                  while (not {                  while (not {
4518                    tbody => 1, tfoot => 1, thead => 1, html => 1,                    tbody => 1, tfoot => 1, thead => 1, html => 1,
4519                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4520                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t217');
4521                      ## ISSUE: Can this state be reached?
4522                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4523                  }                  }
4524                                    
# Line 3653  sub _tree_construction_main ($) { Line 4532  sub _tree_construction_main ($) {
4532                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4533                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4534                  ## reprocess in "in table" insertion mode...                  ## reprocess in "in table" insertion mode...
4535                  } else {
4536                    !!!cp ('t218');
4537                }                }
4538    
4539                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
4540                  ## Clear back to table context                  ## Clear back to table context
4541                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
4542                         $self->{open_elements}->[-1]->[1] ne 'html') {                         $self->{open_elements}->[-1]->[1] ne 'html') {
4543                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t219');
4544                      ## ISSUE: Can this state be reached?
4545                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4546                  }                  }
4547                                    
4548                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
4549                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
4550                  ## reprocess                  ## reprocess
4551                  redo B;                  redo B;
# Line 3675  sub _tree_construction_main ($) { Line 4557  sub _tree_construction_main ($) {
4557                  ## Clear back to table context                  ## Clear back to table context
4558                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
4559                         $self->{open_elements}->[-1]->[1] ne 'html') {                         $self->{open_elements}->[-1]->[1] ne 'html') {
4560                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t220');
4561                      ## ISSUE: Can this state be reached?
4562                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4563                  }                  }
4564                                    
4565                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
4566                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
4567                                    
4568                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4569                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
4570                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
4571                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 3696  sub _tree_construction_main ($) { Line 4579  sub _tree_construction_main ($) {
4579                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
4580                }                }
4581              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4582                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
4583    
4584                ## As if </table>                ## As if </table>
4585                ## have a table element in table scope                ## have a table element in table scope
# Line 3704  sub _tree_construction_main ($) { Line 4587  sub _tree_construction_main ($) {
4587                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4588                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4589                  if ($node->[1] eq 'table') {                  if ($node->[1] eq 'table') {
4590                      !!!cp ('t221');
4591                    $i = $_;                    $i = $_;
4592                    last INSCOPE;                    last INSCOPE;
4593                  } elsif ({                  } elsif ({
4594                            table => 1, html => 1,                            #table => 1,
4595                              html => 1,
4596                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4597                      !!!cp ('t222');
4598                    last INSCOPE;                    last INSCOPE;
4599                  }                  }
4600                } # INSCOPE                } # INSCOPE
4601                unless (defined $i) {                unless (defined $i) {
4602                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
4603    ## TODO: The following is wrong, maybe.
4604                    !!!parse-error (type => 'unmatched end tag:table', token => $token);
4605                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
4606                  !!!next-token;                  !!!next-token;
4607                  redo B;                  redo B;
4608                }                }
4609                                
4610    ## TODO: Followings are removed from the latest spec.
4611                ## generate implied end tags                ## generate implied end tags
4612                if ({                while ({
4613                     dd => 1, dt => 1, li => 1, p => 1,                        dd => 1, dt => 1, li => 1, p => 1,
4614                     td => 1, th => 1, tr => 1,                       }->{$self->{open_elements}->[-1]->[1]}) {
4615                     tbody => 1, tfoot=> 1, thead => 1,                  !!!cp ('t224');
4616                    }->{$self->{open_elements}->[-1]->[1]}) {                  pop @{$self->{open_elements}};
                 !!!back-token; # <table>  
                 $token = {type => END_TAG_TOKEN, tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
4617                }                }
4618    
4619                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
4620                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
4621    ## ISSUE: Can this case be reached?
4622                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
4623                  } else {
4624                    !!!cp ('t226');
4625                }                }
4626    
4627                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4628                  pop @{$open_tables};
4629    
4630                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4631    
4632                ## reprocess                ## reprocess
4633                redo B;                redo B;
4634          } else {          } elsif ($token->{tag_name} eq 'style') {
4635            !!!parse-error (type => 'in table:'.$token->{tag_name});            if (not $open_tables->[-1]->[1]) { # tainted
4636                !!!cp ('t227.8');
4637                ## NOTE: This is a "as if in head" code clone.
4638                $parse_rcdata->(CDATA_CONTENT_MODEL);
4639                redo B;
4640              } else {
4641                !!!cp ('t227.7');
4642                #
4643              }
4644            } elsif ($token->{tag_name} eq 'script') {
4645              if (not $open_tables->[-1]->[1]) { # tainted
4646                !!!cp ('t227.6');
4647                ## NOTE: This is a "as if in head" code clone.
4648                $script_start_tag->();
4649                redo B;
4650              } else {
4651                !!!cp ('t227.5');
4652                #
4653              }
4654            } elsif ($token->{tag_name} eq 'input') {
4655              if (not $open_tables->[-1]->[1]) { # tainted
4656                if ($token->{attributes}->{type}) { ## TODO: case
4657                  my $type = lc $token->{attributes}->{type}->{value};
4658                  if ($type eq 'hidden') {
4659                    !!!cp ('t227.3');
4660                    !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
4661    
4662            $insert = $insert_to_foster;                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4663    
4664                    ## TODO: form element pointer
4665    
4666                    pop @{$self->{open_elements}};
4667    
4668                    !!!next-token;
4669                    redo B;
4670                  } else {
4671                    !!!cp ('t227.2');
4672                    #
4673                  }
4674                } else {
4675                  !!!cp ('t227.1');
4676                  #
4677                }
4678              } else {
4679                !!!cp ('t227.4');
4680                #
4681              }
4682            } else {
4683              !!!cp ('t227');
4684            #            #
4685          }          }
4686    
4687            !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
4688    
4689            $insert = $insert_to_foster;
4690            #
4691        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
4692              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
4693                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 3757  sub _tree_construction_main ($) { Line 4696  sub _tree_construction_main ($) {
4696                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4697                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4698                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4699                      !!!cp ('t228');
4700                    $i = $_;                    $i = $_;
4701                    last INSCOPE;                    last INSCOPE;
4702                  } elsif ({                  } elsif ({
4703                            table => 1, html => 1,                            table => 1, html => 1,
4704                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4705                      !!!cp ('t229');
4706                    last INSCOPE;                    last INSCOPE;
4707                  }                  }
4708                } # INSCOPE                } # INSCOPE
4709                unless (defined $i) {                unless (defined $i) {
4710                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t230');
4711                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4712                  ## Ignore the token                  ## Ignore the token
4713                  !!!next-token;                  !!!next-token;
4714                  redo B;                  redo B;
4715                  } else {
4716                    !!!cp ('t232');
4717                }                }
4718    
4719                ## Clear back to table row context                ## Clear back to table row context
4720                while (not {                while (not {
4721                  tr => 1, html => 1,                  tr => 1, html => 1,
4722                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4723                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t231');
4724    ## ISSUE: Can this state be reached?
4725                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4726                }                }
4727    
# Line 3792  sub _tree_construction_main ($) { Line 4737  sub _tree_construction_main ($) {
4737                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4738                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4739                    if ($node->[1] eq 'tr') {                    if ($node->[1] eq 'tr') {
4740                        !!!cp ('t233');
4741                      $i = $_;                      $i = $_;
4742                      last INSCOPE;                      last INSCOPE;
4743                    } elsif ({                    } elsif ({
4744                              table => 1, html => 1,                              table => 1, html => 1,
4745                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4746                        !!!cp ('t234');
4747                      last INSCOPE;                      last INSCOPE;
4748                    }                    }
4749                  } # INSCOPE                  } # INSCOPE
4750                  unless (defined $i) {                  unless (defined $i) {
4751                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!cp ('t235');
4752    ## TODO: The following is wrong.
4753                      !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
4754                    ## Ignore the token                    ## Ignore the token
4755                    !!!next-token;                    !!!next-token;
4756                    redo B;                    redo B;
# Line 3811  sub _tree_construction_main ($) { Line 4760  sub _tree_construction_main ($) {
4760                  while (not {                  while (not {
4761                    tr => 1, html => 1,                    tr => 1, html => 1,
4762                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4763                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t236');
4764    ## ISSUE: Can this state be reached?
4765                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4766                  }                  }
4767                                    
# Line 3828  sub _tree_construction_main ($) { Line 4778  sub _tree_construction_main ($) {
4778                    if ({                    if ({
4779                         tbody => 1, thead => 1, tfoot => 1,                         tbody => 1, thead => 1, tfoot => 1,
4780                        }->{$node->[1]}) {                        }->{$node->[1]}) {
4781                        !!!cp ('t237');
4782                      $i = $_;                      $i = $_;
4783                      last INSCOPE;                      last INSCOPE;
4784                    } elsif ({                    } elsif ({
4785                              table => 1, html => 1,                              table => 1, html => 1,
4786                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4787                        !!!cp ('t238');
4788                      last INSCOPE;                      last INSCOPE;
4789                    }                    }
4790                  } # INSCOPE                  } # INSCOPE
4791                  unless (defined $i) {                  unless (defined $i) {
4792                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t239');
4793                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4794                    ## Ignore the token                    ## Ignore the token
4795                    !!!next-token;                    !!!next-token;
4796                    redo B;                    redo B;
# Line 3847  sub _tree_construction_main ($) { Line 4800  sub _tree_construction_main ($) {
4800                  while (not {                  while (not {
4801                    tbody => 1, tfoot => 1, thead => 1, html => 1,                    tbody => 1, tfoot => 1, thead => 1, html => 1,
4802                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4803                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t240');
4804                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4805                  }                  }
4806                                    
# Line 3863  sub _tree_construction_main ($) { Line 4816  sub _tree_construction_main ($) {
4816                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
4817                }                }
4818    
4819                  ## NOTE: </table> in the "in table" insertion mode.
4820                  ## When you edit the code fragment below, please ensure that
4821                  ## the code for <table> in the "in table" insertion mode
4822                  ## is synced with it.
4823    
4824                ## have a table element in table scope                ## have a table element in table scope
4825                my $i;                my $i;
4826                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4827                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4828                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4829                      !!!cp ('t241');
4830                    $i = $_;                    $i = $_;
4831                    last INSCOPE;                    last INSCOPE;
4832                  } elsif ({                  } elsif ({
4833                            table => 1, html => 1,                            table => 1, html => 1,
4834                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4835                      !!!cp ('t242');
4836                    last INSCOPE;                    last INSCOPE;
4837                  }                  }
4838                } # INSCOPE                } # INSCOPE
4839                unless (defined $i) {                unless (defined $i) {
4840                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t243');
4841                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4842                  ## Ignore the token                  ## Ignore the token
4843                  !!!next-token;                  !!!next-token;
4844                  redo B;                  redo B;
4845                }                }
   
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
                 
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
4846                                    
4847                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4848                  pop @{$open_tables};
4849                                
4850                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4851                                
# Line 3915  sub _tree_construction_main ($) { Line 4861  sub _tree_construction_main ($) {
4861                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4862                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4863                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[1] eq $token->{tag_name}) {
4864                        !!!cp ('t247');
4865                      $i = $_;                      $i = $_;
4866                      last INSCOPE;                      last INSCOPE;
4867                    } elsif ({                    } elsif ({
4868                              table => 1, html => 1,                              table => 1, html => 1,
4869                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4870                        !!!cp ('t248');
4871                      last INSCOPE;                      last INSCOPE;
4872                    }                    }
4873                  } # INSCOPE                  } # INSCOPE
4874                    unless (defined $i) {                    unless (defined $i) {
4875                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t249');
4876                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4877                      ## Ignore the token                      ## Ignore the token
4878                      !!!next-token;                      !!!next-token;
4879                      redo B;                      redo B;
# Line 3936  sub _tree_construction_main ($) { Line 4885  sub _tree_construction_main ($) {
4885                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4886                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4887                    if ($node->[1] eq 'tr') {                    if ($node->[1] eq 'tr') {
4888                        !!!cp ('t250');
4889                      $i = $_;                      $i = $_;
4890                      last INSCOPE;                      last INSCOPE;
4891                    } elsif ({                    } elsif ({
4892                              table => 1, html => 1,                              table => 1, html => 1,
4893                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4894                        !!!cp ('t251');
4895                      last INSCOPE;                      last INSCOPE;
4896                    }                    }
4897                  } # INSCOPE                  } # INSCOPE
4898                    unless (defined $i) {                    unless (defined $i) {
4899                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!cp ('t252');
4900                        !!!parse-error (type => 'unmatched end tag:tr', token => $token);
4901                      ## Ignore the token                      ## Ignore the token
4902                      !!!next-token;                      !!!next-token;
4903                      redo B;                      redo B;
# Line 3955  sub _tree_construction_main ($) { Line 4907  sub _tree_construction_main ($) {
4907                  while (not {                  while (not {
4908                    tr => 1, html => 1,                    tr => 1, html => 1,
4909                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4910                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t253');
4911    ## ISSUE: Can this case be reached?
4912                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4913                  }                  }
4914                                    
# Line 3969  sub _tree_construction_main ($) { Line 4922  sub _tree_construction_main ($) {
4922                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4923                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4924                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4925                      !!!cp ('t254');
4926                    $i = $_;                    $i = $_;
4927                    last INSCOPE;                    last INSCOPE;
4928                  } elsif ({                  } elsif ({
4929                            table => 1, html => 1,                            table => 1, html => 1,
4930                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4931                      !!!cp ('t255');
4932                    last INSCOPE;                    last INSCOPE;
4933                  }                  }
4934                } # INSCOPE                } # INSCOPE
4935                unless (defined $i) {                unless (defined $i) {
4936                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t256');
4937                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4938                  ## Ignore the token                  ## Ignore the token
4939                  !!!next-token;                  !!!next-token;
4940                  redo B;                  redo B;
# Line 3988  sub _tree_construction_main ($) { Line 4944  sub _tree_construction_main ($) {
4944                while (not {                while (not {
4945                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
4946                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4947                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t257');
4948    ## ISSUE: Can this case be reached?
4949                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4950                }                }
4951    
# Line 4002  sub _tree_construction_main ($) { Line 4959  sub _tree_construction_main ($) {
4959                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4960                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
4961                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4962                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t258');
4963                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4964                ## Ignore the token                ## Ignore the token
4965                !!!next-token;                !!!next-token;
4966                redo B;                redo B;
4967          } else {          } else {
4968            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!cp ('t259');
4969              !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
4970    
4971            $insert = $insert_to_foster;            $insert = $insert_to_foster;
4972            #            #
4973          }          }
4974          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4975            unless ($self->{open_elements}->[-1]->[1] eq 'html' and
4976                    @{$self->{open_elements}} == 1) { # redundant, maybe
4977              !!!parse-error (type => 'in body:#eof', token => $token);
4978              !!!cp ('t259.1');
4979              #
4980            } else {
4981              !!!cp ('t259.2');
4982              #
4983            }
4984    
4985            ## Stop parsing
4986            last B;
4987        } else {        } else {
4988          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4989        }        }
# Line 4020  sub _tree_construction_main ($) { Line 4992  sub _tree_construction_main ($) {
4992              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4993                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4994                unless (length $token->{data}) {                unless (length $token->{data}) {
4995                    !!!cp ('t260');
4996                  !!!next-token;                  !!!next-token;
4997                  redo B;                  redo B;
4998                }                }
4999              }              }
5000                            
5001                !!!cp ('t261');
5002              #              #
5003            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5004              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
5005                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!cp ('t262');
5006                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5007                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5008                !!!next-token;                !!!next-token;
5009                redo B;                redo B;
5010              } else {              } else {
5011                  !!!cp ('t263');
5012                #                #
5013              }              }
5014            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5015              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5016                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] eq 'html') {
5017                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!cp ('t264');
5018                    !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5019                  ## Ignore the token                  ## Ignore the token
5020                  !!!next-token;                  !!!next-token;
5021                  redo B;                  redo B;
5022                } else {                } else {
5023                    !!!cp ('t265');
5024                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5025                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5026                  !!!next-token;                  !!!next-token;
5027                  redo B;                              redo B;            
5028                }                }
5029              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5030                !!!parse-error (type => 'unmatched end tag:col');                !!!cp ('t266');
5031                  !!!parse-error (type => 'unmatched end tag:col', token => $token);
5032                ## Ignore the token                ## Ignore the token
5033                !!!next-token;                !!!next-token;
5034                redo B;                redo B;
5035              } else {              } else {
5036                  !!!cp ('t267');
5037                #                #
5038              }              }
5039            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5040              #          if ($self->{open_elements}->[-1]->[1] eq 'html' or
5041            }              @{$self->{open_elements}} == 1) { # redundant, maybe
5042              !!!cp ('t270.2');
5043              ## Stop parsing.
5044              last B;
5045            } else {
5046              ## NOTE: As if </colgroup>.
5047              !!!cp ('t270.1');
5048              pop @{$self->{open_elements}}; # colgroup
5049              $self->{insertion_mode} = IN_TABLE_IM;
5050              ## Reprocess.
5051              redo B;
5052            }
5053          } else {
5054            die "$0: $token->{type}: Unknown token type";
5055          }
5056    
5057            ## As if </colgroup>            ## As if </colgroup>
5058            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] eq 'html') {
5059              !!!parse-error (type => 'unmatched end tag:colgroup');              !!!cp ('t269');
5060    ## TODO: Wrong error type?
5061                !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5062              ## Ignore the token              ## Ignore the token
5063              !!!next-token;              !!!next-token;
5064              redo B;              redo B;
5065            } else {            } else {
5066                !!!cp ('t270');
5067              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5068              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5069              ## reprocess              ## reprocess
5070              redo B;              redo B;
5071            }            }
5072      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5073        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5074            !!!cp ('t271');
5075          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5076          !!!next-token;          !!!next-token;
5077          redo B;          redo B;
5078        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5079              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
5080                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
5081                    !!!cp ('t272');
5082                  ## As if </option>                  ## As if </option>
5083                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5084                  } else {
5085                    !!!cp ('t273');
5086                }                }
5087    
5088                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5089                !!!next-token;                !!!next-token;
5090                redo B;                redo B;
5091              } elsif ($token->{tag_name} eq 'optgroup') {              } elsif ($token->{tag_name} eq 'optgroup') {
5092                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
5093                    !!!cp ('t274');
5094                  ## As if </option>                  ## As if </option>
5095                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5096                  } else {
5097                    !!!cp ('t275');
5098                }                }
5099    
5100                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
5101                    !!!cp ('t276');
5102                  ## As if </optgroup>                  ## As if </optgroup>
5103                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5104                  } else {
5105                    !!!cp ('t277');
5106                }                }
5107    
5108                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5109                !!!next-token;                !!!next-token;
5110                redo B;                redo B;
5111              } elsif ($token->{tag_name} eq 'select') {          } elsif ($token->{tag_name} eq 'select' or
5112                !!!parse-error (type => 'not closed:select');                   $token->{tag_name} eq 'input' or
5113                ## As if </select> instead                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5114                      {
5115                       caption => 1, table => 1,
5116                       tbody => 1, tfoot => 1, thead => 1,
5117                       tr => 1, td => 1, th => 1,
5118                      }->{$token->{tag_name}})) {
5119              ## TODO: The type below is not good - <select> is replaced by </select>
5120              !!!parse-error (type => 'not closed:select', token => $token);
5121              ## NOTE: As if the token were </select> (<select> case) or
5122              ## as if there were </select> (otherwise).
5123                ## have an element in table scope                ## have an element in table scope
5124                my $i;                my $i;
5125                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5126                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5127                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq 'select') {
5128                      !!!cp ('t278');
5129                    $i = $_;                    $i = $_;
5130                    last INSCOPE;                    last INSCOPE;
5131                  } elsif ({                  } elsif ({
5132                            table => 1, html => 1,                            table => 1, html => 1,
5133                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5134                      !!!cp ('t279');
5135                    last INSCOPE;                    last INSCOPE;
5136                  }                  }
5137                } # INSCOPE                } # INSCOPE
5138                unless (defined $i) {                unless (defined $i) {
5139                  !!!parse-error (type => 'unmatched end tag:select');                  !!!cp ('t280');
5140                    !!!parse-error (type => 'unmatched end tag:select', token => $token);
5141                  ## Ignore the token                  ## Ignore the token
5142                  !!!next-token;                  !!!next-token;
5143                  redo B;                  redo B;
5144                }                }
5145                                
5146                  !!!cp ('t281');
5147                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5148    
5149                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5150    
5151                !!!next-token;            if ($token->{tag_name} eq 'select') {
5152                redo B;              !!!cp ('t281.2');
5153                !!!next-token;
5154                redo B;
5155              } else {
5156                !!!cp ('t281.1');
5157                ## Reprocess the token.
5158                redo B;
5159              }
5160          } else {          } else {
5161            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!cp ('t282');
5162              !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5163            ## Ignore the token            ## Ignore the token
5164            !!!next-token;            !!!next-token;
5165            redo B;            redo B;
# Line 4140  sub _tree_construction_main ($) { Line 5168  sub _tree_construction_main ($) {
5168              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
5169                if ($self->{open_elements}->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
5170                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
5171                    !!!cp ('t283');
5172                  ## As if </option>                  ## As if </option>
5173                  splice @{$self->{open_elements}}, -2;                  splice @{$self->{open_elements}}, -2;
5174                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
5175                    !!!cp ('t284');
5176                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5177                } else {                } else {
5178                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t285');
5179                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5180                  ## Ignore the token                  ## Ignore the token
5181                }                }
5182                !!!next-token;                !!!next-token;
5183                redo B;                redo B;
5184              } elsif ($token->{tag_name} eq 'option') {              } elsif ($token->{tag_name} eq 'option') {
5185                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
5186                    !!!cp ('t286');
5187                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5188                } else {                } else {
5189                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t287');
5190                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5191                  ## Ignore the token                  ## Ignore the token
5192                }                }
5193                !!!next-token;                !!!next-token;
# Line 4165  sub _tree_construction_main ($) { Line 5198  sub _tree_construction_main ($) {
5198                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5199                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5200                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
5201                      !!!cp ('t288');
5202                    $i = $_;                    $i = $_;
5203                    last INSCOPE;                    last INSCOPE;
5204                  } elsif ({                  } elsif ({
5205                            table => 1, html => 1,                            table => 1, html => 1,
5206                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5207                      !!!cp ('t289');
5208                    last INSCOPE;                    last INSCOPE;
5209                  }                  }
5210                } # INSCOPE                } # INSCOPE
5211                unless (defined $i) {                unless (defined $i) {
5212                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t290');
5213                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5214                  ## Ignore the token                  ## Ignore the token
5215                  !!!next-token;                  !!!next-token;
5216                  redo B;                  redo B;
5217                }                }
5218                                
5219                  !!!cp ('t291');
5220                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5221    
5222                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5223    
5224                !!!next-token;                !!!next-token;
5225                redo B;                redo B;
5226              } elsif ({          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5227                        caption => 1, table => 1, tbody => 1,                   {
5228                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    caption => 1, table => 1, tbody => 1,
5229                       }->{$token->{tag_name}}) {                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5230                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                   }->{$token->{tag_name}}) {
5231    ## TODO: The following is wrong?
5232                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5233                                
5234                ## have an element in table scope                ## have an element in table scope
5235                my $i;                my $i;
5236                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5237                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5238                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
5239                      !!!cp ('t292');
5240                    $i = $_;                    $i = $_;
5241                    last INSCOPE;                    last INSCOPE;
5242                  } elsif ({                  } elsif ({
5243                            table => 1, html => 1,                            table => 1, html => 1,
5244                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5245                      !!!cp ('t293');
5246                    last INSCOPE;                    last INSCOPE;
5247                  }                  }
5248                } # INSCOPE                } # INSCOPE
5249                unless (defined $i) {                unless (defined $i) {
5250                    !!!cp ('t294');
5251                  ## Ignore the token                  ## Ignore the token
5252                  !!!next-token;                  !!!next-token;
5253                  redo B;                  redo B;
# Line 4217  sub _tree_construction_main ($) { Line 5259  sub _tree_construction_main ($) {
5259                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5260                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5261                  if ($node->[1] eq 'select') {                  if ($node->[1] eq 'select') {
5262                      !!!cp ('t295');
5263                    $i = $_;                    $i = $_;
5264                    last INSCOPE;                    last INSCOPE;
5265                  } elsif ({                  } elsif ({
5266                            table => 1, html => 1,                            table => 1, html => 1,
5267                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5268    ## ISSUE: Can this state be reached?
5269                      !!!cp ('t296');
5270                    last INSCOPE;                    last INSCOPE;
5271                  }                  }
5272                } # INSCOPE                } # INSCOPE
5273                unless (defined $i) {                unless (defined $i) {
5274                  !!!parse-error (type => 'unmatched end tag:select');                  !!!cp ('t297');
5275    ## TODO: The following error type is correct?
5276                    !!!parse-error (type => 'unmatched end tag:select', token => $token);
5277                  ## Ignore the </select> token                  ## Ignore the </select> token
5278                  !!!next-token; ## TODO: ok?                  !!!next-token; ## TODO: ok?
5279                  redo B;                  redo B;
5280                }                }
5281                                
5282                  !!!cp ('t298');
5283                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5284    
5285                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
# Line 4239  sub _tree_construction_main ($) { Line 5287  sub _tree_construction_main ($) {
5287                ## reprocess                ## reprocess
5288                redo B;                redo B;
5289          } else {          } else {
5290            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!cp ('t299');
5291              !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
5292            ## Ignore the token            ## Ignore the token
5293            !!!next-token;            !!!next-token;
5294            redo B;            redo B;
5295          }          }
5296          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5297            unless ($self->{open_elements}->[-1]->[1] eq 'html' and
5298                    @{$self->{open_elements}} == 1) { # redundant, maybe
5299              !!!cp ('t299.1');
5300              !!!parse-error (type => 'in body:#eof', token => $token);
5301            } else {
5302              !!!cp ('t299.2');
5303            }
5304    
5305            ## Stop parsing.
5306            last B;
5307        } else {        } else {
5308          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5309        }        }
# Line 4257  sub _tree_construction_main ($) { Line 5317  sub _tree_construction_main ($) {
5317            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5318                        
5319            unless (length $token->{data}) {            unless (length $token->{data}) {
5320                !!!cp ('t300');
5321              !!!next-token;              !!!next-token;
5322              redo B;              redo B;
5323            }            }
5324          }          }
5325                    
5326          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5327            !!!parse-error (type => 'after html:#character');            !!!cp ('t301');
5328              !!!parse-error (type => 'after html:#character', token => $token);
5329    
5330            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5331            } else {
5332              !!!cp ('t302');
5333          }          }
5334                    
5335          ## "after body" insertion mode          ## "after body" insertion mode
5336          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#character', token => $token);
5337    
5338          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
5339          ## reprocess          ## reprocess
5340          redo B;          redo B;
5341        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5342          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5343            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!cp ('t303');
5344              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5345                        
5346            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5347            } else {
5348              !!!cp ('t304');
5349          }          }
5350    
5351          ## "after body" insertion mode          ## "after body" insertion mode
5352          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
5353    
5354          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
5355          ## reprocess          ## reprocess
5356          redo B;          redo B;
5357        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5358          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5359            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!cp ('t305');
5360              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
5361                        
5362            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
5363            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5364            } else {
5365              !!!cp ('t306');
5366          }          }
5367    
5368          ## "after body" insertion mode          ## "after body" insertion mode
5369          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
5370            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
5371              !!!parse-error (type => 'unmatched end tag:html');              !!!cp ('t307');
5372                !!!parse-error (type => 'unmatched end tag:html', token => $token);
5373              ## Ignore the token              ## Ignore the token
5374              !!!next-token;              !!!next-token;
5375              redo B;              redo B;
5376            } else {            } else {
5377                !!!cp ('t308');
5378              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
5379              !!!next-token;              !!!next-token;
5380              redo B;              redo B;
5381            }            }
5382          } else {          } else {
5383            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!cp ('t309');
5384              !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
5385    
5386            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
5387            ## reprocess            ## reprocess
5388            redo B;            redo B;
5389          }          }
5390          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5391            !!!cp ('t309.2');
5392            ## Stop parsing
5393            last B;
5394        } else {        } else {
5395          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5396        }        }
# Line 4323  sub _tree_construction_main ($) { Line 5400  sub _tree_construction_main ($) {
5400            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5401                        
5402            unless (length $token->{data}) {            unless (length $token->{data}) {
5403                !!!cp ('t310');
5404              !!!next-token;              !!!next-token;
5405              redo B;              redo B;
5406            }            }
# Line 4330  sub _tree_construction_main ($) { Line 5408  sub _tree_construction_main ($) {
5408                    
5409          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
5410            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5411              !!!parse-error (type => 'in frameset:#character');              !!!cp ('t311');
5412                !!!parse-error (type => 'in frameset:#character', token => $token);
5413            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
5414              !!!parse-error (type => 'after frameset:#character');              !!!cp ('t312');
5415                !!!parse-error (type => 'after frameset:#character', token => $token);
5416            } else { # "after html frameset"            } else { # "after html frameset"
5417              !!!parse-error (type => 'after html:#character');              !!!cp ('t313');
5418                !!!parse-error (type => 'after html:#character', token => $token);
5419    
5420              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5421              ## Reprocess in the "main" phase, "after frameset"...              ## Reprocess in the "after frameset" insertion mode.
5422              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
5423            }            }
5424                        
5425            ## Ignore the token.            ## Ignore the token.
5426            if (length $token->{data}) {            if (length $token->{data}) {
5427                !!!cp ('t314');
5428              ## reprocess the rest of characters              ## reprocess the rest of characters
5429            } else {            } else {
5430                !!!cp ('t315');
5431              !!!next-token;              !!!next-token;
5432            }            }
5433            redo B;            redo B;
# Line 4353  sub _tree_construction_main ($) { Line 5436  sub _tree_construction_main ($) {
5436          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
5437        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5438          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5439            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!cp ('t316');
5440              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5441    
5442            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
5443            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
5444          }          } else {
5445              !!!cp ('t317');
5446            }
5447    
5448          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
5449              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
5450            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t318');
5451              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5452            !!!next-token;            !!!next-token;
5453            redo B;            redo B;
5454          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
5455                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
5456            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t319');
5457              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5458            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
5459            !!!next-token;            !!!next-token;
5460            redo B;            redo B;
5461          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
5462              !!!cp ('t320');
5463            ## NOTE: As if in body.            ## NOTE: As if in body.
5464            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL);
5465            redo B;            redo B;
5466          } else {          } else {
5467            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5468              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!cp ('t321');
5469                !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
5470            } else {            } else {
5471              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!cp ('t322');
5472                !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
5473            }            }
5474            ## Ignore the token            ## Ignore the token
5475            !!!next-token;            !!!next-token;
# Line 4386  sub _tree_construction_main ($) { Line 5477  sub _tree_construction_main ($) {
5477          }          }
5478        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5479          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5480            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!cp ('t323');
5481              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
5482    
5483            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
5484            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
5485            } else {
5486              !!!cp ('t324');
5487          }          }
5488    
5489          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
5490              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
5491            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] eq 'html' and
5492                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
5493              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t325');
5494                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5495              ## Ignore the token              ## Ignore the token
5496              !!!next-token;              !!!next-token;
5497            } else {            } else {
5498                !!!cp ('t326');
5499              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5500              !!!next-token;              !!!next-token;
5501            }            }
5502    
5503            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
5504                $self->{open_elements}->[-1]->[1] ne 'frameset') {                $self->{open_elements}->[-1]->[1] ne 'frameset') {
5505                !!!cp ('t327');
5506              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5507              } else {
5508                !!!cp ('t328');
5509            }            }
5510            redo B;            redo B;
5511          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
5512                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
5513              !!!cp ('t329');
5514            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
5515            !!!next-token;            !!!next-token;
5516            redo B;            redo B;
5517          } else {          } else {
5518            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5519              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!cp ('t330');
5520                !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
5521            } else {            } else {
5522              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!cp ('t331');
5523                !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
5524            }            }
5525            ## Ignore the token            ## Ignore the token
5526            !!!next-token;            !!!next-token;
5527            redo B;            redo B;
5528          }          }
5529          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5530            unless ($self->{open_elements}->[-1]->[1] eq 'html' and
5531                    @{$self->{open_elements}} == 1) { # redundant, maybe
5532              !!!cp ('t331.1');
5533              !!!parse-error (type => 'in body:#eof', token => $token);
5534            } else {
5535              !!!cp ('t331.2');
5536            }
5537            
5538            ## Stop parsing
5539            last B;
5540        } else {        } else {
5541          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5542        }        }
# Line 4436  sub _tree_construction_main ($) { Line 5549  sub _tree_construction_main ($) {
5549      ## "in body" insertion mode      ## "in body" insertion mode
5550      if ($token->{type} == START_TAG_TOKEN) {      if ($token->{type} == START_TAG_TOKEN) {
5551        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
5552            !!!cp ('t332');
5553          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5554          $script_start_tag->($insert);          $script_start_tag->();
5555          redo B;          redo B;
5556        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
5557            !!!cp ('t333');
5558          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5559          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
5560          redo B;          redo B;
5561        } elsif ({        } elsif ({
5562                  base => 1, link => 1,                  base => 1, link => 1,
5563                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5564            !!!cp ('t334');
5565          ## NOTE: This is an "as if in head" code clone, only "-t" differs          ## NOTE: This is an "as if in head" code clone, only "-t" differs
5566          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5567          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
5568          !!!next-token;          !!!next-token;
5569          redo B;          redo B;
5570        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
5571          ## NOTE: This is an "as if in head" code clone, only "-t" differs          ## NOTE: This is an "as if in head" code clone, only "-t" differs
5572          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5573          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
5574    
5575          unless ($self->{confident}) {          unless ($self->{confident}) {
5576            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) { ## TODO: And if supported
5577                !!!cp ('t335');
5578              $self->{change_encoding}              $self->{change_encoding}
5579                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
5580                
5581                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5582                    ->set_user_data (manakai_has_reference =>
5583                                         $token->{attributes}->{charset}
5584                                             ->{has_reference});
5585            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
5586              ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.              ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
5587              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
5588                  =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
5589                        [\x09-\x0D\x20]*=
5590                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
5591                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
5592                  !!!cp ('t336');
5593                $self->{change_encoding}                $self->{change_encoding}
5594                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
5595                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5596                      ->set_user_data (manakai_has_reference =>
5597                                           $token->{attributes}->{content}
5598                                                 ->{has_reference});
5599              }              }
5600            }            }
5601            } else {
5602              if ($token->{attributes}->{charset}) {
5603                !!!cp ('t337');
5604                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5605                    ->set_user_data (manakai_has_reference =>
5606                                         $token->{attributes}->{charset}
5607                                             ->{has_reference});
5608              }
5609              if ($token->{attributes}->{content}) {
5610                !!!cp ('t338');
5611                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5612                    ->set_user_data (manakai_has_reference =>
5613                                         $token->{attributes}->{content}
5614                                             ->{has_reference});
5615              }
5616          }          }
5617    
5618          !!!next-token;          !!!next-token;
5619          redo B;          redo B;
5620        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
5621          !!!parse-error (type => 'in body:title');          !!!cp ('t341');
5622          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5623          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
           if (defined $self->{head_element}) {  
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             $insert->($_[0]);  
           }  
         });  
5624          redo B;          redo B;
5625        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
5626          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body', token => $token);
5627                                
5628          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
5629              $self->{open_elements}->[1]->[1] ne 'body') {              $self->{open_elements}->[1]->[1] ne 'body') {
5630              !!!cp ('t342');
5631            ## Ignore the token            ## Ignore the token
5632          } else {          } else {
5633            my $body_el = $self->{open_elements}->[1]->[0];            my $body_el = $self->{open_elements}->[1]->[0];
5634            for my $attr_name (keys %{$token->{attributes}}) {            for my $attr_name (keys %{$token->{attributes}}) {
5635              unless ($body_el->has_attribute_ns (undef, $attr_name)) {              unless ($body_el->has_attribute_ns (undef, $attr_name)) {
5636                  !!!cp ('t343');
5637                $body_el->set_attribute_ns                $body_el->set_attribute_ns
5638                  (undef, [undef, $attr_name],                  (undef, [undef, $attr_name],
5639                   $token->{attributes}->{$attr_name}->{value});                   $token->{attributes}->{$attr_name}->{value});
# Line 4505  sub _tree_construction_main ($) { Line 5644  sub _tree_construction_main ($) {
5644          redo B;          redo B;
5645        } elsif ({        } elsif ({
5646                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
5647                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1,
5648                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5649                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
5650                  pre => 1,                  pre => 1, listing => 1,
5651                    form => 1,
5652                    table => 1,
5653                    hr => 1,
5654                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5655            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
5656              !!!cp ('t350');
5657              !!!parse-error (type => 'in form:form', token => $token);
5658              ## Ignore the token
5659              !!!next-token;
5660              redo B;
5661            }
5662    
5663          ## has a p element in scope          ## has a p element in scope
5664          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5665            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5666                !!!cp ('t344');
5667              !!!back-token;              !!!back-token;
5668              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
5669                          line => $token->{line}, column => $token->{column}};
5670              redo B;              redo B;
5671            } elsif ({            } elsif ({
5672                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
5673                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5674                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5675                !!!cp ('t345');
5676              last INSCOPE;              last INSCOPE;
5677            }            }
5678          } # INSCOPE          } # INSCOPE
5679                        
5680          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5681          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
5682            !!!next-token;            !!!next-token;
5683            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
5684              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
5685              unless (length $token->{data}) {              unless (length $token->{data}) {
5686                  !!!cp ('t346');
5687                !!!next-token;                !!!next-token;
5688                } else {
5689                  !!!cp ('t349');
5690              }              }
5691              } else {
5692                !!!cp ('t348');
5693            }            }
5694          } else {          } elsif ($token->{tag_name} eq 'form') {
5695              !!!cp ('t347.1');
5696              $self->{form_element} = $self->{open_elements}->[-1]->[0];
5697    
5698            !!!next-token;            !!!next-token;
5699          }          } elsif ($token->{tag_name} eq 'table') {
5700          redo B;            !!!cp ('t382');
5701        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
5702          if (defined $self->{form_element}) {            
5703            !!!parse-error (type => 'in form:form');            $self->{insertion_mode} = IN_TABLE_IM;
5704            ## Ignore the token  
5705              !!!next-token;
5706            } elsif ($token->{tag_name} eq 'hr') {
5707              !!!cp ('t386');
5708              pop @{$self->{open_elements}};
5709            
5710            !!!next-token;            !!!next-token;
           redo B;  
5711          } else {          } else {
5712            ## has a p element in scope            !!!cp ('t347');
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
               redo B;  
             } elsif ({  
                       table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
5713            !!!next-token;            !!!next-token;
           redo B;  
5714          }          }
       } elsif ($token->{tag_name} eq 'li') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'li') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
5715          redo B;          redo B;
5716        } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
5717          ## has a p element in scope          ## has a p element in scope
5718          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5719            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5720                !!!cp ('t353');
5721              !!!back-token;              !!!back-token;
5722              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
5723                          line => $token->{line}, column => $token->{column}};
5724              redo B;              redo B;
5725            } elsif ({            } elsif ({
5726                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
5727                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5728                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5729                !!!cp ('t354');
5730              last INSCOPE;              last INSCOPE;
5731            }            }
5732          } # INSCOPE          } # INSCOPE
# Line 4627  sub _tree_construction_main ($) { Line 5734  sub _tree_construction_main ($) {
5734          ## Step 1          ## Step 1
5735          my $i = -1;          my $i = -1;
5736          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
5737            my $li_or_dtdd = {li => {li => 1},
5738                              dt => {dt => 1, dd => 1},
5739                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
5740          LI: {          LI: {
5741            ## Step 2            ## Step 2
5742            if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {            if ($li_or_dtdd->{$node->[1]}) {
5743              if ($i != -1) {              if ($i != -1) {
5744                  !!!cp ('t355');
5745                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'end tag missing:'.
5746                                $self->{open_elements}->[-1]->[1]);                                $self->{open_elements}->[-1]->[1], token => $token);
5747                } else {
5748                  !!!cp ('t356');
5749              }              }
5750              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
5751              last LI;              last LI;
5752              } else {
5753                !!!cp ('t357');
5754            }            }
5755                        
5756            ## Step 3            ## Step 3
# Line 4644  sub _tree_construction_main ($) { Line 5759  sub _tree_construction_main ($) {
5759                ($special_category->{$node->[1]} or                ($special_category->{$node->[1]} or
5760                 $scoping_category->{$node->[1]}) and                 $scoping_category->{$node->[1]}) and
5761                $node->[1] ne 'address' and $node->[1] ne 'div') {                $node->[1] ne 'address' and $node->[1] ne 'div') {
5762                !!!cp ('t358');
5763              last LI;              last LI;
5764            }            }
5765                        
5766              !!!cp ('t359');
5767            ## Step 4            ## Step 4
5768            $i--;            $i--;
5769            $node = $self->{open_elements}->[$i];            $node = $self->{open_elements}->[$i];
5770            redo LI;            redo LI;
5771          } # LI          } # LI
5772                        
5773          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5774          !!!next-token;          !!!next-token;
5775          redo B;          redo B;
5776        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
5777          ## has a p element in scope          ## has a p element in scope
5778          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5779            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5780                !!!cp ('t367');
5781              !!!back-token;              !!!back-token;
5782              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
5783                          line => $token->{line}, column => $token->{column}};
5784              redo B;              redo B;
5785            } elsif ({            } elsif ({
5786                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
5787                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5788                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5789                !!!cp ('t368');
5790              last INSCOPE;              last INSCOPE;
5791            }            }
5792          } # INSCOPE          } # INSCOPE
5793                        
5794          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5795                        
5796          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
5797                        
5798          !!!next-token;          !!!next-token;
5799          redo B;          redo B;
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>  
         ## has an element in scope  
         #my $i;  
         #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
         #  my $node = $self->{open_elements}->[$_];  
         #  if ({  
         #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
         #      }->{$node->[1]}) {  
         #    $i = $_;  
         #    last INSCOPE;  
         #  } elsif ({  
         #            table => 1, caption => 1, td => 1, th => 1,  
         #            button => 1, marquee => 1, object => 1, html => 1,  
         #           }->{$node->[1]}) {  
         #    last INSCOPE;  
         #  }  
         #} # INSCOPE  
         #    
         #if (defined $i) {  
         #  !!! parse-error (type => 'in hn:hn');  
         #  splice @{$self->{open_elements}}, $i;  
         #}  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         !!!next-token;  
         redo B;  
5800        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
5801          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
5802            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
5803            if ($node->[1] eq 'a') {            if ($node->[1] eq 'a') {
5804              !!!parse-error (type => 'in a:a');              !!!cp ('t371');
5805                !!!parse-error (type => 'in a:a', token => $token);
5806                            
5807              !!!back-token;              !!!back-token;
5808              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
5809              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
5810                $formatting_end_tag->($token);
5811                            
5812              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
5813                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
5814                    !!!cp ('t372');
5815                  splice @$active_formatting_elements, $_, 1;                  splice @$active_formatting_elements, $_, 1;
5816                  last AFE2;                  last AFE2;
5817                }                }
5818              } # AFE2              } # AFE2
5819              OE: for (reverse 0..$#{$self->{open_elements}}) {              OE: for (reverse 0..$#{$self->{open_elements}}) {
5820                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
5821                    !!!cp ('t373');
5822                  splice @{$self->{open_elements}}, $_, 1;                  splice @{$self->{open_elements}}, $_, 1;
5823                  last OE;                  last OE;
5824                }                }
5825              } # OE              } # OE
5826              last AFE;              last AFE;
5827            } elsif ($node->[0] eq '#marker') {            } elsif ($node->[0] eq '#marker') {
5828                !!!cp ('t374');
5829              last AFE;              last AFE;
5830            }            }
5831          } # AFE          } # AFE
5832                        
5833          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5834    
5835          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5836          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
5837    
5838          !!!next-token;          !!!next-token;
5839          redo B;          redo B;
       } elsif ({  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         redo B;  
5840        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
5841          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5842    
# Line 4776  sub _tree_construction_main ($) { Line 5844  sub _tree_construction_main ($) {
5844          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5845            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
5846            if ($node->[1] eq 'nobr') {            if ($node->[1] eq 'nobr') {
5847              !!!parse-error (type => 'in nobr:nobr');              !!!cp ('t376');
5848                !!!parse-error (type => 'in nobr:nobr', token => $token);
5849              !!!back-token;              !!!back-token;
5850              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
5851                          line => $token->{line}, column => $token->{column}};
5852              redo B;              redo B;
5853            } elsif ({            } elsif ({
5854                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
5855                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5856                     }->{$node->[1]}) {                     }->{$node->[1]}) {
5857                !!!cp ('t377');
5858              last INSCOPE;              last INSCOPE;
5859            }            }
5860          } # INSCOPE          } # INSCOPE
5861                    
5862          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5863          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
5864                    
5865          !!!next-token;          !!!next-token;
# Line 4798  sub _tree_construction_main ($) { Line 5869  sub _tree_construction_main ($) {
5869          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5870            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
5871            if ($node->[1] eq 'button') {            if ($node->[1] eq 'button') {
5872              !!!parse-error (type => 'in button:button');              !!!cp ('t378');
5873                !!!parse-error (type => 'in button:button', token => $token);
5874              !!!back-token;              !!!back-token;
5875              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button',
5876                          line => $token->{line}, column => $token->{column}};
5877              redo B;              redo B;
5878            } elsif ({            } elsif ({
5879                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
5880                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5881                     }->{$node->[1]}) {                     }->{$node->[1]}) {
5882                !!!cp ('t379');
5883              last INSCOPE;              last INSCOPE;
5884            }            }
5885          } # INSCOPE          } # INSCOPE
5886                        
5887          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5888                        
5889          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5890          push @$active_formatting_elements, ['#marker', ''];  
5891            ## TODO: associate with $self->{form_element} if defined
5892    
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
5893          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
5894            
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = IN_TABLE_IM;  
             
5895          !!!next-token;          !!!next-token;
5896          redo B;          redo B;
5897        } elsif ({        } elsif ({
5898                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
5899                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
5900                  image => 1,                  noembed => 1,
5901                    noframes => 1,
5902                    noscript => 0, ## TODO: 1 if scripting is enabled
5903                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5904          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
5905            !!!parse-error (type => 'image');            !!!cp ('t381');
5906            $token->{tag_name} = 'img';            $reconstruct_active_formatting_elements->($insert_to_current);
5907            } else {
5908              !!!cp ('t399');
5909          }          }
5910            ## NOTE: There is an "as if in body" code clone.
5911          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'input') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         ## TODO: associate with $self->{form_element} if defined  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
5912          redo B;          redo B;
5913        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
5914          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
5915                    
5916          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
5917              !!!cp ('t389');
5918            ## Ignore the token            ## Ignore the token
5919            !!!next-token;            !!!next-token;
5920            redo B;            redo B;
# Line 4915  sub _tree_construction_main ($) { Line 5928  sub _tree_construction_main ($) {
5928            delete $at->{prompt};            delete $at->{prompt};
5929            my @tokens = (            my @tokens = (
5930                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
5931                           attributes => $form_attrs},                           attributes => $form_attrs,
5932                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
5933                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
5934                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
5935                            {type => START_TAG_TOKEN, tag_name => 'p',
5936                             line => $token->{line}, column => $token->{column}},
5937                            {type => START_TAG_TOKEN, tag_name => 'label',
5938                             line => $token->{line}, column => $token->{column}},
5939                         );                         );
5940            if ($prompt_attr) {            if ($prompt_attr) {
5941              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              !!!cp ('t390');
5942                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
5943                               #line => $token->{line}, column => $token->{column},
5944                              };
5945            } else {            } else {
5946                !!!cp ('t391');
5947              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
5948                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
5949                               #line => $token->{line}, column => $token->{column},
5950                              }; # SHOULD
5951              ## TODO: make this configurable              ## TODO: make this configurable
5952            }            }
5953            push @tokens,            push @tokens,
5954                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
5955                             line => $token->{line}, column => $token->{column}},
5956                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
5957                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
5958                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
5959                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
5960                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
5961                            {type => START_TAG_TOKEN, tag_name => 'hr',
5962                             line => $token->{line}, column => $token->{column}},
5963                            {type => END_TAG_TOKEN, tag_name => 'form',
5964                             line => $token->{line}, column => $token->{column}};
5965            $token = shift @tokens;            $token = shift @tokens;
5966            !!!back-token (@tokens);            !!!back-token (@tokens);
5967            redo B;            redo B;
# Line 4941  sub _tree_construction_main ($) { Line 5969  sub _tree_construction_main ($) {
5969        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
5970          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
5971          my $el;          my $el;
5972          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $token->{tag_name}, $token->{attributes}, $token);
5973                    
5974          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
5975          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 4954  sub _tree_construction_main ($) { Line 5982  sub _tree_construction_main ($) {
5982          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
5983            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
5984            unless (length $token->{data}) {            unless (length $token->{data}) {
5985                !!!cp ('t392');
5986              !!!next-token;              !!!next-token;
5987              } else {
5988                !!!cp ('t393');
5989            }            }
5990            } else {
5991              !!!cp ('t394');
5992          }          }
5993          while ($token->{type} == CHARACTER_TOKEN) {          while ($token->{type} == CHARACTER_TOKEN) {
5994              !!!cp ('t395');
5995            $text .= $token->{data};            $text .= $token->{data};
5996            !!!next-token;            !!!next-token;
5997          }          }
5998          if (length $text) {          if (length $text) {
5999              !!!cp ('t396');
6000            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
6001          }          }
6002                    
# Line 4969  sub _tree_construction_main ($) { Line 6004  sub _tree_construction_main ($) {
6004                    
6005          if ($token->{type} == END_TAG_TOKEN and          if ($token->{type} == END_TAG_TOKEN and
6006              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
6007              !!!cp ('t397');
6008            ## Ignore the token            ## Ignore the token
6009          } else {          } else {
6010            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!cp ('t398');
6011              !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6012          }          }
6013          !!!next-token;          !!!next-token;
6014          redo B;          redo B;
6015        } elsif ({        } elsif ({
                 iframe => 1,  
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         ## NOTE: There is an "as if in body" code clone.  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'select') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $self->{insertion_mode} = IN_SELECT_IM;  
         !!!next-token;  
         redo B;  
       } elsif ({  
6016                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6017                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
6018                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
6019                  thead => 1, tr => 1,                  thead => 1, tr => 1,
6020                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6021          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!cp ('t401');
6022            !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6023          ## Ignore the token          ## Ignore the token
6024          !!!next-token;          !!!next-token;
6025          redo B;          redo B;
6026                    
6027          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6028        } else {        } else {
6029            if ($token->{tag_name} eq 'image') {
6030              !!!cp ('t384');
6031              !!!parse-error (type => 'image', token => $token);
6032              $token->{tag_name} = 'img';
6033            } else {
6034              !!!cp ('t385');
6035            }
6036    
6037            ## NOTE: There is an "as if <br>" code clone.
6038          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6039                    
6040          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6041    
6042            if ({
6043                 applet => 1, marquee => 1, object => 1,
6044                }->{$token->{tag_name}}) {
6045              !!!cp ('t380');
6046              push @$active_formatting_elements, ['#marker', ''];
6047            } elsif ({
6048                      b => 1, big => 1, em => 1, font => 1, i => 1,
6049                      s => 1, small => 1, strile => 1,
6050                      strong => 1, tt => 1, u => 1,
6051                     }->{$token->{tag_name}}) {
6052              !!!cp ('t375');
6053              push @$active_formatting_elements, $self->{open_elements}->[-1];
6054            } elsif ($token->{tag_name} eq 'input') {
6055              !!!cp ('t388');
6056              ## TODO: associate with $self->{form_element} if defined
6057              pop @{$self->{open_elements}};
6058            } elsif ({
6059                      area => 1, basefont => 1, bgsound => 1, br => 1,
6060                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6061                      #image => 1,
6062                     }->{$token->{tag_name}}) {
6063              !!!cp ('t388.1');
6064              pop @{$self->{open_elements}};
6065            } elsif ($token->{tag_name} eq 'select') {
6066              ## TODO: associate with $self->{form_element} if defined
6067            
6068              if ($self->{insertion_mode} & TABLE_IMS or
6069                  $self->{insertion_mode} & BODY_TABLE_IMS or
6070                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6071                !!!cp ('t400.1');
6072                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6073              } else {
6074                !!!cp ('t400.2');
6075                $self->{insertion_mode} = IN_SELECT_IM;
6076              }
6077            } else {
6078              !!!cp ('t402');
6079            }
6080                    
6081          !!!next-token;          !!!next-token;
6082          redo B;          redo B;
6083        }        }
6084      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6085        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
6086          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
6087              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
6088            for (@{$self->{open_elements}}) {          INSCOPE: {
6089              unless ({            for (reverse @{$self->{open_elements}}) {
6090                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] eq 'body') {
6091                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
6092                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
6093                      }->{$_->[1]}) {                last INSCOPE;
6094                !!!parse-error (type => 'not closed:'.$_->[1]);              } elsif ({
6095                          applet => 1, table => 1, caption => 1, td => 1, th => 1,
6096                          button => 1, marquee => 1, object => 1, html => 1,
6097                         }->{$_->[1]}) {
6098                  !!!cp ('t405.1');
6099                  last;
6100              }              }
6101            }            }
6102    
6103            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
6104            !!!next-token;                            value => $token->{tag_name}, token => $token);
6105            redo B;            ## NOTE: Ignore the token.
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
6106            !!!next-token;            !!!next-token;
6107            redo B;            redo B;
6108            } # INSCOPE
6109    
6110            for (@{$self->{open_elements}}) {
6111              unless ({
6112                       dd => 1, dt => 1, li => 1, p => 1, td => 1,
6113                       th => 1, tr => 1, body => 1, html => 1,
6114                       tbody => 1, tfoot => 1, thead => 1,
6115                      }->{$_->[1]}) {
6116                !!!cp ('t403');
6117                !!!parse-error (type => 'not closed:'.$_->[1], token => $token);
6118                last;
6119              } else {
6120                !!!cp ('t404');
6121              }
6122          }          }
6123    
6124            $self->{insertion_mode} = AFTER_BODY_IM;
6125            !!!next-token;
6126            redo B;
6127        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
6128          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
6129            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6130            if ($self->{open_elements}->[-1]->[1] ne 'body') {            if ($self->{open_elements}->[-1]->[1] ne 'body') {
6131              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!cp ('t406');
6132                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1], token => $token);
6133              } else {
6134                !!!cp ('t407');
6135            }            }
6136            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6137            ## reprocess            ## reprocess
6138            redo B;            redo B;
6139          } else {          } else {
6140            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t408');
6141              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6142            ## Ignore the token            ## Ignore the token
6143            !!!next-token;            !!!next-token;
6144            redo B;            redo B;
# Line 5054  sub _tree_construction_main ($) { Line 6147  sub _tree_construction_main ($) {
6147                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6148                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
6149                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
6150                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
6151                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
6152                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6153          ## has an element in scope          ## has an element in scope
6154          my $i;          my $i;
6155          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6156            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6157            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] eq $token->{tag_name}) {
6158              ## generate implied end tags              !!!cp ('t410');
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
6159              $i = $_;              $i = $_;
6160              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
6161            } elsif ({            } elsif ({
6162                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
6163                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
6164                     }->{$node->[1]}) {                     }->{$node->[1]}) {
6165                !!!cp ('t411');
6166              last INSCOPE;              last INSCOPE;
6167            }            }
6168          } # INSCOPE          } # INSCOPE
6169            
6170          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6171            if (defined $i) {            !!!cp ('t413');
6172              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6173            } else {
6174              ## Step 1. generate implied end tags
6175              while ({
6176                      dd => ($token->{tag_name} ne 'dd'),
6177                      dt => ($token->{tag_name} ne 'dt'),
6178                      li => ($token->{tag_name} ne 'li'),
6179                      p => 1,
6180                     }->{$self->{open_elements}->[-1]->[1]}) {
6181                !!!cp ('t409');
6182                pop @{$self->{open_elements}};
6183              }
6184    
6185              ## Step 2.
6186              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6187                !!!cp ('t412');
6188                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
6189            } else {            } else {
6190              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t414');
6191            }            }
6192          }  
6193                      ## Step 3.
         if (defined $i) {  
6194            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6195          } elsif ($token->{tag_name} eq 'p') {  
6196            ## As if <p>, then reprocess the current token            ## Step 4.
6197            my $el;            $clear_up_to_marker->()
6198            !!!create-element ($el, 'p');                if {
6199            $insert->($el);                  applet => 1, button => 1, marquee => 1, object => 1,
6200                  }->{$token->{tag_name}};
6201          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
6202          !!!next-token;          !!!next-token;
6203          redo B;          redo B;
6204        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
6205            undef $self->{form_element};
6206    
6207          ## has an element in scope          ## has an element in scope
6208            my $i;
6209          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6210            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6211            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] eq $token->{tag_name}) {
6212              ## generate implied end tags              !!!cp ('t418');
6213              if ({              $i = $_;
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
6214              last INSCOPE;              last INSCOPE;
6215            } elsif ({            } elsif ({
6216                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
6217                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
6218                     }->{$node->[1]}) {                     }->{$node->[1]}) {
6219                !!!cp ('t419');
6220              last INSCOPE;              last INSCOPE;
6221            }            }
6222          } # INSCOPE          } # INSCOPE
6223            
6224          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6225            pop @{$self->{open_elements}};            !!!cp ('t421');
6226          } else {            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6227            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          } else {
6228              ## Step 1. generate implied end tags
6229              while ({
6230                      dd => 1, dt => 1, li => 1, p => 1,
6231                     }->{$self->{open_elements}->[-1]->[1]}) {
6232                !!!cp ('t417');
6233                pop @{$self->{open_elements}};
6234              }
6235              
6236              ## Step 2.
6237              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6238                !!!cp ('t417.1');
6239                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
6240              } else {
6241                !!!cp ('t420');
6242              }  
6243              
6244              ## Step 3.
6245              splice @{$self->{open_elements}}, $i;
6246          }          }
6247    
         undef $self->{form_element};  
6248          !!!next-token;          !!!next-token;
6249          redo B;          redo B;
6250        } elsif ({        } elsif ({
# Line 5153  sub _tree_construction_main ($) { Line 6257  sub _tree_construction_main ($) {
6257            if ({            if ({
6258                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6259                }->{$node->[1]}) {                }->{$node->[1]}) {
6260              ## generate implied end tags              !!!cp ('t423');
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
6261              $i = $_;              $i = $_;
6262              last INSCOPE;              last INSCOPE;
6263            } elsif ({            } elsif ({
6264                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
6265                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
6266                     }->{$node->[1]}) {                     }->{$node->[1]}) {
6267                !!!cp ('t424');
6268              last INSCOPE;              last INSCOPE;
6269            }            }
6270          } # INSCOPE          } # INSCOPE
6271            
6272          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6273            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t425.1');
6274              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6275            } else {
6276              ## Step 1. generate implied end tags
6277              while ({
6278                      dd => 1, dt => 1, li => 1, p => 1,
6279                     }->{$self->{open_elements}->[-1]->[1]}) {
6280                !!!cp ('t422');
6281                pop @{$self->{open_elements}};
6282              }
6283              
6284              ## Step 2.
6285              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6286                !!!cp ('t425');
6287                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6288              } else {
6289                !!!cp ('t426');
6290              }
6291    
6292              ## Step 3.
6293              splice @{$self->{open_elements}}, $i;
6294          }          }
6295                    
6296          splice @{$self->{open_elements}}, $i if defined $i;          !!!next-token;
6297            redo B;
6298          } elsif ($token->{tag_name} eq 'p') {
6299            ## has an element in scope
6300            my $i;
6301            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6302              my $node = $self->{open_elements}->[$_];
6303              if ($node->[1] eq $token->{tag_name}) {
6304                !!!cp ('t410.1');
6305                $i = $_;
6306                last INSCOPE;
6307              } elsif ({
6308                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
6309                        button => 1, marquee => 1, object => 1, html => 1,
6310                       }->{$node->[1]}) {
6311                !!!cp ('t411.1');
6312                last INSCOPE;
6313              }
6314            } # INSCOPE
6315    
6316            if (defined $i) {
6317              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6318                !!!cp ('t412.1');
6319                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
6320              } else {
6321                !!!cp ('t414.1');
6322              }
6323    
6324              splice @{$self->{open_elements}}, $i;
6325            } else {
6326              !!!cp ('t413.1');
6327              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6328    
6329              !!!cp ('t415.1');
6330              ## As if <p>, then reprocess the current token
6331              my $el;
6332              !!!create-element ($el, 'p',, $token);
6333              $insert->($el);
6334              ## NOTE: Not inserted into |$self->{open_elements}|.
6335            }
6336    
6337          !!!next-token;          !!!next-token;
6338          redo B;          redo B;
6339        } elsif ({        } elsif ({
# Line 5187  sub _tree_construction_main ($) { Line 6342  sub _tree_construction_main ($) {
6342                  nobr => 1, s => 1, small => 1, strile => 1,                  nobr => 1, s => 1, small => 1, strile => 1,
6343                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
6344                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6345          $formatting_end_tag->($token->{tag_name});          !!!cp ('t427');
6346            $formatting_end_tag->($token);
6347          redo B;          redo B;
6348        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
6349          !!!parse-error (type => 'unmatched end tag:br');          !!!cp ('t428');
6350            !!!parse-error (type => 'unmatched end tag:br', token => $token);
6351    
6352          ## As if <br>          ## As if <br>
6353          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6354                    
6355          my $el;          my $el;
6356          !!!create-element ($el, 'br');          !!!create-element ($el, 'br',, $token);
6357          $insert->($el);          $insert->($el);
6358                    
6359          ## Ignore the token.          ## Ignore the token.
# Line 5214  sub _tree_construction_main ($) { Line 6371  sub _tree_construction_main ($) {
6371                  table => 1, textarea => 1, wbr => 1,                  table => 1, textarea => 1, wbr => 1,
6372                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
6373                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6374          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t429');
6375            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6376          ## Ignore the token          ## Ignore the token
6377          !!!next-token;          !!!next-token;
6378          redo B;          redo B;
# Line 5231  sub _tree_construction_main ($) { Line 6389  sub _tree_construction_main ($) {
6389            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] eq $token->{tag_name}) {
6390              ## Step 1              ## Step 1
6391              ## generate implied end tags              ## generate implied end tags
6392              if ({              while ({
6393                   dd => 1, dt => 1, li => 1, p => 1,                      dd => 1, dt => 1, li => 1, p => 1,
6394                   td => 1, th => 1, tr => 1,                     }->{$self->{open_elements}->[-1]->[1]}) {
6395                   tbody => 1, tfoot => 1, thead => 1,                !!!cp ('t430');
6396                  }->{$self->{open_elements}->[-1]->[1]}) {                ## ISSUE: Can this case be reached?
6397                !!!back-token;                pop @{$self->{open_elements}};
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
6398              }              }
6399                    
6400              ## Step 2              ## Step 2
6401              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
6402                  !!!cp ('t431');
6403                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
6404                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
6405                } else {
6406                  !!!cp ('t432');
6407              }              }
6408                            
6409              ## Step 3              ## Step 3
# Line 5259  sub _tree_construction_main ($) { Line 6417  sub _tree_construction_main ($) {
6417                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
6418                  ($special_category->{$node->[1]} or                  ($special_category->{$node->[1]} or
6419                   $scoping_category->{$node->[1]})) {                   $scoping_category->{$node->[1]})) {
6420                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t433');
6421                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6422                ## Ignore the token                ## Ignore the token
6423                !!!next-token;                !!!next-token;
6424                last S2;                last S2;
6425              }              }
6426    
6427                !!!cp ('t434');
6428            }            }
6429                        
6430            ## Step 4            ## Step 4
# Line 5279  sub _tree_construction_main ($) { Line 6440  sub _tree_construction_main ($) {
6440      redo B;      redo B;
6441    } # B    } # B
6442    
   ## NOTE: The "trailing end" phase in HTML5 is split into  
   ## two insertion modes: "after html body" and "after html frameset".  
   ## NOTE: States in the main stage is preserved while  
   ## the parser stays in the trailing end phase. # MUST  
   
6443    ## Stop parsing # MUST    ## Stop parsing # MUST
6444        
6445    ## TODO: script stuffs    ## TODO: script stuffs
# Line 5325  sub set_inner_html ($$$) { Line 6481  sub set_inner_html ($$$) {
6481      my $p = $class->new;      my $p = $class->new;
6482      $p->{document} = $doc;      $p->{document} = $doc;
6483    
6484      ## Step 9 # MUST      ## Step 8 # MUST
6485      my $i = 0;      my $i = 0;
6486      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
6487      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
6488      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
6489        my $self = shift;        my $self = shift;
6490    
6491        pop @{$self->{prev_input_character}};        pop @{$self->{prev_char}};
6492        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        unshift @{$self->{prev_char}}, $self->{next_char};
6493    
6494        $self->{next_input_character} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
6495        $self->{next_input_character} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
6496        $column++;  
6497          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
6498        if ($self->{next_input_character} == 0x000A) { # LF        $p->{column}++;
6499          $line++;  
6500          $column = 0;        if ($self->{next_char} == 0x000A) { # LF
6501        } elsif ($self->{next_input_character} == 0x000D) { # CR          $p->{line}++;
6502            $p->{column} = 0;
6503            !!!cp ('i1');
6504          } elsif ($self->{next_char} == 0x000D) { # CR
6505          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
6506          $self->{next_input_character} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
6507          $line++;          $p->{line}++;
6508          $column = 0;          $p->{column} = 0;
6509        } elsif ($self->{next_input_character} > 0x10FFFF) {          !!!cp ('i2');
6510          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        } elsif ($self->{next_char} > 0x10FFFF) {
6511        } elsif ($self->{next_input_character} == 0x0000) { # NULL          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
6512            !!!cp ('i3');
6513          } elsif ($self->{next_char} == 0x0000) { # NULL
6514            !!!cp ('i4');
6515          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
6516          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
6517        }        }
6518      };      };
6519      $p->{prev_input_character} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
6520      $p->{next_input_character} = -1;      $p->{next_char} = -1;
6521            
6522      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
6523        my (%opt) = @_;        my (%opt) = @_;
6524        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
6525          my $column = $opt{column};
6526          if (defined $opt{token} and defined $opt{token}->{line}) {
6527            $line = $opt{token}->{line};
6528            $column = $opt{token}->{column};
6529          }
6530          warn "Parse error ($opt{type}) at line $line column $column\n";
6531      };      };
6532      $p->{parse_error} = sub {      $p->{parse_error} = sub {
6533        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
6534      };      };
6535            
6536      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
6537      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
6538    
6539      ## Step 2      ## Step 2
6540      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
6541      $p->{content_model} = {      $p->{content_model} = {
6542        title => RCDATA_CONTENT_MODEL,        title => RCDATA_CONTENT_MODEL,
6543        textarea => RCDATA_CONTENT_MODEL,        textarea => RCDATA_CONTENT_MODEL,
# Line 5388  sub set_inner_html ($$$) { Line 6556  sub set_inner_html ($$$) {
6556    
6557      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
6558    
6559      ## Step 4      ## Step 3
6560      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
6561        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
6562    
6563      ## Step 5 # MUST      ## Step 4 # MUST
6564      $doc->append_child ($root);      $doc->append_child ($root);
6565    
6566      ## Step 6 # MUST      ## Step 5 # MUST
6567      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, 'html'];
6568    
6569      undef $p->{head_element};      undef $p->{head_element};
6570    
6571      ## Step 7 # MUST      ## Step 6 # MUST
6572      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
6573    
6574      ## Step 8 # MUST      ## Step 7 # MUST
6575      my $anode = $node;      my $anode = $node;
6576      AN: while (defined $anode) {      AN: while (defined $anode) {
6577        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
6578          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
6579          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
6580            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
6581                !!!cp ('i5');
6582              $p->{form_element} = $anode;              $p->{form_element} = $anode;
6583              last AN;              last AN;
6584            }            }
# Line 5418  sub set_inner_html ($$$) { Line 6587  sub set_inner_html ($$$) {
6587        $anode = $anode->parent_node;        $anode = $anode->parent_node;
6588      } # AN      } # AN
6589            
6590      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
6591      {      {
6592        my $self = $p;        my $self = $p;
6593        !!!next-token;        !!!next-token;
6594      }      }
6595      $p->_tree_construction_main;      $p->_tree_construction_main;
6596    
6597      ## Step 11 # MUST      ## Step 10 # MUST
6598      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
6599      for (@cn) {      for (@cn) {
6600        $node->remove_child ($_);        $node->remove_child ($_);
6601      }      }
6602      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
6603    
6604      ## Step 12 # MUST      ## Step 11 # MUST
6605      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
6606      for (@cn) {      for (@cn) {
6607        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 5442  sub set_inner_html ($$$) { Line 6610  sub set_inner_html ($$$) {
6610      ## ISSUE: mutation events?      ## ISSUE: mutation events?
6611    
6612      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
6613    
6614        delete $p->{parse_error}; # delete loop
6615    } else {    } else {
6616      die "$0: |set_inner_html| is not defined for node of type $nt";      die "$0: |set_inner_html| is not defined for node of type $nt";
6617    }    }

Legend:
Removed from v.1.65  
changed lines
  Added in v.1.121

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24