/[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.89 by wakaba, Sat Mar 8 02:48:20 2008 UTC revision 1.120 by wakaba, Thu Mar 20 03:57:00 2008 UTC
# Line 74  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 108  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 126  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 177  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_char} = sub {    $self->{set_next_char} = sub {
184      my $self = shift;      my $self = shift;
185    
# Line 187  sub parse_string ($$$;$) { Line 188  sub parse_string ($$$;$) {
188    
189      $self->{next_char} = -1 and return if $i >= length $$s;      $self->{next_char} = -1 and return if $i >= length $$s;
190      $self->{next_char} = ord substr $$s, $i++, 1;      $self->{next_char} = ord substr $$s, $i++, 1;
191      $column++;  
192        ($self->{line_prev}, $self->{column_prev})
193            = ($self->{line}, $self->{column});
194        $self->{column}++;
195            
196      if ($self->{next_char} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
197        $line++;        $self->{line}++;
198        $column = 0;        $self->{column} = 0;
199      } elsif ($self->{next_char} == 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_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
202        $line++;        $self->{line}++;
203        $column = 0;        $self->{column} = 0;
204      } elsif ($self->{next_char} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
205        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
206      } elsif ($self->{next_char} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
# Line 209  sub parse_string ($$$;$) { Line 213  sub parse_string ($$$;$) {
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 220  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    
# Line 303  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.  ## NOTE: "initial" and "before html" insertion modes have no constants.
317    
# Line 325  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 447  sub _get_next_token ($) { Line 457  sub _get_next_token ($) {
457          #          #
458        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
459          !!!cp (11);          !!!cp (11);
460          !!!emit ({type => END_OF_FILE_TOKEN});          !!!emit ({type => END_OF_FILE_TOKEN,
461                      line => $self->{line}, column => $self->{column}});
462          last A; ## TODO: ok?          last A; ## TODO: ok?
463        } else {        } else {
464          !!!cp (12);          !!!cp (12);
465        }        }
466        # Anything else        # Anything else
467        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
468                     data => chr $self->{next_char}};                     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 463  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, -1);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
483    
# Line 471  sub _get_next_token ($) { Line 486  sub _get_next_token ($) {
486    
487        unless (defined $token) {        unless (defined $token) {
488          !!!cp (13);          !!!cp (13);
489          !!!emit ({type => CHARACTER_TOKEN, data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&',
490                      line => $l, column => $c,
491                     });
492        } else {        } else {
493          !!!cp (14);          !!!cp (14);
494          !!!emit ($token);          !!!emit ($token);
# Line 490  sub _get_next_token ($) { Line 507  sub _get_next_token ($) {
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          }          }
# Line 510  sub _get_next_token ($) { Line 530  sub _get_next_token ($) {
530            !!!cp (19);            !!!cp (19);
531            $self->{current_token}            $self->{current_token}
532              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
533                 tag_name => chr ($self->{next_char} + 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;
# Line 518  sub _get_next_token ($) { Line 540  sub _get_next_token ($) {
540                   $self->{next_char} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
541            !!!cp (20);            !!!cp (20);
542            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
543                              tag_name => chr ($self->{next_char})};                                      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_char} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
550            !!!cp (21);            !!!cp (21);
551            !!!parse-error (type => 'empty start tag');            !!!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_char} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
564            !!!cp (22);            !!!cp (22);
565            !!!parse-error (type => 'pio');            !!!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->{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            ## $self->{next_char} is intentionally left as is
574            redo A;            redo A;
575          } else {          } else {
# Line 543  sub _get_next_token ($) { Line 578  sub _get_next_token ($) {
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 551  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++) {
# Line 569  sub _get_next_token ($) { Line 609  sub _get_next_token ($) {
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              }              }
# Line 588  sub _get_next_token ($) { Line 630  sub _get_next_token ($) {
630              $self->{next_char} = shift @next_char; # reconsume              $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              !!!cp (27);              !!!cp (27);
# Line 601  sub _get_next_token ($) { Line 645  sub _get_next_token ($) {
645            !!!cp (28);            !!!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        }        }
# Line 609  sub _get_next_token ($) { Line 655  sub _get_next_token ($) {
655        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{next_char} and
656            $self->{next_char} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
657          !!!cp (29);          !!!cp (29);
658          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token}
659                            tag_name => chr ($self->{next_char} + 0x0020)};              = {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;
# Line 618  sub _get_next_token ($) { Line 666  sub _get_next_token ($) {
666                 $self->{next_char} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
667          !!!cp (30);          !!!cp (30);
668          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
669                            tag_name => chr ($self->{next_char})};                                    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_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
675          !!!cp (31);          !!!cp (31);
676          !!!parse-error (type => 'empty end tag');          !!!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;
# Line 634  sub _get_next_token ($) { Line 685  sub _get_next_token ($) {
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);          !!!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->{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          ## $self->{next_char} is intentionally left as is
702          redo A;          redo A;
703        }        }
# Line 657  sub _get_next_token ($) { Line 714  sub _get_next_token ($) {
714        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
715          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
716            !!!cp (35);            !!!cp (35);
           $self->{current_token}->{first_start_tag}  
               = 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
# Line 690  sub _get_next_token ($) { Line 745  sub _get_next_token ($) {
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            !!!cp (39);            !!!cp (39);
           $self->{current_token}->{first_start_tag}  
               = 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
# Line 747  sub _get_next_token ($) { Line 800  sub _get_next_token ($) {
800        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
801          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
802            !!!cp (46);            !!!cp (46);
           $self->{current_token}->{first_start_tag}  
               = 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
# Line 770  sub _get_next_token ($) { Line 821  sub _get_next_token ($) {
821        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
822                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
823          !!!cp (49);          !!!cp (49);
824          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
825                                value => ''};              = {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;
# Line 794  sub _get_next_token ($) { Line 847  sub _get_next_token ($) {
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            !!!cp (52);            !!!cp (52);
           $self->{current_token}->{first_start_tag}  
               = 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
# Line 825  sub _get_next_token ($) { Line 876  sub _get_next_token ($) {
876          } else {          } else {
877            !!!cp (56);            !!!cp (56);
878          }          }
879          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
880                                value => ''};              = {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 836  sub _get_next_token ($) { Line 889  sub _get_next_token ($) {
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            !!!cp (57);            !!!cp (57);
892            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!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);            !!!cp (58);
# Line 865  sub _get_next_token ($) { Line 918  sub _get_next_token ($) {
918          $before_leave->();          $before_leave->();
919          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
920            !!!cp (61);            !!!cp (61);
           $self->{current_token}->{first_start_tag}  
               = 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);            !!!cp (62);
# Line 911  sub _get_next_token ($) { Line 962  sub _get_next_token ($) {
962          $before_leave->();          $before_leave->();
963          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
964            !!!cp (66);            !!!cp (66);
           $self->{current_token}->{first_start_tag}  
               = 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
# Line 963  sub _get_next_token ($) { Line 1012  sub _get_next_token ($) {
1012        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1013          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1014            !!!cp (73);            !!!cp (73);
           $self->{current_token}->{first_start_tag}  
               = 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
# Line 987  sub _get_next_token ($) { Line 1034  sub _get_next_token ($) {
1034        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1035                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1036          !!!cp (76);          !!!cp (76);
1037          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1038                                value => ''};              = {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;
# Line 1012  sub _get_next_token ($) { Line 1061  sub _get_next_token ($) {
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            !!!cp (79);            !!!cp (79);
           $self->{current_token}->{first_start_tag}  
               = 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
# Line 1035  sub _get_next_token ($) { Line 1082  sub _get_next_token ($) {
1082          redo A;          redo A;
1083        } else {        } else {
1084          !!!cp (82);          !!!cp (82);
1085          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1086                                value => ''};              = {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;        
# Line 1069  sub _get_next_token ($) { Line 1118  sub _get_next_token ($) {
1118        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1119          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1120            !!!cp (87);            !!!cp (87);
           $self->{current_token}->{first_start_tag}  
               = 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
# Line 1094  sub _get_next_token ($) { Line 1141  sub _get_next_token ($) {
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            !!!cp (90);            !!!cp (90);
           $self->{current_token}->{first_start_tag}  
               = 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
# Line 1143  sub _get_next_token ($) { Line 1188  sub _get_next_token ($) {
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            !!!cp (97);            !!!cp (97);
           $self->{current_token}->{first_start_tag}  
               = 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
# Line 1187  sub _get_next_token ($) { Line 1230  sub _get_next_token ($) {
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            !!!cp (103);            !!!cp (103);
           $self->{current_token}->{first_start_tag}  
               = 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
# Line 1234  sub _get_next_token ($) { Line 1275  sub _get_next_token ($) {
1275        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1276          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1277            !!!cp (109);            !!!cp (109);
           $self->{current_token}->{first_start_tag}  
               = 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
# Line 1259  sub _get_next_token ($) { Line 1298  sub _get_next_token ($) {
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            !!!cp (112);            !!!cp (112);
           $self->{current_token}->{first_start_tag}  
               = 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
# Line 1331  sub _get_next_token ($) { Line 1368  sub _get_next_token ($) {
1368        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1369          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1370            !!!cp (119);            !!!cp (119);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1371            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1372          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1373            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1377  sub _get_next_token ($) { Line 1412  sub _get_next_token ($) {
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_char} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
# Line 1385  sub _get_next_token ($) { Line 1421  sub _get_next_token ($) {
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_char} == -1) {          } elsif ($self->{next_char} == -1) {
# Line 1393  sub _get_next_token ($) { Line 1429  sub _get_next_token ($) {
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            !!!cp (126);            !!!cp (126);
1437            $token->{data} .= chr ($self->{next_char});            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1438            !!!next-input-character;            !!!next-input-character;
1439            redo BC;            redo BC;
1440          }          }
# Line 1408  sub _get_next_token ($) { Line 1444  sub _get_next_token ($) {
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_char};        push @next_char, $self->{next_char};
1451                
# Line 1416  sub _get_next_token ($) { Line 1454  sub _get_next_token ($) {
1454          push @next_char, $self->{next_char};          push @next_char, $self->{next_char};
1455          if ($self->{next_char} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1456            !!!cp (127);            !!!cp (127);
1457            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};            $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;
# Line 1452  sub _get_next_token ($) { Line 1492  sub _get_next_token ($) {
1492                      !!!cp (129);                      !!!cp (129);
1493                      ## TODO: What a stupid code this is!                      ## 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 {                    } else {
# Line 1480  sub _get_next_token ($) { Line 1524  sub _get_next_token ($) {
1524        $self->{next_char} = 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
# Line 1603  sub _get_next_token ($) { Line 1650  sub _get_next_token ($) {
1650          redo A;          redo A;
1651        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
1652          !!!cp (152);          !!!cp (152);
1653          !!!parse-error (type => 'dash in comment');          !!!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;
# Line 1619  sub _get_next_token ($) { Line 1668  sub _get_next_token ($) {
1668          redo A;          redo A;
1669        } else {        } else {
1670          !!!cp (154);          !!!cp (154);
1671          !!!parse-error (type => 'dash in 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          $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;
# Line 1658  sub _get_next_token ($) { Line 1709  sub _get_next_token ($) {
1709          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1710          !!!next-input-character;          !!!next-input-character;
1711    
1712          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
1713    
1714          redo A;          redo A;
1715        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
# Line 1667  sub _get_next_token ($) { Line 1718  sub _get_next_token ($) {
1718          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1719          ## reconsume          ## reconsume
1720    
1721          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
1722    
1723          redo A;          redo A;
1724        } else {        } else {
1725          !!!cp (160);          !!!cp (160);
1726          $self->{current_token}          $self->{current_token}->{name} = chr $self->{next_char};
1727              = {type => DOCTYPE_TOKEN,          delete $self->{current_token}->{quirks};
                name => chr ($self->{next_char}),  
                #quirks => 0,  
               };  
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 2203  sub _get_next_token ($) { Line 2251  sub _get_next_token ($) {
2251  sub _tokenize_attempt_to_consume_an_entity ($$$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2252    my ($self, $in_attr, $additional) = @_;    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
# Line 2243  sub _tokenize_attempt_to_consume_an_enti Line 2293  sub _tokenize_attempt_to_consume_an_enti
2293            redo X;            redo X;
2294          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2295            !!!cp (1005);            !!!cp (1005);
2296            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2297            !!!back-next-input-character ($x_char, $self->{next_char});            !!!back-next-input-character ($x_char, $self->{next_char});
2298            $self->{next_char} = 0x0023; # #            $self->{next_char} = 0x0023; # #
2299            return undef;            return undef;
# Line 2252  sub _tokenize_attempt_to_consume_an_enti Line 2302  sub _tokenize_attempt_to_consume_an_enti
2302            !!!next-input-character;            !!!next-input-character;
2303          } else {          } else {
2304            !!!cp (1007);            !!!cp (1007);
2305            !!!parse-error (type => 'no refc');            !!!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            !!!cp (1008);            !!!cp (1008);
2310            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!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            !!!cp (1009);            !!!cp (1009);
2314            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!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            !!!cp (1010);            !!!cp (1010);
2318            !!!parse-error (type => 'CR character reference');            !!!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            !!!cp (1011);            !!!cp (1011);
2322            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!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};                  has_reference => 1,
2328                    line => $l, column => $c,
2329                   };
2330        } # X        } # X
2331      } elsif (0x0030 <= $self->{next_char} and      } elsif (0x0030 <= $self->{next_char} and
2332               $self->{next_char} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
# Line 2295  sub _tokenize_attempt_to_consume_an_enti Line 2347  sub _tokenize_attempt_to_consume_an_enti
2347          !!!next-input-character;          !!!next-input-character;
2348        } else {        } else {
2349          !!!cp (1014);          !!!cp (1014);
2350          !!!parse-error (type => 'no refc');          !!!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          !!!cp (1015);          !!!cp (1015);
2355          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!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          !!!cp (1016);          !!!cp (1016);
2359          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!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          !!!cp (1017);          !!!cp (1017);
2363          !!!parse-error (type => 'CR character reference');          !!!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          !!!cp (1018);          !!!cp (1018);
2367          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!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, has_reference => 1};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2372                  line => $l, column => $c,
2373                 };
2374      } else {      } else {
2375        !!!cp (1019);        !!!cp (1019);
2376        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2377        !!!back-next-input-character ($self->{next_char});        !!!back-next-input-character ($self->{next_char});
2378        $self->{next_char} = 0x0023; # #        $self->{next_char} = 0x0023; # #
2379        return undef;        return undef;
# Line 2369  sub _tokenize_attempt_to_consume_an_enti Line 2423  sub _tokenize_attempt_to_consume_an_enti
2423            
2424      if ($match > 0) {      if ($match > 0) {
2425        !!!cp (1023);        !!!cp (1023);
2426        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};        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          !!!cp (1024);          !!!cp (1024);
2433          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
2434                    line => $l, column => $c,
2435                   };
2436        } else {        } else {
2437          !!!cp (1025);          !!!cp (1025);
2438          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2439                    line => $l, column => $c,
2440                   };
2441        }        }
2442      } else {      } else {
2443        !!!cp (1026);        !!!cp (1026);
2444        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero', line => $l, column => $c);
2445        ## NOTE: "No characters are consumed" in the spec.        ## NOTE: "No characters are consumed" in the spec.
2446        return {type => CHARACTER_TOKEN, data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value,
2447                  line => $l, column => $c,
2448                 };
2449      }      }
2450    } else {    } else {
2451      !!!cp (1027);      !!!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 2459  sub _tree_construction_initial ($) { Line 2521  sub _tree_construction_initial ($) {
2521            defined $token->{public_identifier} or            defined $token->{public_identifier} or
2522            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
2523          !!!cp ('t1');          !!!cp ('t1');
2524          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
2525        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
2526          !!!cp ('t2');          !!!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 {        } else {
2530          !!!cp ('t3');          !!!cp ('t3');
2531        }        }
# Line 2602  sub _tree_construction_initial ($) { Line 2664  sub _tree_construction_initial ($) {
2664                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
2665               }->{$token->{type}}) {               }->{$token->{type}}) {
2666        !!!cp ('t14');        !!!cp ('t14');
2667        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
2668        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2669        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
2670        ## reprocess        ## reprocess
# Line 2623  sub _tree_construction_initial ($) { Line 2685  sub _tree_construction_initial ($) {
2685          !!!cp ('t17');          !!!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 "before html" insertion mode.        ## Go to the "before html" insertion mode.
2691        ## reprocess        ## reprocess
# Line 2652  sub _tree_construction_root_element ($) Line 2714  sub _tree_construction_root_element ($)
2714    B: {    B: {
2715        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
2716          !!!cp ('t19');          !!!cp ('t19');
2717          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
2718          ## Ignore the token          ## Ignore the token
2719          ## Stay in the insertion mode.          ## Stay in the insertion mode.
2720          !!!next-token;          !!!next-token;
# Line 2686  sub _tree_construction_root_element ($) Line 2748  sub _tree_construction_root_element ($)
2748        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
2749          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
2750            my $root_element;            my $root_element;
2751            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes});            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes}, $token);
2752            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
2753            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}}, [$root_element, 'html'];
2754    
# Line 2694  sub _tree_construction_root_element ($) Line 2756  sub _tree_construction_root_element ($)
2756              !!!cp ('t24');              !!!cp ('t24');
2757              $self->{application_cache_selection}              $self->{application_cache_selection}
2758                  ->($token->{attributes}->{manifest}->{value});                  ->($token->{attributes}->{manifest}->{value});
2759              ## ISSUE: No relative reference resolution?              ## 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 {            } else {
2764              !!!cp ('t25');              !!!cp ('t25');
2765              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
# Line 2716  sub _tree_construction_root_element ($) Line 2781  sub _tree_construction_root_element ($)
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    
# Line 2743  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}) {
# Line 2908  sub _tree_construction_main ($) { Line 2968  sub _tree_construction_main ($) {
2968      !!!cp ('t39');      !!!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 2947  sub _tree_construction_main ($) { Line 3009  sub _tree_construction_main ($) {
3009          $token->{tag_name} eq $start_tag_name) {          $token->{tag_name} eq $start_tag_name) {
3010        !!!cp ('t42');        !!!cp ('t42');
3011        ## Ignore the token        ## Ignore the token
     } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {  
       !!!cp ('t43');  
       !!!parse-error (type => 'in CDATA:#'.$token->{type});  
     } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {  
       !!!cp ('t44');  
       !!!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 2988  sub _tree_construction_main ($) { Line 3052  sub _tree_construction_main ($) {
3052        ## Ignore the token        ## Ignore the token
3053      } else {      } else {
3054        !!!cp ('t48');        !!!cp ('t48');
3055        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!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      }      }
# Line 3011  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 3031  sub _tree_construction_main ($) { Line 3102  sub _tree_construction_main ($) {
3102        } # AFE        } # AFE
3103        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3104          !!!cp ('t53');          !!!cp ('t53');
3105          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!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 3048  sub _tree_construction_main ($) { Line 3119  sub _tree_construction_main ($) {
3119              last INSCOPE;              last INSCOPE;
3120            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3121              !!!cp ('t55');              !!!cp ('t55');
3122              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!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');            !!!cp ('t56');
# Line 3063  sub _tree_construction_main ($) { Line 3135  sub _tree_construction_main ($) {
3135        } # INSCOPE        } # INSCOPE
3136        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3137          !!!cp ('t57');          !!!cp ('t57');
3138          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!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          !!!cp ('t58');          !!!cp ('t58');
3146          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1],
3147                            token => $end_tag_token);
3148        }        }
3149                
3150        ## Step 2        ## Step 2
# Line 3081  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');            !!!cp ('t59');
3160            $furthest_block = $node;            $furthest_block = $node;
3161            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3167  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 3213  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;
# Line 3245  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                           !!!cp ('t72');      } else {
3350                           $self->{open_elements}->[-1]->[0]->append_child ($child);        !!!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        !!!cp ('t73');        !!!cp ('t73');
3358        !!!parse-error (type => 'DOCTYPE in the middle');        !!!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) {  
         !!!cp ('t74');  
         #  
       } else {  
         ## Generate implied end tags  
         while ({  
                 dd => 1, dt => 1, li => 1, p => 1,  
                }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!cp ('t75');  
           pop @{$self->{open_elements}};  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!cp ('t76');  
           !!!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') {  
 ## ISSUE: This case is never reached.  
           !!!cp ('t77');  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } else {  
           !!!cp ('t78');  
         }  
   
         ## 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          !!!cp ('t79');          !!!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          !!!cp ('t80');          !!!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 {        } else {
3374          !!!cp ('t81');          !!!cp ('t81');
3375        }        }
3376    
3377        !!!cp ('t82');        !!!cp ('t82');
3378        !!!parse-error (type => 'not first start tag');        !!!parse-error (type => 'not first start tag', token => $token);
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)) {
# Line 3337  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');              !!!cp ('t88');
3418              !!!next-token;              !!!next-token;
# Line 3348  sub _tree_construction_main ($) { Line 3423  sub _tree_construction_main ($) {
3423          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3424            !!!cp ('t89');            !!!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 3360  sub _tree_construction_main ($) { Line 3435  sub _tree_construction_main ($) {
3435            !!!cp ('t90');            !!!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 3378  sub _tree_construction_main ($) { Line 3453  sub _tree_construction_main ($) {
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;
# Line 3386  sub _tree_construction_main ($) { Line 3461  sub _tree_construction_main ($) {
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                  !!!cp ('t93');                  !!!cp ('t93');
3464                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});                  !!!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;
# Line 3397  sub _tree_construction_main ($) { Line 3472  sub _tree_construction_main ($) {
3472                  #                  #
3473                } else {                } else {
3474                  !!!cp ('t95');                  !!!cp ('t95');
3475                  !!!parse-error (type => 'in head:head'); # or in head noscript                  !!!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;
# Line 3405  sub _tree_construction_main ($) { Line 3480  sub _tree_construction_main ($) {
3480              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3481                !!!cp ('t96');                !!!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    
# Line 3420  sub _tree_construction_main ($) { Line 3495  sub _tree_construction_main ($) {
3495                  !!!cp ('t98');                  !!!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...
# Line 3431  sub _tree_construction_main ($) { Line 3506  sub _tree_construction_main ($) {
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                  !!!cp ('t100');                  !!!cp ('t100');
3509                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!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 {                } else {
3512                  !!!cp ('t101');                  !!!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;
# Line 3446  sub _tree_construction_main ($) { Line 3521  sub _tree_construction_main ($) {
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                  !!!cp ('t102');                  !!!cp ('t102');
3524                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!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 {                } else {
3527                  !!!cp ('t103');                  !!!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;
# Line 3461  sub _tree_construction_main ($) { Line 3536  sub _tree_construction_main ($) {
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                  !!!cp ('t104');                  !!!cp ('t104');
3539                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!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 {                } else {
3542                  !!!cp ('t105');                  !!!cp ('t105');
3543                }                }
3544                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3545                my $meta_el = 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');                    !!!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')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3555                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
# Line 3488  sub _tree_construction_main ($) { Line 3564  sub _tree_construction_main ($) {
3564                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
3565                      !!!cp ('t107');                      !!!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')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3570                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
3571                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
# Line 3514  sub _tree_construction_main ($) { Line 3591  sub _tree_construction_main ($) {
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;
# Line 3523  sub _tree_construction_main ($) { Line 3600  sub _tree_construction_main ($) {
3600                  !!!cp ('t111');                  !!!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                  !!!cp ('t112');                  !!!cp ('t112');
3609                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!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 {                } else {
3612                  !!!cp ('t113');                  !!!cp ('t113');
# Line 3538  sub _tree_construction_main ($) { Line 3615  sub _tree_construction_main ($) {
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 3549  sub _tree_construction_main ($) { Line 3625  sub _tree_construction_main ($) {
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                  !!!cp ('t114');                  !!!cp ('t114');
3628                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!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 {                } else {
3631                  !!!cp ('t115');                  !!!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');                  !!!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                  !!!cp ('t117');                  !!!cp ('t117');
3647                  !!!parse-error (type => 'in noscript:noscript');                  !!!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;
# Line 3581  sub _tree_construction_main ($) { Line 3657  sub _tree_construction_main ($) {
3657                  !!!cp ('t119');                  !!!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                  !!!cp ('t120');                  !!!cp ('t120');
3666                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!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 {                } else {
3669                  !!!cp ('t121');                  !!!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
# Line 3604  sub _tree_construction_main ($) { Line 3680  sub _tree_construction_main ($) {
3680                  !!!cp ('t122');                  !!!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 3621  sub _tree_construction_main ($) { Line 3697  sub _tree_construction_main ($) {
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');                  !!!cp ('t126');
3703                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
# Line 3642  sub _tree_construction_main ($) { Line 3718  sub _tree_construction_main ($) {
3718                !!!cp ('t129');                !!!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 3661  sub _tree_construction_main ($) { Line 3737  sub _tree_construction_main ($) {
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;
# Line 3670  sub _tree_construction_main ($) { Line 3746  sub _tree_construction_main ($) {
3746                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3747                  !!!cp ('t132');                  !!!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 3683  sub _tree_construction_main ($) { Line 3759  sub _tree_construction_main ($) {
3759                  !!!cp ('t133');                  !!!cp ('t133');
3760                  ## As if </noscript>                  ## As if </noscript>
3761                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3762                  !!!parse-error (type => 'in noscript:/head');                  !!!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 3709  sub _tree_construction_main ($) { Line 3785  sub _tree_construction_main ($) {
3785                  redo B;                  redo B;
3786                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3787                  !!!cp ('t137');                  !!!cp ('t137');
3788                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!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;
# Line 3723  sub _tree_construction_main ($) { Line 3799  sub _tree_construction_main ($) {
3799                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3800                  !!!cp ('t139');                  !!!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    
# Line 3731  sub _tree_construction_main ($) { Line 3807  sub _tree_construction_main ($) {
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                  !!!cp ('t140');                  !!!cp ('t140');
3810                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!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;
# Line 3746  sub _tree_construction_main ($) { Line 3822  sub _tree_construction_main ($) {
3822                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3823                  !!!cp ('t142');                  !!!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    
# Line 3763  sub _tree_construction_main ($) { Line 3839  sub _tree_construction_main ($) {
3839                  #                  #
3840                } else {                } else {
3841                  !!!cp ('t145');                  !!!cp ('t145');
3842                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!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 3774  sub _tree_construction_main ($) { Line 3850  sub _tree_construction_main ($) {
3850                !!!cp ('t146');                !!!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 3790  sub _tree_construction_main ($) { Line 3866  sub _tree_construction_main ($) {
3866              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3867  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
3868                !!!cp ('t148');                !!!cp ('t148');
3869                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!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;
# Line 3800  sub _tree_construction_main ($) { Line 3876  sub _tree_construction_main ($) {
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) {
# Line 3826  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                      !!!cp ('t151');                      !!!cp ('t151');
3962                      $tn = $node->[1];  
3963                      last INSCOPE;                      ## 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                      !!!cp ('t152');                      !!!cp ('t152');
3973                      last INSCOPE;                      ## ISSUE: This case can never be reached, maybe.
3974                    }                      last;
                 } # INSCOPE  
                   unless (defined $tn) {  
                     !!!cp ('t153');  
 ## TODO: This error type is wrong.  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
3975                    }                    }
3976                                    }
3977                  !!!cp ('t154');  
3978                  ## Close the cell                  !!!cp ('t153');
3979                  !!!back-token; # <?>                  !!!parse-error (type => 'start tag not allowed',
3980                  $token = {type => END_TAG_TOKEN, tag_name => $tn};                      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                      !!!cp ('t155');                      if ($node->[1] eq 'caption') {
3994                      $i = $_;                        !!!cp ('t155');
3995                      last INSCOPE;                        $i = $_;
3996                    } elsif ({                        last INSCOPE;
3997                              table => 1, html => 1,                      } elsif ({
3998                             }->{$node->[1]}) {                                table => 1, html => 1,
3999                      !!!cp ('t156');                               }->{$node->[1]}) {
4000                      last INSCOPE;                        !!!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) {  
                     !!!cp ('t157');  
 ## TODO: this type is wrong.  
                     !!!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                  while ({                  while ({
# Line 3892  sub _tree_construction_main ($) { Line 4020  sub _tree_construction_main ($) {
4020    
4021                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4022                    !!!cp ('t159');                    !!!cp ('t159');
4023                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
4024                  } else {                  } else {
4025                    !!!cp ('t160');                    !!!cp ('t160');
4026                  }                  }
# Line 3933  sub _tree_construction_main ($) { Line 4061  sub _tree_construction_main ($) {
4061                  } # INSCOPE                  } # INSCOPE
4062                    unless (defined $i) {                    unless (defined $i) {
4063                      !!!cp ('t165');                      !!!cp ('t165');
4064                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!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;
# Line 3949  sub _tree_construction_main ($) { Line 4077  sub _tree_construction_main ($) {
4077    
4078                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
4079                    !!!cp ('t167');                    !!!cp ('t167');
4080                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
4081                  } else {                  } else {
4082                    !!!cp ('t168');                    !!!cp ('t168');
4083                  }                  }
# Line 3964  sub _tree_construction_main ($) { Line 4092  sub _tree_construction_main ($) {
4092                  redo B;                  redo B;
4093                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4094                  !!!cp ('t169');                  !!!cp ('t169');
4095                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!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;
# Line 3976  sub _tree_construction_main ($) { Line 4104  sub _tree_construction_main ($) {
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                      !!!cp ('t171');                      if ($node->[1] eq $token->{tag_name}) {
4111                      $i = $_;                        !!!cp ('t171');
4112                      last INSCOPE;                        $i = $_;
4113                    } elsif ({                        last INSCOPE;
4114                              table => 1, html => 1,                      } elsif ({
4115                             }->{$node->[1]}) {                                table => 1, html => 1,
4116                      !!!cp ('t172');                               }->{$node->[1]}) {
4117                      last INSCOPE;                        !!!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) {  
                     !!!cp ('t173');  
                     !!!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                  while ({                  while ({
# Line 4007  sub _tree_construction_main ($) { Line 4137  sub _tree_construction_main ($) {
4137                                    
4138                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4139                    !!!cp ('t175');                    !!!cp ('t175');
4140                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
4141                  } else {                  } else {
4142                    !!!cp ('t176');                    !!!cp ('t176');
4143                  }                  }
# Line 4022  sub _tree_construction_main ($) { Line 4152  sub _tree_construction_main ($) {
4152                  redo B;                  redo B;
4153                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4154                  !!!cp ('t177');                  !!!cp ('t177');
4155                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!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;
# Line 4038  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                    !!!cp ('t179');                    if ($node->[1] eq $token->{tag_name}) {
4175                    $i = $_;                      !!!cp ('t179');
4176                    last INSCOPE;                      $i = $_;
4177                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
4178                    !!!cp ('t180');                      ## Close the cell
4179                    $tn = $node->[1];                      !!!back-token; # </?>
4180                    ## NOTE: There is exactly one |td| or |th| element                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4181                    ## in scope in the stack of open elements by definition.                                line => $token->{line},
4182                  } elsif ({                                column => $token->{column}};
4183                            table => 1, html => 1,                      redo B;
4184                           }->{$node->[1]}) {                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
4185                    !!!cp ('t181');                      !!!cp ('t180');
4186                    last INSCOPE;                      $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  
               unless (defined $i) {  
4198                  !!!cp ('t182');                  !!!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                } else {                } # INSCOPE
                 !!!cp ('t183');  
               }  
   
               ## 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 4092  sub _tree_construction_main ($) { Line 4224  sub _tree_construction_main ($) {
4224                } # INSCOPE                } # INSCOPE
4225                unless (defined $i) {                unless (defined $i) {
4226                  !!!cp ('t186');                  !!!cp ('t186');
4227                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!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;
# Line 4108  sub _tree_construction_main ($) { Line 4240  sub _tree_construction_main ($) {
4240    
4241                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4242                  !!!cp ('t188');                  !!!cp ('t188');
4243                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
4244                } else {                } else {
4245                  !!!cp ('t189');                  !!!cp ('t189');
4246                }                }
# Line 4126  sub _tree_construction_main ($) { Line 4258  sub _tree_construction_main ($) {
4258                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4259                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4260                  !!!cp ('t190');                  !!!cp ('t190');
4261                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!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;
# Line 4140  sub _tree_construction_main ($) { Line 4272  sub _tree_construction_main ($) {
4272                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
4273                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4274                !!!cp ('t192');                !!!cp ('t192');
4275                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!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;
# Line 4148  sub _tree_construction_main ($) { Line 4280  sub _tree_construction_main ($) {
4280                !!!cp ('t193');                !!!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 4156  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                  !!!cp ('t194');              !!!cp ('t194');
4311                  !!!next-token;              !!!next-token;
4312                  redo B;              redo B;
4313                } else {            } else {
4314                  !!!cp ('t195');              !!!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 4213  sub _tree_construction_main ($) { Line 4360  sub _tree_construction_main ($) {
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                !!!cp ('t200');          } else {
4365                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            !!!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 4230  sub _tree_construction_main ($) { Line 4378  sub _tree_construction_main ($) {
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                    !!!cp ('t201');                    !!!cp ('t201');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
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                }                }
# Line 4242  sub _tree_construction_main ($) { Line 4389  sub _tree_construction_main ($) {
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                    !!!cp ('t202');                    !!!cp ('t202');
4392                    !!!parse-error (type => 'missing start tag:tr');                    !!!parse-error (type => 'missing start tag:tr', token => $token);
4393                  }                  }
4394                                    
4395                  ## Clear back to table body context                  ## Clear back to table body context
# Line 4251  sub _tree_construction_main ($) { Line 4398  sub _tree_construction_main ($) {
4398                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4399                    !!!cp ('t203');                    !!!cp ('t203');
4400                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
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                    !!!cp ('t204');                    !!!cp ('t204');
4407                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4408                    !!!next-token;                    !!!next-token;
4409                    redo B;                    redo B;
4410                  } else {                  } else {
4411                    !!!cp ('t205');                    !!!cp ('t205');
4412                    !!!insert-element ('tr');                    !!!insert-element ('tr',, $token);
4413                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
4414                  }                  }
4415                } else {                } else {
# Line 4275  sub _tree_construction_main ($) { Line 4421  sub _tree_construction_main ($) {
4421                  tr => 1, html => 1,                  tr => 1, html => 1,
4422                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4423                  !!!cp ('t207');                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
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 4314  sub _tree_construction_main ($) { Line 4459  sub _tree_construction_main ($) {
4459                  unless (defined $i) {                  unless (defined $i) {
4460                   !!!cp ('t210');                   !!!cp ('t210');
4461  ## TODO: This type is wrong.  ## TODO: This type is wrong.
4462                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                   !!!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 4326  sub _tree_construction_main ($) { Line 4471  sub _tree_construction_main ($) {
4471                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4472                    !!!cp ('t211');                    !!!cp ('t211');
4473                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4474                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4475                  }                  }
4476                                    
# Line 4363  sub _tree_construction_main ($) { Line 4507  sub _tree_construction_main ($) {
4507                  unless (defined $i) {                  unless (defined $i) {
4508                    !!!cp ('t216');                    !!!cp ('t216');
4509  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type ios wrong.
4510                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!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 4375  sub _tree_construction_main ($) { Line 4519  sub _tree_construction_main ($) {
4519                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4520                    !!!cp ('t217');                    !!!cp ('t217');
4521                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4522                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4523                  }                  }
4524                                    
# Line 4399  sub _tree_construction_main ($) { Line 4542  sub _tree_construction_main ($) {
4542                         $self->{open_elements}->[-1]->[1] ne 'html') {                         $self->{open_elements}->[-1]->[1] ne 'html') {
4543                    !!!cp ('t219');                    !!!cp ('t219');
4544                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
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 4417  sub _tree_construction_main ($) { Line 4559  sub _tree_construction_main ($) {
4559                         $self->{open_elements}->[-1]->[1] ne 'html') {                         $self->{open_elements}->[-1]->[1] ne 'html') {
4560                    !!!cp ('t220');                    !!!cp ('t220');
4561                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
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 4438  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 4460  sub _tree_construction_main ($) { Line 4601  sub _tree_construction_main ($) {
4601                unless (defined $i) {                unless (defined $i) {
4602                  !!!cp ('t223');                  !!!cp ('t223');
4603  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
4604                  !!!parse-error (type => 'unmatched end tag:table');                  !!!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                while ({                while ({
4613                        dd => 1, dt => 1, li => 1, p => 1,                        dd => 1, dt => 1, li => 1, p => 1,
# Line 4477  sub _tree_construction_main ($) { Line 4619  sub _tree_construction_main ($) {
4619                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
4620                  !!!cp ('t225');                  !!!cp ('t225');
4621  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
4622                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
4623                } else {                } else {
4624                  !!!cp ('t226');                  !!!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            } elsif ($token->{tag_name} eq 'style') {
4635              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-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 {          } else {
4683            !!!cp ('t227');            !!!cp ('t227');
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
   
           $insert = $insert_to_foster;  
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 4515  sub _tree_construction_main ($) { Line 4708  sub _tree_construction_main ($) {
4708                } # INSCOPE                } # INSCOPE
4709                unless (defined $i) {                unless (defined $i) {
4710                  !!!cp ('t230');                  !!!cp ('t230');
4711                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!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;
# Line 4529  sub _tree_construction_main ($) { Line 4722  sub _tree_construction_main ($) {
4722                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4723                  !!!cp ('t231');                  !!!cp ('t231');
4724  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4725                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4726                }                }
4727    
# Line 4558  sub _tree_construction_main ($) { Line 4750  sub _tree_construction_main ($) {
4750                  unless (defined $i) {                  unless (defined $i) {
4751                    !!!cp ('t235');                    !!!cp ('t235');
4752  ## TODO: The following is wrong.  ## TODO: The following is wrong.
4753                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!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 4570  sub _tree_construction_main ($) { Line 4762  sub _tree_construction_main ($) {
4762                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4763                    !!!cp ('t236');                    !!!cp ('t236');
4764  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4765                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4766                  }                  }
4767                                    
# Line 4599  sub _tree_construction_main ($) { Line 4790  sub _tree_construction_main ($) {
4790                  } # INSCOPE                  } # INSCOPE
4791                  unless (defined $i) {                  unless (defined $i) {
4792                    !!!cp ('t239');                    !!!cp ('t239');
4793                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!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 4610  sub _tree_construction_main ($) { Line 4801  sub _tree_construction_main ($) {
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                    !!!cp ('t240');                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4804                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4805                  }                  }
4806                                    
# Line 4626  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}}) {
# Line 4643  sub _tree_construction_main ($) { Line 4838  sub _tree_construction_main ($) {
4838                } # INSCOPE                } # INSCOPE
4839                unless (defined $i) {                unless (defined $i) {
4840                  !!!cp ('t243');                  !!!cp ('t243');
4841                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!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  
               while ({  
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!cp ('t244');  
 ## ISSUE: Can this case be reached?  
                 pop @{$self->{open_elements}};  
               }  
                 
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!cp ('t245');  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               } else {  
                 !!!cp ('t246');  
               }  
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 4693  sub _tree_construction_main ($) { Line 4873  sub _tree_construction_main ($) {
4873                  } # INSCOPE                  } # INSCOPE
4874                    unless (defined $i) {                    unless (defined $i) {
4875                      !!!cp ('t249');                      !!!cp ('t249');
4876                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!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 4717  sub _tree_construction_main ($) { Line 4897  sub _tree_construction_main ($) {
4897                  } # INSCOPE                  } # INSCOPE
4898                    unless (defined $i) {                    unless (defined $i) {
4899                      !!!cp ('t252');                      !!!cp ('t252');
4900                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!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 4729  sub _tree_construction_main ($) { Line 4909  sub _tree_construction_main ($) {
4909                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4910                    !!!cp ('t253');                    !!!cp ('t253');
4911  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4912                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4913                  }                  }
4914                                    
# Line 4755  sub _tree_construction_main ($) { Line 4934  sub _tree_construction_main ($) {
4934                } # INSCOPE                } # INSCOPE
4935                unless (defined $i) {                unless (defined $i) {
4936                  !!!cp ('t256');                  !!!cp ('t256');
4937                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!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 4767  sub _tree_construction_main ($) { Line 4946  sub _tree_construction_main ($) {
4946                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4947                  !!!cp ('t257');                  !!!cp ('t257');
4948  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4949                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4950                }                }
4951    
# Line 4782  sub _tree_construction_main ($) { Line 4960  sub _tree_construction_main ($) {
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                !!!cp ('t258');                !!!cp ('t258');
4963                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!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            !!!cp ('t259');            !!!cp ('t259');
4969            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!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 4812  sub _tree_construction_main ($) { Line 5003  sub _tree_construction_main ($) {
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                !!!cp ('t262');                !!!cp ('t262');
5006                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!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;
# Line 4824  sub _tree_construction_main ($) { Line 5015  sub _tree_construction_main ($) {
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                  !!!cp ('t264');                  !!!cp ('t264');
5018                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!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;
# Line 4837  sub _tree_construction_main ($) { Line 5028  sub _tree_construction_main ($) {
5028                }                }
5029              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5030                !!!cp ('t266');                !!!cp ('t266');
5031                !!!parse-error (type => 'unmatched end tag:col');                !!!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;
# Line 4845  sub _tree_construction_main ($) { Line 5036  sub _tree_construction_main ($) {
5036                !!!cp ('t267');                !!!cp ('t267');
5037                #                #
5038              }              }
5039            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5040              die "$0: $token->{type}: Unknown token type";          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              !!!cp ('t269');              !!!cp ('t269');
5060              !!!parse-error (type => 'unmatched end tag:colgroup');  ## 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;
# Line 4863  sub _tree_construction_main ($) { Line 5069  sub _tree_construction_main ($) {
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');          !!!cp ('t271');
5075          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
# Line 4879  sub _tree_construction_main ($) { Line 5085  sub _tree_construction_main ($) {
5085                  !!!cp ('t273');                  !!!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') {
# Line 4899  sub _tree_construction_main ($) { Line 5105  sub _tree_construction_main ($) {
5105                  !!!cp ('t277');                  !!!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  ## TODO: The type below is not good - <select> is replaced by </select>                   $token->{tag_name} eq 'input' or
5113                !!!parse-error (type => 'not closed:select');                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5114                ## As if </select> instead                    {
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');                    !!!cp ('t278');
5129                    $i = $_;                    $i = $_;
5130                    last INSCOPE;                    last INSCOPE;
# Line 4923  sub _tree_construction_main ($) { Line 5137  sub _tree_construction_main ($) {
5137                } # INSCOPE                } # INSCOPE
5138                unless (defined $i) {                unless (defined $i) {
5139                  !!!cp ('t280');                  !!!cp ('t280');
5140                  !!!parse-error (type => 'unmatched end tag:select');                  !!!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;
# Line 4934  sub _tree_construction_main ($) { Line 5148  sub _tree_construction_main ($) {
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            !!!cp ('t282');            !!!cp ('t282');
5162            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!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 4955  sub _tree_construction_main ($) { Line 5176  sub _tree_construction_main ($) {
5176                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5177                } else {                } else {
5178                  !!!cp ('t285');                  !!!cp ('t285');
5179                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5180                  ## Ignore the token                  ## Ignore the token
5181                }                }
5182                !!!next-token;                !!!next-token;
# Line 4966  sub _tree_construction_main ($) { Line 5187  sub _tree_construction_main ($) {
5187                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5188                } else {                } else {
5189                  !!!cp ('t287');                  !!!cp ('t287');
5190                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!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 4989  sub _tree_construction_main ($) { Line 5210  sub _tree_construction_main ($) {
5210                } # INSCOPE                } # INSCOPE
5211                unless (defined $i) {                unless (defined $i) {
5212                  !!!cp ('t290');                  !!!cp ('t290');
5213                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!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;
# Line 5002  sub _tree_construction_main ($) { Line 5223  sub _tree_construction_main ($) {
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                     }->{$token->{tag_name}}) {
5231  ## TODO: The following is wrong?  ## TODO: The following is wrong?
5232                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!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;
# Line 5051  sub _tree_construction_main ($) { Line 5273  sub _tree_construction_main ($) {
5273                unless (defined $i) {                unless (defined $i) {
5274                  !!!cp ('t297');                  !!!cp ('t297');
5275  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
5276                  !!!parse-error (type => 'unmatched end tag:select');                  !!!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;
# Line 5066  sub _tree_construction_main ($) { Line 5288  sub _tree_construction_main ($) {
5288                redo B;                redo B;
5289          } else {          } else {
5290            !!!cp ('t299');            !!!cp ('t299');
5291            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!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 5092  sub _tree_construction_main ($) { Line 5325  sub _tree_construction_main ($) {
5325                    
5326          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5327            !!!cp ('t301');            !!!cp ('t301');
5328            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#character', token => $token);
5329    
5330            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
5331          } else {          } else {
# Line 5100  sub _tree_construction_main ($) { Line 5333  sub _tree_construction_main ($) {
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
# Line 5108  sub _tree_construction_main ($) { Line 5341  sub _tree_construction_main ($) {
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            !!!cp ('t303');            !!!cp ('t303');
5344            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5345                        
5346            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
5347          } else {          } else {
# Line 5116  sub _tree_construction_main ($) { Line 5349  sub _tree_construction_main ($) {
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
# Line 5124  sub _tree_construction_main ($) { Line 5357  sub _tree_construction_main ($) {
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            !!!cp ('t305');            !!!cp ('t305');
5360            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!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 "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
# Line 5136  sub _tree_construction_main ($) { Line 5369  sub _tree_construction_main ($) {
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              !!!cp ('t307');              !!!cp ('t307');
5372              !!!parse-error (type => 'unmatched end tag:html');              !!!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;
# Line 5148  sub _tree_construction_main ($) { Line 5381  sub _tree_construction_main ($) {
5381            }            }
5382          } else {          } else {
5383            !!!cp ('t309');            !!!cp ('t309');
5384            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!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 5172  sub _tree_construction_main ($) { Line 5409  sub _tree_construction_main ($) {
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              !!!cp ('t311');              !!!cp ('t311');
5412              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#character', token => $token);
5413            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
5414              !!!cp ('t312');              !!!cp ('t312');
5415              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
5416            } else { # "after html frameset"            } else { # "after html frameset"
5417              !!!cp ('t313');              !!!cp ('t313');
5418              !!!parse-error (type => 'after html:#character');              !!!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 "after frameset" insertion mode.              ## 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.
# Line 5200  sub _tree_construction_main ($) { Line 5437  sub _tree_construction_main ($) {
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            !!!cp ('t316');            !!!cp ('t316');
5440            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!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 "after frameset" insertion mode.            ## Process in the "after frameset" insertion mode.
# Line 5211  sub _tree_construction_main ($) { Line 5448  sub _tree_construction_main ($) {
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            !!!cp ('t318');            !!!cp ('t318');
5451            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!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            !!!cp ('t319');            !!!cp ('t319');
5457            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!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');            !!!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              !!!cp ('t321');              !!!cp ('t321');
5469              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
5470            } else {            } else {
5471              !!!cp ('t322');              !!!cp ('t322');
5472              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
5473            }            }
5474            ## Ignore the token            ## Ignore the token
5475            !!!next-token;            !!!next-token;
# Line 5241  sub _tree_construction_main ($) { Line 5478  sub _tree_construction_main ($) {
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            !!!cp ('t323');            !!!cp ('t323');
5481            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!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 "after frameset" insertion mode.            ## Process in the "after frameset" insertion mode.
# Line 5254  sub _tree_construction_main ($) { Line 5491  sub _tree_construction_main ($) {
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              !!!cp ('t325');              !!!cp ('t325');
5494              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!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 {
# Line 5280  sub _tree_construction_main ($) { Line 5517  sub _tree_construction_main ($) {
5517          } else {          } else {
5518            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5519              !!!cp ('t330');              !!!cp ('t330');
5520              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
5521            } else {            } else {
5522              !!!cp ('t331');              !!!cp ('t331');
5523              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!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 5303  sub _tree_construction_main ($) { Line 5551  sub _tree_construction_main ($) {
5551        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
5552          !!!cp ('t332');          !!!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');          !!!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');          !!!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          my $meta_el = 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');              !!!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')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5582                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
# Line 5343  sub _tree_construction_main ($) { Line 5591  sub _tree_construction_main ($) {
5591                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
5592                !!!cp ('t336');                !!!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')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5596                    ->set_user_data (manakai_has_reference =>                    ->set_user_data (manakai_has_reference =>
5597                                         $token->{attributes}->{content}                                         $token->{attributes}->{content}
# Line 5371  sub _tree_construction_main ($) { Line 5619  sub _tree_construction_main ($) {
5619          redo B;          redo B;
5620        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
5621          !!!cp ('t341');          !!!cp ('t341');
         !!!parse-error (type => 'in body:title');  
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}) {  
             !!!cp ('t339');  
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             !!!cp ('t340');  
             $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') {
# Line 5407  sub _tree_construction_main ($) { Line 5646  sub _tree_construction_main ($) {
5646                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
5647                  div => 1, dl => 1, fieldset => 1,                  div => 1, dl => 1, fieldset => 1,
5648                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5649                  listing => 1, 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');              !!!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');              !!!cp ('t345');
# Line 5426  sub _tree_construction_main ($) { Line 5677  sub _tree_construction_main ($) {
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//;
# Line 5440  sub _tree_construction_main ($) { Line 5691  sub _tree_construction_main ($) {
5691            } else {            } else {
5692              !!!cp ('t348');              !!!cp ('t348');
5693            }            }
5694          } else {          } elsif ($token->{tag_name} eq 'form') {
5695            !!!cp ('t347');            !!!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            !!!cp ('t350');            $self->{insertion_mode} = IN_TABLE_IM;
5704            !!!parse-error (type => 'in form:form');  
5705            ## Ignore the token            !!!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') {  
               !!!cp ('t351');  
               !!!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]}) {  
               !!!cp ('t352');  
               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          }          }
5715        } elsif ($token->{tag_name} eq 'li') {          redo B;
5716          } 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');              !!!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');              !!!cp ('t354');
# Line 5494  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 'li') {            if ($li_or_dtdd->{$node->[1]}) {
5743              if ($i != -1) {              if ($i != -1) {
5744                !!!cp ('t355');                !!!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 {              } else {
5748                !!!cp ('t356');                !!!cp ('t356');
5749              }              }
# Line 5527  sub _tree_construction_main ($) { Line 5770  sub _tree_construction_main ($) {
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);
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t360');  
             !!!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]}) {  
             !!!cp ('t361');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!cp ('t362');  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             } else {  
               !!!cp ('t363');  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           } else {  
             !!!cp ('t364');  
           }  
             
           ## 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') {  
             !!!cp ('t365');  
             last LI;  
           }  
             
           !!!cp ('t366');  
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
5774          !!!next-token;          !!!next-token;
5775          redo B;          redo B;
5776        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
# Line 5592  sub _tree_construction_main ($) { Line 5779  sub _tree_construction_main ($) {
5779            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5780              !!!cp ('t367');              !!!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');              !!!cp ('t368');
# Line 5603  sub _tree_construction_main ($) { Line 5791  sub _tree_construction_main ($) {
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                        
# Line 5614  sub _tree_construction_main ($) { Line 5802  sub _tree_construction_main ($) {
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              !!!cp ('t371');              !!!cp ('t371');
5805              !!!parse-error (type => 'in a:a');              !!!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]) {
# Line 5643  sub _tree_construction_main ($) { Line 5832  sub _tree_construction_main ($) {
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}}) {  
         !!!cp ('t375');  
         $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 5669  sub _tree_construction_main ($) { Line 5845  sub _tree_construction_main ($) {
5845            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
5846            if ($node->[1] eq 'nobr') {            if ($node->[1] eq 'nobr') {
5847              !!!cp ('t376');              !!!cp ('t376');
5848              !!!parse-error (type => 'in nobr:nobr');              !!!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');              !!!cp ('t377');
# Line 5682  sub _tree_construction_main ($) { Line 5859  sub _tree_construction_main ($) {
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 5693  sub _tree_construction_main ($) { Line 5870  sub _tree_construction_main ($) {
5870            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
5871            if ($node->[1] eq 'button') {            if ($node->[1] eq 'button') {
5872              !!!cp ('t378');              !!!cp ('t378');
5873              !!!parse-error (type => 'in button:button');              !!!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');              !!!cp ('t379');
# Line 5708  sub _tree_construction_main ($) { Line 5886  sub _tree_construction_main ($) {
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    
5891          ## TODO: associate with $self->{form_element} if defined          ## TODO: associate with $self->{form_element} if defined
5892    
# Line 5716  sub _tree_construction_main ($) { Line 5894  sub _tree_construction_main ($) {
5894    
5895          !!!next-token;          !!!next-token;
5896          redo B;          redo B;
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         !!!cp ('t380');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
           
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         !!!cp ('t381');  
         $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') {  
             !!!cp ('t382');  
             !!!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]}) {  
             !!!cp ('t383');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = IN_TABLE_IM;  
             
         !!!next-token;  
         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            !!!cp ('t384');            !!!cp ('t381');
5906            !!!parse-error (type => 'image');            $reconstruct_active_formatting_elements->($insert_to_current);
           $token->{tag_name} = 'img';  
5907          } else {          } else {
5908            !!!cp ('t385');            !!!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') {  
             !!!cp ('t386');  
             !!!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]}) {  
             !!!cp ('t387');  
             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') {  
         !!!cp ('t388');  
         $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');            !!!cp ('t389');
# Line 5825  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              !!!cp ('t390');              !!!cp ('t390');
5942              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
5943                               #line => $token->{line}, column => $token->{column},
5944                              };
5945            } else {            } else {
5946              !!!cp ('t391');              !!!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 5853  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 5892  sub _tree_construction_main ($) { Line 6008  sub _tree_construction_main ($) {
6008            ## Ignore the token            ## Ignore the token
6009          } else {          } else {
6010            !!!cp ('t398');            !!!cp ('t398');
6011            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!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}}) {  
         !!!cp ('t399');  
         ## NOTE: There is an "as if in body" code clone.  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'select') {  
         !!!cp ('t400');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
   
         ## TODO: associate with $self->{form_element} if defined  
           
         $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          !!!cp ('t401');          !!!cp ('t401');
6022          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!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          !!!cp ('t402');          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                !!!cp ('t403');              } elsif ({
6095                !!!parse-error (type => 'not closed:'.$_->[1]);                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
6096              } else {                        button => 1, marquee => 1, object => 1, html => 1,
6097                !!!cp ('t404');                       }->{$_->[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 {  
           !!!cp ('t405');  
           !!!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              !!!cp ('t406');              !!!cp ('t406');
6132              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1], token => $token);
6133            } else {            } else {
6134              !!!cp ('t407');              !!!cp ('t407');
6135            }            }
# Line 5980  sub _tree_construction_main ($) { Line 6138  sub _tree_construction_main ($) {
6138            redo B;            redo B;
6139          } else {          } else {
6140            !!!cp ('t408');            !!!cp ('t408');
6141            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!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 5990  sub _tree_construction_main ($) { Line 6148  sub _tree_construction_main ($) {
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,
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;
# Line 6001  sub _tree_construction_main ($) { Line 6159  sub _tree_construction_main ($) {
6159              $i = $_;              $i = $_;
6160              last INSCOPE;              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');              !!!cp ('t411');
# Line 6011  sub _tree_construction_main ($) { Line 6169  sub _tree_construction_main ($) {
6169    
6170          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
6171            !!!cp ('t413');            !!!cp ('t413');
6172            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6173          } else {          } else {
6174            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
6175            while ({            while ({
# Line 6027  sub _tree_construction_main ($) { Line 6185  sub _tree_construction_main ($) {
6185            ## Step 2.            ## Step 2.
6186            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6187              !!!cp ('t412');              !!!cp ('t412');
6188              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
6189            } else {            } else {
6190              !!!cp ('t414');              !!!cp ('t414');
6191            }            }
# Line 6038  sub _tree_construction_main ($) { Line 6196  sub _tree_construction_main ($) {
6196            ## Step 4.            ## Step 4.
6197            $clear_up_to_marker->()            $clear_up_to_marker->()
6198                if {                if {
6199                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
6200                }->{$token->{tag_name}};                }->{$token->{tag_name}};
6201          }          }
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}) {
             ## generate implied end tags  
             while ({  
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t417');  
               pop @{$self->{open_elements}};  
             }  
   
6212              !!!cp ('t418');              !!!cp ('t418');
6213                $i = $_;
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');              !!!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
           !!!cp ('t420');  
           pop @{$self->{open_elements}};  
         } else {  
6225            !!!cp ('t421');            !!!cp ('t421');
6226            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6227            } 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 6088  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]}) {
             ## generate implied end tags  
             while ({  
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t422');  
               pop @{$self->{open_elements}};  
             }  
   
6260              !!!cp ('t423');              !!!cp ('t423');
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');              !!!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            !!!cp ('t425');            !!!cp ('t425.1');
6274            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6275          } else {          } else {
6276            !!!cp ('t426');            ## 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                    
         splice @{$self->{open_elements}}, $i if defined $i;  
6296          !!!next-token;          !!!next-token;
6297          redo B;          redo B;
6298        } elsif ($token->{tag_name} eq 'p') {        } elsif ($token->{tag_name} eq 'p') {
# Line 6124  sub _tree_construction_main ($) { Line 6301  sub _tree_construction_main ($) {
6301          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6302            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6303            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] eq $token->{tag_name}) {
             ## generate implied end tags  
             while ({  
                     dd => 1, dt => 1, li => 1, p => 0,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t409.1');  
               pop @{$self->{open_elements}};  
             }  
               
6304              !!!cp ('t410.1');              !!!cp ('t410.1');
6305              $i = $_;              $i = $_;
6306              last INSCOPE;              last INSCOPE;
6307            } elsif ({            } elsif ({
6308                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
6309                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
6310                     }->{$node->[1]}) {                     }->{$node->[1]}) {
6311              !!!cp ('t411.1');              !!!cp ('t411.1');
6312              last INSCOPE;              last INSCOPE;
6313            }            }
6314          } # INSCOPE          } # INSCOPE
6315            
6316          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          if (defined $i) {
6317            if (defined $i) {            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6318              !!!cp ('t412.1');              !!!cp ('t412.1');
6319              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
6320            } else {            } else {
6321              !!!cp ('t413.1');              !!!cp ('t414.1');
             !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
6322            }            }
6323          }  
           
         if (defined $i) {  
           !!!cp ('t414.1');  
6324            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6325          } else {          } else {
6326              !!!cp ('t413.1');
6327              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6328    
6329            !!!cp ('t415.1');            !!!cp ('t415.1');
6330            ## As if <p>, then reprocess the current token            ## As if <p>, then reprocess the current token
6331            my $el;            my $el;
6332            !!!create-element ($el, 'p');            !!!create-element ($el, 'p',, $token);
6333            $insert->($el);            $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 6173  sub _tree_construction_main ($) { Line 6343  sub _tree_construction_main ($) {
6343                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
6344                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6345          !!!cp ('t427');          !!!cp ('t427');
6346          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token);
6347          redo B;          redo B;
6348        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
6349          !!!cp ('t428');          !!!cp ('t428');
6350          !!!parse-error (type => 'unmatched end tag:br');          !!!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 6202  sub _tree_construction_main ($) { Line 6372  sub _tree_construction_main ($) {
6372                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
6373                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6374          !!!cp ('t429');          !!!cp ('t429');
6375          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!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 6231  sub _tree_construction_main ($) { Line 6401  sub _tree_construction_main ($) {
6401              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
6402                !!!cp ('t431');                !!!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 {              } else {
6406                !!!cp ('t432');                !!!cp ('t432');
6407              }              }
# Line 6248  sub _tree_construction_main ($) { Line 6418  sub _tree_construction_main ($) {
6418                  ($special_category->{$node->[1]} or                  ($special_category->{$node->[1]} or
6419                   $scoping_category->{$node->[1]})) {                   $scoping_category->{$node->[1]})) {
6420                !!!cp ('t433');                !!!cp ('t433');
6421                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!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;

Legend:
Removed from v.1.89  
changed lines
  Added in v.1.120

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24