/[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.81 by wakaba, Tue Mar 4 14:51:54 2008 UTC revision 1.122 by wakaba, Sun Apr 6 06:34:11 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.
317    
318    ## NOTE: "after after body" insertion mode.
319  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
320    
321    ## NOTE: "after after frameset" insertion mode.
322  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
323    
324  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
325  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
326  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
# Line 319  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 441  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 457  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 465  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 484  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 504  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 512  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 537  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 545  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 563  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 582  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 595  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 603  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 612  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 628  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 651  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 684  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 741  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 764  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 788  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 819  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 830  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 859  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 905  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 957  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 981  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 1006  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 1029  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 1063  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 1088  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 1137  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 1181  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 1228  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 1253  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 1325  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 1371  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 1379  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 1387  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 1402  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 1410  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 1446  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 1474  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 1597  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 1613  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 1652  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 1661  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 2197  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 2237  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 2246  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 2289  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 2363  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 2420  sub _construct_tree ($) { Line 2488  sub _construct_tree ($) {
2488        
2489    !!!next-token;    !!!next-token;
2490    
   $self->{insertion_mode} = BEFORE_HEAD_IM;  
2491    undef $self->{form_element};    undef $self->{form_element};
2492    undef $self->{head_element};    undef $self->{head_element};
2493    $self->{open_elements} = [];    $self->{open_elements} = [];
2494    undef $self->{inner_html_node};    undef $self->{inner_html_node};
2495    
2496      ## NOTE: The "initial" insertion mode.
2497    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
2498    
2499      ## NOTE: The "before html" insertion mode.
2500    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
2501      $self->{insertion_mode} = BEFORE_HEAD_IM;
2502    
2503      ## NOTE: The "before head" insertion mode and so on.
2504    $self->_tree_construction_main;    $self->_tree_construction_main;
2505  } # _construct_tree  } # _construct_tree
2506    
2507  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
2508    my $self = shift;    my $self = shift;
2509    
2510      ## NOTE: "initial" insertion mode
2511    
2512    INITIAL: {    INITIAL: {
2513      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
2514        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
# Line 2445  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        }        }
2532                
2533        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
2534          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
2535          ## NOTE: Default value for both |public_id| and |system_id| attributes
2536          ## are empty strings, so that we don't set any value in missing cases.
2537        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
2538            if defined $token->{public_identifier};            if defined $token->{public_identifier};
2539        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2579  sub _tree_construction_initial ($) { Line 2657  sub _tree_construction_initial ($) {
2657          !!!cp ('t13');          !!!cp ('t13');
2658        }        }
2659                
2660        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
2661        !!!next-token;        !!!next-token;
2662        return;        return;
2663      } elsif ({      } elsif ({
# Line 2588  sub _tree_construction_initial ($) { Line 2666  sub _tree_construction_initial ($) {
2666                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
2667               }->{$token->{type}}) {               }->{$token->{type}}) {
2668        !!!cp ('t14');        !!!cp ('t14');
2669        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
2670        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2671        ## Go to the root element phase        ## Go to the "before html" insertion mode.
2672        ## reprocess        ## reprocess
2673        return;        return;
2674      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
# Line 2599  sub _tree_construction_initial ($) { Line 2677  sub _tree_construction_initial ($) {
2677    
2678          unless (length $token->{data}) {          unless (length $token->{data}) {
2679            !!!cp ('t15');            !!!cp ('t15');
2680            ## Stay in the phase            ## Stay in the insertion mode.
2681            !!!next-token;            !!!next-token;
2682            redo INITIAL;            redo INITIAL;
2683          } else {          } else {
# Line 2609  sub _tree_construction_initial ($) { Line 2687  sub _tree_construction_initial ($) {
2687          !!!cp ('t17');          !!!cp ('t17');
2688        }        }
2689    
2690        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
2691        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2692        ## Go to the root element phase        ## Go to the "before html" insertion mode.
2693        ## reprocess        ## reprocess
2694        return;        return;
2695      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
# Line 2619  sub _tree_construction_initial ($) { Line 2697  sub _tree_construction_initial ($) {
2697        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2698        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
2699                
2700        ## Stay in the phase.        ## Stay in the insertion mode.
2701        !!!next-token;        !!!next-token;
2702        redo INITIAL;        redo INITIAL;
2703      } else {      } else {
# Line 2632  sub _tree_construction_initial ($) { Line 2710  sub _tree_construction_initial ($) {
2710    
2711  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
2712    my $self = shift;    my $self = shift;
2713    
2714      ## NOTE: "before html" insertion mode.
2715        
2716    B: {    B: {
2717        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
2718          !!!cp ('t19');          !!!cp ('t19');
2719          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
2720          ## Ignore the token          ## Ignore the token
2721          ## Stay in the phase          ## Stay in the insertion mode.
2722          !!!next-token;          !!!next-token;
2723          redo B;          redo B;
2724        } elsif ($token->{type} == COMMENT_TOKEN) {        } elsif ($token->{type} == COMMENT_TOKEN) {
2725          !!!cp ('t20');          !!!cp ('t20');
2726          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2727          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2728          ## Stay in the phase          ## Stay in the insertion mode.
2729          !!!next-token;          !!!next-token;
2730          redo B;          redo B;
2731        } elsif ($token->{type} == CHARACTER_TOKEN) {        } elsif ($token->{type} == CHARACTER_TOKEN) {
# Line 2654  sub _tree_construction_root_element ($) Line 2734  sub _tree_construction_root_element ($)
2734    
2735            unless (length $token->{data}) {            unless (length $token->{data}) {
2736              !!!cp ('t21');              !!!cp ('t21');
2737              ## Stay in the phase              ## Stay in the insertion mode.
2738              !!!next-token;              !!!next-token;
2739              redo B;              redo B;
2740            } else {            } else {
# Line 2668  sub _tree_construction_root_element ($) Line 2748  sub _tree_construction_root_element ($)
2748    
2749          #          #
2750        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
2751          if ($token->{tag_name} eq 'html' and          if ($token->{tag_name} eq 'html') {
2752              $token->{attributes}->{manifest}) {            my $root_element;
2753            !!!cp ('t24');            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes}, $token);
2754            $self->{application_cache_selection}            $self->{document}->append_child ($root_element);
2755                 ->($token->{attributes}->{manifest}->{value});            push @{$self->{open_elements}}, [$root_element, 'html'];
2756            ## ISSUE: No relative reference resolution?  
2757              if ($token->{attributes}->{manifest}) {
2758                !!!cp ('t24');
2759                $self->{application_cache_selection}
2760                    ->($token->{attributes}->{manifest}->{value});
2761                ## ISSUE: Spec is unclear on relative references.
2762                ## According to Hixie (#whatwg 2008-03-19), it should be
2763                ## resolved against the base URI of the document in HTML
2764                ## or xml:base of the element in XHTML.
2765              } else {
2766                !!!cp ('t25');
2767                $self->{application_cache_selection}->(undef);
2768              }
2769    
2770              !!!next-token;
2771              return; ## Go to the "before head" insertion mode.
2772          } else {          } else {
2773            !!!cp ('t25');            !!!cp ('t25.1');
2774            $self->{application_cache_selection}->(undef);            #
2775          }          }
   
         ## ISSUE: There is an issue in the spec  
         #  
2776        } elsif ({        } elsif ({
2777                  END_TAG_TOKEN, 1,                  END_TAG_TOKEN, 1,
2778                  END_OF_FILE_TOKEN, 1,                  END_OF_FILE_TOKEN, 1,
2779                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2780          !!!cp ('t26');          !!!cp ('t26');
         $self->{application_cache_selection}->(undef);  
   
         ## ISSUE: There is an issue in the spec  
2781          #          #
2782        } else {        } else {
2783          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
2784        }        }
2785    
2786        my $root_element; !!!create-element ($root_element, 'html');      my $root_element; !!!create-element ($root_element, 'html',, $token);
2787        $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
2788        push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, 'html'];
2789        ## reprocess  
2790        #redo B;      $self->{application_cache_selection}->(undef);
2791        return; ## Go to the main phase.  
2792        ## NOTE: Reprocess the token.
2793        return; ## Go to the "before head" insertion mode.
2794    
2795        ## ISSUE: There is an issue in the spec
2796    } # B    } # B
2797    
2798    die "$0: _tree_construction_root_element: This should never be reached";    die "$0: _tree_construction_root_element: This should never be reached";
# Line 2717  sub _reset_insertion_mode ($) { Line 2810  sub _reset_insertion_mode ($) {
2810            
2811      ## Step 3      ## Step 3
2812      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"!?  
2813        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2814          $last = 1;          $last = 1;
2815          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
# Line 2739  sub _reset_insertion_mode ($) { Line 2827  sub _reset_insertion_mode ($) {
2827        ## Step 4..13        ## Step 4..13
2828        my $new_mode = {        my $new_mode = {
2829                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
2830                          ## NOTE: |option| and |optgroup| do not set
2831                          ## insertion mode to "in select" by themselves.
2832                        td => IN_CELL_IM,                        td => IN_CELL_IM,
2833                        th => IN_CELL_IM,                        th => IN_CELL_IM,
2834                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
# Line 2880  sub _tree_construction_main ($) { Line 2970  sub _tree_construction_main ($) {
2970      !!!cp ('t39');      !!!cp ('t39');
2971    }; # $clear_up_to_marker    }; # $clear_up_to_marker
2972    
2973    my $parse_rcdata = sub ($$) {    my $insert;
2974      my ($content_model_flag, $insert) = @_;  
2975      my $parse_rcdata = sub ($) {
2976        my ($content_model_flag) = @_;
2977    
2978      ## Step 1      ## Step 1
2979      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
2980      my $el;      my $el;
2981      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $start_tag_name, $token->{attributes}, $token);
2982    
2983      ## Step 2      ## Step 2
2984      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
2985    
2986      ## Step 3      ## Step 3
2987      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2919  sub _tree_construction_main ($) { Line 3011  sub _tree_construction_main ($) {
3011          $token->{tag_name} eq $start_tag_name) {          $token->{tag_name} eq $start_tag_name) {
3012        !!!cp ('t42');        !!!cp ('t42');
3013        ## 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});  
3014      } else {      } else {
3015        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3016          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3017            !!!cp ('t43');
3018            !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3019          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3020            !!!cp ('t44');
3021            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3022          } else {
3023            die "$0: $content_model_flag in parse_rcdata";
3024          }
3025      }      }
3026      !!!next-token;      !!!next-token;
3027    }; # $parse_rcdata    }; # $parse_rcdata
3028    
3029    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3030      my $script_el;      my $script_el;
3031      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes}, $token);
3032      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3033    
3034      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
# Line 2960  sub _tree_construction_main ($) { Line 3054  sub _tree_construction_main ($) {
3054        ## Ignore the token        ## Ignore the token
3055      } else {      } else {
3056        !!!cp ('t48');        !!!cp ('t48');
3057        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3058        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3059        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3060      }      }
# Line 2983  sub _tree_construction_main ($) { Line 3077  sub _tree_construction_main ($) {
3077      !!!next-token;      !!!next-token;
3078    }; # $script_start_tag    }; # $script_start_tag
3079    
3080      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3081      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3082      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3083    
3084    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3085      my $tag_name = shift;      my $end_tag_token = shift;
3086        my $tag_name = $end_tag_token->{tag_name};
3087    
3088        ## NOTE: The adoption agency algorithm (AAA).
3089    
3090      FET: {      FET: {
3091        ## Step 1        ## Step 1
# Line 3003  sub _tree_construction_main ($) { Line 3104  sub _tree_construction_main ($) {
3104        } # AFE        } # AFE
3105        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3106          !!!cp ('t53');          !!!cp ('t53');
3107          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3108          ## Ignore the token          ## Ignore the token
3109          !!!next-token;          !!!next-token;
3110          return;          return;
# Line 3020  sub _tree_construction_main ($) { Line 3121  sub _tree_construction_main ($) {
3121              last INSCOPE;              last INSCOPE;
3122            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3123              !!!cp ('t55');              !!!cp ('t55');
3124              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3125                                token => $end_tag_token);
3126              ## Ignore the token              ## Ignore the token
3127              !!!next-token;              !!!next-token;
3128              return;              return;
3129            }            }
3130          } elsif ({          } elsif ({
3131                    table => 1, caption => 1, td => 1, th => 1,                    applet => 1, table => 1, caption => 1, td => 1, th => 1,
3132                    button => 1, marquee => 1, object => 1, html => 1,                    button => 1, marquee => 1, object => 1, html => 1,
3133                   }->{$node->[1]}) {                   }->{$node->[1]}) {
3134            !!!cp ('t56');            !!!cp ('t56');
# Line 3035  sub _tree_construction_main ($) { Line 3137  sub _tree_construction_main ($) {
3137        } # INSCOPE        } # INSCOPE
3138        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3139          !!!cp ('t57');          !!!cp ('t57');
3140          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3141                            token => $end_tag_token);
3142          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3143          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3144          return;          return;
3145        }        }
3146        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3147          !!!cp ('t58');          !!!cp ('t58');
3148          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!parse-error (type => 'not closed',
3149                            value => $self->{open_elements}->[-1]->[0]
3150                                ->manakai_local_name,
3151                            token => $end_tag_token);
3152        }        }
3153                
3154        ## Step 2        ## Step 2
# Line 3053  sub _tree_construction_main ($) { Line 3159  sub _tree_construction_main ($) {
3159          if (not $formatting_category->{$node->[1]} and          if (not $formatting_category->{$node->[1]} and
3160              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3161              ($special_category->{$node->[1]} or              ($special_category->{$node->[1]} or
3162               $scoping_category->{$node->[1]})) {               $scoping_category->{$node->[1]})) { ## Scoping is redundant, maybe
3163            !!!cp ('t59');            !!!cp ('t59');
3164            $furthest_block = $node;            $furthest_block = $node;
3165            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3139  sub _tree_construction_main ($) { Line 3245  sub _tree_construction_main ($) {
3245        } # S7          } # S7  
3246                
3247        ## Step 8        ## Step 8
3248        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ({
3249               table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,
3250              }->{$common_ancestor_node->[1]}) {
3251            my $foster_parent_element;
3252            my $next_sibling;
3253                             OE: for (reverse 0..$#{$self->{open_elements}}) {
3254                               if ($self->{open_elements}->[$_]->[1] eq 'table') {
3255                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3256                                 if (defined $parent and $parent->node_type == 1) {
3257                                   !!!cp ('t65.1');
3258                                   $foster_parent_element = $parent;
3259                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3260                                 } else {
3261                                   !!!cp ('t65.2');
3262                                   $foster_parent_element
3263                                     = $self->{open_elements}->[$_ - 1]->[0];
3264                                 }
3265                                 last OE;
3266                               }
3267                             } # OE
3268                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3269                               unless defined $foster_parent_element;
3270            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3271            $open_tables->[-1]->[1] = 1; # tainted
3272          } else {
3273            !!!cp ('t65.3');
3274            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3275          }
3276                
3277        ## Step 9        ## Step 9
3278        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 3185  sub _tree_construction_main ($) { Line 3318  sub _tree_construction_main ($) {
3318      } # FET      } # FET
3319    }; # $formatting_end_tag    }; # $formatting_end_tag
3320    
3321    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3322      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3323    }; # $insert_to_current    }; # $insert_to_current
3324    
3325    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3326                         my $child = shift;      my $child = shift;
3327                         if ({      if ({
3328                              table => 1, tbody => 1, tfoot => 1,           table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,
3329                              thead => 1, tr => 1,          }->{$self->{open_elements}->[-1]->[1]}) {
3330                             }->{$self->{open_elements}->[-1]->[1]}) {        # MUST
3331                           # MUST        my $foster_parent_element;
3332                           my $foster_parent_element;        my $next_sibling;
                          my $next_sibling;  
3333                           OE: for (reverse 0..$#{$self->{open_elements}}) {                           OE: for (reverse 0..$#{$self->{open_elements}}) {
3334                             if ($self->{open_elements}->[$_]->[1] eq 'table') {                             if ($self->{open_elements}->[$_]->[1] eq 'table') {
3335                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
# Line 3217  sub _tree_construction_main ($) { Line 3349  sub _tree_construction_main ($) {
3349                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3350                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3351                             ($child, $next_sibling);                             ($child, $next_sibling);
3352                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3353                           !!!cp ('t72');      } else {
3354                           $self->{open_elements}->[-1]->[0]->append_child ($child);        !!!cp ('t72');
3355                         }        $self->{open_elements}->[-1]->[0]->append_child ($child);
3356        }
3357    }; # $insert_to_foster    }; # $insert_to_foster
3358    
   my $insert;  
   
3359    B: {    B: {
3360      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3361        !!!cp ('t73');        !!!cp ('t73');
3362        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3363        ## Ignore the token        ## Ignore the token
3364        ## Stay in the phase        ## Stay in the phase
3365        !!!next-token;        !!!next-token;
3366        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  
         if ({  
              dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,  
              tbody => 1, tfoot=> 1, thead => 1,  
             }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!cp ('t75');  
           !!!back-token;  
           $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};  
           redo B;  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!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;  
3367      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3368               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3369        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3370          !!!cp ('t79');          !!!cp ('t79');
3371          ## Turn into the main phase          !!!parse-error (type => 'after html:html', token => $token);
         !!!parse-error (type => 'after html:html');  
3372          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
3373        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3374          !!!cp ('t80');          !!!cp ('t80');
3375          ## Turn into the main phase          !!!parse-error (type => 'after html:html', token => $token);
         !!!parse-error (type => 'after html:html');  
3376          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3377        } else {        } else {
3378          !!!cp ('t81');          !!!cp ('t81');
3379        }        }
3380    
3381  ## ISSUE: "aa<html>" is not a parse error.        !!!cp ('t82');
3382  ## ISSUE: "<html>" in fragment is not a parse error.        !!!parse-error (type => 'not first start tag', token => $token);
       unless ($token->{first_start_tag}) {  
         !!!cp ('t82');  
         !!!parse-error (type => 'not first start tag');  
       } else {  
         !!!cp ('t83');  
       }  
3383        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
3384        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
3385          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
# Line 3320  sub _tree_construction_main ($) { Line 3408  sub _tree_construction_main ($) {
3408      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & HEAD_IMS) {
3409        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
3410          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3411            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3412                !!!cp ('t88.2');
3413                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3414              } else {
3415                !!!cp ('t88.1');
3416                ## Ignore the token.
3417                !!!next-token;
3418                redo B;
3419              }
3420            unless (length $token->{data}) {            unless (length $token->{data}) {
3421              !!!cp ('t88');              !!!cp ('t88');
3422              !!!next-token;              !!!next-token;
# Line 3331  sub _tree_construction_main ($) { Line 3427  sub _tree_construction_main ($) {
3427          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3428            !!!cp ('t89');            !!!cp ('t89');
3429            ## As if <head>            ## As if <head>
3430            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, 'head',, $token);
3431            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3432            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3433    
# Line 3343  sub _tree_construction_main ($) { Line 3439  sub _tree_construction_main ($) {
3439            !!!cp ('t90');            !!!cp ('t90');
3440            ## As if </noscript>            ## As if </noscript>
3441            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
3442            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character', token => $token);
3443                        
3444            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
3445            ## As if </head>            ## As if </head>
# Line 3361  sub _tree_construction_main ($) { Line 3457  sub _tree_construction_main ($) {
3457    
3458              ## "after head" insertion mode              ## "after head" insertion mode
3459              ## As if <body>              ## As if <body>
3460              !!!insert-element ('body');              !!!insert-element ('body',, $token);
3461              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
3462              ## reprocess              ## reprocess
3463              redo B;              redo B;
# Line 3369  sub _tree_construction_main ($) { Line 3465  sub _tree_construction_main ($) {
3465              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
3466                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3467                  !!!cp ('t93');                  !!!cp ('t93');
3468                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes}, $token);
3469                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3470                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
3471                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
# Line 3380  sub _tree_construction_main ($) { Line 3476  sub _tree_construction_main ($) {
3476                  #                  #
3477                } else {                } else {
3478                  !!!cp ('t95');                  !!!cp ('t95');
3479                  !!!parse-error (type => 'in head:head'); # or in head noscript                  !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
3480                  ## Ignore the token                  ## Ignore the token
3481                  !!!next-token;                  !!!next-token;
3482                  redo B;                  redo B;
# Line 3388  sub _tree_construction_main ($) { Line 3484  sub _tree_construction_main ($) {
3484              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3485                !!!cp ('t96');                !!!cp ('t96');
3486                ## As if <head>                ## As if <head>
3487                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head',, $token);
3488                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3489                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3490    
# Line 3403  sub _tree_construction_main ($) { Line 3499  sub _tree_construction_main ($) {
3499                  !!!cp ('t98');                  !!!cp ('t98');
3500                  ## As if </noscript>                  ## As if </noscript>
3501                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3502                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base', token => $token);
3503                                
3504                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3505                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3414  sub _tree_construction_main ($) { Line 3510  sub _tree_construction_main ($) {
3510                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3511                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3512                  !!!cp ('t100');                  !!!cp ('t100');
3513                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3514                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3515                } else {                } else {
3516                  !!!cp ('t101');                  !!!cp ('t101');
3517                }                }
3518                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3519                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3520                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3521                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3522                !!!next-token;                !!!next-token;
3523                redo B;                redo B;
# Line 3429  sub _tree_construction_main ($) { Line 3525  sub _tree_construction_main ($) {
3525                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3526                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3527                  !!!cp ('t102');                  !!!cp ('t102');
3528                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3529                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3530                } else {                } else {
3531                  !!!cp ('t103');                  !!!cp ('t103');
3532                }                }
3533                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3534                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3535                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3536                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3537                !!!next-token;                !!!next-token;
3538                redo B;                redo B;
# Line 3444  sub _tree_construction_main ($) { Line 3540  sub _tree_construction_main ($) {
3540                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3541                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3542                  !!!cp ('t104');                  !!!cp ('t104');
3543                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3544                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3545                } else {                } else {
3546                  !!!cp ('t105');                  !!!cp ('t105');
3547                }                }
3548                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3549                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.
3550    
3551                unless ($self->{confident}) {                unless ($self->{confident}) {
3552                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
3553                    !!!cp ('t106');                    !!!cp ('t106');
3554                    $self->{change_encoding}                    $self->{change_encoding}
3555                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
3556                             $token);
3557                                        
3558                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3559                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
# Line 3471  sub _tree_construction_main ($) { Line 3568  sub _tree_construction_main ($) {
3568                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
3569                      !!!cp ('t107');                      !!!cp ('t107');
3570                      $self->{change_encoding}                      $self->{change_encoding}
3571                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
3572                               $token);
3573                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3574                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
3575                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
# Line 3497  sub _tree_construction_main ($) { Line 3595  sub _tree_construction_main ($) {
3595                  }                  }
3596                }                }
3597    
3598                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3599                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3600                !!!next-token;                !!!next-token;
3601                redo B;                redo B;
# Line 3506  sub _tree_construction_main ($) { Line 3604  sub _tree_construction_main ($) {
3604                  !!!cp ('t111');                  !!!cp ('t111');
3605                  ## As if </noscript>                  ## As if </noscript>
3606                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3607                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title', token => $token);
3608                                
3609                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3610                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3611                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3612                  !!!cp ('t112');                  !!!cp ('t112');
3613                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3614                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3615                } else {                } else {
3616                  !!!cp ('t113');                  !!!cp ('t113');
# Line 3521  sub _tree_construction_main ($) { Line 3619  sub _tree_construction_main ($) {
3619                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3620                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
3621                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
3622                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
3623                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
3624                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3625                redo B;                redo B;
3626              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
# Line 3532  sub _tree_construction_main ($) { Line 3629  sub _tree_construction_main ($) {
3629                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3630                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3631                  !!!cp ('t114');                  !!!cp ('t114');
3632                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3633                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3634                } else {                } else {
3635                  !!!cp ('t115');                  !!!cp ('t115');
3636                }                }
3637                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
3638                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3639                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3640                redo B;                redo B;
3641              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
3642                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
3643                  !!!cp ('t116');                  !!!cp ('t116');
3644                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
3645                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3646                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
3647                  !!!next-token;                  !!!next-token;
3648                  redo B;                  redo B;
3649                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3650                  !!!cp ('t117');                  !!!cp ('t117');
3651                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript:noscript', token => $token);
3652                  ## Ignore the token                  ## Ignore the token
3653                  !!!next-token;                  !!!next-token;
3654                  redo B;                  redo B;
# Line 3564  sub _tree_construction_main ($) { Line 3661  sub _tree_construction_main ($) {
3661                  !!!cp ('t119');                  !!!cp ('t119');
3662                  ## As if </noscript>                  ## As if </noscript>
3663                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3664                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script', token => $token);
3665                                
3666                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3667                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3668                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3669                  !!!cp ('t120');                  !!!cp ('t120');
3670                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3671                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3672                } else {                } else {
3673                  !!!cp ('t121');                  !!!cp ('t121');
3674                }                }
3675    
3676                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3677                $script_start_tag->($insert_to_current);                $script_start_tag->();
3678                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3679                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3680                redo B;                redo B;
3681              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
# Line 3587  sub _tree_construction_main ($) { Line 3684  sub _tree_construction_main ($) {
3684                  !!!cp ('t122');                  !!!cp ('t122');
3685                  ## As if </noscript>                  ## As if </noscript>
3686                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3687                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
3688                                    
3689                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3690                  ## As if </head>                  ## As if </head>
# Line 3604  sub _tree_construction_main ($) { Line 3701  sub _tree_construction_main ($) {
3701                }                }
3702    
3703                ## "after head" insertion mode                ## "after head" insertion mode
3704                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3705                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
3706                  !!!cp ('t126');                  !!!cp ('t126');
3707                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
# Line 3625  sub _tree_construction_main ($) { Line 3722  sub _tree_construction_main ($) {
3722                !!!cp ('t129');                !!!cp ('t129');
3723                ## As if </noscript>                ## As if </noscript>
3724                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3725                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
3726                                
3727                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
3728                ## As if </head>                ## As if </head>
# Line 3644  sub _tree_construction_main ($) { Line 3741  sub _tree_construction_main ($) {
3741    
3742              ## "after head" insertion mode              ## "after head" insertion mode
3743              ## As if <body>              ## As if <body>
3744              !!!insert-element ('body');              !!!insert-element ('body',, $token);
3745              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
3746              ## reprocess              ## reprocess
3747              redo B;              redo B;
# Line 3653  sub _tree_construction_main ($) { Line 3750  sub _tree_construction_main ($) {
3750                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3751                  !!!cp ('t132');                  !!!cp ('t132');
3752                  ## As if <head>                  ## As if <head>
3753                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head',, $token);
3754                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3755                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3756    
# Line 3666  sub _tree_construction_main ($) { Line 3763  sub _tree_construction_main ($) {
3763                  !!!cp ('t133');                  !!!cp ('t133');
3764                  ## As if </noscript>                  ## As if </noscript>
3765                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3766                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:/head', token => $token);
3767                                    
3768                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3769                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 3692  sub _tree_construction_main ($) { Line 3789  sub _tree_construction_main ($) {
3789                  redo B;                  redo B;
3790                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3791                  !!!cp ('t137');                  !!!cp ('t137');
3792                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
3793                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
3794                  !!!next-token;                  !!!next-token;
3795                  redo B;                  redo B;
# Line 3706  sub _tree_construction_main ($) { Line 3803  sub _tree_construction_main ($) {
3803                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3804                  !!!cp ('t139');                  !!!cp ('t139');
3805                  ## As if <head>                  ## As if <head>
3806                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head',, $token);
3807                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3808                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3809    
# Line 3714  sub _tree_construction_main ($) { Line 3811  sub _tree_construction_main ($) {
3811                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3812                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3813                  !!!cp ('t140');                  !!!cp ('t140');
3814                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
3815                  ## Ignore the token                  ## Ignore the token
3816                  !!!next-token;                  !!!next-token;
3817                  redo B;                  redo B;
# Line 3729  sub _tree_construction_main ($) { Line 3826  sub _tree_construction_main ($) {
3826                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3827                  !!!cp ('t142');                  !!!cp ('t142');
3828                  ## As if <head>                  ## As if <head>
3829                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head',, $token);
3830                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3831                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3832    
# Line 3746  sub _tree_construction_main ($) { Line 3843  sub _tree_construction_main ($) {
3843                  #                  #
3844                } else {                } else {
3845                  !!!cp ('t145');                  !!!cp ('t145');
3846                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
3847                  ## Ignore the token                  ## Ignore the token
3848                  !!!next-token;                  !!!next-token;
3849                  redo B;                  redo B;
# Line 3757  sub _tree_construction_main ($) { Line 3854  sub _tree_construction_main ($) {
3854                !!!cp ('t146');                !!!cp ('t146');
3855                ## As if </noscript>                ## As if </noscript>
3856                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3857                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
3858                                
3859                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
3860                ## As if </head>                ## As if </head>
# Line 3771  sub _tree_construction_main ($) { Line 3868  sub _tree_construction_main ($) {
3868    
3869                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3870              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3871    ## ISSUE: This case cannot be reached?
3872                !!!cp ('t148');                !!!cp ('t148');
3873                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
3874                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
3875                !!!next-token;                !!!next-token;
3876                redo B;                redo B;
# Line 3782  sub _tree_construction_main ($) { Line 3880  sub _tree_construction_main ($) {
3880    
3881              ## "after head" insertion mode              ## "after head" insertion mode
3882              ## As if <body>              ## As if <body>
3883              !!!insert-element ('body');              !!!insert-element ('body',, $token);
3884              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
3885              ## reprocess              ## reprocess
3886              redo B;              redo B;
3887            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
3888              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3889            }            !!!cp ('t149.1');
3890    
3891              ## NOTE: As if <head>
3892              !!!create-element ($self->{head_element}, 'head',, $token);
3893              $self->{open_elements}->[-1]->[0]->append_child
3894                  ($self->{head_element});
3895              #push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3896              #$self->{insertion_mode} = IN_HEAD_IM;
3897              ## NOTE: Reprocess.
3898    
3899              ## NOTE: As if </head>
3900              #pop @{$self->{open_elements}};
3901              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
3902              ## NOTE: Reprocess.
3903              
3904              #
3905            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3906              !!!cp ('t149.2');
3907    
3908              ## NOTE: As if </head>
3909              pop @{$self->{open_elements}};
3910              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
3911              ## NOTE: Reprocess.
3912    
3913              #
3914            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3915              !!!cp ('t149.3');
3916    
3917              !!!parse-error (type => 'in noscript:#eof', token => $token);
3918    
3919              ## As if </noscript>
3920              pop @{$self->{open_elements}};
3921              #$self->{insertion_mode} = IN_HEAD_IM;
3922              ## NOTE: Reprocess.
3923    
3924              ## NOTE: As if </head>
3925              pop @{$self->{open_elements}};
3926              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
3927              ## NOTE: Reprocess.
3928    
3929              #
3930            } else {
3931              !!!cp ('t149.4');
3932              #
3933            }
3934    
3935            ## NOTE: As if <body>
3936            !!!insert-element ('body',, $token);
3937            $self->{insertion_mode} = IN_BODY_IM;
3938            ## NOTE: Reprocess.
3939            redo B;
3940          } else {
3941            die "$0: $token->{type}: Unknown token type";
3942          }
3943    
3944            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
3945      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
# Line 3808  sub _tree_construction_main ($) { Line 3959  sub _tree_construction_main ($) {
3959                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3960                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
3961                  ## have an element in table scope                  ## have an element in table scope
3962                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
3963                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
3964                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3965                      !!!cp ('t151');                      !!!cp ('t151');
3966                      $tn = $node->[1];  
3967                      last INSCOPE;                      ## Close the cell
3968                        !!!back-token; # <?>
3969                        $token = {type => END_TAG_TOKEN,
3970                                  tag_name => $node->[0]->manakai_local_name,
3971                                  line => $token->{line},
3972                                  column => $token->{column}};
3973                        redo B;
3974                    } elsif ({                    } elsif ({
3975                              table => 1, html => 1,                              table => 1, html => 1,
3976                             }->{$node->[1]}) {                             }->{$node->[1]}) {
3977                      !!!cp ('t152');                      !!!cp ('t152');
3978                      last INSCOPE;                      ## ISSUE: This case can never be reached, maybe.
3979                    }                      last;
                 } # INSCOPE  
                   unless (defined $tn) {  
                     !!!cp ('t153');  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
3980                    }                    }
3981                                    }
3982                  !!!cp ('t154');  
3983                  ## Close the cell                  !!!cp ('t153');
3984                  !!!back-token; # <?>                  !!!parse-error (type => 'start tag not allowed',
3985                  $token = {type => END_TAG_TOKEN, tag_name => $tn};                      value => $token->{tag_name}, token => $token);
3986                    ## Ignore the token
3987                    !!!next-token;
3988                  redo B;                  redo B;
3989                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3990                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption', token => $token);
3991                                    
3992                  ## As if </caption>                  ## NOTE: As if </caption>.
3993                  ## have a table element in table scope                  ## have a table element in table scope
3994                  my $i;                  my $i;
3995                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
3996                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
3997                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
3998                      !!!cp ('t155');                      if ($node->[1] eq 'caption') {
3999                      $i = $_;                        !!!cp ('t155');
4000                      last INSCOPE;                        $i = $_;
4001                    } elsif ({                        last INSCOPE;
4002                              table => 1, html => 1,                      } elsif ({
4003                             }->{$node->[1]}) {                                table => 1, html => 1,
4004                      !!!cp ('t156');                               }->{$node->[1]}) {
4005                      last INSCOPE;                        !!!cp ('t156');
4006                          last;
4007                        }
4008                    }                    }
4009    
4010                      !!!cp ('t157');
4011                      !!!parse-error (type => 'start tag not allowed',
4012                                      value => $token->{tag_name}, token => $token);
4013                      ## Ignore the token
4014                      !!!next-token;
4015                      redo B;
4016                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t157');  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4017                                    
4018                  ## generate implied end tags                  ## generate implied end tags
4019                  if ({                  while ({
4020                       dd => 1, dt => 1, li => 1, p => 1,                          dd => 1, dt => 1, li => 1, p => 1,
4021                       td => 1, th => 1, tr => 1,                         }->{$self->{open_elements}->[-1]->[1]}) {
                      tbody => 1, tfoot=> 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
4022                    !!!cp ('t158');                    !!!cp ('t158');
4023                    !!!back-token; # <?>                    pop @{$self->{open_elements}};
                   $token = {type => END_TAG_TOKEN, tag_name => 'caption'};  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4024                  }                  }
4025    
4026                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4027                    !!!cp ('t159');                    !!!cp ('t159');
4028                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4029                                      value => $self->{open_elements}->[-1]->[0]
4030                                          ->manakai_local_name,
4031                                      token => $token);
4032                  } else {                  } else {
4033                    !!!cp ('t160');                    !!!cp ('t160');
4034                  }                  }
# Line 3920  sub _tree_construction_main ($) { Line 4069  sub _tree_construction_main ($) {
4069                  } # INSCOPE                  } # INSCOPE
4070                    unless (defined $i) {                    unless (defined $i) {
4071                      !!!cp ('t165');                      !!!cp ('t165');
4072                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4073                      ## Ignore the token                      ## Ignore the token
4074                      !!!next-token;                      !!!next-token;
4075                      redo B;                      redo B;
4076                    }                    }
4077                                    
4078                  ## generate implied end tags                  ## generate implied end tags
4079                  if ({                  while ({
4080                       dd => 1, dt => 1, li => 1, p => 1,                          dd => 1, dt => 1, li => 1, p => 1,
4081                       td => ($token->{tag_name} eq 'th'),                         }->{$self->{open_elements}->[-1]->[1]}) {
                      th => ($token->{tag_name} eq 'td'),  
                      tr => 1,  
                      tbody => 1, tfoot=> 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
4082                    !!!cp ('t166');                    !!!cp ('t166');
4083                    !!!back-token;                    pop @{$self->{open_elements}};
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4084                  }                  }
4085                    
4086                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
4087                    !!!cp ('t167');                    !!!cp ('t167');
4088                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4089                                      value => $self->{open_elements}->[-1]->[0]
4090                                          ->manakai_local_name,
4091                                      token => $token);
4092                  } else {                  } else {
4093                    !!!cp ('t168');                    !!!cp ('t168');
4094                  }                  }
# Line 3958  sub _tree_construction_main ($) { Line 4103  sub _tree_construction_main ($) {
4103                  redo B;                  redo B;
4104                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4105                  !!!cp ('t169');                  !!!cp ('t169');
4106                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4107                  ## Ignore the token                  ## Ignore the token
4108                  !!!next-token;                  !!!next-token;
4109                  redo B;                  redo B;
# Line 3970  sub _tree_construction_main ($) { Line 4115  sub _tree_construction_main ($) {
4115                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
4116                  ## have a table element in table scope                  ## have a table element in table scope
4117                  my $i;                  my $i;
4118                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4119                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4120                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
4121                      !!!cp ('t171');                      if ($node->[1] eq $token->{tag_name}) {
4122                      $i = $_;                        !!!cp ('t171');
4123                      last INSCOPE;                        $i = $_;
4124                    } elsif ({                        last INSCOPE;
4125                              table => 1, html => 1,                      } elsif ({
4126                             }->{$node->[1]}) {                                table => 1, html => 1,
4127                      !!!cp ('t172');                               }->{$node->[1]}) {
4128                      last INSCOPE;                        !!!cp ('t172');
4129                          last;
4130                        }
4131                    }                    }
4132    
4133                      !!!cp ('t173');
4134                      !!!parse-error (type => 'unmatched end tag',
4135                                      value => $token->{tag_name}, token => $token);
4136                      ## Ignore the token
4137                      !!!next-token;
4138                      redo B;
4139                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t173');  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4140                                    
4141                  ## generate implied end tags                  ## generate implied end tags
4142                  if ({                  while ({
4143                       dd => 1, dt => 1, li => 1, p => 1,                          dd => 1, dt => 1, li => 1, p => 1,
4144                       td => 1, th => 1, tr => 1,                         }->{$self->{open_elements}->[-1]->[1]}) {
                      tbody => 1, tfoot=> 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
4145                    !!!cp ('t174');                    !!!cp ('t174');
4146                    !!!back-token;                    pop @{$self->{open_elements}};
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4147                  }                  }
4148                                    
4149                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4150                    !!!cp ('t175');                    !!!cp ('t175');
4151                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4152                                      value => $self->{open_elements}->[-1]->[0]
4153                                          ->manakai_local_name,
4154                                      token => $token);
4155                  } else {                  } else {
4156                    !!!cp ('t176');                    !!!cp ('t176');
4157                  }                  }
# Line 4021  sub _tree_construction_main ($) { Line 4166  sub _tree_construction_main ($) {
4166                  redo B;                  redo B;
4167                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4168                  !!!cp ('t177');                  !!!cp ('t177');
4169                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4170                  ## Ignore the token                  ## Ignore the token
4171                  !!!next-token;                  !!!next-token;
4172                  redo B;                  redo B;
# Line 4037  sub _tree_construction_main ($) { Line 4182  sub _tree_construction_main ($) {
4182                ## have an element in table scope                ## have an element in table scope
4183                my $i;                my $i;
4184                my $tn;                my $tn;
4185                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
4186                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
4187                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4188                    !!!cp ('t179');                    if ($node->[1] eq $token->{tag_name}) {
4189                    $i = $_;                      !!!cp ('t179');
4190                    last INSCOPE;                      $i = $_;
4191                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
4192                    !!!cp ('t180');                      ## Close the cell
4193                    $tn = $node->[1];                      !!!back-token; # </?>
4194                    ## NOTE: There is exactly one |td| or |th| element                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4195                    ## in scope in the stack of open elements by definition.                                line => $token->{line},
4196                  } elsif ({                                column => $token->{column}};
4197                            table => 1, html => 1,                      redo B;
4198                           }->{$node->[1]}) {                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
4199                    !!!cp ('t181');                      !!!cp ('t180');
4200                    last INSCOPE;                      $tn = $node->[1];
4201                        ## NOTE: There is exactly one |td| or |th| element
4202                        ## in scope in the stack of open elements by definition.
4203                      } elsif ({
4204                                table => 1, html => 1,
4205                               }->{$node->[1]}) {
4206                        ## ISSUE: Can this be reached?
4207                        !!!cp ('t181');
4208                        last;
4209                      }
4210                  }                  }
4211                } # INSCOPE  
               unless (defined $i) {  
4212                  !!!cp ('t182');                  !!!cp ('t182');
4213                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4214                        value => $token->{tag_name}, token => $token);
4215                  ## Ignore the token                  ## Ignore the token
4216                  !!!next-token;                  !!!next-token;
4217                  redo B;                  redo B;
4218                } else {                } # INSCOPE
                 !!!cp ('t183');  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
4219              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
4220                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4221                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4222    
4223                ## As if </caption>                ## As if </caption>
4224                ## have a table element in table scope                ## have a table element in table scope
# Line 4091  sub _tree_construction_main ($) { Line 4238  sub _tree_construction_main ($) {
4238                } # INSCOPE                } # INSCOPE
4239                unless (defined $i) {                unless (defined $i) {
4240                  !!!cp ('t186');                  !!!cp ('t186');
4241                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);
4242                  ## Ignore the token                  ## Ignore the token
4243                  !!!next-token;                  !!!next-token;
4244                  redo B;                  redo B;
4245                }                }
4246                                
4247                ## generate implied end tags                ## generate implied end tags
4248                if ({                while ({
4249                     dd => 1, dt => 1, li => 1, p => 1,                        dd => 1, dt => 1, li => 1, p => 1,
4250                     td => 1, th => 1, tr => 1,                       }->{$self->{open_elements}->[-1]->[1]}) {
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
4251                  !!!cp ('t187');                  !!!cp ('t187');
4252                  !!!back-token; # </table>                  pop @{$self->{open_elements}};
                 $token = {type => END_TAG_TOKEN, tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
4253                }                }
4254    
4255                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4256                  !!!cp ('t188');                  !!!cp ('t188');
4257                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
4258                                    value => $self->{open_elements}->[-1]->[0]
4259                                        ->manakai_local_name,
4260                                    token => $token);
4261                } else {                } else {
4262                  !!!cp ('t189');                  !!!cp ('t189');
4263                }                }
# Line 4132  sub _tree_construction_main ($) { Line 4275  sub _tree_construction_main ($) {
4275                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4276                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4277                  !!!cp ('t190');                  !!!cp ('t190');
4278                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4279                  ## Ignore the token                  ## Ignore the token
4280                  !!!next-token;                  !!!next-token;
4281                  redo B;                  redo B;
# Line 4146  sub _tree_construction_main ($) { Line 4289  sub _tree_construction_main ($) {
4289                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
4290                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4291                !!!cp ('t192');                !!!cp ('t192');
4292                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4293                ## Ignore the token                ## Ignore the token
4294                !!!next-token;                !!!next-token;
4295                redo B;                redo B;
# Line 4154  sub _tree_construction_main ($) { Line 4297  sub _tree_construction_main ($) {
4297                !!!cp ('t193');                !!!cp ('t193');
4298                #                #
4299              }              }
4300          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4301            for my $entry (@{$self->{open_elements}}) {
4302              if (not {
4303                dd => 1, dt => 1, li => 1, p => 1, tbody => 1, td => 1, tfoot => 1,
4304                th => 1, thead => 1, tr => 1, body => 1, html => 1,
4305              }->{$entry->[1]}) {
4306                !!!cp ('t75');
4307                !!!parse-error (type => 'in body:#eof', token => $token);
4308                last;
4309              }
4310            }
4311    
4312            ## Stop parsing.
4313            last B;
4314        } else {        } else {
4315          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4316        }        }
# Line 4162  sub _tree_construction_main ($) { Line 4319  sub _tree_construction_main ($) {
4319        #        #
4320      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
4321        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4322              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
4323                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4324              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4325                                
4326                unless (length $token->{data}) {            unless (length $token->{data}) {
4327                  !!!cp ('t194');              !!!cp ('t194');
4328                  !!!next-token;              !!!next-token;
4329                  redo B;              redo B;
4330                } else {            } else {
4331                  !!!cp ('t195');              !!!cp ('t195');
4332                }            }
4333              }          }
4334    
4335              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
4336    
4337              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
4338              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 4219  sub _tree_construction_main ($) { Line 4377  sub _tree_construction_main ($) {
4377                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
4378                     $next_sibling);                     $next_sibling);
4379                }                }
4380              } else {            $open_tables->[-1]->[1] = 1; # tainted
4381                !!!cp ('t200');          } else {
4382                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            !!!cp ('t200');
4383              }            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4384            }
4385                            
4386              !!!next-token;          !!!next-token;
4387              redo B;          redo B;
4388        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4389              if ({              if ({
4390                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
# Line 4236  sub _tree_construction_main ($) { Line 4395  sub _tree_construction_main ($) {
4395                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
4396                         $self->{open_elements}->[-1]->[1] ne 'html') {                         $self->{open_elements}->[-1]->[1] ne 'html') {
4397                    !!!cp ('t201');                    !!!cp ('t201');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4398                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4399                  }                  }
4400                                    
4401                  !!!insert-element ('tbody');                  !!!insert-element ('tbody',, $token);
4402                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4403                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
4404                }                }
# Line 4248  sub _tree_construction_main ($) { Line 4406  sub _tree_construction_main ($) {
4406                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4407                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
4408                    !!!cp ('t202');                    !!!cp ('t202');
4409                    !!!parse-error (type => 'missing start tag:tr');                    !!!parse-error (type => 'missing start tag:tr', token => $token);
4410                  }                  }
4411                                    
4412                  ## Clear back to table body context                  ## Clear back to table body context
# Line 4256  sub _tree_construction_main ($) { Line 4414  sub _tree_construction_main ($) {
4414                    tbody => 1, tfoot => 1, thead => 1, html => 1,                    tbody => 1, tfoot => 1, thead => 1, html => 1,
4415                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4416                    !!!cp ('t203');                    !!!cp ('t203');
4417                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
4418                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4419                  }                  }
4420                                    
4421                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4422                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
4423                    !!!cp ('t204');                    !!!cp ('t204');
4424                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4425                    !!!next-token;                    !!!next-token;
4426                    redo B;                    redo B;
4427                  } else {                  } else {
4428                    !!!cp ('t205');                    !!!cp ('t205');
4429                    !!!insert-element ('tr');                    !!!insert-element ('tr',, $token);
4430                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
4431                  }                  }
4432                } else {                } else {
# Line 4280  sub _tree_construction_main ($) { Line 4438  sub _tree_construction_main ($) {
4438                  tr => 1, html => 1,                  tr => 1, html => 1,
4439                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4440                  !!!cp ('t207');                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4441                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4442                }                }
4443                                
4444                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4445                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
4446    
4447                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
# Line 4307  sub _tree_construction_main ($) { Line 4464  sub _tree_construction_main ($) {
4464                      $i = $_;                      $i = $_;
4465                      last INSCOPE;                      last INSCOPE;
4466                    } elsif ({                    } elsif ({
4467                              table => 1, html => 1,                              html => 1,
4468    
4469                                ## NOTE: This element does not appear here, maybe.
4470                                table => 1,
4471                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4472                      !!!cp ('t209');                      !!!cp ('t209');
4473                      last INSCOPE;                      last INSCOPE;
# Line 4315  sub _tree_construction_main ($) { Line 4475  sub _tree_construction_main ($) {
4475                  } # INSCOPE                  } # INSCOPE
4476                  unless (defined $i) {                  unless (defined $i) {
4477                   !!!cp ('t210');                   !!!cp ('t210');
4478                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});  ## TODO: This type is wrong.
4479                     !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
4480                    ## Ignore the token                    ## Ignore the token
4481                    !!!next-token;                    !!!next-token;
4482                    redo B;                    redo B;
# Line 4326  sub _tree_construction_main ($) { Line 4487  sub _tree_construction_main ($) {
4487                    tr => 1, html => 1,                    tr => 1, html => 1,
4488                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4489                    !!!cp ('t211');                    !!!cp ('t211');
4490                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
4491                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4492                  }                  }
4493                                    
# Line 4362  sub _tree_construction_main ($) { Line 4523  sub _tree_construction_main ($) {
4523                  } # INSCOPE                  } # INSCOPE
4524                  unless (defined $i) {                  unless (defined $i) {
4525                    !!!cp ('t216');                    !!!cp ('t216');
4526                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  ## TODO: This erorr type ios wrong.
4527                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4528                    ## Ignore the token                    ## Ignore the token
4529                    !!!next-token;                    !!!next-token;
4530                    redo B;                    redo B;
# Line 4373  sub _tree_construction_main ($) { Line 4535  sub _tree_construction_main ($) {
4535                    tbody => 1, tfoot => 1, thead => 1, html => 1,                    tbody => 1, tfoot => 1, thead => 1, html => 1,
4536                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4537                    !!!cp ('t217');                    !!!cp ('t217');
4538                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
4539                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4540                  }                  }
4541                                    
# Line 4396  sub _tree_construction_main ($) { Line 4558  sub _tree_construction_main ($) {
4558                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
4559                         $self->{open_elements}->[-1]->[1] ne 'html') {                         $self->{open_elements}->[-1]->[1] ne 'html') {
4560                    !!!cp ('t219');                    !!!cp ('t219');
4561                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
4562                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4563                  }                  }
4564                                    
4565                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
4566                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
4567                  ## reprocess                  ## reprocess
4568                  redo B;                  redo B;
# Line 4413  sub _tree_construction_main ($) { Line 4575  sub _tree_construction_main ($) {
4575                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
4576                         $self->{open_elements}->[-1]->[1] ne 'html') {                         $self->{open_elements}->[-1]->[1] ne 'html') {
4577                    !!!cp ('t220');                    !!!cp ('t220');
4578                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
4579                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4580                  }                  }
4581                                    
4582                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
4583                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
4584                                    
4585                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4586                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
4587                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
4588                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 4434  sub _tree_construction_main ($) { Line 4596  sub _tree_construction_main ($) {
4596                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
4597                }                }
4598              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4599                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
4600                                  value => $self->{open_elements}->[-1]->[0]
4601                                      ->manakai_local_name,
4602                                  token => $token);
4603    
4604                ## As if </table>                ## As if </table>
4605                ## have a table element in table scope                ## have a table element in table scope
# Line 4446  sub _tree_construction_main ($) { Line 4611  sub _tree_construction_main ($) {
4611                    $i = $_;                    $i = $_;
4612                    last INSCOPE;                    last INSCOPE;
4613                  } elsif ({                  } elsif ({
4614                            table => 1, html => 1,                            #table => 1,
4615                              html => 1,
4616                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4617                    !!!cp ('t222');                    !!!cp ('t222');
4618                    last INSCOPE;                    last INSCOPE;
# Line 4454  sub _tree_construction_main ($) { Line 4620  sub _tree_construction_main ($) {
4620                } # INSCOPE                } # INSCOPE
4621                unless (defined $i) {                unless (defined $i) {
4622                  !!!cp ('t223');                  !!!cp ('t223');
4623                  !!!parse-error (type => 'unmatched end tag:table');  ## TODO: The following is wrong, maybe.
4624                    !!!parse-error (type => 'unmatched end tag:table', token => $token);
4625                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
4626                  !!!next-token;                  !!!next-token;
4627                  redo B;                  redo B;
4628                }                }
4629                                
4630    ## TODO: Followings are removed from the latest spec.
4631                ## generate implied end tags                ## generate implied end tags
4632                if ({                while ({
4633                     dd => 1, dt => 1, li => 1, p => 1,                        dd => 1, dt => 1, li => 1, p => 1,
4634                     td => 1, th => 1, tr => 1,                       }->{$self->{open_elements}->[-1]->[1]}) {
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
4635                  !!!cp ('t224');                  !!!cp ('t224');
4636                  !!!back-token; # <table>                  pop @{$self->{open_elements}};
                 $token = {type => END_TAG_TOKEN, tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
4637                }                }
4638    
4639                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
4640                  !!!cp ('t225');                  !!!cp ('t225');
4641                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  ## NOTE: |<table><tr><table>|
4642                    !!!parse-error (type => 'not closed',
4643                                    value => $self->{open_elements}->[-1]->[0]
4644                                        ->manakai_local_name,
4645                                    token => $token);
4646                } else {                } else {
4647                  !!!cp ('t226');                  !!!cp ('t226');
4648                }                }
4649    
4650                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4651                  pop @{$open_tables};
4652    
4653                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4654    
4655                ## reprocess                ## reprocess
4656                redo B;                redo B;
4657            } elsif ($token->{tag_name} eq 'style') {
4658              if (not $open_tables->[-1]->[1]) { # tainted
4659                !!!cp ('t227.8');
4660                ## NOTE: This is a "as if in head" code clone.
4661                $parse_rcdata->(CDATA_CONTENT_MODEL);
4662                redo B;
4663              } else {
4664                !!!cp ('t227.7');
4665                #
4666              }
4667            } elsif ($token->{tag_name} eq 'script') {
4668              if (not $open_tables->[-1]->[1]) { # tainted
4669                !!!cp ('t227.6');
4670                ## NOTE: This is a "as if in head" code clone.
4671                $script_start_tag->();
4672                redo B;
4673              } else {
4674                !!!cp ('t227.5');
4675                #
4676              }
4677            } elsif ($token->{tag_name} eq 'input') {
4678              if (not $open_tables->[-1]->[1]) { # tainted
4679                if ($token->{attributes}->{type}) { ## TODO: case
4680                  my $type = lc $token->{attributes}->{type}->{value};
4681                  if ($type eq 'hidden') {
4682                    !!!cp ('t227.3');
4683                    !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
4684    
4685                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4686    
4687                    ## TODO: form element pointer
4688    
4689                    pop @{$self->{open_elements}};
4690    
4691                    !!!next-token;
4692                    redo B;
4693                  } else {
4694                    !!!cp ('t227.2');
4695                    #
4696                  }
4697                } else {
4698                  !!!cp ('t227.1');
4699                  #
4700                }
4701              } else {
4702                !!!cp ('t227.4');
4703                #
4704              }
4705          } else {          } else {
4706            !!!cp ('t227');            !!!cp ('t227');
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
   
           $insert = $insert_to_foster;  
4707            #            #
4708          }          }
4709    
4710            !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
4711    
4712            $insert = $insert_to_foster;
4713            #
4714        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
4715              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
4716                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 4515  sub _tree_construction_main ($) { Line 4731  sub _tree_construction_main ($) {
4731                } # INSCOPE                } # INSCOPE
4732                unless (defined $i) {                unless (defined $i) {
4733                  !!!cp ('t230');                  !!!cp ('t230');
4734                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4735                  ## Ignore the token                  ## Ignore the token
4736                  !!!next-token;                  !!!next-token;
4737                  redo B;                  redo B;
# Line 4528  sub _tree_construction_main ($) { Line 4744  sub _tree_construction_main ($) {
4744                  tr => 1, html => 1,                  tr => 1, html => 1,
4745                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4746                  !!!cp ('t231');                  !!!cp ('t231');
4747                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
4748                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4749                }                }
4750    
# Line 4556  sub _tree_construction_main ($) { Line 4772  sub _tree_construction_main ($) {
4772                  } # INSCOPE                  } # INSCOPE
4773                  unless (defined $i) {                  unless (defined $i) {
4774                    !!!cp ('t235');                    !!!cp ('t235');
4775                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});  ## TODO: The following is wrong.
4776                      !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
4777                    ## Ignore the token                    ## Ignore the token
4778                    !!!next-token;                    !!!next-token;
4779                    redo B;                    redo B;
# Line 4567  sub _tree_construction_main ($) { Line 4784  sub _tree_construction_main ($) {
4784                    tr => 1, html => 1,                    tr => 1, html => 1,
4785                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4786                    !!!cp ('t236');                    !!!cp ('t236');
4787                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
4788                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4789                  }                  }
4790                                    
# Line 4596  sub _tree_construction_main ($) { Line 4813  sub _tree_construction_main ($) {
4813                  } # INSCOPE                  } # INSCOPE
4814                  unless (defined $i) {                  unless (defined $i) {
4815                    !!!cp ('t239');                    !!!cp ('t239');
4816                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4817                    ## Ignore the token                    ## Ignore the token
4818                    !!!next-token;                    !!!next-token;
4819                    redo B;                    redo B;
# Line 4607  sub _tree_construction_main ($) { Line 4824  sub _tree_construction_main ($) {
4824                    tbody => 1, tfoot => 1, thead => 1, html => 1,                    tbody => 1, tfoot => 1, thead => 1, html => 1,
4825                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4826                    !!!cp ('t240');                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4827                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4828                  }                  }
4829                                    
# Line 4623  sub _tree_construction_main ($) { Line 4839  sub _tree_construction_main ($) {
4839                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
4840                }                }
4841    
4842                  ## NOTE: </table> in the "in table" insertion mode.
4843                  ## When you edit the code fragment below, please ensure that
4844                  ## the code for <table> in the "in table" insertion mode
4845                  ## is synced with it.
4846    
4847                ## have a table element in table scope                ## have a table element in table scope
4848                my $i;                my $i;
4849                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 4640  sub _tree_construction_main ($) { Line 4861  sub _tree_construction_main ($) {
4861                } # INSCOPE                } # INSCOPE
4862                unless (defined $i) {                unless (defined $i) {
4863                  !!!cp ('t243');                  !!!cp ('t243');
4864                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4865                  ## Ignore the token                  ## Ignore the token
4866                  !!!next-token;                  !!!next-token;
4867                  redo B;                  redo B;
4868                }                }
   
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!cp ('t244');  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
                 
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!cp ('t245');  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               } else {  
                 !!!cp ('t246');  
               }  
4869                                    
4870                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4871                  pop @{$open_tables};
4872                                
4873                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4874                                
# Line 4694  sub _tree_construction_main ($) { Line 4896  sub _tree_construction_main ($) {
4896                  } # INSCOPE                  } # INSCOPE
4897                    unless (defined $i) {                    unless (defined $i) {
4898                      !!!cp ('t249');                      !!!cp ('t249');
4899                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4900                      ## Ignore the token                      ## Ignore the token
4901                      !!!next-token;                      !!!next-token;
4902                      redo B;                      redo B;
# Line 4718  sub _tree_construction_main ($) { Line 4920  sub _tree_construction_main ($) {
4920                  } # INSCOPE                  } # INSCOPE
4921                    unless (defined $i) {                    unless (defined $i) {
4922                      !!!cp ('t252');                      !!!cp ('t252');
4923                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);
4924                      ## Ignore the token                      ## Ignore the token
4925                      !!!next-token;                      !!!next-token;
4926                      redo B;                      redo B;
# Line 4729  sub _tree_construction_main ($) { Line 4931  sub _tree_construction_main ($) {
4931                    tr => 1, html => 1,                    tr => 1, html => 1,
4932                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4933                    !!!cp ('t253');                    !!!cp ('t253');
4934                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
4935                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4936                  }                  }
4937                                    
# Line 4755  sub _tree_construction_main ($) { Line 4957  sub _tree_construction_main ($) {
4957                } # INSCOPE                } # INSCOPE
4958                unless (defined $i) {                unless (defined $i) {
4959                  !!!cp ('t256');                  !!!cp ('t256');
4960                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4961                  ## Ignore the token                  ## Ignore the token
4962                  !!!next-token;                  !!!next-token;
4963                  redo B;                  redo B;
# Line 4766  sub _tree_construction_main ($) { Line 4968  sub _tree_construction_main ($) {
4968                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
4969                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4970                  !!!cp ('t257');                  !!!cp ('t257');
4971                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
4972                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4973                }                }
4974    
# Line 4781  sub _tree_construction_main ($) { Line 4983  sub _tree_construction_main ($) {
4983                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
4984                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4985                !!!cp ('t258');                !!!cp ('t258');
4986                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4987                ## Ignore the token                ## Ignore the token
4988                !!!next-token;                !!!next-token;
4989                redo B;                redo B;
4990          } else {          } else {
4991            !!!cp ('t259');            !!!cp ('t259');
4992            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
4993    
4994            $insert = $insert_to_foster;            $insert = $insert_to_foster;
4995            #            #
4996          }          }
4997          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4998            unless ($self->{open_elements}->[-1]->[1] eq 'html' and
4999                    @{$self->{open_elements}} == 1) { # redundant, maybe
5000              !!!parse-error (type => 'in body:#eof', token => $token);
5001              !!!cp ('t259.1');
5002              #
5003            } else {
5004              !!!cp ('t259.2');
5005              #
5006            }
5007    
5008            ## Stop parsing
5009            last B;
5010        } else {        } else {
5011          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5012        }        }
# Line 4811  sub _tree_construction_main ($) { Line 5026  sub _tree_construction_main ($) {
5026            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5027              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
5028                !!!cp ('t262');                !!!cp ('t262');
5029                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5030                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5031                !!!next-token;                !!!next-token;
5032                redo B;                redo B;
# Line 4823  sub _tree_construction_main ($) { Line 5038  sub _tree_construction_main ($) {
5038              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5039                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] eq 'html') {
5040                  !!!cp ('t264');                  !!!cp ('t264');
5041                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5042                  ## Ignore the token                  ## Ignore the token
5043                  !!!next-token;                  !!!next-token;
5044                  redo B;                  redo B;
# Line 4836  sub _tree_construction_main ($) { Line 5051  sub _tree_construction_main ($) {
5051                }                }
5052              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5053                !!!cp ('t266');                !!!cp ('t266');
5054                !!!parse-error (type => 'unmatched end tag:col');                !!!parse-error (type => 'unmatched end tag:col', token => $token);
5055                ## Ignore the token                ## Ignore the token
5056                !!!next-token;                !!!next-token;
5057                redo B;                redo B;
# Line 4844  sub _tree_construction_main ($) { Line 5059  sub _tree_construction_main ($) {
5059                !!!cp ('t267');                !!!cp ('t267');
5060                #                #
5061              }              }
5062            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5063              !!!cp ('t268');          if ($self->{open_elements}->[-1]->[1] eq 'html' or
5064              #              @{$self->{open_elements}} == 1) { # redundant, maybe
5065            }            !!!cp ('t270.2');
5066              ## Stop parsing.
5067              last B;
5068            } else {
5069              ## NOTE: As if </colgroup>.
5070              !!!cp ('t270.1');
5071              pop @{$self->{open_elements}}; # colgroup
5072              $self->{insertion_mode} = IN_TABLE_IM;
5073              ## Reprocess.
5074              redo B;
5075            }
5076          } else {
5077            die "$0: $token->{type}: Unknown token type";
5078          }
5079    
5080            ## As if </colgroup>            ## As if </colgroup>
5081            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] eq 'html') {
5082              !!!cp ('t269');              !!!cp ('t269');
5083              !!!parse-error (type => 'unmatched end tag:colgroup');  ## TODO: Wrong error type?
5084                !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5085              ## Ignore the token              ## Ignore the token
5086              !!!next-token;              !!!next-token;
5087              redo B;              redo B;
# Line 4863  sub _tree_construction_main ($) { Line 5092  sub _tree_construction_main ($) {
5092              ## reprocess              ## reprocess
5093              redo B;              redo B;
5094            }            }
5095      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5096        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5097          !!!cp ('t271');          !!!cp ('t271');
5098          $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 5108  sub _tree_construction_main ($) {
5108                  !!!cp ('t273');                  !!!cp ('t273');
5109                }                }
5110    
5111                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5112                !!!next-token;                !!!next-token;
5113                redo B;                redo B;
5114              } elsif ($token->{tag_name} eq 'optgroup') {              } elsif ($token->{tag_name} eq 'optgroup') {
# Line 4899  sub _tree_construction_main ($) { Line 5128  sub _tree_construction_main ($) {
5128                  !!!cp ('t277');                  !!!cp ('t277');
5129                }                }
5130    
5131                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5132                !!!next-token;                !!!next-token;
5133                redo B;                redo B;
5134              } elsif ($token->{tag_name} eq 'select') {          } elsif ($token->{tag_name} eq 'select' or
5135                !!!parse-error (type => 'not closed:select');                   $token->{tag_name} eq 'input' or
5136                ## As if </select> instead                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5137                      {
5138                       caption => 1, table => 1,
5139                       tbody => 1, tfoot => 1, thead => 1,
5140                       tr => 1, td => 1, th => 1,
5141                      }->{$token->{tag_name}})) {
5142              ## TODO: The type below is not good - <select> is replaced by </select>
5143              !!!parse-error (type => 'not closed:select', token => $token);
5144              ## NOTE: As if the token were </select> (<select> case) or
5145              ## as if there were </select> (otherwise).
5146                ## have an element in table scope                ## have an element in table scope
5147                my $i;                my $i;
5148                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5149                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5150                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq 'select') {
5151                    !!!cp ('t278');                    !!!cp ('t278');
5152                    $i = $_;                    $i = $_;
5153                    last INSCOPE;                    last INSCOPE;
# Line 4922  sub _tree_construction_main ($) { Line 5160  sub _tree_construction_main ($) {
5160                } # INSCOPE                } # INSCOPE
5161                unless (defined $i) {                unless (defined $i) {
5162                  !!!cp ('t280');                  !!!cp ('t280');
5163                  !!!parse-error (type => 'unmatched end tag:select');                  !!!parse-error (type => 'unmatched end tag:select', token => $token);
5164                  ## Ignore the token                  ## Ignore the token
5165                  !!!next-token;                  !!!next-token;
5166                  redo B;                  redo B;
# Line 4933  sub _tree_construction_main ($) { Line 5171  sub _tree_construction_main ($) {
5171    
5172                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5173    
5174                !!!next-token;            if ($token->{tag_name} eq 'select') {
5175                redo B;              !!!cp ('t281.2');
5176                !!!next-token;
5177                redo B;
5178              } else {
5179                !!!cp ('t281.1');
5180                ## Reprocess the token.
5181                redo B;
5182              }
5183          } else {          } else {
5184            !!!cp ('t282');            !!!cp ('t282');
5185            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5186            ## Ignore the token            ## Ignore the token
5187            !!!next-token;            !!!next-token;
5188            redo B;            redo B;
# Line 4954  sub _tree_construction_main ($) { Line 5199  sub _tree_construction_main ($) {
5199                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5200                } else {                } else {
5201                  !!!cp ('t285');                  !!!cp ('t285');
5202                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5203                  ## Ignore the token                  ## Ignore the token
5204                }                }
5205                !!!next-token;                !!!next-token;
# Line 4965  sub _tree_construction_main ($) { Line 5210  sub _tree_construction_main ($) {
5210                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5211                } else {                } else {
5212                  !!!cp ('t287');                  !!!cp ('t287');
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                }                }
5216                !!!next-token;                !!!next-token;
# Line 4988  sub _tree_construction_main ($) { Line 5233  sub _tree_construction_main ($) {
5233                } # INSCOPE                } # INSCOPE
5234                unless (defined $i) {                unless (defined $i) {
5235                  !!!cp ('t290');                  !!!cp ('t290');
5236                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5237                  ## Ignore the token                  ## Ignore the token
5238                  !!!next-token;                  !!!next-token;
5239                  redo B;                  redo B;
# Line 5001  sub _tree_construction_main ($) { Line 5246  sub _tree_construction_main ($) {
5246    
5247                !!!next-token;                !!!next-token;
5248                redo B;                redo B;
5249              } elsif ({          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5250                        caption => 1, table => 1, tbody => 1,                   {
5251                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    caption => 1, table => 1, tbody => 1,
5252                       }->{$token->{tag_name}}) {                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5253                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                   }->{$token->{tag_name}}) {
5254    ## TODO: The following is wrong?
5255                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5256                                
5257                ## have an element in table scope                ## have an element in table scope
5258                my $i;                my $i;
# Line 5041  sub _tree_construction_main ($) { Line 5288  sub _tree_construction_main ($) {
5288                  } elsif ({                  } elsif ({
5289                            table => 1, html => 1,                            table => 1, html => 1,
5290                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5291    ## ISSUE: Can this state be reached?
5292                    !!!cp ('t296');                    !!!cp ('t296');
5293                    last INSCOPE;                    last INSCOPE;
5294                  }                  }
5295                } # INSCOPE                } # INSCOPE
5296                unless (defined $i) {                unless (defined $i) {
5297                  !!!cp ('t297');                  !!!cp ('t297');
5298                  !!!parse-error (type => 'unmatched end tag:select');  ## TODO: The following error type is correct?
5299                    !!!parse-error (type => 'unmatched end tag:select', token => $token);
5300                  ## Ignore the </select> token                  ## Ignore the </select> token
5301                  !!!next-token; ## TODO: ok?                  !!!next-token; ## TODO: ok?
5302                  redo B;                  redo B;
# Line 5062  sub _tree_construction_main ($) { Line 5311  sub _tree_construction_main ($) {
5311                redo B;                redo B;
5312          } else {          } else {
5313            !!!cp ('t299');            !!!cp ('t299');
5314            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
5315            ## Ignore the token            ## Ignore the token
5316            !!!next-token;            !!!next-token;
5317            redo B;            redo B;
5318          }          }
5319          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5320            unless ($self->{open_elements}->[-1]->[1] eq 'html' and
5321                    @{$self->{open_elements}} == 1) { # redundant, maybe
5322              !!!cp ('t299.1');
5323              !!!parse-error (type => 'in body:#eof', token => $token);
5324            } else {
5325              !!!cp ('t299.2');
5326            }
5327    
5328            ## Stop parsing.
5329            last B;
5330        } else {        } else {
5331          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5332        }        }
# Line 5088  sub _tree_construction_main ($) { Line 5348  sub _tree_construction_main ($) {
5348                    
5349          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5350            !!!cp ('t301');            !!!cp ('t301');
5351            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#character', token => $token);
5352    
5353            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5354          } else {          } else {
5355            !!!cp ('t302');            !!!cp ('t302');
5356          }          }
5357                    
5358          ## "after body" insertion mode          ## "after body" insertion mode
5359          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#character', token => $token);
5360    
5361          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
5362          ## reprocess          ## reprocess
# Line 5104  sub _tree_construction_main ($) { Line 5364  sub _tree_construction_main ($) {
5364        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5365          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5366            !!!cp ('t303');            !!!cp ('t303');
5367            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5368                        
5369            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5370          } else {          } else {
5371            !!!cp ('t304');            !!!cp ('t304');
5372          }          }
5373    
5374          ## "after body" insertion mode          ## "after body" insertion mode
5375          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
5376    
5377          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
5378          ## reprocess          ## reprocess
# Line 5120  sub _tree_construction_main ($) { Line 5380  sub _tree_construction_main ($) {
5380        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5381          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5382            !!!cp ('t305');            !!!cp ('t305');
5383            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
5384                        
5385            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
5386            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5387          } else {          } else {
5388            !!!cp ('t306');            !!!cp ('t306');
5389          }          }
# Line 5132  sub _tree_construction_main ($) { Line 5392  sub _tree_construction_main ($) {
5392          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
5393            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
5394              !!!cp ('t307');              !!!cp ('t307');
5395              !!!parse-error (type => 'unmatched end tag:html');              !!!parse-error (type => 'unmatched end tag:html', token => $token);
5396              ## Ignore the token              ## Ignore the token
5397              !!!next-token;              !!!next-token;
5398              redo B;              redo B;
# Line 5144  sub _tree_construction_main ($) { Line 5404  sub _tree_construction_main ($) {
5404            }            }
5405          } else {          } else {
5406            !!!cp ('t309');            !!!cp ('t309');
5407            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
5408    
5409            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
5410            ## reprocess            ## reprocess
5411            redo B;            redo B;
5412          }          }
5413          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5414            !!!cp ('t309.2');
5415            ## Stop parsing
5416            last B;
5417        } else {        } else {
5418          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5419        }        }
# Line 5168  sub _tree_construction_main ($) { Line 5432  sub _tree_construction_main ($) {
5432          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
5433            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5434              !!!cp ('t311');              !!!cp ('t311');
5435              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#character', token => $token);
5436            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
5437              !!!cp ('t312');              !!!cp ('t312');
5438              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
5439            } else { # "after html frameset"            } else { # "after html frameset"
5440              !!!cp ('t313');              !!!cp ('t313');
5441              !!!parse-error (type => 'after html:#character');              !!!parse-error (type => 'after html:#character', token => $token);
5442    
5443              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5444              ## Reprocess in the "main" phase, "after frameset"...              ## Reprocess in the "after frameset" insertion mode.
5445              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
5446            }            }
5447                        
5448            ## Ignore the token.            ## Ignore the token.
# Line 5196  sub _tree_construction_main ($) { Line 5460  sub _tree_construction_main ($) {
5460        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5461          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5462            !!!cp ('t316');            !!!cp ('t316');
5463            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5464    
5465            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
5466            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
5467          } else {          } else {
5468            !!!cp ('t317');            !!!cp ('t317');
5469          }          }
# Line 5207  sub _tree_construction_main ($) { Line 5471  sub _tree_construction_main ($) {
5471          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
5472              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
5473            !!!cp ('t318');            !!!cp ('t318');
5474            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5475            !!!next-token;            !!!next-token;
5476            redo B;            redo B;
5477          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
5478                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
5479            !!!cp ('t319');            !!!cp ('t319');
5480            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5481            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
5482            !!!next-token;            !!!next-token;
5483            redo B;            redo B;
5484          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
5485            !!!cp ('t320');            !!!cp ('t320');
5486            ## NOTE: As if in body.            ## NOTE: As if in body.
5487            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL);
5488            redo B;            redo B;
5489          } else {          } else {
5490            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5491              !!!cp ('t321');              !!!cp ('t321');
5492              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
5493            } else {            } else {
5494              !!!cp ('t322');              !!!cp ('t322');
5495              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
5496            }            }
5497            ## Ignore the token            ## Ignore the token
5498            !!!next-token;            !!!next-token;
# Line 5237  sub _tree_construction_main ($) { Line 5501  sub _tree_construction_main ($) {
5501        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5502          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5503            !!!cp ('t323');            !!!cp ('t323');
5504            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
5505    
5506            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
5507            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
5508          } else {          } else {
5509            !!!cp ('t324');            !!!cp ('t324');
5510          }          }
# Line 5250  sub _tree_construction_main ($) { Line 5514  sub _tree_construction_main ($) {
5514            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] eq 'html' and
5515                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
5516              !!!cp ('t325');              !!!cp ('t325');
5517              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5518              ## Ignore the token              ## Ignore the token
5519              !!!next-token;              !!!next-token;
5520            } else {            } else {
# Line 5276  sub _tree_construction_main ($) { Line 5540  sub _tree_construction_main ($) {
5540          } else {          } else {
5541            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5542              !!!cp ('t330');              !!!cp ('t330');
5543              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
5544            } else {            } else {
5545              !!!cp ('t331');              !!!cp ('t331');
5546              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
5547            }            }
5548            ## Ignore the token            ## Ignore the token
5549            !!!next-token;            !!!next-token;
5550            redo B;            redo B;
5551          }          }
5552          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5553            unless ($self->{open_elements}->[-1]->[1] eq 'html' and
5554                    @{$self->{open_elements}} == 1) { # redundant, maybe
5555              !!!cp ('t331.1');
5556              !!!parse-error (type => 'in body:#eof', token => $token);
5557            } else {
5558              !!!cp ('t331.2');
5559            }
5560            
5561            ## Stop parsing
5562            last B;
5563        } else {        } else {
5564          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5565        }        }
# Line 5299  sub _tree_construction_main ($) { Line 5574  sub _tree_construction_main ($) {
5574        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
5575          !!!cp ('t332');          !!!cp ('t332');
5576          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5577          $script_start_tag->($insert);          $script_start_tag->();
5578          redo B;          redo B;
5579        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
5580          !!!cp ('t333');          !!!cp ('t333');
5581          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5582          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
5583          redo B;          redo B;
5584        } elsif ({        } elsif ({
5585                  base => 1, link => 1,                  base => 1, link => 1,
5586                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5587          !!!cp ('t334');          !!!cp ('t334');
5588          ## 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
5589          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5590          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
5591          !!!next-token;          !!!next-token;
5592          redo B;          redo B;
5593        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
5594          ## 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
5595          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5596          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.
5597    
5598          unless ($self->{confident}) {          unless ($self->{confident}) {
5599            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) { ## TODO: And if supported
5600              !!!cp ('t335');              !!!cp ('t335');
5601              $self->{change_encoding}              $self->{change_encoding}
5602                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
5603                            
5604              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5605                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
# Line 5339  sub _tree_construction_main ($) { Line 5614  sub _tree_construction_main ($) {
5614                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
5615                !!!cp ('t336');                !!!cp ('t336');
5616                $self->{change_encoding}                $self->{change_encoding}
5617                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
5618                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5619                    ->set_user_data (manakai_has_reference =>                    ->set_user_data (manakai_has_reference =>
5620                                         $token->{attributes}->{content}                                         $token->{attributes}->{content}
# Line 5367  sub _tree_construction_main ($) { Line 5642  sub _tree_construction_main ($) {
5642          redo B;          redo B;
5643        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
5644          !!!cp ('t341');          !!!cp ('t341');
         !!!parse-error (type => 'in body:title');  
5645          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5646          $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]);  
           }  
         });  
5647          redo B;          redo B;
5648        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
5649          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body', token => $token);
5650                                
5651          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
5652              $self->{open_elements}->[1]->[1] ne 'body') {              $self->{open_elements}->[1]->[1] ne 'body') {
# Line 5401  sub _tree_construction_main ($) { Line 5667  sub _tree_construction_main ($) {
5667          redo B;          redo B;
5668        } elsif ({        } elsif ({
5669                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
5670                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1,
5671                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5672                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
5673                  pre => 1,                  pre => 1, listing => 1,
5674                    form => 1,
5675                    table => 1,
5676                    hr => 1,
5677                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5678            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
5679              !!!cp ('t350');
5680              !!!parse-error (type => 'in form:form', token => $token);
5681              ## Ignore the token
5682              !!!next-token;
5683              redo B;
5684            }
5685    
5686          ## has a p element in scope          ## has a p element in scope
5687          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5688            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5689              !!!cp ('t344');              !!!cp ('t344');
5690              !!!back-token;              !!!back-token;
5691              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
5692                          line => $token->{line}, column => $token->{column}};
5693              redo B;              redo B;
5694            } elsif ({            } elsif ({
5695                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
5696                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5697                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5698              !!!cp ('t345');              !!!cp ('t345');
# Line 5421  sub _tree_construction_main ($) { Line 5700  sub _tree_construction_main ($) {
5700            }            }
5701          } # INSCOPE          } # INSCOPE
5702                        
5703          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5704          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
5705            !!!next-token;            !!!next-token;
5706            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
5707              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5435  sub _tree_construction_main ($) { Line 5714  sub _tree_construction_main ($) {
5714            } else {            } else {
5715              !!!cp ('t348');              !!!cp ('t348');
5716            }            }
5717          } else {          } elsif ($token->{tag_name} eq 'form') {
5718            !!!cp ('t347');            !!!cp ('t347.1');
5719              $self->{form_element} = $self->{open_elements}->[-1]->[0];
5720    
5721            !!!next-token;            !!!next-token;
5722          }          } elsif ($token->{tag_name} eq 'table') {
5723          redo B;            !!!cp ('t382');
5724        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
5725          if (defined $self->{form_element}) {            
5726            !!!cp ('t350');            $self->{insertion_mode} = IN_TABLE_IM;
5727            !!!parse-error (type => 'in form:form');  
5728            ## Ignore the token            !!!next-token;
5729            } elsif ($token->{tag_name} eq 'hr') {
5730              !!!cp ('t386');
5731              pop @{$self->{open_elements}};
5732            
5733            !!!next-token;            !!!next-token;
           redo B;  
5734          } else {          } else {
5735            ## 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];  
5736            !!!next-token;            !!!next-token;
           redo B;  
5737          }          }
5738        } elsif ($token->{tag_name} eq 'li') {          redo B;
5739          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
5740          ## has a p element in scope          ## has a p element in scope
5741          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5742            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5743              !!!cp ('t353');              !!!cp ('t353');
5744              !!!back-token;              !!!back-token;
5745              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
5746                          line => $token->{line}, column => $token->{column}};
5747              redo B;              redo B;
5748            } elsif ({            } elsif ({
5749                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
5750                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5751                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5752              !!!cp ('t354');              !!!cp ('t354');
# Line 5489  sub _tree_construction_main ($) { Line 5757  sub _tree_construction_main ($) {
5757          ## Step 1          ## Step 1
5758          my $i = -1;          my $i = -1;
5759          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
5760            my $li_or_dtdd = {li => {li => 1},
5761                              dt => {dt => 1, dd => 1},
5762                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
5763          LI: {          LI: {
5764            ## Step 2            ## Step 2
5765            if ($node->[1] eq 'li') {            if ($li_or_dtdd->{$node->[1]}) {
5766              if ($i != -1) {              if ($i != -1) {
5767                !!!cp ('t355');                !!!cp ('t355');
5768                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
5769                                $self->{open_elements}->[-1]->[1]);                                value => $self->{open_elements}->[-1]->[0]
5770                                      ->manakai_local_name,
5771                                  token => $token);
5772              } else {              } else {
5773                !!!cp ('t356');                !!!cp ('t356');
5774              }              }
# Line 5522  sub _tree_construction_main ($) { Line 5795  sub _tree_construction_main ($) {
5795            redo LI;            redo LI;
5796          } # LI          } # LI
5797                        
5798          !!!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});  
5799          !!!next-token;          !!!next-token;
5800          redo B;          redo B;
5801        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
# Line 5587  sub _tree_construction_main ($) { Line 5804  sub _tree_construction_main ($) {
5804            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5805              !!!cp ('t367');              !!!cp ('t367');
5806              !!!back-token;              !!!back-token;
5807              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
5808                          line => $token->{line}, column => $token->{column}};
5809              redo B;              redo B;
5810            } elsif ({            } elsif ({
5811                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
5812                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5813                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5814              !!!cp ('t368');              !!!cp ('t368');
# Line 5598  sub _tree_construction_main ($) { Line 5816  sub _tree_construction_main ($) {
5816            }            }
5817          } # INSCOPE          } # INSCOPE
5818                        
5819          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5820                        
5821          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
5822                        
5823          !!!next-token;          !!!next-token;
5824          redo B;          redo B;
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!cp ('t369');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             !!!cp ('t370');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>  
         ## has an element in scope  
         #my $i;  
         #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
         #  my $node = $self->{open_elements}->[$_];  
         #  if ({  
         #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
         #      }->{$node->[1]}) {  
         #    $i = $_;  
         #    last INSCOPE;  
         #  } elsif ({  
         #            table => 1, caption => 1, td => 1, th => 1,  
         #            button => 1, marquee => 1, object => 1, html => 1,  
         #           }->{$node->[1]}) {  
         #    last INSCOPE;  
         #  }  
         #} # INSCOPE  
         #    
         #if (defined $i) {  
         #  !!! parse-error (type => 'in hn:hn');  
         #  splice @{$self->{open_elements}}, $i;  
         #}  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         !!!next-token;  
         redo B;  
5825        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
5826          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
5827            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
5828            if ($node->[1] eq 'a') {            if ($node->[1] eq 'a') {
5829              !!!cp ('t371');              !!!cp ('t371');
5830              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a', token => $token);
5831                            
5832              !!!back-token;              !!!back-token;
5833              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
5834              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
5835                $formatting_end_tag->($token);
5836                            
5837              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
5838                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
# Line 5685  sub _tree_construction_main ($) { Line 5857  sub _tree_construction_main ($) {
5857                        
5858          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5859    
5860          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5861          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
5862    
5863          !!!next-token;          !!!next-token;
5864          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;  
5865        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
5866          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5867    
# Line 5711  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 'nobr') {            if ($node->[1] eq 'nobr') {
5872              !!!cp ('t376');              !!!cp ('t376');
5873              !!!parse-error (type => 'in nobr:nobr');              !!!parse-error (type => 'in nobr:nobr', token => $token);
5874              !!!back-token;              !!!back-token;
5875              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
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 ('t377');              !!!cp ('t377');
# Line 5724  sub _tree_construction_main ($) { Line 5884  sub _tree_construction_main ($) {
5884            }            }
5885          } # INSCOPE          } # INSCOPE
5886                    
5887          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5888          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
5889                    
5890          !!!next-token;          !!!next-token;
# Line 5735  sub _tree_construction_main ($) { Line 5895  sub _tree_construction_main ($) {
5895            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
5896            if ($node->[1] eq 'button') {            if ($node->[1] eq 'button') {
5897              !!!cp ('t378');              !!!cp ('t378');
5898              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button', token => $token);
5899              !!!back-token;              !!!back-token;
5900              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button',
5901                          line => $token->{line}, column => $token->{column}};
5902              redo B;              redo B;
5903            } elsif ({            } elsif ({
5904                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
5905                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5906                     }->{$node->[1]}) {                     }->{$node->[1]}) {
5907              !!!cp ('t379');              !!!cp ('t379');
# Line 5750  sub _tree_construction_main ($) { Line 5911  sub _tree_construction_main ($) {
5911                        
5912          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5913                        
5914          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5915          push @$active_formatting_elements, ['#marker', ''];  
5916            ## TODO: associate with $self->{form_element} if defined
5917    
         !!!next-token;  
         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});  
5918          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
5919            
         !!!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;  
             
5920          !!!next-token;          !!!next-token;
5921          redo B;          redo B;
5922        } elsif ({        } elsif ({
5923                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
5924                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
5925                  image => 1,                  noembed => 1,
5926                    noframes => 1,
5927                    noscript => 0, ## TODO: 1 if scripting is enabled
5928                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5929          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
5930            !!!cp ('t384');            !!!cp ('t381');
5931            !!!parse-error (type => 'image');            $reconstruct_active_formatting_elements->($insert_to_current);
           $token->{tag_name} = 'img';  
5932          } else {          } else {
5933            !!!cp ('t385');            !!!cp ('t399');
5934          }          }
5935            ## NOTE: There is an "as if in body" code clone.
5936          ## 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;  
5937          redo B;          redo B;
5938        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
5939          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
5940                    
5941          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
5942            !!!cp ('t389');            !!!cp ('t389');
# Line 5864  sub _tree_construction_main ($) { Line 5953  sub _tree_construction_main ($) {
5953            delete $at->{prompt};            delete $at->{prompt};
5954            my @tokens = (            my @tokens = (
5955                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
5956                           attributes => $form_attrs},                           attributes => $form_attrs,
5957                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
5958                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
5959                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
5960                            {type => START_TAG_TOKEN, tag_name => 'p',
5961                             line => $token->{line}, column => $token->{column}},
5962                            {type => START_TAG_TOKEN, tag_name => 'label',
5963                             line => $token->{line}, column => $token->{column}},
5964                         );                         );
5965            if ($prompt_attr) {            if ($prompt_attr) {
5966              !!!cp ('t390');              !!!cp ('t390');
5967              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
5968                               #line => $token->{line}, column => $token->{column},
5969                              };
5970            } else {            } else {
5971              !!!cp ('t391');              !!!cp ('t391');
5972              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
5973                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
5974                               #line => $token->{line}, column => $token->{column},
5975                              }; # SHOULD
5976              ## TODO: make this configurable              ## TODO: make this configurable
5977            }            }
5978            push @tokens,            push @tokens,
5979                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
5980                             line => $token->{line}, column => $token->{column}},
5981                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
5982                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
5983                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
5984                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
5985                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
5986                            {type => START_TAG_TOKEN, tag_name => 'hr',
5987                             line => $token->{line}, column => $token->{column}},
5988                            {type => END_TAG_TOKEN, tag_name => 'form',
5989                             line => $token->{line}, column => $token->{column}};
5990            $token = shift @tokens;            $token = shift @tokens;
5991            !!!back-token (@tokens);            !!!back-token (@tokens);
5992            redo B;            redo B;
# Line 5892  sub _tree_construction_main ($) { Line 5994  sub _tree_construction_main ($) {
5994        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
5995          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
5996          my $el;          my $el;
5997          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $token->{tag_name}, $token->{attributes}, $token);
5998                    
5999          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6000          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5931  sub _tree_construction_main ($) { Line 6033  sub _tree_construction_main ($) {
6033            ## Ignore the token            ## Ignore the token
6034          } else {          } else {
6035            !!!cp ('t398');            !!!cp ('t398');
6036            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6037          }          }
6038          !!!next-token;          !!!next-token;
6039          redo B;          redo B;
6040        } 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});  
           
         $self->{insertion_mode} = IN_SELECT_IM;  
         !!!next-token;  
         redo B;  
       } elsif ({  
6041                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6042                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
6043                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
6044                  thead => 1, tr => 1,                  thead => 1, tr => 1,
6045                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6046          !!!cp ('t401');          !!!cp ('t401');
6047          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6048          ## Ignore the token          ## Ignore the token
6049          !!!next-token;          !!!next-token;
6050          redo B;          redo B;
6051                    
6052          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6053        } else {        } else {
6054          !!!cp ('t402');          if ($token->{tag_name} eq 'image') {
6055              !!!cp ('t384');
6056              !!!parse-error (type => 'image', token => $token);
6057              $token->{tag_name} = 'img';
6058            } else {
6059              !!!cp ('t385');
6060            }
6061    
6062            ## NOTE: There is an "as if <br>" code clone.
6063          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6064                    
6065          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6066    
6067            if ({
6068                 applet => 1, marquee => 1, object => 1,
6069                }->{$token->{tag_name}}) {
6070              !!!cp ('t380');
6071              push @$active_formatting_elements, ['#marker', ''];
6072            } elsif ({
6073                      b => 1, big => 1, em => 1, font => 1, i => 1,
6074                      s => 1, small => 1, strile => 1,
6075                      strong => 1, tt => 1, u => 1,
6076                     }->{$token->{tag_name}}) {
6077              !!!cp ('t375');
6078              push @$active_formatting_elements, $self->{open_elements}->[-1];
6079            } elsif ($token->{tag_name} eq 'input') {
6080              !!!cp ('t388');
6081              ## TODO: associate with $self->{form_element} if defined
6082              pop @{$self->{open_elements}};
6083            } elsif ({
6084                      area => 1, basefont => 1, bgsound => 1, br => 1,
6085                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6086                      #image => 1,
6087                     }->{$token->{tag_name}}) {
6088              !!!cp ('t388.1');
6089              pop @{$self->{open_elements}};
6090            } elsif ($token->{tag_name} eq 'select') {
6091              ## TODO: associate with $self->{form_element} if defined
6092            
6093              if ($self->{insertion_mode} & TABLE_IMS or
6094                  $self->{insertion_mode} & BODY_TABLE_IMS or
6095                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6096                !!!cp ('t400.1');
6097                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6098              } else {
6099                !!!cp ('t400.2');
6100                $self->{insertion_mode} = IN_SELECT_IM;
6101              }
6102            } else {
6103              !!!cp ('t402');
6104            }
6105                    
6106          !!!next-token;          !!!next-token;
6107          redo B;          redo B;
6108        }        }
6109      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6110        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
6111          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
6112              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
6113            for (@{$self->{open_elements}}) {          INSCOPE: {
6114              unless ({            for (reverse @{$self->{open_elements}}) {
6115                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] eq 'body') {
6116                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
6117                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
6118                      }->{$_->[1]}) {                last INSCOPE;
6119                !!!cp ('t403');              } elsif ({
6120                !!!parse-error (type => 'not closed:'.$_->[1]);                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
6121              } else {                        button => 1, marquee => 1, object => 1, html => 1,
6122                !!!cp ('t404');                       }->{$_->[1]}) {
6123                  !!!cp ('t405.1');
6124                  last;
6125              }              }
6126            }            }
6127    
6128            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
6129            !!!next-token;                            value => $token->{tag_name}, token => $token);
6130            redo B;            ## NOTE: Ignore the token.
         } else {  
           !!!cp ('t405');  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
6131            !!!next-token;            !!!next-token;
6132            redo B;            redo B;
6133            } # INSCOPE
6134    
6135            for (@{$self->{open_elements}}) {
6136              unless ({
6137                       dd => 1, dt => 1, li => 1, p => 1, td => 1,
6138                       th => 1, tr => 1, body => 1, html => 1,
6139                       tbody => 1, tfoot => 1, thead => 1,
6140                      }->{$_->[1]}) {
6141                !!!cp ('t403');
6142                !!!parse-error (type => 'not closed',
6143                                value => $_->[0]->manakai_local_name,
6144                                token => $token);
6145                last;
6146              } else {
6147                !!!cp ('t404');
6148              }
6149          }          }
6150    
6151            $self->{insertion_mode} = AFTER_BODY_IM;
6152            !!!next-token;
6153            redo B;
6154        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
6155            ## TODO: Update this code.  It seems that the code below is not
6156            ## up-to-date, though it has same effect as speced.
6157          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') {
6158            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6159            if ($self->{open_elements}->[-1]->[1] ne 'body') {            if ($self->{open_elements}->[-1]->[1] ne 'body') {
6160              !!!cp ('t406');              !!!cp ('t406');
6161              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed',
6162                                value => $self->{open_elements}->[1]->[0]
6163                                    ->manakai_local_name,
6164                                token => $token);
6165            } else {            } else {
6166              !!!cp ('t407');              !!!cp ('t407');
6167            }            }
# Line 6017  sub _tree_construction_main ($) { Line 6170  sub _tree_construction_main ($) {
6170            redo B;            redo B;
6171          } else {          } else {
6172            !!!cp ('t408');            !!!cp ('t408');
6173            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6174            ## Ignore the token            ## Ignore the token
6175            !!!next-token;            !!!next-token;
6176            redo B;            redo B;
# Line 6026  sub _tree_construction_main ($) { Line 6179  sub _tree_construction_main ($) {
6179                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6180                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
6181                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
6182                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
6183                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
6184                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6185          ## has an element in scope          ## has an element in scope
6186          my $i;          my $i;
6187          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6188            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6189            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] eq $token->{tag_name}) {
             ## generate implied end tags  
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t409');  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
               
6190              !!!cp ('t410');              !!!cp ('t410');
6191              $i = $_;              $i = $_;
6192              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
6193            } elsif ({            } elsif ({
6194                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
6195                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
6196                     }->{$node->[1]}) {                     }->{$node->[1]}) {
6197              !!!cp ('t411');              !!!cp ('t411');
6198              last INSCOPE;              last INSCOPE;
6199            }            }
6200          } # INSCOPE          } # INSCOPE
6201            
6202          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6203            if (defined $i) {            !!!cp ('t413');
6204              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6205            } else {
6206              ## Step 1. generate implied end tags
6207              while ({
6208                      dd => ($token->{tag_name} ne 'dd'),
6209                      dt => ($token->{tag_name} ne 'dt'),
6210                      li => ($token->{tag_name} ne 'li'),
6211                      p => 1,
6212                     }->{$self->{open_elements}->[-1]->[1]}) {
6213                !!!cp ('t409');
6214                pop @{$self->{open_elements}};
6215              }
6216    
6217              ## Step 2.
6218              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6219              !!!cp ('t412');              !!!cp ('t412');
6220              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
6221                                value => $self->{open_elements}->[-1]->[0]
6222                                    ->manakai_local_name,
6223                                token => $token);
6224            } else {            } else {
6225              !!!cp ('t413');              !!!cp ('t414');
             !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
6226            }            }
6227          }  
6228                      ## Step 3.
         if (defined $i) {  
           !!!cp ('t414');  
6229            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6230          } elsif ($token->{tag_name} eq 'p') {  
6231            !!!cp ('t415');            ## Step 4.
6232            ## As if <p>, then reprocess the current token            $clear_up_to_marker->()
6233            my $el;                if {
6234            !!!create-element ($el, 'p');                  applet => 1, button => 1, marquee => 1, object => 1,
6235            $insert->($el);                }->{$token->{tag_name}};
         } else {  
           !!!cp ('t416');  
6236          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
6237          !!!next-token;          !!!next-token;
6238          redo B;          redo B;
6239        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
6240            undef $self->{form_element};
6241    
6242          ## has an element in scope          ## has an element in scope
6243            my $i;
6244          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6245            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6246            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] eq $token->{tag_name}) {
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t417');  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
               
6247              !!!cp ('t418');              !!!cp ('t418');
6248                $i = $_;
6249              last INSCOPE;              last INSCOPE;
6250            } elsif ({            } elsif ({
6251                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
6252                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
6253                     }->{$node->[1]}) {                     }->{$node->[1]}) {
6254              !!!cp ('t419');              !!!cp ('t419');
6255              last INSCOPE;              last INSCOPE;
6256            }            }
6257          } # INSCOPE          } # INSCOPE
6258            
6259          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 {  
6260            !!!cp ('t421');            !!!cp ('t421');
6261            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6262            } else {
6263              ## Step 1. generate implied end tags
6264              while ({
6265                      dd => 1, dt => 1, li => 1, p => 1,
6266                     }->{$self->{open_elements}->[-1]->[1]}) {
6267                !!!cp ('t417');
6268                pop @{$self->{open_elements}};
6269              }
6270              
6271              ## Step 2.
6272              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6273                !!!cp ('t417.1');
6274                !!!parse-error (type => 'not closed',
6275                                value => $self->{open_elements}->[-1]->[0]
6276                                    ->manakai_local_name,
6277                                token => $token);
6278              } else {
6279                !!!cp ('t420');
6280              }  
6281              
6282              ## Step 3.
6283              splice @{$self->{open_elements}}, $i;
6284          }          }
6285    
         undef $self->{form_element};  
6286          !!!next-token;          !!!next-token;
6287          redo B;          redo B;
6288        } elsif ({        } elsif ({
# Line 6141  sub _tree_construction_main ($) { Line 6295  sub _tree_construction_main ($) {
6295            if ({            if ({
6296                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6297                }->{$node->[1]}) {                }->{$node->[1]}) {
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t422');  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
   
6298              !!!cp ('t423');              !!!cp ('t423');
6299              $i = $_;              $i = $_;
6300              last INSCOPE;              last INSCOPE;
6301            } elsif ({            } elsif ({
6302                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
6303                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
6304                     }->{$node->[1]}) {                     }->{$node->[1]}) {
6305              !!!cp ('t424');              !!!cp ('t424');
6306              last INSCOPE;              last INSCOPE;
6307            }            }
6308          } # INSCOPE          } # INSCOPE
6309    
6310            unless (defined $i) { # has an element in scope
6311              !!!cp ('t425.1');
6312              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6313            } else {
6314              ## Step 1. generate implied end tags
6315              while ({
6316                      dd => 1, dt => 1, li => 1, p => 1,
6317                     }->{$self->{open_elements}->[-1]->[1]}) {
6318                !!!cp ('t422');
6319                pop @{$self->{open_elements}};
6320              }
6321              
6322              ## Step 2.
6323              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6324                !!!cp ('t425');
6325                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6326              } else {
6327                !!!cp ('t426');
6328              }
6329    
6330              ## Step 3.
6331              splice @{$self->{open_elements}}, $i;
6332            }
6333                    
6334          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          !!!next-token;
6335            !!!cp ('t425');          redo B;
6336            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});        } elsif ($token->{tag_name} eq 'p') {
6337            ## has an element in scope
6338            my $i;
6339            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6340              my $node = $self->{open_elements}->[$_];
6341              if ($node->[1] eq $token->{tag_name}) {
6342                !!!cp ('t410.1');
6343                $i = $_;
6344                last INSCOPE;
6345              } elsif ({
6346                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
6347                        button => 1, marquee => 1, object => 1, html => 1,
6348                       }->{$node->[1]}) {
6349                !!!cp ('t411.1');
6350                last INSCOPE;
6351              }
6352            } # INSCOPE
6353    
6354            if (defined $i) {
6355              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6356                !!!cp ('t412.1');
6357                !!!parse-error (type => 'not closed',
6358                                value => $self->{open_elements}->[-1]->[0]
6359                                    ->manakai_local_name,
6360                                token => $token);
6361              } else {
6362                !!!cp ('t414.1');
6363              }
6364    
6365              splice @{$self->{open_elements}}, $i;
6366          } else {          } else {
6367            !!!cp ('t426');            !!!cp ('t413.1');
6368              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6369    
6370              !!!cp ('t415.1');
6371              ## As if <p>, then reprocess the current token
6372              my $el;
6373              !!!create-element ($el, 'p',, $token);
6374              $insert->($el);
6375              ## NOTE: Not inserted into |$self->{open_elements}|.
6376          }          }
6377            
         splice @{$self->{open_elements}}, $i if defined $i;  
6378          !!!next-token;          !!!next-token;
6379          redo B;          redo B;
6380        } elsif ({        } elsif ({
# Line 6183  sub _tree_construction_main ($) { Line 6384  sub _tree_construction_main ($) {
6384                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
6385                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6386          !!!cp ('t427');          !!!cp ('t427');
6387          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token);
6388          redo B;          redo B;
6389        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
6390          !!!cp ('t428');          !!!cp ('t428');
6391          !!!parse-error (type => 'unmatched end tag:br');          !!!parse-error (type => 'unmatched end tag:br', token => $token);
6392    
6393          ## As if <br>          ## As if <br>
6394          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6395                    
6396          my $el;          my $el;
6397          !!!create-element ($el, 'br');          !!!create-element ($el, 'br',, $token);
6398          $insert->($el);          $insert->($el);
6399                    
6400          ## Ignore the token.          ## Ignore the token.
# Line 6212  sub _tree_construction_main ($) { Line 6413  sub _tree_construction_main ($) {
6413                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
6414                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6415          !!!cp ('t429');          !!!cp ('t429');
6416          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6417          ## Ignore the token          ## Ignore the token
6418          !!!next-token;          !!!next-token;
6419          redo B;          redo B;
# Line 6229  sub _tree_construction_main ($) { Line 6430  sub _tree_construction_main ($) {
6430            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] eq $token->{tag_name}) {
6431              ## Step 1              ## Step 1
6432              ## generate implied end tags              ## generate implied end tags
6433              if ({              while ({
6434                   dd => 1, dt => 1, li => 1, p => 1,                      dd => 1, dt => 1, li => 1, p => 1,
6435                   td => 1, th => 1, tr => 1,                     }->{$self->{open_elements}->[-1]->[1]}) {
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
6436                !!!cp ('t430');                !!!cp ('t430');
6437                !!!back-token;                ## ISSUE: Can this case be reached?
6438                $token = {type => END_TAG_TOKEN,                pop @{$self->{open_elements}};
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
6439              }              }
6440                    
6441              ## Step 2              ## Step 2
6442              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
6443                !!!cp ('t431');                !!!cp ('t431');
6444                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
6445                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
6446                                  value => $self->{open_elements}->[-1]->[0]
6447                                      ->manakai_local_name,
6448                                  token => $token);
6449              } else {              } else {
6450                !!!cp ('t432');                !!!cp ('t432');
6451              }              }
# Line 6262  sub _tree_construction_main ($) { Line 6462  sub _tree_construction_main ($) {
6462                  ($special_category->{$node->[1]} or                  ($special_category->{$node->[1]} or
6463                   $scoping_category->{$node->[1]})) {                   $scoping_category->{$node->[1]})) {
6464                !!!cp ('t433');                !!!cp ('t433');
6465                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6466                ## Ignore the token                ## Ignore the token
6467                !!!next-token;                !!!next-token;
6468                last S2;                last S2;
# Line 6284  sub _tree_construction_main ($) { Line 6484  sub _tree_construction_main ($) {
6484      redo B;      redo B;
6485    } # B    } # B
6486    
   ## NOTE: The "trailing end" phase in HTML5 is split into  
   ## two insertion modes: "after html body" and "after html frameset".  
   ## NOTE: States in the main stage is preserved while  
   ## the parser stays in the trailing end phase. # MUST  
   
6487    ## Stop parsing # MUST    ## Stop parsing # MUST
6488        
6489    ## TODO: script stuffs    ## TODO: script stuffs
# Line 6330  sub set_inner_html ($$$) { Line 6525  sub set_inner_html ($$$) {
6525      my $p = $class->new;      my $p = $class->new;
6526      $p->{document} = $doc;      $p->{document} = $doc;
6527    
6528      ## Step 9 # MUST      ## Step 8 # MUST
6529      my $i = 0;      my $i = 0;
6530      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
6531      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
6532      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
6533        my $self = shift;        my $self = shift;
6534    
# Line 6342  sub set_inner_html ($$$) { Line 6537  sub set_inner_html ($$$) {
6537    
6538        $self->{next_char} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
6539        $self->{next_char} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
6540        $column++;  
6541          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
6542          $p->{column}++;
6543    
6544        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
6545          $line++;          $p->{line}++;
6546          $column = 0;          $p->{column} = 0;
6547          !!!cp ('i1');          !!!cp ('i1');
6548        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
6549          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
6550          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
6551          $line++;          $p->{line}++;
6552          $column = 0;          $p->{column} = 0;
6553          !!!cp ('i2');          !!!cp ('i2');
6554        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
6555          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
# Line 6368  sub set_inner_html ($$$) { Line 6565  sub set_inner_html ($$$) {
6565            
6566      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
6567        my (%opt) = @_;        my (%opt) = @_;
6568        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
6569          my $column = $opt{column};
6570          if (defined $opt{token} and defined $opt{token}->{line}) {
6571            $line = $opt{token}->{line};
6572            $column = $opt{token}->{column};
6573          }
6574          warn "Parse error ($opt{type}) at line $line column $column\n";
6575      };      };
6576      $p->{parse_error} = sub {      $p->{parse_error} = sub {
6577        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
6578      };      };
6579            
6580      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
# Line 6397  sub set_inner_html ($$$) { Line 6600  sub set_inner_html ($$$) {
6600    
6601      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
6602    
6603      ## Step 4      ## Step 3
6604      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
6605        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
6606    
6607      ## Step 5 # MUST      ## Step 4 # MUST
6608      $doc->append_child ($root);      $doc->append_child ($root);
6609    
6610      ## Step 6 # MUST      ## Step 5 # MUST
6611      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, 'html'];
6612    
6613      undef $p->{head_element};      undef $p->{head_element};
6614    
6615      ## Step 7 # MUST      ## Step 6 # MUST
6616      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
6617    
6618      ## Step 8 # MUST      ## Step 7 # MUST
6619      my $anode = $node;      my $anode = $node;
6620      AN: while (defined $anode) {      AN: while (defined $anode) {
6621        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
# Line 6428  sub set_inner_html ($$$) { Line 6631  sub set_inner_html ($$$) {
6631        $anode = $anode->parent_node;        $anode = $anode->parent_node;
6632      } # AN      } # AN
6633            
6634      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
6635      {      {
6636        my $self = $p;        my $self = $p;
6637        !!!next-token;        !!!next-token;
6638      }      }
6639      $p->_tree_construction_main;      $p->_tree_construction_main;
6640    
6641      ## Step 11 # MUST      ## Step 10 # MUST
6642      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
6643      for (@cn) {      for (@cn) {
6644        $node->remove_child ($_);        $node->remove_child ($_);
6645      }      }
6646      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
6647    
6648      ## Step 12 # MUST      ## Step 11 # MUST
6649      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
6650      for (@cn) {      for (@cn) {
6651        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 6452  sub set_inner_html ($$$) { Line 6654  sub set_inner_html ($$$) {
6654      ## ISSUE: mutation events?      ## ISSUE: mutation events?
6655    
6656      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
6657    
6658        delete $p->{parse_error}; # delete loop
6659    } else {    } else {
6660      die "$0: |set_inner_html| is not defined for node of type $nt";      die "$0: |set_inner_html| is not defined for node of type $nt";
6661    }    }

Legend:
Removed from v.1.81  
changed lines
  Added in v.1.122

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24