/[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.84 by wakaba, Thu Mar 6 15:23:17 2008 UTC revision 1.126 by wakaba, Sat Apr 12 14:54:33 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  my $permitted_slash_tag_name = {  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
16    base => 1,  my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
17    link => 1,  my $SVG_NS = q<http://www.w3.org/2000/svg>;
18    meta => 1,  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
19    hr => 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
20    br => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
21    img => 1,  
22    embed => 1,  sub A_EL () { 0b1 }
23    param => 1,  sub ADDRESS_EL () { 0b10 }
24    area => 1,  sub BODY_EL () { 0b100 }
25    col => 1,  sub BUTTON_EL () { 0b1000 }
26    input => 1,  sub CAPTION_EL () { 0b10000 }
27    sub DD_EL () { 0b100000 }
28    sub DIV_EL () { 0b1000000 }
29    sub DT_EL () { 0b10000000 }
30    sub FORM_EL () { 0b100000000 }
31    sub FORMATTING_EL () { 0b1000000000 }
32    sub FRAMESET_EL () { 0b10000000000 }
33    sub HEADING_EL () { 0b100000000000 }
34    sub HTML_EL () { 0b1000000000000 }
35    sub LI_EL () { 0b10000000000000 }
36    sub NOBR_EL () { 0b100000000000000 }
37    sub OPTION_EL () { 0b1000000000000000 }
38    sub OPTGROUP_EL () { 0b10000000000000000 }
39    sub P_EL () { 0b100000000000000000 }
40    sub SELECT_EL () { 0b1000000000000000000 }
41    sub TABLE_EL () { 0b10000000000000000000 }
42    sub TABLE_CELL_EL () { 0b100000000000000000000 }
43    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
44    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
45    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
46    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
47    sub FOREIGN_EL () { 0b10000000000000000000000000 }
48    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
49    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
50    
51    sub TABLE_ROWS_EL () {
52      TABLE_EL |
53      TABLE_ROW_EL |
54      TABLE_ROW_GROUP_EL
55    }
56    
57    sub END_TAG_OPTIONAL_EL () {
58      DD_EL |
59      DT_EL |
60      LI_EL |
61      P_EL
62    }
63    
64    sub ALL_END_TAG_OPTIONAL_EL () {
65      END_TAG_OPTIONAL_EL |
66      BODY_EL |
67      HTML_EL |
68      TABLE_CELL_EL |
69      TABLE_ROW_EL |
70      TABLE_ROW_GROUP_EL
71    }
72    
73    sub SCOPING_EL () {
74      BUTTON_EL |
75      CAPTION_EL |
76      HTML_EL |
77      TABLE_EL |
78      TABLE_CELL_EL |
79      MISC_SCOPING_EL
80    }
81    
82    sub TABLE_SCOPING_EL () {
83      HTML_EL |
84      TABLE_EL
85    }
86    
87    sub TABLE_ROWS_SCOPING_EL () {
88      HTML_EL |
89      TABLE_ROW_GROUP_EL
90    }
91    
92    sub TABLE_ROW_SCOPING_EL () {
93      HTML_EL |
94      TABLE_ROW_EL
95    }
96    
97    sub SPECIAL_EL () {
98      ADDRESS_EL |
99      BODY_EL |
100      DIV_EL |
101      END_TAG_OPTIONAL_EL |
102      FORM_EL |
103      FRAMESET_EL |
104      HEADING_EL |
105      OPTION_EL |
106      OPTGROUP_EL |
107      SELECT_EL |
108      TABLE_ROW_EL |
109      TABLE_ROW_GROUP_EL |
110      MISC_SPECIAL_EL
111    }
112    
113    my $el_category = {
114      a => A_EL | FORMATTING_EL,
115      address => ADDRESS_EL,
116      applet => MISC_SCOPING_EL,
117      area => MISC_SPECIAL_EL,
118      b => FORMATTING_EL,
119      base => MISC_SPECIAL_EL,
120      basefont => MISC_SPECIAL_EL,
121      bgsound => MISC_SPECIAL_EL,
122      big => FORMATTING_EL,
123      blockquote => MISC_SPECIAL_EL,
124      body => BODY_EL,
125      br => MISC_SPECIAL_EL,
126      button => BUTTON_EL,
127      caption => CAPTION_EL,
128      center => MISC_SPECIAL_EL,
129      col => MISC_SPECIAL_EL,
130      colgroup => MISC_SPECIAL_EL,
131      dd => DD_EL,
132      dir => MISC_SPECIAL_EL,
133      div => DIV_EL,
134      dl => MISC_SPECIAL_EL,
135      dt => DT_EL,
136      em => FORMATTING_EL,
137      embed => MISC_SPECIAL_EL,
138      fieldset => MISC_SPECIAL_EL,
139      font => FORMATTING_EL,
140      form => FORM_EL,
141      frame => MISC_SPECIAL_EL,
142      frameset => FRAMESET_EL,
143      h1 => HEADING_EL,
144      h2 => HEADING_EL,
145      h3 => HEADING_EL,
146      h4 => HEADING_EL,
147      h5 => HEADING_EL,
148      h6 => HEADING_EL,
149      head => MISC_SPECIAL_EL,
150      hr => MISC_SPECIAL_EL,
151      html => HTML_EL,
152      i => FORMATTING_EL,
153      iframe => MISC_SPECIAL_EL,
154      img => MISC_SPECIAL_EL,
155      input => MISC_SPECIAL_EL,
156      isindex => MISC_SPECIAL_EL,
157      li => LI_EL,
158      link => MISC_SPECIAL_EL,
159      listing => MISC_SPECIAL_EL,
160      marquee => MISC_SCOPING_EL,
161      menu => MISC_SPECIAL_EL,
162      meta => MISC_SPECIAL_EL,
163      nobr => NOBR_EL | FORMATTING_EL,
164      noembed => MISC_SPECIAL_EL,
165      noframes => MISC_SPECIAL_EL,
166      noscript => MISC_SPECIAL_EL,
167      object => MISC_SCOPING_EL,
168      ol => MISC_SPECIAL_EL,
169      optgroup => OPTGROUP_EL,
170      option => OPTION_EL,
171      p => P_EL,
172      param => MISC_SPECIAL_EL,
173      plaintext => MISC_SPECIAL_EL,
174      pre => MISC_SPECIAL_EL,
175      s => FORMATTING_EL,
176      script => MISC_SPECIAL_EL,
177      select => SELECT_EL,
178      small => FORMATTING_EL,
179      spacer => MISC_SPECIAL_EL,
180      strike => FORMATTING_EL,
181      strong => FORMATTING_EL,
182      style => MISC_SPECIAL_EL,
183      table => TABLE_EL,
184      tbody => TABLE_ROW_GROUP_EL,
185      td => TABLE_CELL_EL,
186      textarea => MISC_SPECIAL_EL,
187      tfoot => TABLE_ROW_GROUP_EL,
188      th => TABLE_CELL_EL,
189      thead => TABLE_ROW_GROUP_EL,
190      title => MISC_SPECIAL_EL,
191      tr => TABLE_ROW_EL,
192      tt => FORMATTING_EL,
193      u => FORMATTING_EL,
194      ul => MISC_SPECIAL_EL,
195      wbr => MISC_SPECIAL_EL,
196    };
197    
198    my $el_category_f = {
199      $MML_NS => {
200        'annotation-xml' => MML_AXML_EL,
201        mi => FOREIGN_FLOW_CONTENT_EL,
202        mo => FOREIGN_FLOW_CONTENT_EL,
203        mn => FOREIGN_FLOW_CONTENT_EL,
204        ms => FOREIGN_FLOW_CONTENT_EL,
205        mtext => FOREIGN_FLOW_CONTENT_EL,
206      },
207      $SVG_NS => {
208        foreignobject => FOREIGN_FLOW_CONTENT_EL, ## TODO: case
209        desc => FOREIGN_FLOW_CONTENT_EL,
210        title => FOREIGN_FLOW_CONTENT_EL,
211      },
212      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
213  };  };
214    
215  my $c1_entity_char = {  my $c1_entity_char = {
# Line 61  my $c1_entity_char = { Line 247  my $c1_entity_char = {
247    0x9F => 0x0178,    0x9F => 0x0178,
248  }; # $c1_entity_char  }; # $c1_entity_char
249    
 my $special_category = {  
   address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,  
   blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,  
   dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,  
   form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,  
   h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  
   img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
   menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  
   ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,  
   pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,  
   textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,  
 };  
 my $scoping_category = {  
   button => 1, caption => 1, html => 1, marquee => 1, object => 1,  
   table => 1, td => 1, th => 1,  
 };  
 my $formatting_category = {  
   a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  
   s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
 };  
 # $phrasing_category: all other elements  
   
250  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
251    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
252    my $charset = shift;    my $charset = shift;
# Line 108  sub parse_byte_string ($$$$;$) { Line 272  sub parse_byte_string ($$$$;$) {
272    $self->{change_encoding} = sub {    $self->{change_encoding} = sub {
273      my $self = shift;      my $self = shift;
274      my $charset = lc shift;      my $charset = lc shift;
275        my $token = shift;
276      ## TODO: if $charset is supported      ## TODO: if $charset is supported
277      ## TODO: normalize charset name      ## TODO: normalize charset name
278    
# Line 126  sub parse_byte_string ($$$$;$) { Line 291  sub parse_byte_string ($$$$;$) {
291      }      }
292    
293      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
294          ':'.$charset, level => 'w');          ':'.$charset, level => 'w', token => $token);
295    
296      ## Step 3      ## Step 3
297      # if (can) {      # if (can) {
# Line 177  sub parse_string ($$$;$) { Line 342  sub parse_string ($$$;$) {
342        if defined $self->{input_encoding};        if defined $self->{input_encoding};
343    
344    my $i = 0;    my $i = 0;
345    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
346    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
347    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
348      my $self = shift;      my $self = shift;
349    
# Line 187  sub parse_string ($$$;$) { Line 352  sub parse_string ($$$;$) {
352    
353      $self->{next_char} = -1 and return if $i >= length $$s;      $self->{next_char} = -1 and return if $i >= length $$s;
354      $self->{next_char} = ord substr $$s, $i++, 1;      $self->{next_char} = ord substr $$s, $i++, 1;
355      $column++;  
356        ($self->{line_prev}, $self->{column_prev})
357            = ($self->{line}, $self->{column});
358        $self->{column}++;
359            
360      if ($self->{next_char} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
361        $line++;        $self->{line}++;
362        $column = 0;        $self->{column} = 0;
363      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
364        $i++ if substr ($$s, $i, 1) eq "\x0A";        $i++ if substr ($$s, $i, 1) eq "\x0A";
365        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
366        $line++;        $self->{line}++;
367        $column = 0;        $self->{column} = 0;
368      } elsif ($self->{next_char} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
369        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
370      } elsif ($self->{next_char} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
# Line 209  sub parse_string ($$$;$) { Line 377  sub parse_string ($$$;$) {
377    
378    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
379      my (%opt) = @_;      my (%opt) = @_;
380      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
381        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
382        warn "Parse error ($opt{type}) at line $line column $column\n";
383    };    };
384    $self->{parse_error} = sub {    $self->{parse_error} = sub {
385      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
386    };    };
387    
388    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 220  sub parse_string ($$$;$) { Line 390  sub parse_string ($$$;$) {
390    $self->_construct_tree;    $self->_construct_tree;
391    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
392    
393      delete $self->{parse_error}; # remove loop
394    
395    return $self->{document};    return $self->{document};
396  } # parse_string  } # parse_string
397    
# Line 287  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 459  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
459  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
460  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
461  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
462    sub SELF_CLOSING_START_TAG_STATE () { 34 }
463    
464  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
465  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 303  sub TABLE_IMS ()      { 0b1000000 } Line 476  sub TABLE_IMS ()      { 0b1000000 }
476  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
477  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
478  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
479    sub SELECT_IMS ()     { 0b10000000000 }
480    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
481        ## NOTE: "in foreign content" insertion mode is special; it is combined
482        ## with the secondary insertion mode.  In this parser, they are stored
483        ## together in the bit-or'ed form.
484    
485  ## NOTE: "initial" and "before html" insertion modes have no constants.  ## NOTE: "initial" and "before html" insertion modes have no constants.
486    
# Line 325  sub IN_TABLE_IM () { TABLE_IMS } Line 503  sub IN_TABLE_IM () { TABLE_IMS }
503  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
504  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
505  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
506  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
507    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
508  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
509    
510  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
# Line 338  sub _initialize_tokenizer ($) { Line 517  sub _initialize_tokenizer ($) {
517    undef $self->{current_attribute};    undef $self->{current_attribute};
518    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
519    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
520      delete $self->{self_closing};
521    $self->{char} = [];    $self->{char} = [];
522    # $self->{next_char}    # $self->{next_char}
523    !!!next-input-character;    !!!next-input-character;
# Line 358  sub _initialize_tokenizer ($) { Line 538  sub _initialize_tokenizer ($) {
538  ##        ->{value}  ##        ->{value}
539  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
540  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
541    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
542    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
543    ##     while the token is pushed back to the stack.
544    
545    ## ISSUE: "When a DOCTYPE token is created, its
546    ## <i>self-closing flag</i> must be unset (its other state is that it
547    ## be set), and its attributes list must be empty.": Wrong subject?
548    
549  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
550    
# Line 384  sub _initialize_tokenizer ($) { Line 571  sub _initialize_tokenizer ($) {
571    
572  sub _get_next_token ($) {  sub _get_next_token ($) {
573    my $self = shift;    my $self = shift;
574    
575      if ($self->{self_closing}) {
576        !!!parse-error (type => 'nestc', token => $self->{current_token});
577        ## NOTE: The |self_closing| flag is only set by start tag token.
578        ## In addition, when a start tag token is emitted, it is always set to
579        ## |current_token|.
580        delete $self->{self_closing};
581      }
582    
583    if (@{$self->{token}}) {    if (@{$self->{token}}) {
584        $self->{self_closing} = $self->{token}->[0]->{self_closing};
585      return shift @{$self->{token}};      return shift @{$self->{token}};
586    }    }
587    
# Line 447  sub _get_next_token ($) { Line 644  sub _get_next_token ($) {
644          #          #
645        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
646          !!!cp (11);          !!!cp (11);
647          !!!emit ({type => END_OF_FILE_TOKEN});          !!!emit ({type => END_OF_FILE_TOKEN,
648                      line => $self->{line}, column => $self->{column}});
649          last A; ## TODO: ok?          last A; ## TODO: ok?
650        } else {        } else {
651          !!!cp (12);          !!!cp (12);
652        }        }
653        # Anything else        # Anything else
654        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
655                     data => chr $self->{next_char}};                     data => chr $self->{next_char},
656                       line => $self->{line}, column => $self->{column},
657                      };
658        ## Stay in the data state        ## Stay in the data state
659        !!!next-input-character;        !!!next-input-character;
660    
# Line 463  sub _get_next_token ($) { Line 663  sub _get_next_token ($) {
663        redo A;        redo A;
664      } elsif ($self->{state} == ENTITY_DATA_STATE) {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
665        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
666    
667          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
668                
669        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
670    
# Line 471  sub _get_next_token ($) { Line 673  sub _get_next_token ($) {
673    
674        unless (defined $token) {        unless (defined $token) {
675          !!!cp (13);          !!!cp (13);
676          !!!emit ({type => CHARACTER_TOKEN, data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&',
677                      line => $l, column => $c,
678                     });
679        } else {        } else {
680          !!!cp (14);          !!!cp (14);
681          !!!emit ($token);          !!!emit ($token);
# Line 490  sub _get_next_token ($) { Line 694  sub _get_next_token ($) {
694            ## reconsume            ## reconsume
695            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
696    
697            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
698                        line => $self->{line_prev},
699                        column => $self->{column_prev},
700                       });
701    
702            redo A;            redo A;
703          }          }
# Line 510  sub _get_next_token ($) { Line 717  sub _get_next_token ($) {
717            !!!cp (19);            !!!cp (19);
718            $self->{current_token}            $self->{current_token}
719              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
720                 tag_name => chr ($self->{next_char} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
721                   line => $self->{line_prev},
722                   column => $self->{column_prev}};
723            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
724            !!!next-input-character;            !!!next-input-character;
725            redo A;            redo A;
# Line 518  sub _get_next_token ($) { Line 727  sub _get_next_token ($) {
727                   $self->{next_char} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
728            !!!cp (20);            !!!cp (20);
729            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
730                              tag_name => chr ($self->{next_char})};                                      tag_name => chr ($self->{next_char}),
731                                        line => $self->{line_prev},
732                                        column => $self->{column_prev}};
733            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
734            !!!next-input-character;            !!!next-input-character;
735            redo A;            redo A;
736          } elsif ($self->{next_char} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
737            !!!cp (21);            !!!cp (21);
738            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag',
739                              line => $self->{line_prev},
740                              column => $self->{column_prev});
741            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
742            !!!next-input-character;            !!!next-input-character;
743    
744            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
745                        line => $self->{line_prev},
746                        column => $self->{column_prev},
747                       });
748    
749            redo A;            redo A;
750          } elsif ($self->{next_char} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
751            !!!cp (22);            !!!cp (22);
752            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio',
753                              line => $self->{line_prev},
754                              column => $self->{column_prev});
755            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
756              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
757                                        line => $self->{line_prev},
758                                        column => $self->{column_prev},
759                                       };
760            ## $self->{next_char} is intentionally left as is            ## $self->{next_char} is intentionally left as is
761            redo A;            redo A;
762          } else {          } else {
# Line 543  sub _get_next_token ($) { Line 765  sub _get_next_token ($) {
765            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
766            ## reconsume            ## reconsume
767    
768            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
769                        line => $self->{line_prev},
770                        column => $self->{column_prev},
771                       });
772    
773            redo A;            redo A;
774          }          }
# Line 551  sub _get_next_token ($) { Line 776  sub _get_next_token ($) {
776          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
777        }        }
778      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
779          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
780        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
781          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
782    
783            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
784            my @next_char;            my @next_char;
785            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 796  sub _get_next_token ($) {
796                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
797                $self->{state} = DATA_STATE;                $self->{state} = DATA_STATE;
798    
799                !!!emit ({type => CHARACTER_TOKEN, data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
800                            line => $l, column => $c,
801                           });
802        
803                redo A;                redo A;
804              }              }
# Line 588  sub _get_next_token ($) { Line 817  sub _get_next_token ($) {
817              $self->{next_char} = shift @next_char; # reconsume              $self->{next_char} = shift @next_char; # reconsume
818              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
819              $self->{state} = DATA_STATE;              $self->{state} = DATA_STATE;
820              !!!emit ({type => CHARACTER_TOKEN, data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
821                          line => $l, column => $c,
822                         });
823              redo A;              redo A;
824            } else {            } else {
825              !!!cp (27);              !!!cp (27);
# Line 601  sub _get_next_token ($) { Line 832  sub _get_next_token ($) {
832            !!!cp (28);            !!!cp (28);
833            # next-input-character is already done            # next-input-character is already done
834            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
835            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
836                        line => $l, column => $c,
837                       });
838            redo A;            redo A;
839          }          }
840        }        }
# Line 609  sub _get_next_token ($) { Line 842  sub _get_next_token ($) {
842        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{next_char} and
843            $self->{next_char} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
844          !!!cp (29);          !!!cp (29);
845          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token}
846                            tag_name => chr ($self->{next_char} + 0x0020)};              = {type => END_TAG_TOKEN,
847                   tag_name => chr ($self->{next_char} + 0x0020),
848                   line => $l, column => $c};
849          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
850          !!!next-input-character;          !!!next-input-character;
851          redo A;          redo A;
# Line 618  sub _get_next_token ($) { Line 853  sub _get_next_token ($) {
853                 $self->{next_char} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
854          !!!cp (30);          !!!cp (30);
855          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
856                            tag_name => chr ($self->{next_char})};                                    tag_name => chr ($self->{next_char}),
857                                      line => $l, column => $c};
858          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
859          !!!next-input-character;          !!!next-input-character;
860          redo A;          redo A;
861        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
862          !!!cp (31);          !!!cp (31);
863          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag',
864                            line => $self->{line_prev}, ## "<" in "</>"
865                            column => $self->{column_prev} - 1);
866          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
867          !!!next-input-character;          !!!next-input-character;
868          redo A;          redo A;
# Line 634  sub _get_next_token ($) { Line 872  sub _get_next_token ($) {
872          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
873          # reconsume          # reconsume
874    
875          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
876                      line => $l, column => $c,
877                     });
878    
879          redo A;          redo A;
880        } else {        } else {
881          !!!cp (33);          !!!cp (33);
882          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
883          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
884            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
885                                      line => $self->{line_prev}, # "<" of "</"
886                                      column => $self->{column_prev} - 1,
887                                     };
888          ## $self->{next_char} is intentionally left as is          ## $self->{next_char} is intentionally left as is
889          redo A;          redo A;
890        }        }
# Line 657  sub _get_next_token ($) { Line 901  sub _get_next_token ($) {
901        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
902          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
903            !!!cp (35);            !!!cp (35);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
904            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
905          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
906            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 690  sub _get_next_token ($) { Line 932  sub _get_next_token ($) {
932          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
933          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
934            !!!cp (39);            !!!cp (39);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
935            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
936          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
937            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 712  sub _get_next_token ($) { Line 952  sub _get_next_token ($) {
952    
953          redo A;          redo A;
954        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
955            !!!cp (42);
956            $self->{state} = SELF_CLOSING_START_TAG_STATE;
957          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_char} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           !!!cp (42);  
           #  
         } else {  
           !!!cp (43);  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
958          redo A;          redo A;
959        } else {        } else {
960          !!!cp (44);          !!!cp (44);
# Line 747  sub _get_next_token ($) { Line 977  sub _get_next_token ($) {
977        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
978          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
979            !!!cp (46);            !!!cp (46);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
980            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
981          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
982            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 770  sub _get_next_token ($) { Line 998  sub _get_next_token ($) {
998        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
999                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1000          !!!cp (49);          !!!cp (49);
1001          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1002                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1003                   value => '',
1004                   line => $self->{line}, column => $self->{column}};
1005          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1006          !!!next-input-character;          !!!next-input-character;
1007          redo A;          redo A;
1008        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1009            !!!cp (50);
1010            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1011          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_char} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           !!!cp (50);  
           #  
         } else {  
           !!!cp (51);  
           !!!parse-error (type => 'nestc');  
         }  
         ## Stay in the state  
         # next-input-character is already done  
1012          redo A;          redo A;
1013        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1014          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1015          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1016            !!!cp (52);            !!!cp (52);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1017            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1018          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1019            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 825  sub _get_next_token ($) { Line 1043  sub _get_next_token ($) {
1043          } else {          } else {
1044            !!!cp (56);            !!!cp (56);
1045          }          }
1046          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1047                                value => ''};              = {name => chr ($self->{next_char}),
1048                   value => '',
1049                   line => $self->{line}, column => $self->{column}};
1050          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1051          !!!next-input-character;          !!!next-input-character;
1052          redo A;          redo A;
# Line 836  sub _get_next_token ($) { Line 1056  sub _get_next_token ($) {
1056          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1057              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1058            !!!cp (57);            !!!cp (57);
1059            !!!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});
1060            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1061          } else {          } else {
1062            !!!cp (58);            !!!cp (58);
# Line 865  sub _get_next_token ($) { Line 1085  sub _get_next_token ($) {
1085          $before_leave->();          $before_leave->();
1086          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1087            !!!cp (61);            !!!cp (61);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1088            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1089          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1090            !!!cp (62);            !!!cp (62);
# Line 891  sub _get_next_token ($) { Line 1109  sub _get_next_token ($) {
1109          !!!next-input-character;          !!!next-input-character;
1110          redo A;          redo A;
1111        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1112            !!!cp (64);
1113          $before_leave->();          $before_leave->();
1114            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1115          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_char} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           !!!cp (64);  
           #  
         } else {  
           !!!cp (65);  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
1116          redo A;          redo A;
1117        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1118          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1119          $before_leave->();          $before_leave->();
1120          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1121            !!!cp (66);            !!!cp (66);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1122            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1123          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1124            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 963  sub _get_next_token ($) { Line 1169  sub _get_next_token ($) {
1169        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1170          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1171            !!!cp (73);            !!!cp (73);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1172            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1173          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1174            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 987  sub _get_next_token ($) { Line 1191  sub _get_next_token ($) {
1191        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1192                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1193          !!!cp (76);          !!!cp (76);
1194          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1195                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1196                   value => '',
1197                   line => $self->{line}, column => $self->{column}};
1198          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1199          !!!next-input-character;          !!!next-input-character;
1200          redo A;          redo A;
1201        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1202            !!!cp (77);
1203            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1204          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_char} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           !!!cp (77);  
           #  
         } else {  
           !!!cp (78);  
           !!!parse-error (type => 'nestc');  
           ## TODO: Different error type for <aa / bb> than <aa/>  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
1205          redo A;          redo A;
1206        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1207          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1208          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1209            !!!cp (79);            !!!cp (79);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1210            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1211          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1212            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1035  sub _get_next_token ($) { Line 1228  sub _get_next_token ($) {
1228          redo A;          redo A;
1229        } else {        } else {
1230          !!!cp (82);          !!!cp (82);
1231          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1232                                value => ''};              = {name => chr ($self->{next_char}),
1233                   value => '',
1234                   line => $self->{line}, column => $self->{column}};
1235          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1236          !!!next-input-character;          !!!next-input-character;
1237          redo A;                  redo A;        
# Line 1069  sub _get_next_token ($) { Line 1264  sub _get_next_token ($) {
1264        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1265          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1266            !!!cp (87);            !!!cp (87);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1267            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1268          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1269            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1094  sub _get_next_token ($) { Line 1287  sub _get_next_token ($) {
1287          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1288          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1289            !!!cp (90);            !!!cp (90);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1290            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1291          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1292            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1143  sub _get_next_token ($) { Line 1334  sub _get_next_token ($) {
1334          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1335          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1336            !!!cp (97);            !!!cp (97);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1337            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1338          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1339            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1187  sub _get_next_token ($) { Line 1376  sub _get_next_token ($) {
1376          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1377          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1378            !!!cp (103);            !!!cp (103);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1379            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1380          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1381            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1234  sub _get_next_token ($) { Line 1421  sub _get_next_token ($) {
1421        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1422          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1423            !!!cp (109);            !!!cp (109);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1424            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1425          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1426            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1259  sub _get_next_token ($) { Line 1444  sub _get_next_token ($) {
1444          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1445          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1446            !!!cp (112);            !!!cp (112);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1447            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1448          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1449            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1331  sub _get_next_token ($) { Line 1514  sub _get_next_token ($) {
1514        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1515          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1516            !!!cp (119);            !!!cp (119);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1517            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1518          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1519            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1353  sub _get_next_token ($) { Line 1534  sub _get_next_token ($) {
1534    
1535          redo A;          redo A;
1536        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1537            !!!cp (122);
1538            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1539          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_char} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           !!!cp (122);  
           #  
         } else {  
           !!!cp (123);  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
1540          redo A;          redo A;
1541        } else {        } else {
1542          !!!cp (124);          !!!cp ('124.1');
1543          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1544          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1545          ## reconsume          ## reconsume
1546          redo A;          redo A;
1547        }        }
1548        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1549          if ($self->{next_char} == 0x003E) { # >
1550            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1551              !!!cp ('124.2');
1552              !!!parse-error (type => 'nestc', token => $self->{current_token});
1553              ## TODO: Different type than slash in start tag
1554              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1555              if ($self->{current_token}->{attributes}) {
1556                !!!cp ('124.4');
1557                !!!parse-error (type => 'end tag attribute');
1558              } else {
1559                !!!cp ('124.5');
1560              }
1561              ## TODO: Test |<title></title/>|
1562            } else {
1563              !!!cp ('124.3');
1564              $self->{self_closing} = 1;
1565            }
1566    
1567            $self->{state} = DATA_STATE;
1568            !!!next-input-character;
1569    
1570            !!!emit ($self->{current_token}); # start tag or end tag
1571    
1572            redo A;
1573          } else {
1574            !!!cp ('124.4');
1575            !!!parse-error (type => 'nestc');
1576            ## TODO: This error type is wrong.
1577            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1578            ## Reconsume.
1579            redo A;
1580          }
1581      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1582        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1583                
1584        my $token = {type => COMMENT_TOKEN, data => ''};        ## NOTE: Set by the previous state
1585          #my $token = {type => COMMENT_TOKEN, data => ''};
1586    
1587        BC: {        BC: {
1588          if ($self->{next_char} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
# Line 1385  sub _get_next_token ($) { Line 1590  sub _get_next_token ($) {
1590            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1591            !!!next-input-character;            !!!next-input-character;
1592    
1593            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1594    
1595            redo A;            redo A;
1596          } elsif ($self->{next_char} == -1) {          } elsif ($self->{next_char} == -1) {
# Line 1393  sub _get_next_token ($) { Line 1598  sub _get_next_token ($) {
1598            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1599            ## reconsume            ## reconsume
1600    
1601            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1602    
1603            redo A;            redo A;
1604          } else {          } else {
1605            !!!cp (126);            !!!cp (126);
1606            $token->{data} .= chr ($self->{next_char});            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1607            !!!next-input-character;            !!!next-input-character;
1608            redo BC;            redo BC;
1609          }          }
# Line 1408  sub _get_next_token ($) { Line 1613  sub _get_next_token ($) {
1613      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1614        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1615    
1616          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1617    
1618        my @next_char;        my @next_char;
1619        push @next_char, $self->{next_char};        push @next_char, $self->{next_char};
1620                
# Line 1416  sub _get_next_token ($) { Line 1623  sub _get_next_token ($) {
1623          push @next_char, $self->{next_char};          push @next_char, $self->{next_char};
1624          if ($self->{next_char} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1625            !!!cp (127);            !!!cp (127);
1626            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1627                                        line => $l, column => $c,
1628                                       };
1629            $self->{state} = COMMENT_START_STATE;            $self->{state} = COMMENT_START_STATE;
1630            !!!next-input-character;            !!!next-input-character;
1631            redo A;            redo A;
# Line 1452  sub _get_next_token ($) { Line 1661  sub _get_next_token ($) {
1661                      !!!cp (129);                      !!!cp (129);
1662                      ## TODO: What a stupid code this is!                      ## TODO: What a stupid code this is!
1663                      $self->{state} = DOCTYPE_STATE;                      $self->{state} = DOCTYPE_STATE;
1664                        $self->{current_token} = {type => DOCTYPE_TOKEN,
1665                                                  quirks => 1,
1666                                                  line => $l, column => $c,
1667                                                 };
1668                      !!!next-input-character;                      !!!next-input-character;
1669                      redo A;                      redo A;
1670                    } else {                    } else {
# Line 1480  sub _get_next_token ($) { Line 1693  sub _get_next_token ($) {
1693        $self->{next_char} = shift @next_char;        $self->{next_char} = shift @next_char;
1694        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1695        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
1696          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1697                                    line => $l, column => $c,
1698                                   };
1699        redo A;        redo A;
1700                
1701        ## 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 1819  sub _get_next_token ($) {
1819          redo A;          redo A;
1820        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
1821          !!!cp (152);          !!!cp (152);
1822          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
1823                            line => $self->{line_prev},
1824                            column => $self->{column_prev});
1825          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
1826          ## Stay in the state          ## Stay in the state
1827          !!!next-input-character;          !!!next-input-character;
# Line 1619  sub _get_next_token ($) { Line 1837  sub _get_next_token ($) {
1837          redo A;          redo A;
1838        } else {        } else {
1839          !!!cp (154);          !!!cp (154);
1840          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
1841                            line => $self->{line_prev},
1842                            column => $self->{column_prev});
1843          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
1844          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
1845          !!!next-input-character;          !!!next-input-character;
# Line 1658  sub _get_next_token ($) { Line 1878  sub _get_next_token ($) {
1878          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1879          !!!next-input-character;          !!!next-input-character;
1880    
1881          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
1882    
1883          redo A;          redo A;
1884        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
# Line 1667  sub _get_next_token ($) { Line 1887  sub _get_next_token ($) {
1887          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1888          ## reconsume          ## reconsume
1889    
1890          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
1891    
1892          redo A;          redo A;
1893        } else {        } else {
1894          !!!cp (160);          !!!cp (160);
1895          $self->{current_token}          $self->{current_token}->{name} = chr $self->{next_char};
1896              = {type => DOCTYPE_TOKEN,          delete $self->{current_token}->{quirks};
                name => chr ($self->{next_char}),  
                #quirks => 0,  
               };  
1897  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1898          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
1899          !!!next-input-character;          !!!next-input-character;
# Line 2203  sub _get_next_token ($) { Line 2420  sub _get_next_token ($) {
2420  sub _tokenize_attempt_to_consume_an_entity ($$$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2421    my ($self, $in_attr, $additional) = @_;    my ($self, $in_attr, $additional) = @_;
2422    
2423      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2424    
2425    if ({    if ({
2426         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2427         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 2462  sub _tokenize_attempt_to_consume_an_enti
2462            redo X;            redo X;
2463          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2464            !!!cp (1005);            !!!cp (1005);
2465            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2466            !!!back-next-input-character ($x_char, $self->{next_char});            !!!back-next-input-character ($x_char, $self->{next_char});
2467            $self->{next_char} = 0x0023; # #            $self->{next_char} = 0x0023; # #
2468            return undef;            return undef;
# Line 2252  sub _tokenize_attempt_to_consume_an_enti Line 2471  sub _tokenize_attempt_to_consume_an_enti
2471            !!!next-input-character;            !!!next-input-character;
2472          } else {          } else {
2473            !!!cp (1007);            !!!cp (1007);
2474            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc', line => $l, column => $c);
2475          }          }
2476    
2477          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2478            !!!cp (1008);            !!!cp (1008);
2479            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2480            $code = 0xFFFD;            $code = 0xFFFD;
2481          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2482            !!!cp (1009);            !!!cp (1009);
2483            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2484            $code = 0xFFFD;            $code = 0xFFFD;
2485          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2486            !!!cp (1010);            !!!cp (1010);
2487            !!!parse-error (type => 'CR character reference');            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2488            $code = 0x000A;            $code = 0x000A;
2489          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2490            !!!cp (1011);            !!!cp (1011);
2491            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2492            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2493          }          }
2494    
2495          return {type => CHARACTER_TOKEN, data => chr $code,          return {type => CHARACTER_TOKEN, data => chr $code,
2496                  has_reference => 1};                  has_reference => 1,
2497                    line => $l, column => $c,
2498                   };
2499        } # X        } # X
2500      } elsif (0x0030 <= $self->{next_char} and      } elsif (0x0030 <= $self->{next_char} and
2501               $self->{next_char} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
# Line 2295  sub _tokenize_attempt_to_consume_an_enti Line 2516  sub _tokenize_attempt_to_consume_an_enti
2516          !!!next-input-character;          !!!next-input-character;
2517        } else {        } else {
2518          !!!cp (1014);          !!!cp (1014);
2519          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc', line => $l, column => $c);
2520        }        }
2521    
2522        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2523          !!!cp (1015);          !!!cp (1015);
2524          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2525          $code = 0xFFFD;          $code = 0xFFFD;
2526        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2527          !!!cp (1016);          !!!cp (1016);
2528          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2529          $code = 0xFFFD;          $code = 0xFFFD;
2530        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2531          !!!cp (1017);          !!!cp (1017);
2532          !!!parse-error (type => 'CR character reference');          !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2533          $code = 0x000A;          $code = 0x000A;
2534        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2535          !!!cp (1018);          !!!cp (1018);
2536          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2537          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2538        }        }
2539                
2540        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2541                  line => $l, column => $c,
2542                 };
2543      } else {      } else {
2544        !!!cp (1019);        !!!cp (1019);
2545        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2546        !!!back-next-input-character ($self->{next_char});        !!!back-next-input-character ($self->{next_char});
2547        $self->{next_char} = 0x0023; # #        $self->{next_char} = 0x0023; # #
2548        return undef;        return undef;
# Line 2369  sub _tokenize_attempt_to_consume_an_enti Line 2592  sub _tokenize_attempt_to_consume_an_enti
2592            
2593      if ($match > 0) {      if ($match > 0) {
2594        !!!cp (1023);        !!!cp (1023);
2595        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2596                  line => $l, column => $c,
2597                 };
2598      } elsif ($match < 0) {      } elsif ($match < 0) {
2599        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
2600        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
2601          !!!cp (1024);          !!!cp (1024);
2602          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
2603                    line => $l, column => $c,
2604                   };
2605        } else {        } else {
2606          !!!cp (1025);          !!!cp (1025);
2607          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2608                    line => $l, column => $c,
2609                   };
2610        }        }
2611      } else {      } else {
2612        !!!cp (1026);        !!!cp (1026);
2613        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero', line => $l, column => $c);
2614        ## NOTE: "No characters are consumed" in the spec.        ## NOTE: "No characters are consumed" in the spec.
2615        return {type => CHARACTER_TOKEN, data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value,
2616                  line => $l, column => $c,
2617                 };
2618      }      }
2619    } else {    } else {
2620      !!!cp (1027);      !!!cp (1027);
2621      ## no characters are consumed      ## no characters are consumed
2622      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
2623      return undef;      return undef;
2624    }    }
2625  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 2459  sub _tree_construction_initial ($) { Line 2690  sub _tree_construction_initial ($) {
2690            defined $token->{public_identifier} or            defined $token->{public_identifier} or
2691            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
2692          !!!cp ('t1');          !!!cp ('t1');
2693          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
2694        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
2695          !!!cp ('t2');          !!!cp ('t2');
2696          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
2697          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
2698        } else {        } else {
2699          !!!cp ('t3');          !!!cp ('t3');
2700        }        }
2701                
2702        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
2703          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
2704          ## NOTE: Default value for both |public_id| and |system_id| attributes
2705          ## are empty strings, so that we don't set any value in missing cases.
2706        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
2707            if defined $token->{public_identifier};            if defined $token->{public_identifier};
2708        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2602  sub _tree_construction_initial ($) { Line 2835  sub _tree_construction_initial ($) {
2835                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
2836               }->{$token->{type}}) {               }->{$token->{type}}) {
2837        !!!cp ('t14');        !!!cp ('t14');
2838        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
2839        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2840        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
2841        ## reprocess        ## reprocess
2842          !!!ack-later;
2843        return;        return;
2844      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
2845        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
# Line 2623  sub _tree_construction_initial ($) { Line 2857  sub _tree_construction_initial ($) {
2857          !!!cp ('t17');          !!!cp ('t17');
2858        }        }
2859    
2860        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
2861        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2862        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
2863        ## reprocess        ## reprocess
# Line 2652  sub _tree_construction_root_element ($) Line 2886  sub _tree_construction_root_element ($)
2886    B: {    B: {
2887        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
2888          !!!cp ('t19');          !!!cp ('t19');
2889          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
2890          ## Ignore the token          ## Ignore the token
2891          ## Stay in the insertion mode.          ## Stay in the insertion mode.
2892          !!!next-token;          !!!next-token;
# Line 2686  sub _tree_construction_root_element ($) Line 2920  sub _tree_construction_root_element ($)
2920        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
2921          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
2922            my $root_element;            my $root_element;
2923            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes});            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
2924            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
2925            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}},
2926                  [$root_element, $el_category->{html}];
2927    
2928            if ($token->{attributes}->{manifest}) {            if ($token->{attributes}->{manifest}) {
2929              !!!cp ('t24');              !!!cp ('t24');
2930              $self->{application_cache_selection}              $self->{application_cache_selection}
2931                  ->($token->{attributes}->{manifest}->{value});                  ->($token->{attributes}->{manifest}->{value});
2932              ## ISSUE: No relative reference resolution?              ## ISSUE: Spec is unclear on relative references.
2933                ## According to Hixie (#whatwg 2008-03-19), it should be
2934                ## resolved against the base URI of the document in HTML
2935                ## or xml:base of the element in XHTML.
2936            } else {            } else {
2937              !!!cp ('t25');              !!!cp ('t25');
2938              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
2939            }            }
2940    
2941              !!!nack ('t25c');
2942    
2943            !!!next-token;            !!!next-token;
2944            return; ## Go to the "before head" insertion mode.            return; ## Go to the "before head" insertion mode.
2945          } else {          } else {
# Line 2716  sub _tree_construction_root_element ($) Line 2956  sub _tree_construction_root_element ($)
2956          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
2957        }        }
2958    
2959      my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
2960        !!!create-element ($root_element, $HTML_NS, '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    
2966      ## NOTE: Reprocess the token.      ## NOTE: Reprocess the token.
2967        !!!ack-later;
2968      return; ## Go to the "before head" insertion mode.      return; ## Go to the "before head" insertion mode.
2969    
2970      ## ISSUE: There is an issue in the spec      ## ISSUE: There is an issue in the spec
# Line 2743  sub _reset_insertion_mode ($) { Line 2985  sub _reset_insertion_mode ($) {
2985            
2986      ## Step 3      ## Step 3
2987      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"!?  
2988        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2989          $last = 1;          $last = 1;
2990          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
2991            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') {  
2992              !!!cp ('t27');              !!!cp ('t27');
2993              #              #
2994            } else {            } else {
# Line 2762  sub _reset_insertion_mode ($) { Line 2998  sub _reset_insertion_mode ($) {
2998          }          }
2999        }        }
3000            
3001        ## Step 4..13      ## Step 4..14
3002        my $new_mode = {      my $new_mode;
3003        if ($node->[1] & FOREIGN_EL) {
3004          ## NOTE: Strictly spaking, the line below only applies to MathML and
3005          ## SVG elements.  Currently the HTML syntax supports only MathML and
3006          ## SVG elements as foreigners.
3007          $new_mode = $self->{insertion_mode} | IN_FOREIGN_CONTENT_IM;
3008          ## ISSUE: What is set as the secondary insertion mode?
3009        } else {
3010          $new_mode = {
3011                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3012                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3013                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
# Line 2779  sub _reset_insertion_mode ($) { Line 3023  sub _reset_insertion_mode ($) {
3023                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3024                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3025                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3026                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3027        $self->{insertion_mode} = $new_mode and return if defined $new_mode;      }
3028        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3029                
3030        ## Step 14        ## Step 15
3031        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3032          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3033            !!!cp ('t29');            !!!cp ('t29');
3034            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2797  sub _reset_insertion_mode ($) { Line 3042  sub _reset_insertion_mode ($) {
3042          !!!cp ('t31');          !!!cp ('t31');
3043        }        }
3044                
3045        ## Step 15        ## Step 16
3046        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3047                
3048        ## Step 16        ## Step 17
3049        $i--;        $i--;
3050        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3051                
3052        ## Step 17        ## Step 18
3053        redo S3;        redo S3;
3054      } # S3      } # S3
3055    
# Line 2908  sub _tree_construction_main ($) { Line 3153  sub _tree_construction_main ($) {
3153      !!!cp ('t39');      !!!cp ('t39');
3154    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3155    
3156    my $parse_rcdata = sub ($$) {    my $insert;
3157      my ($content_model_flag, $insert) = @_;  
3158      my $parse_rcdata = sub ($) {
3159        my ($content_model_flag) = @_;
3160    
3161      ## Step 1      ## Step 1
3162      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3163      my $el;      my $el;
3164      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3165    
3166      ## Step 2      ## Step 2
3167      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3168    
3169      ## Step 3      ## Step 3
3170      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2925  sub _tree_construction_main ($) { Line 3172  sub _tree_construction_main ($) {
3172    
3173      ## Step 4      ## Step 4
3174      my $text = '';      my $text = '';
3175        !!!nack ('t40.1');
3176      !!!next-token;      !!!next-token;
3177      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3178        !!!cp ('t40');        !!!cp ('t40');
# Line 2947  sub _tree_construction_main ($) { Line 3195  sub _tree_construction_main ($) {
3195          $token->{tag_name} eq $start_tag_name) {          $token->{tag_name} eq $start_tag_name) {
3196        !!!cp ('t42');        !!!cp ('t42');
3197        ## 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});  
3198      } else {      } else {
3199        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3200          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3201            !!!cp ('t43');
3202            !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3203          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3204            !!!cp ('t44');
3205            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3206          } else {
3207            die "$0: $content_model_flag in parse_rcdata";
3208          }
3209      }      }
3210      !!!next-token;      !!!next-token;
3211    }; # $parse_rcdata    }; # $parse_rcdata
3212    
3213    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3214      my $script_el;      my $script_el;
3215      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3216      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3217    
3218      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3219      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3220            
3221      my $text = '';      my $text = '';
3222        !!!nack ('t45.1');
3223      !!!next-token;      !!!next-token;
3224      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3225        !!!cp ('t45');        !!!cp ('t45');
# Line 2988  sub _tree_construction_main ($) { Line 3239  sub _tree_construction_main ($) {
3239        ## Ignore the token        ## Ignore the token
3240      } else {      } else {
3241        !!!cp ('t48');        !!!cp ('t48');
3242        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3243        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3244        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3245      }      }
# Line 3011  sub _tree_construction_main ($) { Line 3262  sub _tree_construction_main ($) {
3262      !!!next-token;      !!!next-token;
3263    }; # $script_start_tag    }; # $script_start_tag
3264    
3265      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3266      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3267      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3268    
3269    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3270      my $tag_name = shift;      my $end_tag_token = shift;
3271        my $tag_name = $end_tag_token->{tag_name};
3272    
3273        ## NOTE: The adoption agency algorithm (AAA).
3274    
3275      FET: {      FET: {
3276        ## Step 1        ## Step 1
3277        my $formatting_element;        my $formatting_element;
3278        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3279        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3280          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3281              !!!cp ('t52');
3282              last AFE;
3283            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3284                         eq $tag_name) {
3285            !!!cp ('t51');            !!!cp ('t51');
3286            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3287            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3288            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
3289          }          }
3290        } # AFE        } # AFE
3291        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3292          !!!cp ('t53');          !!!cp ('t53');
3293          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3294          ## Ignore the token          ## Ignore the token
3295          !!!next-token;          !!!next-token;
3296          return;          return;
# Line 3048  sub _tree_construction_main ($) { Line 3307  sub _tree_construction_main ($) {
3307              last INSCOPE;              last INSCOPE;
3308            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3309              !!!cp ('t55');              !!!cp ('t55');
3310              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3311                                token => $end_tag_token);
3312              ## Ignore the token              ## Ignore the token
3313              !!!next-token;              !!!next-token;
3314              return;              return;
3315            }            }
3316          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
                   table => 1, caption => 1, td => 1, th => 1,  
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3317            !!!cp ('t56');            !!!cp ('t56');
3318            $in_scope = 0;            $in_scope = 0;
3319          }          }
3320        } # INSCOPE        } # INSCOPE
3321        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3322          !!!cp ('t57');          !!!cp ('t57');
3323          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3324                            token => $end_tag_token);
3325          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3326          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3327          return;          return;
3328        }        }
3329        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3330          !!!cp ('t58');          !!!cp ('t58');
3331          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!parse-error (type => 'not closed',
3332                            value => $self->{open_elements}->[-1]->[0]
3333                                ->manakai_local_name,
3334                            token => $end_tag_token);
3335        }        }
3336                
3337        ## Step 2        ## Step 2
# Line 3078  sub _tree_construction_main ($) { Line 3339  sub _tree_construction_main ($) {
3339        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3340        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3341          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3342          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3343              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3344              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3345               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3346            !!!cp ('t59');            !!!cp ('t59');
3347            $furthest_block = $node;            $furthest_block = $node;
3348            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3167  sub _tree_construction_main ($) { Line 3428  sub _tree_construction_main ($) {
3428        } # S7          } # S7  
3429                
3430        ## Step 8        ## Step 8
3431        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3432            my $foster_parent_element;
3433            my $next_sibling;
3434            OE: for (reverse 0..$#{$self->{open_elements}}) {
3435              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3436                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3437                                 if (defined $parent and $parent->node_type == 1) {
3438                                   !!!cp ('t65.1');
3439                                   $foster_parent_element = $parent;
3440                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3441                                 } else {
3442                                   !!!cp ('t65.2');
3443                                   $foster_parent_element
3444                                     = $self->{open_elements}->[$_ - 1]->[0];
3445                                 }
3446                                 last OE;
3447                               }
3448                             } # OE
3449                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3450                               unless defined $foster_parent_element;
3451            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3452            $open_tables->[-1]->[1] = 1; # tainted
3453          } else {
3454            !!!cp ('t65.3');
3455            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3456          }
3457                
3458        ## Step 9        ## Step 9
3459        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 3213  sub _tree_construction_main ($) { Line 3499  sub _tree_construction_main ($) {
3499      } # FET      } # FET
3500    }; # $formatting_end_tag    }; # $formatting_end_tag
3501    
3502    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3503      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3504    }; # $insert_to_current    }; # $insert_to_current
3505    
3506    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3507                         my $child = shift;      my $child = shift;
3508                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3509                              table => 1, tbody => 1, tfoot => 1,        # MUST
3510                              thead => 1, tr => 1,        my $foster_parent_element;
3511                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
3512                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
3513                           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') {  
3514                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3515                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3516                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3245  sub _tree_construction_main ($) { Line 3528  sub _tree_construction_main ($) {
3528                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3529                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3530                             ($child, $next_sibling);                             ($child, $next_sibling);
3531                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3532                           !!!cp ('t72');      } else {
3533                           $self->{open_elements}->[-1]->[0]->append_child ($child);        !!!cp ('t72');
3534                         }        $self->{open_elements}->[-1]->[0]->append_child ($child);
3535        }
3536    }; # $insert_to_foster    }; # $insert_to_foster
3537    
3538    my $insert;    B: while (1) {
   
   B: {  
3539      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3540        !!!cp ('t73');        !!!cp ('t73');
3541        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3542        ## Ignore the token        ## Ignore the token
3543        ## Stay in the phase        ## Stay in the phase
3544        !!!next-token;        !!!next-token;
3545        redo B;        next B;
     } elsif ($token->{type} == END_OF_FILE_TOKEN) {  
       if ($self->{insertion_mode} & AFTER_HTML_IMS) {  
         !!!cp ('t74');  
         #  
       } else {  
         ## Generate implied end tags  
         if ({  
              dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,  
              tbody => 1, tfoot=> 1, thead => 1,  
             }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!cp ('t75');  
           !!!back-token;  
           $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};  
           redo B;  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!cp ('t76');  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
 ## ISSUE: This case is never reached.  
           !!!cp ('t77');  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } else {  
           !!!cp ('t78');  
         }  
   
         ## ISSUE: There is an issue in the spec.  
       }  
   
       ## Stop parsing  
       last B;  
3546      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3547               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3548        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3549          !!!cp ('t79');          !!!cp ('t79');
3550          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3551          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
3552        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3553          !!!cp ('t80');          !!!cp ('t80');
3554          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3555          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3556        } else {        } else {
3557          !!!cp ('t81');          !!!cp ('t81');
3558        }        }
3559    
3560        !!!cp ('t82');        !!!cp ('t82');
3561        !!!parse-error (type => 'not first start tag');        !!!parse-error (type => 'not first start tag', token => $token);
3562        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
3563        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
3564          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
# Line 3321  sub _tree_construction_main ($) { Line 3568  sub _tree_construction_main ($) {
3568               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
3569          }          }
3570        }        }
3571          !!!nack ('t84.1');
3572        !!!next-token;        !!!next-token;
3573        redo B;        next B;
3574      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3575        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3576        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3336  sub _tree_construction_main ($) { Line 3584  sub _tree_construction_main ($) {
3584          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
3585        }        }
3586        !!!next-token;        !!!next-token;
3587        redo B;        next B;
3588      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
3589          if ($token->{type} == CHARACTER_TOKEN) {
3590            !!!cp ('t87.1');
3591            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3592            !!!next-token;
3593            next B;
3594          } elsif ($token->{type} == START_TAG_TOKEN) {
3595            if ($self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL or
3596                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
3597                ($token->{tag_name} eq 'svg' and
3598                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
3599              ## NOTE: "using the rules for secondary insertion mode"then"continue"
3600              !!!cp ('t87.2');
3601              #
3602            } elsif ({
3603      ## TODO:
3604                     }->{$token->{tag_name}}) {
3605              !!!cp ('t87.2');
3606              !!!parse-error (type => 'not closed',
3607                              value => $self->{open_elements}->[-1]->[0]
3608                                  ->manakai_local_name,
3609                              token => $token);
3610    
3611              pop @{$self->{open_elements}}
3612                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
3613    
3614              $self->{insertion_mode} &= ~ $self->{insertion_mode};
3615              ## Reprocess.
3616              next B;
3617            } else {
3618              ## TODO: case fixup
3619    
3620              !!!insert-element-f ($self->{open_elements}->[-1]->[0]->namespace_uri, $token);
3621    
3622              if ($self->{self_closing}) {
3623                pop @{$self->{open_elements}};
3624                !!!ack ('t87.3');
3625              } else {
3626                !!!cp ('t87.4');
3627              }
3628    
3629              !!!next-token;
3630              next B;
3631            }
3632          } elsif ($token->{type} == END_TAG_TOKEN) {
3633            ## NOTE: "using the rules for secondary insertion mode" then "continue"
3634            !!!cp ('t87.5');
3635            #
3636          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
3637            ## NOTE: "using the rules for secondary insertion mode" then "continue"
3638            !!!cp ('t87.6');
3639            #
3640            ## TODO: ...
3641          } else {
3642            die "$0: $token->{type}: Unknown token type";        
3643          }
3644        }
3645    
3646        if ($self->{insertion_mode} & HEAD_IMS) {
3647        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
3648          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3649            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3650                !!!cp ('t88.2');
3651                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3652              } else {
3653                !!!cp ('t88.1');
3654                ## Ignore the token.
3655                !!!next-token;
3656                next B;
3657              }
3658            unless (length $token->{data}) {            unless (length $token->{data}) {
3659              !!!cp ('t88');              !!!cp ('t88');
3660              !!!next-token;              !!!next-token;
3661              redo B;              next B;
3662            }            }
3663          }          }
3664    
3665          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3666            !!!cp ('t89');            !!!cp ('t89');
3667            ## As if <head>            ## As if <head>
3668            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
3669            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3670            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
3671                  [$self->{head_element}, $el_category->{head}];
3672    
3673            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
3674            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3363  sub _tree_construction_main ($) { Line 3678  sub _tree_construction_main ($) {
3678            !!!cp ('t90');            !!!cp ('t90');
3679            ## As if </noscript>            ## As if </noscript>
3680            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
3681            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character', token => $token);
3682                        
3683            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
3684            ## As if </head>            ## As if </head>
# Line 3379  sub _tree_construction_main ($) { Line 3694  sub _tree_construction_main ($) {
3694            !!!cp ('t92');            !!!cp ('t92');
3695          }          }
3696    
3697              ## "after head" insertion mode          ## "after head" insertion mode
3698              ## As if <body>          ## As if <body>
3699              !!!insert-element ('body');          !!!insert-element ('body',, $token);
3700              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
3701              ## reprocess          ## reprocess
3702              redo B;          next B;
3703            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3704              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
3705                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3706                  !!!cp ('t93');              !!!cp ('t93');
3707                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3708                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
3709                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
3710                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
3711                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
3712                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
3713                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
3714                  !!!cp ('t94');              !!!next-token;
3715                  #              next B;
3716                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3717                  !!!cp ('t95');              !!!cp ('t94');
3718                  !!!parse-error (type => 'in head:head'); # or in head noscript              #
3719                  ## Ignore the token            } else {
3720                  !!!next-token;              !!!cp ('t95');
3721                  redo B;              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
3722                }              ## Ignore the token
3723              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              !!!nack ('t95.1');
3724                !!!cp ('t96');              !!!next-token;
3725                ## As if <head>              next B;
3726                !!!create-element ($self->{head_element}, 'head');            }
3727                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});          } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3728                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            !!!cp ('t96');
3729              ## As if <head>
3730              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
3731              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3732              push @{$self->{open_elements}},
3733                  [$self->{head_element}, $el_category->{head}];
3734    
3735                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
3736                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
3737              } else {          } else {
3738                !!!cp ('t97');            !!!cp ('t97');
3739              }          }
3740    
3741              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
3742                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3743                  !!!cp ('t98');                  !!!cp ('t98');
3744                  ## As if </noscript>                  ## As if </noscript>
3745                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3746                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base', token => $token);
3747                                
3748                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3749                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3434  sub _tree_construction_main ($) { Line 3754  sub _tree_construction_main ($) {
3754                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3755                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3756                  !!!cp ('t100');                  !!!cp ('t100');
3757                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3758                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
3759                        [$self->{head_element}, $el_category->{head}];
3760                } else {                } else {
3761                  !!!cp ('t101');                  !!!cp ('t101');
3762                }                }
3763                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3764                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3765                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3766                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3767                  !!!nack ('t101.1');
3768                !!!next-token;                !!!next-token;
3769                redo B;                next B;
3770              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
3771                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3772                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3773                  !!!cp ('t102');                  !!!cp ('t102');
3774                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3775                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
3776                        [$self->{head_element}, $el_category->{head}];
3777                } else {                } else {
3778                  !!!cp ('t103');                  !!!cp ('t103');
3779                }                }
3780                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3781                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3782                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3783                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3784                  !!!ack ('t103.1');
3785                !!!next-token;                !!!next-token;
3786                redo B;                next B;
3787              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
3788                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3789                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3790                  !!!cp ('t104');                  !!!cp ('t104');
3791                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3792                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
3793                        [$self->{head_element}, $el_category->{head}];
3794                } else {                } else {
3795                  !!!cp ('t105');                  !!!cp ('t105');
3796                }                }
3797                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3798                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.
3799    
3800                unless ($self->{confident}) {                unless ($self->{confident}) {
3801                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
3802                    !!!cp ('t106');                    !!!cp ('t106');
3803                    $self->{change_encoding}                    $self->{change_encoding}
3804                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
3805                             $token);
3806                                        
3807                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3808                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
# Line 3491  sub _tree_construction_main ($) { Line 3817  sub _tree_construction_main ($) {
3817                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
3818                      !!!cp ('t107');                      !!!cp ('t107');
3819                      $self->{change_encoding}                      $self->{change_encoding}
3820                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
3821                               $token);
3822                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3823                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
3824                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
# Line 3517  sub _tree_construction_main ($) { Line 3844  sub _tree_construction_main ($) {
3844                  }                  }
3845                }                }
3846    
3847                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3848                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3849                  !!!ack ('t110.1');
3850                !!!next-token;                !!!next-token;
3851                redo B;                next B;
3852              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
3853                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3854                  !!!cp ('t111');                  !!!cp ('t111');
3855                  ## As if </noscript>                  ## As if </noscript>
3856                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3857                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title', token => $token);
3858                                
3859                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3860                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3861                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3862                  !!!cp ('t112');                  !!!cp ('t112');
3863                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3864                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
3865                        [$self->{head_element}, $el_category->{head}];
3866                } else {                } else {
3867                  !!!cp ('t113');                  !!!cp ('t113');
3868                }                }
# Line 3541  sub _tree_construction_main ($) { Line 3870  sub _tree_construction_main ($) {
3870                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3871                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
3872                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
3873                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
3874                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
3875                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3876                redo B;                next B;
3877              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
3878                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
3879                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
3880                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3881                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3882                  !!!cp ('t114');                  !!!cp ('t114');
3883                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3884                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
3885                        [$self->{head_element}, $el_category->{head}];
3886                } else {                } else {
3887                  !!!cp ('t115');                  !!!cp ('t115');
3888                }                }
3889                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
3890                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3891                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3892                redo B;                next B;
3893              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
3894                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
3895                  !!!cp ('t116');                  !!!cp ('t116');
3896                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
3897                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3898                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
3899                    !!!nack ('t116.1');
3900                  !!!next-token;                  !!!next-token;
3901                  redo B;                  next B;
3902                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3903                  !!!cp ('t117');                  !!!cp ('t117');
3904                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript:noscript', token => $token);
3905                  ## Ignore the token                  ## Ignore the token
3906                    !!!nack ('t117.1');
3907                  !!!next-token;                  !!!next-token;
3908                  redo B;                  next B;
3909                } else {                } else {
3910                  !!!cp ('t118');                  !!!cp ('t118');
3911                  #                  #
# Line 3584  sub _tree_construction_main ($) { Line 3915  sub _tree_construction_main ($) {
3915                  !!!cp ('t119');                  !!!cp ('t119');
3916                  ## As if </noscript>                  ## As if </noscript>
3917                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3918                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script', token => $token);
3919                                
3920                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3921                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3922                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3923                  !!!cp ('t120');                  !!!cp ('t120');
3924                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3925                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
3926                        [$self->{head_element}, $el_category->{head}];
3927                } else {                } else {
3928                  !!!cp ('t121');                  !!!cp ('t121');
3929                }                }
3930    
3931                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3932                $script_start_tag->($insert_to_current);                $script_start_tag->();
3933                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3934                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3935                redo B;                next B;
3936              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
3937                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
3938                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3939                  !!!cp ('t122');                  !!!cp ('t122');
3940                  ## As if </noscript>                  ## As if </noscript>
3941                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3942                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
3943                                    
3944                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3945                  ## As if </head>                  ## As if </head>
# Line 3624  sub _tree_construction_main ($) { Line 3956  sub _tree_construction_main ($) {
3956                }                }
3957    
3958                ## "after head" insertion mode                ## "after head" insertion mode
3959                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3960                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
3961                  !!!cp ('t126');                  !!!cp ('t126');
3962                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
# Line 3634  sub _tree_construction_main ($) { Line 3966  sub _tree_construction_main ($) {
3966                } else {                } else {
3967                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
3968                }                }
3969                  !!!nack ('t127.1');
3970                !!!next-token;                !!!next-token;
3971                redo B;                next B;
3972              } else {              } else {
3973                !!!cp ('t128');                !!!cp ('t128');
3974                #                #
# Line 3645  sub _tree_construction_main ($) { Line 3978  sub _tree_construction_main ($) {
3978                !!!cp ('t129');                !!!cp ('t129');
3979                ## As if </noscript>                ## As if </noscript>
3980                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3981                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
3982                                
3983                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
3984                ## As if </head>                ## As if </head>
# Line 3664  sub _tree_construction_main ($) { Line 3997  sub _tree_construction_main ($) {
3997    
3998              ## "after head" insertion mode              ## "after head" insertion mode
3999              ## As if <body>              ## As if <body>
4000              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4001              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4002              ## reprocess              ## reprocess
4003              redo B;              !!!ack-later;
4004                next B;
4005            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4006              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4007                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4008                  !!!cp ('t132');                  !!!cp ('t132');
4009                  ## As if <head>                  ## As if <head>
4010                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4011                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4012                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4013                        [$self->{head_element}, $el_category->{head}];
4014    
4015                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4016                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4017                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4018                  !!!next-token;                  !!!next-token;
4019                  redo B;                  next B;
4020                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4021                  !!!cp ('t133');                  !!!cp ('t133');
4022                  ## As if </noscript>                  ## As if </noscript>
4023                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4024                  !!!parse-error (type => 'in noscript:/head');                  !!!parse-error (type => 'in noscript:/head', token => $token);
4025                                    
4026                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4027                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4028                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4029                  !!!next-token;                  !!!next-token;
4030                  redo B;                  next B;
4031                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4032                  !!!cp ('t134');                  !!!cp ('t134');
4033                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4034                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4035                  !!!next-token;                  !!!next-token;
4036                  redo B;                  next B;
4037                } else {                } else {
4038                  !!!cp ('t135');                  !!!cp ('t135');
4039                  #                  #
# Line 3709  sub _tree_construction_main ($) { Line 4044  sub _tree_construction_main ($) {
4044                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4045                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4046                  !!!next-token;                  !!!next-token;
4047                  redo B;                  next B;
4048                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4049                  !!!cp ('t137');                  !!!cp ('t137');
4050                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
4051                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4052                  !!!next-token;                  !!!next-token;
4053                  redo B;                  next B;
4054                } else {                } else {
4055                  !!!cp ('t138');                  !!!cp ('t138');
4056                  #                  #
# Line 3726  sub _tree_construction_main ($) { Line 4061  sub _tree_construction_main ($) {
4061                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4062                  !!!cp ('t139');                  !!!cp ('t139');
4063                  ## As if <head>                  ## As if <head>
4064                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4065                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4066                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4067                        [$self->{head_element}, $el_category->{head}];
4068    
4069                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4070                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4071                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4072                  !!!cp ('t140');                  !!!cp ('t140');
4073                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4074                  ## Ignore the token                  ## Ignore the token
4075                  !!!next-token;                  !!!next-token;
4076                  redo B;                  next B;
4077                } else {                } else {
4078                  !!!cp ('t141');                  !!!cp ('t141');
4079                }                }
# Line 3749  sub _tree_construction_main ($) { Line 4085  sub _tree_construction_main ($) {
4085                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4086                  !!!cp ('t142');                  !!!cp ('t142');
4087                  ## As if <head>                  ## As if <head>
4088                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4089                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4090                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4091                        [$self->{head_element}, $el_category->{head}];
4092    
4093                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4094                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3766  sub _tree_construction_main ($) { Line 4103  sub _tree_construction_main ($) {
4103                  #                  #
4104                } else {                } else {
4105                  !!!cp ('t145');                  !!!cp ('t145');
4106                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4107                  ## Ignore the token                  ## Ignore the token
4108                  !!!next-token;                  !!!next-token;
4109                  redo B;                  next B;
4110                }                }
4111              }              }
4112    
# Line 3777  sub _tree_construction_main ($) { Line 4114  sub _tree_construction_main ($) {
4114                !!!cp ('t146');                !!!cp ('t146');
4115                ## As if </noscript>                ## As if </noscript>
4116                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4117                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4118                                
4119                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4120                ## As if </head>                ## As if </head>
# Line 3793  sub _tree_construction_main ($) { Line 4130  sub _tree_construction_main ($) {
4130              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4131  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
4132                !!!cp ('t148');                !!!cp ('t148');
4133                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4134                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4135                !!!next-token;                !!!next-token;
4136                redo B;                next B;
4137              } else {              } else {
4138                !!!cp ('t149');                !!!cp ('t149');
4139              }              }
4140    
4141              ## "after head" insertion mode              ## "after head" insertion mode
4142              ## As if <body>              ## As if <body>
4143              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4144              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4145              ## reprocess              ## reprocess
4146              redo B;              next B;
4147            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4148              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4149            }            !!!cp ('t149.1');
4150    
4151              ## NOTE: As if <head>
4152              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4153              $self->{open_elements}->[-1]->[0]->append_child
4154                  ($self->{head_element});
4155              #push @{$self->{open_elements}},
4156              #    [$self->{head_element}, $el_category->{head}];
4157              #$self->{insertion_mode} = IN_HEAD_IM;
4158              ## NOTE: Reprocess.
4159    
4160              ## NOTE: As if </head>
4161              #pop @{$self->{open_elements}};
4162              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4163              ## NOTE: Reprocess.
4164              
4165              #
4166            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4167              !!!cp ('t149.2');
4168    
4169              ## NOTE: As if </head>
4170              pop @{$self->{open_elements}};
4171              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4172              ## NOTE: Reprocess.
4173    
4174              #
4175            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4176              !!!cp ('t149.3');
4177    
4178              !!!parse-error (type => 'in noscript:#eof', token => $token);
4179    
4180              ## As if </noscript>
4181              pop @{$self->{open_elements}};
4182              #$self->{insertion_mode} = IN_HEAD_IM;
4183              ## NOTE: Reprocess.
4184    
4185              ## NOTE: As if </head>
4186              pop @{$self->{open_elements}};
4187              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4188              ## NOTE: Reprocess.
4189    
4190              #
4191            } else {
4192              !!!cp ('t149.4');
4193              #
4194            }
4195    
4196            ## NOTE: As if <body>
4197            !!!insert-element ('body',, $token);
4198            $self->{insertion_mode} = IN_BODY_IM;
4199            ## NOTE: Reprocess.
4200            next B;
4201          } else {
4202            die "$0: $token->{type}: Unknown token type";
4203          }
4204    
4205            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
4206      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
# Line 3821  sub _tree_construction_main ($) { Line 4212  sub _tree_construction_main ($) {
4212              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4213    
4214              !!!next-token;              !!!next-token;
4215              redo B;              next B;
4216            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4217              if ({              if ({
4218                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3829  sub _tree_construction_main ($) { Line 4220  sub _tree_construction_main ($) {
4220                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4221                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
4222                  ## have an element in table scope                  ## have an element in table scope
4223                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
4224                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4225                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4226                      !!!cp ('t151');                      !!!cp ('t151');
4227                      $tn = $node->[1];  
4228                      last INSCOPE;                      ## Close the cell
4229                    } elsif ({                      !!!back-token; # <x>
4230                              table => 1, html => 1,                      $token = {type => END_TAG_TOKEN,
4231                             }->{$node->[1]}) {                                tag_name => $node->[0]->manakai_local_name,
4232                                  line => $token->{line},
4233                                  column => $token->{column}};
4234                        next B;
4235                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4236                      !!!cp ('t152');                      !!!cp ('t152');
4237                      last INSCOPE;                      ## ISSUE: This case can never be reached, maybe.
4238                        last;
4239                    }                    }
4240                  } # INSCOPE                  }
4241                    unless (defined $tn) {  
4242                      !!!cp ('t153');                  !!!cp ('t153');
4243  ## TODO: This error type is wrong.                  !!!parse-error (type => 'start tag not allowed',
4244                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      value => $token->{tag_name}, token => $token);
4245                      ## Ignore the token                  ## Ignore the token
4246                      !!!next-token;                  !!!nack ('t153.1');
4247                      redo B;                  !!!next-token;
4248                    }                  next B;
                   
                 !!!cp ('t154');  
                 ## Close the cell  
                 !!!back-token; # <?>  
                 $token = {type => END_TAG_TOKEN, tag_name => $tn};  
                 redo B;  
4249                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4250                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption', token => $token);
4251                                    
4252                  ## As if </caption>                  ## NOTE: As if </caption>.
4253                  ## have a table element in table scope                  ## have a table element in table scope
4254                  my $i;                  my $i;
4255                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4256                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4257                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
4258                      !!!cp ('t155');                      if ($node->[1] & CAPTION_EL) {
4259                      $i = $_;                        !!!cp ('t155');
4260                      last INSCOPE;                        $i = $_;
4261                    } elsif ({                        last INSCOPE;
4262                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4263                             }->{$node->[1]}) {                        !!!cp ('t156');
4264                      !!!cp ('t156');                        last;
4265                      last INSCOPE;                      }
4266                    }                    }
4267    
4268                      !!!cp ('t157');
4269                      !!!parse-error (type => 'start tag not allowed',
4270                                      value => $token->{tag_name}, token => $token);
4271                      ## Ignore the token
4272                      !!!nack ('t157.1');
4273                      !!!next-token;
4274                      next B;
4275                  } # 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;  
                   }  
4276                                    
4277                  ## generate implied end tags                  ## generate implied end tags
4278                  if ({                  while ($self->{open_elements}->[-1]->[1]
4279                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
   
                      ## NOTE: Maybe the following elements never appear here.  
                      td => 1, th => 1, tr => 1,  
                      tbody => 1, tfoot => 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
4280                    !!!cp ('t158');                    !!!cp ('t158');
4281                    !!!back-token; # <?>                    pop @{$self->{open_elements}};
                   $token = {type => END_TAG_TOKEN, tag_name => 'caption'};  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4282                  }                  }
4283    
4284                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4285                    !!!cp ('t159');                    !!!cp ('t159');
4286                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4287                                      value => $self->{open_elements}->[-1]->[0]
4288                                          ->manakai_local_name,
4289                                      token => $token);
4290                  } else {                  } else {
4291                    !!!cp ('t160');                    !!!cp ('t160');
4292                  }                  }
# Line 3916  sub _tree_construction_main ($) { Line 4298  sub _tree_construction_main ($) {
4298                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4299                                    
4300                  ## reprocess                  ## reprocess
4301                  redo B;                  !!!ack-later;
4302                    next B;
4303                } else {                } else {
4304                  !!!cp ('t161');                  !!!cp ('t161');
4305                  #                  #
# Line 3932  sub _tree_construction_main ($) { Line 4315  sub _tree_construction_main ($) {
4315                  my $i;                  my $i;
4316                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4317                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4318                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4319                      !!!cp ('t163');                      !!!cp ('t163');
4320                      $i = $_;                      $i = $_;
4321                      last INSCOPE;                      last INSCOPE;
4322                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4323                      !!!cp ('t164');                      !!!cp ('t164');
4324                      last INSCOPE;                      last INSCOPE;
4325                    }                    }
4326                  } # INSCOPE                  } # INSCOPE
4327                    unless (defined $i) {                    unless (defined $i) {
4328                      !!!cp ('t165');                      !!!cp ('t165');
4329                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4330                      ## Ignore the token                      ## Ignore the token
4331                      !!!next-token;                      !!!next-token;
4332                      redo B;                      next B;
4333                    }                    }
4334                                    
4335                  ## generate implied end tags                  ## generate implied end tags
4336                  if ({                  while ($self->{open_elements}->[-1]->[1]
4337                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                      td => ($token->{tag_name} eq 'th'),  
                      th => ($token->{tag_name} eq 'td'),  
   
                      ## NOTE: Maybe the following elements never appear here.  
                      tr => 1,  
                      tbody => 1, tfoot => 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
4338                    !!!cp ('t166');                    !!!cp ('t166');
4339                    !!!back-token;                    pop @{$self->{open_elements}};
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4340                  }                  }
4341                    
4342                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4343                            ne $token->{tag_name}) {
4344                    !!!cp ('t167');                    !!!cp ('t167');
4345                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4346                                      value => $self->{open_elements}->[-1]->[0]
4347                                          ->manakai_local_name,
4348                                      token => $token);
4349                  } else {                  } else {
4350                    !!!cp ('t168');                    !!!cp ('t168');
4351                  }                  }
# Line 3982  sub _tree_construction_main ($) { Line 4357  sub _tree_construction_main ($) {
4357                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4358                                    
4359                  !!!next-token;                  !!!next-token;
4360                  redo B;                  next B;
4361                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4362                  !!!cp ('t169');                  !!!cp ('t169');
4363                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4364                  ## Ignore the token                  ## Ignore the token
4365                  !!!next-token;                  !!!next-token;
4366                  redo B;                  next B;
4367                } else {                } else {
4368                  !!!cp ('t170');                  !!!cp ('t170');
4369                  #                  #
# Line 3997  sub _tree_construction_main ($) { Line 4372  sub _tree_construction_main ($) {
4372                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
4373                  ## have a table element in table scope                  ## have a table element in table scope
4374                  my $i;                  my $i;
4375                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4376                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4377                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
4378                      !!!cp ('t171');                      if ($node->[1] & CAPTION_EL) {
4379                      $i = $_;                        !!!cp ('t171');
4380                      last INSCOPE;                        $i = $_;
4381                    } elsif ({                        last INSCOPE;
4382                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4383                             }->{$node->[1]}) {                        !!!cp ('t172');
4384                      !!!cp ('t172');                        last;
4385                      last INSCOPE;                      }
4386                    }                    }
4387    
4388                      !!!cp ('t173');
4389                      !!!parse-error (type => 'unmatched end tag',
4390                                      value => $token->{tag_name}, token => $token);
4391                      ## Ignore the token
4392                      !!!next-token;
4393                      next B;
4394                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t173');  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4395                                    
4396                  ## generate implied end tags                  ## generate implied end tags
4397                  if ({                  while ($self->{open_elements}->[-1]->[1]
4398                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
   
                      ## NOTE: The following elements never appear here, maybe.  
                      td => 1, th => 1, tr => 1,  
                      tbody => 1, tfoot => 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
4399                    !!!cp ('t174');                    !!!cp ('t174');
4400                    !!!back-token;                    pop @{$self->{open_elements}};
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4401                  }                  }
4402                                    
4403                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4404                    !!!cp ('t175');                    !!!cp ('t175');
4405                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4406                                      value => $self->{open_elements}->[-1]->[0]
4407                                          ->manakai_local_name,
4408                                      token => $token);
4409                  } else {                  } else {
4410                    !!!cp ('t176');                    !!!cp ('t176');
4411                  }                  }
# Line 4047  sub _tree_construction_main ($) { Line 4417  sub _tree_construction_main ($) {
4417                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4418                                    
4419                  !!!next-token;                  !!!next-token;
4420                  redo B;                  next B;
4421                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4422                  !!!cp ('t177');                  !!!cp ('t177');
4423                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4424                  ## Ignore the token                  ## Ignore the token
4425                  !!!next-token;                  !!!next-token;
4426                  redo B;                  next B;
4427                } else {                } else {
4428                  !!!cp ('t178');                  !!!cp ('t178');
4429                  #                  #
# Line 4066  sub _tree_construction_main ($) { Line 4436  sub _tree_construction_main ($) {
4436                ## have an element in table scope                ## have an element in table scope
4437                my $i;                my $i;
4438                my $tn;                my $tn;
4439                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
4440                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
4441                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4442                    !!!cp ('t179');                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4443                    $i = $_;                      !!!cp ('t179');
4444                    last INSCOPE;                      $i = $_;
4445                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
4446                    !!!cp ('t180');                      ## Close the cell
4447                    $tn = $node->[1];                      !!!back-token; # </x>
4448                    ## NOTE: There is exactly one |td| or |th| element                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4449                    ## in scope in the stack of open elements by definition.                                line => $token->{line},
4450                  } elsif ({                                column => $token->{column}};
4451                            table => 1, html => 1,                      next B;
4452                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_CELL_EL) {
4453                    !!!cp ('t181');                      !!!cp ('t180');
4454                    last INSCOPE;                      $tn = $node->[0]->manakai_local_name;
4455                        ## NOTE: There is exactly one |td| or |th| element
4456                        ## in scope in the stack of open elements by definition.
4457                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4458                        ## ISSUE: Can this be reached?
4459                        !!!cp ('t181');
4460                        last;
4461                      }
4462                  }                  }
4463                } # INSCOPE  
               unless (defined $i) {  
4464                  !!!cp ('t182');                  !!!cp ('t182');
4465                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4466                        value => $token->{tag_name}, token => $token);
4467                  ## Ignore the token                  ## Ignore the token
4468                  !!!next-token;                  !!!next-token;
4469                  redo B;                  next B;
4470                } else {                } # INSCOPE
                 !!!cp ('t183');  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
4471              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
4472                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4473                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4474    
4475                ## As if </caption>                ## As if </caption>
4476                ## have a table element in table scope                ## have a table element in table scope
4477                my $i;                my $i;
4478                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4479                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4480                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4481                    !!!cp ('t184');                    !!!cp ('t184');
4482                    $i = $_;                    $i = $_;
4483                    last INSCOPE;                    last INSCOPE;
4484                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
4485                    !!!cp ('t185');                    !!!cp ('t185');
4486                    last INSCOPE;                    last INSCOPE;
4487                  }                  }
4488                } # INSCOPE                } # INSCOPE
4489                unless (defined $i) {                unless (defined $i) {
4490                  !!!cp ('t186');                  !!!cp ('t186');
4491                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);
4492                  ## Ignore the token                  ## Ignore the token
4493                  !!!next-token;                  !!!next-token;
4494                  redo B;                  next B;
4495                }                }
4496                                
4497                ## generate implied end tags                ## generate implied end tags
4498                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                    dd => 1, dt => 1, li => 1, p => 1,  
   
                    ## NOTE: The following elements never appear, maybe.  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot => 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
4499                  !!!cp ('t187');                  !!!cp ('t187');
4500                  !!!back-token; # </table>                  pop @{$self->{open_elements}};
                 $token = {type => END_TAG_TOKEN, tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
4501                }                }
4502    
4503                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4504                  !!!cp ('t188');                  !!!cp ('t188');
4505                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
4506                                    value => $self->{open_elements}->[-1]->[0]
4507                                        ->manakai_local_name,
4508                                    token => $token);
4509                } else {                } else {
4510                  !!!cp ('t189');                  !!!cp ('t189');
4511                }                }
# Line 4157  sub _tree_construction_main ($) { Line 4517  sub _tree_construction_main ($) {
4517                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
4518    
4519                ## reprocess                ## reprocess
4520                redo B;                next B;
4521              } elsif ({              } elsif ({
4522                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
4523                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4524                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4525                  !!!cp ('t190');                  !!!cp ('t190');
4526                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4527                  ## Ignore the token                  ## Ignore the token
4528                  !!!next-token;                  !!!next-token;
4529                  redo B;                  next B;
4530                } else {                } else {
4531                  !!!cp ('t191');                  !!!cp ('t191');
4532                  #                  #
# Line 4177  sub _tree_construction_main ($) { Line 4537  sub _tree_construction_main ($) {
4537                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
4538                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4539                !!!cp ('t192');                !!!cp ('t192');
4540                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4541                ## Ignore the token                ## Ignore the token
4542                !!!next-token;                !!!next-token;
4543                redo B;                next B;
4544              } else {              } else {
4545                !!!cp ('t193');                !!!cp ('t193');
4546                #                #
4547              }              }
4548          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4549            for my $entry (@{$self->{open_elements}}) {
4550              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
4551                !!!cp ('t75');
4552                !!!parse-error (type => 'in body:#eof', token => $token);
4553                last;
4554              }
4555            }
4556    
4557            ## Stop parsing.
4558            last B;
4559        } else {        } else {
4560          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4561        }        }
# Line 4193  sub _tree_construction_main ($) { Line 4564  sub _tree_construction_main ($) {
4564        #        #
4565      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
4566        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4567              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
4568                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4569              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4570                                
4571                unless (length $token->{data}) {            unless (length $token->{data}) {
4572                  !!!cp ('t194');              !!!cp ('t194');
4573                  !!!next-token;              !!!next-token;
4574                  redo B;              next B;
4575                } else {            } else {
4576                  !!!cp ('t195');              !!!cp ('t195');
4577                }            }
4578              }          }
4579    
4580              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
4581    
4582              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
4583              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 4213  sub _tree_construction_main ($) { Line 4585  sub _tree_construction_main ($) {
4585              ## result in a new Text node.              ## result in a new Text node.
4586              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
4587                            
4588              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]}) {  
4589                # MUST                # MUST
4590                my $foster_parent_element;                my $foster_parent_element;
4591                my $next_sibling;                my $next_sibling;
4592                my $prev_sibling;                my $prev_sibling;
4593                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
4594                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4595                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4596                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
4597                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4250  sub _tree_construction_main ($) { Line 4619  sub _tree_construction_main ($) {
4619                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
4620                     $next_sibling);                     $next_sibling);
4621                }                }
4622              } else {            $open_tables->[-1]->[1] = 1; # tainted
4623                !!!cp ('t200');          } else {
4624                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            !!!cp ('t200');
4625              }            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4626            }
4627                            
4628              !!!next-token;          !!!next-token;
4629              redo B;          next B;
4630        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4631              if ({              if ({
4632                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
# Line 4264  sub _tree_construction_main ($) { Line 4634  sub _tree_construction_main ($) {
4634                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4635                if ($self->{insertion_mode} == IN_TABLE_IM) {                if ($self->{insertion_mode} == IN_TABLE_IM) {
4636                  ## Clear back to table context                  ## Clear back to table context
4637                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
4638                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
4639                    !!!cp ('t201');                    !!!cp ('t201');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4640                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4641                  }                  }
4642                                    
4643                  !!!insert-element ('tbody');                  !!!insert-element ('tbody',, $token);
4644                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4645                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
4646                }                }
# Line 4279  sub _tree_construction_main ($) { Line 4648  sub _tree_construction_main ($) {
4648                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4649                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
4650                    !!!cp ('t202');                    !!!cp ('t202');
4651                    !!!parse-error (type => 'missing start tag:tr');                    !!!parse-error (type => 'missing start tag:tr', token => $token);
4652                  }                  }
4653                                    
4654                  ## Clear back to table body context                  ## Clear back to table body context
4655                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
4656                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
4657                    !!!cp ('t203');                    !!!cp ('t203');
4658                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4659                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4660                  }                  }
4661                                    
4662                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4663                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
4664                    !!!cp ('t204');                    !!!cp ('t204');
4665                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4666                      !!!nack ('t204');
4667                    !!!next-token;                    !!!next-token;
4668                    redo B;                    next B;
4669                  } else {                  } else {
4670                    !!!cp ('t205');                    !!!cp ('t205');
4671                    !!!insert-element ('tr');                    !!!insert-element ('tr',, $token);
4672                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
4673                  }                  }
4674                } else {                } else {
# Line 4308  sub _tree_construction_main ($) { Line 4676  sub _tree_construction_main ($) {
4676                }                }
4677    
4678                ## Clear back to table row context                ## Clear back to table row context
4679                while (not {                while (not ($self->{open_elements}->[-1]->[1]
4680                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
4681                  !!!cp ('t207');                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4682                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4683                }                }
4684                                
4685                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4686                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
4687    
4688                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
4689                                
4690                  !!!nack ('t207.1');
4691                !!!next-token;                !!!next-token;
4692                redo B;                next B;
4693              } elsif ({              } elsif ({
4694                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
4695                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4334  sub _tree_construction_main ($) { Line 4701  sub _tree_construction_main ($) {
4701                  my $i;                  my $i;
4702                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4703                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4704                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
4705                      !!!cp ('t208');                      !!!cp ('t208');
4706                      $i = $_;                      $i = $_;
4707                      last INSCOPE;                      last INSCOPE;
4708                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
4709                      !!!cp ('t209');                      !!!cp ('t209');
4710                      last INSCOPE;                      last INSCOPE;
4711                    }                    }
4712                  } # INSCOPE                  } # INSCOPE
4713                  unless (defined $i) {                  unless (defined $i) {
4714                   !!!cp ('t210');                    !!!cp ('t210');
4715  ## TODO: This type is wrong.  ## TODO: This type is wrong.
4716                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
4717                    ## Ignore the token                    ## Ignore the token
4718                      !!!nack ('t210.1');
4719                    !!!next-token;                    !!!next-token;
4720                    redo B;                    next B;
4721                  }                  }
4722                                    
4723                  ## Clear back to table row context                  ## Clear back to table row context
4724                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
4725                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
4726                    !!!cp ('t211');                    !!!cp ('t211');
4727                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4728                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4729                  }                  }
4730                                    
# Line 4372  sub _tree_construction_main ($) { Line 4733  sub _tree_construction_main ($) {
4733                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
4734                    !!!cp ('t212');                    !!!cp ('t212');
4735                    ## reprocess                    ## reprocess
4736                    redo B;                    !!!ack-later;
4737                      next B;
4738                  } else {                  } else {
4739                    !!!cp ('t213');                    !!!cp ('t213');
4740                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4384  sub _tree_construction_main ($) { Line 4746  sub _tree_construction_main ($) {
4746                  my $i;                  my $i;
4747                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4748                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4749                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
4750                      !!!cp ('t214');                      !!!cp ('t214');
4751                      $i = $_;                      $i = $_;
4752                      last INSCOPE;                      last INSCOPE;
4753                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4754                      !!!cp ('t215');                      !!!cp ('t215');
4755                      last INSCOPE;                      last INSCOPE;
4756                    }                    }
# Line 4400  sub _tree_construction_main ($) { Line 4758  sub _tree_construction_main ($) {
4758                  unless (defined $i) {                  unless (defined $i) {
4759                    !!!cp ('t216');                    !!!cp ('t216');
4760  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type ios wrong.
4761                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4762                    ## Ignore the token                    ## Ignore the token
4763                      !!!nack ('t216.1');
4764                    !!!next-token;                    !!!next-token;
4765                    redo B;                    next B;
4766                  }                  }
4767    
4768                  ## Clear back to table body context                  ## Clear back to table body context
4769                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
4770                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
4771                    !!!cp ('t217');                    !!!cp ('t217');
4772                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4773                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4774                  }                  }
4775                                    
# Line 4432  sub _tree_construction_main ($) { Line 4789  sub _tree_construction_main ($) {
4789    
4790                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
4791                  ## Clear back to table context                  ## Clear back to table context
4792                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
4793                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
4794                    !!!cp ('t219');                    !!!cp ('t219');
4795                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4796                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4797                  }                  }
4798                                    
4799                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
4800                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
4801                  ## reprocess                  ## reprocess
4802                  redo B;                  !!!ack-later;
4803                    next B;
4804                } elsif ({                } elsif ({
4805                          caption => 1,                          caption => 1,
4806                          colgroup => 1,                          colgroup => 1,
4807                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
4808                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
4809                  ## Clear back to table context                  ## Clear back to table context
4810                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
4811                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
4812                    !!!cp ('t220');                    !!!cp ('t220');
4813                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4814                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4815                  }                  }
4816                                    
4817                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
4818                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
4819                                    
4820                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4821                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
4822                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
4823                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 4470  sub _tree_construction_main ($) { Line 4826  sub _tree_construction_main ($) {
4826                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
4827                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
4828                  !!!next-token;                  !!!next-token;
4829                  redo B;                  !!!nack ('t220.1');
4830                    next B;
4831                } else {                } else {
4832                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
4833                }                }
4834              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4835                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
4836                                  value => $self->{open_elements}->[-1]->[0]
4837                                      ->manakai_local_name,
4838                                  token => $token);
4839    
4840                ## As if </table>                ## As if </table>
4841                ## have a table element in table scope                ## have a table element in table scope
4842                my $i;                my $i;
4843                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4844                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4845                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
4846                    !!!cp ('t221');                    !!!cp ('t221');
4847                    $i = $_;                    $i = $_;
4848                    last INSCOPE;                    last INSCOPE;
4849                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
4850                    !!!cp ('t222');                    !!!cp ('t222');
4851                    last INSCOPE;                    last INSCOPE;
4852                  }                  }
# Line 4497  sub _tree_construction_main ($) { Line 4854  sub _tree_construction_main ($) {
4854                unless (defined $i) {                unless (defined $i) {
4855                  !!!cp ('t223');                  !!!cp ('t223');
4856  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
4857                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:table', token => $token);
4858                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
4859                    !!!nack ('t223.1');
4860                  !!!next-token;                  !!!next-token;
4861                  redo B;                  next B;
4862                }                }
4863                                
4864    ## TODO: Followings are removed from the latest spec.
4865                ## generate implied end tags                ## generate implied end tags
4866                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
4867                  !!!cp ('t224');                  !!!cp ('t224');
4868                  !!!back-token; # <table>                  pop @{$self->{open_elements}};
                 $token = {type => END_TAG_TOKEN, tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
4869                }                }
4870    
4871                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
4872                  !!!cp ('t225');                  !!!cp ('t225');
4873  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
4874                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
4875                                    value => $self->{open_elements}->[-1]->[0]
4876                                        ->manakai_local_name,
4877                                    token => $token);
4878                } else {                } else {
4879                  !!!cp ('t226');                  !!!cp ('t226');
4880                }                }
4881    
4882                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4883                  pop @{$open_tables};
4884    
4885                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4886    
4887                ## reprocess            ## reprocess
4888                redo B;            !!!ack-later;
4889              next B;
4890            } elsif ($token->{tag_name} eq 'style') {
4891              if (not $open_tables->[-1]->[1]) { # tainted
4892                !!!cp ('t227.8');
4893                ## NOTE: This is a "as if in head" code clone.
4894                $parse_rcdata->(CDATA_CONTENT_MODEL);
4895                next B;
4896              } else {
4897                !!!cp ('t227.7');
4898                #
4899              }
4900            } elsif ($token->{tag_name} eq 'script') {
4901              if (not $open_tables->[-1]->[1]) { # tainted
4902                !!!cp ('t227.6');
4903                ## NOTE: This is a "as if in head" code clone.
4904                $script_start_tag->();
4905                next B;
4906              } else {
4907                !!!cp ('t227.5');
4908                #
4909              }
4910            } elsif ($token->{tag_name} eq 'input') {
4911              if (not $open_tables->[-1]->[1]) { # tainted
4912                if ($token->{attributes}->{type}) { ## TODO: case
4913                  my $type = lc $token->{attributes}->{type}->{value};
4914                  if ($type eq 'hidden') {
4915                    !!!cp ('t227.3');
4916                    !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
4917    
4918                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4919    
4920                    ## TODO: form element pointer
4921    
4922                    pop @{$self->{open_elements}};
4923    
4924                    !!!next-token;
4925                    !!!ack ('t227.2.1');
4926                    next B;
4927                  } else {
4928                    !!!cp ('t227.2');
4929                    #
4930                  }
4931                } else {
4932                  !!!cp ('t227.1');
4933                  #
4934                }
4935              } else {
4936                !!!cp ('t227.4');
4937                #
4938              }
4939          } else {          } else {
4940            !!!cp ('t227');            !!!cp ('t227');
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
   
           $insert = $insert_to_foster;  
4941            #            #
4942          }          }
4943    
4944            !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
4945    
4946            $insert = $insert_to_foster;
4947            #
4948        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
4949              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
4950                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 4546  sub _tree_construction_main ($) { Line 4952  sub _tree_construction_main ($) {
4952                my $i;                my $i;
4953                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4954                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4955                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
4956                    !!!cp ('t228');                    !!!cp ('t228');
4957                    $i = $_;                    $i = $_;
4958                    last INSCOPE;                    last INSCOPE;
4959                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
4960                    !!!cp ('t229');                    !!!cp ('t229');
4961                    last INSCOPE;                    last INSCOPE;
4962                  }                  }
4963                } # INSCOPE                } # INSCOPE
4964                unless (defined $i) {                unless (defined $i) {
4965                  !!!cp ('t230');                  !!!cp ('t230');
4966                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4967                  ## Ignore the token                  ## Ignore the token
4968                    !!!nack ('t230.1');
4969                  !!!next-token;                  !!!next-token;
4970                  redo B;                  next B;
4971                } else {                } else {
4972                  !!!cp ('t232');                  !!!cp ('t232');
4973                }                }
4974    
4975                ## Clear back to table row context                ## Clear back to table row context
4976                while (not {                while (not ($self->{open_elements}->[-1]->[1]
4977                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
4978                  !!!cp ('t231');                  !!!cp ('t231');
4979  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4980                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4981                }                }
4982    
4983                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
4984                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
4985                !!!next-token;                !!!next-token;
4986                redo B;                !!!nack ('t231.1');
4987                  next B;
4988              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4989                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
4990                  ## As if </tr>                  ## As if </tr>
# Line 4588  sub _tree_construction_main ($) { Line 4992  sub _tree_construction_main ($) {
4992                  my $i;                  my $i;
4993                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4994                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4995                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
4996                      !!!cp ('t233');                      !!!cp ('t233');
4997                      $i = $_;                      $i = $_;
4998                      last INSCOPE;                      last INSCOPE;
4999                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5000                      !!!cp ('t234');                      !!!cp ('t234');
5001                      last INSCOPE;                      last INSCOPE;
5002                    }                    }
# Line 4602  sub _tree_construction_main ($) { Line 5004  sub _tree_construction_main ($) {
5004                  unless (defined $i) {                  unless (defined $i) {
5005                    !!!cp ('t235');                    !!!cp ('t235');
5006  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5007                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
5008                    ## Ignore the token                    ## Ignore the token
5009                      !!!nack ('t236.1');
5010                    !!!next-token;                    !!!next-token;
5011                    redo B;                    next B;
5012                  }                  }
5013                                    
5014                  ## Clear back to table row context                  ## Clear back to table row context
5015                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5016                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5017                    !!!cp ('t236');                    !!!cp ('t236');
5018  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5019                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5020                  }                  }
5021                                    
# Line 4628  sub _tree_construction_main ($) { Line 5029  sub _tree_construction_main ($) {
5029                  my $i;                  my $i;
5030                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5031                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5032                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5033                      !!!cp ('t237');                      !!!cp ('t237');
5034                      $i = $_;                      $i = $_;
5035                      last INSCOPE;                      last INSCOPE;
5036                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5037                      !!!cp ('t238');                      !!!cp ('t238');
5038                      last INSCOPE;                      last INSCOPE;
5039                    }                    }
5040                  } # INSCOPE                  } # INSCOPE
5041                  unless (defined $i) {                  unless (defined $i) {
5042                    !!!cp ('t239');                    !!!cp ('t239');
5043                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5044                    ## Ignore the token                    ## Ignore the token
5045                      !!!nack ('t239.1');
5046                    !!!next-token;                    !!!next-token;
5047                    redo B;                    next B;
5048                  }                  }
5049                                    
5050                  ## Clear back to table body context                  ## Clear back to table body context
5051                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5052                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5053                    !!!cp ('t240');                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5054                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5055                  }                  }
5056                                    
# Line 4670  sub _tree_construction_main ($) { Line 5066  sub _tree_construction_main ($) {
5066                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
5067                }                }
5068    
5069                  ## NOTE: </table> in the "in table" insertion mode.
5070                  ## When you edit the code fragment below, please ensure that
5071                  ## the code for <table> in the "in table" insertion mode
5072                  ## is synced with it.
5073    
5074                ## have a table element in table scope                ## have a table element in table scope
5075                my $i;                my $i;
5076                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5077                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5078                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5079                    !!!cp ('t241');                    !!!cp ('t241');
5080                    $i = $_;                    $i = $_;
5081                    last INSCOPE;                    last INSCOPE;
5082                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5083                    !!!cp ('t242');                    !!!cp ('t242');
5084                    last INSCOPE;                    last INSCOPE;
5085                  }                  }
5086                } # INSCOPE                } # INSCOPE
5087                unless (defined $i) {                unless (defined $i) {
5088                  !!!cp ('t243');                  !!!cp ('t243');
5089                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5090                  ## Ignore the token                  ## Ignore the token
5091                    !!!nack ('t243.1');
5092                  !!!next-token;                  !!!next-token;
5093                  redo B;                  next B;
               }  
   
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!cp ('t244');  
 ## ISSUE: Can this case be reached?  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
                 
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!cp ('t245');  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               } else {  
                 !!!cp ('t246');  
5094                }                }
5095                                    
5096                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5097                  pop @{$open_tables};
5098                                
5099                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5100                                
5101                !!!next-token;                !!!next-token;
5102                redo B;                next B;
5103              } elsif ({              } elsif ({
5104                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5105                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4729  sub _tree_construction_main ($) { Line 5109  sub _tree_construction_main ($) {
5109                  my $i;                  my $i;
5110                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5111                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5112                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5113                      !!!cp ('t247');                      !!!cp ('t247');
5114                      $i = $_;                      $i = $_;
5115                      last INSCOPE;                      last INSCOPE;
5116                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5117                      !!!cp ('t248');                      !!!cp ('t248');
5118                      last INSCOPE;                      last INSCOPE;
5119                    }                    }
5120                  } # INSCOPE                  } # INSCOPE
5121                    unless (defined $i) {                    unless (defined $i) {
5122                      !!!cp ('t249');                      !!!cp ('t249');
5123                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5124                      ## Ignore the token                      ## Ignore the token
5125                        !!!nack ('t249.1');
5126                      !!!next-token;                      !!!next-token;
5127                      redo B;                      next B;
5128                    }                    }
5129                                    
5130                  ## As if </tr>                  ## As if </tr>
# Line 4753  sub _tree_construction_main ($) { Line 5132  sub _tree_construction_main ($) {
5132                  my $i;                  my $i;
5133                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5134                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5135                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5136                      !!!cp ('t250');                      !!!cp ('t250');
5137                      $i = $_;                      $i = $_;
5138                      last INSCOPE;                      last INSCOPE;
5139                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5140                      !!!cp ('t251');                      !!!cp ('t251');
5141                      last INSCOPE;                      last INSCOPE;
5142                    }                    }
5143                  } # INSCOPE                  } # INSCOPE
5144                    unless (defined $i) {                    unless (defined $i) {
5145                      !!!cp ('t252');                      !!!cp ('t252');
5146                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5147                      ## Ignore the token                      ## Ignore the token
5148                        !!!nack ('t252.1');
5149                      !!!next-token;                      !!!next-token;
5150                      redo B;                      next B;
5151                    }                    }
5152                                    
5153                  ## Clear back to table row context                  ## Clear back to table row context
5154                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5155                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5156                    !!!cp ('t253');                    !!!cp ('t253');
5157  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5158                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5159                  }                  }
5160                                    
# Line 4791  sub _tree_construction_main ($) { Line 5167  sub _tree_construction_main ($) {
5167                my $i;                my $i;
5168                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5169                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5170                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5171                    !!!cp ('t254');                    !!!cp ('t254');
5172                    $i = $_;                    $i = $_;
5173                    last INSCOPE;                    last INSCOPE;
5174                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5175                    !!!cp ('t255');                    !!!cp ('t255');
5176                    last INSCOPE;                    last INSCOPE;
5177                  }                  }
5178                } # INSCOPE                } # INSCOPE
5179                unless (defined $i) {                unless (defined $i) {
5180                  !!!cp ('t256');                  !!!cp ('t256');
5181                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5182                  ## Ignore the token                  ## Ignore the token
5183                    !!!nack ('t256.1');
5184                  !!!next-token;                  !!!next-token;
5185                  redo B;                  next B;
5186                }                }
5187    
5188                ## Clear back to table body context                ## Clear back to table body context
5189                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5190                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5191                  !!!cp ('t257');                  !!!cp ('t257');
5192  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5193                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5194                }                }
5195    
5196                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5197                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5198                  !!!nack ('t257.1');
5199                !!!next-token;                !!!next-token;
5200                redo B;                next B;
5201              } elsif ({              } elsif ({
5202                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5203                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5204                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5205                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5206                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5207                !!!cp ('t258');            !!!cp ('t258');
5208                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5209                ## Ignore the token            ## Ignore the token
5210                !!!next-token;            !!!nack ('t258.1');
5211                redo B;             !!!next-token;
5212              next B;
5213          } else {          } else {
5214            !!!cp ('t259');            !!!cp ('t259');
5215            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
5216    
5217            $insert = $insert_to_foster;            $insert = $insert_to_foster;
5218            #            #
5219          }          }
5220          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5221            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5222                    @{$self->{open_elements}} == 1) { # redundant, maybe
5223              !!!parse-error (type => 'in body:#eof', token => $token);
5224              !!!cp ('t259.1');
5225              #
5226            } else {
5227              !!!cp ('t259.2');
5228              #
5229            }
5230    
5231            ## Stop parsing
5232            last B;
5233        } else {        } else {
5234          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5235        }        }
# Line 4852  sub _tree_construction_main ($) { Line 5240  sub _tree_construction_main ($) {
5240                unless (length $token->{data}) {                unless (length $token->{data}) {
5241                  !!!cp ('t260');                  !!!cp ('t260');
5242                  !!!next-token;                  !!!next-token;
5243                  redo B;                  next B;
5244                }                }
5245              }              }
5246                            
# Line 4861  sub _tree_construction_main ($) { Line 5249  sub _tree_construction_main ($) {
5249            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5250              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
5251                !!!cp ('t262');                !!!cp ('t262');
5252                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5253                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5254                  !!!ack ('t262.1');
5255                !!!next-token;                !!!next-token;
5256                redo B;                next B;
5257              } else {              } else {
5258                !!!cp ('t263');                !!!cp ('t263');
5259                #                #
5260              }              }
5261            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5262              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5263                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5264                  !!!cp ('t264');                  !!!cp ('t264');
5265                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5266                  ## Ignore the token                  ## Ignore the token
5267                  !!!next-token;                  !!!next-token;
5268                  redo B;                  next B;
5269                } else {                } else {
5270                  !!!cp ('t265');                  !!!cp ('t265');
5271                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5272                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5273                  !!!next-token;                  !!!next-token;
5274                  redo B;                              next B;            
5275                }                }
5276              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5277                !!!cp ('t266');                !!!cp ('t266');
5278                !!!parse-error (type => 'unmatched end tag:col');                !!!parse-error (type => 'unmatched end tag:col', token => $token);
5279                ## Ignore the token                ## Ignore the token
5280                !!!next-token;                !!!next-token;
5281                redo B;                next B;
5282              } else {              } else {
5283                !!!cp ('t267');                !!!cp ('t267');
5284                #                #
5285              }              }
5286            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5287              die "$0: $token->{type}: Unknown token type";          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5288            }              @{$self->{open_elements}} == 1) { # redundant, maybe
5289              !!!cp ('t270.2');
5290              ## Stop parsing.
5291              last B;
5292            } else {
5293              ## NOTE: As if </colgroup>.
5294              !!!cp ('t270.1');
5295              pop @{$self->{open_elements}}; # colgroup
5296              $self->{insertion_mode} = IN_TABLE_IM;
5297              ## Reprocess.
5298              next B;
5299            }
5300          } else {
5301            die "$0: $token->{type}: Unknown token type";
5302          }
5303    
5304            ## As if </colgroup>            ## As if </colgroup>
5305            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5306              !!!cp ('t269');              !!!cp ('t269');
5307              !!!parse-error (type => 'unmatched end tag:colgroup');  ## TODO: Wrong error type?
5308                !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5309              ## Ignore the token              ## Ignore the token
5310                !!!nack ('t269.1');
5311              !!!next-token;              !!!next-token;
5312              redo B;              next B;
5313            } else {            } else {
5314              !!!cp ('t270');              !!!cp ('t270');
5315              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5316              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5317                !!!ack-later;
5318              ## reprocess              ## reprocess
5319              redo B;              next B;
5320            }            }
5321      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5322        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5323          !!!cp ('t271');          !!!cp ('t271');
5324          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5325          !!!next-token;          !!!next-token;
5326          redo B;          next B;
5327        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5328              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5329                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5330                  !!!cp ('t272');              !!!cp ('t272');
5331                  ## As if </option>              ## As if </option>
5332                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5333                } else {            } else {
5334                  !!!cp ('t273');              !!!cp ('t273');
5335                }            }
5336    
5337                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5338                !!!next-token;            !!!nack ('t273.1');
5339                redo B;            !!!next-token;
5340              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5341                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5342                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5343                  ## As if </option>              !!!cp ('t274');
5344                  pop @{$self->{open_elements}};              ## As if </option>
5345                } else {              pop @{$self->{open_elements}};
5346                  !!!cp ('t275');            } else {
5347                }              !!!cp ('t275');
5348              }
5349    
5350                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5351                  !!!cp ('t276');              !!!cp ('t276');
5352                  ## As if </optgroup>              ## As if </optgroup>
5353                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5354                } else {            } else {
5355                  !!!cp ('t277');              !!!cp ('t277');
5356                }            }
5357    
5358                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5359                !!!next-token;            !!!nack ('t277.1');
5360                redo B;            !!!next-token;
5361              } elsif ($token->{tag_name} eq 'select') {            next B;
5362  ## TODO: The type below is not good - <select> is replaced by </select>          } elsif ($token->{tag_name} eq 'select' or
5363                !!!parse-error (type => 'not closed:select');                   $token->{tag_name} eq 'input' or
5364                ## As if </select> instead                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5365                ## have an element in table scope                    {
5366                my $i;                     caption => 1, table => 1,
5367                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                     tbody => 1, tfoot => 1, thead => 1,
5368                  my $node = $self->{open_elements}->[$_];                     tr => 1, td => 1, th => 1,
5369                  if ($node->[1] eq $token->{tag_name}) {                    }->{$token->{tag_name}})) {
5370                    !!!cp ('t278');            ## TODO: The type below is not good - <select> is replaced by </select>
5371                    $i = $_;            !!!parse-error (type => 'not closed:select', token => $token);
5372                    last INSCOPE;            ## NOTE: As if the token were </select> (<select> case) or
5373                  } elsif ({            ## as if there were </select> (otherwise).
5374                            table => 1, html => 1,            ## have an element in table scope
5375                           }->{$node->[1]}) {            my $i;
5376                    !!!cp ('t279');            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5377                    last INSCOPE;              my $node = $self->{open_elements}->[$_];
5378                  }              if ($node->[1] & SELECT_EL) {
5379                } # INSCOPE                !!!cp ('t278');
5380                unless (defined $i) {                $i = $_;
5381                  !!!cp ('t280');                last INSCOPE;
5382                  !!!parse-error (type => 'unmatched end tag:select');              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5383                  ## Ignore the token                !!!cp ('t279');
5384                  !!!next-token;                last INSCOPE;
5385                  redo B;              }
5386                }            } # INSCOPE
5387              unless (defined $i) {
5388                !!!cp ('t280');
5389                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5390                ## Ignore the token
5391                !!!nack ('t280.1');
5392                !!!next-token;
5393                next B;
5394              }
5395                                
5396                !!!cp ('t281');            !!!cp ('t281');
5397                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5398    
5399                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5400    
5401                !!!next-token;            if ($token->{tag_name} eq 'select') {
5402                redo B;              !!!nack ('t281.2');
5403                !!!next-token;
5404                next B;
5405              } else {
5406                !!!cp ('t281.1');
5407                !!!ack-later;
5408                ## Reprocess the token.
5409                next B;
5410              }
5411          } else {          } else {
5412            !!!cp ('t282');            !!!cp ('t282');
5413            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5414            ## Ignore the token            ## Ignore the token
5415              !!!nack ('t282.1');
5416            !!!next-token;            !!!next-token;
5417            redo B;            next B;
5418          }          }
5419        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5420              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
5421                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5422                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5423                  !!!cp ('t283');              !!!cp ('t283');
5424                  ## As if </option>              ## As if </option>
5425                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
5426                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5427                  !!!cp ('t284');              !!!cp ('t284');
5428                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5429                } else {            } else {
5430                  !!!cp ('t285');              !!!cp ('t285');
5431                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5432                  ## Ignore the token              ## Ignore the token
5433                }            }
5434                !!!next-token;            !!!nack ('t285.1');
5435                redo B;            !!!next-token;
5436              } elsif ($token->{tag_name} eq 'option') {            next B;
5437                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'option') {
5438                  !!!cp ('t286');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5439                  pop @{$self->{open_elements}};              !!!cp ('t286');
5440                } else {              pop @{$self->{open_elements}};
5441                  !!!cp ('t287');            } else {
5442                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t287');
5443                  ## Ignore the token              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5444                }              ## Ignore the token
5445                !!!next-token;            }
5446                redo B;            !!!nack ('t287.1');
5447              } elsif ($token->{tag_name} eq 'select') {            !!!next-token;
5448                ## have an element in table scope            next B;
5449                my $i;          } elsif ($token->{tag_name} eq 'select') {
5450                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            ## have an element in table scope
5451                  my $node = $self->{open_elements}->[$_];            my $i;
5452                  if ($node->[1] eq $token->{tag_name}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5453                    !!!cp ('t288');              my $node = $self->{open_elements}->[$_];
5454                    $i = $_;              if ($node->[1] & SELECT_EL) {
5455                    last INSCOPE;                !!!cp ('t288');
5456                  } elsif ({                $i = $_;
5457                            table => 1, html => 1,                last INSCOPE;
5458                           }->{$node->[1]}) {              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5459                    !!!cp ('t289');                !!!cp ('t289');
5460                    last INSCOPE;                last INSCOPE;
5461                  }              }
5462                } # INSCOPE            } # INSCOPE
5463                unless (defined $i) {            unless (defined $i) {
5464                  !!!cp ('t290');              !!!cp ('t290');
5465                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5466                  ## Ignore the token              ## Ignore the token
5467                  !!!next-token;              !!!nack ('t290.1');
5468                  redo B;              !!!next-token;
5469                }              next B;
5470              }
5471                                
5472                !!!cp ('t291');            !!!cp ('t291');
5473                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5474    
5475                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5476    
5477                !!!next-token;            !!!nack ('t291.1');
5478                redo B;            !!!next-token;
5479              } elsif ({            next B;
5480                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5481                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
5482                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
5483                      tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5484                     }->{$token->{tag_name}}) {
5485  ## TODO: The following is wrong?  ## TODO: The following is wrong?
5486                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5487                                
5488                ## have an element in table scope            ## have an element in table scope
5489                my $i;            my $i;
5490                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5491                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5492                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5493                    !!!cp ('t292');                !!!cp ('t292');
5494                    $i = $_;                $i = $_;
5495                    last INSCOPE;                last INSCOPE;
5496                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5497                            table => 1, html => 1,                !!!cp ('t293');
5498                           }->{$node->[1]}) {                last INSCOPE;
5499                    !!!cp ('t293');              }
5500                    last INSCOPE;            } # INSCOPE
5501                  }            unless (defined $i) {
5502                } # INSCOPE              !!!cp ('t294');
5503                unless (defined $i) {              ## Ignore the token
5504                  !!!cp ('t294');              !!!nack ('t294.1');
5505                  ## Ignore the token              !!!next-token;
5506                  !!!next-token;              next B;
5507                  redo B;            }
               }  
5508                                
5509                ## As if </select>            ## As if </select>
5510                ## have an element in table scope            ## have an element in table scope
5511                undef $i;            undef $i;
5512                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5513                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5514                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
5515                    !!!cp ('t295');                !!!cp ('t295');
5516                    $i = $_;                $i = $_;
5517                    last INSCOPE;                last INSCOPE;
5518                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5519  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5520                    !!!cp ('t296');                !!!cp ('t296');
5521                    last INSCOPE;                last INSCOPE;
5522                  }              }
5523                } # INSCOPE            } # INSCOPE
5524                unless (defined $i) {            unless (defined $i) {
5525                  !!!cp ('t297');              !!!cp ('t297');
5526  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
5527                  !!!parse-error (type => 'unmatched end tag:select');              !!!parse-error (type => 'unmatched end tag:select', token => $token);
5528                  ## Ignore the </select> token              ## Ignore the </select> token
5529                  !!!next-token; ## TODO: ok?              !!!nack ('t297.1');
5530                  redo B;              !!!next-token; ## TODO: ok?
5531                }              next B;
5532              }
5533                                
5534                !!!cp ('t298');            !!!cp ('t298');
5535                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5536    
5537                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5538    
5539                ## reprocess            !!!ack-later;
5540                redo B;            ## reprocess
5541              next B;
5542          } else {          } else {
5543            !!!cp ('t299');            !!!cp ('t299');
5544            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
5545            ## Ignore the token            ## Ignore the token
5546              !!!nack ('t299.3');
5547            !!!next-token;            !!!next-token;
5548            redo B;            next B;
5549            }
5550          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5551            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5552                    @{$self->{open_elements}} == 1) { # redundant, maybe
5553              !!!cp ('t299.1');
5554              !!!parse-error (type => 'in body:#eof', token => $token);
5555            } else {
5556              !!!cp ('t299.2');
5557          }          }
5558    
5559            ## Stop parsing.
5560            last B;
5561        } else {        } else {
5562          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5563        }        }
# Line 5135  sub _tree_construction_main ($) { Line 5573  sub _tree_construction_main ($) {
5573            unless (length $token->{data}) {            unless (length $token->{data}) {
5574              !!!cp ('t300');              !!!cp ('t300');
5575              !!!next-token;              !!!next-token;
5576              redo B;              next B;
5577            }            }
5578          }          }
5579                    
5580          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5581            !!!cp ('t301');            !!!cp ('t301');
5582            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#character', token => $token);
5583    
5584            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
5585          } else {          } else {
# Line 5149  sub _tree_construction_main ($) { Line 5587  sub _tree_construction_main ($) {
5587          }          }
5588                    
5589          ## "after body" insertion mode          ## "after body" insertion mode
5590          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#character', token => $token);
5591    
5592          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
5593          ## reprocess          ## reprocess
5594          redo B;          next B;
5595        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5596          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5597            !!!cp ('t303');            !!!cp ('t303');
5598            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5599                        
5600            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
5601          } else {          } else {
# Line 5165  sub _tree_construction_main ($) { Line 5603  sub _tree_construction_main ($) {
5603          }          }
5604    
5605          ## "after body" insertion mode          ## "after body" insertion mode
5606          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
5607    
5608          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
5609            !!!ack-later;
5610          ## reprocess          ## reprocess
5611          redo B;          next B;
5612        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5613          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5614            !!!cp ('t305');            !!!cp ('t305');
5615            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
5616                        
5617            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
5618            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
# Line 5185  sub _tree_construction_main ($) { Line 5624  sub _tree_construction_main ($) {
5624          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
5625            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
5626              !!!cp ('t307');              !!!cp ('t307');
5627              !!!parse-error (type => 'unmatched end tag:html');              !!!parse-error (type => 'unmatched end tag:html', token => $token);
5628              ## Ignore the token              ## Ignore the token
5629              !!!next-token;              !!!next-token;
5630              redo B;              next B;
5631            } else {            } else {
5632              !!!cp ('t308');              !!!cp ('t308');
5633              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
5634              !!!next-token;              !!!next-token;
5635              redo B;              next B;
5636            }            }
5637          } else {          } else {
5638            !!!cp ('t309');            !!!cp ('t309');
5639            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
5640    
5641            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
5642            ## reprocess            ## reprocess
5643            redo B;            next B;
5644          }          }
5645          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5646            !!!cp ('t309.2');
5647            ## Stop parsing
5648            last B;
5649        } else {        } else {
5650          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5651        }        }
# Line 5214  sub _tree_construction_main ($) { Line 5657  sub _tree_construction_main ($) {
5657            unless (length $token->{data}) {            unless (length $token->{data}) {
5658              !!!cp ('t310');              !!!cp ('t310');
5659              !!!next-token;              !!!next-token;
5660              redo B;              next B;
5661            }            }
5662          }          }
5663                    
5664          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
5665            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5666              !!!cp ('t311');              !!!cp ('t311');
5667              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#character', token => $token);
5668            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
5669              !!!cp ('t312');              !!!cp ('t312');
5670              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
5671            } else { # "after html frameset"            } else { # "after html frameset"
5672              !!!cp ('t313');              !!!cp ('t313');
5673              !!!parse-error (type => 'after html:#character');              !!!parse-error (type => 'after html:#character', token => $token);
5674    
5675              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5676              ## Reprocess in the "after frameset" insertion mode.              ## Reprocess in the "after frameset" insertion mode.
5677              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
5678            }            }
5679                        
5680            ## Ignore the token.            ## Ignore the token.
# Line 5242  sub _tree_construction_main ($) { Line 5685  sub _tree_construction_main ($) {
5685              !!!cp ('t315');              !!!cp ('t315');
5686              !!!next-token;              !!!next-token;
5687            }            }
5688            redo B;            next B;
5689          }          }
5690                    
5691          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
5692        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5693          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5694            !!!cp ('t316');            !!!cp ('t316');
5695            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5696    
5697            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
5698            ## Process in the "after frameset" insertion mode.            ## Process in the "after frameset" insertion mode.
# Line 5260  sub _tree_construction_main ($) { Line 5703  sub _tree_construction_main ($) {
5703          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
5704              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
5705            !!!cp ('t318');            !!!cp ('t318');
5706            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5707              !!!nack ('t318.1');
5708            !!!next-token;            !!!next-token;
5709            redo B;            next B;
5710          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
5711                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
5712            !!!cp ('t319');            !!!cp ('t319');
5713            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5714            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
5715              !!!ack ('t319.1');
5716            !!!next-token;            !!!next-token;
5717            redo B;            next B;
5718          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
5719            !!!cp ('t320');            !!!cp ('t320');
5720            ## NOTE: As if in body.            ## NOTE: As if in body.
5721            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL);
5722            redo B;            next B;
5723          } else {          } else {
5724            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5725              !!!cp ('t321');              !!!cp ('t321');
5726              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
5727            } else {            } else {
5728              !!!cp ('t322');              !!!cp ('t322');
5729              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
5730            }            }
5731            ## Ignore the token            ## Ignore the token
5732              !!!nack ('t322.1');
5733            !!!next-token;            !!!next-token;
5734            redo B;            next B;
5735          }          }
5736        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5737          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5738            !!!cp ('t323');            !!!cp ('t323');
5739            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
5740    
5741            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
5742            ## Process in the "after frameset" insertion mode.            ## Process in the "after frameset" insertion mode.
# Line 5300  sub _tree_construction_main ($) { Line 5746  sub _tree_construction_main ($) {
5746    
5747          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
5748              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
5749            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5750                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
5751              !!!cp ('t325');              !!!cp ('t325');
5752              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5753              ## Ignore the token              ## Ignore the token
5754              !!!next-token;              !!!next-token;
5755            } else {            } else {
# Line 5313  sub _tree_construction_main ($) { Line 5759  sub _tree_construction_main ($) {
5759            }            }
5760    
5761            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
5762                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
5763              !!!cp ('t327');              !!!cp ('t327');
5764              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5765            } else {            } else {
5766              !!!cp ('t328');              !!!cp ('t328');
5767            }            }
5768            redo B;            next B;
5769          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
5770                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
5771            !!!cp ('t329');            !!!cp ('t329');
5772            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
5773            !!!next-token;            !!!next-token;
5774            redo B;            next B;
5775          } else {          } else {
5776            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5777              !!!cp ('t330');              !!!cp ('t330');
5778              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
5779            } else {            } else {
5780              !!!cp ('t331');              !!!cp ('t331');
5781              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
5782            }            }
5783            ## Ignore the token            ## Ignore the token
5784            !!!next-token;            !!!next-token;
5785            redo B;            next B;
5786            }
5787          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5788            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5789                    @{$self->{open_elements}} == 1) { # redundant, maybe
5790              !!!cp ('t331.1');
5791              !!!parse-error (type => 'in body:#eof', token => $token);
5792            } else {
5793              !!!cp ('t331.2');
5794          }          }
5795            
5796            ## Stop parsing
5797            last B;
5798        } else {        } else {
5799          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5800        }        }
# Line 5352  sub _tree_construction_main ($) { Line 5809  sub _tree_construction_main ($) {
5809        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
5810          !!!cp ('t332');          !!!cp ('t332');
5811          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5812          $script_start_tag->($insert);          $script_start_tag->();
5813          redo B;          next B;
5814        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
5815          !!!cp ('t333');          !!!cp ('t333');
5816          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5817          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
5818          redo B;          next B;
5819        } elsif ({        } elsif ({
5820                  base => 1, link => 1,                  base => 1, link => 1,
5821                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5822          !!!cp ('t334');          !!!cp ('t334');
5823          ## 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
5824          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5825          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
5826            !!!ack ('t334.1');
5827          !!!next-token;          !!!next-token;
5828          redo B;          next B;
5829        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
5830          ## 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
5831          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5832          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.
5833    
5834          unless ($self->{confident}) {          unless ($self->{confident}) {
5835            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) { ## TODO: And if supported
5836              !!!cp ('t335');              !!!cp ('t335');
5837              $self->{change_encoding}              $self->{change_encoding}
5838                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
5839                            
5840              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5841                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
# Line 5392  sub _tree_construction_main ($) { Line 5850  sub _tree_construction_main ($) {
5850                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
5851                !!!cp ('t336');                !!!cp ('t336');
5852                $self->{change_encoding}                $self->{change_encoding}
5853                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
5854                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5855                    ->set_user_data (manakai_has_reference =>                    ->set_user_data (manakai_has_reference =>
5856                                         $token->{attributes}->{content}                                         $token->{attributes}->{content}
# Line 5416  sub _tree_construction_main ($) { Line 5874  sub _tree_construction_main ($) {
5874            }            }
5875          }          }
5876    
5877            !!!ack ('t338.1');
5878          !!!next-token;          !!!next-token;
5879          redo B;          next B;
5880        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
5881          !!!cp ('t341');          !!!cp ('t341');
         !!!parse-error (type => 'in body:title');  
5882          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5883          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
5884            if (defined $self->{head_element}) {          next B;
             !!!cp ('t339');  
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             !!!cp ('t340');  
             $insert->($_[0]);  
           }  
         });  
         redo B;  
5885        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
5886          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body', token => $token);
5887                                
5888          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
5889              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
5890            !!!cp ('t342');            !!!cp ('t342');
5891            ## Ignore the token            ## Ignore the token
5892          } else {          } else {
# Line 5450  sub _tree_construction_main ($) { Line 5900  sub _tree_construction_main ($) {
5900              }              }
5901            }            }
5902          }          }
5903            !!!nack ('t343.1');
5904          !!!next-token;          !!!next-token;
5905          redo B;          next B;
5906        } elsif ({        } elsif ({
5907                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
5908                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1,
5909                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5910                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
5911                  pre => 1,                  pre => 1, listing => 1,
5912                    form => 1,
5913                    table => 1,
5914                    hr => 1,
5915                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5916            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
5917              !!!cp ('t350');
5918              !!!parse-error (type => 'in form:form', token => $token);
5919              ## Ignore the token
5920              !!!nack ('t350.1');
5921              !!!next-token;
5922              next B;
5923            }
5924    
5925          ## has a p element in scope          ## has a p element in scope
5926          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5927            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
5928              !!!cp ('t344');              !!!cp ('t344');
5929              !!!back-token;              !!!back-token; # <form>
5930              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
5931              redo B;                        line => $token->{line}, column => $token->{column}};
5932            } elsif ({              next B;
5933                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
5934              !!!cp ('t345');              !!!cp ('t345');
5935              last INSCOPE;              last INSCOPE;
5936            }            }
5937          } # INSCOPE          } # INSCOPE
5938                        
5939          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5940          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
5941              !!!nack ('t346.1');
5942            !!!next-token;            !!!next-token;
5943            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
5944              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5488  sub _tree_construction_main ($) { Line 5951  sub _tree_construction_main ($) {
5951            } else {            } else {
5952              !!!cp ('t348');              !!!cp ('t348');
5953            }            }
5954          } else {          } elsif ($token->{tag_name} eq 'form') {
5955            !!!cp ('t347');            !!!cp ('t347.1');
5956              $self->{form_element} = $self->{open_elements}->[-1]->[0];
5957    
5958              !!!nack ('t347.2');
5959            !!!next-token;            !!!next-token;
5960          }          } elsif ($token->{tag_name} eq 'table') {
5961          redo B;            !!!cp ('t382');
5962        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
5963          if (defined $self->{form_element}) {            
5964            !!!cp ('t350');            $self->{insertion_mode} = IN_TABLE_IM;
5965            !!!parse-error (type => 'in form:form');  
5966            ## Ignore the token            !!!nack ('t382.1');
5967              !!!next-token;
5968            } elsif ($token->{tag_name} eq 'hr') {
5969              !!!cp ('t386');
5970              pop @{$self->{open_elements}};
5971            
5972              !!!nack ('t386.1');
5973            !!!next-token;            !!!next-token;
           redo B;  
5974          } else {          } else {
5975            ## has a p element in scope            !!!nack ('t347.1');
           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];  
5976            !!!next-token;            !!!next-token;
           redo B;  
5977          }          }
5978        } elsif ($token->{tag_name} eq 'li') {          next B;
5979          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
5980          ## has a p element in scope          ## has a p element in scope
5981          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5982            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
5983              !!!cp ('t353');              !!!cp ('t353');
5984              !!!back-token;              !!!back-token; # <x>
5985              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
5986              redo B;                        line => $token->{line}, column => $token->{column}};
5987            } elsif ({              next B;
5988                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
5989              !!!cp ('t354');              !!!cp ('t354');
5990              last INSCOPE;              last INSCOPE;
5991            }            }
# Line 5542  sub _tree_construction_main ($) { Line 5994  sub _tree_construction_main ($) {
5994          ## Step 1          ## Step 1
5995          my $i = -1;          my $i = -1;
5996          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
5997            my $li_or_dtdd = {li => {li => 1},
5998                              dt => {dt => 1, dd => 1},
5999                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6000          LI: {          LI: {
6001            ## Step 2            ## Step 2
6002            if ($node->[1] eq 'li') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6003              if ($i != -1) {              if ($i != -1) {
6004                !!!cp ('t355');                !!!cp ('t355');
6005                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6006                                $self->{open_elements}->[-1]->[1]);                                value => $self->{open_elements}->[-1]->[0]
6007                                      ->manakai_local_name,
6008                                  token => $token);
6009              } else {              } else {
6010                !!!cp ('t356');                !!!cp ('t356');
6011              }              }
# Line 5559  sub _tree_construction_main ($) { Line 6016  sub _tree_construction_main ($) {
6016            }            }
6017                        
6018            ## Step 3            ## Step 3
6019            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6020                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6021                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6022                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6023                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6024                  not ($node->[1] & DIV_EL)) {
6025              !!!cp ('t358');              !!!cp ('t358');
6026              last LI;              last LI;
6027            }            }
# Line 5575  sub _tree_construction_main ($) { Line 6033  sub _tree_construction_main ($) {
6033            redo LI;            redo LI;
6034          } # LI          } # LI
6035                        
6036          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6037          !!!next-token;          !!!nack ('t359.1');
         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});  
6038          !!!next-token;          !!!next-token;
6039          redo B;          next B;
6040        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6041          ## has a p element in scope          ## has a p element in scope
6042          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6043            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6044              !!!cp ('t367');              !!!cp ('t367');
6045              !!!back-token;              !!!back-token; # <plaintext>
6046              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6047              redo B;                        line => $token->{line}, column => $token->{column}};
6048            } elsif ({              next B;
6049                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6050              !!!cp ('t368');              !!!cp ('t368');
6051              last INSCOPE;              last INSCOPE;
6052            }            }
6053          } # INSCOPE          } # INSCOPE
6054                        
6055          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6056                        
6057          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6058                        
6059            !!!nack ('t368.1');
6060          !!!next-token;          !!!next-token;
6061          redo B;          next B;
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!cp ('t369');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             !!!cp ('t370');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>  
         ## has an element in scope  
         #my $i;  
         #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
         #  my $node = $self->{open_elements}->[$_];  
         #  if ({  
         #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
         #      }->{$node->[1]}) {  
         #    $i = $_;  
         #    last INSCOPE;  
         #  } elsif ({  
         #            table => 1, caption => 1, td => 1, th => 1,  
         #            button => 1, marquee => 1, object => 1, html => 1,  
         #           }->{$node->[1]}) {  
         #    last INSCOPE;  
         #  }  
         #} # INSCOPE  
         #    
         #if (defined $i) {  
         #  !!! parse-error (type => 'in hn:hn');  
         #  splice @{$self->{open_elements}}, $i;  
         #}  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         !!!next-token;  
         redo B;  
6062        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6063          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6064            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6065            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6066              !!!cp ('t371');              !!!cp ('t371');
6067              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a', token => $token);
6068                            
6069              !!!back-token;              !!!back-token; # <a>
6070              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6071              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
6072                $formatting_end_tag->($token);
6073                            
6074              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
6075                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
# Line 5738  sub _tree_construction_main ($) { Line 6094  sub _tree_construction_main ($) {
6094                        
6095          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6096    
6097          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6098          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6099    
6100            !!!nack ('t374.1');
6101          !!!next-token;          !!!next-token;
6102          redo B;          next 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;  
6103        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6104          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6105    
6106          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6107          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6108            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6109            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6110              !!!cp ('t376');              !!!cp ('t376');
6111              !!!parse-error (type => 'in nobr:nobr');              !!!parse-error (type => 'in nobr:nobr', token => $token);
6112              !!!back-token;              !!!back-token; # <nobr>
6113              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6114              redo B;                        line => $token->{line}, column => $token->{column}};
6115            } elsif ({              next B;
6116                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6117              !!!cp ('t377');              !!!cp ('t377');
6118              last INSCOPE;              last INSCOPE;
6119            }            }
6120          } # INSCOPE          } # INSCOPE
6121                    
6122          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6123          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6124                    
6125            !!!nack ('t377.1');
6126          !!!next-token;          !!!next-token;
6127          redo B;          next B;
6128        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6129          ## has a button element in scope          ## has a button element in scope
6130          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6131            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6132            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6133              !!!cp ('t378');              !!!cp ('t378');
6134              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button', token => $token);
6135              !!!back-token;              !!!back-token; # <button>
6136              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6137              redo B;                        line => $token->{line}, column => $token->{column}};
6138            } elsif ({              next B;
6139                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6140              !!!cp ('t379');              !!!cp ('t379');
6141              last INSCOPE;              last INSCOPE;
6142            }            }
# Line 5803  sub _tree_construction_main ($) { Line 6144  sub _tree_construction_main ($) {
6144                        
6145          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6146                        
6147          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6148          push @$active_formatting_elements, ['#marker', ''];  
6149            ## TODO: associate with $self->{form_element} if defined
6150    
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         !!!cp ('t380');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
6151          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6152            
6153          !!!next-token;          !!!nack ('t379.1');
         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;  
             
6154          !!!next-token;          !!!next-token;
6155          redo B;          next B;
6156        } elsif ({        } elsif ({
6157                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
6158                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
6159                  image => 1,                  noembed => 1,
6160                    noframes => 1,
6161                    noscript => 0, ## TODO: 1 if scripting is enabled
6162                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6163          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
6164            !!!cp ('t384');            !!!cp ('t381');
6165            !!!parse-error (type => 'image');            $reconstruct_active_formatting_elements->($insert_to_current);
           $token->{tag_name} = 'img';  
6166          } else {          } else {
6167            !!!cp ('t385');            !!!cp ('t399');
6168          }          }
6169            ## NOTE: There is an "as if in body" code clone.
6170          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
6171          $reconstruct_active_formatting_elements->($insert_to_current);          next B;
           
         !!!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;  
         redo B;  
6172        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6173          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
6174                    
6175          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6176            !!!cp ('t389');            !!!cp ('t389');
6177            ## Ignore the token            ## Ignore the token
6178              !!!nack ('t389'); ## NOTE: Not acknowledged.
6179            !!!next-token;            !!!next-token;
6180            redo B;            next B;
6181          } else {          } else {
6182            my $at = $token->{attributes};            my $at = $token->{attributes};
6183            my $form_attrs;            my $form_attrs;
# Line 5917  sub _tree_construction_main ($) { Line 6188  sub _tree_construction_main ($) {
6188            delete $at->{prompt};            delete $at->{prompt};
6189            my @tokens = (            my @tokens = (
6190                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
6191                           attributes => $form_attrs},                           attributes => $form_attrs,
6192                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
6193                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
6194                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
6195                            {type => START_TAG_TOKEN, tag_name => 'p',
6196                             line => $token->{line}, column => $token->{column}},
6197                            {type => START_TAG_TOKEN, tag_name => 'label',
6198                             line => $token->{line}, column => $token->{column}},
6199                         );                         );
6200            if ($prompt_attr) {            if ($prompt_attr) {
6201              !!!cp ('t390');              !!!cp ('t390');
6202              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6203                               #line => $token->{line}, column => $token->{column},
6204                              };
6205            } else {            } else {
6206              !!!cp ('t391');              !!!cp ('t391');
6207              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
6208                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
6209                               #line => $token->{line}, column => $token->{column},
6210                              }; # SHOULD
6211              ## TODO: make this configurable              ## TODO: make this configurable
6212            }            }
6213            push @tokens,            push @tokens,
6214                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6215                             line => $token->{line}, column => $token->{column}},
6216                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6217                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
6218                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
6219                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
6220                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
6221            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
6222                             line => $token->{line}, column => $token->{column}},
6223                            {type => END_TAG_TOKEN, tag_name => 'form',
6224                             line => $token->{line}, column => $token->{column}};
6225              !!!nack ('t391.1'); ## NOTE: Not acknowledged.
6226            !!!back-token (@tokens);            !!!back-token (@tokens);
6227            redo B;            !!!next-token;
6228              next B;
6229          }          }
6230        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6231          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6232          my $el;          my $el;
6233          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6234                    
6235          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6236          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5954  sub _tree_construction_main ($) { Line 6239  sub _tree_construction_main ($) {
6239          $insert->($el);          $insert->($el);
6240                    
6241          my $text = '';          my $text = '';
6242            !!!nack ('t392.1');
6243          !!!next-token;          !!!next-token;
6244          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6245            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 5984  sub _tree_construction_main ($) { Line 6270  sub _tree_construction_main ($) {
6270            ## Ignore the token            ## Ignore the token
6271          } else {          } else {
6272            !!!cp ('t398');            !!!cp ('t398');
6273            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6274          }          }
6275          !!!next-token;          !!!next-token;
6276          redo B;          next B;
6277        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
6278                  iframe => 1,                 $token->{tag_name} eq 'svg') {
                 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');  
6279          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6280                    
6281          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token);
6282                    
6283          $self->{insertion_mode} = IN_SELECT_IM;          if ($self->{self_closing}) {
6284              pop @{$self->{open_elements}};
6285              !!!ack ('t398.1');
6286            } else {
6287              !!!cp ('t398.2');
6288              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6289              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6290              ## mode, "in body" (not "in foreign content") secondary insertion
6291              ## mode, maybe.
6292            }
6293    
6294          !!!next-token;          !!!next-token;
6295          redo B;          next B;
6296        } elsif ({        } elsif ({
6297                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6298                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6014  sub _tree_construction_main ($) { Line 6300  sub _tree_construction_main ($) {
6300                  thead => 1, tr => 1,                  thead => 1, tr => 1,
6301                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6302          !!!cp ('t401');          !!!cp ('t401');
6303          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6304          ## Ignore the token          ## Ignore the token
6305            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6306          !!!next-token;          !!!next-token;
6307          redo B;          next B;
6308                    
6309          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6310        } else {        } else {
6311          !!!cp ('t402');          if ($token->{tag_name} eq 'image') {
6312              !!!cp ('t384');
6313              !!!parse-error (type => 'image', token => $token);
6314              $token->{tag_name} = 'img';
6315            } else {
6316              !!!cp ('t385');
6317            }
6318    
6319            ## NOTE: There is an "as if <br>" code clone.
6320          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6321                    
6322          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6323    
6324            if ({
6325                 applet => 1, marquee => 1, object => 1,
6326                }->{$token->{tag_name}}) {
6327              !!!cp ('t380');
6328              push @$active_formatting_elements, ['#marker', ''];
6329              !!!nack ('t380.1');
6330            } elsif ({
6331                      b => 1, big => 1, em => 1, font => 1, i => 1,
6332                      s => 1, small => 1, strile => 1,
6333                      strong => 1, tt => 1, u => 1,
6334                     }->{$token->{tag_name}}) {
6335              !!!cp ('t375');
6336              push @$active_formatting_elements, $self->{open_elements}->[-1];
6337              !!!nack ('t375.1');
6338            } elsif ($token->{tag_name} eq 'input') {
6339              !!!cp ('t388');
6340              ## TODO: associate with $self->{form_element} if defined
6341              pop @{$self->{open_elements}};
6342              !!!ack ('t388.2');
6343            } elsif ({
6344                      area => 1, basefont => 1, bgsound => 1, br => 1,
6345                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6346                      #image => 1,
6347                     }->{$token->{tag_name}}) {
6348              !!!cp ('t388.1');
6349              pop @{$self->{open_elements}};
6350              !!!ack ('t388.3');
6351            } elsif ($token->{tag_name} eq 'select') {
6352              ## TODO: associate with $self->{form_element} if defined
6353            
6354              if ($self->{insertion_mode} & TABLE_IMS or
6355                  $self->{insertion_mode} & BODY_TABLE_IMS or
6356                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6357                !!!cp ('t400.1');
6358                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6359              } else {
6360                !!!cp ('t400.2');
6361                $self->{insertion_mode} = IN_SELECT_IM;
6362              }
6363              !!!nack ('t400.3');
6364            } else {
6365              !!!nack ('t402');
6366            }
6367                    
6368          !!!next-token;          !!!next-token;
6369          redo B;          next B;
6370        }        }
6371      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6372        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
6373          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
6374              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
6375            for (@{$self->{open_elements}}) {          INSCOPE: {
6376              unless ({            for (reverse @{$self->{open_elements}}) {
6377                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
6378                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
6379                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
6380                      }->{$_->[1]}) {                last INSCOPE;
6381                !!!cp ('t403');              } elsif ($_->[1] & SCOPING_EL) {
6382                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!cp ('t405.1');
6383              } else {                last;
               !!!cp ('t404');  
6384              }              }
6385            }            }
6386    
6387            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
6388                              value => $token->{tag_name}, token => $token);
6389              ## NOTE: Ignore the token.
6390            !!!next-token;            !!!next-token;
6391            redo B;            next B;
6392          } else {          } # INSCOPE
6393            !!!cp ('t405');  
6394            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          for (@{$self->{open_elements}}) {
6395            ## Ignore the token            unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
6396            !!!next-token;              !!!cp ('t403');
6397            redo B;              !!!parse-error (type => 'not closed',
6398                                value => $_->[0]->manakai_local_name,
6399                                token => $token);
6400                last;
6401              } else {
6402                !!!cp ('t404');
6403              }
6404          }          }
6405    
6406            $self->{insertion_mode} = AFTER_BODY_IM;
6407            !!!next-token;
6408            next B;
6409        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
6410          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
6411            ## up-to-date, though it has same effect as speced.
6412            if (@{$self->{open_elements}} > 1 and
6413                $self->{open_elements}->[1]->[1] & BODY_EL) {
6414            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6415            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6416              !!!cp ('t406');              !!!cp ('t406');
6417              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed',
6418                                value => $self->{open_elements}->[1]->[0]
6419                                    ->manakai_local_name,
6420                                token => $token);
6421            } else {            } else {
6422              !!!cp ('t407');              !!!cp ('t407');
6423            }            }
6424            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6425            ## reprocess            ## reprocess
6426            redo B;            next B;
6427          } else {          } else {
6428            !!!cp ('t408');            !!!cp ('t408');
6429            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6430            ## Ignore the token            ## Ignore the token
6431            !!!next-token;            !!!next-token;
6432            redo B;            next B;
6433          }          }
6434        } elsif ({        } elsif ({
6435                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6436                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
6437                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
6438                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
6439                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
6440                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6441          ## has an element in scope          ## has an element in scope
6442          my $i;          my $i;
6443          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6444            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6445            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
             ## generate implied end tags  
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t409');  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
               
6446              !!!cp ('t410');              !!!cp ('t410');
6447              $i = $_;              $i = $_;
6448              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
6449            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6450              !!!cp ('t411');              !!!cp ('t411');
6451              last INSCOPE;              last INSCOPE;
6452            }            }
6453          } # INSCOPE          } # INSCOPE
6454            
6455          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6456            if (defined $i) {            !!!cp ('t413');
6457              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6458            } else {
6459              ## Step 1. generate implied end tags
6460              while ({
6461                      dd => ($token->{tag_name} ne 'dd'),
6462                      dt => ($token->{tag_name} ne 'dt'),
6463                      li => ($token->{tag_name} ne 'li'),
6464                      p => 1,
6465                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6466                !!!cp ('t409');
6467                pop @{$self->{open_elements}};
6468              }
6469    
6470              ## Step 2.
6471              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6472                      ne $token->{tag_name}) {
6473              !!!cp ('t412');              !!!cp ('t412');
6474              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
6475                                value => $self->{open_elements}->[-1]->[0]
6476                                    ->manakai_local_name,
6477                                token => $token);
6478            } else {            } else {
6479              !!!cp ('t413');              !!!cp ('t414');
             !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
6480            }            }
6481          }  
6482                      ## Step 3.
         if (defined $i) {  
           !!!cp ('t414');  
6483            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6484          } elsif ($token->{tag_name} eq 'p') {  
6485            !!!cp ('t415');            ## Step 4.
6486            ## As if <p>, then reprocess the current token            $clear_up_to_marker->()
6487            my $el;                if {
6488            !!!create-element ($el, 'p');                  applet => 1, button => 1, marquee => 1, object => 1,
6489            $insert->($el);                }->{$token->{tag_name}};
         } else {  
           !!!cp ('t416');  
6490          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
6491          !!!next-token;          !!!next-token;
6492          redo B;          next B;
6493        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
6494            undef $self->{form_element};
6495    
6496          ## has an element in scope          ## has an element in scope
6497            my $i;
6498          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6499            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6500            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
   
                  ## NOTE: The following elements never appear here, maybe.  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t417');  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
               
6501              !!!cp ('t418');              !!!cp ('t418');
6502                $i = $_;
6503              last INSCOPE;              last INSCOPE;
6504            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6505              !!!cp ('t419');              !!!cp ('t419');
6506              last INSCOPE;              last INSCOPE;
6507            }            }
6508          } # INSCOPE          } # INSCOPE
6509            
6510          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          unless (defined $i) { # has an element in scope
           !!!cp ('t420');  
           pop @{$self->{open_elements}};  
         } else {  
6511            !!!cp ('t421');            !!!cp ('t421');
6512            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6513            } else {
6514              ## Step 1. generate implied end tags
6515              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6516                !!!cp ('t417');
6517                pop @{$self->{open_elements}};
6518              }
6519              
6520              ## Step 2.
6521              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6522                      ne $token->{tag_name}) {
6523                !!!cp ('t417.1');
6524                !!!parse-error (type => 'not closed',
6525                                value => $self->{open_elements}->[-1]->[0]
6526                                    ->manakai_local_name,
6527                                token => $token);
6528              } else {
6529                !!!cp ('t420');
6530              }  
6531              
6532              ## Step 3.
6533              splice @{$self->{open_elements}}, $i;
6534          }          }
6535    
         undef $self->{form_element};  
6536          !!!next-token;          !!!next-token;
6537          redo B;          next B;
6538        } elsif ({        } elsif ({
6539                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6540                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6193  sub _tree_construction_main ($) { Line 6542  sub _tree_construction_main ($) {
6542          my $i;          my $i;
6543          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6544            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6545            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t422');  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
   
6546              !!!cp ('t423');              !!!cp ('t423');
6547              $i = $_;              $i = $_;
6548              last INSCOPE;              last INSCOPE;
6549            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6550              !!!cp ('t424');              !!!cp ('t424');
6551              last INSCOPE;              last INSCOPE;
6552            }            }
6553          } # INSCOPE          } # INSCOPE
6554    
6555            unless (defined $i) { # has an element in scope
6556              !!!cp ('t425.1');
6557              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6558            } else {
6559              ## Step 1. generate implied end tags
6560              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6561                !!!cp ('t422');
6562                pop @{$self->{open_elements}};
6563              }
6564              
6565              ## Step 2.
6566              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6567                      ne $token->{tag_name}) {
6568                !!!cp ('t425');
6569                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6570              } else {
6571                !!!cp ('t426');
6572              }
6573    
6574              ## Step 3.
6575              splice @{$self->{open_elements}}, $i;
6576            }
6577                    
6578          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          !!!next-token;
6579            !!!cp ('t425');          next B;
6580            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});        } elsif ($token->{tag_name} eq 'p') {
6581            ## has an element in scope
6582            my $i;
6583            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6584              my $node = $self->{open_elements}->[$_];
6585              if ($node->[1] & P_EL) {
6586                !!!cp ('t410.1');
6587                $i = $_;
6588                last INSCOPE;
6589              } elsif ($node->[1] & SCOPING_EL) {
6590                !!!cp ('t411.1');
6591                last INSCOPE;
6592              }
6593            } # INSCOPE
6594    
6595            if (defined $i) {
6596              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6597                      ne $token->{tag_name}) {
6598                !!!cp ('t412.1');
6599                !!!parse-error (type => 'not closed',
6600                                value => $self->{open_elements}->[-1]->[0]
6601                                    ->manakai_local_name,
6602                                token => $token);
6603              } else {
6604                !!!cp ('t414.1');
6605              }
6606    
6607              splice @{$self->{open_elements}}, $i;
6608          } else {          } else {
6609            !!!cp ('t426');            !!!cp ('t413.1');
6610              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6611    
6612              !!!cp ('t415.1');
6613              ## As if <p>, then reprocess the current token
6614              my $el;
6615              !!!create-element ($el, $HTML_NS, 'p',, $token);
6616              $insert->($el);
6617              ## NOTE: Not inserted into |$self->{open_elements}|.
6618          }          }
6619            
         splice @{$self->{open_elements}}, $i if defined $i;  
6620          !!!next-token;          !!!next-token;
6621          redo B;          next B;
6622        } elsif ({        } elsif ({
6623                  a => 1,                  a => 1,
6624                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 6238  sub _tree_construction_main ($) { Line 6626  sub _tree_construction_main ($) {
6626                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
6627                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6628          !!!cp ('t427');          !!!cp ('t427');
6629          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token);
6630          redo B;          next B;
6631        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
6632          !!!cp ('t428');          !!!cp ('t428');
6633          !!!parse-error (type => 'unmatched end tag:br');          !!!parse-error (type => 'unmatched end tag:br', token => $token);
6634    
6635          ## As if <br>          ## As if <br>
6636          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6637                    
6638          my $el;          my $el;
6639          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
6640          $insert->($el);          $insert->($el);
6641                    
6642          ## Ignore the token.          ## Ignore the token.
6643          !!!next-token;          !!!next-token;
6644          redo B;          next B;
6645        } elsif ({        } elsif ({
6646                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6647                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6267  sub _tree_construction_main ($) { Line 6655  sub _tree_construction_main ($) {
6655                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
6656                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6657          !!!cp ('t429');          !!!cp ('t429');
6658          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6659          ## Ignore the token          ## Ignore the token
6660          !!!next-token;          !!!next-token;
6661          redo B;          next B;
6662                    
6663          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
6664                    
# Line 6281  sub _tree_construction_main ($) { Line 6669  sub _tree_construction_main ($) {
6669    
6670          ## Step 2          ## Step 2
6671          S2: {          S2: {
6672            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6673              ## Step 1              ## Step 1
6674              ## generate implied end tags              ## generate implied end tags
6675              if ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
6676                !!!cp ('t430');                !!!cp ('t430');
6677                ## ISSUE: Can this case be reached?                ## ISSUE: Can this case be reached?
6678                !!!back-token;                pop @{$self->{open_elements}};
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
6679              }              }
6680                    
6681              ## Step 2              ## Step 2
6682              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6683                        ne $token->{tag_name}) {
6684                !!!cp ('t431');                !!!cp ('t431');
6685                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
6686                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
6687                                  value => $self->{open_elements}->[-1]->[0]
6688                                      ->manakai_local_name,
6689                                  token => $token);
6690              } else {              } else {
6691                !!!cp ('t432');                !!!cp ('t432');
6692              }              }
# Line 6313  sub _tree_construction_main ($) { Line 6698  sub _tree_construction_main ($) {
6698              last S2;              last S2;
6699            } else {            } else {
6700              ## Step 3              ## Step 3
6701              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
6702                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
6703                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
6704                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
6705                !!!cp ('t433');                !!!cp ('t433');
6706                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6707                ## Ignore the token                ## Ignore the token
6708                !!!next-token;                !!!next-token;
6709                last S2;                last S2;
# Line 6334  sub _tree_construction_main ($) { Line 6719  sub _tree_construction_main ($) {
6719            ## Step 5;            ## Step 5;
6720            redo S2;            redo S2;
6721          } # S2          } # S2
6722          redo B;          next B;
6723        }        }
6724      }      }
6725      redo B;      next B;
6726      } continue { # B
6727        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
6728          ## NOTE: The code below is executed in cases where it does not have
6729          ## to be, but it it is harmless even in those cases.
6730          ## has an element in scope
6731          INSCOPE: {
6732            for (reverse 0..$#{$self->{open_elements}}) {
6733              my $node = $self->{open_elements}->[$_];
6734              if ($node->[1] & FOREIGN_EL) {
6735                last INSCOPE;
6736              } elsif ($node->[1] & SCOPING_EL) {
6737                last;
6738              }
6739            }
6740            
6741            ## NOTE: No foreign element in scope.
6742            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
6743          } # INSCOPE
6744        }
6745    } # B    } # B
6746    
6747    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 6383  sub set_inner_html ($$$) { Line 6787  sub set_inner_html ($$$) {
6787    
6788      ## Step 8 # MUST      ## Step 8 # MUST
6789      my $i = 0;      my $i = 0;
6790      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
6791      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
6792      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
6793        my $self = shift;        my $self = shift;
6794    
# Line 6393  sub set_inner_html ($$$) { Line 6797  sub set_inner_html ($$$) {
6797    
6798        $self->{next_char} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
6799        $self->{next_char} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
6800        $column++;  
6801          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
6802          $p->{column}++;
6803    
6804        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
6805          $line++;          $p->{line}++;
6806          $column = 0;          $p->{column} = 0;
6807          !!!cp ('i1');          !!!cp ('i1');
6808        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
6809          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
6810          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
6811          $line++;          $p->{line}++;
6812          $column = 0;          $p->{column} = 0;
6813          !!!cp ('i2');          !!!cp ('i2');
6814        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
6815          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
# Line 6419  sub set_inner_html ($$$) { Line 6825  sub set_inner_html ($$$) {
6825            
6826      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
6827        my (%opt) = @_;        my (%opt) = @_;
6828        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
6829          my $column = $opt{column};
6830          if (defined $opt{token} and defined $opt{token}->{line}) {
6831            $line = $opt{token}->{line};
6832            $column = $opt{token}->{column};
6833          }
6834          warn "Parse error ($opt{type}) at line $line column $column\n";
6835      };      };
6836      $p->{parse_error} = sub {      $p->{parse_error} = sub {
6837        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
6838      };      };
6839            
6840      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
# Line 6446  sub set_inner_html ($$$) { Line 6858  sub set_inner_html ($$$) {
6858          unless defined $p->{content_model};          unless defined $p->{content_model};
6859          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
6860    
6861      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
6862          ## TODO: Foreign element OK?
6863    
6864      ## Step 3      ## Step 3
6865      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6456  sub set_inner_html ($$$) { Line 6869  sub set_inner_html ($$$) {
6869      $doc->append_child ($root);      $doc->append_child ($root);
6870    
6871      ## Step 5 # MUST      ## Step 5 # MUST
6872      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
6873    
6874      undef $p->{head_element};      undef $p->{head_element};
6875    
# Line 6502  sub set_inner_html ($$$) { Line 6915  sub set_inner_html ($$$) {
6915      ## ISSUE: mutation events?      ## ISSUE: mutation events?
6916    
6917      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
6918    
6919        delete $p->{parse_error}; # delete loop
6920    } else {    } else {
6921      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";
6922    }    }

Legend:
Removed from v.1.84  
changed lines
  Added in v.1.126

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24