/[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.93 by wakaba, Sat Mar 8 03:43:48 2008 UTC revision 1.123 by wakaba, Sun Apr 6 10:32:00 2008 UTC
# Line 12  use Error qw(:try); Line 12  use Error qw(:try);
12  ## TODO: 1252 parse error (revision 1264)  ## TODO: 1252 parse error (revision 1264)
13  ## TODO: 8859-11 = 874 (revision 1271)  ## TODO: 8859-11 = 874 (revision 1271)
14    
15    sub A_EL () { 0b1 }
16    sub ADDRESS_EL () { 0b10 }
17    sub BODY_EL () { 0b100 }
18    sub BUTTON_EL () { 0b1000 }
19    sub CAPTION_EL () { 0b10000 }
20    sub DD_EL () { 0b100000 }
21    sub DIV_EL () { 0b1000000 }
22    sub DT_EL () { 0b10000000 }
23    sub FORM_EL () { 0b100000000 }
24    sub FORMATTING_EL () { 0b1000000000 }
25    sub FRAMESET_EL () { 0b10000000000 }
26    sub HEADING_EL () { 0b100000000000 }
27    sub HTML_EL () { 0b1000000000000 }
28    sub LI_EL () { 0b10000000000000 }
29    sub NOBR_EL () { 0b100000000000000 }
30    sub OPTION_EL () { 0b1000000000000000 }
31    sub OPTGROUP_EL () { 0b10000000000000000 }
32    sub P_EL () { 0b100000000000000000 }
33    sub SELECT_EL () { 0b1000000000000000000 }
34    sub TABLE_EL () { 0b10000000000000000000 }
35    sub TABLE_CELL_EL () { 0b100000000000000000000 }
36    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
37    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
38    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
39    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
40    
41    sub TABLE_ROWS_EL () {
42      TABLE_EL |
43      TABLE_ROW_EL |
44      TABLE_ROW_GROUP_EL
45    }
46    
47    sub END_TAG_OPTIONAL_EL () {
48      DD_EL |
49      DT_EL |
50      LI_EL |
51      P_EL
52    }
53    
54    sub ALL_END_TAG_OPTIONAL_EL () {
55      END_TAG_OPTIONAL_EL |
56      BODY_EL |
57      HTML_EL |
58      TABLE_CELL_EL |
59      TABLE_ROW_EL |
60      TABLE_ROW_GROUP_EL
61    }
62    
63    sub SCOPING_EL () {
64      BUTTON_EL |
65      CAPTION_EL |
66      HTML_EL |
67      TABLE_EL |
68      TABLE_CELL_EL |
69      MISC_SCOPING_EL
70    }
71    
72    sub TABLE_SCOPING_EL () {
73      HTML_EL |
74      TABLE_EL
75    }
76    
77    sub TABLE_ROWS_SCOPING_EL () {
78      HTML_EL |
79      TABLE_ROW_GROUP_EL
80    }
81    
82    sub TABLE_ROW_SCOPING_EL () {
83      HTML_EL |
84      TABLE_ROW_EL
85    }
86    
87    sub SPECIAL_EL () {
88      ADDRESS_EL |
89      BODY_EL |
90      DIV_EL |
91      END_TAG_OPTIONAL_EL |
92      FORM_EL |
93      FRAMESET_EL |
94      HEADING_EL |
95      OPTION_EL |
96      OPTGROUP_EL |
97      SELECT_EL |
98      TABLE_ROW_EL |
99      TABLE_ROW_GROUP_EL |
100      MISC_SPECIAL_EL
101    }
102    
103    my $el_category = {
104      a => A_EL | FORMATTING_EL,
105      address => ADDRESS_EL,
106      applet => MISC_SCOPING_EL,
107      area => MISC_SPECIAL_EL,
108      b => FORMATTING_EL,
109      base => MISC_SPECIAL_EL,
110      basefont => MISC_SPECIAL_EL,
111      bgsound => MISC_SPECIAL_EL,
112      big => FORMATTING_EL,
113      blockquote => MISC_SPECIAL_EL,
114      body => BODY_EL,
115      br => MISC_SPECIAL_EL,
116      button => BUTTON_EL,
117      caption => CAPTION_EL,
118      center => MISC_SPECIAL_EL,
119      col => MISC_SPECIAL_EL,
120      colgroup => MISC_SPECIAL_EL,
121      dd => DD_EL,
122      dir => MISC_SPECIAL_EL,
123      div => DIV_EL,
124      dl => MISC_SPECIAL_EL,
125      dt => DT_EL,
126      em => FORMATTING_EL,
127      embed => MISC_SPECIAL_EL,
128      fieldset => MISC_SPECIAL_EL,
129      font => FORMATTING_EL,
130      form => FORM_EL,
131      frame => MISC_SPECIAL_EL,
132      frameset => FRAMESET_EL,
133      h1 => HEADING_EL,
134      h2 => HEADING_EL,
135      h3 => HEADING_EL,
136      h4 => HEADING_EL,
137      h5 => HEADING_EL,
138      h6 => HEADING_EL,
139      head => MISC_SPECIAL_EL,
140      hr => MISC_SPECIAL_EL,
141      html => HTML_EL,
142      i => FORMATTING_EL,
143      iframe => MISC_SPECIAL_EL,
144      img => MISC_SPECIAL_EL,
145      input => MISC_SPECIAL_EL,
146      isindex => MISC_SPECIAL_EL,
147      li => LI_EL,
148      link => MISC_SPECIAL_EL,
149      listing => MISC_SPECIAL_EL,
150      marquee => MISC_SCOPING_EL,
151      menu => MISC_SPECIAL_EL,
152      meta => MISC_SPECIAL_EL,
153      nobr => NOBR_EL | FORMATTING_EL,
154      noembed => MISC_SPECIAL_EL,
155      noframes => MISC_SPECIAL_EL,
156      noscript => MISC_SPECIAL_EL,
157      object => MISC_SCOPING_EL,
158      ol => MISC_SPECIAL_EL,
159      optgroup => OPTGROUP_EL,
160      option => OPTION_EL,
161      p => P_EL,
162      param => MISC_SPECIAL_EL,
163      plaintext => MISC_SPECIAL_EL,
164      pre => MISC_SPECIAL_EL,
165      s => FORMATTING_EL,
166      script => MISC_SPECIAL_EL,
167      select => SELECT_EL,
168      small => FORMATTING_EL,
169      spacer => MISC_SPECIAL_EL,
170      strike => FORMATTING_EL,
171      strong => FORMATTING_EL,
172      style => MISC_SPECIAL_EL,
173      table => TABLE_EL,
174      tbody => TABLE_ROW_GROUP_EL,
175      td => TABLE_CELL_EL,
176      textarea => MISC_SPECIAL_EL,
177      tfoot => TABLE_ROW_GROUP_EL,
178      th => TABLE_CELL_EL,
179      thead => TABLE_ROW_GROUP_EL,
180      title => MISC_SPECIAL_EL,
181      tr => TABLE_ROW_EL,
182      tt => FORMATTING_EL,
183      u => FORMATTING_EL,
184      ul => MISC_SPECIAL_EL,
185      wbr => MISC_SPECIAL_EL,
186    };
187    
188  my $permitted_slash_tag_name = {  my $permitted_slash_tag_name = {
189    base => 1,    base => 1,
190    link => 1,    link => 1,
# Line 74  my $special_category = { Line 247  my $special_category = {
247    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,
248  };  };
249  my $scoping_category = {  my $scoping_category = {
250    button => 1, caption => 1, html => 1, marquee => 1, object => 1,    applet => 1, button => 1, caption => 1, html => 1, marquee => 1, object => 1,
251    table => 1, td => 1, th => 1,    table => 1, td => 1, th => 1,
252  };  };
253  my $formatting_category = {  my $formatting_category = {
# Line 108  sub parse_byte_string ($$$$;$) { Line 281  sub parse_byte_string ($$$$;$) {
281    $self->{change_encoding} = sub {    $self->{change_encoding} = sub {
282      my $self = shift;      my $self = shift;
283      my $charset = lc shift;      my $charset = lc shift;
284        my $token = shift;
285      ## TODO: if $charset is supported      ## TODO: if $charset is supported
286      ## TODO: normalize charset name      ## TODO: normalize charset name
287    
# Line 126  sub parse_byte_string ($$$$;$) { Line 300  sub parse_byte_string ($$$$;$) {
300      }      }
301    
302      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
303          ':'.$charset, level => 'w');          ':'.$charset, level => 'w', token => $token);
304    
305      ## Step 3      ## Step 3
306      # if (can) {      # if (can) {
# Line 177  sub parse_string ($$$;$) { Line 351  sub parse_string ($$$;$) {
351        if defined $self->{input_encoding};        if defined $self->{input_encoding};
352    
353    my $i = 0;    my $i = 0;
354    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
355    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
356    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
357      my $self = shift;      my $self = shift;
358    
# Line 187  sub parse_string ($$$;$) { Line 361  sub parse_string ($$$;$) {
361    
362      $self->{next_char} = -1 and return if $i >= length $$s;      $self->{next_char} = -1 and return if $i >= length $$s;
363      $self->{next_char} = ord substr $$s, $i++, 1;      $self->{next_char} = ord substr $$s, $i++, 1;
364      $column++;  
365        ($self->{line_prev}, $self->{column_prev})
366            = ($self->{line}, $self->{column});
367        $self->{column}++;
368            
369      if ($self->{next_char} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
370        $line++;        $self->{line}++;
371        $column = 0;        $self->{column} = 0;
372      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
373        $i++ if substr ($$s, $i, 1) eq "\x0A";        $i++ if substr ($$s, $i, 1) eq "\x0A";
374        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
375        $line++;        $self->{line}++;
376        $column = 0;        $self->{column} = 0;
377      } elsif ($self->{next_char} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
378        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
379      } elsif ($self->{next_char} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
# Line 209  sub parse_string ($$$;$) { Line 386  sub parse_string ($$$;$) {
386    
387    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
388      my (%opt) = @_;      my (%opt) = @_;
389      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
390        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
391        warn "Parse error ($opt{type}) at line $line column $column\n";
392    };    };
393    $self->{parse_error} = sub {    $self->{parse_error} = sub {
394      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
395    };    };
396    
397    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 220  sub parse_string ($$$;$) { Line 399  sub parse_string ($$$;$) {
399    $self->_construct_tree;    $self->_construct_tree;
400    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
401    
402      delete $self->{parse_error}; # remove loop
403    
404    return $self->{document};    return $self->{document};
405  } # parse_string  } # parse_string
406    
# Line 303  sub TABLE_IMS ()      { 0b1000000 } Line 484  sub TABLE_IMS ()      { 0b1000000 }
484  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
485  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
486  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
487    sub SELECT_IMS ()     { 0b10000000000 }
488    
489  ## NOTE: "initial" and "before html" insertion modes have no constants.  ## NOTE: "initial" and "before html" insertion modes have no constants.
490    
# Line 325  sub IN_TABLE_IM () { TABLE_IMS } Line 507  sub IN_TABLE_IM () { TABLE_IMS }
507  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
508  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
509  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
510  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
511    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
512  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
513    
514  ## 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 630  sub _get_next_token ($) {
630          #          #
631        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
632          !!!cp (11);          !!!cp (11);
633          !!!emit ({type => END_OF_FILE_TOKEN});          !!!emit ({type => END_OF_FILE_TOKEN,
634                      line => $self->{line}, column => $self->{column}});
635          last A; ## TODO: ok?          last A; ## TODO: ok?
636        } else {        } else {
637          !!!cp (12);          !!!cp (12);
638        }        }
639        # Anything else        # Anything else
640        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
641                     data => chr $self->{next_char}};                     data => chr $self->{next_char},
642                       line => $self->{line}, column => $self->{column},
643                      };
644        ## Stay in the data state        ## Stay in the data state
645        !!!next-input-character;        !!!next-input-character;
646    
# Line 463  sub _get_next_token ($) { Line 649  sub _get_next_token ($) {
649        redo A;        redo A;
650      } elsif ($self->{state} == ENTITY_DATA_STATE) {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
651        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
652    
653          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
654                
655        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
656    
# Line 471  sub _get_next_token ($) { Line 659  sub _get_next_token ($) {
659    
660        unless (defined $token) {        unless (defined $token) {
661          !!!cp (13);          !!!cp (13);
662          !!!emit ({type => CHARACTER_TOKEN, data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&',
663                      line => $l, column => $c,
664                     });
665        } else {        } else {
666          !!!cp (14);          !!!cp (14);
667          !!!emit ($token);          !!!emit ($token);
# Line 490  sub _get_next_token ($) { Line 680  sub _get_next_token ($) {
680            ## reconsume            ## reconsume
681            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
682    
683            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
684                        line => $self->{line_prev},
685                        column => $self->{column_prev},
686                       });
687    
688            redo A;            redo A;
689          }          }
# Line 510  sub _get_next_token ($) { Line 703  sub _get_next_token ($) {
703            !!!cp (19);            !!!cp (19);
704            $self->{current_token}            $self->{current_token}
705              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
706                 tag_name => chr ($self->{next_char} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
707                   line => $self->{line_prev},
708                   column => $self->{column_prev}};
709            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
710            !!!next-input-character;            !!!next-input-character;
711            redo A;            redo A;
# Line 518  sub _get_next_token ($) { Line 713  sub _get_next_token ($) {
713                   $self->{next_char} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
714            !!!cp (20);            !!!cp (20);
715            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
716                              tag_name => chr ($self->{next_char})};                                      tag_name => chr ($self->{next_char}),
717                                        line => $self->{line_prev},
718                                        column => $self->{column_prev}};
719            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
720            !!!next-input-character;            !!!next-input-character;
721            redo A;            redo A;
722          } elsif ($self->{next_char} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
723            !!!cp (21);            !!!cp (21);
724            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag',
725                              line => $self->{line_prev},
726                              column => $self->{column_prev});
727            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
728            !!!next-input-character;            !!!next-input-character;
729    
730            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
731                        line => $self->{line_prev},
732                        column => $self->{column_prev},
733                       });
734    
735            redo A;            redo A;
736          } elsif ($self->{next_char} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
737            !!!cp (22);            !!!cp (22);
738            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio',
739                              line => $self->{line_prev},
740                              column => $self->{column_prev});
741            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
742              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
743                                        line => $self->{line_prev},
744                                        column => $self->{column_prev},
745                                       };
746            ## $self->{next_char} is intentionally left as is            ## $self->{next_char} is intentionally left as is
747            redo A;            redo A;
748          } else {          } else {
# Line 543  sub _get_next_token ($) { Line 751  sub _get_next_token ($) {
751            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
752            ## reconsume            ## reconsume
753    
754            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
755                        line => $self->{line_prev},
756                        column => $self->{column_prev},
757                       });
758    
759            redo A;            redo A;
760          }          }
# Line 551  sub _get_next_token ($) { Line 762  sub _get_next_token ($) {
762          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
763        }        }
764      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
765          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
766        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
767          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
768    
769            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
770            my @next_char;            my @next_char;
771            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 782  sub _get_next_token ($) {
782                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
783                $self->{state} = DATA_STATE;                $self->{state} = DATA_STATE;
784    
785                !!!emit ({type => CHARACTER_TOKEN, data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
786                            line => $l, column => $c,
787                           });
788        
789                redo A;                redo A;
790              }              }
# Line 588  sub _get_next_token ($) { Line 803  sub _get_next_token ($) {
803              $self->{next_char} = shift @next_char; # reconsume              $self->{next_char} = shift @next_char; # reconsume
804              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
805              $self->{state} = DATA_STATE;              $self->{state} = DATA_STATE;
806              !!!emit ({type => CHARACTER_TOKEN, data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
807                          line => $l, column => $c,
808                         });
809              redo A;              redo A;
810            } else {            } else {
811              !!!cp (27);              !!!cp (27);
# Line 601  sub _get_next_token ($) { Line 818  sub _get_next_token ($) {
818            !!!cp (28);            !!!cp (28);
819            # next-input-character is already done            # next-input-character is already done
820            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
821            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
822                        line => $l, column => $c,
823                       });
824            redo A;            redo A;
825          }          }
826        }        }
# Line 609  sub _get_next_token ($) { Line 828  sub _get_next_token ($) {
828        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{next_char} and
829            $self->{next_char} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
830          !!!cp (29);          !!!cp (29);
831          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token}
832                            tag_name => chr ($self->{next_char} + 0x0020)};              = {type => END_TAG_TOKEN,
833                   tag_name => chr ($self->{next_char} + 0x0020),
834                   line => $l, column => $c};
835          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
836          !!!next-input-character;          !!!next-input-character;
837          redo A;          redo A;
# Line 618  sub _get_next_token ($) { Line 839  sub _get_next_token ($) {
839                 $self->{next_char} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
840          !!!cp (30);          !!!cp (30);
841          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
842                            tag_name => chr ($self->{next_char})};                                    tag_name => chr ($self->{next_char}),
843                                      line => $l, column => $c};
844          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
845          !!!next-input-character;          !!!next-input-character;
846          redo A;          redo A;
847        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
848          !!!cp (31);          !!!cp (31);
849          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag',
850                            line => $self->{line_prev}, ## "<" in "</>"
851                            column => $self->{column_prev} - 1);
852          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
853          !!!next-input-character;          !!!next-input-character;
854          redo A;          redo A;
# Line 634  sub _get_next_token ($) { Line 858  sub _get_next_token ($) {
858          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
859          # reconsume          # reconsume
860    
861          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
862                      line => $l, column => $c,
863                     });
864    
865          redo A;          redo A;
866        } else {        } else {
867          !!!cp (33);          !!!cp (33);
868          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
869          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
870            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
871                                      line => $self->{line_prev}, # "<" of "</"
872                                      column => $self->{column_prev} - 1,
873                                     };
874          ## $self->{next_char} is intentionally left as is          ## $self->{next_char} is intentionally left as is
875          redo A;          redo A;
876        }        }
# Line 657  sub _get_next_token ($) { Line 887  sub _get_next_token ($) {
887        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
888          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
889            !!!cp (35);            !!!cp (35);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
890            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
891          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
892            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 690  sub _get_next_token ($) { Line 918  sub _get_next_token ($) {
918          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
919          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
920            !!!cp (39);            !!!cp (39);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
921            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
922          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
923            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 747  sub _get_next_token ($) { Line 973  sub _get_next_token ($) {
973        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
974          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
975            !!!cp (46);            !!!cp (46);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
976            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
977          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
978            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 770  sub _get_next_token ($) { Line 994  sub _get_next_token ($) {
994        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
995                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
996          !!!cp (49);          !!!cp (49);
997          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
998                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
999                   value => '',
1000                   line => $self->{line}, column => $self->{column}};
1001          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1002          !!!next-input-character;          !!!next-input-character;
1003          redo A;          redo A;
# Line 794  sub _get_next_token ($) { Line 1020  sub _get_next_token ($) {
1020          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1021          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1022            !!!cp (52);            !!!cp (52);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1023            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1024          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1025            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 825  sub _get_next_token ($) { Line 1049  sub _get_next_token ($) {
1049          } else {          } else {
1050            !!!cp (56);            !!!cp (56);
1051          }          }
1052          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1053                                value => ''};              = {name => chr ($self->{next_char}),
1054                   value => '',
1055                   line => $self->{line}, column => $self->{column}};
1056          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1057          !!!next-input-character;          !!!next-input-character;
1058          redo A;          redo A;
# Line 836  sub _get_next_token ($) { Line 1062  sub _get_next_token ($) {
1062          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1063              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1064            !!!cp (57);            !!!cp (57);
1065            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1066            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1067          } else {          } else {
1068            !!!cp (58);            !!!cp (58);
# Line 865  sub _get_next_token ($) { Line 1091  sub _get_next_token ($) {
1091          $before_leave->();          $before_leave->();
1092          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1093            !!!cp (61);            !!!cp (61);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1094            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1095          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1096            !!!cp (62);            !!!cp (62);
# Line 911  sub _get_next_token ($) { Line 1135  sub _get_next_token ($) {
1135          $before_leave->();          $before_leave->();
1136          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1137            !!!cp (66);            !!!cp (66);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1138            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1139          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1140            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 963  sub _get_next_token ($) { Line 1185  sub _get_next_token ($) {
1185        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1186          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1187            !!!cp (73);            !!!cp (73);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1188            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1189          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1190            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 987  sub _get_next_token ($) { Line 1207  sub _get_next_token ($) {
1207        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1208                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1209          !!!cp (76);          !!!cp (76);
1210          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1211                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1212                   value => '',
1213                   line => $self->{line}, column => $self->{column}};
1214          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1215          !!!next-input-character;          !!!next-input-character;
1216          redo A;          redo A;
# Line 1012  sub _get_next_token ($) { Line 1234  sub _get_next_token ($) {
1234          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1235          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1236            !!!cp (79);            !!!cp (79);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1237            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1238          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1239            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1035  sub _get_next_token ($) { Line 1255  sub _get_next_token ($) {
1255          redo A;          redo A;
1256        } else {        } else {
1257          !!!cp (82);          !!!cp (82);
1258          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1259                                value => ''};              = {name => chr ($self->{next_char}),
1260                   value => '',
1261                   line => $self->{line}, column => $self->{column}};
1262          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1263          !!!next-input-character;          !!!next-input-character;
1264          redo A;                  redo A;        
# Line 1069  sub _get_next_token ($) { Line 1291  sub _get_next_token ($) {
1291        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1292          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1293            !!!cp (87);            !!!cp (87);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1294            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1295          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1296            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1094  sub _get_next_token ($) { Line 1314  sub _get_next_token ($) {
1314          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1315          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1316            !!!cp (90);            !!!cp (90);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1317            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1318          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1319            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1143  sub _get_next_token ($) { Line 1361  sub _get_next_token ($) {
1361          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1362          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1363            !!!cp (97);            !!!cp (97);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1364            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1365          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1366            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1187  sub _get_next_token ($) { Line 1403  sub _get_next_token ($) {
1403          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1404          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1405            !!!cp (103);            !!!cp (103);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1406            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1407          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1408            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1234  sub _get_next_token ($) { Line 1448  sub _get_next_token ($) {
1448        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1449          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1450            !!!cp (109);            !!!cp (109);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1451            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1452          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1453            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1259  sub _get_next_token ($) { Line 1471  sub _get_next_token ($) {
1471          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1472          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1473            !!!cp (112);            !!!cp (112);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1474            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1475          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1476            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1331  sub _get_next_token ($) { Line 1541  sub _get_next_token ($) {
1541        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1542          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1543            !!!cp (119);            !!!cp (119);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1544            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1545          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1546            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1377  sub _get_next_token ($) { Line 1585  sub _get_next_token ($) {
1585      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1586        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1587                
1588        my $token = {type => COMMENT_TOKEN, data => ''};        ## NOTE: Set by the previous state
1589          #my $token = {type => COMMENT_TOKEN, data => ''};
1590    
1591        BC: {        BC: {
1592          if ($self->{next_char} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
# Line 1385  sub _get_next_token ($) { Line 1594  sub _get_next_token ($) {
1594            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1595            !!!next-input-character;            !!!next-input-character;
1596    
1597            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1598    
1599            redo A;            redo A;
1600          } elsif ($self->{next_char} == -1) {          } elsif ($self->{next_char} == -1) {
# Line 1393  sub _get_next_token ($) { Line 1602  sub _get_next_token ($) {
1602            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1603            ## reconsume            ## reconsume
1604    
1605            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1606    
1607            redo A;            redo A;
1608          } else {          } else {
1609            !!!cp (126);            !!!cp (126);
1610            $token->{data} .= chr ($self->{next_char});            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1611            !!!next-input-character;            !!!next-input-character;
1612            redo BC;            redo BC;
1613          }          }
# Line 1408  sub _get_next_token ($) { Line 1617  sub _get_next_token ($) {
1617      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1618        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1619    
1620          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1621    
1622        my @next_char;        my @next_char;
1623        push @next_char, $self->{next_char};        push @next_char, $self->{next_char};
1624                
# Line 1416  sub _get_next_token ($) { Line 1627  sub _get_next_token ($) {
1627          push @next_char, $self->{next_char};          push @next_char, $self->{next_char};
1628          if ($self->{next_char} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1629            !!!cp (127);            !!!cp (127);
1630            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1631                                        line => $l, column => $c,
1632                                       };
1633            $self->{state} = COMMENT_START_STATE;            $self->{state} = COMMENT_START_STATE;
1634            !!!next-input-character;            !!!next-input-character;
1635            redo A;            redo A;
# Line 1452  sub _get_next_token ($) { Line 1665  sub _get_next_token ($) {
1665                      !!!cp (129);                      !!!cp (129);
1666                      ## TODO: What a stupid code this is!                      ## TODO: What a stupid code this is!
1667                      $self->{state} = DOCTYPE_STATE;                      $self->{state} = DOCTYPE_STATE;
1668                        $self->{current_token} = {type => DOCTYPE_TOKEN,
1669                                                  quirks => 1,
1670                                                  line => $l, column => $c,
1671                                                 };
1672                      !!!next-input-character;                      !!!next-input-character;
1673                      redo A;                      redo A;
1674                    } else {                    } else {
# Line 1480  sub _get_next_token ($) { Line 1697  sub _get_next_token ($) {
1697        $self->{next_char} = shift @next_char;        $self->{next_char} = shift @next_char;
1698        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1699        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
1700          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1701                                    line => $l, column => $c,
1702                                   };
1703        redo A;        redo A;
1704                
1705        ## 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 1823  sub _get_next_token ($) {
1823          redo A;          redo A;
1824        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
1825          !!!cp (152);          !!!cp (152);
1826          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
1827                            line => $self->{line_prev},
1828                            column => $self->{column_prev});
1829          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
1830          ## Stay in the state          ## Stay in the state
1831          !!!next-input-character;          !!!next-input-character;
# Line 1619  sub _get_next_token ($) { Line 1841  sub _get_next_token ($) {
1841          redo A;          redo A;
1842        } else {        } else {
1843          !!!cp (154);          !!!cp (154);
1844          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
1845                            line => $self->{line_prev},
1846                            column => $self->{column_prev});
1847          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
1848          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
1849          !!!next-input-character;          !!!next-input-character;
# Line 1658  sub _get_next_token ($) { Line 1882  sub _get_next_token ($) {
1882          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1883          !!!next-input-character;          !!!next-input-character;
1884    
1885          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
1886    
1887          redo A;          redo A;
1888        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
# Line 1667  sub _get_next_token ($) { Line 1891  sub _get_next_token ($) {
1891          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1892          ## reconsume          ## reconsume
1893    
1894          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
1895    
1896          redo A;          redo A;
1897        } else {        } else {
1898          !!!cp (160);          !!!cp (160);
1899          $self->{current_token}          $self->{current_token}->{name} = chr $self->{next_char};
1900              = {type => DOCTYPE_TOKEN,          delete $self->{current_token}->{quirks};
                name => chr ($self->{next_char}),  
                #quirks => 0,  
               };  
1901  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1902          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
1903          !!!next-input-character;          !!!next-input-character;
# Line 2203  sub _get_next_token ($) { Line 2424  sub _get_next_token ($) {
2424  sub _tokenize_attempt_to_consume_an_entity ($$$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2425    my ($self, $in_attr, $additional) = @_;    my ($self, $in_attr, $additional) = @_;
2426    
2427      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2428    
2429    if ({    if ({
2430         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2431         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 2466  sub _tokenize_attempt_to_consume_an_enti
2466            redo X;            redo X;
2467          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2468            !!!cp (1005);            !!!cp (1005);
2469            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2470            !!!back-next-input-character ($x_char, $self->{next_char});            !!!back-next-input-character ($x_char, $self->{next_char});
2471            $self->{next_char} = 0x0023; # #            $self->{next_char} = 0x0023; # #
2472            return undef;            return undef;
# Line 2252  sub _tokenize_attempt_to_consume_an_enti Line 2475  sub _tokenize_attempt_to_consume_an_enti
2475            !!!next-input-character;            !!!next-input-character;
2476          } else {          } else {
2477            !!!cp (1007);            !!!cp (1007);
2478            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc', line => $l, column => $c);
2479          }          }
2480    
2481          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2482            !!!cp (1008);            !!!cp (1008);
2483            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2484            $code = 0xFFFD;            $code = 0xFFFD;
2485          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2486            !!!cp (1009);            !!!cp (1009);
2487            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2488            $code = 0xFFFD;            $code = 0xFFFD;
2489          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2490            !!!cp (1010);            !!!cp (1010);
2491            !!!parse-error (type => 'CR character reference');            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2492            $code = 0x000A;            $code = 0x000A;
2493          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2494            !!!cp (1011);            !!!cp (1011);
2495            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2496            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2497          }          }
2498    
2499          return {type => CHARACTER_TOKEN, data => chr $code,          return {type => CHARACTER_TOKEN, data => chr $code,
2500                  has_reference => 1};                  has_reference => 1,
2501                    line => $l, column => $c,
2502                   };
2503        } # X        } # X
2504      } elsif (0x0030 <= $self->{next_char} and      } elsif (0x0030 <= $self->{next_char} and
2505               $self->{next_char} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
# Line 2295  sub _tokenize_attempt_to_consume_an_enti Line 2520  sub _tokenize_attempt_to_consume_an_enti
2520          !!!next-input-character;          !!!next-input-character;
2521        } else {        } else {
2522          !!!cp (1014);          !!!cp (1014);
2523          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc', line => $l, column => $c);
2524        }        }
2525    
2526        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2527          !!!cp (1015);          !!!cp (1015);
2528          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2529          $code = 0xFFFD;          $code = 0xFFFD;
2530        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2531          !!!cp (1016);          !!!cp (1016);
2532          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2533          $code = 0xFFFD;          $code = 0xFFFD;
2534        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2535          !!!cp (1017);          !!!cp (1017);
2536          !!!parse-error (type => 'CR character reference');          !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2537          $code = 0x000A;          $code = 0x000A;
2538        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2539          !!!cp (1018);          !!!cp (1018);
2540          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2541          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2542        }        }
2543                
2544        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2545                  line => $l, column => $c,
2546                 };
2547      } else {      } else {
2548        !!!cp (1019);        !!!cp (1019);
2549        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2550        !!!back-next-input-character ($self->{next_char});        !!!back-next-input-character ($self->{next_char});
2551        $self->{next_char} = 0x0023; # #        $self->{next_char} = 0x0023; # #
2552        return undef;        return undef;
# Line 2369  sub _tokenize_attempt_to_consume_an_enti Line 2596  sub _tokenize_attempt_to_consume_an_enti
2596            
2597      if ($match > 0) {      if ($match > 0) {
2598        !!!cp (1023);        !!!cp (1023);
2599        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2600                  line => $l, column => $c,
2601                 };
2602      } elsif ($match < 0) {      } elsif ($match < 0) {
2603        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
2604        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
2605          !!!cp (1024);          !!!cp (1024);
2606          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
2607                    line => $l, column => $c,
2608                   };
2609        } else {        } else {
2610          !!!cp (1025);          !!!cp (1025);
2611          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2612                    line => $l, column => $c,
2613                   };
2614        }        }
2615      } else {      } else {
2616        !!!cp (1026);        !!!cp (1026);
2617        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero', line => $l, column => $c);
2618        ## NOTE: "No characters are consumed" in the spec.        ## NOTE: "No characters are consumed" in the spec.
2619        return {type => CHARACTER_TOKEN, data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value,
2620                  line => $l, column => $c,
2621                 };
2622      }      }
2623    } else {    } else {
2624      !!!cp (1027);      !!!cp (1027);
2625      ## no characters are consumed      ## no characters are consumed
2626      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
2627      return undef;      return undef;
2628    }    }
2629  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 2459  sub _tree_construction_initial ($) { Line 2694  sub _tree_construction_initial ($) {
2694            defined $token->{public_identifier} or            defined $token->{public_identifier} or
2695            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
2696          !!!cp ('t1');          !!!cp ('t1');
2697          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
2698        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
2699          !!!cp ('t2');          !!!cp ('t2');
2700          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
2701          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
2702        } else {        } else {
2703          !!!cp ('t3');          !!!cp ('t3');
2704        }        }
2705                
2706        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
2707          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
2708          ## NOTE: Default value for both |public_id| and |system_id| attributes
2709          ## are empty strings, so that we don't set any value in missing cases.
2710        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
2711            if defined $token->{public_identifier};            if defined $token->{public_identifier};
2712        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2602  sub _tree_construction_initial ($) { Line 2839  sub _tree_construction_initial ($) {
2839                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
2840               }->{$token->{type}}) {               }->{$token->{type}}) {
2841        !!!cp ('t14');        !!!cp ('t14');
2842        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
2843        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2844        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
2845        ## reprocess        ## reprocess
# Line 2623  sub _tree_construction_initial ($) { Line 2860  sub _tree_construction_initial ($) {
2860          !!!cp ('t17');          !!!cp ('t17');
2861        }        }
2862    
2863        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
2864        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2865        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
2866        ## reprocess        ## reprocess
# Line 2652  sub _tree_construction_root_element ($) Line 2889  sub _tree_construction_root_element ($)
2889    B: {    B: {
2890        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
2891          !!!cp ('t19');          !!!cp ('t19');
2892          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
2893          ## Ignore the token          ## Ignore the token
2894          ## Stay in the insertion mode.          ## Stay in the insertion mode.
2895          !!!next-token;          !!!next-token;
# Line 2686  sub _tree_construction_root_element ($) Line 2923  sub _tree_construction_root_element ($)
2923        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
2924          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
2925            my $root_element;            my $root_element;
2926            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes});            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes}, $token);
2927            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
2928            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}},
2929                  [$root_element, $el_category->{html}];
2930    
2931            if ($token->{attributes}->{manifest}) {            if ($token->{attributes}->{manifest}) {
2932              !!!cp ('t24');              !!!cp ('t24');
2933              $self->{application_cache_selection}              $self->{application_cache_selection}
2934                  ->($token->{attributes}->{manifest}->{value});                  ->($token->{attributes}->{manifest}->{value});
2935              ## ISSUE: No relative reference resolution?              ## ISSUE: Spec is unclear on relative references.
2936                ## According to Hixie (#whatwg 2008-03-19), it should be
2937                ## resolved against the base URI of the document in HTML
2938                ## or xml:base of the element in XHTML.
2939            } else {            } else {
2940              !!!cp ('t25');              !!!cp ('t25');
2941              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
# Line 2716  sub _tree_construction_root_element ($) Line 2957  sub _tree_construction_root_element ($)
2957          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
2958        }        }
2959    
2960      my $root_element; !!!create-element ($root_element, 'html');      my $root_element; !!!create-element ($root_element, 'html',, $token);
2961      $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
2962      push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
2963    
2964      $self->{application_cache_selection}->(undef);      $self->{application_cache_selection}->(undef);
2965    
# Line 2743  sub _reset_insertion_mode ($) { Line 2984  sub _reset_insertion_mode ($) {
2984            
2985      ## Step 3      ## Step 3
2986      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"!?  
2987        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2988          $last = 1;          $last = 1;
2989          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
2990            if ($self->{inner_html_node}->[1] eq 'td' or            if ($self->{inner_html_node}->[1] & TABLE_CELL_EL) {
               $self->{inner_html_node}->[1] eq 'th') {  
2991              !!!cp ('t27');              !!!cp ('t27');
2992              #              #
2993            } else {            } else {
# Line 2779  sub _reset_insertion_mode ($) { Line 3014  sub _reset_insertion_mode ($) {
3014                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3015                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3016                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3017                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3018                         ## TODO: Foreign namespace case OK?
3019        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3020                
3021        ## Step 14        ## Step 14
3022        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3023          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3024            !!!cp ('t29');            !!!cp ('t29');
3025            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2908  sub _tree_construction_main ($) { Line 3144  sub _tree_construction_main ($) {
3144      !!!cp ('t39');      !!!cp ('t39');
3145    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3146    
3147    my $parse_rcdata = sub ($$) {    my $insert;
3148      my ($content_model_flag, $insert) = @_;  
3149      my $parse_rcdata = sub ($) {
3150        my ($content_model_flag) = @_;
3151    
3152      ## Step 1      ## Step 1
3153      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3154      my $el;      my $el;
3155      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $start_tag_name, $token->{attributes}, $token);
3156    
3157      ## Step 2      ## Step 2
3158      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3159    
3160      ## Step 3      ## Step 3
3161      $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 3185  sub _tree_construction_main ($) {
3185          $token->{tag_name} eq $start_tag_name) {          $token->{tag_name} eq $start_tag_name) {
3186        !!!cp ('t42');        !!!cp ('t42');
3187        ## 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});  
3188      } else {      } else {
3189        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3190          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3191            !!!cp ('t43');
3192            !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3193          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3194            !!!cp ('t44');
3195            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3196          } else {
3197            die "$0: $content_model_flag in parse_rcdata";
3198          }
3199      }      }
3200      !!!next-token;      !!!next-token;
3201    }; # $parse_rcdata    }; # $parse_rcdata
3202    
3203    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3204      my $script_el;      my $script_el;
3205      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes}, $token);
3206      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3207    
3208      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
# Line 2988  sub _tree_construction_main ($) { Line 3228  sub _tree_construction_main ($) {
3228        ## Ignore the token        ## Ignore the token
3229      } else {      } else {
3230        !!!cp ('t48');        !!!cp ('t48');
3231        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3232        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3233        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3234      }      }
# Line 3011  sub _tree_construction_main ($) { Line 3251  sub _tree_construction_main ($) {
3251      !!!next-token;      !!!next-token;
3252    }; # $script_start_tag    }; # $script_start_tag
3253    
3254      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3255      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3256      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3257    
3258    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3259      my $tag_name = shift;      my $end_tag_token = shift;
3260        my $tag_name = $end_tag_token->{tag_name};
3261    
3262        ## NOTE: The adoption agency algorithm (AAA).
3263    
3264      FET: {      FET: {
3265        ## Step 1        ## Step 1
3266        my $formatting_element;        my $formatting_element;
3267        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3268        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3269          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3270              !!!cp ('t52');
3271              last AFE;
3272            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3273                         eq $tag_name) {
3274            !!!cp ('t51');            !!!cp ('t51');
3275            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3276            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3277            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
3278          }          }
3279        } # AFE        } # AFE
3280        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3281          !!!cp ('t53');          !!!cp ('t53');
3282          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3283          ## Ignore the token          ## Ignore the token
3284          !!!next-token;          !!!next-token;
3285          return;          return;
# Line 3048  sub _tree_construction_main ($) { Line 3296  sub _tree_construction_main ($) {
3296              last INSCOPE;              last INSCOPE;
3297            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3298              !!!cp ('t55');              !!!cp ('t55');
3299              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3300                                token => $end_tag_token);
3301              ## Ignore the token              ## Ignore the token
3302              !!!next-token;              !!!next-token;
3303              return;              return;
3304            }            }
3305          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
                   table => 1, caption => 1, td => 1, th => 1,  
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3306            !!!cp ('t56');            !!!cp ('t56');
3307            $in_scope = 0;            $in_scope = 0;
3308          }          }
3309        } # INSCOPE        } # INSCOPE
3310        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3311          !!!cp ('t57');          !!!cp ('t57');
3312          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3313                            token => $end_tag_token);
3314          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3315          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3316          return;          return;
3317        }        }
3318        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3319          !!!cp ('t58');          !!!cp ('t58');
3320          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!parse-error (type => 'not closed',
3321                            value => $self->{open_elements}->[-1]->[0]
3322                                ->manakai_local_name,
3323                            token => $end_tag_token);
3324        }        }
3325                
3326        ## Step 2        ## Step 2
# Line 3078  sub _tree_construction_main ($) { Line 3328  sub _tree_construction_main ($) {
3328        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3329        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3330          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3331          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3332              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3333              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3334               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3335            !!!cp ('t59');            !!!cp ('t59');
3336            $furthest_block = $node;            $furthest_block = $node;
3337            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3167  sub _tree_construction_main ($) { Line 3417  sub _tree_construction_main ($) {
3417        } # S7          } # S7  
3418                
3419        ## Step 8        ## Step 8
3420        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3421            my $foster_parent_element;
3422            my $next_sibling;
3423            OE: for (reverse 0..$#{$self->{open_elements}}) {
3424              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3425                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3426                                 if (defined $parent and $parent->node_type == 1) {
3427                                   !!!cp ('t65.1');
3428                                   $foster_parent_element = $parent;
3429                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3430                                 } else {
3431                                   !!!cp ('t65.2');
3432                                   $foster_parent_element
3433                                     = $self->{open_elements}->[$_ - 1]->[0];
3434                                 }
3435                                 last OE;
3436                               }
3437                             } # OE
3438                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3439                               unless defined $foster_parent_element;
3440            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3441            $open_tables->[-1]->[1] = 1; # tainted
3442          } else {
3443            !!!cp ('t65.3');
3444            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3445          }
3446                
3447        ## Step 9        ## Step 9
3448        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 3213  sub _tree_construction_main ($) { Line 3488  sub _tree_construction_main ($) {
3488      } # FET      } # FET
3489    }; # $formatting_end_tag    }; # $formatting_end_tag
3490    
3491    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3492      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3493    }; # $insert_to_current    }; # $insert_to_current
3494    
3495    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3496                         my $child = shift;      my $child = shift;
3497                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3498                              table => 1, tbody => 1, tfoot => 1,        # MUST
3499                              thead => 1, tr => 1,        my $foster_parent_element;
3500                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
3501                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
3502                           my $foster_parent_element;          if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
                          my $next_sibling;  
                          OE: for (reverse 0..$#{$self->{open_elements}}) {  
                            if ($self->{open_elements}->[$_]->[1] eq 'table') {  
3503                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3504                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3505                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3245  sub _tree_construction_main ($) { Line 3517  sub _tree_construction_main ($) {
3517                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3518                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3519                             ($child, $next_sibling);                             ($child, $next_sibling);
3520                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3521                           !!!cp ('t72');      } else {
3522                           $self->{open_elements}->[-1]->[0]->append_child ($child);        !!!cp ('t72');
3523                         }        $self->{open_elements}->[-1]->[0]->append_child ($child);
3524        }
3525    }; # $insert_to_foster    }; # $insert_to_foster
3526    
   my $insert;  
   
3527    B: {    B: {
3528      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3529        !!!cp ('t73');        !!!cp ('t73');
3530        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3531        ## Ignore the token        ## Ignore the token
3532        ## Stay in the phase        ## Stay in the phase
3533        !!!next-token;        !!!next-token;
3534        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;  
3535      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3536               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3537        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3538          !!!cp ('t79');          !!!cp ('t79');
3539          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3540          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
3541        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3542          !!!cp ('t80');          !!!cp ('t80');
3543          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3544          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3545        } else {        } else {
3546          !!!cp ('t81');          !!!cp ('t81');
3547        }        }
3548    
3549        !!!cp ('t82');        !!!cp ('t82');
3550        !!!parse-error (type => 'not first start tag');        !!!parse-error (type => 'not first start tag', token => $token);
3551        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
3552        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
3553          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 3576  sub _tree_construction_main ($) {
3576      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & HEAD_IMS) {
3577        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
3578          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3579            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3580                !!!cp ('t88.2');
3581                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3582              } else {
3583                !!!cp ('t88.1');
3584                ## Ignore the token.
3585                !!!next-token;
3586                redo B;
3587              }
3588            unless (length $token->{data}) {            unless (length $token->{data}) {
3589              !!!cp ('t88');              !!!cp ('t88');
3590              !!!next-token;              !!!next-token;
# Line 3348  sub _tree_construction_main ($) { Line 3595  sub _tree_construction_main ($) {
3595          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3596            !!!cp ('t89');            !!!cp ('t89');
3597            ## As if <head>            ## As if <head>
3598            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, 'head',, $token);
3599            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3600            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
3601                  [$self->{head_element}, $el_category->{head}];
3602    
3603            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
3604            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3360  sub _tree_construction_main ($) { Line 3608  sub _tree_construction_main ($) {
3608            !!!cp ('t90');            !!!cp ('t90');
3609            ## As if </noscript>            ## As if </noscript>
3610            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
3611            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character', token => $token);
3612                        
3613            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
3614            ## As if </head>            ## As if </head>
# Line 3376  sub _tree_construction_main ($) { Line 3624  sub _tree_construction_main ($) {
3624            !!!cp ('t92');            !!!cp ('t92');
3625          }          }
3626    
3627              ## "after head" insertion mode          ## "after head" insertion mode
3628              ## As if <body>          ## As if <body>
3629              !!!insert-element ('body');          !!!insert-element ('body',, $token);
3630              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
3631              ## reprocess          ## reprocess
3632            redo B;
3633          } elsif ($token->{type} == START_TAG_TOKEN) {
3634            if ($token->{tag_name} eq 'head') {
3635              if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3636                !!!cp ('t93');
3637                !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes}, $token);
3638                $self->{open_elements}->[-1]->[0]->append_child
3639                    ($self->{head_element});
3640                push @{$self->{open_elements}},
3641                    [$self->{head_element}, $el_category->{head}];
3642                $self->{insertion_mode} = IN_HEAD_IM;
3643                !!!next-token;
3644              redo B;              redo B;
           } elsif ($token->{type} == START_TAG_TOKEN) {  
             if ($token->{tag_name} eq 'head') {  
               if ($self->{insertion_mode} == BEFORE_HEAD_IM) {  
                 !!!cp ('t93');  
                 !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});  
                 $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
                 push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];  
                 $self->{insertion_mode} = IN_HEAD_IM;  
                 !!!next-token;  
                 redo B;  
3645                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3646                  !!!cp ('t94');                  !!!cp ('t94');
3647                  #                  #
3648                } else {                } else {
3649                  !!!cp ('t95');                  !!!cp ('t95');
3650                  !!!parse-error (type => 'in head:head'); # or in head noscript                  !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
3651                  ## Ignore the token                  ## Ignore the token
3652                  !!!next-token;                  !!!next-token;
3653                  redo B;                  redo B;
# Line 3405  sub _tree_construction_main ($) { Line 3655  sub _tree_construction_main ($) {
3655              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3656                !!!cp ('t96');                !!!cp ('t96');
3657                ## As if <head>                ## As if <head>
3658                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head',, $token);
3659                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3660                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}},
3661                      [$self->{head_element}, $el_category->{head}];
3662    
3663                $self->{insertion_mode} = IN_HEAD_IM;                $self->{insertion_mode} = IN_HEAD_IM;
3664                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
# Line 3420  sub _tree_construction_main ($) { Line 3671  sub _tree_construction_main ($) {
3671                  !!!cp ('t98');                  !!!cp ('t98');
3672                  ## As if </noscript>                  ## As if </noscript>
3673                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3674                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base', token => $token);
3675                                
3676                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3677                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3431  sub _tree_construction_main ($) { Line 3682  sub _tree_construction_main ($) {
3682                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3683                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3684                  !!!cp ('t100');                  !!!cp ('t100');
3685                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3686                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
3687                        [$self->{head_element}, $el_category->{head}];
3688                } else {                } else {
3689                  !!!cp ('t101');                  !!!cp ('t101');
3690                }                }
3691                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3692                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3693                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3694                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3695                !!!next-token;                !!!next-token;
3696                redo B;                redo B;
# Line 3446  sub _tree_construction_main ($) { Line 3698  sub _tree_construction_main ($) {
3698                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3699                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3700                  !!!cp ('t102');                  !!!cp ('t102');
3701                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3702                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
3703                        [$self->{head_element}, $el_category->{head}];
3704                } else {                } else {
3705                  !!!cp ('t103');                  !!!cp ('t103');
3706                }                }
3707                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3708                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3709                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3710                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3711                !!!next-token;                !!!next-token;
3712                redo B;                redo B;
# Line 3461  sub _tree_construction_main ($) { Line 3714  sub _tree_construction_main ($) {
3714                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3715                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3716                  !!!cp ('t104');                  !!!cp ('t104');
3717                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3718                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
3719                        [$self->{head_element}, $el_category->{head}];
3720                } else {                } else {
3721                  !!!cp ('t105');                  !!!cp ('t105');
3722                }                }
3723                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3724                my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3725    
3726                unless ($self->{confident}) {                unless ($self->{confident}) {
3727                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
3728                    !!!cp ('t106');                    !!!cp ('t106');
3729                    $self->{change_encoding}                    $self->{change_encoding}
3730                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
3731                             $token);
3732                                        
3733                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3734                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
# Line 3488  sub _tree_construction_main ($) { Line 3743  sub _tree_construction_main ($) {
3743                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
3744                      !!!cp ('t107');                      !!!cp ('t107');
3745                      $self->{change_encoding}                      $self->{change_encoding}
3746                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
3747                               $token);
3748                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3749                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
3750                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
# Line 3514  sub _tree_construction_main ($) { Line 3770  sub _tree_construction_main ($) {
3770                  }                  }
3771                }                }
3772    
3773                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3774                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3775                !!!next-token;                !!!next-token;
3776                redo B;                redo B;
# Line 3523  sub _tree_construction_main ($) { Line 3779  sub _tree_construction_main ($) {
3779                  !!!cp ('t111');                  !!!cp ('t111');
3780                  ## As if </noscript>                  ## As if </noscript>
3781                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3782                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title', token => $token);
3783                                
3784                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3785                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3786                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3787                  !!!cp ('t112');                  !!!cp ('t112');
3788                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3789                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
3790                        [$self->{head_element}, $el_category->{head}];
3791                } else {                } else {
3792                  !!!cp ('t113');                  !!!cp ('t113');
3793                }                }
# Line 3538  sub _tree_construction_main ($) { Line 3795  sub _tree_construction_main ($) {
3795                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3796                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
3797                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
3798                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
3799                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
3800                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3801                redo B;                redo B;
3802              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
# Line 3549  sub _tree_construction_main ($) { Line 3805  sub _tree_construction_main ($) {
3805                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3806                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3807                  !!!cp ('t114');                  !!!cp ('t114');
3808                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3809                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
3810                        [$self->{head_element}, $el_category->{head}];
3811                } else {                } else {
3812                  !!!cp ('t115');                  !!!cp ('t115');
3813                }                }
3814                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
3815                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3816                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3817                redo B;                redo B;
3818              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
3819                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
3820                  !!!cp ('t116');                  !!!cp ('t116');
3821                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
3822                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3823                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
3824                  !!!next-token;                  !!!next-token;
3825                  redo B;                  redo B;
3826                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3827                  !!!cp ('t117');                  !!!cp ('t117');
3828                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript:noscript', token => $token);
3829                  ## Ignore the token                  ## Ignore the token
3830                  !!!next-token;                  !!!next-token;
3831                  redo B;                  redo B;
# Line 3581  sub _tree_construction_main ($) { Line 3838  sub _tree_construction_main ($) {
3838                  !!!cp ('t119');                  !!!cp ('t119');
3839                  ## As if </noscript>                  ## As if </noscript>
3840                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3841                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script', token => $token);
3842                                
3843                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3844                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3845                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3846                  !!!cp ('t120');                  !!!cp ('t120');
3847                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3848                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
3849                        [$self->{head_element}, $el_category->{head}];
3850                } else {                } else {
3851                  !!!cp ('t121');                  !!!cp ('t121');
3852                }                }
3853    
3854                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3855                $script_start_tag->($insert_to_current);                $script_start_tag->();
3856                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3857                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3858                redo B;                redo B;
3859              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
# Line 3604  sub _tree_construction_main ($) { Line 3862  sub _tree_construction_main ($) {
3862                  !!!cp ('t122');                  !!!cp ('t122');
3863                  ## As if </noscript>                  ## As if </noscript>
3864                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3865                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
3866                                    
3867                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3868                  ## As if </head>                  ## As if </head>
# Line 3621  sub _tree_construction_main ($) { Line 3879  sub _tree_construction_main ($) {
3879                }                }
3880    
3881                ## "after head" insertion mode                ## "after head" insertion mode
3882                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3883                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
3884                  !!!cp ('t126');                  !!!cp ('t126');
3885                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
# Line 3642  sub _tree_construction_main ($) { Line 3900  sub _tree_construction_main ($) {
3900                !!!cp ('t129');                !!!cp ('t129');
3901                ## As if </noscript>                ## As if </noscript>
3902                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3903                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
3904                                
3905                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
3906                ## As if </head>                ## As if </head>
# Line 3661  sub _tree_construction_main ($) { Line 3919  sub _tree_construction_main ($) {
3919    
3920              ## "after head" insertion mode              ## "after head" insertion mode
3921              ## As if <body>              ## As if <body>
3922              !!!insert-element ('body');              !!!insert-element ('body',, $token);
3923              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
3924              ## reprocess              ## reprocess
3925              redo B;              redo B;
# Line 3670  sub _tree_construction_main ($) { Line 3928  sub _tree_construction_main ($) {
3928                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3929                  !!!cp ('t132');                  !!!cp ('t132');
3930                  ## As if <head>                  ## As if <head>
3931                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head',, $token);
3932                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3933                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
3934                        [$self->{head_element}, $el_category->{head}];
3935    
3936                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3937                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 3683  sub _tree_construction_main ($) { Line 3942  sub _tree_construction_main ($) {
3942                  !!!cp ('t133');                  !!!cp ('t133');
3943                  ## As if </noscript>                  ## As if </noscript>
3944                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3945                  !!!parse-error (type => 'in noscript:/head');                  !!!parse-error (type => 'in noscript:/head', token => $token);
3946                                    
3947                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3948                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 3709  sub _tree_construction_main ($) { Line 3968  sub _tree_construction_main ($) {
3968                  redo B;                  redo B;
3969                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3970                  !!!cp ('t137');                  !!!cp ('t137');
3971                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
3972                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
3973                  !!!next-token;                  !!!next-token;
3974                  redo B;                  redo B;
# Line 3723  sub _tree_construction_main ($) { Line 3982  sub _tree_construction_main ($) {
3982                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3983                  !!!cp ('t139');                  !!!cp ('t139');
3984                  ## As if <head>                  ## As if <head>
3985                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head',, $token);
3986                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3987                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
3988                        [$self->{head_element}, $el_category->{head}];
3989    
3990                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3991                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3992                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3993                  !!!cp ('t140');                  !!!cp ('t140');
3994                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
3995                  ## Ignore the token                  ## Ignore the token
3996                  !!!next-token;                  !!!next-token;
3997                  redo B;                  redo B;
# Line 3746  sub _tree_construction_main ($) { Line 4006  sub _tree_construction_main ($) {
4006                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4007                  !!!cp ('t142');                  !!!cp ('t142');
4008                  ## As if <head>                  ## As if <head>
4009                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head',, $token);
4010                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4011                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4012                        [$self->{head_element}, $el_category->{head}];
4013    
4014                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4015                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3763  sub _tree_construction_main ($) { Line 4024  sub _tree_construction_main ($) {
4024                  #                  #
4025                } else {                } else {
4026                  !!!cp ('t145');                  !!!cp ('t145');
4027                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4028                  ## Ignore the token                  ## Ignore the token
4029                  !!!next-token;                  !!!next-token;
4030                  redo B;                  redo B;
# Line 3774  sub _tree_construction_main ($) { Line 4035  sub _tree_construction_main ($) {
4035                !!!cp ('t146');                !!!cp ('t146');
4036                ## As if </noscript>                ## As if </noscript>
4037                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4038                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4039                                
4040                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4041                ## As if </head>                ## As if </head>
# Line 3790  sub _tree_construction_main ($) { Line 4051  sub _tree_construction_main ($) {
4051              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4052  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
4053                !!!cp ('t148');                !!!cp ('t148');
4054                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4055                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4056                !!!next-token;                !!!next-token;
4057                redo B;                redo B;
# Line 3800  sub _tree_construction_main ($) { Line 4061  sub _tree_construction_main ($) {
4061    
4062              ## "after head" insertion mode              ## "after head" insertion mode
4063              ## As if <body>              ## As if <body>
4064              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4065              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4066              ## reprocess              ## reprocess
4067              redo B;              redo B;
4068            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4069              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4070            }            !!!cp ('t149.1');
4071    
4072              ## NOTE: As if <head>
4073              !!!create-element ($self->{head_element}, 'head',, $token);
4074              $self->{open_elements}->[-1]->[0]->append_child
4075                  ($self->{head_element});
4076              #push @{$self->{open_elements}},
4077              #    [$self->{head_element}, $el_category->{head}];
4078              #$self->{insertion_mode} = IN_HEAD_IM;
4079              ## NOTE: Reprocess.
4080    
4081              ## NOTE: As if </head>
4082              #pop @{$self->{open_elements}};
4083              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4084              ## NOTE: Reprocess.
4085              
4086              #
4087            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4088              !!!cp ('t149.2');
4089    
4090              ## NOTE: As if </head>
4091              pop @{$self->{open_elements}};
4092              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4093              ## NOTE: Reprocess.
4094    
4095              #
4096            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4097              !!!cp ('t149.3');
4098    
4099              !!!parse-error (type => 'in noscript:#eof', token => $token);
4100    
4101              ## As if </noscript>
4102              pop @{$self->{open_elements}};
4103              #$self->{insertion_mode} = IN_HEAD_IM;
4104              ## NOTE: Reprocess.
4105    
4106              ## NOTE: As if </head>
4107              pop @{$self->{open_elements}};
4108              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4109              ## NOTE: Reprocess.
4110    
4111              #
4112            } else {
4113              !!!cp ('t149.4');
4114              #
4115            }
4116    
4117            ## NOTE: As if <body>
4118            !!!insert-element ('body',, $token);
4119            $self->{insertion_mode} = IN_BODY_IM;
4120            ## NOTE: Reprocess.
4121            redo B;
4122          } else {
4123            die "$0: $token->{type}: Unknown token type";
4124          }
4125    
4126            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
4127      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
# Line 3826  sub _tree_construction_main ($) { Line 4141  sub _tree_construction_main ($) {
4141                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4142                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
4143                  ## have an element in table scope                  ## have an element in table scope
4144                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
4145                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4146                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4147                      !!!cp ('t151');                      !!!cp ('t151');
4148                      $tn = $node->[1];  
4149                      last INSCOPE;                      ## Close the cell
4150                    } elsif ({                      !!!back-token; # <?>
4151                              table => 1, html => 1,                      $token = {type => END_TAG_TOKEN,
4152                             }->{$node->[1]}) {                                tag_name => $node->[0]->manakai_local_name,
4153                      !!!cp ('t152');                                line => $token->{line},
4154                      last INSCOPE;                                column => $token->{column}};
                   }  
                 } # 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;  
4155                      redo B;                      redo B;
4156                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4157                        !!!cp ('t152');
4158                        ## ISSUE: This case can never be reached, maybe.
4159                        last;
4160                    }                    }
4161                                    }
4162                  !!!cp ('t154');  
4163                  ## Close the cell                  !!!cp ('t153');
4164                  !!!back-token; # <?>                  !!!parse-error (type => 'start tag not allowed',
4165                  $token = {type => END_TAG_TOKEN, tag_name => $tn};                      value => $token->{tag_name}, token => $token);
4166                    ## Ignore the token
4167                    !!!next-token;
4168                  redo B;                  redo B;
4169                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4170                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption', token => $token);
4171                                    
4172                  ## As if </caption>                  ## NOTE: As if </caption>.
4173                  ## have a table element in table scope                  ## have a table element in table scope
4174                  my $i;                  my $i;
4175                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4176                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4177                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
4178                      !!!cp ('t155');                      if ($node->[1] & CAPTION_EL) {
4179                      $i = $_;                        !!!cp ('t155');
4180                      last INSCOPE;                        $i = $_;
4181                    } elsif ({                        last INSCOPE;
4182                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4183                             }->{$node->[1]}) {                        !!!cp ('t156');
4184                      !!!cp ('t156');                        last;
4185                      last INSCOPE;                      }
4186                    }                    }
4187    
4188                      !!!cp ('t157');
4189                      !!!parse-error (type => 'start tag not allowed',
4190                                      value => $token->{tag_name}, token => $token);
4191                      ## Ignore the token
4192                      !!!next-token;
4193                      redo B;
4194                  } # 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;  
                   }  
4195                                    
4196                  ## generate implied end tags                  ## generate implied end tags
4197                  while ({                  while ($self->{open_elements}->[-1]->[1]
4198                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4199                    !!!cp ('t158');                    !!!cp ('t158');
4200                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4201                  }                  }
4202    
4203                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4204                    !!!cp ('t159');                    !!!cp ('t159');
4205                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4206                                      value => $self->{open_elements}->[-1]->[0]
4207                                          ->manakai_local_name,
4208                                      token => $token);
4209                  } else {                  } else {
4210                    !!!cp ('t160');                    !!!cp ('t160');
4211                  }                  }
# Line 3920  sub _tree_construction_main ($) { Line 4233  sub _tree_construction_main ($) {
4233                  my $i;                  my $i;
4234                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4235                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4236                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4237                      !!!cp ('t163');                      !!!cp ('t163');
4238                      $i = $_;                      $i = $_;
4239                      last INSCOPE;                      last INSCOPE;
4240                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4241                      !!!cp ('t164');                      !!!cp ('t164');
4242                      last INSCOPE;                      last INSCOPE;
4243                    }                    }
4244                  } # INSCOPE                  } # INSCOPE
4245                    unless (defined $i) {                    unless (defined $i) {
4246                      !!!cp ('t165');                      !!!cp ('t165');
4247                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4248                      ## Ignore the token                      ## Ignore the token
4249                      !!!next-token;                      !!!next-token;
4250                      redo B;                      redo B;
4251                    }                    }
4252                                    
4253                  ## generate implied end tags                  ## generate implied end tags
4254                  while ({                  while ($self->{open_elements}->[-1]->[1]
4255                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4256                    !!!cp ('t166');                    !!!cp ('t166');
4257                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4258                  }                  }
4259    
4260                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4261                            ne $token->{tag_name}) {
4262                    !!!cp ('t167');                    !!!cp ('t167');
4263                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4264                                      value => $self->{open_elements}->[-1]->[0]
4265                                          ->manakai_local_name,
4266                                      token => $token);
4267                  } else {                  } else {
4268                    !!!cp ('t168');                    !!!cp ('t168');
4269                  }                  }
# Line 3964  sub _tree_construction_main ($) { Line 4278  sub _tree_construction_main ($) {
4278                  redo B;                  redo B;
4279                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4280                  !!!cp ('t169');                  !!!cp ('t169');
4281                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4282                  ## Ignore the token                  ## Ignore the token
4283                  !!!next-token;                  !!!next-token;
4284                  redo B;                  redo B;
# Line 3976  sub _tree_construction_main ($) { Line 4290  sub _tree_construction_main ($) {
4290                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
4291                  ## have a table element in table scope                  ## have a table element in table scope
4292                  my $i;                  my $i;
4293                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4294                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4295                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
4296                      !!!cp ('t171');                      if ($node->[1] & CAPTION_EL) {
4297                      $i = $_;                        !!!cp ('t171');
4298                      last INSCOPE;                        $i = $_;
4299                    } elsif ({                        last INSCOPE;
4300                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4301                             }->{$node->[1]}) {                        !!!cp ('t172');
4302                      !!!cp ('t172');                        last;
4303                      last INSCOPE;                      }
4304                    }                    }
4305    
4306                      !!!cp ('t173');
4307                      !!!parse-error (type => 'unmatched end tag',
4308                                      value => $token->{tag_name}, token => $token);
4309                      ## Ignore the token
4310                      !!!next-token;
4311                      redo B;
4312                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t173');  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4313                                    
4314                  ## generate implied end tags                  ## generate implied end tags
4315                  while ({                  while ($self->{open_elements}->[-1]->[1]
4316                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4317                    !!!cp ('t174');                    !!!cp ('t174');
4318                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4319                  }                  }
4320                                    
4321                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4322                    !!!cp ('t175');                    !!!cp ('t175');
4323                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4324                                      value => $self->{open_elements}->[-1]->[0]
4325                                          ->manakai_local_name,
4326                                      token => $token);
4327                  } else {                  } else {
4328                    !!!cp ('t176');                    !!!cp ('t176');
4329                  }                  }
# Line 4022  sub _tree_construction_main ($) { Line 4338  sub _tree_construction_main ($) {
4338                  redo B;                  redo B;
4339                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4340                  !!!cp ('t177');                  !!!cp ('t177');
4341                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4342                  ## Ignore the token                  ## Ignore the token
4343                  !!!next-token;                  !!!next-token;
4344                  redo B;                  redo B;
# Line 4038  sub _tree_construction_main ($) { Line 4354  sub _tree_construction_main ($) {
4354                ## have an element in table scope                ## have an element in table scope
4355                my $i;                my $i;
4356                my $tn;                my $tn;
4357                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
4358                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
4359                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4360                    !!!cp ('t179');                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4361                    $i = $_;                      !!!cp ('t179');
4362                    last INSCOPE;                      $i = $_;
4363                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
4364                    !!!cp ('t180');                      ## Close the cell
4365                    $tn = $node->[1];                      !!!back-token; # </?>
4366                    ## NOTE: There is exactly one |td| or |th| element                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4367                    ## in scope in the stack of open elements by definition.                                line => $token->{line},
4368                  } elsif ({                                column => $token->{column}};
4369                            table => 1, html => 1,                      redo B;
4370                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_CELL_EL) {
4371                    !!!cp ('t181');                      !!!cp ('t180');
4372                    last INSCOPE;                      $tn = $node->[0]->manakai_local_name;
4373                        ## NOTE: There is exactly one |td| or |th| element
4374                        ## in scope in the stack of open elements by definition.
4375                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4376                        ## ISSUE: Can this be reached?
4377                        !!!cp ('t181');
4378                        last;
4379                      }
4380                  }                  }
4381                } # INSCOPE  
               unless (defined $i) {  
4382                  !!!cp ('t182');                  !!!cp ('t182');
4383                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4384                        value => $token->{tag_name}, token => $token);
4385                  ## Ignore the token                  ## Ignore the token
4386                  !!!next-token;                  !!!next-token;
4387                  redo B;                  redo B;
4388                } else {                } # INSCOPE
                 !!!cp ('t183');  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
4389              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
4390                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4391                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4392    
4393                ## As if </caption>                ## As if </caption>
4394                ## have a table element in table scope                ## have a table element in table scope
4395                my $i;                my $i;
4396                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4397                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4398                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4399                    !!!cp ('t184');                    !!!cp ('t184');
4400                    $i = $_;                    $i = $_;
4401                    last INSCOPE;                    last INSCOPE;
4402                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
4403                    !!!cp ('t185');                    !!!cp ('t185');
4404                    last INSCOPE;                    last INSCOPE;
4405                  }                  }
4406                } # INSCOPE                } # INSCOPE
4407                unless (defined $i) {                unless (defined $i) {
4408                  !!!cp ('t186');                  !!!cp ('t186');
4409                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);
4410                  ## Ignore the token                  ## Ignore the token
4411                  !!!next-token;                  !!!next-token;
4412                  redo B;                  redo B;
4413                }                }
4414                                
4415                ## generate implied end tags                ## generate implied end tags
4416                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
4417                  !!!cp ('t187');                  !!!cp ('t187');
4418                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4419                }                }
4420    
4421                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4422                  !!!cp ('t188');                  !!!cp ('t188');
4423                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
4424                                    value => $self->{open_elements}->[-1]->[0]
4425                                        ->manakai_local_name,
4426                                    token => $token);
4427                } else {                } else {
4428                  !!!cp ('t189');                  !!!cp ('t189');
4429                }                }
# Line 4126  sub _tree_construction_main ($) { Line 4441  sub _tree_construction_main ($) {
4441                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4442                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4443                  !!!cp ('t190');                  !!!cp ('t190');
4444                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4445                  ## Ignore the token                  ## Ignore the token
4446                  !!!next-token;                  !!!next-token;
4447                  redo B;                  redo B;
# Line 4140  sub _tree_construction_main ($) { Line 4455  sub _tree_construction_main ($) {
4455                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
4456                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4457                !!!cp ('t192');                !!!cp ('t192');
4458                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4459                ## Ignore the token                ## Ignore the token
4460                !!!next-token;                !!!next-token;
4461                redo B;                redo B;
# Line 4148  sub _tree_construction_main ($) { Line 4463  sub _tree_construction_main ($) {
4463                !!!cp ('t193');                !!!cp ('t193');
4464                #                #
4465              }              }
4466          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4467            for my $entry (@{$self->{open_elements}}) {
4468              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
4469                !!!cp ('t75');
4470                !!!parse-error (type => 'in body:#eof', token => $token);
4471                last;
4472              }
4473            }
4474    
4475            ## Stop parsing.
4476            last B;
4477        } else {        } else {
4478          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4479        }        }
# Line 4156  sub _tree_construction_main ($) { Line 4482  sub _tree_construction_main ($) {
4482        #        #
4483      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
4484        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4485              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
4486                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4487              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4488                                
4489                unless (length $token->{data}) {            unless (length $token->{data}) {
4490                  !!!cp ('t194');              !!!cp ('t194');
4491                  !!!next-token;              !!!next-token;
4492                  redo B;              redo B;
4493                } else {            } else {
4494                  !!!cp ('t195');              !!!cp ('t195');
4495                }            }
4496              }          }
4497    
4498              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
4499    
4500              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
4501              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 4176  sub _tree_construction_main ($) { Line 4503  sub _tree_construction_main ($) {
4503              ## result in a new Text node.              ## result in a new Text node.
4504              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
4505                            
4506              if ({              if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
4507                # MUST                # MUST
4508                my $foster_parent_element;                my $foster_parent_element;
4509                my $next_sibling;                my $next_sibling;
4510                my $prev_sibling;                my $prev_sibling;
4511                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
4512                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4513                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4514                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
4515                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4213  sub _tree_construction_main ($) { Line 4537  sub _tree_construction_main ($) {
4537                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
4538                     $next_sibling);                     $next_sibling);
4539                }                }
4540              } else {            $open_tables->[-1]->[1] = 1; # tainted
4541                !!!cp ('t200');          } else {
4542                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            !!!cp ('t200');
4543              }            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4544            }
4545                            
4546              !!!next-token;          !!!next-token;
4547              redo B;          redo B;
4548        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4549              if ({              if ({
4550                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
# Line 4227  sub _tree_construction_main ($) { Line 4552  sub _tree_construction_main ($) {
4552                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4553                if ($self->{insertion_mode} == IN_TABLE_IM) {                if ($self->{insertion_mode} == IN_TABLE_IM) {
4554                  ## Clear back to table context                  ## Clear back to table context
4555                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
4556                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
4557                    !!!cp ('t201');                    !!!cp ('t201');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4558                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4559                  }                  }
4560                                    
4561                  !!!insert-element ('tbody');                  !!!insert-element ('tbody',, $token);
4562                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4563                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
4564                }                }
# Line 4242  sub _tree_construction_main ($) { Line 4566  sub _tree_construction_main ($) {
4566                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4567                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
4568                    !!!cp ('t202');                    !!!cp ('t202');
4569                    !!!parse-error (type => 'missing start tag:tr');                    !!!parse-error (type => 'missing start tag:tr', token => $token);
4570                  }                  }
4571                                    
4572                  ## Clear back to table body context                  ## Clear back to table body context
4573                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
4574                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
4575                    !!!cp ('t203');                    !!!cp ('t203');
4576                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4577                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4578                  }                  }
4579                                    
4580                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4581                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
4582                    !!!cp ('t204');                    !!!cp ('t204');
4583                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4584                    !!!next-token;                    !!!next-token;
4585                    redo B;                    redo B;
4586                  } else {                  } else {
4587                    !!!cp ('t205');                    !!!cp ('t205');
4588                    !!!insert-element ('tr');                    !!!insert-element ('tr',, $token);
4589                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
4590                  }                  }
4591                } else {                } else {
# Line 4271  sub _tree_construction_main ($) { Line 4593  sub _tree_construction_main ($) {
4593                }                }
4594    
4595                ## Clear back to table row context                ## Clear back to table row context
4596                while (not {                while (not ($self->{open_elements}->[-1]->[1]
4597                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
4598                  !!!cp ('t207');                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4599                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4600                }                }
4601                                
4602                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4603                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
4604    
4605                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
# Line 4297  sub _tree_construction_main ($) { Line 4617  sub _tree_construction_main ($) {
4617                  my $i;                  my $i;
4618                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4619                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4620                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
4621                      !!!cp ('t208');                      !!!cp ('t208');
4622                      $i = $_;                      $i = $_;
4623                      last INSCOPE;                      last INSCOPE;
4624                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
4625                      !!!cp ('t209');                      !!!cp ('t209');
4626                      last INSCOPE;                      last INSCOPE;
4627                    }                    }
# Line 4314  sub _tree_construction_main ($) { Line 4629  sub _tree_construction_main ($) {
4629                  unless (defined $i) {                  unless (defined $i) {
4630                   !!!cp ('t210');                   !!!cp ('t210');
4631  ## TODO: This type is wrong.  ## TODO: This type is wrong.
4632                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
4633                    ## Ignore the token                    ## Ignore the token
4634                    !!!next-token;                    !!!next-token;
4635                    redo B;                    redo B;
4636                  }                  }
4637                                    
4638                  ## Clear back to table row context                  ## Clear back to table row context
4639                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
4640                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
4641                    !!!cp ('t211');                    !!!cp ('t211');
4642                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4643                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4644                  }                  }
4645                                    
# Line 4347  sub _tree_construction_main ($) { Line 4660  sub _tree_construction_main ($) {
4660                  my $i;                  my $i;
4661                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4662                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4663                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
4664                      !!!cp ('t214');                      !!!cp ('t214');
4665                      $i = $_;                      $i = $_;
4666                      last INSCOPE;                      last INSCOPE;
4667                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4668                      !!!cp ('t215');                      !!!cp ('t215');
4669                      last INSCOPE;                      last INSCOPE;
4670                    }                    }
# Line 4363  sub _tree_construction_main ($) { Line 4672  sub _tree_construction_main ($) {
4672                  unless (defined $i) {                  unless (defined $i) {
4673                    !!!cp ('t216');                    !!!cp ('t216');
4674  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type ios wrong.
4675                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4676                    ## Ignore the token                    ## Ignore the token
4677                    !!!next-token;                    !!!next-token;
4678                    redo B;                    redo B;
4679                  }                  }
4680    
4681                  ## Clear back to table body context                  ## Clear back to table body context
4682                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
4683                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
4684                    !!!cp ('t217');                    !!!cp ('t217');
4685                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4686                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4687                  }                  }
4688                                    
# Line 4395  sub _tree_construction_main ($) { Line 4702  sub _tree_construction_main ($) {
4702    
4703                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
4704                  ## Clear back to table context                  ## Clear back to table context
4705                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
4706                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
4707                    !!!cp ('t219');                    !!!cp ('t219');
4708                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4709                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4710                  }                  }
4711                                    
4712                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
4713                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
4714                  ## reprocess                  ## reprocess
4715                  redo B;                  redo B;
# Line 4413  sub _tree_construction_main ($) { Line 4719  sub _tree_construction_main ($) {
4719                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
4720                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
4721                  ## Clear back to table context                  ## Clear back to table context
4722                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
4723                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
4724                    !!!cp ('t220');                    !!!cp ('t220');
4725                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4726                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4727                  }                  }
4728                                    
4729                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
4730                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
4731                                    
4732                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4733                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
4734                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
4735                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 4438  sub _tree_construction_main ($) { Line 4743  sub _tree_construction_main ($) {
4743                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
4744                }                }
4745              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4746                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
4747                                  value => $self->{open_elements}->[-1]->[0]
4748                                      ->manakai_local_name,
4749                                  token => $token);
4750    
4751                ## As if </table>                ## As if </table>
4752                ## have a table element in table scope                ## have a table element in table scope
4753                my $i;                my $i;
4754                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4755                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4756                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
4757                    !!!cp ('t221');                    !!!cp ('t221');
4758                    $i = $_;                    $i = $_;
4759                    last INSCOPE;                    last INSCOPE;
4760                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
4761                    !!!cp ('t222');                    !!!cp ('t222');
4762                    last INSCOPE;                    last INSCOPE;
4763                  }                  }
# Line 4460  sub _tree_construction_main ($) { Line 4765  sub _tree_construction_main ($) {
4765                unless (defined $i) {                unless (defined $i) {
4766                  !!!cp ('t223');                  !!!cp ('t223');
4767  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
4768                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:table', token => $token);
4769                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
4770                  !!!next-token;                  !!!next-token;
4771                  redo B;                  redo B;
4772                }                }
4773                                
4774    ## TODO: Followings are removed from the latest spec.
4775                ## generate implied end tags                ## generate implied end tags
4776                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
4777                  !!!cp ('t224');                  !!!cp ('t224');
4778                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4779                }                }
4780    
4781                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
4782                  !!!cp ('t225');                  !!!cp ('t225');
4783  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
4784                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
4785                                    value => $self->{open_elements}->[-1]->[0]
4786                                        ->manakai_local_name,
4787                                    token => $token);
4788                } else {                } else {
4789                  !!!cp ('t226');                  !!!cp ('t226');
4790                }                }
4791    
4792                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4793                  pop @{$open_tables};
4794    
4795                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4796    
4797                ## reprocess                ## reprocess
4798                redo B;                redo B;
4799            } elsif ($token->{tag_name} eq 'style') {
4800              if (not $open_tables->[-1]->[1]) { # tainted
4801                !!!cp ('t227.8');
4802                ## NOTE: This is a "as if in head" code clone.
4803                $parse_rcdata->(CDATA_CONTENT_MODEL);
4804                redo B;
4805              } else {
4806                !!!cp ('t227.7');
4807                #
4808              }
4809            } elsif ($token->{tag_name} eq 'script') {
4810              if (not $open_tables->[-1]->[1]) { # tainted
4811                !!!cp ('t227.6');
4812                ## NOTE: This is a "as if in head" code clone.
4813                $script_start_tag->();
4814                redo B;
4815              } else {
4816                !!!cp ('t227.5');
4817                #
4818              }
4819            } elsif ($token->{tag_name} eq 'input') {
4820              if (not $open_tables->[-1]->[1]) { # tainted
4821                if ($token->{attributes}->{type}) { ## TODO: case
4822                  my $type = lc $token->{attributes}->{type}->{value};
4823                  if ($type eq 'hidden') {
4824                    !!!cp ('t227.3');
4825                    !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
4826    
4827                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4828    
4829                    ## TODO: form element pointer
4830    
4831                    pop @{$self->{open_elements}};
4832    
4833                    !!!next-token;
4834                    redo B;
4835                  } else {
4836                    !!!cp ('t227.2');
4837                    #
4838                  }
4839                } else {
4840                  !!!cp ('t227.1');
4841                  #
4842                }
4843              } else {
4844                !!!cp ('t227.4');
4845                #
4846              }
4847          } else {          } else {
4848            !!!cp ('t227');            !!!cp ('t227');
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
   
           $insert = $insert_to_foster;  
4849            #            #
4850          }          }
4851    
4852            !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
4853    
4854            $insert = $insert_to_foster;
4855            #
4856        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
4857              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
4858                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 4502  sub _tree_construction_main ($) { Line 4860  sub _tree_construction_main ($) {
4860                my $i;                my $i;
4861                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4862                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4863                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
4864                    !!!cp ('t228');                    !!!cp ('t228');
4865                    $i = $_;                    $i = $_;
4866                    last INSCOPE;                    last INSCOPE;
4867                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
4868                    !!!cp ('t229');                    !!!cp ('t229');
4869                    last INSCOPE;                    last INSCOPE;
4870                  }                  }
4871                } # INSCOPE                } # INSCOPE
4872                unless (defined $i) {                unless (defined $i) {
4873                  !!!cp ('t230');                  !!!cp ('t230');
4874                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4875                  ## Ignore the token                  ## Ignore the token
4876                  !!!next-token;                  !!!next-token;
4877                  redo B;                  redo B;
# Line 4524  sub _tree_construction_main ($) { Line 4880  sub _tree_construction_main ($) {
4880                }                }
4881    
4882                ## Clear back to table row context                ## Clear back to table row context
4883                while (not {                while (not ($self->{open_elements}->[-1]->[1]
4884                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
4885                  !!!cp ('t231');                  !!!cp ('t231');
4886  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4887                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4888                }                }
4889    
# Line 4544  sub _tree_construction_main ($) { Line 4898  sub _tree_construction_main ($) {
4898                  my $i;                  my $i;
4899                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4900                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4901                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
4902                      !!!cp ('t233');                      !!!cp ('t233');
4903                      $i = $_;                      $i = $_;
4904                      last INSCOPE;                      last INSCOPE;
4905                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4906                      !!!cp ('t234');                      !!!cp ('t234');
4907                      last INSCOPE;                      last INSCOPE;
4908                    }                    }
# Line 4558  sub _tree_construction_main ($) { Line 4910  sub _tree_construction_main ($) {
4910                  unless (defined $i) {                  unless (defined $i) {
4911                    !!!cp ('t235');                    !!!cp ('t235');
4912  ## TODO: The following is wrong.  ## TODO: The following is wrong.
4913                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
4914                    ## Ignore the token                    ## Ignore the token
4915                    !!!next-token;                    !!!next-token;
4916                    redo B;                    redo B;
4917                  }                  }
4918                                    
4919                  ## Clear back to table row context                  ## Clear back to table row context
4920                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
4921                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
4922                    !!!cp ('t236');                    !!!cp ('t236');
4923  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4924                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4925                  }                  }
4926                                    
# Line 4584  sub _tree_construction_main ($) { Line 4934  sub _tree_construction_main ($) {
4934                  my $i;                  my $i;
4935                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4936                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4937                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
4938                      !!!cp ('t237');                      !!!cp ('t237');
4939                      $i = $_;                      $i = $_;
4940                      last INSCOPE;                      last INSCOPE;
4941                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4942                      !!!cp ('t238');                      !!!cp ('t238');
4943                      last INSCOPE;                      last INSCOPE;
4944                    }                    }
4945                  } # INSCOPE                  } # INSCOPE
4946                  unless (defined $i) {                  unless (defined $i) {
4947                    !!!cp ('t239');                    !!!cp ('t239');
4948                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4949                    ## Ignore the token                    ## Ignore the token
4950                    !!!next-token;                    !!!next-token;
4951                    redo B;                    redo B;
4952                  }                  }
4953                                    
4954                  ## Clear back to table body context                  ## Clear back to table body context
4955                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
4956                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
4957                    !!!cp ('t240');                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4958                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4959                  }                  }
4960                                    
# Line 4626  sub _tree_construction_main ($) { Line 4970  sub _tree_construction_main ($) {
4970                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
4971                }                }
4972    
4973                  ## NOTE: </table> in the "in table" insertion mode.
4974                  ## When you edit the code fragment below, please ensure that
4975                  ## the code for <table> in the "in table" insertion mode
4976                  ## is synced with it.
4977    
4978                ## have a table element in table scope                ## have a table element in table scope
4979                my $i;                my $i;
4980                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4981                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4982                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
4983                    !!!cp ('t241');                    !!!cp ('t241');
4984                    $i = $_;                    $i = $_;
4985                    last INSCOPE;                    last INSCOPE;
4986                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
4987                    !!!cp ('t242');                    !!!cp ('t242');
4988                    last INSCOPE;                    last INSCOPE;
4989                  }                  }
4990                } # INSCOPE                } # INSCOPE
4991                unless (defined $i) {                unless (defined $i) {
4992                  !!!cp ('t243');                  !!!cp ('t243');
4993                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4994                  ## Ignore the token                  ## Ignore the token
4995                  !!!next-token;                  !!!next-token;
4996                  redo B;                  redo B;
4997                }                }
   
               ## 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');  
               }  
4998                                    
4999                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5000                  pop @{$open_tables};
5001                                
5002                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5003                                
# Line 4680  sub _tree_construction_main ($) { Line 5012  sub _tree_construction_main ($) {
5012                  my $i;                  my $i;
5013                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5014                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5015                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5016                      !!!cp ('t247');                      !!!cp ('t247');
5017                      $i = $_;                      $i = $_;
5018                      last INSCOPE;                      last INSCOPE;
5019                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5020                      !!!cp ('t248');                      !!!cp ('t248');
5021                      last INSCOPE;                      last INSCOPE;
5022                    }                    }
5023                  } # INSCOPE                  } # INSCOPE
5024                    unless (defined $i) {                    unless (defined $i) {
5025                      !!!cp ('t249');                      !!!cp ('t249');
5026                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5027                      ## Ignore the token                      ## Ignore the token
5028                      !!!next-token;                      !!!next-token;
5029                      redo B;                      redo B;
# Line 4704  sub _tree_construction_main ($) { Line 5034  sub _tree_construction_main ($) {
5034                  my $i;                  my $i;
5035                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5036                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5037                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5038                      !!!cp ('t250');                      !!!cp ('t250');
5039                      $i = $_;                      $i = $_;
5040                      last INSCOPE;                      last INSCOPE;
5041                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5042                      !!!cp ('t251');                      !!!cp ('t251');
5043                      last INSCOPE;                      last INSCOPE;
5044                    }                    }
5045                  } # INSCOPE                  } # INSCOPE
5046                    unless (defined $i) {                    unless (defined $i) {
5047                      !!!cp ('t252');                      !!!cp ('t252');
5048                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5049                      ## Ignore the token                      ## Ignore the token
5050                      !!!next-token;                      !!!next-token;
5051                      redo B;                      redo B;
5052                    }                    }
5053                                    
5054                  ## Clear back to table row context                  ## Clear back to table row context
5055                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5056                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5057                    !!!cp ('t253');                    !!!cp ('t253');
5058  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5059                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5060                  }                  }
5061                                    
# Line 4742  sub _tree_construction_main ($) { Line 5068  sub _tree_construction_main ($) {
5068                my $i;                my $i;
5069                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5070                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5071                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5072                    !!!cp ('t254');                    !!!cp ('t254');
5073                    $i = $_;                    $i = $_;
5074                    last INSCOPE;                    last INSCOPE;
5075                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5076                    !!!cp ('t255');                    !!!cp ('t255');
5077                    last INSCOPE;                    last INSCOPE;
5078                  }                  }
5079                } # INSCOPE                } # INSCOPE
5080                unless (defined $i) {                unless (defined $i) {
5081                  !!!cp ('t256');                  !!!cp ('t256');
5082                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5083                  ## Ignore the token                  ## Ignore the token
5084                  !!!next-token;                  !!!next-token;
5085                  redo B;                  redo B;
5086                }                }
5087    
5088                ## Clear back to table body context                ## Clear back to table body context
5089                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5090                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5091                  !!!cp ('t257');                  !!!cp ('t257');
5092  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5093                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5094                }                }
5095    
# Line 4782  sub _tree_construction_main ($) { Line 5104  sub _tree_construction_main ($) {
5104                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5105                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5106                !!!cp ('t258');                !!!cp ('t258');
5107                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5108                ## Ignore the token                ## Ignore the token
5109                !!!next-token;                !!!next-token;
5110                redo B;                redo B;
5111          } else {          } else {
5112            !!!cp ('t259');            !!!cp ('t259');
5113            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
5114    
5115            $insert = $insert_to_foster;            $insert = $insert_to_foster;
5116            #            #
5117          }          }
5118          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5119            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5120                    @{$self->{open_elements}} == 1) { # redundant, maybe
5121              !!!parse-error (type => 'in body:#eof', token => $token);
5122              !!!cp ('t259.1');
5123              #
5124            } else {
5125              !!!cp ('t259.2');
5126              #
5127            }
5128    
5129            ## Stop parsing
5130            last B;
5131        } else {        } else {
5132          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5133        }        }
# Line 4812  sub _tree_construction_main ($) { Line 5147  sub _tree_construction_main ($) {
5147            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5148              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
5149                !!!cp ('t262');                !!!cp ('t262');
5150                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5151                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5152                !!!next-token;                !!!next-token;
5153                redo B;                redo B;
# Line 4822  sub _tree_construction_main ($) { Line 5157  sub _tree_construction_main ($) {
5157              }              }
5158            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5159              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5160                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5161                  !!!cp ('t264');                  !!!cp ('t264');
5162                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5163                  ## Ignore the token                  ## Ignore the token
5164                  !!!next-token;                  !!!next-token;
5165                  redo B;                  redo B;
# Line 4837  sub _tree_construction_main ($) { Line 5172  sub _tree_construction_main ($) {
5172                }                }
5173              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5174                !!!cp ('t266');                !!!cp ('t266');
5175                !!!parse-error (type => 'unmatched end tag:col');                !!!parse-error (type => 'unmatched end tag:col', token => $token);
5176                ## Ignore the token                ## Ignore the token
5177                !!!next-token;                !!!next-token;
5178                redo B;                redo B;
# Line 4845  sub _tree_construction_main ($) { Line 5180  sub _tree_construction_main ($) {
5180                !!!cp ('t267');                !!!cp ('t267');
5181                #                #
5182              }              }
5183            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5184              die "$0: $token->{type}: Unknown token type";          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5185            }              @{$self->{open_elements}} == 1) { # redundant, maybe
5186              !!!cp ('t270.2');
5187              ## Stop parsing.
5188              last B;
5189            } else {
5190              ## NOTE: As if </colgroup>.
5191              !!!cp ('t270.1');
5192              pop @{$self->{open_elements}}; # colgroup
5193              $self->{insertion_mode} = IN_TABLE_IM;
5194              ## Reprocess.
5195              redo B;
5196            }
5197          } else {
5198            die "$0: $token->{type}: Unknown token type";
5199          }
5200    
5201            ## As if </colgroup>            ## As if </colgroup>
5202            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5203              !!!cp ('t269');              !!!cp ('t269');
5204              !!!parse-error (type => 'unmatched end tag:colgroup');  ## TODO: Wrong error type?
5205                !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5206              ## Ignore the token              ## Ignore the token
5207              !!!next-token;              !!!next-token;
5208              redo B;              redo B;
# Line 4863  sub _tree_construction_main ($) { Line 5213  sub _tree_construction_main ($) {
5213              ## reprocess              ## reprocess
5214              redo B;              redo B;
5215            }            }
5216      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5217        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5218          !!!cp ('t271');          !!!cp ('t271');
5219          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5220          !!!next-token;          !!!next-token;
5221          redo B;          redo B;
5222        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5223              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5224                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5225                  !!!cp ('t272');              !!!cp ('t272');
5226                  ## As if </option>              ## As if </option>
5227                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5228                } else {            } else {
5229                  !!!cp ('t273');              !!!cp ('t273');
5230                }            }
5231    
5232                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5233                !!!next-token;            !!!next-token;
5234                redo B;            redo B;
5235              } elsif ($token->{tag_name} eq 'optgroup') {          } elsif ($token->{tag_name} eq 'optgroup') {
5236                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5237                  !!!cp ('t274');              !!!cp ('t274');
5238                  ## As if </option>              ## As if </option>
5239                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5240                } else {            } else {
5241                  !!!cp ('t275');              !!!cp ('t275');
5242                }            }
5243    
5244                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5245                  !!!cp ('t276');              !!!cp ('t276');
5246                  ## As if </optgroup>              ## As if </optgroup>
5247                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5248                } else {            } else {
5249                  !!!cp ('t277');              !!!cp ('t277');
5250                }            }
5251    
5252                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5253                !!!next-token;            !!!next-token;
5254                redo B;            redo B;
5255              } elsif ($token->{tag_name} eq 'select') {          } elsif ($token->{tag_name} eq 'select' or
5256  ## TODO: The type below is not good - <select> is replaced by </select>                   $token->{tag_name} eq 'input' or
5257                !!!parse-error (type => 'not closed:select');                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5258                ## As if </select> instead                    {
5259                ## have an element in table scope                     caption => 1, table => 1,
5260                my $i;                     tbody => 1, tfoot => 1, thead => 1,
5261                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                     tr => 1, td => 1, th => 1,
5262                  my $node = $self->{open_elements}->[$_];                    }->{$token->{tag_name}})) {
5263                  if ($node->[1] eq $token->{tag_name}) {            ## TODO: The type below is not good - <select> is replaced by </select>
5264                    !!!cp ('t278');            !!!parse-error (type => 'not closed:select', token => $token);
5265                    $i = $_;            ## NOTE: As if the token were </select> (<select> case) or
5266                    last INSCOPE;            ## as if there were </select> (otherwise).
5267                  } elsif ({            ## have an element in table scope
5268                            table => 1, html => 1,            my $i;
5269                           }->{$node->[1]}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5270                    !!!cp ('t279');              my $node = $self->{open_elements}->[$_];
5271                    last INSCOPE;              if ($node->[1] & SELECT_EL) {
5272                  }                !!!cp ('t278');
5273                } # INSCOPE                $i = $_;
5274                unless (defined $i) {                last INSCOPE;
5275                  !!!cp ('t280');              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5276                  !!!parse-error (type => 'unmatched end tag:select');                !!!cp ('t279');
5277                  ## Ignore the token                last INSCOPE;
5278                  !!!next-token;              }
5279                  redo B;            } # INSCOPE
5280                }            unless (defined $i) {
5281                !!!cp ('t280');
5282                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5283                ## Ignore the token
5284                !!!next-token;
5285                redo B;
5286              }
5287                                
5288                !!!cp ('t281');            !!!cp ('t281');
5289                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5290    
5291                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5292    
5293                !!!next-token;            if ($token->{tag_name} eq 'select') {
5294                redo B;              !!!cp ('t281.2');
5295                !!!next-token;
5296                redo B;
5297              } else {
5298                !!!cp ('t281.1');
5299                ## Reprocess the token.
5300                redo B;
5301              }
5302          } else {          } else {
5303            !!!cp ('t282');            !!!cp ('t282');
5304            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5305            ## Ignore the token            ## Ignore the token
5306            !!!next-token;            !!!next-token;
5307            redo B;            redo B;
5308          }          }
5309        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5310              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
5311                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5312                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5313                  !!!cp ('t283');              !!!cp ('t283');
5314                  ## As if </option>              ## As if </option>
5315                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
5316                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5317                  !!!cp ('t284');              !!!cp ('t284');
5318                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5319                } else {            } else {
5320                  !!!cp ('t285');              !!!cp ('t285');
5321                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5322                  ## Ignore the token              ## Ignore the token
5323                }            }
5324                !!!next-token;            !!!next-token;
5325                redo B;            redo B;
5326              } elsif ($token->{tag_name} eq 'option') {          } elsif ($token->{tag_name} eq 'option') {
5327                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5328                  !!!cp ('t286');              !!!cp ('t286');
5329                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5330                } else {            } else {
5331                  !!!cp ('t287');              !!!cp ('t287');
5332                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5333                  ## Ignore the token              ## Ignore the token
5334                }            }
5335                !!!next-token;            !!!next-token;
5336                redo B;            redo B;
5337              } elsif ($token->{tag_name} eq 'select') {          } elsif ($token->{tag_name} eq 'select') {
5338                ## have an element in table scope            ## have an element in table scope
5339                my $i;            my $i;
5340                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5341                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5342                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[1] & SELECT_EL) {
5343                    !!!cp ('t288');                !!!cp ('t288');
5344                    $i = $_;                $i = $_;
5345                    last INSCOPE;                last INSCOPE;
5346                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5347                            table => 1, html => 1,                !!!cp ('t289');
5348                           }->{$node->[1]}) {                last INSCOPE;
5349                    !!!cp ('t289');              }
5350                    last INSCOPE;            } # INSCOPE
5351                  }            unless (defined $i) {
5352                } # INSCOPE              !!!cp ('t290');
5353                unless (defined $i) {              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5354                  !!!cp ('t290');              ## Ignore the token
5355                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!next-token;
5356                  ## Ignore the token              redo B;
5357                  !!!next-token;            }
                 redo B;  
               }  
5358                                
5359                !!!cp ('t291');            !!!cp ('t291');
5360                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5361    
5362                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5363    
5364                !!!next-token;            !!!next-token;
5365                redo B;            redo B;
5366              } elsif ({          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5367                        caption => 1, table => 1, tbody => 1,                   {
5368                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    caption => 1, table => 1, tbody => 1,
5369                       }->{$token->{tag_name}}) {                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5370                     }->{$token->{tag_name}}) {
5371  ## TODO: The following is wrong?  ## TODO: The following is wrong?
5372                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5373                                
5374                ## have an element in table scope            ## have an element in table scope
5375                my $i;            my $i;
5376                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5377                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5378                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5379                    !!!cp ('t292');                !!!cp ('t292');
5380                    $i = $_;                $i = $_;
5381                    last INSCOPE;                last INSCOPE;
5382                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5383                            table => 1, html => 1,                !!!cp ('t293');
5384                           }->{$node->[1]}) {                last INSCOPE;
5385                    !!!cp ('t293');              }
5386                    last INSCOPE;            } # INSCOPE
5387                  }            unless (defined $i) {
5388                } # INSCOPE              !!!cp ('t294');
5389                unless (defined $i) {              ## Ignore the token
5390                  !!!cp ('t294');              !!!next-token;
5391                  ## Ignore the token              redo B;
5392                  !!!next-token;            }
                 redo B;  
               }  
5393                                
5394                ## As if </select>            ## As if </select>
5395                ## have an element in table scope            ## have an element in table scope
5396                undef $i;            undef $i;
5397                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5398                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5399                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
5400                    !!!cp ('t295');                !!!cp ('t295');
5401                    $i = $_;                $i = $_;
5402                    last INSCOPE;                last INSCOPE;
5403                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5404  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5405                    !!!cp ('t296');                !!!cp ('t296');
5406                    last INSCOPE;                last INSCOPE;
5407                  }              }
5408                } # INSCOPE            } # INSCOPE
5409                unless (defined $i) {            unless (defined $i) {
5410                  !!!cp ('t297');              !!!cp ('t297');
5411  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
5412                  !!!parse-error (type => 'unmatched end tag:select');              !!!parse-error (type => 'unmatched end tag:select', token => $token);
5413                  ## Ignore the </select> token              ## Ignore the </select> token
5414                  !!!next-token; ## TODO: ok?              !!!next-token; ## TODO: ok?
5415                  redo B;              redo B;
5416                }            }
5417                                
5418                !!!cp ('t298');            !!!cp ('t298');
5419                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5420    
5421                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5422    
5423                ## reprocess            ## reprocess
5424                redo B;            redo B;
5425          } else {          } else {
5426            !!!cp ('t299');            !!!cp ('t299');
5427            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
5428            ## Ignore the token            ## Ignore the token
5429            !!!next-token;            !!!next-token;
5430            redo B;            redo B;
5431          }          }
5432          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5433            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5434                    @{$self->{open_elements}} == 1) { # redundant, maybe
5435              !!!cp ('t299.1');
5436              !!!parse-error (type => 'in body:#eof', token => $token);
5437            } else {
5438              !!!cp ('t299.2');
5439            }
5440    
5441            ## Stop parsing.
5442            last B;
5443        } else {        } else {
5444          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5445        }        }
# Line 5092  sub _tree_construction_main ($) { Line 5461  sub _tree_construction_main ($) {
5461                    
5462          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5463            !!!cp ('t301');            !!!cp ('t301');
5464            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#character', token => $token);
5465    
5466            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
5467          } else {          } else {
# Line 5100  sub _tree_construction_main ($) { Line 5469  sub _tree_construction_main ($) {
5469          }          }
5470                    
5471          ## "after body" insertion mode          ## "after body" insertion mode
5472          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#character', token => $token);
5473    
5474          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
5475          ## reprocess          ## reprocess
# Line 5108  sub _tree_construction_main ($) { Line 5477  sub _tree_construction_main ($) {
5477        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5478          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5479            !!!cp ('t303');            !!!cp ('t303');
5480            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5481                        
5482            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
5483          } else {          } else {
# Line 5116  sub _tree_construction_main ($) { Line 5485  sub _tree_construction_main ($) {
5485          }          }
5486    
5487          ## "after body" insertion mode          ## "after body" insertion mode
5488          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
5489    
5490          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
5491          ## reprocess          ## reprocess
# Line 5124  sub _tree_construction_main ($) { Line 5493  sub _tree_construction_main ($) {
5493        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5494          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5495            !!!cp ('t305');            !!!cp ('t305');
5496            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
5497                        
5498            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
5499            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
# Line 5136  sub _tree_construction_main ($) { Line 5505  sub _tree_construction_main ($) {
5505          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
5506            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
5507              !!!cp ('t307');              !!!cp ('t307');
5508              !!!parse-error (type => 'unmatched end tag:html');              !!!parse-error (type => 'unmatched end tag:html', token => $token);
5509              ## Ignore the token              ## Ignore the token
5510              !!!next-token;              !!!next-token;
5511              redo B;              redo B;
# Line 5148  sub _tree_construction_main ($) { Line 5517  sub _tree_construction_main ($) {
5517            }            }
5518          } else {          } else {
5519            !!!cp ('t309');            !!!cp ('t309');
5520            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
5521    
5522            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
5523            ## reprocess            ## reprocess
5524            redo B;            redo B;
5525          }          }
5526          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5527            !!!cp ('t309.2');
5528            ## Stop parsing
5529            last B;
5530        } else {        } else {
5531          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5532        }        }
# Line 5172  sub _tree_construction_main ($) { Line 5545  sub _tree_construction_main ($) {
5545          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
5546            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5547              !!!cp ('t311');              !!!cp ('t311');
5548              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#character', token => $token);
5549            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
5550              !!!cp ('t312');              !!!cp ('t312');
5551              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
5552            } else { # "after html frameset"            } else { # "after html frameset"
5553              !!!cp ('t313');              !!!cp ('t313');
5554              !!!parse-error (type => 'after html:#character');              !!!parse-error (type => 'after html:#character', token => $token);
5555    
5556              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5557              ## Reprocess in the "after frameset" insertion mode.              ## Reprocess in the "after frameset" insertion mode.
5558              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
5559            }            }
5560                        
5561            ## Ignore the token.            ## Ignore the token.
# Line 5200  sub _tree_construction_main ($) { Line 5573  sub _tree_construction_main ($) {
5573        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5574          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5575            !!!cp ('t316');            !!!cp ('t316');
5576            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5577    
5578            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
5579            ## Process in the "after frameset" insertion mode.            ## Process in the "after frameset" insertion mode.
# Line 5211  sub _tree_construction_main ($) { Line 5584  sub _tree_construction_main ($) {
5584          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
5585              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
5586            !!!cp ('t318');            !!!cp ('t318');
5587            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5588            !!!next-token;            !!!next-token;
5589            redo B;            redo B;
5590          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
5591                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
5592            !!!cp ('t319');            !!!cp ('t319');
5593            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5594            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
5595            !!!next-token;            !!!next-token;
5596            redo B;            redo B;
5597          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
5598            !!!cp ('t320');            !!!cp ('t320');
5599            ## NOTE: As if in body.            ## NOTE: As if in body.
5600            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL);
5601            redo B;            redo B;
5602          } else {          } else {
5603            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5604              !!!cp ('t321');              !!!cp ('t321');
5605              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
5606            } else {            } else {
5607              !!!cp ('t322');              !!!cp ('t322');
5608              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
5609            }            }
5610            ## Ignore the token            ## Ignore the token
5611            !!!next-token;            !!!next-token;
# Line 5241  sub _tree_construction_main ($) { Line 5614  sub _tree_construction_main ($) {
5614        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5615          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5616            !!!cp ('t323');            !!!cp ('t323');
5617            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
5618    
5619            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
5620            ## Process in the "after frameset" insertion mode.            ## Process in the "after frameset" insertion mode.
# Line 5251  sub _tree_construction_main ($) { Line 5624  sub _tree_construction_main ($) {
5624    
5625          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
5626              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
5627            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5628                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
5629              !!!cp ('t325');              !!!cp ('t325');
5630              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5631              ## Ignore the token              ## Ignore the token
5632              !!!next-token;              !!!next-token;
5633            } else {            } else {
# Line 5264  sub _tree_construction_main ($) { Line 5637  sub _tree_construction_main ($) {
5637            }            }
5638    
5639            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
5640                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
5641              !!!cp ('t327');              !!!cp ('t327');
5642              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5643            } else {            } else {
# Line 5280  sub _tree_construction_main ($) { Line 5653  sub _tree_construction_main ($) {
5653          } else {          } else {
5654            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5655              !!!cp ('t330');              !!!cp ('t330');
5656              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
5657            } else {            } else {
5658              !!!cp ('t331');              !!!cp ('t331');
5659              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
5660            }            }
5661            ## Ignore the token            ## Ignore the token
5662            !!!next-token;            !!!next-token;
5663            redo B;            redo B;
5664          }          }
5665          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5666            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5667                    @{$self->{open_elements}} == 1) { # redundant, maybe
5668              !!!cp ('t331.1');
5669              !!!parse-error (type => 'in body:#eof', token => $token);
5670            } else {
5671              !!!cp ('t331.2');
5672            }
5673            
5674            ## Stop parsing
5675            last B;
5676        } else {        } else {
5677          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5678        }        }
# Line 5303  sub _tree_construction_main ($) { Line 5687  sub _tree_construction_main ($) {
5687        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
5688          !!!cp ('t332');          !!!cp ('t332');
5689          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5690          $script_start_tag->($insert);          $script_start_tag->();
5691          redo B;          redo B;
5692        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
5693          !!!cp ('t333');          !!!cp ('t333');
5694          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5695          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
5696          redo B;          redo B;
5697        } elsif ({        } elsif ({
5698                  base => 1, link => 1,                  base => 1, link => 1,
5699                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5700          !!!cp ('t334');          !!!cp ('t334');
5701          ## NOTE: This is an "as if in head" code clone, only "-t" differs          ## NOTE: This is an "as if in head" code clone, only "-t" differs
5702          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5703          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
5704          !!!next-token;          !!!next-token;
5705          redo B;          redo B;
5706        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
5707          ## NOTE: This is an "as if in head" code clone, only "-t" differs          ## NOTE: This is an "as if in head" code clone, only "-t" differs
5708          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5709          my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
5710    
5711          unless ($self->{confident}) {          unless ($self->{confident}) {
5712            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) { ## TODO: And if supported
5713              !!!cp ('t335');              !!!cp ('t335');
5714              $self->{change_encoding}              $self->{change_encoding}
5715                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
5716                            
5717              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5718                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
# Line 5343  sub _tree_construction_main ($) { Line 5727  sub _tree_construction_main ($) {
5727                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
5728                !!!cp ('t336');                !!!cp ('t336');
5729                $self->{change_encoding}                $self->{change_encoding}
5730                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
5731                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5732                    ->set_user_data (manakai_has_reference =>                    ->set_user_data (manakai_has_reference =>
5733                                         $token->{attributes}->{content}                                         $token->{attributes}->{content}
# Line 5371  sub _tree_construction_main ($) { Line 5755  sub _tree_construction_main ($) {
5755          redo B;          redo B;
5756        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
5757          !!!cp ('t341');          !!!cp ('t341');
         !!!parse-error (type => 'in body:title');  
5758          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5759          $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]);  
           }  
         });  
5760          redo B;          redo B;
5761        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
5762          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body', token => $token);
5763                                
5764          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
5765              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
5766            !!!cp ('t342');            !!!cp ('t342');
5767            ## Ignore the token            ## Ignore the token
5768          } else {          } else {
# Line 5407  sub _tree_construction_main ($) { Line 5782  sub _tree_construction_main ($) {
5782                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
5783                  div => 1, dl => 1, fieldset => 1,                  div => 1, dl => 1, fieldset => 1,
5784                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5785                  listing => 1, menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
5786                  pre => 1,                  pre => 1, listing => 1,
5787                    form => 1,
5788                    table => 1,
5789                    hr => 1,
5790                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5791            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
5792              !!!cp ('t350');
5793              !!!parse-error (type => 'in form:form', token => $token);
5794              ## Ignore the token
5795              !!!next-token;
5796              redo B;
5797            }
5798    
5799          ## has a p element in scope          ## has a p element in scope
5800          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5801            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
5802              !!!cp ('t344');              !!!cp ('t344');
5803              !!!back-token;              !!!back-token;
5804              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
5805                          line => $token->{line}, column => $token->{column}};
5806              redo B;              redo B;
5807            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
5808              !!!cp ('t345');              !!!cp ('t345');
5809              last INSCOPE;              last INSCOPE;
5810            }            }
5811          } # INSCOPE          } # INSCOPE
5812                        
5813          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5814          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
5815            !!!next-token;            !!!next-token;
5816            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
5817              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5440  sub _tree_construction_main ($) { Line 5824  sub _tree_construction_main ($) {
5824            } else {            } else {
5825              !!!cp ('t348');              !!!cp ('t348');
5826            }            }
5827          } else {          } elsif ($token->{tag_name} eq 'form') {
5828            !!!cp ('t347');            !!!cp ('t347.1');
5829              $self->{form_element} = $self->{open_elements}->[-1]->[0];
5830    
5831            !!!next-token;            !!!next-token;
5832          }          } elsif ($token->{tag_name} eq 'table') {
5833          redo B;            !!!cp ('t382');
5834        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
5835          if (defined $self->{form_element}) {            
5836            !!!cp ('t350');            $self->{insertion_mode} = IN_TABLE_IM;
5837            !!!parse-error (type => 'in form:form');  
5838            ## Ignore the token            !!!next-token;
5839            } elsif ($token->{tag_name} eq 'hr') {
5840              !!!cp ('t386');
5841              pop @{$self->{open_elements}};
5842            
5843            !!!next-token;            !!!next-token;
           redo B;  
5844          } else {          } else {
5845            ## 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];  
5846            !!!next-token;            !!!next-token;
           redo B;  
5847          }          }
5848        } elsif ($token->{tag_name} eq 'li') {          redo B;
5849          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
5850          ## has a p element in scope          ## has a p element in scope
5851          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5852            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
5853              !!!cp ('t353');              !!!cp ('t353');
5854              !!!back-token;              !!!back-token;
5855              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
5856                          line => $token->{line}, column => $token->{column}};
5857              redo B;              redo B;
5858            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
5859              !!!cp ('t354');              !!!cp ('t354');
5860              last INSCOPE;              last INSCOPE;
5861            }            }
# Line 5494  sub _tree_construction_main ($) { Line 5864  sub _tree_construction_main ($) {
5864          ## Step 1          ## Step 1
5865          my $i = -1;          my $i = -1;
5866          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
5867            my $li_or_dtdd = {li => {li => 1},
5868                              dt => {dt => 1, dd => 1},
5869                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
5870          LI: {          LI: {
5871            ## Step 2            ## Step 2
5872            if ($node->[1] eq 'li') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
5873              if ($i != -1) {              if ($i != -1) {
5874                !!!cp ('t355');                !!!cp ('t355');
5875                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
5876                                $self->{open_elements}->[-1]->[1]);                                value => $self->{open_elements}->[-1]->[0]
5877                                      ->manakai_local_name,
5878                                  token => $token);
5879              } else {              } else {
5880                !!!cp ('t356');                !!!cp ('t356');
5881              }              }
# Line 5511  sub _tree_construction_main ($) { Line 5886  sub _tree_construction_main ($) {
5886            }            }
5887                        
5888            ## Step 3            ## Step 3
5889            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
5890                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
5891                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
5892                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
5893                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
5894                  not ($node->[1] & DIV_EL)) {
5895              !!!cp ('t358');              !!!cp ('t358');
5896              last LI;              last LI;
5897            }            }
# Line 5527  sub _tree_construction_main ($) { Line 5903  sub _tree_construction_main ($) {
5903            redo LI;            redo LI;
5904          } # LI          } # LI
5905                        
5906          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t360');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t361');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!cp ('t362');  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             } else {  
               !!!cp ('t363');  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           } else {  
             !!!cp ('t364');  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             !!!cp ('t365');  
             last LI;  
           }  
             
           !!!cp ('t366');  
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
5907          !!!next-token;          !!!next-token;
5908          redo B;          redo B;
5909        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
5910          ## has a p element in scope          ## has a p element in scope
5911          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5912            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
5913              !!!cp ('t367');              !!!cp ('t367');
5914              !!!back-token;              !!!back-token;
5915              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
5916                          line => $token->{line}, column => $token->{column}};
5917              redo B;              redo B;
5918            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
5919              !!!cp ('t368');              !!!cp ('t368');
5920              last INSCOPE;              last INSCOPE;
5921            }            }
5922          } # INSCOPE          } # INSCOPE
5923                        
5924          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5925                        
5926          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
5927                        
# Line 5612  sub _tree_construction_main ($) { Line 5930  sub _tree_construction_main ($) {
5930        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
5931          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
5932            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
5933            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
5934              !!!cp ('t371');              !!!cp ('t371');
5935              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a', token => $token);
5936                            
5937              !!!back-token;              !!!back-token;
5938              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
5939              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
5940                $formatting_end_tag->($token);
5941                            
5942              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
5943                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
# Line 5643  sub _tree_construction_main ($) { Line 5962  sub _tree_construction_main ($) {
5962                        
5963          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5964    
5965          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5966          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
5967    
5968          !!!next-token;          !!!next-token;
5969          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;  
5970        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
5971          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5972    
5973          ## has a |nobr| element in scope          ## has a |nobr| element in scope
5974          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5975            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
5976            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
5977              !!!cp ('t376');              !!!cp ('t376');
5978              !!!parse-error (type => 'in nobr:nobr');              !!!parse-error (type => 'in nobr:nobr', token => $token);
5979              !!!back-token;              !!!back-token;
5980              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
5981                          line => $token->{line}, column => $token->{column}};
5982              redo B;              redo B;
5983            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
5984              !!!cp ('t377');              !!!cp ('t377');
5985              last INSCOPE;              last INSCOPE;
5986            }            }
5987          } # INSCOPE          } # INSCOPE
5988                    
5989          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5990          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
5991                    
5992          !!!next-token;          !!!next-token;
# Line 5691  sub _tree_construction_main ($) { Line 5995  sub _tree_construction_main ($) {
5995          ## has a button element in scope          ## has a button element in scope
5996          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5997            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
5998            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
5999              !!!cp ('t378');              !!!cp ('t378');
6000              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button', token => $token);
6001              !!!back-token;              !!!back-token;
6002              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6003                          line => $token->{line}, column => $token->{column}};
6004              redo B;              redo B;
6005            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6006              !!!cp ('t379');              !!!cp ('t379');
6007              last INSCOPE;              last INSCOPE;
6008            }            }
# Line 5708  sub _tree_construction_main ($) { Line 6010  sub _tree_construction_main ($) {
6010                        
6011          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6012                        
6013          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6014    
6015          ## TODO: associate with $self->{form_element} if defined          ## TODO: associate with $self->{form_element} if defined
6016    
# Line 5716  sub _tree_construction_main ($) { Line 6018  sub _tree_construction_main ($) {
6018    
6019          !!!next-token;          !!!next-token;
6020          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;  
6021        } elsif ({        } elsif ({
6022                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
6023                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
6024                  image => 1,                  noembed => 1,
6025                    noframes => 1,
6026                    noscript => 0, ## TODO: 1 if scripting is enabled
6027                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6028          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
6029            !!!cp ('t384');            !!!cp ('t381');
6030            !!!parse-error (type => 'image');            $reconstruct_active_formatting_elements->($insert_to_current);
           $token->{tag_name} = 'img';  
6031          } else {          } else {
6032            !!!cp ('t385');            !!!cp ('t399');
6033          }          }
6034            ## NOTE: There is an "as if in body" code clone.
6035          ## 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;  
6036          redo B;          redo B;
6037        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6038          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
6039                    
6040          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6041            !!!cp ('t389');            !!!cp ('t389');
# Line 5825  sub _tree_construction_main ($) { Line 6052  sub _tree_construction_main ($) {
6052            delete $at->{prompt};            delete $at->{prompt};
6053            my @tokens = (            my @tokens = (
6054                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
6055                           attributes => $form_attrs},                           attributes => $form_attrs,
6056                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
6057                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
6058                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
6059                            {type => START_TAG_TOKEN, tag_name => 'p',
6060                             line => $token->{line}, column => $token->{column}},
6061                            {type => START_TAG_TOKEN, tag_name => 'label',
6062                             line => $token->{line}, column => $token->{column}},
6063                         );                         );
6064            if ($prompt_attr) {            if ($prompt_attr) {
6065              !!!cp ('t390');              !!!cp ('t390');
6066              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6067                               #line => $token->{line}, column => $token->{column},
6068                              };
6069            } else {            } else {
6070              !!!cp ('t391');              !!!cp ('t391');
6071              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
6072                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
6073                               #line => $token->{line}, column => $token->{column},
6074                              }; # SHOULD
6075              ## TODO: make this configurable              ## TODO: make this configurable
6076            }            }
6077            push @tokens,            push @tokens,
6078                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6079                             line => $token->{line}, column => $token->{column}},
6080                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6081                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
6082                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
6083                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
6084                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
6085                            {type => START_TAG_TOKEN, tag_name => 'hr',
6086                             line => $token->{line}, column => $token->{column}},
6087                            {type => END_TAG_TOKEN, tag_name => 'form',
6088                             line => $token->{line}, column => $token->{column}};
6089            $token = shift @tokens;            $token = shift @tokens;
6090            !!!back-token (@tokens);            !!!back-token (@tokens);
6091            redo B;            redo B;
# Line 5853  sub _tree_construction_main ($) { Line 6093  sub _tree_construction_main ($) {
6093        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6094          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6095          my $el;          my $el;
6096          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $token->{tag_name}, $token->{attributes}, $token);
6097                    
6098          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6099          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5892  sub _tree_construction_main ($) { Line 6132  sub _tree_construction_main ($) {
6132            ## Ignore the token            ## Ignore the token
6133          } else {          } else {
6134            !!!cp ('t398');            !!!cp ('t398');
6135            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6136          }          }
6137          !!!next-token;          !!!next-token;
6138          redo B;          redo B;
6139        } 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 ({  
6140                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6141                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
6142                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
6143                  thead => 1, tr => 1,                  thead => 1, tr => 1,
6144                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6145          !!!cp ('t401');          !!!cp ('t401');
6146          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6147          ## Ignore the token          ## Ignore the token
6148          !!!next-token;          !!!next-token;
6149          redo B;          redo B;
6150                    
6151          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6152        } else {        } else {
6153          !!!cp ('t402');          if ($token->{tag_name} eq 'image') {
6154              !!!cp ('t384');
6155              !!!parse-error (type => 'image', token => $token);
6156              $token->{tag_name} = 'img';
6157            } else {
6158              !!!cp ('t385');
6159            }
6160    
6161            ## NOTE: There is an "as if <br>" code clone.
6162          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6163                    
6164          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6165    
6166            if ({
6167                 applet => 1, marquee => 1, object => 1,
6168                }->{$token->{tag_name}}) {
6169              !!!cp ('t380');
6170              push @$active_formatting_elements, ['#marker', ''];
6171            } elsif ({
6172                      b => 1, big => 1, em => 1, font => 1, i => 1,
6173                      s => 1, small => 1, strile => 1,
6174                      strong => 1, tt => 1, u => 1,
6175                     }->{$token->{tag_name}}) {
6176              !!!cp ('t375');
6177              push @$active_formatting_elements, $self->{open_elements}->[-1];
6178            } elsif ($token->{tag_name} eq 'input') {
6179              !!!cp ('t388');
6180              ## TODO: associate with $self->{form_element} if defined
6181              pop @{$self->{open_elements}};
6182            } elsif ({
6183                      area => 1, basefont => 1, bgsound => 1, br => 1,
6184                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6185                      #image => 1,
6186                     }->{$token->{tag_name}}) {
6187              !!!cp ('t388.1');
6188              pop @{$self->{open_elements}};
6189            } elsif ($token->{tag_name} eq 'select') {
6190              ## TODO: associate with $self->{form_element} if defined
6191            
6192              if ($self->{insertion_mode} & TABLE_IMS or
6193                  $self->{insertion_mode} & BODY_TABLE_IMS or
6194                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6195                !!!cp ('t400.1');
6196                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6197              } else {
6198                !!!cp ('t400.2');
6199                $self->{insertion_mode} = IN_SELECT_IM;
6200              }
6201            } else {
6202              !!!cp ('t402');
6203            }
6204                    
6205          !!!next-token;          !!!next-token;
6206          redo B;          redo B;
6207        }        }
6208      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6209        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
6210          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
6211              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
6212            for (@{$self->{open_elements}}) {          INSCOPE: {
6213              unless ({            for (reverse @{$self->{open_elements}}) {
6214                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
6215                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
6216                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
6217                      }->{$_->[1]}) {                last INSCOPE;
6218                !!!cp ('t403');              } elsif ($_->[1] & SCOPING_EL) {
6219                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!cp ('t405.1');
6220              } else {                last;
               !!!cp ('t404');  
6221              }              }
6222            }            }
6223    
6224            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
6225            !!!next-token;                            value => $token->{tag_name}, token => $token);
6226            redo B;            ## NOTE: Ignore the token.
         } else {  
           !!!cp ('t405');  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
6227            !!!next-token;            !!!next-token;
6228            redo B;            redo B;
6229            } # INSCOPE
6230    
6231            for (@{$self->{open_elements}}) {
6232              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
6233                !!!cp ('t403');
6234                !!!parse-error (type => 'not closed',
6235                                value => $_->[0]->manakai_local_name,
6236                                token => $token);
6237                last;
6238              } else {
6239                !!!cp ('t404');
6240              }
6241          }          }
6242    
6243            $self->{insertion_mode} = AFTER_BODY_IM;
6244            !!!next-token;
6245            redo B;
6246        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
6247          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {          ## TODO: Update this code.  It seems that the code below is not
6248            ## up-to-date, though it has same effect as speced.
6249            if (@{$self->{open_elements}} > 1 and
6250                $self->{open_elements}->[1]->[1] & BODY_EL) {
6251            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6252            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6253              !!!cp ('t406');              !!!cp ('t406');
6254              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed',
6255                                value => $self->{open_elements}->[1]->[0]
6256                                    ->manakai_local_name,
6257                                token => $token);
6258            } else {            } else {
6259              !!!cp ('t407');              !!!cp ('t407');
6260            }            }
# Line 5980  sub _tree_construction_main ($) { Line 6263  sub _tree_construction_main ($) {
6263            redo B;            redo B;
6264          } else {          } else {
6265            !!!cp ('t408');            !!!cp ('t408');
6266            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6267            ## Ignore the token            ## Ignore the token
6268            !!!next-token;            !!!next-token;
6269            redo B;            redo B;
# Line 5990  sub _tree_construction_main ($) { Line 6273  sub _tree_construction_main ($) {
6273                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
6274                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
6275                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
6276                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
6277                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6278          ## has an element in scope          ## has an element in scope
6279          my $i;          my $i;
6280          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6281            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6282            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6283              !!!cp ('t410');              !!!cp ('t410');
6284              $i = $_;              $i = $_;
6285              last INSCOPE;              last INSCOPE;
6286            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6287              !!!cp ('t411');              !!!cp ('t411');
6288              last INSCOPE;              last INSCOPE;
6289            }            }
# Line 6011  sub _tree_construction_main ($) { Line 6291  sub _tree_construction_main ($) {
6291    
6292          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
6293            !!!cp ('t413');            !!!cp ('t413');
6294            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6295          } else {          } else {
6296            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
6297            while ({            while ({
# Line 6019  sub _tree_construction_main ($) { Line 6299  sub _tree_construction_main ($) {
6299                    dt => ($token->{tag_name} ne 'dt'),                    dt => ($token->{tag_name} ne 'dt'),
6300                    li => ($token->{tag_name} ne 'li'),                    li => ($token->{tag_name} ne 'li'),
6301                    p => 1,                    p => 1,
6302                   }->{$self->{open_elements}->[-1]->[1]}) {                   }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6303              !!!cp ('t409');              !!!cp ('t409');
6304              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6305            }            }
6306    
6307            ## Step 2.            ## Step 2.
6308            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6309                      ne $token->{tag_name}) {
6310              !!!cp ('t412');              !!!cp ('t412');
6311              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
6312                                value => $self->{open_elements}->[-1]->[0]
6313                                    ->manakai_local_name,
6314                                token => $token);
6315            } else {            } else {
6316              !!!cp ('t414');              !!!cp ('t414');
6317            }            }
# Line 6038  sub _tree_construction_main ($) { Line 6322  sub _tree_construction_main ($) {
6322            ## Step 4.            ## Step 4.
6323            $clear_up_to_marker->()            $clear_up_to_marker->()
6324                if {                if {
6325                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
6326                }->{$token->{tag_name}};                }->{$token->{tag_name}};
6327          }          }
6328          !!!next-token;          !!!next-token;
# Line 6050  sub _tree_construction_main ($) { Line 6334  sub _tree_construction_main ($) {
6334          my $i;          my $i;
6335          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6336            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6337            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
6338              !!!cp ('t418');              !!!cp ('t418');
6339              $i = $_;              $i = $_;
6340              last INSCOPE;              last INSCOPE;
6341            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6342              !!!cp ('t419');              !!!cp ('t419');
6343              last INSCOPE;              last INSCOPE;
6344            }            }
# Line 6065  sub _tree_construction_main ($) { Line 6346  sub _tree_construction_main ($) {
6346    
6347          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
6348            !!!cp ('t421');            !!!cp ('t421');
6349            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6350          } else {          } else {
6351            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
6352            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
6353              !!!cp ('t417');              !!!cp ('t417');
6354              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6355            }            }
6356                        
6357            ## Step 2.            ## Step 2.
6358            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6359                      ne $token->{tag_name}) {
6360              !!!cp ('t417.1');              !!!cp ('t417.1');
6361              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
6362                                value => $self->{open_elements}->[-1]->[0]
6363                                    ->manakai_local_name,
6364                                token => $token);
6365            } else {            } else {
6366              !!!cp ('t420');              !!!cp ('t420');
6367            }              }  
# Line 6096  sub _tree_construction_main ($) { Line 6379  sub _tree_construction_main ($) {
6379          my $i;          my $i;
6380          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6381            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6382            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
6383              !!!cp ('t423');              !!!cp ('t423');
6384              $i = $_;              $i = $_;
6385              last INSCOPE;              last INSCOPE;
6386            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6387              !!!cp ('t424');              !!!cp ('t424');
6388              last INSCOPE;              last INSCOPE;
6389            }            }
# Line 6113  sub _tree_construction_main ($) { Line 6391  sub _tree_construction_main ($) {
6391    
6392          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
6393            !!!cp ('t425.1');            !!!cp ('t425.1');
6394            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6395          } else {          } else {
6396            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
6397            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
6398              !!!cp ('t422');              !!!cp ('t422');
6399              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6400            }            }
6401                        
6402            ## Step 2.            ## Step 2.
6403            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6404                      ne $token->{tag_name}) {
6405              !!!cp ('t425');              !!!cp ('t425');
6406              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6407            } else {            } else {
6408              !!!cp ('t426');              !!!cp ('t426');
6409            }            }
# Line 6142  sub _tree_construction_main ($) { Line 6419  sub _tree_construction_main ($) {
6419          my $i;          my $i;
6420          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6421            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6422            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & P_EL) {
6423              !!!cp ('t410.1');              !!!cp ('t410.1');
6424              $i = $_;              $i = $_;
6425              last INSCOPE;              last INSCOPE;
6426            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6427              !!!cp ('t411.1');              !!!cp ('t411.1');
6428              last INSCOPE;              last INSCOPE;
6429            }            }
6430          } # INSCOPE          } # INSCOPE
6431    
6432          if (defined $i) {          if (defined $i) {
6433            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6434                      ne $token->{tag_name}) {
6435              !!!cp ('t412.1');              !!!cp ('t412.1');
6436              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
6437                                value => $self->{open_elements}->[-1]->[0]
6438                                    ->manakai_local_name,
6439                                token => $token);
6440            } else {            } else {
6441              !!!cp ('t414.1');              !!!cp ('t414.1');
6442            }            }
# Line 6166  sub _tree_construction_main ($) { Line 6444  sub _tree_construction_main ($) {
6444            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6445          } else {          } else {
6446            !!!cp ('t413.1');            !!!cp ('t413.1');
6447            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6448    
6449            !!!cp ('t415.1');            !!!cp ('t415.1');
6450            ## As if <p>, then reprocess the current token            ## As if <p>, then reprocess the current token
6451            my $el;            my $el;
6452            !!!create-element ($el, 'p');            !!!create-element ($el, 'p',, $token);
6453            $insert->($el);            $insert->($el);
6454            ## NOTE: Not inserted into |$self->{open_elements}|.            ## NOTE: Not inserted into |$self->{open_elements}|.
6455          }          }
# Line 6185  sub _tree_construction_main ($) { Line 6463  sub _tree_construction_main ($) {
6463                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
6464                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6465          !!!cp ('t427');          !!!cp ('t427');
6466          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token);
6467          redo B;          redo B;
6468        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
6469          !!!cp ('t428');          !!!cp ('t428');
6470          !!!parse-error (type => 'unmatched end tag:br');          !!!parse-error (type => 'unmatched end tag:br', token => $token);
6471    
6472          ## As if <br>          ## As if <br>
6473          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6474                    
6475          my $el;          my $el;
6476          !!!create-element ($el, 'br');          !!!create-element ($el, 'br',, $token);
6477          $insert->($el);          $insert->($el);
6478                    
6479          ## Ignore the token.          ## Ignore the token.
# Line 6214  sub _tree_construction_main ($) { Line 6492  sub _tree_construction_main ($) {
6492                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
6493                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6494          !!!cp ('t429');          !!!cp ('t429');
6495          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6496          ## Ignore the token          ## Ignore the token
6497          !!!next-token;          !!!next-token;
6498          redo B;          redo B;
# Line 6228  sub _tree_construction_main ($) { Line 6506  sub _tree_construction_main ($) {
6506    
6507          ## Step 2          ## Step 2
6508          S2: {          S2: {
6509            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6510              ## Step 1              ## Step 1
6511              ## generate implied end tags              ## generate implied end tags
6512              while ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
6513                !!!cp ('t430');                !!!cp ('t430');
6514                ## ISSUE: Can this case be reached?                ## ISSUE: Can this case be reached?
6515                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
6516              }              }
6517                    
6518              ## Step 2              ## Step 2
6519              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6520                        ne $token->{tag_name}) {
6521                !!!cp ('t431');                !!!cp ('t431');
6522                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
6523                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
6524                                  value => $self->{open_elements}->[-1]->[0]
6525                                      ->manakai_local_name,
6526                                  token => $token);
6527              } else {              } else {
6528                !!!cp ('t432');                !!!cp ('t432');
6529              }              }
# Line 6255  sub _tree_construction_main ($) { Line 6535  sub _tree_construction_main ($) {
6535              last S2;              last S2;
6536            } else {            } else {
6537              ## Step 3              ## Step 3
6538              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
6539                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
6540                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
6541                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
6542                !!!cp ('t433');                !!!cp ('t433');
6543                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6544                ## Ignore the token                ## Ignore the token
6545                !!!next-token;                !!!next-token;
6546                last S2;                last S2;
# Line 6325  sub set_inner_html ($$$) { Line 6605  sub set_inner_html ($$$) {
6605    
6606      ## Step 8 # MUST      ## Step 8 # MUST
6607      my $i = 0;      my $i = 0;
6608      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
6609      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
6610      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
6611        my $self = shift;        my $self = shift;
6612    
# Line 6335  sub set_inner_html ($$$) { Line 6615  sub set_inner_html ($$$) {
6615    
6616        $self->{next_char} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
6617        $self->{next_char} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
6618        $column++;  
6619          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
6620          $p->{column}++;
6621    
6622        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
6623          $line++;          $p->{line}++;
6624          $column = 0;          $p->{column} = 0;
6625          !!!cp ('i1');          !!!cp ('i1');
6626        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
6627          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
6628          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
6629          $line++;          $p->{line}++;
6630          $column = 0;          $p->{column} = 0;
6631          !!!cp ('i2');          !!!cp ('i2');
6632        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
6633          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
# Line 6361  sub set_inner_html ($$$) { Line 6643  sub set_inner_html ($$$) {
6643            
6644      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
6645        my (%opt) = @_;        my (%opt) = @_;
6646        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
6647          my $column = $opt{column};
6648          if (defined $opt{token} and defined $opt{token}->{line}) {
6649            $line = $opt{token}->{line};
6650            $column = $opt{token}->{column};
6651          }
6652          warn "Parse error ($opt{type}) at line $line column $column\n";
6653      };      };
6654      $p->{parse_error} = sub {      $p->{parse_error} = sub {
6655        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
6656      };      };
6657            
6658      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
# Line 6388  sub set_inner_html ($$$) { Line 6676  sub set_inner_html ($$$) {
6676          unless defined $p->{content_model};          unless defined $p->{content_model};
6677          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
6678    
6679      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
6680          ## TODO: Foreign element OK?
6681    
6682      ## Step 3      ## Step 3
6683      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6398  sub set_inner_html ($$$) { Line 6687  sub set_inner_html ($$$) {
6687      $doc->append_child ($root);      $doc->append_child ($root);
6688    
6689      ## Step 5 # MUST      ## Step 5 # MUST
6690      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
6691    
6692      undef $p->{head_element};      undef $p->{head_element};
6693    
# Line 6444  sub set_inner_html ($$$) { Line 6733  sub set_inner_html ($$$) {
6733      ## ISSUE: mutation events?      ## ISSUE: mutation events?
6734    
6735      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
6736    
6737        delete $p->{parse_error}; # delete loop
6738    } else {    } else {
6739      die "$0: |set_inner_html| is not defined for node of type $nt";      die "$0: |set_inner_html| is not defined for node of type $nt";
6740    }    }

Legend:
Removed from v.1.93  
changed lines
  Added in v.1.123

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24