/[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.91 by wakaba, Sat Mar 8 03:04:09 2008 UTC revision 1.114 by wakaba, Sun Mar 16 11:40:19 2008 UTC
# Line 74  my $special_category = { Line 74  my $special_category = {
74    textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,    textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,
75  };  };
76  my $scoping_category = {  my $scoping_category = {
77    button => 1, caption => 1, html => 1, marquee => 1, object => 1,    applet => 1, button => 1, caption => 1, html => 1, marquee => 1, object => 1,
78    table => 1, td => 1, th => 1,    table => 1, td => 1, th => 1,
79  };  };
80  my $formatting_category = {  my $formatting_category = {
# Line 108  sub parse_byte_string ($$$$;$) { Line 108  sub parse_byte_string ($$$$;$) {
108    $self->{change_encoding} = sub {    $self->{change_encoding} = sub {
109      my $self = shift;      my $self = shift;
110      my $charset = lc shift;      my $charset = lc shift;
111        my $token = shift;
112      ## TODO: if $charset is supported      ## TODO: if $charset is supported
113      ## TODO: normalize charset name      ## TODO: normalize charset name
114    
# Line 126  sub parse_byte_string ($$$$;$) { Line 127  sub parse_byte_string ($$$$;$) {
127      }      }
128    
129      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
130          ':'.$charset, level => 'w');          ':'.$charset, level => 'w', token => $token);
131    
132      ## Step 3      ## Step 3
133      # if (can) {      # if (can) {
# Line 177  sub parse_string ($$$;$) { Line 178  sub parse_string ($$$;$) {
178        if defined $self->{input_encoding};        if defined $self->{input_encoding};
179    
180    my $i = 0;    my $i = 0;
181    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
182    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
183    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
184      my $self = shift;      my $self = shift;
185    
# Line 187  sub parse_string ($$$;$) { Line 188  sub parse_string ($$$;$) {
188    
189      $self->{next_char} = -1 and return if $i >= length $$s;      $self->{next_char} = -1 and return if $i >= length $$s;
190      $self->{next_char} = ord substr $$s, $i++, 1;      $self->{next_char} = ord substr $$s, $i++, 1;
191      $column++;  
192        ($self->{line_prev}, $self->{column_prev})
193            = ($self->{line}, $self->{column});
194        $self->{column}++;
195            
196      if ($self->{next_char} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
197        $line++;        $self->{line}++;
198        $column = 0;        $self->{column} = 0;
199      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
200        $i++ if substr ($$s, $i, 1) eq "\x0A";        $i++ if substr ($$s, $i, 1) eq "\x0A";
201        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
202        $line++;        $self->{line}++;
203        $column = 0;        $self->{column} = 0;
204      } elsif ($self->{next_char} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
205        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
206      } elsif ($self->{next_char} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
# Line 209  sub parse_string ($$$;$) { Line 213  sub parse_string ($$$;$) {
213    
214    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
215      my (%opt) = @_;      my (%opt) = @_;
216      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
217        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
218        warn "Parse error ($opt{type}) at line $line column $column\n";
219    };    };
220    $self->{parse_error} = sub {    $self->{parse_error} = sub {
221      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
222    };    };
223    
224    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 220  sub parse_string ($$$;$) { Line 226  sub parse_string ($$$;$) {
226    $self->_construct_tree;    $self->_construct_tree;
227    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
228    
229      delete $self->{parse_error}; # remove loop
230    
231    return $self->{document};    return $self->{document};
232  } # parse_string  } # parse_string
233    
# Line 303  sub TABLE_IMS ()      { 0b1000000 } Line 311  sub TABLE_IMS ()      { 0b1000000 }
311  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
312  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
313  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
314    sub SELECT_IMS ()     { 0b10000000000 }
315    
316  ## NOTE: "initial" and "before html" insertion modes have no constants.  ## NOTE: "initial" and "before html" insertion modes have no constants.
317    
# Line 325  sub IN_TABLE_IM () { TABLE_IMS } Line 334  sub IN_TABLE_IM () { TABLE_IMS }
334  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
335  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
336  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
337  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
338    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
339  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
340    
341  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
# Line 447  sub _get_next_token ($) { Line 457  sub _get_next_token ($) {
457          #          #
458        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
459          !!!cp (11);          !!!cp (11);
460          !!!emit ({type => END_OF_FILE_TOKEN});          !!!emit ({type => END_OF_FILE_TOKEN,
461                      line => $self->{line}, column => $self->{column}});
462          last A; ## TODO: ok?          last A; ## TODO: ok?
463        } else {        } else {
464          !!!cp (12);          !!!cp (12);
465        }        }
466        # Anything else        # Anything else
467        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
468                     data => chr $self->{next_char}};                     data => chr $self->{next_char},
469                       line => $self->{line}, column => $self->{column}};
470        ## Stay in the data state        ## Stay in the data state
471        !!!next-input-character;        !!!next-input-character;
472    
# Line 463  sub _get_next_token ($) { Line 475  sub _get_next_token ($) {
475        redo A;        redo A;
476      } elsif ($self->{state} == ENTITY_DATA_STATE) {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
477        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
478    
479          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
480                
481        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
482    
# Line 471  sub _get_next_token ($) { Line 485  sub _get_next_token ($) {
485    
486        unless (defined $token) {        unless (defined $token) {
487          !!!cp (13);          !!!cp (13);
488          !!!emit ({type => CHARACTER_TOKEN, data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&',
489                      line => $l, column => $c});
490        } else {        } else {
491          !!!cp (14);          !!!cp (14);
492          !!!emit ($token);          !!!emit ($token);
# Line 490  sub _get_next_token ($) { Line 505  sub _get_next_token ($) {
505            ## reconsume            ## reconsume
506            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
507    
508            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
509                        line => $self->{line_prev},
510                        column => $self->{column_prev}});
511    
512            redo A;            redo A;
513          }          }
# Line 510  sub _get_next_token ($) { Line 527  sub _get_next_token ($) {
527            !!!cp (19);            !!!cp (19);
528            $self->{current_token}            $self->{current_token}
529              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
530                 tag_name => chr ($self->{next_char} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
531                   line => $self->{line_prev},
532                   column => $self->{column_prev}};
533            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
534            !!!next-input-character;            !!!next-input-character;
535            redo A;            redo A;
# Line 518  sub _get_next_token ($) { Line 537  sub _get_next_token ($) {
537                   $self->{next_char} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
538            !!!cp (20);            !!!cp (20);
539            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
540                              tag_name => chr ($self->{next_char})};                                      tag_name => chr ($self->{next_char}),
541                                        line => $self->{line_prev},
542                                        column => $self->{column_prev}};
543            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
544            !!!next-input-character;            !!!next-input-character;
545            redo A;            redo A;
# Line 528  sub _get_next_token ($) { Line 549  sub _get_next_token ($) {
549            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
550            !!!next-input-character;            !!!next-input-character;
551    
552            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
553                        line => $self->{line_prev},
554                        column => $self->{column_prev}});
555    
556            redo A;            redo A;
557          } elsif ($self->{next_char} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
558            !!!cp (22);            !!!cp (22);
559            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio');
560            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
561              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
562                                        line => $self->{line_prev},
563                                        column => $self->{column_prev}};
564            ## $self->{next_char} is intentionally left as is            ## $self->{next_char} is intentionally left as is
565            redo A;            redo A;
566          } else {          } else {
# Line 543  sub _get_next_token ($) { Line 569  sub _get_next_token ($) {
569            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
570            ## reconsume            ## reconsume
571    
572            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
573                        line => $self->{line_prev},
574                        column => $self->{column_prev}});
575    
576            redo A;            redo A;
577          }          }
# Line 551  sub _get_next_token ($) { Line 579  sub _get_next_token ($) {
579          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
580        }        }
581      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
582          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
583        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
584          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
585    
586            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
587            my @next_char;            my @next_char;
588            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
# Line 569  sub _get_next_token ($) { Line 599  sub _get_next_token ($) {
599                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
600                $self->{state} = DATA_STATE;                $self->{state} = DATA_STATE;
601    
602                !!!emit ({type => CHARACTER_TOKEN, data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
603                            line => $l, column => $c});
604        
605                redo A;                redo A;
606              }              }
# Line 588  sub _get_next_token ($) { Line 619  sub _get_next_token ($) {
619              $self->{next_char} = shift @next_char; # reconsume              $self->{next_char} = shift @next_char; # reconsume
620              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
621              $self->{state} = DATA_STATE;              $self->{state} = DATA_STATE;
622              !!!emit ({type => CHARACTER_TOKEN, data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
623                          line => $l, column => $c});
624              redo A;              redo A;
625            } else {            } else {
626              !!!cp (27);              !!!cp (27);
# Line 601  sub _get_next_token ($) { Line 633  sub _get_next_token ($) {
633            !!!cp (28);            !!!cp (28);
634            # next-input-character is already done            # next-input-character is already done
635            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
636            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
637                        line => $l, column => $c});
638            redo A;            redo A;
639          }          }
640        }        }
# Line 609  sub _get_next_token ($) { Line 642  sub _get_next_token ($) {
642        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{next_char} and
643            $self->{next_char} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
644          !!!cp (29);          !!!cp (29);
645          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token}
646                            tag_name => chr ($self->{next_char} + 0x0020)};              = {type => END_TAG_TOKEN,
647                   tag_name => chr ($self->{next_char} + 0x0020),
648                   line => $l, column => $c};
649          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
650          !!!next-input-character;          !!!next-input-character;
651          redo A;          redo A;
# Line 618  sub _get_next_token ($) { Line 653  sub _get_next_token ($) {
653                 $self->{next_char} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
654          !!!cp (30);          !!!cp (30);
655          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
656                            tag_name => chr ($self->{next_char})};                                    tag_name => chr ($self->{next_char}),
657                                      line => $l, column => $c};
658          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
659          !!!next-input-character;          !!!next-input-character;
660          redo A;          redo A;
# Line 634  sub _get_next_token ($) { Line 670  sub _get_next_token ($) {
670          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
671          # reconsume          # reconsume
672    
673          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
674                      line => $l, column => $c});
675    
676          redo A;          redo A;
677        } else {        } else {
678          !!!cp (33);          !!!cp (33);
679          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
680          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
681            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
682                                      line => $self->{line_prev}, # "<" of "</"
683                                      column => $self->{column_prev} - 1};
684          ## $self->{next_char} is intentionally left as is          ## $self->{next_char} is intentionally left as is
685          redo A;          redo A;
686        }        }
# Line 1377  sub _get_next_token ($) { Line 1417  sub _get_next_token ($) {
1417      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1418        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1419                
1420        my $token = {type => COMMENT_TOKEN, data => ''};        ## NOTE: Set by the previous state
1421          #my $token = {type => COMMENT_TOKEN, data => ''};
1422    
1423        BC: {        BC: {
1424          if ($self->{next_char} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
# Line 1385  sub _get_next_token ($) { Line 1426  sub _get_next_token ($) {
1426            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1427            !!!next-input-character;            !!!next-input-character;
1428    
1429            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1430    
1431            redo A;            redo A;
1432          } elsif ($self->{next_char} == -1) {          } elsif ($self->{next_char} == -1) {
# Line 1393  sub _get_next_token ($) { Line 1434  sub _get_next_token ($) {
1434            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1435            ## reconsume            ## reconsume
1436    
1437            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1438    
1439            redo A;            redo A;
1440          } else {          } else {
1441            !!!cp (126);            !!!cp (126);
1442            $token->{data} .= chr ($self->{next_char});            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1443            !!!next-input-character;            !!!next-input-character;
1444            redo BC;            redo BC;
1445          }          }
# Line 1408  sub _get_next_token ($) { Line 1449  sub _get_next_token ($) {
1449      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1450        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1451    
1452          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1453    
1454        my @next_char;        my @next_char;
1455        push @next_char, $self->{next_char};        push @next_char, $self->{next_char};
1456                
# Line 1416  sub _get_next_token ($) { Line 1459  sub _get_next_token ($) {
1459          push @next_char, $self->{next_char};          push @next_char, $self->{next_char};
1460          if ($self->{next_char} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1461            !!!cp (127);            !!!cp (127);
1462            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1463                                        line => $l, column => $c};
1464            $self->{state} = COMMENT_START_STATE;            $self->{state} = COMMENT_START_STATE;
1465            !!!next-input-character;            !!!next-input-character;
1466            redo A;            redo A;
# Line 1452  sub _get_next_token ($) { Line 1496  sub _get_next_token ($) {
1496                      !!!cp (129);                      !!!cp (129);
1497                      ## TODO: What a stupid code this is!                      ## TODO: What a stupid code this is!
1498                      $self->{state} = DOCTYPE_STATE;                      $self->{state} = DOCTYPE_STATE;
1499                        $self->{current_token} = {type => DOCTYPE_TOKEN,
1500                                                  quirks => 1,
1501                                                  line => $l, column => $c};
1502                      !!!next-input-character;                      !!!next-input-character;
1503                      redo A;                      redo A;
1504                    } else {                    } else {
# Line 1480  sub _get_next_token ($) { Line 1527  sub _get_next_token ($) {
1527        $self->{next_char} = shift @next_char;        $self->{next_char} = shift @next_char;
1528        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1529        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
1530          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1531                                    line => $l, column => $c};
1532        redo A;        redo A;
1533                
1534        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
# Line 1603  sub _get_next_token ($) { Line 1652  sub _get_next_token ($) {
1652          redo A;          redo A;
1653        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
1654          !!!cp (152);          !!!cp (152);
1655          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
1656                            line => $self->{line_prev},
1657                            column => $self->{column_prev});
1658          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
1659          ## Stay in the state          ## Stay in the state
1660          !!!next-input-character;          !!!next-input-character;
# Line 1619  sub _get_next_token ($) { Line 1670  sub _get_next_token ($) {
1670          redo A;          redo A;
1671        } else {        } else {
1672          !!!cp (154);          !!!cp (154);
1673          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
1674                            line => $self->{line_prev},
1675                            column => $self->{column_prev});
1676          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
1677          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
1678          !!!next-input-character;          !!!next-input-character;
# Line 1658  sub _get_next_token ($) { Line 1711  sub _get_next_token ($) {
1711          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1712          !!!next-input-character;          !!!next-input-character;
1713    
1714          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
1715    
1716          redo A;          redo A;
1717        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
# Line 1667  sub _get_next_token ($) { Line 1720  sub _get_next_token ($) {
1720          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1721          ## reconsume          ## reconsume
1722    
1723          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
1724    
1725          redo A;          redo A;
1726        } else {        } else {
1727          !!!cp (160);          !!!cp (160);
1728          $self->{current_token}          $self->{current_token}->{name} = chr $self->{next_char};
1729              = {type => DOCTYPE_TOKEN,          delete $self->{current_token}->{quirks};
                name => chr ($self->{next_char}),  
                #quirks => 0,  
               };  
1730  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1731          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
1732          !!!next-input-character;          !!!next-input-character;
# Line 2203  sub _get_next_token ($) { Line 2253  sub _get_next_token ($) {
2253  sub _tokenize_attempt_to_consume_an_entity ($$$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2254    my ($self, $in_attr, $additional) = @_;    my ($self, $in_attr, $additional) = @_;
2255    
2256      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2257    
2258    if ({    if ({
2259         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2260         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
# Line 2243  sub _tokenize_attempt_to_consume_an_enti Line 2295  sub _tokenize_attempt_to_consume_an_enti
2295            redo X;            redo X;
2296          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2297            !!!cp (1005);            !!!cp (1005);
2298            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2299            !!!back-next-input-character ($x_char, $self->{next_char});            !!!back-next-input-character ($x_char, $self->{next_char});
2300            $self->{next_char} = 0x0023; # #            $self->{next_char} = 0x0023; # #
2301            return undef;            return undef;
# Line 2252  sub _tokenize_attempt_to_consume_an_enti Line 2304  sub _tokenize_attempt_to_consume_an_enti
2304            !!!next-input-character;            !!!next-input-character;
2305          } else {          } else {
2306            !!!cp (1007);            !!!cp (1007);
2307            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc', line => $l, column => $c);
2308          }          }
2309    
2310          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2311            !!!cp (1008);            !!!cp (1008);
2312            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2313            $code = 0xFFFD;            $code = 0xFFFD;
2314          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2315            !!!cp (1009);            !!!cp (1009);
2316            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2317            $code = 0xFFFD;            $code = 0xFFFD;
2318          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2319            !!!cp (1010);            !!!cp (1010);
2320            !!!parse-error (type => 'CR character reference');            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2321            $code = 0x000A;            $code = 0x000A;
2322          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2323            !!!cp (1011);            !!!cp (1011);
2324            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2325            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2326          }          }
2327    
2328          return {type => CHARACTER_TOKEN, data => chr $code,          return {type => CHARACTER_TOKEN, data => chr $code,
2329                  has_reference => 1};                  has_reference => 1, line => $l, column => $c};
2330        } # X        } # X
2331      } elsif (0x0030 <= $self->{next_char} and      } elsif (0x0030 <= $self->{next_char} and
2332               $self->{next_char} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
# Line 2295  sub _tokenize_attempt_to_consume_an_enti Line 2347  sub _tokenize_attempt_to_consume_an_enti
2347          !!!next-input-character;          !!!next-input-character;
2348        } else {        } else {
2349          !!!cp (1014);          !!!cp (1014);
2350          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc', line => $l, column => $c);
2351        }        }
2352    
2353        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2354          !!!cp (1015);          !!!cp (1015);
2355          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2356          $code = 0xFFFD;          $code = 0xFFFD;
2357        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2358          !!!cp (1016);          !!!cp (1016);
2359          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2360          $code = 0xFFFD;          $code = 0xFFFD;
2361        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2362          !!!cp (1017);          !!!cp (1017);
2363          !!!parse-error (type => 'CR character reference');          !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2364          $code = 0x000A;          $code = 0x000A;
2365        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2366          !!!cp (1018);          !!!cp (1018);
2367          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2368          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2369        }        }
2370                
2371        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2372                  line => $l, column => $c};
2373      } else {      } else {
2374        !!!cp (1019);        !!!cp (1019);
2375        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2376        !!!back-next-input-character ($self->{next_char});        !!!back-next-input-character ($self->{next_char});
2377        $self->{next_char} = 0x0023; # #        $self->{next_char} = 0x0023; # #
2378        return undef;        return undef;
# Line 2369  sub _tokenize_attempt_to_consume_an_enti Line 2422  sub _tokenize_attempt_to_consume_an_enti
2422            
2423      if ($match > 0) {      if ($match > 0) {
2424        !!!cp (1023);        !!!cp (1023);
2425        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2426                  line => $l, column => $c};
2427      } elsif ($match < 0) {      } elsif ($match < 0) {
2428        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
2429        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
2430          !!!cp (1024);          !!!cp (1024);
2431          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
2432                    line => $l, column => $c};
2433        } else {        } else {
2434          !!!cp (1025);          !!!cp (1025);
2435          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2436                    line => $l, column => $c};
2437        }        }
2438      } else {      } else {
2439        !!!cp (1026);        !!!cp (1026);
2440        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero', line => $l, column => $c);
2441        ## NOTE: "No characters are consumed" in the spec.        ## NOTE: "No characters are consumed" in the spec.
2442        return {type => CHARACTER_TOKEN, data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value,
2443                  line => $l, column => $c};
2444      }      }
2445    } else {    } else {
2446      !!!cp (1027);      !!!cp (1027);
2447      ## no characters are consumed      ## no characters are consumed
2448      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
2449      return undef;      return undef;
2450    }    }
2451  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 2459  sub _tree_construction_initial ($) { Line 2516  sub _tree_construction_initial ($) {
2516            defined $token->{public_identifier} or            defined $token->{public_identifier} or
2517            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
2518          !!!cp ('t1');          !!!cp ('t1');
2519          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
2520        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
2521          !!!cp ('t2');          !!!cp ('t2');
2522          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
2523          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
2524        } else {        } else {
2525          !!!cp ('t3');          !!!cp ('t3');
2526        }        }
# Line 2602  sub _tree_construction_initial ($) { Line 2659  sub _tree_construction_initial ($) {
2659                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
2660               }->{$token->{type}}) {               }->{$token->{type}}) {
2661        !!!cp ('t14');        !!!cp ('t14');
2662        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
2663        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2664        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
2665        ## reprocess        ## reprocess
# Line 2623  sub _tree_construction_initial ($) { Line 2680  sub _tree_construction_initial ($) {
2680          !!!cp ('t17');          !!!cp ('t17');
2681        }        }
2682    
2683        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
2684        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2685        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
2686        ## reprocess        ## reprocess
# Line 2652  sub _tree_construction_root_element ($) Line 2709  sub _tree_construction_root_element ($)
2709    B: {    B: {
2710        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
2711          !!!cp ('t19');          !!!cp ('t19');
2712          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
2713          ## Ignore the token          ## Ignore the token
2714          ## Stay in the insertion mode.          ## Stay in the insertion mode.
2715          !!!next-token;          !!!next-token;
# Line 2743  sub _reset_insertion_mode ($) { Line 2800  sub _reset_insertion_mode ($) {
2800            
2801      ## Step 3      ## Step 3
2802      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"!?  
2803        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2804          $last = 1;          $last = 1;
2805          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
# Line 2908  sub _tree_construction_main ($) { Line 2960  sub _tree_construction_main ($) {
2960      !!!cp ('t39');      !!!cp ('t39');
2961    }; # $clear_up_to_marker    }; # $clear_up_to_marker
2962    
2963    my $parse_rcdata = sub ($$) {    my $insert;
2964      my ($content_model_flag, $insert) = @_;  
2965      my $parse_rcdata = sub ($) {
2966        my ($content_model_flag) = @_;
2967    
2968      ## Step 1      ## Step 1
2969      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
# Line 2917  sub _tree_construction_main ($) { Line 2971  sub _tree_construction_main ($) {
2971      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $start_tag_name, $token->{attributes});
2972    
2973      ## Step 2      ## Step 2
2974      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
2975    
2976      ## Step 3      ## Step 3
2977      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2947  sub _tree_construction_main ($) { Line 3001  sub _tree_construction_main ($) {
3001          $token->{tag_name} eq $start_tag_name) {          $token->{tag_name} eq $start_tag_name) {
3002        !!!cp ('t42');        !!!cp ('t42');
3003        ## 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});  
3004      } else {      } else {
3005        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3006          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3007            !!!cp ('t43');
3008            !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3009          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3010            !!!cp ('t44');
3011            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3012          } else {
3013            die "$0: $content_model_flag in parse_rcdata";
3014          }
3015      }      }
3016      !!!next-token;      !!!next-token;
3017    }; # $parse_rcdata    }; # $parse_rcdata
3018    
3019    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3020      my $script_el;      my $script_el;
3021      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes});
3022      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
# Line 2988  sub _tree_construction_main ($) { Line 3044  sub _tree_construction_main ($) {
3044        ## Ignore the token        ## Ignore the token
3045      } else {      } else {
3046        !!!cp ('t48');        !!!cp ('t48');
3047        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3048        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3049        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3050      }      }
# Line 3011  sub _tree_construction_main ($) { Line 3067  sub _tree_construction_main ($) {
3067      !!!next-token;      !!!next-token;
3068    }; # $script_start_tag    }; # $script_start_tag
3069    
3070      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3071      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3072      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3073    
3074    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3075      my $tag_name = shift;      my $end_tag_token = shift;
3076        my $tag_name = $end_tag_token->{tag_name};
3077    
3078        ## NOTE: The adoption agency algorithm (AAA).
3079    
3080      FET: {      FET: {
3081        ## Step 1        ## Step 1
# Line 3031  sub _tree_construction_main ($) { Line 3094  sub _tree_construction_main ($) {
3094        } # AFE        } # AFE
3095        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3096          !!!cp ('t53');          !!!cp ('t53');
3097          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3098          ## Ignore the token          ## Ignore the token
3099          !!!next-token;          !!!next-token;
3100          return;          return;
# Line 3048  sub _tree_construction_main ($) { Line 3111  sub _tree_construction_main ($) {
3111              last INSCOPE;              last INSCOPE;
3112            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3113              !!!cp ('t55');              !!!cp ('t55');
3114              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3115                                token => $end_tag_token);
3116              ## Ignore the token              ## Ignore the token
3117              !!!next-token;              !!!next-token;
3118              return;              return;
3119            }            }
3120          } elsif ({          } elsif ({
3121                    table => 1, caption => 1, td => 1, th => 1,                    applet => 1, table => 1, caption => 1, td => 1, th => 1,
3122                    button => 1, marquee => 1, object => 1, html => 1,                    button => 1, marquee => 1, object => 1, html => 1,
3123                   }->{$node->[1]}) {                   }->{$node->[1]}) {
3124            !!!cp ('t56');            !!!cp ('t56');
# Line 3063  sub _tree_construction_main ($) { Line 3127  sub _tree_construction_main ($) {
3127        } # INSCOPE        } # INSCOPE
3128        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3129          !!!cp ('t57');          !!!cp ('t57');
3130          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3131                            token => $end_tag_token);
3132          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3133          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3134          return;          return;
3135        }        }
3136        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3137          !!!cp ('t58');          !!!cp ('t58');
3138          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1],
3139                            token => $end_tag_token);
3140        }        }
3141                
3142        ## Step 2        ## Step 2
# Line 3081  sub _tree_construction_main ($) { Line 3147  sub _tree_construction_main ($) {
3147          if (not $formatting_category->{$node->[1]} and          if (not $formatting_category->{$node->[1]} and
3148              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3149              ($special_category->{$node->[1]} or              ($special_category->{$node->[1]} or
3150               $scoping_category->{$node->[1]})) {               $scoping_category->{$node->[1]})) { ## Scoping is redundant, maybe
3151            !!!cp ('t59');            !!!cp ('t59');
3152            $furthest_block = $node;            $furthest_block = $node;
3153            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3167  sub _tree_construction_main ($) { Line 3233  sub _tree_construction_main ($) {
3233        } # S7          } # S7  
3234                
3235        ## Step 8        ## Step 8
3236        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ({
3237               table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,
3238              }->{$common_ancestor_node->[1]}) {
3239            my $foster_parent_element;
3240            my $next_sibling;
3241                             OE: for (reverse 0..$#{$self->{open_elements}}) {
3242                               if ($self->{open_elements}->[$_]->[1] eq 'table') {
3243                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3244                                 if (defined $parent and $parent->node_type == 1) {
3245                                   !!!cp ('t65.1');
3246                                   $foster_parent_element = $parent;
3247                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3248                                 } else {
3249                                   !!!cp ('t65.2');
3250                                   $foster_parent_element
3251                                     = $self->{open_elements}->[$_ - 1]->[0];
3252                                 }
3253                                 last OE;
3254                               }
3255                             } # OE
3256                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3257                               unless defined $foster_parent_element;
3258            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3259            $open_tables->[-1]->[1] = 1; # tainted
3260          } else {
3261            !!!cp ('t65.3');
3262            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3263          }
3264                
3265        ## Step 9        ## Step 9
3266        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 3213  sub _tree_construction_main ($) { Line 3306  sub _tree_construction_main ($) {
3306      } # FET      } # FET
3307    }; # $formatting_end_tag    }; # $formatting_end_tag
3308    
3309    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3310      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3311    }; # $insert_to_current    }; # $insert_to_current
3312    
3313    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3314                         my $child = shift;      my $child = shift;
3315                         if ({      if ({
3316                              table => 1, tbody => 1, tfoot => 1,           table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,
3317                              thead => 1, tr => 1,          }->{$self->{open_elements}->[-1]->[1]}) {
3318                             }->{$self->{open_elements}->[-1]->[1]}) {        # MUST
3319                           # MUST        my $foster_parent_element;
3320                           my $foster_parent_element;        my $next_sibling;
                          my $next_sibling;  
3321                           OE: for (reverse 0..$#{$self->{open_elements}}) {                           OE: for (reverse 0..$#{$self->{open_elements}}) {
3322                             if ($self->{open_elements}->[$_]->[1] eq 'table') {                             if ($self->{open_elements}->[$_]->[1] eq 'table') {
3323                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
# Line 3245  sub _tree_construction_main ($) { Line 3337  sub _tree_construction_main ($) {
3337                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3338                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3339                             ($child, $next_sibling);                             ($child, $next_sibling);
3340                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3341                           !!!cp ('t72');      } else {
3342                           $self->{open_elements}->[-1]->[0]->append_child ($child);        !!!cp ('t72');
3343                         }        $self->{open_elements}->[-1]->[0]->append_child ($child);
3344        }
3345    }; # $insert_to_foster    }; # $insert_to_foster
3346    
   my $insert;  
   
3347    B: {    B: {
3348      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3349        !!!cp ('t73');        !!!cp ('t73');
3350        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3351        ## Ignore the token        ## Ignore the token
3352        ## Stay in the phase        ## Stay in the phase
3353        !!!next-token;        !!!next-token;
3354        redo B;        redo B;
     } elsif ($token->{type} == END_OF_FILE_TOKEN) {  
       if ($self->{insertion_mode} & AFTER_HTML_IMS) {  
         !!!cp ('t74');  
         #  
       } else {  
         ## Generate implied end tags  
         while ({  
                 dd => 1, dt => 1, li => 1, p => 1,  
                }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!cp ('t75');  
           pop @{$self->{open_elements}};  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!cp ('t76');  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
 ## ISSUE: This case is never reached.  
           !!!cp ('t77');  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } else {  
           !!!cp ('t78');  
         }  
   
         ## ISSUE: There is an issue in the spec.  
       }  
   
       ## Stop parsing  
       last B;  
3355      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3356               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3357        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3358          !!!cp ('t79');          !!!cp ('t79');
3359          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3360          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
3361        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3362          !!!cp ('t80');          !!!cp ('t80');
3363          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3364          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3365        } else {        } else {
3366          !!!cp ('t81');          !!!cp ('t81');
3367        }        }
3368    
3369        !!!cp ('t82');        !!!cp ('t82');
3370        !!!parse-error (type => 'not first start tag');        !!!parse-error (type => 'not first start tag', token => $token);
3371        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
3372        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
3373          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
# Line 3337  sub _tree_construction_main ($) { Line 3396  sub _tree_construction_main ($) {
3396      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & HEAD_IMS) {
3397        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
3398          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3399            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3400                !!!cp ('t88.2');
3401                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3402              } else {
3403                !!!cp ('t88.1');
3404                ## Ignore the token.
3405                !!!next-token;
3406                redo B;
3407              }
3408            unless (length $token->{data}) {            unless (length $token->{data}) {
3409              !!!cp ('t88');              !!!cp ('t88');
3410              !!!next-token;              !!!next-token;
# Line 3360  sub _tree_construction_main ($) { Line 3427  sub _tree_construction_main ($) {
3427            !!!cp ('t90');            !!!cp ('t90');
3428            ## As if </noscript>            ## As if </noscript>
3429            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
3430            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character', token => $token);
3431                        
3432            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
3433            ## As if </head>            ## As if </head>
# Line 3397  sub _tree_construction_main ($) { Line 3464  sub _tree_construction_main ($) {
3464                  #                  #
3465                } else {                } else {
3466                  !!!cp ('t95');                  !!!cp ('t95');
3467                  !!!parse-error (type => 'in head:head'); # or in head noscript                  !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
3468                  ## Ignore the token                  ## Ignore the token
3469                  !!!next-token;                  !!!next-token;
3470                  redo B;                  redo B;
# Line 3420  sub _tree_construction_main ($) { Line 3487  sub _tree_construction_main ($) {
3487                  !!!cp ('t98');                  !!!cp ('t98');
3488                  ## As if </noscript>                  ## As if </noscript>
3489                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3490                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base', token => $token);
3491                                
3492                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3493                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3431  sub _tree_construction_main ($) { Line 3498  sub _tree_construction_main ($) {
3498                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3499                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3500                  !!!cp ('t100');                  !!!cp ('t100');
3501                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3502                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3503                } else {                } else {
3504                  !!!cp ('t101');                  !!!cp ('t101');
3505                }                }
3506                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3507                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3508                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3509                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3510                !!!next-token;                !!!next-token;
3511                redo B;                redo B;
# Line 3446  sub _tree_construction_main ($) { Line 3513  sub _tree_construction_main ($) {
3513                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3514                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3515                  !!!cp ('t102');                  !!!cp ('t102');
3516                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3517                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3518                } else {                } else {
3519                  !!!cp ('t103');                  !!!cp ('t103');
3520                }                }
3521                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3522                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3523                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3524                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3525                !!!next-token;                !!!next-token;
3526                redo B;                redo B;
# Line 3461  sub _tree_construction_main ($) { Line 3528  sub _tree_construction_main ($) {
3528                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3529                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3530                  !!!cp ('t104');                  !!!cp ('t104');
3531                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3532                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3533                } else {                } else {
3534                  !!!cp ('t105');                  !!!cp ('t105');
# Line 3473  sub _tree_construction_main ($) { Line 3540  sub _tree_construction_main ($) {
3540                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
3541                    !!!cp ('t106');                    !!!cp ('t106');
3542                    $self->{change_encoding}                    $self->{change_encoding}
3543                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
3544                             $token);
3545                                        
3546                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3547                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
# Line 3488  sub _tree_construction_main ($) { Line 3556  sub _tree_construction_main ($) {
3556                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
3557                      !!!cp ('t107');                      !!!cp ('t107');
3558                      $self->{change_encoding}                      $self->{change_encoding}
3559                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
3560                               $token);
3561                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3562                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
3563                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
# Line 3514  sub _tree_construction_main ($) { Line 3583  sub _tree_construction_main ($) {
3583                  }                  }
3584                }                }
3585    
3586                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3587                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3588                !!!next-token;                !!!next-token;
3589                redo B;                redo B;
# Line 3523  sub _tree_construction_main ($) { Line 3592  sub _tree_construction_main ($) {
3592                  !!!cp ('t111');                  !!!cp ('t111');
3593                  ## As if </noscript>                  ## As if </noscript>
3594                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3595                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title', token => $token);
3596                                
3597                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3598                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3599                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3600                  !!!cp ('t112');                  !!!cp ('t112');
3601                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3602                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3603                } else {                } else {
3604                  !!!cp ('t113');                  !!!cp ('t113');
# Line 3538  sub _tree_construction_main ($) { Line 3607  sub _tree_construction_main ($) {
3607                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3608                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
3609                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
3610                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
3611                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
3612                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3613                redo B;                redo B;
3614              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
# Line 3549  sub _tree_construction_main ($) { Line 3617  sub _tree_construction_main ($) {
3617                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3618                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3619                  !!!cp ('t114');                  !!!cp ('t114');
3620                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3621                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3622                } else {                } else {
3623                  !!!cp ('t115');                  !!!cp ('t115');
3624                }                }
3625                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
3626                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3627                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3628                redo B;                redo B;
3629              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
# Line 3568  sub _tree_construction_main ($) { Line 3636  sub _tree_construction_main ($) {
3636                  redo B;                  redo B;
3637                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3638                  !!!cp ('t117');                  !!!cp ('t117');
3639                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript:noscript', token => $token);
3640                  ## Ignore the token                  ## Ignore the token
3641                  !!!next-token;                  !!!next-token;
3642                  redo B;                  redo B;
# Line 3581  sub _tree_construction_main ($) { Line 3649  sub _tree_construction_main ($) {
3649                  !!!cp ('t119');                  !!!cp ('t119');
3650                  ## As if </noscript>                  ## As if </noscript>
3651                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3652                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script', token => $token);
3653                                
3654                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3655                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3656                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3657                  !!!cp ('t120');                  !!!cp ('t120');
3658                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3659                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3660                } else {                } else {
3661                  !!!cp ('t121');                  !!!cp ('t121');
3662                }                }
3663    
3664                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3665                $script_start_tag->($insert_to_current);                $script_start_tag->();
3666                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3667                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3668                redo B;                redo B;
3669              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
# Line 3604  sub _tree_construction_main ($) { Line 3672  sub _tree_construction_main ($) {
3672                  !!!cp ('t122');                  !!!cp ('t122');
3673                  ## As if </noscript>                  ## As if </noscript>
3674                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3675                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
3676                                    
3677                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3678                  ## As if </head>                  ## As if </head>
# Line 3642  sub _tree_construction_main ($) { Line 3710  sub _tree_construction_main ($) {
3710                !!!cp ('t129');                !!!cp ('t129');
3711                ## As if </noscript>                ## As if </noscript>
3712                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3713                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
3714                                
3715                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
3716                ## As if </head>                ## As if </head>
# Line 3683  sub _tree_construction_main ($) { Line 3751  sub _tree_construction_main ($) {
3751                  !!!cp ('t133');                  !!!cp ('t133');
3752                  ## As if </noscript>                  ## As if </noscript>
3753                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3754                  !!!parse-error (type => 'in noscript:/head');                  !!!parse-error (type => 'in noscript:/head', token => $token);
3755                                    
3756                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3757                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 3709  sub _tree_construction_main ($) { Line 3777  sub _tree_construction_main ($) {
3777                  redo B;                  redo B;
3778                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3779                  !!!cp ('t137');                  !!!cp ('t137');
3780                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
3781                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
3782                  !!!next-token;                  !!!next-token;
3783                  redo B;                  redo B;
# Line 3731  sub _tree_construction_main ($) { Line 3799  sub _tree_construction_main ($) {
3799                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3800                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3801                  !!!cp ('t140');                  !!!cp ('t140');
3802                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
3803                  ## Ignore the token                  ## Ignore the token
3804                  !!!next-token;                  !!!next-token;
3805                  redo B;                  redo B;
# Line 3763  sub _tree_construction_main ($) { Line 3831  sub _tree_construction_main ($) {
3831                  #                  #
3832                } else {                } else {
3833                  !!!cp ('t145');                  !!!cp ('t145');
3834                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
3835                  ## Ignore the token                  ## Ignore the token
3836                  !!!next-token;                  !!!next-token;
3837                  redo B;                  redo B;
# Line 3774  sub _tree_construction_main ($) { Line 3842  sub _tree_construction_main ($) {
3842                !!!cp ('t146');                !!!cp ('t146');
3843                ## As if </noscript>                ## As if </noscript>
3844                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3845                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
3846                                
3847                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
3848                ## As if </head>                ## As if </head>
# Line 3790  sub _tree_construction_main ($) { Line 3858  sub _tree_construction_main ($) {
3858              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3859  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
3860                !!!cp ('t148');                !!!cp ('t148');
3861                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
3862                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
3863                !!!next-token;                !!!next-token;
3864                redo B;                redo B;
# Line 3804  sub _tree_construction_main ($) { Line 3872  sub _tree_construction_main ($) {
3872              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
3873              ## reprocess              ## reprocess
3874              redo B;              redo B;
3875            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
3876              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3877            }            !!!cp ('t149.1');
3878    
3879              ## NOTE: As if <head>
3880              !!!create-element ($self->{head_element}, 'head');
3881              $self->{open_elements}->[-1]->[0]->append_child
3882                  ($self->{head_element});
3883              #push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3884              #$self->{insertion_mode} = IN_HEAD_IM;
3885              ## NOTE: Reprocess.
3886    
3887              ## NOTE: As if </head>
3888              #pop @{$self->{open_elements}};
3889              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
3890              ## NOTE: Reprocess.
3891              
3892              #
3893            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3894              !!!cp ('t149.2');
3895    
3896              ## NOTE: As if </head>
3897              pop @{$self->{open_elements}};
3898              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
3899              ## NOTE: Reprocess.
3900    
3901              #
3902            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3903              !!!cp ('t149.3');
3904    
3905              !!!parse-error (type => 'in noscript:#eof', token => $token);
3906    
3907              ## As if </noscript>
3908              pop @{$self->{open_elements}};
3909              #$self->{insertion_mode} = IN_HEAD_IM;
3910              ## NOTE: Reprocess.
3911    
3912              ## NOTE: As if </head>
3913              pop @{$self->{open_elements}};
3914              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
3915              ## NOTE: Reprocess.
3916    
3917              #
3918            } else {
3919              !!!cp ('t149.4');
3920              #
3921            }
3922    
3923            ## NOTE: As if <body>
3924            !!!insert-element ('body');
3925            $self->{insertion_mode} = IN_BODY_IM;
3926            ## NOTE: Reprocess.
3927            redo B;
3928          } else {
3929            die "$0: $token->{type}: Unknown token type";
3930          }
3931    
3932            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
3933      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
# Line 3826  sub _tree_construction_main ($) { Line 3947  sub _tree_construction_main ($) {
3947                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3948                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
3949                  ## have an element in table scope                  ## have an element in table scope
3950                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
3951                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
3952                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3953                      !!!cp ('t151');                      !!!cp ('t151');
3954                      $tn = $node->[1];  
3955                      last INSCOPE;                      ## Close the cell
3956                        !!!back-token; # <?>
3957                        $token = {type => END_TAG_TOKEN, tag_name => $node->[1],
3958                                  line => $token->{line},
3959                                  column => $token->{column}};
3960                        redo B;
3961                    } elsif ({                    } elsif ({
3962                              table => 1, html => 1,                              table => 1, html => 1,
3963                             }->{$node->[1]}) {                             }->{$node->[1]}) {
3964                      !!!cp ('t152');                      !!!cp ('t152');
3965                      last INSCOPE;                      ## ISSUE: This case can never be reached, maybe.
3966                    }                      last;
                 } # INSCOPE  
                   unless (defined $tn) {  
                     !!!cp ('t153');  
 ## TODO: This error type is wrong.  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
3967                    }                    }
3968                                    }
3969                  !!!cp ('t154');  
3970                  ## Close the cell                  !!!cp ('t153');
3971                  !!!back-token; # <?>                  !!!parse-error (type => 'start tag not allowed',
3972                  $token = {type => END_TAG_TOKEN, tag_name => $tn};                      value => $token->{tag_name}, token => $token);
3973                    ## Ignore the token
3974                    !!!next-token;
3975                  redo B;                  redo B;
3976                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3977                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption', token => $token);
3978                                    
3979                  ## As if </caption>                  ## NOTE: As if </caption>.
3980                  ## have a table element in table scope                  ## have a table element in table scope
3981                  my $i;                  my $i;
3982                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
3983                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
3984                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
3985                      !!!cp ('t155');                      if ($node->[1] eq 'caption') {
3986                      $i = $_;                        !!!cp ('t155');
3987                      last INSCOPE;                        $i = $_;
3988                    } elsif ({                        last INSCOPE;
3989                              table => 1, html => 1,                      } elsif ({
3990                             }->{$node->[1]}) {                                table => 1, html => 1,
3991                      !!!cp ('t156');                               }->{$node->[1]}) {
3992                      last INSCOPE;                        !!!cp ('t156');
3993                          last;
3994                        }
3995                    }                    }
3996    
3997                      !!!cp ('t157');
3998                      !!!parse-error (type => 'start tag not allowed',
3999                                      value => $token->{tag_name}, token => $token);
4000                      ## Ignore the token
4001                      !!!next-token;
4002                      redo B;
4003                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t157');  
 ## TODO: this type is wrong.  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4004                                    
4005                  ## generate implied end tags                  ## generate implied end tags
4006                  while ({                  while ({
# Line 3892  sub _tree_construction_main ($) { Line 4012  sub _tree_construction_main ($) {
4012    
4013                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4014                    !!!cp ('t159');                    !!!cp ('t159');
4015                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
4016                  } else {                  } else {
4017                    !!!cp ('t160');                    !!!cp ('t160');
4018                  }                  }
# Line 3933  sub _tree_construction_main ($) { Line 4053  sub _tree_construction_main ($) {
4053                  } # INSCOPE                  } # INSCOPE
4054                    unless (defined $i) {                    unless (defined $i) {
4055                      !!!cp ('t165');                      !!!cp ('t165');
4056                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4057                      ## Ignore the token                      ## Ignore the token
4058                      !!!next-token;                      !!!next-token;
4059                      redo B;                      redo B;
# Line 3949  sub _tree_construction_main ($) { Line 4069  sub _tree_construction_main ($) {
4069    
4070                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
4071                    !!!cp ('t167');                    !!!cp ('t167');
4072                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
4073                  } else {                  } else {
4074                    !!!cp ('t168');                    !!!cp ('t168');
4075                  }                  }
# Line 3964  sub _tree_construction_main ($) { Line 4084  sub _tree_construction_main ($) {
4084                  redo B;                  redo B;
4085                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4086                  !!!cp ('t169');                  !!!cp ('t169');
4087                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4088                  ## Ignore the token                  ## Ignore the token
4089                  !!!next-token;                  !!!next-token;
4090                  redo B;                  redo B;
# Line 3976  sub _tree_construction_main ($) { Line 4096  sub _tree_construction_main ($) {
4096                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
4097                  ## have a table element in table scope                  ## have a table element in table scope
4098                  my $i;                  my $i;
4099                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4100                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4101                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
4102                      !!!cp ('t171');                      if ($node->[1] eq $token->{tag_name}) {
4103                      $i = $_;                        !!!cp ('t171');
4104                      last INSCOPE;                        $i = $_;
4105                    } elsif ({                        last INSCOPE;
4106                              table => 1, html => 1,                      } elsif ({
4107                             }->{$node->[1]}) {                                table => 1, html => 1,
4108                      !!!cp ('t172');                               }->{$node->[1]}) {
4109                      last INSCOPE;                        !!!cp ('t172');
4110                          last;
4111                        }
4112                    }                    }
4113    
4114                      !!!cp ('t173');
4115                      !!!parse-error (type => 'unmatched end tag',
4116                                      value => $token->{tag_name}, token => $token);
4117                      ## Ignore the token
4118                      !!!next-token;
4119                      redo B;
4120                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t173');  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4121                                    
4122                  ## generate implied end tags                  ## generate implied end tags
4123                  while ({                  while ({
# Line 4007  sub _tree_construction_main ($) { Line 4129  sub _tree_construction_main ($) {
4129                                    
4130                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4131                    !!!cp ('t175');                    !!!cp ('t175');
4132                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
4133                  } else {                  } else {
4134                    !!!cp ('t176');                    !!!cp ('t176');
4135                  }                  }
# Line 4022  sub _tree_construction_main ($) { Line 4144  sub _tree_construction_main ($) {
4144                  redo B;                  redo B;
4145                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4146                  !!!cp ('t177');                  !!!cp ('t177');
4147                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4148                  ## Ignore the token                  ## Ignore the token
4149                  !!!next-token;                  !!!next-token;
4150                  redo B;                  redo B;
# Line 4038  sub _tree_construction_main ($) { Line 4160  sub _tree_construction_main ($) {
4160                ## have an element in table scope                ## have an element in table scope
4161                my $i;                my $i;
4162                my $tn;                my $tn;
4163                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
4164                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
4165                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4166                    !!!cp ('t179');                    if ($node->[1] eq $token->{tag_name}) {
4167                    $i = $_;                      !!!cp ('t179');
4168                    last INSCOPE;                      $i = $_;
4169                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
4170                    !!!cp ('t180');                      ## Close the cell
4171                    $tn = $node->[1];                      !!!back-token; # </?>
4172                    ## NOTE: There is exactly one |td| or |th| element                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4173                    ## in scope in the stack of open elements by definition.                                line => $token->{line},
4174                  } elsif ({                                column => $token->{column}};
4175                            table => 1, html => 1,                      redo B;
4176                           }->{$node->[1]}) {                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
4177                    !!!cp ('t181');                      !!!cp ('t180');
4178                    last INSCOPE;                      $tn = $node->[1];
4179                        ## NOTE: There is exactly one |td| or |th| element
4180                        ## in scope in the stack of open elements by definition.
4181                      } elsif ({
4182                                table => 1, html => 1,
4183                               }->{$node->[1]}) {
4184                        ## ISSUE: Can this be reached?
4185                        !!!cp ('t181');
4186                        last;
4187                      }
4188                  }                  }
4189                } # INSCOPE  
               unless (defined $i) {  
4190                  !!!cp ('t182');                  !!!cp ('t182');
4191                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4192                        value => $token->{tag_name}, token => $token);
4193                  ## Ignore the token                  ## Ignore the token
4194                  !!!next-token;                  !!!next-token;
4195                  redo B;                  redo B;
4196                } else {                } # INSCOPE
                 !!!cp ('t183');  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
4197              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
4198                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4199                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4200    
4201                ## As if </caption>                ## As if </caption>
4202                ## have a table element in table scope                ## have a table element in table scope
# Line 4092  sub _tree_construction_main ($) { Line 4216  sub _tree_construction_main ($) {
4216                } # INSCOPE                } # INSCOPE
4217                unless (defined $i) {                unless (defined $i) {
4218                  !!!cp ('t186');                  !!!cp ('t186');
4219                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);
4220                  ## Ignore the token                  ## Ignore the token
4221                  !!!next-token;                  !!!next-token;
4222                  redo B;                  redo B;
# Line 4108  sub _tree_construction_main ($) { Line 4232  sub _tree_construction_main ($) {
4232    
4233                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4234                  !!!cp ('t188');                  !!!cp ('t188');
4235                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
4236                } else {                } else {
4237                  !!!cp ('t189');                  !!!cp ('t189');
4238                }                }
# Line 4126  sub _tree_construction_main ($) { Line 4250  sub _tree_construction_main ($) {
4250                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4251                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4252                  !!!cp ('t190');                  !!!cp ('t190');
4253                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4254                  ## Ignore the token                  ## Ignore the token
4255                  !!!next-token;                  !!!next-token;
4256                  redo B;                  redo B;
# Line 4140  sub _tree_construction_main ($) { Line 4264  sub _tree_construction_main ($) {
4264                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
4265                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4266                !!!cp ('t192');                !!!cp ('t192');
4267                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4268                ## Ignore the token                ## Ignore the token
4269                !!!next-token;                !!!next-token;
4270                redo B;                redo B;
# Line 4148  sub _tree_construction_main ($) { Line 4272  sub _tree_construction_main ($) {
4272                !!!cp ('t193');                !!!cp ('t193');
4273                #                #
4274              }              }
4275          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4276            for my $entry (@{$self->{open_elements}}) {
4277              if (not {
4278                dd => 1, dt => 1, li => 1, p => 1, tbody => 1, td => 1, tfoot => 1,
4279                th => 1, thead => 1, tr => 1, body => 1, html => 1,
4280              }->{$entry->[1]}) {
4281                !!!cp ('t75');
4282                !!!parse-error (type => 'in body:#eof', token => $token);
4283                last;
4284              }
4285            }
4286    
4287            ## Stop parsing.
4288            last B;
4289        } else {        } else {
4290          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4291        }        }
# Line 4156  sub _tree_construction_main ($) { Line 4294  sub _tree_construction_main ($) {
4294        #        #
4295      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
4296        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4297              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
4298                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4299              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4300                                
4301                unless (length $token->{data}) {            unless (length $token->{data}) {
4302                  !!!cp ('t194');              !!!cp ('t194');
4303                  !!!next-token;              !!!next-token;
4304                  redo B;              redo B;
4305                } else {            } else {
4306                  !!!cp ('t195');              !!!cp ('t195');
4307                }            }
4308              }          }
4309    
4310              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
4311    
4312              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
4313              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 4213  sub _tree_construction_main ($) { Line 4352  sub _tree_construction_main ($) {
4352                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
4353                     $next_sibling);                     $next_sibling);
4354                }                }
4355              } else {            $open_tables->[-1]->[1] = 1; # tainted
4356                !!!cp ('t200');          } else {
4357                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            !!!cp ('t200');
4358              }            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4359            }
4360                            
4361              !!!next-token;          !!!next-token;
4362              redo B;          redo B;
4363        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4364              if ({              if ({
4365                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
# Line 4230  sub _tree_construction_main ($) { Line 4370  sub _tree_construction_main ($) {
4370                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
4371                         $self->{open_elements}->[-1]->[1] ne 'html') {                         $self->{open_elements}->[-1]->[1] ne 'html') {
4372                    !!!cp ('t201');                    !!!cp ('t201');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4373                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4374                  }                  }
4375                                    
# Line 4242  sub _tree_construction_main ($) { Line 4381  sub _tree_construction_main ($) {
4381                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4382                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
4383                    !!!cp ('t202');                    !!!cp ('t202');
4384                    !!!parse-error (type => 'missing start tag:tr');                    !!!parse-error (type => 'missing start tag:tr', token => $token);
4385                  }                  }
4386                                    
4387                  ## Clear back to table body context                  ## Clear back to table body context
# Line 4251  sub _tree_construction_main ($) { Line 4390  sub _tree_construction_main ($) {
4390                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4391                    !!!cp ('t203');                    !!!cp ('t203');
4392                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4393                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4394                  }                  }
4395                                    
# Line 4275  sub _tree_construction_main ($) { Line 4413  sub _tree_construction_main ($) {
4413                  tr => 1, html => 1,                  tr => 1, html => 1,
4414                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4415                  !!!cp ('t207');                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4416                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4417                }                }
4418                                
# Line 4314  sub _tree_construction_main ($) { Line 4451  sub _tree_construction_main ($) {
4451                  unless (defined $i) {                  unless (defined $i) {
4452                   !!!cp ('t210');                   !!!cp ('t210');
4453  ## TODO: This type is wrong.  ## TODO: This type is wrong.
4454                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
4455                    ## Ignore the token                    ## Ignore the token
4456                    !!!next-token;                    !!!next-token;
4457                    redo B;                    redo B;
# Line 4326  sub _tree_construction_main ($) { Line 4463  sub _tree_construction_main ($) {
4463                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4464                    !!!cp ('t211');                    !!!cp ('t211');
4465                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4466                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4467                  }                  }
4468                                    
# Line 4363  sub _tree_construction_main ($) { Line 4499  sub _tree_construction_main ($) {
4499                  unless (defined $i) {                  unless (defined $i) {
4500                    !!!cp ('t216');                    !!!cp ('t216');
4501  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type ios wrong.
4502                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4503                    ## Ignore the token                    ## Ignore the token
4504                    !!!next-token;                    !!!next-token;
4505                    redo B;                    redo B;
# Line 4375  sub _tree_construction_main ($) { Line 4511  sub _tree_construction_main ($) {
4511                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4512                    !!!cp ('t217');                    !!!cp ('t217');
4513                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4514                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4515                  }                  }
4516                                    
# Line 4399  sub _tree_construction_main ($) { Line 4534  sub _tree_construction_main ($) {
4534                         $self->{open_elements}->[-1]->[1] ne 'html') {                         $self->{open_elements}->[-1]->[1] ne 'html') {
4535                    !!!cp ('t219');                    !!!cp ('t219');
4536                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4537                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4538                  }                  }
4539                                    
# Line 4417  sub _tree_construction_main ($) { Line 4551  sub _tree_construction_main ($) {
4551                         $self->{open_elements}->[-1]->[1] ne 'html') {                         $self->{open_elements}->[-1]->[1] ne 'html') {
4552                    !!!cp ('t220');                    !!!cp ('t220');
4553                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4554                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4555                  }                  }
4556                                    
# Line 4438  sub _tree_construction_main ($) { Line 4571  sub _tree_construction_main ($) {
4571                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
4572                }                }
4573              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4574                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
4575    
4576                ## As if </table>                ## As if </table>
4577                ## have a table element in table scope                ## have a table element in table scope
# Line 4460  sub _tree_construction_main ($) { Line 4593  sub _tree_construction_main ($) {
4593                unless (defined $i) {                unless (defined $i) {
4594                  !!!cp ('t223');                  !!!cp ('t223');
4595  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
4596                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:table', token => $token);
4597                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
4598                  !!!next-token;                  !!!next-token;
4599                  redo B;                  redo B;
4600                }                }
4601                                
4602    ## TODO: Followings are removed from the latest spec.
4603                ## generate implied end tags                ## generate implied end tags
4604                while ({                while ({
4605                        dd => 1, dt => 1, li => 1, p => 1,                        dd => 1, dt => 1, li => 1, p => 1,
# Line 4477  sub _tree_construction_main ($) { Line 4611  sub _tree_construction_main ($) {
4611                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
4612                  !!!cp ('t225');                  !!!cp ('t225');
4613  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
4614                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
4615                } else {                } else {
4616                  !!!cp ('t226');                  !!!cp ('t226');
4617                }                }
4618    
4619                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4620                  pop @{$open_tables};
4621    
4622                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4623    
4624                ## reprocess                ## reprocess
4625                redo B;                redo B;
4626            } elsif ($token->{tag_name} eq 'style') {
4627              if (not $open_tables->[-1]->[1]) { # tainted
4628                !!!cp ('t227.8');
4629                ## NOTE: This is a "as if in head" code clone.
4630                $parse_rcdata->(CDATA_CONTENT_MODEL);
4631                redo B;
4632              } else {
4633                !!!cp ('t227.7');
4634                #
4635              }
4636            } elsif ($token->{tag_name} eq 'script') {
4637              if (not $open_tables->[-1]->[1]) { # tainted
4638                !!!cp ('t227.6');
4639                ## NOTE: This is a "as if in head" code clone.
4640                $script_start_tag->();
4641                redo B;
4642              } else {
4643                !!!cp ('t227.5');
4644                #
4645              }
4646            } elsif ($token->{tag_name} eq 'input') {
4647              if (not $open_tables->[-1]->[1]) { # tainted
4648                if ($token->{attributes}->{type}) { ## TODO: case
4649                  my $type = lc $token->{attributes}->{type}->{value};
4650                  if ($type eq 'hidden') {
4651                    !!!cp ('t227.3');
4652                    !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
4653    
4654                    !!!insert-element ($token->{tag_name}, $token->{attributes});
4655    
4656                    ## TODO: form element pointer
4657    
4658                    pop @{$self->{open_elements}};
4659    
4660                    !!!next-token;
4661                    redo B;
4662                  } else {
4663                    !!!cp ('t227.2');
4664                    #
4665                  }
4666                } else {
4667                  !!!cp ('t227.1');
4668                  #
4669                }
4670              } else {
4671                !!!cp ('t227.4');
4672                #
4673              }
4674          } else {          } else {
4675            !!!cp ('t227');            !!!cp ('t227');
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
   
           $insert = $insert_to_foster;  
4676            #            #
4677          }          }
4678    
4679            !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
4680    
4681            $insert = $insert_to_foster;
4682            #
4683        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
4684              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
4685                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 4515  sub _tree_construction_main ($) { Line 4700  sub _tree_construction_main ($) {
4700                } # INSCOPE                } # INSCOPE
4701                unless (defined $i) {                unless (defined $i) {
4702                  !!!cp ('t230');                  !!!cp ('t230');
4703                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4704                  ## Ignore the token                  ## Ignore the token
4705                  !!!next-token;                  !!!next-token;
4706                  redo B;                  redo B;
# Line 4529  sub _tree_construction_main ($) { Line 4714  sub _tree_construction_main ($) {
4714                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4715                  !!!cp ('t231');                  !!!cp ('t231');
4716  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4717                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4718                }                }
4719    
# Line 4558  sub _tree_construction_main ($) { Line 4742  sub _tree_construction_main ($) {
4742                  unless (defined $i) {                  unless (defined $i) {
4743                    !!!cp ('t235');                    !!!cp ('t235');
4744  ## TODO: The following is wrong.  ## TODO: The following is wrong.
4745                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
4746                    ## Ignore the token                    ## Ignore the token
4747                    !!!next-token;                    !!!next-token;
4748                    redo B;                    redo B;
# Line 4570  sub _tree_construction_main ($) { Line 4754  sub _tree_construction_main ($) {
4754                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4755                    !!!cp ('t236');                    !!!cp ('t236');
4756  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4757                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4758                  }                  }
4759                                    
# Line 4599  sub _tree_construction_main ($) { Line 4782  sub _tree_construction_main ($) {
4782                  } # INSCOPE                  } # INSCOPE
4783                  unless (defined $i) {                  unless (defined $i) {
4784                    !!!cp ('t239');                    !!!cp ('t239');
4785                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4786                    ## Ignore the token                    ## Ignore the token
4787                    !!!next-token;                    !!!next-token;
4788                    redo B;                    redo B;
# Line 4610  sub _tree_construction_main ($) { Line 4793  sub _tree_construction_main ($) {
4793                    tbody => 1, tfoot => 1, thead => 1, html => 1,                    tbody => 1, tfoot => 1, thead => 1, html => 1,
4794                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4795                    !!!cp ('t240');                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4796                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4797                  }                  }
4798                                    
# Line 4626  sub _tree_construction_main ($) { Line 4808  sub _tree_construction_main ($) {
4808                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
4809                }                }
4810    
4811                  ## NOTE: </table> in the "in table" insertion mode.
4812                  ## When you edit the code fragment below, please ensure that
4813                  ## the code for <table> in the "in table" insertion mode
4814                  ## is synced with it.
4815    
4816                ## have a table element in table scope                ## have a table element in table scope
4817                my $i;                my $i;
4818                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 4643  sub _tree_construction_main ($) { Line 4830  sub _tree_construction_main ($) {
4830                } # INSCOPE                } # INSCOPE
4831                unless (defined $i) {                unless (defined $i) {
4832                  !!!cp ('t243');                  !!!cp ('t243');
4833                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4834                  ## Ignore the token                  ## Ignore the token
4835                  !!!next-token;                  !!!next-token;
4836                  redo B;                  redo B;
4837                }                }
   
               ## generate implied end tags  
               while ({  
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!cp ('t244');  
 ## ISSUE: Can this case be reached?  
                 pop @{$self->{open_elements}};  
               }  
                 
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!cp ('t245');  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               } else {  
                 !!!cp ('t246');  
               }  
4838                                    
4839                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4840                  pop @{$open_tables};
4841                                
4842                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4843                                
# Line 4693  sub _tree_construction_main ($) { Line 4865  sub _tree_construction_main ($) {
4865                  } # INSCOPE                  } # INSCOPE
4866                    unless (defined $i) {                    unless (defined $i) {
4867                      !!!cp ('t249');                      !!!cp ('t249');
4868                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4869                      ## Ignore the token                      ## Ignore the token
4870                      !!!next-token;                      !!!next-token;
4871                      redo B;                      redo B;
# Line 4717  sub _tree_construction_main ($) { Line 4889  sub _tree_construction_main ($) {
4889                  } # INSCOPE                  } # INSCOPE
4890                    unless (defined $i) {                    unless (defined $i) {
4891                      !!!cp ('t252');                      !!!cp ('t252');
4892                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);
4893                      ## Ignore the token                      ## Ignore the token
4894                      !!!next-token;                      !!!next-token;
4895                      redo B;                      redo B;
# Line 4729  sub _tree_construction_main ($) { Line 4901  sub _tree_construction_main ($) {
4901                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4902                    !!!cp ('t253');                    !!!cp ('t253');
4903  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4904                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4905                  }                  }
4906                                    
# Line 4755  sub _tree_construction_main ($) { Line 4926  sub _tree_construction_main ($) {
4926                } # INSCOPE                } # INSCOPE
4927                unless (defined $i) {                unless (defined $i) {
4928                  !!!cp ('t256');                  !!!cp ('t256');
4929                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4930                  ## Ignore the token                  ## Ignore the token
4931                  !!!next-token;                  !!!next-token;
4932                  redo B;                  redo B;
# Line 4767  sub _tree_construction_main ($) { Line 4938  sub _tree_construction_main ($) {
4938                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4939                  !!!cp ('t257');                  !!!cp ('t257');
4940  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4941                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4942                }                }
4943    
# Line 4782  sub _tree_construction_main ($) { Line 4952  sub _tree_construction_main ($) {
4952                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
4953                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4954                !!!cp ('t258');                !!!cp ('t258');
4955                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4956                ## Ignore the token                ## Ignore the token
4957                !!!next-token;                !!!next-token;
4958                redo B;                redo B;
4959          } else {          } else {
4960            !!!cp ('t259');            !!!cp ('t259');
4961            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
4962    
4963            $insert = $insert_to_foster;            $insert = $insert_to_foster;
4964            #            #
4965          }          }
4966          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4967            unless ($self->{open_elements}->[-1]->[1] eq 'html' and
4968                    @{$self->{open_elements}} == 1) { # redundant, maybe
4969              !!!parse-error (type => 'in body:#eof', token => $token);
4970              !!!cp ('t259.1');
4971              #
4972            } else {
4973              !!!cp ('t259.2');
4974              #
4975            }
4976    
4977            ## Stop parsing
4978            last B;
4979        } else {        } else {
4980          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4981        }        }
# Line 4824  sub _tree_construction_main ($) { Line 5007  sub _tree_construction_main ($) {
5007              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5008                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] eq 'html') {
5009                  !!!cp ('t264');                  !!!cp ('t264');
5010                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5011                  ## Ignore the token                  ## Ignore the token
5012                  !!!next-token;                  !!!next-token;
5013                  redo B;                  redo B;
# Line 4837  sub _tree_construction_main ($) { Line 5020  sub _tree_construction_main ($) {
5020                }                }
5021              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5022                !!!cp ('t266');                !!!cp ('t266');
5023                !!!parse-error (type => 'unmatched end tag:col');                !!!parse-error (type => 'unmatched end tag:col', token => $token);
5024                ## Ignore the token                ## Ignore the token
5025                !!!next-token;                !!!next-token;
5026                redo B;                redo B;
# Line 4845  sub _tree_construction_main ($) { Line 5028  sub _tree_construction_main ($) {
5028                !!!cp ('t267');                !!!cp ('t267');
5029                #                #
5030              }              }
5031            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5032              die "$0: $token->{type}: Unknown token type";          if ($self->{open_elements}->[-1]->[1] eq 'html' or
5033            }              @{$self->{open_elements}} == 1) { # redundant, maybe
5034              !!!cp ('t270.2');
5035              ## Stop parsing.
5036              last B;
5037            } else {
5038              ## NOTE: As if </colgroup>.
5039              !!!cp ('t270.1');
5040              pop @{$self->{open_elements}}; # colgroup
5041              $self->{insertion_mode} = IN_TABLE_IM;
5042              ## Reprocess.
5043              redo B;
5044            }
5045          } else {
5046            die "$0: $token->{type}: Unknown token type";
5047          }
5048    
5049            ## As if </colgroup>            ## As if </colgroup>
5050            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] eq 'html') {
5051              !!!cp ('t269');              !!!cp ('t269');
5052              !!!parse-error (type => 'unmatched end tag:colgroup');  ## TODO: Wrong error type?
5053                !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5054              ## Ignore the token              ## Ignore the token
5055              !!!next-token;              !!!next-token;
5056              redo B;              redo B;
# Line 4863  sub _tree_construction_main ($) { Line 5061  sub _tree_construction_main ($) {
5061              ## reprocess              ## reprocess
5062              redo B;              redo B;
5063            }            }
5064      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5065        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5066          !!!cp ('t271');          !!!cp ('t271');
5067          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
# Line 4902  sub _tree_construction_main ($) { Line 5100  sub _tree_construction_main ($) {
5100                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
5101                !!!next-token;                !!!next-token;
5102                redo B;                redo B;
5103              } elsif ($token->{tag_name} eq 'select') {          } elsif ($token->{tag_name} eq 'select' or
5104  ## TODO: The type below is not good - <select> is replaced by </select>                   $token->{tag_name} eq 'input' or
5105                !!!parse-error (type => 'not closed:select');                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5106                ## As if </select> instead                    {
5107                       caption => 1, table => 1,
5108                       tbody => 1, tfoot => 1, thead => 1,
5109                       tr => 1, td => 1, th => 1,
5110                      }->{$token->{tag_name}})) {
5111              ## TODO: The type below is not good - <select> is replaced by </select>
5112              !!!parse-error (type => 'not closed:select', token => $token);
5113              ## NOTE: As if the token were </select> (<select> case) or
5114              ## as if there were </select> (otherwise).
5115                ## have an element in table scope                ## have an element in table scope
5116                my $i;                my $i;
5117                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5118                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5119                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq 'select') {
5120                    !!!cp ('t278');                    !!!cp ('t278');
5121                    $i = $_;                    $i = $_;
5122                    last INSCOPE;                    last INSCOPE;
# Line 4923  sub _tree_construction_main ($) { Line 5129  sub _tree_construction_main ($) {
5129                } # INSCOPE                } # INSCOPE
5130                unless (defined $i) {                unless (defined $i) {
5131                  !!!cp ('t280');                  !!!cp ('t280');
5132                  !!!parse-error (type => 'unmatched end tag:select');                  !!!parse-error (type => 'unmatched end tag:select', token => $token);
5133                  ## Ignore the token                  ## Ignore the token
5134                  !!!next-token;                  !!!next-token;
5135                  redo B;                  redo B;
# Line 4934  sub _tree_construction_main ($) { Line 5140  sub _tree_construction_main ($) {
5140    
5141                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5142    
5143                !!!next-token;            if ($token->{tag_name} eq 'select') {
5144                redo B;              !!!cp ('t281.2');
5145                !!!next-token;
5146                redo B;
5147              } else {
5148                !!!cp ('t281.1');
5149                ## Reprocess the token.
5150                redo B;
5151              }
5152          } else {          } else {
5153            !!!cp ('t282');            !!!cp ('t282');
5154            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5155            ## Ignore the token            ## Ignore the token
5156            !!!next-token;            !!!next-token;
5157            redo B;            redo B;
# Line 4955  sub _tree_construction_main ($) { Line 5168  sub _tree_construction_main ($) {
5168                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5169                } else {                } else {
5170                  !!!cp ('t285');                  !!!cp ('t285');
5171                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5172                  ## Ignore the token                  ## Ignore the token
5173                }                }
5174                !!!next-token;                !!!next-token;
# Line 4966  sub _tree_construction_main ($) { Line 5179  sub _tree_construction_main ($) {
5179                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5180                } else {                } else {
5181                  !!!cp ('t287');                  !!!cp ('t287');
5182                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5183                  ## Ignore the token                  ## Ignore the token
5184                }                }
5185                !!!next-token;                !!!next-token;
# Line 4989  sub _tree_construction_main ($) { Line 5202  sub _tree_construction_main ($) {
5202                } # INSCOPE                } # INSCOPE
5203                unless (defined $i) {                unless (defined $i) {
5204                  !!!cp ('t290');                  !!!cp ('t290');
5205                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5206                  ## Ignore the token                  ## Ignore the token
5207                  !!!next-token;                  !!!next-token;
5208                  redo B;                  redo B;
# Line 5002  sub _tree_construction_main ($) { Line 5215  sub _tree_construction_main ($) {
5215    
5216                !!!next-token;                !!!next-token;
5217                redo B;                redo B;
5218              } elsif ({          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5219                        caption => 1, table => 1, tbody => 1,                   {
5220                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    caption => 1, table => 1, tbody => 1,
5221                       }->{$token->{tag_name}}) {                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5222                     }->{$token->{tag_name}}) {
5223  ## TODO: The following is wrong?  ## TODO: The following is wrong?
5224                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5225                                
5226                ## have an element in table scope                ## have an element in table scope
5227                my $i;                my $i;
# Line 5051  sub _tree_construction_main ($) { Line 5265  sub _tree_construction_main ($) {
5265                unless (defined $i) {                unless (defined $i) {
5266                  !!!cp ('t297');                  !!!cp ('t297');
5267  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
5268                  !!!parse-error (type => 'unmatched end tag:select');                  !!!parse-error (type => 'unmatched end tag:select', token => $token);
5269                  ## Ignore the </select> token                  ## Ignore the </select> token
5270                  !!!next-token; ## TODO: ok?                  !!!next-token; ## TODO: ok?
5271                  redo B;                  redo B;
# Line 5066  sub _tree_construction_main ($) { Line 5280  sub _tree_construction_main ($) {
5280                redo B;                redo B;
5281          } else {          } else {
5282            !!!cp ('t299');            !!!cp ('t299');
5283            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
5284            ## Ignore the token            ## Ignore the token
5285            !!!next-token;            !!!next-token;
5286            redo B;            redo B;
5287          }          }
5288          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5289            unless ($self->{open_elements}->[-1]->[1] eq 'html' and
5290                    @{$self->{open_elements}} == 1) { # redundant, maybe
5291              !!!cp ('t299.1');
5292              !!!parse-error (type => 'in body:#eof', token => $token);
5293            } else {
5294              !!!cp ('t299.2');
5295            }
5296    
5297            ## Stop parsing.
5298            last B;
5299        } else {        } else {
5300          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5301        }        }
# Line 5092  sub _tree_construction_main ($) { Line 5317  sub _tree_construction_main ($) {
5317                    
5318          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5319            !!!cp ('t301');            !!!cp ('t301');
5320            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#character', token => $token);
5321    
5322            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
5323          } else {          } else {
# Line 5100  sub _tree_construction_main ($) { Line 5325  sub _tree_construction_main ($) {
5325          }          }
5326                    
5327          ## "after body" insertion mode          ## "after body" insertion mode
5328          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#character', token => $token);
5329    
5330          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
5331          ## reprocess          ## reprocess
# Line 5108  sub _tree_construction_main ($) { Line 5333  sub _tree_construction_main ($) {
5333        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5334          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5335            !!!cp ('t303');            !!!cp ('t303');
5336            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5337                        
5338            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
5339          } else {          } else {
# Line 5116  sub _tree_construction_main ($) { Line 5341  sub _tree_construction_main ($) {
5341          }          }
5342    
5343          ## "after body" insertion mode          ## "after body" insertion mode
5344          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
5345    
5346          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
5347          ## reprocess          ## reprocess
# Line 5124  sub _tree_construction_main ($) { Line 5349  sub _tree_construction_main ($) {
5349        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5350          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5351            !!!cp ('t305');            !!!cp ('t305');
5352            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
5353                        
5354            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
5355            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
# Line 5136  sub _tree_construction_main ($) { Line 5361  sub _tree_construction_main ($) {
5361          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
5362            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
5363              !!!cp ('t307');              !!!cp ('t307');
5364              !!!parse-error (type => 'unmatched end tag:html');              !!!parse-error (type => 'unmatched end tag:html', token => $token);
5365              ## Ignore the token              ## Ignore the token
5366              !!!next-token;              !!!next-token;
5367              redo B;              redo B;
# Line 5148  sub _tree_construction_main ($) { Line 5373  sub _tree_construction_main ($) {
5373            }            }
5374          } else {          } else {
5375            !!!cp ('t309');            !!!cp ('t309');
5376            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
5377    
5378            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
5379            ## reprocess            ## reprocess
5380            redo B;            redo B;
5381          }          }
5382          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5383            !!!cp ('t309.2');
5384            ## Stop parsing
5385            last B;
5386        } else {        } else {
5387          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5388        }        }
# Line 5172  sub _tree_construction_main ($) { Line 5401  sub _tree_construction_main ($) {
5401          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
5402            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5403              !!!cp ('t311');              !!!cp ('t311');
5404              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#character', token => $token);
5405            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
5406              !!!cp ('t312');              !!!cp ('t312');
5407              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
5408            } else { # "after html frameset"            } else { # "after html frameset"
5409              !!!cp ('t313');              !!!cp ('t313');
5410              !!!parse-error (type => 'after html:#character');              !!!parse-error (type => 'after html:#character', token => $token);
5411    
5412              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5413              ## Reprocess in the "after frameset" insertion mode.              ## Reprocess in the "after frameset" insertion mode.
5414              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
5415            }            }
5416                        
5417            ## Ignore the token.            ## Ignore the token.
# Line 5200  sub _tree_construction_main ($) { Line 5429  sub _tree_construction_main ($) {
5429        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5430          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5431            !!!cp ('t316');            !!!cp ('t316');
5432            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5433    
5434            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
5435            ## Process in the "after frameset" insertion mode.            ## Process in the "after frameset" insertion mode.
# Line 5224  sub _tree_construction_main ($) { Line 5453  sub _tree_construction_main ($) {
5453          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
5454            !!!cp ('t320');            !!!cp ('t320');
5455            ## NOTE: As if in body.            ## NOTE: As if in body.
5456            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL);
5457            redo B;            redo B;
5458          } else {          } else {
5459            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5460              !!!cp ('t321');              !!!cp ('t321');
5461              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
5462            } else {            } else {
5463              !!!cp ('t322');              !!!cp ('t322');
5464              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
5465            }            }
5466            ## Ignore the token            ## Ignore the token
5467            !!!next-token;            !!!next-token;
# Line 5241  sub _tree_construction_main ($) { Line 5470  sub _tree_construction_main ($) {
5470        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5471          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5472            !!!cp ('t323');            !!!cp ('t323');
5473            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
5474    
5475            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
5476            ## Process in the "after frameset" insertion mode.            ## Process in the "after frameset" insertion mode.
# Line 5254  sub _tree_construction_main ($) { Line 5483  sub _tree_construction_main ($) {
5483            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] eq 'html' and
5484                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
5485              !!!cp ('t325');              !!!cp ('t325');
5486              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5487              ## Ignore the token              ## Ignore the token
5488              !!!next-token;              !!!next-token;
5489            } else {            } else {
# Line 5280  sub _tree_construction_main ($) { Line 5509  sub _tree_construction_main ($) {
5509          } else {          } else {
5510            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5511              !!!cp ('t330');              !!!cp ('t330');
5512              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
5513            } else {            } else {
5514              !!!cp ('t331');              !!!cp ('t331');
5515              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
5516            }            }
5517            ## Ignore the token            ## Ignore the token
5518            !!!next-token;            !!!next-token;
5519            redo B;            redo B;
5520          }          }
5521          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5522            unless ($self->{open_elements}->[-1]->[1] eq 'html' and
5523                    @{$self->{open_elements}} == 1) { # redundant, maybe
5524              !!!cp ('t331.1');
5525              !!!parse-error (type => 'in body:#eof', token => $token);
5526            } else {
5527              !!!cp ('t331.2');
5528            }
5529            
5530            ## Stop parsing
5531            last B;
5532        } else {        } else {
5533          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5534        }        }
# Line 5303  sub _tree_construction_main ($) { Line 5543  sub _tree_construction_main ($) {
5543        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
5544          !!!cp ('t332');          !!!cp ('t332');
5545          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5546          $script_start_tag->($insert);          $script_start_tag->();
5547          redo B;          redo B;
5548        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
5549          !!!cp ('t333');          !!!cp ('t333');
5550          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5551          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
5552          redo B;          redo B;
5553        } elsif ({        } elsif ({
5554                  base => 1, link => 1,                  base => 1, link => 1,
# Line 5328  sub _tree_construction_main ($) { Line 5568  sub _tree_construction_main ($) {
5568            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) { ## TODO: And if supported
5569              !!!cp ('t335');              !!!cp ('t335');
5570              $self->{change_encoding}              $self->{change_encoding}
5571                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
5572                            
5573              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5574                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
# Line 5343  sub _tree_construction_main ($) { Line 5583  sub _tree_construction_main ($) {
5583                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
5584                !!!cp ('t336');                !!!cp ('t336');
5585                $self->{change_encoding}                $self->{change_encoding}
5586                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
5587                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5588                    ->set_user_data (manakai_has_reference =>                    ->set_user_data (manakai_has_reference =>
5589                                         $token->{attributes}->{content}                                         $token->{attributes}->{content}
# Line 5371  sub _tree_construction_main ($) { Line 5611  sub _tree_construction_main ($) {
5611          redo B;          redo B;
5612        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
5613          !!!cp ('t341');          !!!cp ('t341');
         !!!parse-error (type => 'in body:title');  
5614          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5615          $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]);  
           }  
         });  
5616          redo B;          redo B;
5617        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
5618          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body', token => $token);
5619                                
5620          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
5621              $self->{open_elements}->[1]->[1] ne 'body') {              $self->{open_elements}->[1]->[1] ne 'body') {
# Line 5407  sub _tree_construction_main ($) { Line 5638  sub _tree_construction_main ($) {
5638                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
5639                  div => 1, dl => 1, fieldset => 1,                  div => 1, dl => 1, fieldset => 1,
5640                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5641                  listing => 1, menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
5642                  pre => 1,                  pre => 1, listing => 1,
5643                    form => 1,
5644                    table => 1,
5645                    hr => 1,
5646                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5647            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
5648              !!!cp ('t350');
5649              !!!parse-error (type => 'in form:form', token => $token);
5650              ## Ignore the token
5651              !!!next-token;
5652              redo B;
5653            }
5654    
5655          ## has a p element in scope          ## has a p element in scope
5656          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5657            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5658              !!!cp ('t344');              !!!cp ('t344');
5659              !!!back-token;              !!!back-token;
5660              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
5661                          line => $token->{line}, column => $token->{column}};
5662              redo B;              redo B;
5663            } elsif ({            } elsif ({
5664                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
5665                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5666                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5667              !!!cp ('t345');              !!!cp ('t345');
# Line 5427  sub _tree_construction_main ($) { Line 5670  sub _tree_construction_main ($) {
5670          } # INSCOPE          } # INSCOPE
5671                        
5672          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5673          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
5674            !!!next-token;            !!!next-token;
5675            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
5676              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5440  sub _tree_construction_main ($) { Line 5683  sub _tree_construction_main ($) {
5683            } else {            } else {
5684              !!!cp ('t348');              !!!cp ('t348');
5685            }            }
5686          } else {          } elsif ($token->{tag_name} eq 'form') {
5687            !!!cp ('t347');            !!!cp ('t347.1');
5688              $self->{form_element} = $self->{open_elements}->[-1]->[0];
5689    
5690            !!!next-token;            !!!next-token;
5691          }          } elsif ($token->{tag_name} eq 'table') {
5692          redo B;            !!!cp ('t382');
5693        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
5694          if (defined $self->{form_element}) {            
5695            !!!cp ('t350');            $self->{insertion_mode} = IN_TABLE_IM;
5696            !!!parse-error (type => 'in form:form');  
5697            ## Ignore the token            !!!next-token;
5698            } elsif ($token->{tag_name} eq 'hr') {
5699              !!!cp ('t386');
5700              pop @{$self->{open_elements}};
5701            
5702            !!!next-token;            !!!next-token;
           redo B;  
5703          } else {          } else {
5704            ## 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];  
5705            !!!next-token;            !!!next-token;
           redo B;  
5706          }          }
5707        } elsif ($token->{tag_name} eq 'li') {          redo B;
5708          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
5709          ## has a p element in scope          ## has a p element in scope
5710          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5711            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5712              !!!cp ('t353');              !!!cp ('t353');
5713              !!!back-token;              !!!back-token;
5714              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
5715                          line => $token->{line}, column => $token->{column}};
5716              redo B;              redo B;
5717            } elsif ({            } elsif ({
5718                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
5719                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5720                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5721              !!!cp ('t354');              !!!cp ('t354');
# Line 5494  sub _tree_construction_main ($) { Line 5726  sub _tree_construction_main ($) {
5726          ## Step 1          ## Step 1
5727          my $i = -1;          my $i = -1;
5728          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
5729            my $li_or_dtdd = {li => {li => 1},
5730                              dt => {dt => 1, dd => 1},
5731                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
5732          LI: {          LI: {
5733            ## Step 2            ## Step 2
5734            if ($node->[1] eq 'li') {            if ($li_or_dtdd->{$node->[1]}) {
5735              if ($i != -1) {              if ($i != -1) {
5736                !!!cp ('t355');                !!!cp ('t355');
5737                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'end tag missing:'.
5738                                $self->{open_elements}->[-1]->[1]);                                $self->{open_elements}->[-1]->[1], token => $token);
5739              } else {              } else {
5740                !!!cp ('t356');                !!!cp ('t356');
5741              }              }
# Line 5530  sub _tree_construction_main ($) { Line 5765  sub _tree_construction_main ($) {
5765          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5766          !!!next-token;          !!!next-token;
5767          redo B;          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});  
         !!!next-token;  
         redo B;  
5768        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
5769          ## has a p element in scope          ## has a p element in scope
5770          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5771            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5772              !!!cp ('t367');              !!!cp ('t367');
5773              !!!back-token;              !!!back-token;
5774              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
5775                          line => $token->{line}, column => $token->{column}};
5776              redo B;              redo B;
5777            } elsif ({            } elsif ({
5778                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
5779                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5780                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5781              !!!cp ('t368');              !!!cp ('t368');
# Line 5614  sub _tree_construction_main ($) { Line 5794  sub _tree_construction_main ($) {
5794            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
5795            if ($node->[1] eq 'a') {            if ($node->[1] eq 'a') {
5796              !!!cp ('t371');              !!!cp ('t371');
5797              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a', token => $token);
5798                            
5799              !!!back-token;              !!!back-token;
5800              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
5801              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
5802                $formatting_end_tag->($token);
5803                            
5804              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
5805                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
# Line 5648  sub _tree_construction_main ($) { Line 5829  sub _tree_construction_main ($) {
5829    
5830          !!!next-token;          !!!next-token;
5831          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;  
5832        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
5833          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5834    
# Line 5669  sub _tree_construction_main ($) { Line 5837  sub _tree_construction_main ($) {
5837            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
5838            if ($node->[1] eq 'nobr') {            if ($node->[1] eq 'nobr') {
5839              !!!cp ('t376');              !!!cp ('t376');
5840              !!!parse-error (type => 'in nobr:nobr');              !!!parse-error (type => 'in nobr:nobr', token => $token);
5841              !!!back-token;              !!!back-token;
5842              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
5843                          line => $token->{line}, column => $token->{column}};
5844              redo B;              redo B;
5845            } elsif ({            } elsif ({
5846                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
5847                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5848                     }->{$node->[1]}) {                     }->{$node->[1]}) {
5849              !!!cp ('t377');              !!!cp ('t377');
# Line 5693  sub _tree_construction_main ($) { Line 5862  sub _tree_construction_main ($) {
5862            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
5863            if ($node->[1] eq 'button') {            if ($node->[1] eq 'button') {
5864              !!!cp ('t378');              !!!cp ('t378');
5865              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button', token => $token);
5866              !!!back-token;              !!!back-token;
5867              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button',
5868                          line => $token->{line}, column => $token->{column}};
5869              redo B;              redo B;
5870            } elsif ({            } elsif ({
5871                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
5872                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5873                     }->{$node->[1]}) {                     }->{$node->[1]}) {
5874              !!!cp ('t379');              !!!cp ('t379');
# Line 5716  sub _tree_construction_main ($) { Line 5886  sub _tree_construction_main ($) {
5886    
5887          !!!next-token;          !!!next-token;
5888          redo B;          redo B;
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         !!!cp ('t380');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
           
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         !!!cp ('t381');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t382');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t383');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = IN_TABLE_IM;  
             
         !!!next-token;  
         redo B;  
5889        } elsif ({        } elsif ({
5890                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
5891                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
5892                  image => 1,                  noembed => 1,
5893                    noframes => 1,
5894                    noscript => 0, ## TODO: 1 if scripting is enabled
5895                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5896          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
5897            !!!cp ('t384');            !!!cp ('t381');
5898            !!!parse-error (type => 'image');            $reconstruct_active_formatting_elements->($insert_to_current);
           $token->{tag_name} = 'img';  
5899          } else {          } else {
5900            !!!cp ('t385');            !!!cp ('t399');
5901          }          }
5902            ## NOTE: There is an "as if in body" code clone.
5903          ## 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;  
5904          redo B;          redo B;
5905        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
5906          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
5907                    
5908          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
5909            !!!cp ('t389');            !!!cp ('t389');
# Line 5825  sub _tree_construction_main ($) { Line 5920  sub _tree_construction_main ($) {
5920            delete $at->{prompt};            delete $at->{prompt};
5921            my @tokens = (            my @tokens = (
5922                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
5923                           attributes => $form_attrs},                           attributes => $form_attrs,
5924                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
5925                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
5926                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
5927                            {type => START_TAG_TOKEN, tag_name => 'p',
5928                             line => $token->{line}, column => $token->{column}},
5929                            {type => START_TAG_TOKEN, tag_name => 'label',
5930                             line => $token->{line}, column => $token->{column}},
5931                         );                         );
5932            if ($prompt_attr) {            if ($prompt_attr) {
5933              !!!cp ('t390');              !!!cp ('t390');
5934              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
5935                               line => $token->{line}, column => $token->{column}};
5936            } else {            } else {
5937              !!!cp ('t391');              !!!cp ('t391');
5938              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
5939                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
5940                               line => $token->{line}, column => $token->{column}}; # SHOULD
5941              ## TODO: make this configurable              ## TODO: make this configurable
5942            }            }
5943            push @tokens,            push @tokens,
5944                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
5945                             line => $token->{line}, column => $token->{column}},
5946                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
5947                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
5948                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
5949                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
5950                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
5951                            {type => START_TAG_TOKEN, tag_name => 'hr',
5952                             line => $token->{line}, column => $token->{column}},
5953                            {type => END_TAG_TOKEN, tag_name => 'form',
5954                             line => $token->{line}, column => $token->{column}};
5955            $token = shift @tokens;            $token = shift @tokens;
5956            !!!back-token (@tokens);            !!!back-token (@tokens);
5957            redo B;            redo B;
# Line 5892  sub _tree_construction_main ($) { Line 5998  sub _tree_construction_main ($) {
5998            ## Ignore the token            ## Ignore the token
5999          } else {          } else {
6000            !!!cp ('t398');            !!!cp ('t398');
6001            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6002          }          }
6003          !!!next-token;          !!!next-token;
6004          redo B;          redo B;
6005        } elsif ({        } elsif ({
                 iframe => 1,  
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!cp ('t399');  
         ## NOTE: There is an "as if in body" code clone.  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'select') {  
         !!!cp ('t400');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
   
         ## TODO: associate with $self->{form_element} if defined  
           
         $self->{insertion_mode} = IN_SELECT_IM;  
         !!!next-token;  
         redo B;  
       } elsif ({  
6006                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6007                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
6008                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
6009                  thead => 1, tr => 1,                  thead => 1, tr => 1,
6010                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6011          !!!cp ('t401');          !!!cp ('t401');
6012          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6013          ## Ignore the token          ## Ignore the token
6014          !!!next-token;          !!!next-token;
6015          redo B;          redo B;
6016                    
6017          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6018        } else {        } else {
6019          !!!cp ('t402');          if ($token->{tag_name} eq 'image') {
6020              !!!cp ('t384');
6021              !!!parse-error (type => 'image', token => $token);
6022              $token->{tag_name} = 'img';
6023            } else {
6024              !!!cp ('t385');
6025            }
6026    
6027            ## NOTE: There is an "as if <br>" code clone.
6028          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6029                    
6030          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
6031    
6032            if ({
6033                 applet => 1, marquee => 1, object => 1,
6034                }->{$token->{tag_name}}) {
6035              !!!cp ('t380');
6036              push @$active_formatting_elements, ['#marker', ''];
6037            } elsif ({
6038                      b => 1, big => 1, em => 1, font => 1, i => 1,
6039                      s => 1, small => 1, strile => 1,
6040                      strong => 1, tt => 1, u => 1,
6041                     }->{$token->{tag_name}}) {
6042              !!!cp ('t375');
6043              push @$active_formatting_elements, $self->{open_elements}->[-1];
6044            } elsif ($token->{tag_name} eq 'input') {
6045              !!!cp ('t388');
6046              ## TODO: associate with $self->{form_element} if defined
6047              pop @{$self->{open_elements}};
6048            } elsif ({
6049                      area => 1, basefont => 1, bgsound => 1, br => 1,
6050                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6051                      #image => 1,
6052                     }->{$token->{tag_name}}) {
6053              !!!cp ('t388.1');
6054              pop @{$self->{open_elements}};
6055            } elsif ($token->{tag_name} eq 'select') {
6056              ## TODO: associate with $self->{form_element} if defined
6057            
6058              if ($self->{insertion_mode} & TABLE_IMS or
6059                  $self->{insertion_mode} & BODY_TABLE_IMS or
6060                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6061                !!!cp ('t400.1');
6062                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6063              } else {
6064                !!!cp ('t400.2');
6065                $self->{insertion_mode} = IN_SELECT_IM;
6066              }
6067            } else {
6068              !!!cp ('t402');
6069            }
6070                    
6071          !!!next-token;          !!!next-token;
6072          redo B;          redo B;
6073        }        }
6074      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6075        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
6076          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
6077              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
6078            for (@{$self->{open_elements}}) {          INSCOPE: {
6079              unless ({            for (reverse @{$self->{open_elements}}) {
6080                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] eq 'body') {
6081                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
6082                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
6083                      }->{$_->[1]}) {                last INSCOPE;
6084                !!!cp ('t403');              } elsif ({
6085                !!!parse-error (type => 'not closed:'.$_->[1]);                        applet => 1, table => 1, caption => 1, td => 1, th => 1,
6086              } else {                        button => 1, marquee => 1, object => 1, html => 1,
6087                !!!cp ('t404');                       }->{$_->[1]}) {
6088                  !!!cp ('t405.1');
6089                  last;
6090              }              }
6091            }            }
6092    
6093            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
6094            !!!next-token;                            value => $token->{tag_name}, token => $token);
6095            redo B;            ## NOTE: Ignore the token.
         } else {  
           !!!cp ('t405');  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
6096            !!!next-token;            !!!next-token;
6097            redo B;            redo B;
6098            } # INSCOPE
6099    
6100            for (@{$self->{open_elements}}) {
6101              unless ({
6102                       dd => 1, dt => 1, li => 1, p => 1, td => 1,
6103                       th => 1, tr => 1, body => 1, html => 1,
6104                       tbody => 1, tfoot => 1, thead => 1,
6105                      }->{$_->[1]}) {
6106                !!!cp ('t403');
6107                !!!parse-error (type => 'not closed:'.$_->[1], token => $token);
6108                last;
6109              } else {
6110                !!!cp ('t404');
6111              }
6112          }          }
6113    
6114            $self->{insertion_mode} = AFTER_BODY_IM;
6115            !!!next-token;
6116            redo B;
6117        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
6118          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') {
6119            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6120            if ($self->{open_elements}->[-1]->[1] ne 'body') {            if ($self->{open_elements}->[-1]->[1] ne 'body') {
6121              !!!cp ('t406');              !!!cp ('t406');
6122              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1], token => $token);
6123            } else {            } else {
6124              !!!cp ('t407');              !!!cp ('t407');
6125            }            }
# Line 5980  sub _tree_construction_main ($) { Line 6128  sub _tree_construction_main ($) {
6128            redo B;            redo B;
6129          } else {          } else {
6130            !!!cp ('t408');            !!!cp ('t408');
6131            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6132            ## Ignore the token            ## Ignore the token
6133            !!!next-token;            !!!next-token;
6134            redo B;            redo B;
# Line 5990  sub _tree_construction_main ($) { Line 6138  sub _tree_construction_main ($) {
6138                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
6139                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
6140                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
6141                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
6142                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6143          ## has an element in scope          ## has an element in scope
6144          my $i;          my $i;
# Line 6001  sub _tree_construction_main ($) { Line 6149  sub _tree_construction_main ($) {
6149              $i = $_;              $i = $_;
6150              last INSCOPE;              last INSCOPE;
6151            } elsif ({            } elsif ({
6152                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
6153                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
6154                     }->{$node->[1]}) {                     }->{$node->[1]}) {
6155              !!!cp ('t411');              !!!cp ('t411');
# Line 6011  sub _tree_construction_main ($) { Line 6159  sub _tree_construction_main ($) {
6159    
6160          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
6161            !!!cp ('t413');            !!!cp ('t413');
6162            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6163          } else {          } else {
6164            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
6165            while ({            while ({
# Line 6027  sub _tree_construction_main ($) { Line 6175  sub _tree_construction_main ($) {
6175            ## Step 2.            ## Step 2.
6176            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6177              !!!cp ('t412');              !!!cp ('t412');
6178              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
6179            } else {            } else {
6180              !!!cp ('t414');              !!!cp ('t414');
6181            }            }
# Line 6038  sub _tree_construction_main ($) { Line 6186  sub _tree_construction_main ($) {
6186            ## Step 4.            ## Step 4.
6187            $clear_up_to_marker->()            $clear_up_to_marker->()
6188                if {                if {
6189                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
6190                }->{$token->{tag_name}};                }->{$token->{tag_name}};
6191          }          }
6192          !!!next-token;          !!!next-token;
6193          redo B;          redo B;
6194        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
6195            undef $self->{form_element};
6196    
6197          ## has an element in scope          ## has an element in scope
6198            my $i;
6199          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6200            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6201            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] eq $token->{tag_name}) {
             ## generate implied end tags  
             while ({  
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t417');  
               pop @{$self->{open_elements}};  
             }  
   
6202              !!!cp ('t418');              !!!cp ('t418');
6203                $i = $_;
6204              last INSCOPE;              last INSCOPE;
6205            } elsif ({            } elsif ({
6206                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
6207                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
6208                     }->{$node->[1]}) {                     }->{$node->[1]}) {
6209              !!!cp ('t419');              !!!cp ('t419');
6210              last INSCOPE;              last INSCOPE;
6211            }            }
6212          } # INSCOPE          } # INSCOPE
6213            
6214          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 {  
6215            !!!cp ('t421');            !!!cp ('t421');
6216            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6217            } else {
6218              ## Step 1. generate implied end tags
6219              while ({
6220                      dd => 1, dt => 1, li => 1, p => 1,
6221                     }->{$self->{open_elements}->[-1]->[1]}) {
6222                !!!cp ('t417');
6223                pop @{$self->{open_elements}};
6224              }
6225              
6226              ## Step 2.
6227              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6228                !!!cp ('t417.1');
6229                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
6230              } else {
6231                !!!cp ('t420');
6232              }  
6233              
6234              ## Step 3.
6235              splice @{$self->{open_elements}}, $i;
6236          }          }
6237    
         undef $self->{form_element};  
6238          !!!next-token;          !!!next-token;
6239          redo B;          redo B;
6240        } elsif ({        } elsif ({
# Line 6088  sub _tree_construction_main ($) { Line 6247  sub _tree_construction_main ($) {
6247            if ({            if ({
6248                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6249                }->{$node->[1]}) {                }->{$node->[1]}) {
             ## generate implied end tags  
             while ({  
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t422');  
               pop @{$self->{open_elements}};  
             }  
   
6250              !!!cp ('t423');              !!!cp ('t423');
6251              $i = $_;              $i = $_;
6252              last INSCOPE;              last INSCOPE;
6253            } elsif ({            } elsif ({
6254                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
6255                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
6256                     }->{$node->[1]}) {                     }->{$node->[1]}) {
6257              !!!cp ('t424');              !!!cp ('t424');
6258              last INSCOPE;              last INSCOPE;
6259            }            }
6260          } # INSCOPE          } # INSCOPE
6261            
6262          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6263            !!!cp ('t425');            !!!cp ('t425.1');
6264            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6265          } else {          } else {
6266            !!!cp ('t426');            ## Step 1. generate implied end tags
6267              while ({
6268                      dd => 1, dt => 1, li => 1, p => 1,
6269                     }->{$self->{open_elements}->[-1]->[1]}) {
6270                !!!cp ('t422');
6271                pop @{$self->{open_elements}};
6272              }
6273              
6274              ## Step 2.
6275              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6276                !!!cp ('t425');
6277                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6278              } else {
6279                !!!cp ('t426');
6280              }
6281    
6282              ## Step 3.
6283              splice @{$self->{open_elements}}, $i;
6284          }          }
6285                    
         splice @{$self->{open_elements}}, $i if defined $i;  
6286          !!!next-token;          !!!next-token;
6287          redo B;          redo B;
6288        } elsif ($token->{tag_name} eq 'p') {        } elsif ($token->{tag_name} eq 'p') {
# Line 6128  sub _tree_construction_main ($) { Line 6295  sub _tree_construction_main ($) {
6295              $i = $_;              $i = $_;
6296              last INSCOPE;              last INSCOPE;
6297            } elsif ({            } elsif ({
6298                      table => 1, caption => 1, td => 1, th => 1,                      applet => 1, table => 1, caption => 1, td => 1, th => 1,
6299                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
6300                     }->{$node->[1]}) {                     }->{$node->[1]}) {
6301              !!!cp ('t411.1');              !!!cp ('t411.1');
# Line 6139  sub _tree_construction_main ($) { Line 6306  sub _tree_construction_main ($) {
6306          if (defined $i) {          if (defined $i) {
6307            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6308              !!!cp ('t412.1');              !!!cp ('t412.1');
6309              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
6310            } else {            } else {
6311              !!!cp ('t414.1');              !!!cp ('t414.1');
6312            }            }
# Line 6147  sub _tree_construction_main ($) { Line 6314  sub _tree_construction_main ($) {
6314            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6315          } else {          } else {
6316            !!!cp ('t413.1');            !!!cp ('t413.1');
6317            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6318    
6319            !!!cp ('t415.1');            !!!cp ('t415.1');
6320            ## As if <p>, then reprocess the current token            ## As if <p>, then reprocess the current token
# Line 6166  sub _tree_construction_main ($) { Line 6333  sub _tree_construction_main ($) {
6333                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
6334                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6335          !!!cp ('t427');          !!!cp ('t427');
6336          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token);
6337          redo B;          redo B;
6338        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
6339          !!!cp ('t428');          !!!cp ('t428');
6340          !!!parse-error (type => 'unmatched end tag:br');          !!!parse-error (type => 'unmatched end tag:br', token => $token);
6341    
6342          ## As if <br>          ## As if <br>
6343          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
# Line 6195  sub _tree_construction_main ($) { Line 6362  sub _tree_construction_main ($) {
6362                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
6363                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6364          !!!cp ('t429');          !!!cp ('t429');
6365          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6366          ## Ignore the token          ## Ignore the token
6367          !!!next-token;          !!!next-token;
6368          redo B;          redo B;
# Line 6224  sub _tree_construction_main ($) { Line 6391  sub _tree_construction_main ($) {
6391              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
6392                !!!cp ('t431');                !!!cp ('t431');
6393                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
6394                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1], token => $token);
6395              } else {              } else {
6396                !!!cp ('t432');                !!!cp ('t432');
6397              }              }
# Line 6241  sub _tree_construction_main ($) { Line 6408  sub _tree_construction_main ($) {
6408                  ($special_category->{$node->[1]} or                  ($special_category->{$node->[1]} or
6409                   $scoping_category->{$node->[1]})) {                   $scoping_category->{$node->[1]})) {
6410                !!!cp ('t433');                !!!cp ('t433');
6411                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6412                ## Ignore the token                ## Ignore the token
6413                !!!next-token;                !!!next-token;
6414                last S2;                last S2;

Legend:
Removed from v.1.91  
changed lines
  Added in v.1.114

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24