/[suikacvs]/markup/html/whatpm/Whatpm/HTML.pm.src
Suika

Diff of /markup/html/whatpm/Whatpm/HTML.pm.src

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.93 by wakaba, Sat Mar 8 03:43:48 2008 UTC revision 1.125 by wakaba, Sat Apr 12 10:41:31 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 = {  sub A_EL () { 0b1 }
16    base => 1,  sub ADDRESS_EL () { 0b10 }
17    link => 1,  sub BODY_EL () { 0b100 }
18    meta => 1,  sub BUTTON_EL () { 0b1000 }
19    hr => 1,  sub CAPTION_EL () { 0b10000 }
20    br => 1,  sub DD_EL () { 0b100000 }
21    img => 1,  sub DIV_EL () { 0b1000000 }
22    embed => 1,  sub DT_EL () { 0b10000000 }
23    param => 1,  sub FORM_EL () { 0b100000000 }
24    area => 1,  sub FORMATTING_EL () { 0b1000000000 }
25    col => 1,  sub FRAMESET_EL () { 0b10000000000 }
26    input => 1,  sub HEADING_EL () { 0b100000000000 }
27    sub HTML_EL () { 0b1000000000000 }
28    sub LI_EL () { 0b10000000000000 }
29    sub NOBR_EL () { 0b100000000000000 }
30    sub OPTION_EL () { 0b1000000000000000 }
31    sub OPTGROUP_EL () { 0b10000000000000000 }
32    sub P_EL () { 0b100000000000000000 }
33    sub SELECT_EL () { 0b1000000000000000000 }
34    sub TABLE_EL () { 0b10000000000000000000 }
35    sub TABLE_CELL_EL () { 0b100000000000000000000 }
36    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
37    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
38    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
39    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
40    
41    sub TABLE_ROWS_EL () {
42      TABLE_EL |
43      TABLE_ROW_EL |
44      TABLE_ROW_GROUP_EL
45    }
46    
47    sub END_TAG_OPTIONAL_EL () {
48      DD_EL |
49      DT_EL |
50      LI_EL |
51      P_EL
52    }
53    
54    sub ALL_END_TAG_OPTIONAL_EL () {
55      END_TAG_OPTIONAL_EL |
56      BODY_EL |
57      HTML_EL |
58      TABLE_CELL_EL |
59      TABLE_ROW_EL |
60      TABLE_ROW_GROUP_EL
61    }
62    
63    sub SCOPING_EL () {
64      BUTTON_EL |
65      CAPTION_EL |
66      HTML_EL |
67      TABLE_EL |
68      TABLE_CELL_EL |
69      MISC_SCOPING_EL
70    }
71    
72    sub TABLE_SCOPING_EL () {
73      HTML_EL |
74      TABLE_EL
75    }
76    
77    sub TABLE_ROWS_SCOPING_EL () {
78      HTML_EL |
79      TABLE_ROW_GROUP_EL
80    }
81    
82    sub TABLE_ROW_SCOPING_EL () {
83      HTML_EL |
84      TABLE_ROW_EL
85    }
86    
87    sub SPECIAL_EL () {
88      ADDRESS_EL |
89      BODY_EL |
90      DIV_EL |
91      END_TAG_OPTIONAL_EL |
92      FORM_EL |
93      FRAMESET_EL |
94      HEADING_EL |
95      OPTION_EL |
96      OPTGROUP_EL |
97      SELECT_EL |
98      TABLE_ROW_EL |
99      TABLE_ROW_GROUP_EL |
100      MISC_SPECIAL_EL
101    }
102    
103    my $el_category = {
104      a => A_EL | FORMATTING_EL,
105      address => ADDRESS_EL,
106      applet => MISC_SCOPING_EL,
107      area => MISC_SPECIAL_EL,
108      b => FORMATTING_EL,
109      base => MISC_SPECIAL_EL,
110      basefont => MISC_SPECIAL_EL,
111      bgsound => MISC_SPECIAL_EL,
112      big => FORMATTING_EL,
113      blockquote => MISC_SPECIAL_EL,
114      body => BODY_EL,
115      br => MISC_SPECIAL_EL,
116      button => BUTTON_EL,
117      caption => CAPTION_EL,
118      center => MISC_SPECIAL_EL,
119      col => MISC_SPECIAL_EL,
120      colgroup => MISC_SPECIAL_EL,
121      dd => DD_EL,
122      dir => MISC_SPECIAL_EL,
123      div => DIV_EL,
124      dl => MISC_SPECIAL_EL,
125      dt => DT_EL,
126      em => FORMATTING_EL,
127      embed => MISC_SPECIAL_EL,
128      fieldset => MISC_SPECIAL_EL,
129      font => FORMATTING_EL,
130      form => FORM_EL,
131      frame => MISC_SPECIAL_EL,
132      frameset => FRAMESET_EL,
133      h1 => HEADING_EL,
134      h2 => HEADING_EL,
135      h3 => HEADING_EL,
136      h4 => HEADING_EL,
137      h5 => HEADING_EL,
138      h6 => HEADING_EL,
139      head => MISC_SPECIAL_EL,
140      hr => MISC_SPECIAL_EL,
141      html => HTML_EL,
142      i => FORMATTING_EL,
143      iframe => MISC_SPECIAL_EL,
144      img => MISC_SPECIAL_EL,
145      input => MISC_SPECIAL_EL,
146      isindex => MISC_SPECIAL_EL,
147      li => LI_EL,
148      link => MISC_SPECIAL_EL,
149      listing => MISC_SPECIAL_EL,
150      marquee => MISC_SCOPING_EL,
151      menu => MISC_SPECIAL_EL,
152      meta => MISC_SPECIAL_EL,
153      nobr => NOBR_EL | FORMATTING_EL,
154      noembed => MISC_SPECIAL_EL,
155      noframes => MISC_SPECIAL_EL,
156      noscript => MISC_SPECIAL_EL,
157      object => MISC_SCOPING_EL,
158      ol => MISC_SPECIAL_EL,
159      optgroup => OPTGROUP_EL,
160      option => OPTION_EL,
161      p => P_EL,
162      param => MISC_SPECIAL_EL,
163      plaintext => MISC_SPECIAL_EL,
164      pre => MISC_SPECIAL_EL,
165      s => FORMATTING_EL,
166      script => MISC_SPECIAL_EL,
167      select => SELECT_EL,
168      small => FORMATTING_EL,
169      spacer => MISC_SPECIAL_EL,
170      strike => FORMATTING_EL,
171      strong => FORMATTING_EL,
172      style => MISC_SPECIAL_EL,
173      table => TABLE_EL,
174      tbody => TABLE_ROW_GROUP_EL,
175      td => TABLE_CELL_EL,
176      textarea => MISC_SPECIAL_EL,
177      tfoot => TABLE_ROW_GROUP_EL,
178      th => TABLE_CELL_EL,
179      thead => TABLE_ROW_GROUP_EL,
180      title => MISC_SPECIAL_EL,
181      tr => TABLE_ROW_EL,
182      tt => FORMATTING_EL,
183      u => FORMATTING_EL,
184      ul => MISC_SPECIAL_EL,
185      wbr => MISC_SPECIAL_EL,
186  };  };
187    
188  my $c1_entity_char = {  my $c1_entity_char = {
# Line 61  my $c1_entity_char = { Line 220  my $c1_entity_char = {
220    0x9F => 0x0178,    0x9F => 0x0178,
221  }; # $c1_entity_char  }; # $c1_entity_char
222    
 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  
   
223  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
224    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
225    my $charset = shift;    my $charset = shift;
# Line 108  sub parse_byte_string ($$$$;$) { Line 245  sub parse_byte_string ($$$$;$) {
245    $self->{change_encoding} = sub {    $self->{change_encoding} = sub {
246      my $self = shift;      my $self = shift;
247      my $charset = lc shift;      my $charset = lc shift;
248        my $token = shift;
249      ## TODO: if $charset is supported      ## TODO: if $charset is supported
250      ## TODO: normalize charset name      ## TODO: normalize charset name
251    
# Line 126  sub parse_byte_string ($$$$;$) { Line 264  sub parse_byte_string ($$$$;$) {
264      }      }
265    
266      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
267          ':'.$charset, level => 'w');          ':'.$charset, level => 'w', token => $token);
268    
269      ## Step 3      ## Step 3
270      # if (can) {      # if (can) {
# Line 177  sub parse_string ($$$;$) { Line 315  sub parse_string ($$$;$) {
315        if defined $self->{input_encoding};        if defined $self->{input_encoding};
316    
317    my $i = 0;    my $i = 0;
318    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
319    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
320    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
321      my $self = shift;      my $self = shift;
322    
# Line 187  sub parse_string ($$$;$) { Line 325  sub parse_string ($$$;$) {
325    
326      $self->{next_char} = -1 and return if $i >= length $$s;      $self->{next_char} = -1 and return if $i >= length $$s;
327      $self->{next_char} = ord substr $$s, $i++, 1;      $self->{next_char} = ord substr $$s, $i++, 1;
328      $column++;  
329        ($self->{line_prev}, $self->{column_prev})
330            = ($self->{line}, $self->{column});
331        $self->{column}++;
332            
333      if ($self->{next_char} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
334        $line++;        $self->{line}++;
335        $column = 0;        $self->{column} = 0;
336      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
337        $i++ if substr ($$s, $i, 1) eq "\x0A";        $i++ if substr ($$s, $i, 1) eq "\x0A";
338        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
339        $line++;        $self->{line}++;
340        $column = 0;        $self->{column} = 0;
341      } elsif ($self->{next_char} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
342        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
343      } elsif ($self->{next_char} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
# Line 209  sub parse_string ($$$;$) { Line 350  sub parse_string ($$$;$) {
350    
351    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
352      my (%opt) = @_;      my (%opt) = @_;
353      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
354        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
355        warn "Parse error ($opt{type}) at line $line column $column\n";
356    };    };
357    $self->{parse_error} = sub {    $self->{parse_error} = sub {
358      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
359    };    };
360    
361    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 220  sub parse_string ($$$;$) { Line 363  sub parse_string ($$$;$) {
363    $self->_construct_tree;    $self->_construct_tree;
364    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
365    
366      delete $self->{parse_error}; # remove loop
367    
368    return $self->{document};    return $self->{document};
369  } # parse_string  } # parse_string
370    
# Line 287  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 432  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
432  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
433  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
434  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
435    sub SELF_CLOSING_START_TAG_STATE () { 34 }
436    
437  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
438  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 303  sub TABLE_IMS ()      { 0b1000000 } Line 449  sub TABLE_IMS ()      { 0b1000000 }
449  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
450  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
451  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
452    sub SELECT_IMS ()     { 0b10000000000 }
453    
454  ## NOTE: "initial" and "before html" insertion modes have no constants.  ## NOTE: "initial" and "before html" insertion modes have no constants.
455    
# Line 325  sub IN_TABLE_IM () { TABLE_IMS } Line 472  sub IN_TABLE_IM () { TABLE_IMS }
472  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
473  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
474  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
475  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
476    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
477  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
478    
479  ## 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 486  sub _initialize_tokenizer ($) {
486    undef $self->{current_attribute};    undef $self->{current_attribute};
487    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
488    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
489      delete $self->{self_closing};
490    $self->{char} = [];    $self->{char} = [];
491    # $self->{next_char}    # $self->{next_char}
492    !!!next-input-character;    !!!next-input-character;
# Line 358  sub _initialize_tokenizer ($) { Line 507  sub _initialize_tokenizer ($) {
507  ##        ->{value}  ##        ->{value}
508  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
509  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
510    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
511    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
512    ##     while the token is pushed back to the stack.
513    
514    ## ISSUE: "When a DOCTYPE token is created, its
515    ## <i>self-closing flag</i> must be unset (its other state is that it
516    ## be set), and its attributes list must be empty.": Wrong subject?
517    
518  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
519    
# Line 384  sub _initialize_tokenizer ($) { Line 540  sub _initialize_tokenizer ($) {
540    
541  sub _get_next_token ($) {  sub _get_next_token ($) {
542    my $self = shift;    my $self = shift;
543    
544      if ($self->{self_closing}) {
545        !!!parse-error (type => 'nestc', token => $self->{current_token});
546        ## NOTE: The |self_closing| flag is only set by start tag token.
547        ## In addition, when a start tag token is emitted, it is always set to
548        ## |current_token|.
549        delete $self->{self_closing};
550      }
551    
552    if (@{$self->{token}}) {    if (@{$self->{token}}) {
553        $self->{self_closing} = $self->{token}->[0]->{self_closing};
554      return shift @{$self->{token}};      return shift @{$self->{token}};
555    }    }
556    
# Line 447  sub _get_next_token ($) { Line 613  sub _get_next_token ($) {
613          #          #
614        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
615          !!!cp (11);          !!!cp (11);
616          !!!emit ({type => END_OF_FILE_TOKEN});          !!!emit ({type => END_OF_FILE_TOKEN,
617                      line => $self->{line}, column => $self->{column}});
618          last A; ## TODO: ok?          last A; ## TODO: ok?
619        } else {        } else {
620          !!!cp (12);          !!!cp (12);
621        }        }
622        # Anything else        # Anything else
623        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
624                     data => chr $self->{next_char}};                     data => chr $self->{next_char},
625                       line => $self->{line}, column => $self->{column},
626                      };
627        ## Stay in the data state        ## Stay in the data state
628        !!!next-input-character;        !!!next-input-character;
629    
# Line 463  sub _get_next_token ($) { Line 632  sub _get_next_token ($) {
632        redo A;        redo A;
633      } elsif ($self->{state} == ENTITY_DATA_STATE) {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
634        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
635    
636          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
637                
638        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
639    
# Line 471  sub _get_next_token ($) { Line 642  sub _get_next_token ($) {
642    
643        unless (defined $token) {        unless (defined $token) {
644          !!!cp (13);          !!!cp (13);
645          !!!emit ({type => CHARACTER_TOKEN, data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&',
646                      line => $l, column => $c,
647                     });
648        } else {        } else {
649          !!!cp (14);          !!!cp (14);
650          !!!emit ($token);          !!!emit ($token);
# Line 490  sub _get_next_token ($) { Line 663  sub _get_next_token ($) {
663            ## reconsume            ## reconsume
664            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
665    
666            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
667                        line => $self->{line_prev},
668                        column => $self->{column_prev},
669                       });
670    
671            redo A;            redo A;
672          }          }
# Line 510  sub _get_next_token ($) { Line 686  sub _get_next_token ($) {
686            !!!cp (19);            !!!cp (19);
687            $self->{current_token}            $self->{current_token}
688              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
689                 tag_name => chr ($self->{next_char} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
690                   line => $self->{line_prev},
691                   column => $self->{column_prev}};
692            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
693            !!!next-input-character;            !!!next-input-character;
694            redo A;            redo A;
# Line 518  sub _get_next_token ($) { Line 696  sub _get_next_token ($) {
696                   $self->{next_char} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
697            !!!cp (20);            !!!cp (20);
698            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
699                              tag_name => chr ($self->{next_char})};                                      tag_name => chr ($self->{next_char}),
700                                        line => $self->{line_prev},
701                                        column => $self->{column_prev}};
702            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
703            !!!next-input-character;            !!!next-input-character;
704            redo A;            redo A;
705          } elsif ($self->{next_char} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
706            !!!cp (21);            !!!cp (21);
707            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag',
708                              line => $self->{line_prev},
709                              column => $self->{column_prev});
710            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
711            !!!next-input-character;            !!!next-input-character;
712    
713            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
714                        line => $self->{line_prev},
715                        column => $self->{column_prev},
716                       });
717    
718            redo A;            redo A;
719          } elsif ($self->{next_char} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
720            !!!cp (22);            !!!cp (22);
721            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio',
722                              line => $self->{line_prev},
723                              column => $self->{column_prev});
724            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
725              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
726                                        line => $self->{line_prev},
727                                        column => $self->{column_prev},
728                                       };
729            ## $self->{next_char} is intentionally left as is            ## $self->{next_char} is intentionally left as is
730            redo A;            redo A;
731          } else {          } else {
# Line 543  sub _get_next_token ($) { Line 734  sub _get_next_token ($) {
734            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
735            ## reconsume            ## reconsume
736    
737            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
738                        line => $self->{line_prev},
739                        column => $self->{column_prev},
740                       });
741    
742            redo A;            redo A;
743          }          }
# Line 551  sub _get_next_token ($) { Line 745  sub _get_next_token ($) {
745          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
746        }        }
747      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
748          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
749        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
750          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
751    
752            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
753            my @next_char;            my @next_char;
754            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 765  sub _get_next_token ($) {
765                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
766                $self->{state} = DATA_STATE;                $self->{state} = DATA_STATE;
767    
768                !!!emit ({type => CHARACTER_TOKEN, data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
769                            line => $l, column => $c,
770                           });
771        
772                redo A;                redo A;
773              }              }
# Line 588  sub _get_next_token ($) { Line 786  sub _get_next_token ($) {
786              $self->{next_char} = shift @next_char; # reconsume              $self->{next_char} = shift @next_char; # reconsume
787              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
788              $self->{state} = DATA_STATE;              $self->{state} = DATA_STATE;
789              !!!emit ({type => CHARACTER_TOKEN, data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
790                          line => $l, column => $c,
791                         });
792              redo A;              redo A;
793            } else {            } else {
794              !!!cp (27);              !!!cp (27);
# Line 601  sub _get_next_token ($) { Line 801  sub _get_next_token ($) {
801            !!!cp (28);            !!!cp (28);
802            # next-input-character is already done            # next-input-character is already done
803            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
804            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
805                        line => $l, column => $c,
806                       });
807            redo A;            redo A;
808          }          }
809        }        }
# Line 609  sub _get_next_token ($) { Line 811  sub _get_next_token ($) {
811        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{next_char} and
812            $self->{next_char} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
813          !!!cp (29);          !!!cp (29);
814          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token}
815                            tag_name => chr ($self->{next_char} + 0x0020)};              = {type => END_TAG_TOKEN,
816                   tag_name => chr ($self->{next_char} + 0x0020),
817                   line => $l, column => $c};
818          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
819          !!!next-input-character;          !!!next-input-character;
820          redo A;          redo A;
# Line 618  sub _get_next_token ($) { Line 822  sub _get_next_token ($) {
822                 $self->{next_char} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
823          !!!cp (30);          !!!cp (30);
824          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
825                            tag_name => chr ($self->{next_char})};                                    tag_name => chr ($self->{next_char}),
826                                      line => $l, column => $c};
827          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
828          !!!next-input-character;          !!!next-input-character;
829          redo A;          redo A;
830        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
831          !!!cp (31);          !!!cp (31);
832          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag',
833                            line => $self->{line_prev}, ## "<" in "</>"
834                            column => $self->{column_prev} - 1);
835          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
836          !!!next-input-character;          !!!next-input-character;
837          redo A;          redo A;
# Line 634  sub _get_next_token ($) { Line 841  sub _get_next_token ($) {
841          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
842          # reconsume          # reconsume
843    
844          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
845                      line => $l, column => $c,
846                     });
847    
848          redo A;          redo A;
849        } else {        } else {
850          !!!cp (33);          !!!cp (33);
851          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
852          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
853            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
854                                      line => $self->{line_prev}, # "<" of "</"
855                                      column => $self->{column_prev} - 1,
856                                     };
857          ## $self->{next_char} is intentionally left as is          ## $self->{next_char} is intentionally left as is
858          redo A;          redo A;
859        }        }
# Line 657  sub _get_next_token ($) { Line 870  sub _get_next_token ($) {
870        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
871          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
872            !!!cp (35);            !!!cp (35);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
873            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
874          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
875            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 690  sub _get_next_token ($) { Line 901  sub _get_next_token ($) {
901          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
902          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
903            !!!cp (39);            !!!cp (39);
           $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 712  sub _get_next_token ($) { Line 921  sub _get_next_token ($) {
921    
922          redo A;          redo A;
923        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
924            !!!cp (42);
925            $self->{state} = SELF_CLOSING_START_TAG_STATE;
926          !!!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  
927          redo A;          redo A;
928        } else {        } else {
929          !!!cp (44);          !!!cp (44);
# Line 747  sub _get_next_token ($) { Line 946  sub _get_next_token ($) {
946        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
947          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
948            !!!cp (46);            !!!cp (46);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
949            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
950          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
951            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 770  sub _get_next_token ($) { Line 967  sub _get_next_token ($) {
967        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
968                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
969          !!!cp (49);          !!!cp (49);
970          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
971                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
972                   value => '',
973                   line => $self->{line}, column => $self->{column}};
974          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
975          !!!next-input-character;          !!!next-input-character;
976          redo A;          redo A;
977        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
978            !!!cp (50);
979            $self->{state} = SELF_CLOSING_START_TAG_STATE;
980          !!!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  
981          redo A;          redo A;
982        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
983          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
984          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
985            !!!cp (52);            !!!cp (52);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
986            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
987          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
988            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 825  sub _get_next_token ($) { Line 1012  sub _get_next_token ($) {
1012          } else {          } else {
1013            !!!cp (56);            !!!cp (56);
1014          }          }
1015          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1016                                value => ''};              = {name => chr ($self->{next_char}),
1017                   value => '',
1018                   line => $self->{line}, column => $self->{column}};
1019          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1020          !!!next-input-character;          !!!next-input-character;
1021          redo A;          redo A;
# Line 836  sub _get_next_token ($) { Line 1025  sub _get_next_token ($) {
1025          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1026              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1027            !!!cp (57);            !!!cp (57);
1028            !!!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});
1029            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1030          } else {          } else {
1031            !!!cp (58);            !!!cp (58);
# Line 865  sub _get_next_token ($) { Line 1054  sub _get_next_token ($) {
1054          $before_leave->();          $before_leave->();
1055          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1056            !!!cp (61);            !!!cp (61);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1057            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1058          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1059            !!!cp (62);            !!!cp (62);
# Line 891  sub _get_next_token ($) { Line 1078  sub _get_next_token ($) {
1078          !!!next-input-character;          !!!next-input-character;
1079          redo A;          redo A;
1080        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1081            !!!cp (64);
1082          $before_leave->();          $before_leave->();
1083            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1084          !!!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  
1085          redo A;          redo A;
1086        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1087          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1088          $before_leave->();          $before_leave->();
1089          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1090            !!!cp (66);            !!!cp (66);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1091            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1092          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1093            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 963  sub _get_next_token ($) { Line 1138  sub _get_next_token ($) {
1138        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1139          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1140            !!!cp (73);            !!!cp (73);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1141            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1142          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1143            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 987  sub _get_next_token ($) { Line 1160  sub _get_next_token ($) {
1160        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1161                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1162          !!!cp (76);          !!!cp (76);
1163          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1164                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1165                   value => '',
1166                   line => $self->{line}, column => $self->{column}};
1167          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1168          !!!next-input-character;          !!!next-input-character;
1169          redo A;          redo A;
1170        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1171            !!!cp (77);
1172            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1173          !!!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  
1174          redo A;          redo A;
1175        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1176          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1177          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1178            !!!cp (79);            !!!cp (79);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1179            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1180          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1181            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1035  sub _get_next_token ($) { Line 1197  sub _get_next_token ($) {
1197          redo A;          redo A;
1198        } else {        } else {
1199          !!!cp (82);          !!!cp (82);
1200          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1201                                value => ''};              = {name => chr ($self->{next_char}),
1202                   value => '',
1203                   line => $self->{line}, column => $self->{column}};
1204          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1205          !!!next-input-character;          !!!next-input-character;
1206          redo A;                  redo A;        
# Line 1069  sub _get_next_token ($) { Line 1233  sub _get_next_token ($) {
1233        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1234          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1235            !!!cp (87);            !!!cp (87);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1236            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1237          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1238            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1094  sub _get_next_token ($) { Line 1256  sub _get_next_token ($) {
1256          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1257          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1258            !!!cp (90);            !!!cp (90);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1259            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1260          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1261            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1143  sub _get_next_token ($) { Line 1303  sub _get_next_token ($) {
1303          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1304          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1305            !!!cp (97);            !!!cp (97);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1306            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1307          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1308            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1187  sub _get_next_token ($) { Line 1345  sub _get_next_token ($) {
1345          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1346          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1347            !!!cp (103);            !!!cp (103);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1348            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1349          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1350            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1234  sub _get_next_token ($) { Line 1390  sub _get_next_token ($) {
1390        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1391          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1392            !!!cp (109);            !!!cp (109);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1393            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1394          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1395            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1259  sub _get_next_token ($) { Line 1413  sub _get_next_token ($) {
1413          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1414          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1415            !!!cp (112);            !!!cp (112);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1416            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1417          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1418            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1331  sub _get_next_token ($) { Line 1483  sub _get_next_token ($) {
1483        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1484          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1485            !!!cp (119);            !!!cp (119);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1486            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1487          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1488            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1353  sub _get_next_token ($) { Line 1503  sub _get_next_token ($) {
1503    
1504          redo A;          redo A;
1505        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1506            !!!cp (122);
1507            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1508          !!!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  
1509          redo A;          redo A;
1510        } else {        } else {
1511          !!!cp (124);          !!!cp ('124.1');
1512          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1513          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1514          ## reconsume          ## reconsume
1515          redo A;          redo A;
1516        }        }
1517        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1518          if ($self->{next_char} == 0x003E) { # >
1519            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1520              !!!cp ('124.2');
1521              !!!parse-error (type => 'nestc', token => $self->{current_token});
1522              ## TODO: Different type than slash in start tag
1523              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1524              if ($self->{current_token}->{attributes}) {
1525                !!!cp ('124.4');
1526                !!!parse-error (type => 'end tag attribute');
1527              } else {
1528                !!!cp ('124.5');
1529              }
1530              ## TODO: Test |<title></title/>|
1531            } else {
1532              !!!cp ('124.3');
1533              $self->{self_closing} = 1;
1534            }
1535    
1536            $self->{state} = DATA_STATE;
1537            !!!next-input-character;
1538    
1539            !!!emit ($self->{current_token}); # start tag or end tag
1540    
1541            redo A;
1542          } else {
1543            !!!cp ('124.4');
1544            !!!parse-error (type => 'nestc');
1545            ## TODO: This error type is wrong.
1546            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1547            ## Reconsume.
1548            redo A;
1549          }
1550      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1551        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1552                
1553        my $token = {type => COMMENT_TOKEN, data => ''};        ## NOTE: Set by the previous state
1554          #my $token = {type => COMMENT_TOKEN, data => ''};
1555    
1556        BC: {        BC: {
1557          if ($self->{next_char} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
# Line 1385  sub _get_next_token ($) { Line 1559  sub _get_next_token ($) {
1559            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1560            !!!next-input-character;            !!!next-input-character;
1561    
1562            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1563    
1564            redo A;            redo A;
1565          } elsif ($self->{next_char} == -1) {          } elsif ($self->{next_char} == -1) {
# Line 1393  sub _get_next_token ($) { Line 1567  sub _get_next_token ($) {
1567            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1568            ## reconsume            ## reconsume
1569    
1570            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1571    
1572            redo A;            redo A;
1573          } else {          } else {
1574            !!!cp (126);            !!!cp (126);
1575            $token->{data} .= chr ($self->{next_char});            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1576            !!!next-input-character;            !!!next-input-character;
1577            redo BC;            redo BC;
1578          }          }
# Line 1408  sub _get_next_token ($) { Line 1582  sub _get_next_token ($) {
1582      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1583        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1584    
1585          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1586    
1587        my @next_char;        my @next_char;
1588        push @next_char, $self->{next_char};        push @next_char, $self->{next_char};
1589                
# Line 1416  sub _get_next_token ($) { Line 1592  sub _get_next_token ($) {
1592          push @next_char, $self->{next_char};          push @next_char, $self->{next_char};
1593          if ($self->{next_char} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1594            !!!cp (127);            !!!cp (127);
1595            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1596                                        line => $l, column => $c,
1597                                       };
1598            $self->{state} = COMMENT_START_STATE;            $self->{state} = COMMENT_START_STATE;
1599            !!!next-input-character;            !!!next-input-character;
1600            redo A;            redo A;
# Line 1452  sub _get_next_token ($) { Line 1630  sub _get_next_token ($) {
1630                      !!!cp (129);                      !!!cp (129);
1631                      ## TODO: What a stupid code this is!                      ## TODO: What a stupid code this is!
1632                      $self->{state} = DOCTYPE_STATE;                      $self->{state} = DOCTYPE_STATE;
1633                        $self->{current_token} = {type => DOCTYPE_TOKEN,
1634                                                  quirks => 1,
1635                                                  line => $l, column => $c,
1636                                                 };
1637                      !!!next-input-character;                      !!!next-input-character;
1638                      redo A;                      redo A;
1639                    } else {                    } else {
# Line 1480  sub _get_next_token ($) { Line 1662  sub _get_next_token ($) {
1662        $self->{next_char} = shift @next_char;        $self->{next_char} = shift @next_char;
1663        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1664        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
1665          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1666                                    line => $l, column => $c,
1667                                   };
1668        redo A;        redo A;
1669                
1670        ## 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 1788  sub _get_next_token ($) {
1788          redo A;          redo A;
1789        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
1790          !!!cp (152);          !!!cp (152);
1791          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
1792                            line => $self->{line_prev},
1793                            column => $self->{column_prev});
1794          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
1795          ## Stay in the state          ## Stay in the state
1796          !!!next-input-character;          !!!next-input-character;
# Line 1619  sub _get_next_token ($) { Line 1806  sub _get_next_token ($) {
1806          redo A;          redo A;
1807        } else {        } else {
1808          !!!cp (154);          !!!cp (154);
1809          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
1810                            line => $self->{line_prev},
1811                            column => $self->{column_prev});
1812          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
1813          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
1814          !!!next-input-character;          !!!next-input-character;
# Line 1658  sub _get_next_token ($) { Line 1847  sub _get_next_token ($) {
1847          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1848          !!!next-input-character;          !!!next-input-character;
1849    
1850          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
1851    
1852          redo A;          redo A;
1853        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
# Line 1667  sub _get_next_token ($) { Line 1856  sub _get_next_token ($) {
1856          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1857          ## reconsume          ## reconsume
1858    
1859          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
1860    
1861          redo A;          redo A;
1862        } else {        } else {
1863          !!!cp (160);          !!!cp (160);
1864          $self->{current_token}          $self->{current_token}->{name} = chr $self->{next_char};
1865              = {type => DOCTYPE_TOKEN,          delete $self->{current_token}->{quirks};
                name => chr ($self->{next_char}),  
                #quirks => 0,  
               };  
1866  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1867          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
1868          !!!next-input-character;          !!!next-input-character;
# Line 2203  sub _get_next_token ($) { Line 2389  sub _get_next_token ($) {
2389  sub _tokenize_attempt_to_consume_an_entity ($$$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2390    my ($self, $in_attr, $additional) = @_;    my ($self, $in_attr, $additional) = @_;
2391    
2392      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2393    
2394    if ({    if ({
2395         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2396         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 2431  sub _tokenize_attempt_to_consume_an_enti
2431            redo X;            redo X;
2432          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2433            !!!cp (1005);            !!!cp (1005);
2434            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2435            !!!back-next-input-character ($x_char, $self->{next_char});            !!!back-next-input-character ($x_char, $self->{next_char});
2436            $self->{next_char} = 0x0023; # #            $self->{next_char} = 0x0023; # #
2437            return undef;            return undef;
# Line 2252  sub _tokenize_attempt_to_consume_an_enti Line 2440  sub _tokenize_attempt_to_consume_an_enti
2440            !!!next-input-character;            !!!next-input-character;
2441          } else {          } else {
2442            !!!cp (1007);            !!!cp (1007);
2443            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc', line => $l, column => $c);
2444          }          }
2445    
2446          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2447            !!!cp (1008);            !!!cp (1008);
2448            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2449            $code = 0xFFFD;            $code = 0xFFFD;
2450          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2451            !!!cp (1009);            !!!cp (1009);
2452            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2453            $code = 0xFFFD;            $code = 0xFFFD;
2454          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2455            !!!cp (1010);            !!!cp (1010);
2456            !!!parse-error (type => 'CR character reference');            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2457            $code = 0x000A;            $code = 0x000A;
2458          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2459            !!!cp (1011);            !!!cp (1011);
2460            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2461            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2462          }          }
2463    
2464          return {type => CHARACTER_TOKEN, data => chr $code,          return {type => CHARACTER_TOKEN, data => chr $code,
2465                  has_reference => 1};                  has_reference => 1,
2466                    line => $l, column => $c,
2467                   };
2468        } # X        } # X
2469      } elsif (0x0030 <= $self->{next_char} and      } elsif (0x0030 <= $self->{next_char} and
2470               $self->{next_char} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
# Line 2295  sub _tokenize_attempt_to_consume_an_enti Line 2485  sub _tokenize_attempt_to_consume_an_enti
2485          !!!next-input-character;          !!!next-input-character;
2486        } else {        } else {
2487          !!!cp (1014);          !!!cp (1014);
2488          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc', line => $l, column => $c);
2489        }        }
2490    
2491        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2492          !!!cp (1015);          !!!cp (1015);
2493          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2494          $code = 0xFFFD;          $code = 0xFFFD;
2495        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2496          !!!cp (1016);          !!!cp (1016);
2497          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2498          $code = 0xFFFD;          $code = 0xFFFD;
2499        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2500          !!!cp (1017);          !!!cp (1017);
2501          !!!parse-error (type => 'CR character reference');          !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2502          $code = 0x000A;          $code = 0x000A;
2503        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2504          !!!cp (1018);          !!!cp (1018);
2505          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2506          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2507        }        }
2508                
2509        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2510                  line => $l, column => $c,
2511                 };
2512      } else {      } else {
2513        !!!cp (1019);        !!!cp (1019);
2514        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2515        !!!back-next-input-character ($self->{next_char});        !!!back-next-input-character ($self->{next_char});
2516        $self->{next_char} = 0x0023; # #        $self->{next_char} = 0x0023; # #
2517        return undef;        return undef;
# Line 2369  sub _tokenize_attempt_to_consume_an_enti Line 2561  sub _tokenize_attempt_to_consume_an_enti
2561            
2562      if ($match > 0) {      if ($match > 0) {
2563        !!!cp (1023);        !!!cp (1023);
2564        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2565                  line => $l, column => $c,
2566                 };
2567      } elsif ($match < 0) {      } elsif ($match < 0) {
2568        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
2569        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
2570          !!!cp (1024);          !!!cp (1024);
2571          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
2572                    line => $l, column => $c,
2573                   };
2574        } else {        } else {
2575          !!!cp (1025);          !!!cp (1025);
2576          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2577                    line => $l, column => $c,
2578                   };
2579        }        }
2580      } else {      } else {
2581        !!!cp (1026);        !!!cp (1026);
2582        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero', line => $l, column => $c);
2583        ## NOTE: "No characters are consumed" in the spec.        ## NOTE: "No characters are consumed" in the spec.
2584        return {type => CHARACTER_TOKEN, data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value,
2585                  line => $l, column => $c,
2586                 };
2587      }      }
2588    } else {    } else {
2589      !!!cp (1027);      !!!cp (1027);
2590      ## no characters are consumed      ## no characters are consumed
2591      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
2592      return undef;      return undef;
2593    }    }
2594  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 2459  sub _tree_construction_initial ($) { Line 2659  sub _tree_construction_initial ($) {
2659            defined $token->{public_identifier} or            defined $token->{public_identifier} or
2660            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
2661          !!!cp ('t1');          !!!cp ('t1');
2662          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
2663        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
2664          !!!cp ('t2');          !!!cp ('t2');
2665          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
2666          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
2667        } else {        } else {
2668          !!!cp ('t3');          !!!cp ('t3');
2669        }        }
2670                
2671        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
2672          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
2673          ## NOTE: Default value for both |public_id| and |system_id| attributes
2674          ## are empty strings, so that we don't set any value in missing cases.
2675        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
2676            if defined $token->{public_identifier};            if defined $token->{public_identifier};
2677        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2602  sub _tree_construction_initial ($) { Line 2804  sub _tree_construction_initial ($) {
2804                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
2805               }->{$token->{type}}) {               }->{$token->{type}}) {
2806        !!!cp ('t14');        !!!cp ('t14');
2807        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
2808        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2809        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
2810        ## reprocess        ## reprocess
2811          !!!ack-later;
2812        return;        return;
2813      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
2814        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 2826  sub _tree_construction_initial ($) {
2826          !!!cp ('t17');          !!!cp ('t17');
2827        }        }
2828    
2829        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
2830        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2831        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
2832        ## reprocess        ## reprocess
# Line 2652  sub _tree_construction_root_element ($) Line 2855  sub _tree_construction_root_element ($)
2855    B: {    B: {
2856        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
2857          !!!cp ('t19');          !!!cp ('t19');
2858          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
2859          ## Ignore the token          ## Ignore the token
2860          ## Stay in the insertion mode.          ## Stay in the insertion mode.
2861          !!!next-token;          !!!next-token;
# Line 2686  sub _tree_construction_root_element ($) Line 2889  sub _tree_construction_root_element ($)
2889        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
2890          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
2891            my $root_element;            my $root_element;
2892            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes});            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes}, $token);
2893            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
2894            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}},
2895                  [$root_element, $el_category->{html}];
2896    
2897            if ($token->{attributes}->{manifest}) {            if ($token->{attributes}->{manifest}) {
2898              !!!cp ('t24');              !!!cp ('t24');
2899              $self->{application_cache_selection}              $self->{application_cache_selection}
2900                  ->($token->{attributes}->{manifest}->{value});                  ->($token->{attributes}->{manifest}->{value});
2901              ## ISSUE: No relative reference resolution?              ## ISSUE: Spec is unclear on relative references.
2902                ## According to Hixie (#whatwg 2008-03-19), it should be
2903                ## resolved against the base URI of the document in HTML
2904                ## or xml:base of the element in XHTML.
2905            } else {            } else {
2906              !!!cp ('t25');              !!!cp ('t25');
2907              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
2908            }            }
2909    
2910              !!!nack ('t25c');
2911    
2912            !!!next-token;            !!!next-token;
2913            return; ## Go to the "before head" insertion mode.            return; ## Go to the "before head" insertion mode.
2914          } else {          } else {
# Line 2716  sub _tree_construction_root_element ($) Line 2925  sub _tree_construction_root_element ($)
2925          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
2926        }        }
2927    
2928      my $root_element; !!!create-element ($root_element, 'html');      my $root_element; !!!create-element ($root_element, 'html',, $token);
2929      $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
2930      push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
2931    
2932      $self->{application_cache_selection}->(undef);      $self->{application_cache_selection}->(undef);
2933    
2934      ## NOTE: Reprocess the token.      ## NOTE: Reprocess the token.
2935        !!!ack-later;
2936      return; ## Go to the "before head" insertion mode.      return; ## Go to the "before head" insertion mode.
2937    
2938      ## ISSUE: There is an issue in the spec      ## ISSUE: There is an issue in the spec
# Line 2743  sub _reset_insertion_mode ($) { Line 2953  sub _reset_insertion_mode ($) {
2953            
2954      ## Step 3      ## Step 3
2955      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"!?  
2956        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2957          $last = 1;          $last = 1;
2958          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
2959            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') {  
2960              !!!cp ('t27');              !!!cp ('t27');
2961              #              #
2962            } else {            } else {
# Line 2779  sub _reset_insertion_mode ($) { Line 2983  sub _reset_insertion_mode ($) {
2983                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
2984                        body => IN_BODY_IM,                        body => IN_BODY_IM,
2985                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
2986                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
2987                         ## TODO: Foreign namespace case OK?
2988        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2989                
2990        ## Step 14        ## Step 14
2991        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
2992          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2993            !!!cp ('t29');            !!!cp ('t29');
2994            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2908  sub _tree_construction_main ($) { Line 3113  sub _tree_construction_main ($) {
3113      !!!cp ('t39');      !!!cp ('t39');
3114    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3115    
3116    my $parse_rcdata = sub ($$) {    my $insert;
3117      my ($content_model_flag, $insert) = @_;  
3118      my $parse_rcdata = sub ($) {
3119        my ($content_model_flag) = @_;
3120    
3121      ## Step 1      ## Step 1
3122      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3123      my $el;      my $el;
3124      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $start_tag_name, $token->{attributes}, $token);
3125    
3126      ## Step 2      ## Step 2
3127      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3128    
3129      ## Step 3      ## Step 3
3130      $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 3132  sub _tree_construction_main ($) {
3132    
3133      ## Step 4      ## Step 4
3134      my $text = '';      my $text = '';
3135        !!!nack ('t40.1');
3136      !!!next-token;      !!!next-token;
3137      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3138        !!!cp ('t40');        !!!cp ('t40');
# Line 2947  sub _tree_construction_main ($) { Line 3155  sub _tree_construction_main ($) {
3155          $token->{tag_name} eq $start_tag_name) {          $token->{tag_name} eq $start_tag_name) {
3156        !!!cp ('t42');        !!!cp ('t42');
3157        ## 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});  
3158      } else {      } else {
3159        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3160          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3161            !!!cp ('t43');
3162            !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3163          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3164            !!!cp ('t44');
3165            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3166          } else {
3167            die "$0: $content_model_flag in parse_rcdata";
3168          }
3169      }      }
3170      !!!next-token;      !!!next-token;
3171    }; # $parse_rcdata    }; # $parse_rcdata
3172    
3173    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3174      my $script_el;      my $script_el;
3175      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes}, $token);
3176      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3177    
3178      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3179      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3180            
3181      my $text = '';      my $text = '';
3182        !!!nack ('t45.1');
3183      !!!next-token;      !!!next-token;
3184      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3185        !!!cp ('t45');        !!!cp ('t45');
# Line 2988  sub _tree_construction_main ($) { Line 3199  sub _tree_construction_main ($) {
3199        ## Ignore the token        ## Ignore the token
3200      } else {      } else {
3201        !!!cp ('t48');        !!!cp ('t48');
3202        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3203        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3204        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3205      }      }
# Line 3011  sub _tree_construction_main ($) { Line 3222  sub _tree_construction_main ($) {
3222      !!!next-token;      !!!next-token;
3223    }; # $script_start_tag    }; # $script_start_tag
3224    
3225      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3226      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3227      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3228    
3229    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3230      my $tag_name = shift;      my $end_tag_token = shift;
3231        my $tag_name = $end_tag_token->{tag_name};
3232    
3233        ## NOTE: The adoption agency algorithm (AAA).
3234    
3235      FET: {      FET: {
3236        ## Step 1        ## Step 1
3237        my $formatting_element;        my $formatting_element;
3238        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3239        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3240          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3241              !!!cp ('t52');
3242              last AFE;
3243            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3244                         eq $tag_name) {
3245            !!!cp ('t51');            !!!cp ('t51');
3246            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3247            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3248            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
3249          }          }
3250        } # AFE        } # AFE
3251        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3252          !!!cp ('t53');          !!!cp ('t53');
3253          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3254          ## Ignore the token          ## Ignore the token
3255          !!!next-token;          !!!next-token;
3256          return;          return;
# Line 3048  sub _tree_construction_main ($) { Line 3267  sub _tree_construction_main ($) {
3267              last INSCOPE;              last INSCOPE;
3268            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3269              !!!cp ('t55');              !!!cp ('t55');
3270              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3271                                token => $end_tag_token);
3272              ## Ignore the token              ## Ignore the token
3273              !!!next-token;              !!!next-token;
3274              return;              return;
3275            }            }
3276          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
                   table => 1, caption => 1, td => 1, th => 1,  
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3277            !!!cp ('t56');            !!!cp ('t56');
3278            $in_scope = 0;            $in_scope = 0;
3279          }          }
3280        } # INSCOPE        } # INSCOPE
3281        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3282          !!!cp ('t57');          !!!cp ('t57');
3283          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3284                            token => $end_tag_token);
3285          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3286          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3287          return;          return;
3288        }        }
3289        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3290          !!!cp ('t58');          !!!cp ('t58');
3291          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!parse-error (type => 'not closed',
3292                            value => $self->{open_elements}->[-1]->[0]
3293                                ->manakai_local_name,
3294                            token => $end_tag_token);
3295        }        }
3296                
3297        ## Step 2        ## Step 2
# Line 3078  sub _tree_construction_main ($) { Line 3299  sub _tree_construction_main ($) {
3299        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3300        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3301          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3302          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3303              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3304              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3305               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3306            !!!cp ('t59');            !!!cp ('t59');
3307            $furthest_block = $node;            $furthest_block = $node;
3308            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3167  sub _tree_construction_main ($) { Line 3388  sub _tree_construction_main ($) {
3388        } # S7          } # S7  
3389                
3390        ## Step 8        ## Step 8
3391        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3392            my $foster_parent_element;
3393            my $next_sibling;
3394            OE: for (reverse 0..$#{$self->{open_elements}}) {
3395              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3396                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3397                                 if (defined $parent and $parent->node_type == 1) {
3398                                   !!!cp ('t65.1');
3399                                   $foster_parent_element = $parent;
3400                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3401                                 } else {
3402                                   !!!cp ('t65.2');
3403                                   $foster_parent_element
3404                                     = $self->{open_elements}->[$_ - 1]->[0];
3405                                 }
3406                                 last OE;
3407                               }
3408                             } # OE
3409                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3410                               unless defined $foster_parent_element;
3411            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3412            $open_tables->[-1]->[1] = 1; # tainted
3413          } else {
3414            !!!cp ('t65.3');
3415            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3416          }
3417                
3418        ## Step 9        ## Step 9
3419        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 3213  sub _tree_construction_main ($) { Line 3459  sub _tree_construction_main ($) {
3459      } # FET      } # FET
3460    }; # $formatting_end_tag    }; # $formatting_end_tag
3461    
3462    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3463      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3464    }; # $insert_to_current    }; # $insert_to_current
3465    
3466    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3467                         my $child = shift;      my $child = shift;
3468                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3469                              table => 1, tbody => 1, tfoot => 1,        # MUST
3470                              thead => 1, tr => 1,        my $foster_parent_element;
3471                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
3472                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
3473                           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') {  
3474                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3475                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3476                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3245  sub _tree_construction_main ($) { Line 3488  sub _tree_construction_main ($) {
3488                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3489                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3490                             ($child, $next_sibling);                             ($child, $next_sibling);
3491                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3492                           !!!cp ('t72');      } else {
3493                           $self->{open_elements}->[-1]->[0]->append_child ($child);        !!!cp ('t72');
3494                         }        $self->{open_elements}->[-1]->[0]->append_child ($child);
3495        }
3496    }; # $insert_to_foster    }; # $insert_to_foster
3497    
   my $insert;  
   
3498    B: {    B: {
3499      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3500        !!!cp ('t73');        !!!cp ('t73');
3501        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3502        ## Ignore the token        ## Ignore the token
3503        ## Stay in the phase        ## Stay in the phase
3504        !!!next-token;        !!!next-token;
3505        redo B;        redo B;
     } elsif ($token->{type} == END_OF_FILE_TOKEN) {  
       if ($self->{insertion_mode} & AFTER_HTML_IMS) {  
         !!!cp ('t74');  
         #  
       } else {  
         ## Generate implied end tags  
         while ({  
                 dd => 1, dt => 1, li => 1, p => 1,  
                }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!cp ('t75');  
           pop @{$self->{open_elements}};  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!cp ('t76');  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
 ## ISSUE: This case is never reached.  
           !!!cp ('t77');  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } else {  
           !!!cp ('t78');  
         }  
   
         ## ISSUE: There is an issue in the spec.  
       }  
   
       ## Stop parsing  
       last B;  
3506      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3507               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3508        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3509          !!!cp ('t79');          !!!cp ('t79');
3510          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3511          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
3512        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3513          !!!cp ('t80');          !!!cp ('t80');
3514          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3515          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3516        } else {        } else {
3517          !!!cp ('t81');          !!!cp ('t81');
3518        }        }
3519    
3520        !!!cp ('t82');        !!!cp ('t82');
3521        !!!parse-error (type => 'not first start tag');        !!!parse-error (type => 'not first start tag', token => $token);
3522        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
3523        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
3524          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
# Line 3318  sub _tree_construction_main ($) { Line 3528  sub _tree_construction_main ($) {
3528               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
3529          }          }
3530        }        }
3531          !!!nack ('t84.1');
3532        !!!next-token;        !!!next-token;
3533        redo B;        redo B;
3534      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
# Line 3337  sub _tree_construction_main ($) { Line 3548  sub _tree_construction_main ($) {
3548      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & HEAD_IMS) {
3549        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
3550          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3551            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3552                !!!cp ('t88.2');
3553                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3554              } else {
3555                !!!cp ('t88.1');
3556                ## Ignore the token.
3557                !!!next-token;
3558                redo B;
3559              }
3560            unless (length $token->{data}) {            unless (length $token->{data}) {
3561              !!!cp ('t88');              !!!cp ('t88');
3562              !!!next-token;              !!!next-token;
# Line 3348  sub _tree_construction_main ($) { Line 3567  sub _tree_construction_main ($) {
3567          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3568            !!!cp ('t89');            !!!cp ('t89');
3569            ## As if <head>            ## As if <head>
3570            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, 'head',, $token);
3571            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3572            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
3573                  [$self->{head_element}, $el_category->{head}];
3574    
3575            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
3576            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3360  sub _tree_construction_main ($) { Line 3580  sub _tree_construction_main ($) {
3580            !!!cp ('t90');            !!!cp ('t90');
3581            ## As if </noscript>            ## As if </noscript>
3582            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
3583            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character', token => $token);
3584                        
3585            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
3586            ## As if </head>            ## As if </head>
# Line 3376  sub _tree_construction_main ($) { Line 3596  sub _tree_construction_main ($) {
3596            !!!cp ('t92');            !!!cp ('t92');
3597          }          }
3598    
3599              ## "after head" insertion mode          ## "after head" insertion mode
3600              ## As if <body>          ## As if <body>
3601              !!!insert-element ('body');          !!!insert-element ('body',, $token);
3602              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
3603              ## reprocess          ## reprocess
3604            redo B;
3605          } elsif ($token->{type} == START_TAG_TOKEN) {
3606            if ($token->{tag_name} eq 'head') {
3607              if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3608                !!!cp ('t93');
3609                !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes}, $token);
3610                $self->{open_elements}->[-1]->[0]->append_child
3611                    ($self->{head_element});
3612                push @{$self->{open_elements}},
3613                    [$self->{head_element}, $el_category->{head}];
3614                $self->{insertion_mode} = IN_HEAD_IM;
3615                !!!nack ('t93.1');
3616                !!!next-token;
3617              redo B;              redo B;
3618            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3619              if ($token->{tag_name} eq 'head') {              !!!cp ('t94');
3620                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {              #
3621                  !!!cp ('t93');            } else {
3622                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!cp ('t95');
3623                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
3624                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];              ## Ignore the token
3625                  $self->{insertion_mode} = IN_HEAD_IM;              !!!nack ('t95.1');
3626                  !!!next-token;              !!!next-token;
3627                  redo B;              redo B;
3628                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {            }
3629                  !!!cp ('t94');          } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
                 #  
               } else {  
                 !!!cp ('t95');  
                 !!!parse-error (type => 'in head:head'); # or in head noscript  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
             } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {  
3630                !!!cp ('t96');                !!!cp ('t96');
3631                ## As if <head>                ## As if <head>
3632                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head',, $token);
3633                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3634                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}},
3635                      [$self->{head_element}, $el_category->{head}];
3636    
3637                $self->{insertion_mode} = IN_HEAD_IM;                $self->{insertion_mode} = IN_HEAD_IM;
3638                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
# Line 3420  sub _tree_construction_main ($) { Line 3645  sub _tree_construction_main ($) {
3645                  !!!cp ('t98');                  !!!cp ('t98');
3646                  ## As if </noscript>                  ## As if </noscript>
3647                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3648                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base', token => $token);
3649                                
3650                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3651                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3431  sub _tree_construction_main ($) { Line 3656  sub _tree_construction_main ($) {
3656                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3657                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3658                  !!!cp ('t100');                  !!!cp ('t100');
3659                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3660                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
3661                        [$self->{head_element}, $el_category->{head}];
3662                } else {                } else {
3663                  !!!cp ('t101');                  !!!cp ('t101');
3664                }                }
3665                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3666                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3667                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3668                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3669                  !!!nack ('t101.1');
3670                !!!next-token;                !!!next-token;
3671                redo B;                redo B;
3672              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
3673                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3674                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3675                  !!!cp ('t102');                  !!!cp ('t102');
3676                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3677                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
3678                        [$self->{head_element}, $el_category->{head}];
3679                } else {                } else {
3680                  !!!cp ('t103');                  !!!cp ('t103');
3681                }                }
3682                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3683                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3684                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3685                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3686                  !!!ack ('t103.1');
3687                !!!next-token;                !!!next-token;
3688                redo B;                redo B;
3689              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
3690                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3691                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3692                  !!!cp ('t104');                  !!!cp ('t104');
3693                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3694                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
3695                        [$self->{head_element}, $el_category->{head}];
3696                } else {                } else {
3697                  !!!cp ('t105');                  !!!cp ('t105');
3698                }                }
3699                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3700                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.
3701    
3702                unless ($self->{confident}) {                unless ($self->{confident}) {
3703                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
3704                    !!!cp ('t106');                    !!!cp ('t106');
3705                    $self->{change_encoding}                    $self->{change_encoding}
3706                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
3707                             $token);
3708                                        
3709                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3710                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
# Line 3488  sub _tree_construction_main ($) { Line 3719  sub _tree_construction_main ($) {
3719                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
3720                      !!!cp ('t107');                      !!!cp ('t107');
3721                      $self->{change_encoding}                      $self->{change_encoding}
3722                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
3723                               $token);
3724                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3725                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
3726                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
# Line 3514  sub _tree_construction_main ($) { Line 3746  sub _tree_construction_main ($) {
3746                  }                  }
3747                }                }
3748    
3749                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3750                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3751                  !!!ack ('t110.1');
3752                !!!next-token;                !!!next-token;
3753                redo B;                redo B;
3754              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
# Line 3523  sub _tree_construction_main ($) { Line 3756  sub _tree_construction_main ($) {
3756                  !!!cp ('t111');                  !!!cp ('t111');
3757                  ## As if </noscript>                  ## As if </noscript>
3758                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3759                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title', token => $token);
3760                                
3761                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3762                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3763                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3764                  !!!cp ('t112');                  !!!cp ('t112');
3765                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3766                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
3767                        [$self->{head_element}, $el_category->{head}];
3768                } else {                } else {
3769                  !!!cp ('t113');                  !!!cp ('t113');
3770                }                }
# Line 3538  sub _tree_construction_main ($) { Line 3772  sub _tree_construction_main ($) {
3772                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3773                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
3774                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
3775                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
3776                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
3777                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3778                redo B;                redo B;
3779              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
# Line 3549  sub _tree_construction_main ($) { Line 3782  sub _tree_construction_main ($) {
3782                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3783                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3784                  !!!cp ('t114');                  !!!cp ('t114');
3785                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3786                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
3787                        [$self->{head_element}, $el_category->{head}];
3788                } else {                } else {
3789                  !!!cp ('t115');                  !!!cp ('t115');
3790                }                }
3791                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
3792                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3793                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3794                redo B;                redo B;
3795              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
3796                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
3797                  !!!cp ('t116');                  !!!cp ('t116');
3798                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
3799                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3800                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
3801                    !!!nack ('t116.1');
3802                  !!!next-token;                  !!!next-token;
3803                  redo B;                  redo B;
3804                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3805                  !!!cp ('t117');                  !!!cp ('t117');
3806                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript:noscript', token => $token);
3807                  ## Ignore the token                  ## Ignore the token
3808                    !!!nack ('t117.1');
3809                  !!!next-token;                  !!!next-token;
3810                  redo B;                  redo B;
3811                } else {                } else {
# Line 3581  sub _tree_construction_main ($) { Line 3817  sub _tree_construction_main ($) {
3817                  !!!cp ('t119');                  !!!cp ('t119');
3818                  ## As if </noscript>                  ## As if </noscript>
3819                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3820                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script', token => $token);
3821                                
3822                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3823                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3824                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3825                  !!!cp ('t120');                  !!!cp ('t120');
3826                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
3827                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
3828                        [$self->{head_element}, $el_category->{head}];
3829                } else {                } else {
3830                  !!!cp ('t121');                  !!!cp ('t121');
3831                }                }
3832    
3833                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3834                $script_start_tag->($insert_to_current);                $script_start_tag->();
3835                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
3836                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3837                redo B;                redo B;
3838              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
# Line 3604  sub _tree_construction_main ($) { Line 3841  sub _tree_construction_main ($) {
3841                  !!!cp ('t122');                  !!!cp ('t122');
3842                  ## As if </noscript>                  ## As if </noscript>
3843                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3844                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
3845                                    
3846                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3847                  ## As if </head>                  ## As if </head>
# Line 3621  sub _tree_construction_main ($) { Line 3858  sub _tree_construction_main ($) {
3858                }                }
3859    
3860                ## "after head" insertion mode                ## "after head" insertion mode
3861                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
3862                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
3863                  !!!cp ('t126');                  !!!cp ('t126');
3864                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
# Line 3631  sub _tree_construction_main ($) { Line 3868  sub _tree_construction_main ($) {
3868                } else {                } else {
3869                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
3870                }                }
3871                  !!!nack ('t127.1');
3872                !!!next-token;                !!!next-token;
3873                redo B;                redo B;
3874              } else {              } else {
# Line 3642  sub _tree_construction_main ($) { Line 3880  sub _tree_construction_main ($) {
3880                !!!cp ('t129');                !!!cp ('t129');
3881                ## As if </noscript>                ## As if </noscript>
3882                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3883                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
3884                                
3885                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
3886                ## As if </head>                ## As if </head>
# Line 3661  sub _tree_construction_main ($) { Line 3899  sub _tree_construction_main ($) {
3899    
3900              ## "after head" insertion mode              ## "after head" insertion mode
3901              ## As if <body>              ## As if <body>
3902              !!!insert-element ('body');              !!!insert-element ('body',, $token);
3903              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
3904              ## reprocess              ## reprocess
3905                !!!ack-later;
3906              redo B;              redo B;
3907            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
3908              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
3909                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3910                  !!!cp ('t132');                  !!!cp ('t132');
3911                  ## As if <head>                  ## As if <head>
3912                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head',, $token);
3913                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3914                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
3915                        [$self->{head_element}, $el_category->{head}];
3916    
3917                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3918                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 3683  sub _tree_construction_main ($) { Line 3923  sub _tree_construction_main ($) {
3923                  !!!cp ('t133');                  !!!cp ('t133');
3924                  ## As if </noscript>                  ## As if </noscript>
3925                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3926                  !!!parse-error (type => 'in noscript:/head');                  !!!parse-error (type => 'in noscript:/head', token => $token);
3927                                    
3928                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3929                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 3709  sub _tree_construction_main ($) { Line 3949  sub _tree_construction_main ($) {
3949                  redo B;                  redo B;
3950                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3951                  !!!cp ('t137');                  !!!cp ('t137');
3952                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
3953                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
3954                  !!!next-token;                  !!!next-token;
3955                  redo B;                  redo B;
# Line 3723  sub _tree_construction_main ($) { Line 3963  sub _tree_construction_main ($) {
3963                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3964                  !!!cp ('t139');                  !!!cp ('t139');
3965                  ## As if <head>                  ## As if <head>
3966                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head',, $token);
3967                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3968                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
3969                        [$self->{head_element}, $el_category->{head}];
3970    
3971                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3972                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3973                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3974                  !!!cp ('t140');                  !!!cp ('t140');
3975                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
3976                  ## Ignore the token                  ## Ignore the token
3977                  !!!next-token;                  !!!next-token;
3978                  redo B;                  redo B;
# Line 3746  sub _tree_construction_main ($) { Line 3987  sub _tree_construction_main ($) {
3987                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3988                  !!!cp ('t142');                  !!!cp ('t142');
3989                  ## As if <head>                  ## As if <head>
3990                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head',, $token);
3991                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3992                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
3993                        [$self->{head_element}, $el_category->{head}];
3994    
3995                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3996                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3763  sub _tree_construction_main ($) { Line 4005  sub _tree_construction_main ($) {
4005                  #                  #
4006                } else {                } else {
4007                  !!!cp ('t145');                  !!!cp ('t145');
4008                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4009                  ## Ignore the token                  ## Ignore the token
4010                  !!!next-token;                  !!!next-token;
4011                  redo B;                  redo B;
# Line 3774  sub _tree_construction_main ($) { Line 4016  sub _tree_construction_main ($) {
4016                !!!cp ('t146');                !!!cp ('t146');
4017                ## As if </noscript>                ## As if </noscript>
4018                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4019                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4020                                
4021                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4022                ## As if </head>                ## As if </head>
# Line 3790  sub _tree_construction_main ($) { Line 4032  sub _tree_construction_main ($) {
4032              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4033  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
4034                !!!cp ('t148');                !!!cp ('t148');
4035                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4036                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4037                !!!next-token;                !!!next-token;
4038                redo B;                redo B;
# Line 3800  sub _tree_construction_main ($) { Line 4042  sub _tree_construction_main ($) {
4042    
4043              ## "after head" insertion mode              ## "after head" insertion mode
4044              ## As if <body>              ## As if <body>
4045              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4046              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4047              ## reprocess              ## reprocess
4048              redo B;              redo B;
4049            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4050              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4051            }            !!!cp ('t149.1');
4052    
4053              ## NOTE: As if <head>
4054              !!!create-element ($self->{head_element}, 'head',, $token);
4055              $self->{open_elements}->[-1]->[0]->append_child
4056                  ($self->{head_element});
4057              #push @{$self->{open_elements}},
4058              #    [$self->{head_element}, $el_category->{head}];
4059              #$self->{insertion_mode} = IN_HEAD_IM;
4060              ## NOTE: Reprocess.
4061    
4062              ## NOTE: As if </head>
4063              #pop @{$self->{open_elements}};
4064              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4065              ## NOTE: Reprocess.
4066              
4067              #
4068            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4069              !!!cp ('t149.2');
4070    
4071              ## NOTE: As if </head>
4072              pop @{$self->{open_elements}};
4073              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4074              ## NOTE: Reprocess.
4075    
4076              #
4077            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4078              !!!cp ('t149.3');
4079    
4080              !!!parse-error (type => 'in noscript:#eof', token => $token);
4081    
4082              ## As if </noscript>
4083              pop @{$self->{open_elements}};
4084              #$self->{insertion_mode} = IN_HEAD_IM;
4085              ## NOTE: Reprocess.
4086    
4087              ## NOTE: As if </head>
4088              pop @{$self->{open_elements}};
4089              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4090              ## NOTE: Reprocess.
4091    
4092              #
4093            } else {
4094              !!!cp ('t149.4');
4095              #
4096            }
4097    
4098            ## NOTE: As if <body>
4099            !!!insert-element ('body',, $token);
4100            $self->{insertion_mode} = IN_BODY_IM;
4101            ## NOTE: Reprocess.
4102            redo B;
4103          } else {
4104            die "$0: $token->{type}: Unknown token type";
4105          }
4106    
4107            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
4108      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
# Line 3826  sub _tree_construction_main ($) { Line 4122  sub _tree_construction_main ($) {
4122                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4123                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
4124                  ## have an element in table scope                  ## have an element in table scope
4125                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
4126                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4127                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4128                      !!!cp ('t151');                      !!!cp ('t151');
4129                      $tn = $node->[1];  
4130                      last INSCOPE;                      ## Close the cell
4131                    } elsif ({                      !!!back-token; # <x>
4132                              table => 1, html => 1,                      $token = {type => END_TAG_TOKEN,
4133                             }->{$node->[1]}) {                                tag_name => $node->[0]->manakai_local_name,
4134                      !!!cp ('t152');                                line => $token->{line},
4135                      last INSCOPE;                                column => $token->{column}};
                   }  
                 } # INSCOPE  
                   unless (defined $tn) {  
                     !!!cp ('t153');  
 ## TODO: This error type is wrong.  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
4136                      redo B;                      redo B;
4137                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4138                        !!!cp ('t152');
4139                        ## ISSUE: This case can never be reached, maybe.
4140                        last;
4141                    }                    }
4142                                    }
4143                  !!!cp ('t154');  
4144                  ## Close the cell                  !!!cp ('t153');
4145                  !!!back-token; # <?>                  !!!parse-error (type => 'start tag not allowed',
4146                  $token = {type => END_TAG_TOKEN, tag_name => $tn};                      value => $token->{tag_name}, token => $token);
4147                    ## Ignore the token
4148                    !!!nack ('t153.1');
4149                    !!!next-token;
4150                  redo B;                  redo B;
4151                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4152                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption', token => $token);
4153                                    
4154                  ## As if </caption>                  ## NOTE: As if </caption>.
4155                  ## have a table element in table scope                  ## have a table element in table scope
4156                  my $i;                  my $i;
4157                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4158                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4159                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
4160                      !!!cp ('t155');                      if ($node->[1] & CAPTION_EL) {
4161                      $i = $_;                        !!!cp ('t155');
4162                      last INSCOPE;                        $i = $_;
4163                    } elsif ({                        last INSCOPE;
4164                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4165                             }->{$node->[1]}) {                        !!!cp ('t156');
4166                      !!!cp ('t156');                        last;
4167                      last INSCOPE;                      }
4168                    }                    }
4169    
4170                      !!!cp ('t157');
4171                      !!!parse-error (type => 'start tag not allowed',
4172                                      value => $token->{tag_name}, token => $token);
4173                      ## Ignore the token
4174                      !!!nack ('t157.1');
4175                      !!!next-token;
4176                      redo B;
4177                  } # 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;  
                   }  
4178                                    
4179                  ## generate implied end tags                  ## generate implied end tags
4180                  while ({                  while ($self->{open_elements}->[-1]->[1]
4181                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4182                    !!!cp ('t158');                    !!!cp ('t158');
4183                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4184                  }                  }
4185    
4186                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4187                    !!!cp ('t159');                    !!!cp ('t159');
4188                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4189                                      value => $self->{open_elements}->[-1]->[0]
4190                                          ->manakai_local_name,
4191                                      token => $token);
4192                  } else {                  } else {
4193                    !!!cp ('t160');                    !!!cp ('t160');
4194                  }                  }
# Line 3904  sub _tree_construction_main ($) { Line 4200  sub _tree_construction_main ($) {
4200                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4201                                    
4202                  ## reprocess                  ## reprocess
4203                    !!!ack-later;
4204                  redo B;                  redo B;
4205                } else {                } else {
4206                  !!!cp ('t161');                  !!!cp ('t161');
# Line 3920  sub _tree_construction_main ($) { Line 4217  sub _tree_construction_main ($) {
4217                  my $i;                  my $i;
4218                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4219                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4220                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4221                      !!!cp ('t163');                      !!!cp ('t163');
4222                      $i = $_;                      $i = $_;
4223                      last INSCOPE;                      last INSCOPE;
4224                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4225                      !!!cp ('t164');                      !!!cp ('t164');
4226                      last INSCOPE;                      last INSCOPE;
4227                    }                    }
4228                  } # INSCOPE                  } # INSCOPE
4229                    unless (defined $i) {                    unless (defined $i) {
4230                      !!!cp ('t165');                      !!!cp ('t165');
4231                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4232                      ## Ignore the token                      ## Ignore the token
4233                      !!!next-token;                      !!!next-token;
4234                      redo B;                      redo B;
4235                    }                    }
4236                                    
4237                  ## generate implied end tags                  ## generate implied end tags
4238                  while ({                  while ($self->{open_elements}->[-1]->[1]
4239                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4240                    !!!cp ('t166');                    !!!cp ('t166');
4241                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4242                  }                  }
4243    
4244                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4245                            ne $token->{tag_name}) {
4246                    !!!cp ('t167');                    !!!cp ('t167');
4247                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4248                                      value => $self->{open_elements}->[-1]->[0]
4249                                          ->manakai_local_name,
4250                                      token => $token);
4251                  } else {                  } else {
4252                    !!!cp ('t168');                    !!!cp ('t168');
4253                  }                  }
# Line 3964  sub _tree_construction_main ($) { Line 4262  sub _tree_construction_main ($) {
4262                  redo B;                  redo B;
4263                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4264                  !!!cp ('t169');                  !!!cp ('t169');
4265                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4266                  ## Ignore the token                  ## Ignore the token
4267                  !!!next-token;                  !!!next-token;
4268                  redo B;                  redo B;
# Line 3976  sub _tree_construction_main ($) { Line 4274  sub _tree_construction_main ($) {
4274                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
4275                  ## have a table element in table scope                  ## have a table element in table scope
4276                  my $i;                  my $i;
4277                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4278                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4279                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
4280                      !!!cp ('t171');                      if ($node->[1] & CAPTION_EL) {
4281                      $i = $_;                        !!!cp ('t171');
4282                      last INSCOPE;                        $i = $_;
4283                    } elsif ({                        last INSCOPE;
4284                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4285                             }->{$node->[1]}) {                        !!!cp ('t172');
4286                      !!!cp ('t172');                        last;
4287                      last INSCOPE;                      }
4288                    }                    }
4289    
4290                      !!!cp ('t173');
4291                      !!!parse-error (type => 'unmatched end tag',
4292                                      value => $token->{tag_name}, token => $token);
4293                      ## Ignore the token
4294                      !!!next-token;
4295                      redo B;
4296                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t173');  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4297                                    
4298                  ## generate implied end tags                  ## generate implied end tags
4299                  while ({                  while ($self->{open_elements}->[-1]->[1]
4300                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
4301                    !!!cp ('t174');                    !!!cp ('t174');
4302                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4303                  }                  }
4304                                    
4305                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4306                    !!!cp ('t175');                    !!!cp ('t175');
4307                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
4308                                      value => $self->{open_elements}->[-1]->[0]
4309                                          ->manakai_local_name,
4310                                      token => $token);
4311                  } else {                  } else {
4312                    !!!cp ('t176');                    !!!cp ('t176');
4313                  }                  }
# Line 4022  sub _tree_construction_main ($) { Line 4322  sub _tree_construction_main ($) {
4322                  redo B;                  redo B;
4323                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4324                  !!!cp ('t177');                  !!!cp ('t177');
4325                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4326                  ## Ignore the token                  ## Ignore the token
4327                  !!!next-token;                  !!!next-token;
4328                  redo B;                  redo B;
# Line 4038  sub _tree_construction_main ($) { Line 4338  sub _tree_construction_main ($) {
4338                ## have an element in table scope                ## have an element in table scope
4339                my $i;                my $i;
4340                my $tn;                my $tn;
4341                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
4342                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
4343                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4344                    !!!cp ('t179');                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4345                    $i = $_;                      !!!cp ('t179');
4346                    last INSCOPE;                      $i = $_;
4347                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
4348                    !!!cp ('t180');                      ## Close the cell
4349                    $tn = $node->[1];                      !!!back-token; # </x>
4350                    ## NOTE: There is exactly one |td| or |th| element                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4351                    ## in scope in the stack of open elements by definition.                                line => $token->{line},
4352                  } elsif ({                                column => $token->{column}};
4353                            table => 1, html => 1,                      redo B;
4354                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_CELL_EL) {
4355                    !!!cp ('t181');                      !!!cp ('t180');
4356                    last INSCOPE;                      $tn = $node->[0]->manakai_local_name;
4357                        ## NOTE: There is exactly one |td| or |th| element
4358                        ## in scope in the stack of open elements by definition.
4359                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4360                        ## ISSUE: Can this be reached?
4361                        !!!cp ('t181');
4362                        last;
4363                      }
4364                  }                  }
4365                } # INSCOPE  
               unless (defined $i) {  
4366                  !!!cp ('t182');                  !!!cp ('t182');
4367                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4368                        value => $token->{tag_name}, token => $token);
4369                  ## Ignore the token                  ## Ignore the token
4370                  !!!next-token;                  !!!next-token;
4371                  redo B;                  redo B;
4372                } else {                } # INSCOPE
                 !!!cp ('t183');  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
4373              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
4374                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4375                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4376    
4377                ## As if </caption>                ## As if </caption>
4378                ## have a table element in table scope                ## have a table element in table scope
4379                my $i;                my $i;
4380                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4381                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4382                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4383                    !!!cp ('t184');                    !!!cp ('t184');
4384                    $i = $_;                    $i = $_;
4385                    last INSCOPE;                    last INSCOPE;
4386                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
4387                    !!!cp ('t185');                    !!!cp ('t185');
4388                    last INSCOPE;                    last INSCOPE;
4389                  }                  }
4390                } # INSCOPE                } # INSCOPE
4391                unless (defined $i) {                unless (defined $i) {
4392                  !!!cp ('t186');                  !!!cp ('t186');
4393                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);
4394                  ## Ignore the token                  ## Ignore the token
4395                  !!!next-token;                  !!!next-token;
4396                  redo B;                  redo B;
4397                }                }
4398                                
4399                ## generate implied end tags                ## generate implied end tags
4400                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
4401                  !!!cp ('t187');                  !!!cp ('t187');
4402                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4403                }                }
4404    
4405                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4406                  !!!cp ('t188');                  !!!cp ('t188');
4407                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
4408                                    value => $self->{open_elements}->[-1]->[0]
4409                                        ->manakai_local_name,
4410                                    token => $token);
4411                } else {                } else {
4412                  !!!cp ('t189');                  !!!cp ('t189');
4413                }                }
# Line 4126  sub _tree_construction_main ($) { Line 4425  sub _tree_construction_main ($) {
4425                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4426                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4427                  !!!cp ('t190');                  !!!cp ('t190');
4428                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4429                  ## Ignore the token                  ## Ignore the token
4430                  !!!next-token;                  !!!next-token;
4431                  redo B;                  redo B;
# Line 4140  sub _tree_construction_main ($) { Line 4439  sub _tree_construction_main ($) {
4439                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
4440                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4441                !!!cp ('t192');                !!!cp ('t192');
4442                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4443                ## Ignore the token                ## Ignore the token
4444                !!!next-token;                !!!next-token;
4445                redo B;                redo B;
# Line 4148  sub _tree_construction_main ($) { Line 4447  sub _tree_construction_main ($) {
4447                !!!cp ('t193');                !!!cp ('t193');
4448                #                #
4449              }              }
4450          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4451            for my $entry (@{$self->{open_elements}}) {
4452              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
4453                !!!cp ('t75');
4454                !!!parse-error (type => 'in body:#eof', token => $token);
4455                last;
4456              }
4457            }
4458    
4459            ## Stop parsing.
4460            last B;
4461        } else {        } else {
4462          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4463        }        }
# Line 4156  sub _tree_construction_main ($) { Line 4466  sub _tree_construction_main ($) {
4466        #        #
4467      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
4468        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4469              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
4470                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4471              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4472                                
4473                unless (length $token->{data}) {            unless (length $token->{data}) {
4474                  !!!cp ('t194');              !!!cp ('t194');
4475                  !!!next-token;              !!!next-token;
4476                  redo B;              redo B;
4477                } else {            } else {
4478                  !!!cp ('t195');              !!!cp ('t195');
4479                }            }
4480              }          }
4481    
4482              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
4483    
4484              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
4485              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 4176  sub _tree_construction_main ($) { Line 4487  sub _tree_construction_main ($) {
4487              ## result in a new Text node.              ## result in a new Text node.
4488              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
4489                            
4490              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]}) {  
4491                # MUST                # MUST
4492                my $foster_parent_element;                my $foster_parent_element;
4493                my $next_sibling;                my $next_sibling;
4494                my $prev_sibling;                my $prev_sibling;
4495                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
4496                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4497                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4498                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
4499                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4213  sub _tree_construction_main ($) { Line 4521  sub _tree_construction_main ($) {
4521                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
4522                     $next_sibling);                     $next_sibling);
4523                }                }
4524              } else {            $open_tables->[-1]->[1] = 1; # tainted
4525                !!!cp ('t200');          } else {
4526                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            !!!cp ('t200');
4527              }            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4528            }
4529                            
4530              !!!next-token;          !!!next-token;
4531              redo B;          redo B;
4532        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4533              if ({              if ({
4534                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
# Line 4227  sub _tree_construction_main ($) { Line 4536  sub _tree_construction_main ($) {
4536                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4537                if ($self->{insertion_mode} == IN_TABLE_IM) {                if ($self->{insertion_mode} == IN_TABLE_IM) {
4538                  ## Clear back to table context                  ## Clear back to table context
4539                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
4540                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
4541                    !!!cp ('t201');                    !!!cp ('t201');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4542                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4543                  }                  }
4544                                    
4545                  !!!insert-element ('tbody');                  !!!insert-element ('tbody',, $token);
4546                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4547                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
4548                }                }
# Line 4242  sub _tree_construction_main ($) { Line 4550  sub _tree_construction_main ($) {
4550                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4551                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
4552                    !!!cp ('t202');                    !!!cp ('t202');
4553                    !!!parse-error (type => 'missing start tag:tr');                    !!!parse-error (type => 'missing start tag:tr', token => $token);
4554                  }                  }
4555                                    
4556                  ## Clear back to table body context                  ## Clear back to table body context
4557                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
4558                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
4559                    !!!cp ('t203');                    !!!cp ('t203');
4560                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4561                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4562                  }                  }
4563                                    
4564                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4565                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
4566                    !!!cp ('t204');                    !!!cp ('t204');
4567                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4568                      !!!nack ('t204');
4569                    !!!next-token;                    !!!next-token;
4570                    redo B;                    redo B;
4571                  } else {                  } else {
4572                    !!!cp ('t205');                    !!!cp ('t205');
4573                    !!!insert-element ('tr');                    !!!insert-element ('tr',, $token);
4574                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
4575                  }                  }
4576                } else {                } else {
# Line 4271  sub _tree_construction_main ($) { Line 4578  sub _tree_construction_main ($) {
4578                }                }
4579    
4580                ## Clear back to table row context                ## Clear back to table row context
4581                while (not {                while (not ($self->{open_elements}->[-1]->[1]
4582                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
4583                  !!!cp ('t207');                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4584                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4585                }                }
4586                                
4587                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4588                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
4589    
4590                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
4591                                
4592                  !!!nack ('t207.1');
4593                !!!next-token;                !!!next-token;
4594                redo B;                redo B;
4595              } elsif ({              } elsif ({
# Line 4297  sub _tree_construction_main ($) { Line 4603  sub _tree_construction_main ($) {
4603                  my $i;                  my $i;
4604                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4605                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4606                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
4607                      !!!cp ('t208');                      !!!cp ('t208');
4608                      $i = $_;                      $i = $_;
4609                      last INSCOPE;                      last INSCOPE;
4610                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
4611                      !!!cp ('t209');                      !!!cp ('t209');
4612                      last INSCOPE;                      last INSCOPE;
4613                    }                    }
4614                  } # INSCOPE                  } # INSCOPE
4615                  unless (defined $i) {                  unless (defined $i) {
4616                   !!!cp ('t210');                    !!!cp ('t210');
4617  ## TODO: This type is wrong.  ## TODO: This type is wrong.
4618                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
4619                    ## Ignore the token                    ## Ignore the token
4620                      !!!nack ('t210.1');
4621                    !!!next-token;                    !!!next-token;
4622                    redo B;                    redo B;
4623                  }                  }
4624                                    
4625                  ## Clear back to table row context                  ## Clear back to table row context
4626                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
4627                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
4628                    !!!cp ('t211');                    !!!cp ('t211');
4629                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4630                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4631                  }                  }
4632                                    
# Line 4335  sub _tree_construction_main ($) { Line 4635  sub _tree_construction_main ($) {
4635                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
4636                    !!!cp ('t212');                    !!!cp ('t212');
4637                    ## reprocess                    ## reprocess
4638                      !!!ack-later;
4639                    redo B;                    redo B;
4640                  } else {                  } else {
4641                    !!!cp ('t213');                    !!!cp ('t213');
# Line 4347  sub _tree_construction_main ($) { Line 4648  sub _tree_construction_main ($) {
4648                  my $i;                  my $i;
4649                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4650                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4651                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
4652                      !!!cp ('t214');                      !!!cp ('t214');
4653                      $i = $_;                      $i = $_;
4654                      last INSCOPE;                      last INSCOPE;
4655                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4656                      !!!cp ('t215');                      !!!cp ('t215');
4657                      last INSCOPE;                      last INSCOPE;
4658                    }                    }
# Line 4363  sub _tree_construction_main ($) { Line 4660  sub _tree_construction_main ($) {
4660                  unless (defined $i) {                  unless (defined $i) {
4661                    !!!cp ('t216');                    !!!cp ('t216');
4662  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type ios wrong.
4663                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4664                    ## Ignore the token                    ## Ignore the token
4665                      !!!nack ('t216.1');
4666                    !!!next-token;                    !!!next-token;
4667                    redo B;                    redo B;
4668                  }                  }
4669    
4670                  ## Clear back to table body context                  ## Clear back to table body context
4671                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
4672                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
4673                    !!!cp ('t217');                    !!!cp ('t217');
4674                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4675                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4676                  }                  }
4677                                    
# Line 4395  sub _tree_construction_main ($) { Line 4691  sub _tree_construction_main ($) {
4691    
4692                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
4693                  ## Clear back to table context                  ## Clear back to table context
4694                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
4695                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
4696                    !!!cp ('t219');                    !!!cp ('t219');
4697                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4698                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4699                  }                  }
4700                                    
4701                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
4702                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
4703                  ## reprocess                  ## reprocess
4704                    !!!ack-later;
4705                  redo B;                  redo B;
4706                } elsif ({                } elsif ({
4707                          caption => 1,                          caption => 1,
# Line 4413  sub _tree_construction_main ($) { Line 4709  sub _tree_construction_main ($) {
4709                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
4710                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
4711                  ## Clear back to table context                  ## Clear back to table context
4712                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
4713                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
4714                    !!!cp ('t220');                    !!!cp ('t220');
4715                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4716                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4717                  }                  }
4718                                    
4719                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
4720                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
4721                                    
4722                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4723                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
4724                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
4725                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 4433  sub _tree_construction_main ($) { Line 4728  sub _tree_construction_main ($) {
4728                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
4729                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
4730                  !!!next-token;                  !!!next-token;
4731                    !!!nack ('t220.1');
4732                  redo B;                  redo B;
4733                } else {                } else {
4734                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
4735                }                }
4736              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4737                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
4738                                  value => $self->{open_elements}->[-1]->[0]
4739                                      ->manakai_local_name,
4740                                  token => $token);
4741    
4742                ## As if </table>                ## As if </table>
4743                ## have a table element in table scope                ## have a table element in table scope
4744                my $i;                my $i;
4745                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4746                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4747                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
4748                    !!!cp ('t221');                    !!!cp ('t221');
4749                    $i = $_;                    $i = $_;
4750                    last INSCOPE;                    last INSCOPE;
4751                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
4752                    !!!cp ('t222');                    !!!cp ('t222');
4753                    last INSCOPE;                    last INSCOPE;
4754                  }                  }
# Line 4460  sub _tree_construction_main ($) { Line 4756  sub _tree_construction_main ($) {
4756                unless (defined $i) {                unless (defined $i) {
4757                  !!!cp ('t223');                  !!!cp ('t223');
4758  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
4759                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:table', token => $token);
4760                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
4761                    !!!nack ('t223.1');
4762                  !!!next-token;                  !!!next-token;
4763                  redo B;                  redo B;
4764                }                }
4765                                
4766    ## TODO: Followings are removed from the latest spec.
4767                ## generate implied end tags                ## generate implied end tags
4768                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
4769                  !!!cp ('t224');                  !!!cp ('t224');
4770                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4771                }                }
4772    
4773                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
4774                  !!!cp ('t225');                  !!!cp ('t225');
4775  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
4776                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
4777                                    value => $self->{open_elements}->[-1]->[0]
4778                                        ->manakai_local_name,
4779                                    token => $token);
4780                } else {                } else {
4781                  !!!cp ('t226');                  !!!cp ('t226');
4782                }                }
4783    
4784                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4785                  pop @{$open_tables};
4786    
4787                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4788    
4789                ## reprocess            ## reprocess
4790                redo B;            !!!ack-later;
4791              redo B;
4792            } elsif ($token->{tag_name} eq 'style') {
4793              if (not $open_tables->[-1]->[1]) { # tainted
4794                !!!cp ('t227.8');
4795                ## NOTE: This is a "as if in head" code clone.
4796                $parse_rcdata->(CDATA_CONTENT_MODEL);
4797                redo B;
4798              } else {
4799                !!!cp ('t227.7');
4800                #
4801              }
4802            } elsif ($token->{tag_name} eq 'script') {
4803              if (not $open_tables->[-1]->[1]) { # tainted
4804                !!!cp ('t227.6');
4805                ## NOTE: This is a "as if in head" code clone.
4806                $script_start_tag->();
4807                redo B;
4808              } else {
4809                !!!cp ('t227.5');
4810                #
4811              }
4812            } elsif ($token->{tag_name} eq 'input') {
4813              if (not $open_tables->[-1]->[1]) { # tainted
4814                if ($token->{attributes}->{type}) { ## TODO: case
4815                  my $type = lc $token->{attributes}->{type}->{value};
4816                  if ($type eq 'hidden') {
4817                    !!!cp ('t227.3');
4818                    !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
4819    
4820                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4821    
4822                    ## TODO: form element pointer
4823    
4824                    pop @{$self->{open_elements}};
4825    
4826                    !!!next-token;
4827                    !!!ack ('t227.2.1');
4828                    redo B;
4829                  } else {
4830                    !!!cp ('t227.2');
4831                    #
4832                  }
4833                } else {
4834                  !!!cp ('t227.1');
4835                  #
4836                }
4837              } else {
4838                !!!cp ('t227.4');
4839                #
4840              }
4841          } else {          } else {
4842            !!!cp ('t227');            !!!cp ('t227');
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
   
           $insert = $insert_to_foster;  
4843            #            #
4844          }          }
4845    
4846            !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
4847    
4848            $insert = $insert_to_foster;
4849            #
4850        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
4851              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
4852                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 4502  sub _tree_construction_main ($) { Line 4854  sub _tree_construction_main ($) {
4854                my $i;                my $i;
4855                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4856                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4857                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
4858                    !!!cp ('t228');                    !!!cp ('t228');
4859                    $i = $_;                    $i = $_;
4860                    last INSCOPE;                    last INSCOPE;
4861                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
4862                    !!!cp ('t229');                    !!!cp ('t229');
4863                    last INSCOPE;                    last INSCOPE;
4864                  }                  }
4865                } # INSCOPE                } # INSCOPE
4866                unless (defined $i) {                unless (defined $i) {
4867                  !!!cp ('t230');                  !!!cp ('t230');
4868                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4869                  ## Ignore the token                  ## Ignore the token
4870                    !!!nack ('t230.1');
4871                  !!!next-token;                  !!!next-token;
4872                  redo B;                  redo B;
4873                } else {                } else {
# Line 4524  sub _tree_construction_main ($) { Line 4875  sub _tree_construction_main ($) {
4875                }                }
4876    
4877                ## Clear back to table row context                ## Clear back to table row context
4878                while (not {                while (not ($self->{open_elements}->[-1]->[1]
4879                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
4880                  !!!cp ('t231');                  !!!cp ('t231');
4881  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4882                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4883                }                }
4884    
4885                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
4886                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
4887                !!!next-token;                !!!next-token;
4888                  !!!nack ('t231.1');
4889                redo B;                redo B;
4890              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4891                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
# Line 4544  sub _tree_construction_main ($) { Line 4894  sub _tree_construction_main ($) {
4894                  my $i;                  my $i;
4895                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4896                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4897                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
4898                      !!!cp ('t233');                      !!!cp ('t233');
4899                      $i = $_;                      $i = $_;
4900                      last INSCOPE;                      last INSCOPE;
4901                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4902                      !!!cp ('t234');                      !!!cp ('t234');
4903                      last INSCOPE;                      last INSCOPE;
4904                    }                    }
# Line 4558  sub _tree_construction_main ($) { Line 4906  sub _tree_construction_main ($) {
4906                  unless (defined $i) {                  unless (defined $i) {
4907                    !!!cp ('t235');                    !!!cp ('t235');
4908  ## TODO: The following is wrong.  ## TODO: The following is wrong.
4909                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
4910                    ## Ignore the token                    ## Ignore the token
4911                      !!!nack ('t236.1');
4912                    !!!next-token;                    !!!next-token;
4913                    redo B;                    redo B;
4914                  }                  }
4915                                    
4916                  ## Clear back to table row context                  ## Clear back to table row context
4917                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
4918                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
4919                    !!!cp ('t236');                    !!!cp ('t236');
4920  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4921                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4922                  }                  }
4923                                    
# Line 4584  sub _tree_construction_main ($) { Line 4931  sub _tree_construction_main ($) {
4931                  my $i;                  my $i;
4932                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4933                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4934                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
4935                      !!!cp ('t237');                      !!!cp ('t237');
4936                      $i = $_;                      $i = $_;
4937                      last INSCOPE;                      last INSCOPE;
4938                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
4939                      !!!cp ('t238');                      !!!cp ('t238');
4940                      last INSCOPE;                      last INSCOPE;
4941                    }                    }
4942                  } # INSCOPE                  } # INSCOPE
4943                  unless (defined $i) {                  unless (defined $i) {
4944                    !!!cp ('t239');                    !!!cp ('t239');
4945                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4946                    ## Ignore the token                    ## Ignore the token
4947                      !!!nack ('t239.1');
4948                    !!!next-token;                    !!!next-token;
4949                    redo B;                    redo B;
4950                  }                  }
4951                                    
4952                  ## Clear back to table body context                  ## Clear back to table body context
4953                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
4954                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
4955                    !!!cp ('t240');                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4956                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4957                  }                  }
4958                                    
# Line 4626  sub _tree_construction_main ($) { Line 4968  sub _tree_construction_main ($) {
4968                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
4969                }                }
4970    
4971                  ## NOTE: </table> in the "in table" insertion mode.
4972                  ## When you edit the code fragment below, please ensure that
4973                  ## the code for <table> in the "in table" insertion mode
4974                  ## is synced with it.
4975    
4976                ## have a table element in table scope                ## have a table element in table scope
4977                my $i;                my $i;
4978                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4979                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4980                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
4981                    !!!cp ('t241');                    !!!cp ('t241');
4982                    $i = $_;                    $i = $_;
4983                    last INSCOPE;                    last INSCOPE;
4984                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
4985                    !!!cp ('t242');                    !!!cp ('t242');
4986                    last INSCOPE;                    last INSCOPE;
4987                  }                  }
4988                } # INSCOPE                } # INSCOPE
4989                unless (defined $i) {                unless (defined $i) {
4990                  !!!cp ('t243');                  !!!cp ('t243');
4991                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4992                  ## Ignore the token                  ## Ignore the token
4993                    !!!nack ('t243.1');
4994                  !!!next-token;                  !!!next-token;
4995                  redo B;                  redo B;
4996                }                }
   
               ## generate implied end tags  
               while ({  
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!cp ('t244');  
 ## ISSUE: Can this case be reached?  
                 pop @{$self->{open_elements}};  
               }  
                 
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!cp ('t245');  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               } else {  
                 !!!cp ('t246');  
               }  
4997                                    
4998                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4999                  pop @{$open_tables};
5000                                
5001                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5002                                
# Line 4680  sub _tree_construction_main ($) { Line 5011  sub _tree_construction_main ($) {
5011                  my $i;                  my $i;
5012                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5013                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5014                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5015                      !!!cp ('t247');                      !!!cp ('t247');
5016                      $i = $_;                      $i = $_;
5017                      last INSCOPE;                      last INSCOPE;
5018                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5019                      !!!cp ('t248');                      !!!cp ('t248');
5020                      last INSCOPE;                      last INSCOPE;
5021                    }                    }
5022                  } # INSCOPE                  } # INSCOPE
5023                    unless (defined $i) {                    unless (defined $i) {
5024                      !!!cp ('t249');                      !!!cp ('t249');
5025                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5026                      ## Ignore the token                      ## Ignore the token
5027                        !!!nack ('t249.1');
5028                      !!!next-token;                      !!!next-token;
5029                      redo B;                      redo B;
5030                    }                    }
# Line 4704  sub _tree_construction_main ($) { Line 5034  sub _tree_construction_main ($) {
5034                  my $i;                  my $i;
5035                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5036                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5037                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5038                      !!!cp ('t250');                      !!!cp ('t250');
5039                      $i = $_;                      $i = $_;
5040                      last INSCOPE;                      last INSCOPE;
5041                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5042                      !!!cp ('t251');                      !!!cp ('t251');
5043                      last INSCOPE;                      last INSCOPE;
5044                    }                    }
5045                  } # INSCOPE                  } # INSCOPE
5046                    unless (defined $i) {                    unless (defined $i) {
5047                      !!!cp ('t252');                      !!!cp ('t252');
5048                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5049                      ## Ignore the token                      ## Ignore the token
5050                        !!!nack ('t252.1');
5051                      !!!next-token;                      !!!next-token;
5052                      redo B;                      redo B;
5053                    }                    }
5054                                    
5055                  ## Clear back to table row context                  ## Clear back to table row context
5056                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5057                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5058                    !!!cp ('t253');                    !!!cp ('t253');
5059  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5060                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5061                  }                  }
5062                                    
# Line 4742  sub _tree_construction_main ($) { Line 5069  sub _tree_construction_main ($) {
5069                my $i;                my $i;
5070                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5071                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5072                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5073                    !!!cp ('t254');                    !!!cp ('t254');
5074                    $i = $_;                    $i = $_;
5075                    last INSCOPE;                    last INSCOPE;
5076                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5077                    !!!cp ('t255');                    !!!cp ('t255');
5078                    last INSCOPE;                    last INSCOPE;
5079                  }                  }
5080                } # INSCOPE                } # INSCOPE
5081                unless (defined $i) {                unless (defined $i) {
5082                  !!!cp ('t256');                  !!!cp ('t256');
5083                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5084                  ## Ignore the token                  ## Ignore the token
5085                    !!!nack ('t256.1');
5086                  !!!next-token;                  !!!next-token;
5087                  redo B;                  redo B;
5088                }                }
5089    
5090                ## Clear back to table body context                ## Clear back to table body context
5091                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5092                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5093                  !!!cp ('t257');                  !!!cp ('t257');
5094  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5095                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5096                }                }
5097    
5098                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5099                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5100                  !!!nack ('t257.1');
5101                !!!next-token;                !!!next-token;
5102                redo B;                redo B;
5103              } elsif ({              } elsif ({
# Line 4781  sub _tree_construction_main ($) { Line 5106  sub _tree_construction_main ($) {
5106                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5107                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5108                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5109                !!!cp ('t258');            !!!cp ('t258');
5110                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5111                ## Ignore the token            ## Ignore the token
5112                !!!next-token;            !!!nack ('t258.1');
5113                redo B;             !!!next-token;
5114              redo B;
5115          } else {          } else {
5116            !!!cp ('t259');            !!!cp ('t259');
5117            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
5118    
5119            $insert = $insert_to_foster;            $insert = $insert_to_foster;
5120            #            #
5121          }          }
5122          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5123            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5124                    @{$self->{open_elements}} == 1) { # redundant, maybe
5125              !!!parse-error (type => 'in body:#eof', token => $token);
5126              !!!cp ('t259.1');
5127              #
5128            } else {
5129              !!!cp ('t259.2');
5130              #
5131            }
5132    
5133            ## Stop parsing
5134            last B;
5135        } else {        } else {
5136          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5137        }        }
# Line 4812  sub _tree_construction_main ($) { Line 5151  sub _tree_construction_main ($) {
5151            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5152              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
5153                !!!cp ('t262');                !!!cp ('t262');
5154                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5155                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5156                  !!!ack ('t262.1');
5157                !!!next-token;                !!!next-token;
5158                redo B;                redo B;
5159              } else {              } else {
# Line 4822  sub _tree_construction_main ($) { Line 5162  sub _tree_construction_main ($) {
5162              }              }
5163            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5164              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5165                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5166                  !!!cp ('t264');                  !!!cp ('t264');
5167                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5168                  ## Ignore the token                  ## Ignore the token
5169                  !!!next-token;                  !!!next-token;
5170                  redo B;                  redo B;
# Line 4837  sub _tree_construction_main ($) { Line 5177  sub _tree_construction_main ($) {
5177                }                }
5178              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5179                !!!cp ('t266');                !!!cp ('t266');
5180                !!!parse-error (type => 'unmatched end tag:col');                !!!parse-error (type => 'unmatched end tag:col', token => $token);
5181                ## Ignore the token                ## Ignore the token
5182                !!!next-token;                !!!next-token;
5183                redo B;                redo B;
# Line 4845  sub _tree_construction_main ($) { Line 5185  sub _tree_construction_main ($) {
5185                !!!cp ('t267');                !!!cp ('t267');
5186                #                #
5187              }              }
5188            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5189              die "$0: $token->{type}: Unknown token type";          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5190            }              @{$self->{open_elements}} == 1) { # redundant, maybe
5191              !!!cp ('t270.2');
5192              ## Stop parsing.
5193              last B;
5194            } else {
5195              ## NOTE: As if </colgroup>.
5196              !!!cp ('t270.1');
5197              pop @{$self->{open_elements}}; # colgroup
5198              $self->{insertion_mode} = IN_TABLE_IM;
5199              ## Reprocess.
5200              redo B;
5201            }
5202          } else {
5203            die "$0: $token->{type}: Unknown token type";
5204          }
5205    
5206            ## As if </colgroup>            ## As if </colgroup>
5207            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5208              !!!cp ('t269');              !!!cp ('t269');
5209              !!!parse-error (type => 'unmatched end tag:colgroup');  ## TODO: Wrong error type?
5210                !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5211              ## Ignore the token              ## Ignore the token
5212                !!!nack ('t269.1');
5213              !!!next-token;              !!!next-token;
5214              redo B;              redo B;
5215            } else {            } else {
5216              !!!cp ('t270');              !!!cp ('t270');
5217              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5218              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5219                !!!ack-later;
5220              ## reprocess              ## reprocess
5221              redo B;              redo B;
5222            }            }
5223      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5224        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5225          !!!cp ('t271');          !!!cp ('t271');
5226          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5227          !!!next-token;          !!!next-token;
5228          redo B;          redo B;
5229        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5230              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5231                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5232                  !!!cp ('t272');              !!!cp ('t272');
5233                  ## As if </option>              ## As if </option>
5234                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5235                } else {            } else {
5236                  !!!cp ('t273');              !!!cp ('t273');
5237                }            }
5238    
5239                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5240                !!!next-token;            !!!nack ('t273.1');
5241                redo B;            !!!next-token;
5242              } elsif ($token->{tag_name} eq 'optgroup') {            redo B;
5243                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5244                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5245                  ## As if </option>              !!!cp ('t274');
5246                  pop @{$self->{open_elements}};              ## As if </option>
5247                } else {              pop @{$self->{open_elements}};
5248                  !!!cp ('t275');            } else {
5249                }              !!!cp ('t275');
5250              }
5251    
5252                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5253                  !!!cp ('t276');              !!!cp ('t276');
5254                  ## As if </optgroup>              ## As if </optgroup>
5255                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5256                } else {            } else {
5257                  !!!cp ('t277');              !!!cp ('t277');
5258                }            }
5259    
5260                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5261                !!!next-token;            !!!nack ('t277.1');
5262                redo B;            !!!next-token;
5263              } elsif ($token->{tag_name} eq 'select') {            redo B;
5264  ## TODO: The type below is not good - <select> is replaced by </select>          } elsif ($token->{tag_name} eq 'select' or
5265                !!!parse-error (type => 'not closed:select');                   $token->{tag_name} eq 'input' or
5266                ## As if </select> instead                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5267                ## have an element in table scope                    {
5268                my $i;                     caption => 1, table => 1,
5269                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                     tbody => 1, tfoot => 1, thead => 1,
5270                  my $node = $self->{open_elements}->[$_];                     tr => 1, td => 1, th => 1,
5271                  if ($node->[1] eq $token->{tag_name}) {                    }->{$token->{tag_name}})) {
5272                    !!!cp ('t278');            ## TODO: The type below is not good - <select> is replaced by </select>
5273                    $i = $_;            !!!parse-error (type => 'not closed:select', token => $token);
5274                    last INSCOPE;            ## NOTE: As if the token were </select> (<select> case) or
5275                  } elsif ({            ## as if there were </select> (otherwise).
5276                            table => 1, html => 1,            ## have an element in table scope
5277                           }->{$node->[1]}) {            my $i;
5278                    !!!cp ('t279');            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5279                    last INSCOPE;              my $node = $self->{open_elements}->[$_];
5280                  }              if ($node->[1] & SELECT_EL) {
5281                } # INSCOPE                !!!cp ('t278');
5282                unless (defined $i) {                $i = $_;
5283                  !!!cp ('t280');                last INSCOPE;
5284                  !!!parse-error (type => 'unmatched end tag:select');              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5285                  ## Ignore the token                !!!cp ('t279');
5286                  !!!next-token;                last INSCOPE;
5287                  redo B;              }
5288                }            } # INSCOPE
5289              unless (defined $i) {
5290                !!!cp ('t280');
5291                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5292                ## Ignore the token
5293                !!!nack ('t280.1');
5294                !!!next-token;
5295                redo B;
5296              }
5297                                
5298                !!!cp ('t281');            !!!cp ('t281');
5299                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5300    
5301                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5302    
5303                !!!next-token;            if ($token->{tag_name} eq 'select') {
5304                redo B;              !!!nack ('t281.2');
5305                !!!next-token;
5306                redo B;
5307              } else {
5308                !!!cp ('t281.1');
5309                !!!ack-later;
5310                ## Reprocess the token.
5311                redo B;
5312              }
5313          } else {          } else {
5314            !!!cp ('t282');            !!!cp ('t282');
5315            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5316            ## Ignore the token            ## Ignore the token
5317              !!!nack ('t282.1');
5318            !!!next-token;            !!!next-token;
5319            redo B;            redo B;
5320          }          }
5321        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5322              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
5323                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5324                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5325                  !!!cp ('t283');              !!!cp ('t283');
5326                  ## As if </option>              ## As if </option>
5327                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
5328                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5329                  !!!cp ('t284');              !!!cp ('t284');
5330                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5331                } else {            } else {
5332                  !!!cp ('t285');              !!!cp ('t285');
5333                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5334                  ## Ignore the token              ## Ignore the token
5335                }            }
5336                !!!next-token;            !!!nack ('t285.1');
5337                redo B;            !!!next-token;
5338              } elsif ($token->{tag_name} eq 'option') {            redo B;
5339                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'option') {
5340                  !!!cp ('t286');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5341                  pop @{$self->{open_elements}};              !!!cp ('t286');
5342                } else {              pop @{$self->{open_elements}};
5343                  !!!cp ('t287');            } else {
5344                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t287');
5345                  ## Ignore the token              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5346                }              ## Ignore the token
5347                !!!next-token;            }
5348                redo B;            !!!nack ('t287.1');
5349              } elsif ($token->{tag_name} eq 'select') {            !!!next-token;
5350                ## have an element in table scope            redo B;
5351                my $i;          } elsif ($token->{tag_name} eq 'select') {
5352                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            ## have an element in table scope
5353                  my $node = $self->{open_elements}->[$_];            my $i;
5354                  if ($node->[1] eq $token->{tag_name}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5355                    !!!cp ('t288');              my $node = $self->{open_elements}->[$_];
5356                    $i = $_;              if ($node->[1] & SELECT_EL) {
5357                    last INSCOPE;                !!!cp ('t288');
5358                  } elsif ({                $i = $_;
5359                            table => 1, html => 1,                last INSCOPE;
5360                           }->{$node->[1]}) {              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5361                    !!!cp ('t289');                !!!cp ('t289');
5362                    last INSCOPE;                last INSCOPE;
5363                  }              }
5364                } # INSCOPE            } # INSCOPE
5365                unless (defined $i) {            unless (defined $i) {
5366                  !!!cp ('t290');              !!!cp ('t290');
5367                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5368                  ## Ignore the token              ## Ignore the token
5369                  !!!next-token;              !!!nack ('t290.1');
5370                  redo B;              !!!next-token;
5371                }              redo B;
5372              }
5373                                
5374                !!!cp ('t291');            !!!cp ('t291');
5375                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5376    
5377                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5378    
5379                !!!next-token;            !!!nack ('t291.1');
5380                redo B;            !!!next-token;
5381              } elsif ({            redo B;
5382                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5383                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
5384                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
5385                      tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5386                     }->{$token->{tag_name}}) {
5387  ## TODO: The following is wrong?  ## TODO: The following is wrong?
5388                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5389                                
5390                ## have an element in table scope            ## have an element in table scope
5391                my $i;            my $i;
5392                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5393                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5394                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5395                    !!!cp ('t292');                !!!cp ('t292');
5396                    $i = $_;                $i = $_;
5397                    last INSCOPE;                last INSCOPE;
5398                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5399                            table => 1, html => 1,                !!!cp ('t293');
5400                           }->{$node->[1]}) {                last INSCOPE;
5401                    !!!cp ('t293');              }
5402                    last INSCOPE;            } # INSCOPE
5403                  }            unless (defined $i) {
5404                } # INSCOPE              !!!cp ('t294');
5405                unless (defined $i) {              ## Ignore the token
5406                  !!!cp ('t294');              !!!nack ('t294.1');
5407                  ## Ignore the token              !!!next-token;
5408                  !!!next-token;              redo B;
5409                  redo B;            }
               }  
5410                                
5411                ## As if </select>            ## As if </select>
5412                ## have an element in table scope            ## have an element in table scope
5413                undef $i;            undef $i;
5414                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5415                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5416                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
5417                    !!!cp ('t295');                !!!cp ('t295');
5418                    $i = $_;                $i = $_;
5419                    last INSCOPE;                last INSCOPE;
5420                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5421  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5422                    !!!cp ('t296');                !!!cp ('t296');
5423                    last INSCOPE;                last INSCOPE;
5424                  }              }
5425                } # INSCOPE            } # INSCOPE
5426                unless (defined $i) {            unless (defined $i) {
5427                  !!!cp ('t297');              !!!cp ('t297');
5428  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
5429                  !!!parse-error (type => 'unmatched end tag:select');              !!!parse-error (type => 'unmatched end tag:select', token => $token);
5430                  ## Ignore the </select> token              ## Ignore the </select> token
5431                  !!!next-token; ## TODO: ok?              !!!nack ('t297.1');
5432                  redo B;              !!!next-token; ## TODO: ok?
5433                }              redo B;
5434              }
5435                                
5436                !!!cp ('t298');            !!!cp ('t298');
5437                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
5438    
5439                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5440    
5441                ## reprocess            !!!ack-later;
5442                redo B;            ## reprocess
5443              redo B;
5444          } else {          } else {
5445            !!!cp ('t299');            !!!cp ('t299');
5446            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
5447            ## Ignore the token            ## Ignore the token
5448              !!!nack ('t299.3');
5449            !!!next-token;            !!!next-token;
5450            redo B;            redo B;
5451          }          }
5452          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5453            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5454                    @{$self->{open_elements}} == 1) { # redundant, maybe
5455              !!!cp ('t299.1');
5456              !!!parse-error (type => 'in body:#eof', token => $token);
5457            } else {
5458              !!!cp ('t299.2');
5459            }
5460    
5461            ## Stop parsing.
5462            last B;
5463        } else {        } else {
5464          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5465        }        }
# Line 5092  sub _tree_construction_main ($) { Line 5481  sub _tree_construction_main ($) {
5481                    
5482          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5483            !!!cp ('t301');            !!!cp ('t301');
5484            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#character', token => $token);
5485    
5486            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
5487          } else {          } else {
# Line 5100  sub _tree_construction_main ($) { Line 5489  sub _tree_construction_main ($) {
5489          }          }
5490                    
5491          ## "after body" insertion mode          ## "after body" insertion mode
5492          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#character', token => $token);
5493    
5494          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
5495          ## reprocess          ## reprocess
# Line 5108  sub _tree_construction_main ($) { Line 5497  sub _tree_construction_main ($) {
5497        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5498          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5499            !!!cp ('t303');            !!!cp ('t303');
5500            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5501                        
5502            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
5503          } else {          } else {
# Line 5116  sub _tree_construction_main ($) { Line 5505  sub _tree_construction_main ($) {
5505          }          }
5506    
5507          ## "after body" insertion mode          ## "after body" insertion mode
5508          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
5509    
5510          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
5511            !!!ack-later;
5512          ## reprocess          ## reprocess
5513          redo B;          redo B;
5514        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5515          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5516            !!!cp ('t305');            !!!cp ('t305');
5517            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
5518                        
5519            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
5520            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
# Line 5136  sub _tree_construction_main ($) { Line 5526  sub _tree_construction_main ($) {
5526          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
5527            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
5528              !!!cp ('t307');              !!!cp ('t307');
5529              !!!parse-error (type => 'unmatched end tag:html');              !!!parse-error (type => 'unmatched end tag:html', token => $token);
5530              ## Ignore the token              ## Ignore the token
5531              !!!next-token;              !!!next-token;
5532              redo B;              redo B;
# Line 5148  sub _tree_construction_main ($) { Line 5538  sub _tree_construction_main ($) {
5538            }            }
5539          } else {          } else {
5540            !!!cp ('t309');            !!!cp ('t309');
5541            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
5542    
5543            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
5544            ## reprocess            ## reprocess
5545            redo B;            redo B;
5546          }          }
5547          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5548            !!!cp ('t309.2');
5549            ## Stop parsing
5550            last B;
5551        } else {        } else {
5552          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5553        }        }
# Line 5172  sub _tree_construction_main ($) { Line 5566  sub _tree_construction_main ($) {
5566          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
5567            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5568              !!!cp ('t311');              !!!cp ('t311');
5569              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#character', token => $token);
5570            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
5571              !!!cp ('t312');              !!!cp ('t312');
5572              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
5573            } else { # "after html frameset"            } else { # "after html frameset"
5574              !!!cp ('t313');              !!!cp ('t313');
5575              !!!parse-error (type => 'after html:#character');              !!!parse-error (type => 'after html:#character', token => $token);
5576    
5577              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5578              ## Reprocess in the "after frameset" insertion mode.              ## Reprocess in the "after frameset" insertion mode.
5579              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
5580            }            }
5581                        
5582            ## Ignore the token.            ## Ignore the token.
# Line 5200  sub _tree_construction_main ($) { Line 5594  sub _tree_construction_main ($) {
5594        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5595          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5596            !!!cp ('t316');            !!!cp ('t316');
5597            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
5598    
5599            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
5600            ## Process in the "after frameset" insertion mode.            ## Process in the "after frameset" insertion mode.
# Line 5211  sub _tree_construction_main ($) { Line 5605  sub _tree_construction_main ($) {
5605          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
5606              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
5607            !!!cp ('t318');            !!!cp ('t318');
5608            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5609              !!!nack ('t318.1');
5610            !!!next-token;            !!!next-token;
5611            redo B;            redo B;
5612          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
5613                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
5614            !!!cp ('t319');            !!!cp ('t319');
5615            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5616            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
5617              !!!ack ('t319.1');
5618            !!!next-token;            !!!next-token;
5619            redo B;            redo B;
5620          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
5621            !!!cp ('t320');            !!!cp ('t320');
5622            ## NOTE: As if in body.            ## NOTE: As if in body.
5623            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL);
5624            redo B;            redo B;
5625          } else {          } else {
5626            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5627              !!!cp ('t321');              !!!cp ('t321');
5628              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
5629            } else {            } else {
5630              !!!cp ('t322');              !!!cp ('t322');
5631              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
5632            }            }
5633            ## Ignore the token            ## Ignore the token
5634              !!!nack ('t322.1');
5635            !!!next-token;            !!!next-token;
5636            redo B;            redo B;
5637          }          }
5638        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5639          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5640            !!!cp ('t323');            !!!cp ('t323');
5641            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
5642    
5643            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
5644            ## Process in the "after frameset" insertion mode.            ## Process in the "after frameset" insertion mode.
# Line 5251  sub _tree_construction_main ($) { Line 5648  sub _tree_construction_main ($) {
5648    
5649          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
5650              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
5651            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5652                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
5653              !!!cp ('t325');              !!!cp ('t325');
5654              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5655              ## Ignore the token              ## Ignore the token
5656              !!!next-token;              !!!next-token;
5657            } else {            } else {
# Line 5264  sub _tree_construction_main ($) { Line 5661  sub _tree_construction_main ($) {
5661            }            }
5662    
5663            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
5664                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
5665              !!!cp ('t327');              !!!cp ('t327');
5666              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5667            } else {            } else {
# Line 5280  sub _tree_construction_main ($) { Line 5677  sub _tree_construction_main ($) {
5677          } else {          } else {
5678            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5679              !!!cp ('t330');              !!!cp ('t330');
5680              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
5681            } else {            } else {
5682              !!!cp ('t331');              !!!cp ('t331');
5683              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
5684            }            }
5685            ## Ignore the token            ## Ignore the token
5686            !!!next-token;            !!!next-token;
5687            redo B;            redo B;
5688          }          }
5689          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5690            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5691                    @{$self->{open_elements}} == 1) { # redundant, maybe
5692              !!!cp ('t331.1');
5693              !!!parse-error (type => 'in body:#eof', token => $token);
5694            } else {
5695              !!!cp ('t331.2');
5696            }
5697            
5698            ## Stop parsing
5699            last B;
5700        } else {        } else {
5701          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5702        }        }
# Line 5303  sub _tree_construction_main ($) { Line 5711  sub _tree_construction_main ($) {
5711        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
5712          !!!cp ('t332');          !!!cp ('t332');
5713          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5714          $script_start_tag->($insert);          $script_start_tag->();
5715          redo B;          redo B;
5716        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
5717          !!!cp ('t333');          !!!cp ('t333');
5718          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5719          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
5720          redo B;          redo B;
5721        } elsif ({        } elsif ({
5722                  base => 1, link => 1,                  base => 1, link => 1,
5723                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5724          !!!cp ('t334');          !!!cp ('t334');
5725          ## 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
5726          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5727          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
5728            !!!ack ('t334.1');
5729          !!!next-token;          !!!next-token;
5730          redo B;          redo B;
5731        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
5732          ## 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
5733          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5734          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.
5735    
5736          unless ($self->{confident}) {          unless ($self->{confident}) {
5737            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) { ## TODO: And if supported
5738              !!!cp ('t335');              !!!cp ('t335');
5739              $self->{change_encoding}              $self->{change_encoding}
5740                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
5741                            
5742              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5743                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
# Line 5343  sub _tree_construction_main ($) { Line 5752  sub _tree_construction_main ($) {
5752                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
5753                !!!cp ('t336');                !!!cp ('t336');
5754                $self->{change_encoding}                $self->{change_encoding}
5755                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
5756                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5757                    ->set_user_data (manakai_has_reference =>                    ->set_user_data (manakai_has_reference =>
5758                                         $token->{attributes}->{content}                                         $token->{attributes}->{content}
# Line 5367  sub _tree_construction_main ($) { Line 5776  sub _tree_construction_main ($) {
5776            }            }
5777          }          }
5778    
5779            !!!ack ('t338.1');
5780          !!!next-token;          !!!next-token;
5781          redo B;          redo B;
5782        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
5783          !!!cp ('t341');          !!!cp ('t341');
         !!!parse-error (type => 'in body:title');  
5784          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5785          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
           if (defined $self->{head_element}) {  
             !!!cp ('t339');  
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             !!!cp ('t340');  
             $insert->($_[0]);  
           }  
         });  
5786          redo B;          redo B;
5787        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
5788          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body', token => $token);
5789                                
5790          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
5791              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
5792            !!!cp ('t342');            !!!cp ('t342');
5793            ## Ignore the token            ## Ignore the token
5794          } else {          } else {
# Line 5401  sub _tree_construction_main ($) { Line 5802  sub _tree_construction_main ($) {
5802              }              }
5803            }            }
5804          }          }
5805            !!!nack ('t343.1');
5806          !!!next-token;          !!!next-token;
5807          redo B;          redo B;
5808        } elsif ({        } elsif ({
5809                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
5810                  div => 1, dl => 1, fieldset => 1,                  div => 1, dl => 1, fieldset => 1,
5811                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5812                  listing => 1, menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
5813                  pre => 1,                  pre => 1, listing => 1,
5814                    form => 1,
5815                    table => 1,
5816                    hr => 1,
5817                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5818            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
5819              !!!cp ('t350');
5820              !!!parse-error (type => 'in form:form', token => $token);
5821              ## Ignore the token
5822              !!!nack ('t350.1');
5823              !!!next-token;
5824              redo B;
5825            }
5826    
5827          ## has a p element in scope          ## has a p element in scope
5828          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5829            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
5830              !!!cp ('t344');              !!!cp ('t344');
5831              !!!back-token;              !!!back-token; # <form>
5832              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
5833                          line => $token->{line}, column => $token->{column}};
5834              redo B;              redo B;
5835            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
5836              !!!cp ('t345');              !!!cp ('t345');
5837              last INSCOPE;              last INSCOPE;
5838            }            }
5839          } # INSCOPE          } # INSCOPE
5840                        
5841          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5842          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
5843              !!!nack ('t346.1');
5844            !!!next-token;            !!!next-token;
5845            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
5846              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5440  sub _tree_construction_main ($) { Line 5853  sub _tree_construction_main ($) {
5853            } else {            } else {
5854              !!!cp ('t348');              !!!cp ('t348');
5855            }            }
5856          } else {          } elsif ($token->{tag_name} eq 'form') {
5857            !!!cp ('t347');            !!!cp ('t347.1');
5858              $self->{form_element} = $self->{open_elements}->[-1]->[0];
5859    
5860              !!!nack ('t347.2');
5861            !!!next-token;            !!!next-token;
5862          }          } elsif ($token->{tag_name} eq 'table') {
5863          redo B;            !!!cp ('t382');
5864        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
5865          if (defined $self->{form_element}) {            
5866            !!!cp ('t350');            $self->{insertion_mode} = IN_TABLE_IM;
5867            !!!parse-error (type => 'in form:form');  
5868            ## Ignore the token            !!!nack ('t382.1');
5869              !!!next-token;
5870            } elsif ($token->{tag_name} eq 'hr') {
5871              !!!cp ('t386');
5872              pop @{$self->{open_elements}};
5873            
5874              !!!nack ('t386.1');
5875            !!!next-token;            !!!next-token;
           redo B;  
5876          } else {          } else {
5877            ## 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];  
5878            !!!next-token;            !!!next-token;
           redo B;  
5879          }          }
5880        } elsif ($token->{tag_name} eq 'li') {          redo B;
5881          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
5882          ## has a p element in scope          ## has a p element in scope
5883          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5884            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
5885              !!!cp ('t353');              !!!cp ('t353');
5886              !!!back-token;              !!!back-token; # <x>
5887              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
5888                          line => $token->{line}, column => $token->{column}};
5889              redo B;              redo B;
5890            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
5891              !!!cp ('t354');              !!!cp ('t354');
5892              last INSCOPE;              last INSCOPE;
5893            }            }
# Line 5494  sub _tree_construction_main ($) { Line 5896  sub _tree_construction_main ($) {
5896          ## Step 1          ## Step 1
5897          my $i = -1;          my $i = -1;
5898          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
5899            my $li_or_dtdd = {li => {li => 1},
5900                              dt => {dt => 1, dd => 1},
5901                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
5902          LI: {          LI: {
5903            ## Step 2            ## Step 2
5904            if ($node->[1] eq 'li') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
5905              if ($i != -1) {              if ($i != -1) {
5906                !!!cp ('t355');                !!!cp ('t355');
5907                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
5908                                $self->{open_elements}->[-1]->[1]);                                value => $self->{open_elements}->[-1]->[0]
5909                                      ->manakai_local_name,
5910                                  token => $token);
5911              } else {              } else {
5912                !!!cp ('t356');                !!!cp ('t356');
5913              }              }
# Line 5511  sub _tree_construction_main ($) { Line 5918  sub _tree_construction_main ($) {
5918            }            }
5919                        
5920            ## Step 3            ## Step 3
5921            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
5922                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
5923                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
5924                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
5925                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
5926                  not ($node->[1] & DIV_EL)) {
5927              !!!cp ('t358');              !!!cp ('t358');
5928              last LI;              last LI;
5929            }            }
# Line 5527  sub _tree_construction_main ($) { Line 5935  sub _tree_construction_main ($) {
5935            redo LI;            redo LI;
5936          } # LI          } # LI
5937                        
5938          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5939          !!!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});  
5940          !!!next-token;          !!!next-token;
5941          redo B;          redo B;
5942        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
5943          ## has a p element in scope          ## has a p element in scope
5944          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5945            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
5946              !!!cp ('t367');              !!!cp ('t367');
5947              !!!back-token;              !!!back-token; # <plaintext>
5948              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
5949                          line => $token->{line}, column => $token->{column}};
5950              redo B;              redo B;
5951            } elsif ({            } elsif ($_->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
5952              !!!cp ('t368');              !!!cp ('t368');
5953              last INSCOPE;              last INSCOPE;
5954            }            }
5955          } # INSCOPE          } # INSCOPE
5956                        
5957          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
5958                        
5959          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
5960                        
5961            !!!nack ('t368.1');
5962          !!!next-token;          !!!next-token;
5963          redo B;          redo B;
5964        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
5965          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
5966            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
5967            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
5968              !!!cp ('t371');              !!!cp ('t371');
5969              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a', token => $token);
5970                            
5971              !!!back-token;              !!!back-token; # <a>
5972              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
5973              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
5974                $formatting_end_tag->($token);
5975                            
5976              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
5977                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
# Line 5643  sub _tree_construction_main ($) { Line 5996  sub _tree_construction_main ($) {
5996                        
5997          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5998    
5999          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6000          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6001    
6002          !!!next-token;          !!!nack ('t374.1');
         redo B;  
       } elsif ({  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         !!!cp ('t375');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
6003          !!!next-token;          !!!next-token;
6004          redo B;          redo B;
6005        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
# Line 5667  sub _tree_construction_main ($) { Line 6008  sub _tree_construction_main ($) {
6008          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6009          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6010            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6011            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6012              !!!cp ('t376');              !!!cp ('t376');
6013              !!!parse-error (type => 'in nobr:nobr');              !!!parse-error (type => 'in nobr:nobr', token => $token);
6014              !!!back-token;              !!!back-token; # <nobr>
6015              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6016                          line => $token->{line}, column => $token->{column}};
6017              redo B;              redo B;
6018            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6019              !!!cp ('t377');              !!!cp ('t377');
6020              last INSCOPE;              last INSCOPE;
6021            }            }
6022          } # INSCOPE          } # INSCOPE
6023                    
6024          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6025          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6026                    
6027            !!!nack ('t377.1');
6028          !!!next-token;          !!!next-token;
6029          redo B;          redo B;
6030        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6031          ## has a button element in scope          ## has a button element in scope
6032          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6033            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6034            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6035              !!!cp ('t378');              !!!cp ('t378');
6036              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button', token => $token);
6037              !!!back-token;              !!!back-token; # <button>
6038              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6039                          line => $token->{line}, column => $token->{column}};
6040              redo B;              redo B;
6041            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6042              !!!cp ('t379');              !!!cp ('t379');
6043              last INSCOPE;              last INSCOPE;
6044            }            }
# Line 5708  sub _tree_construction_main ($) { Line 6046  sub _tree_construction_main ($) {
6046                        
6047          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6048                        
6049          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6050    
6051          ## TODO: associate with $self->{form_element} if defined          ## TODO: associate with $self->{form_element} if defined
6052    
6053          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6054    
6055          !!!next-token;          !!!nack ('t379.1');
         redo B;  
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         !!!cp ('t380');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
           
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         !!!cp ('t381');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t382');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t383');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = IN_TABLE_IM;  
             
6056          !!!next-token;          !!!next-token;
6057          redo B;          redo B;
6058        } elsif ({        } elsif ({
6059                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
6060                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
6061                  image => 1,                  noembed => 1,
6062                    noframes => 1,
6063                    noscript => 0, ## TODO: 1 if scripting is enabled
6064                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6065          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
6066            !!!cp ('t384');            !!!cp ('t381');
6067            !!!parse-error (type => 'image');            $reconstruct_active_formatting_elements->($insert_to_current);
           $token->{tag_name} = 'img';  
6068          } else {          } else {
6069            !!!cp ('t385');            !!!cp ('t399');
6070          }          }
6071            ## NOTE: There is an "as if in body" code clone.
6072          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t386');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t387');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'input') {  
         !!!cp ('t388');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         ## TODO: associate with $self->{form_element} if defined  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
6073          redo B;          redo B;
6074        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6075          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
6076                    
6077          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6078            !!!cp ('t389');            !!!cp ('t389');
6079            ## Ignore the token            ## Ignore the token
6080              !!!nack ('t389'); ## NOTE: Not acknowledged.
6081            !!!next-token;            !!!next-token;
6082            redo B;            redo B;
6083          } else {          } else {
# Line 5825  sub _tree_construction_main ($) { Line 6090  sub _tree_construction_main ($) {
6090            delete $at->{prompt};            delete $at->{prompt};
6091            my @tokens = (            my @tokens = (
6092                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
6093                           attributes => $form_attrs},                           attributes => $form_attrs,
6094                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
6095                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
6096                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
6097                            {type => START_TAG_TOKEN, tag_name => 'p',
6098                             line => $token->{line}, column => $token->{column}},
6099                            {type => START_TAG_TOKEN, tag_name => 'label',
6100                             line => $token->{line}, column => $token->{column}},
6101                         );                         );
6102            if ($prompt_attr) {            if ($prompt_attr) {
6103              !!!cp ('t390');              !!!cp ('t390');
6104              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6105                               #line => $token->{line}, column => $token->{column},
6106                              };
6107            } else {            } else {
6108              !!!cp ('t391');              !!!cp ('t391');
6109              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
6110                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
6111                               #line => $token->{line}, column => $token->{column},
6112                              }; # SHOULD
6113              ## TODO: make this configurable              ## TODO: make this configurable
6114            }            }
6115            push @tokens,            push @tokens,
6116                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6117                             line => $token->{line}, column => $token->{column}},
6118                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6119                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
6120                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
6121                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
6122                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
6123            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
6124                             line => $token->{line}, column => $token->{column}},
6125                            {type => END_TAG_TOKEN, tag_name => 'form',
6126                             line => $token->{line}, column => $token->{column}};
6127              !!!nack ('t391.1'); ## NOTE: Not acknowledged.
6128            !!!back-token (@tokens);            !!!back-token (@tokens);
6129              !!!next-token;
6130            redo B;            redo B;
6131          }          }
6132        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6133          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6134          my $el;          my $el;
6135          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $token->{tag_name}, $token->{attributes}, $token);
6136                    
6137          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6138          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5862  sub _tree_construction_main ($) { Line 6141  sub _tree_construction_main ($) {
6141          $insert->($el);          $insert->($el);
6142                    
6143          my $text = '';          my $text = '';
6144            !!!nack ('t392.1');
6145          !!!next-token;          !!!next-token;
6146          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6147            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 5892  sub _tree_construction_main ($) { Line 6172  sub _tree_construction_main ($) {
6172            ## Ignore the token            ## Ignore the token
6173          } else {          } else {
6174            !!!cp ('t398');            !!!cp ('t398');
6175            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6176          }          }
6177          !!!next-token;          !!!next-token;
6178          redo B;          redo B;
6179        } elsif ({        } elsif ({
                 iframe => 1,  
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!cp ('t399');  
         ## NOTE: There is an "as if in body" code clone.  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'select') {  
         !!!cp ('t400');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
   
         ## TODO: associate with $self->{form_element} if defined  
           
         $self->{insertion_mode} = IN_SELECT_IM;  
         !!!next-token;  
         redo B;  
       } elsif ({  
6180                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6181                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
6182                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
6183                  thead => 1, tr => 1,                  thead => 1, tr => 1,
6184                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6185          !!!cp ('t401');          !!!cp ('t401');
6186          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6187          ## Ignore the token          ## Ignore the token
6188            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6189          !!!next-token;          !!!next-token;
6190          redo B;          redo B;
6191                    
6192          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6193        } else {        } else {
6194          !!!cp ('t402');          if ($token->{tag_name} eq 'image') {
6195              !!!cp ('t384');
6196              !!!parse-error (type => 'image', token => $token);
6197              $token->{tag_name} = 'img';
6198            } else {
6199              !!!cp ('t385');
6200            }
6201    
6202            ## NOTE: There is an "as if <br>" code clone.
6203          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6204                    
6205          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6206    
6207            if ({
6208                 applet => 1, marquee => 1, object => 1,
6209                }->{$token->{tag_name}}) {
6210              !!!cp ('t380');
6211              push @$active_formatting_elements, ['#marker', ''];
6212              !!!nack ('t380.1');
6213            } elsif ({
6214                      b => 1, big => 1, em => 1, font => 1, i => 1,
6215                      s => 1, small => 1, strile => 1,
6216                      strong => 1, tt => 1, u => 1,
6217                     }->{$token->{tag_name}}) {
6218              !!!cp ('t375');
6219              push @$active_formatting_elements, $self->{open_elements}->[-1];
6220              !!!nack ('t375.1');
6221            } elsif ($token->{tag_name} eq 'input') {
6222              !!!cp ('t388');
6223              ## TODO: associate with $self->{form_element} if defined
6224              pop @{$self->{open_elements}};
6225              !!!ack ('t388.2');
6226            } elsif ({
6227                      area => 1, basefont => 1, bgsound => 1, br => 1,
6228                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6229                      #image => 1,
6230                     }->{$token->{tag_name}}) {
6231              !!!cp ('t388.1');
6232              pop @{$self->{open_elements}};
6233              !!!ack ('t388.3');
6234            } elsif ($token->{tag_name} eq 'select') {
6235              ## TODO: associate with $self->{form_element} if defined
6236            
6237              if ($self->{insertion_mode} & TABLE_IMS or
6238                  $self->{insertion_mode} & BODY_TABLE_IMS or
6239                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6240                !!!cp ('t400.1');
6241                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6242              } else {
6243                !!!cp ('t400.2');
6244                $self->{insertion_mode} = IN_SELECT_IM;
6245              }
6246              !!!nack ('t400.3');
6247            } else {
6248              !!!nack ('t402');
6249            }
6250                    
6251          !!!next-token;          !!!next-token;
6252          redo B;          redo B;
6253        }        }
6254      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6255        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
6256          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
6257              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
6258            for (@{$self->{open_elements}}) {          INSCOPE: {
6259              unless ({            for (reverse @{$self->{open_elements}}) {
6260                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
6261                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
6262                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
6263                      }->{$_->[1]}) {                last INSCOPE;
6264                !!!cp ('t403');              } elsif ($_->[1] & SCOPING_EL) {
6265                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!cp ('t405.1');
6266              } else {                last;
               !!!cp ('t404');  
6267              }              }
6268            }            }
6269    
6270            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
6271            !!!next-token;                            value => $token->{tag_name}, token => $token);
6272            redo B;            ## NOTE: Ignore the token.
         } else {  
           !!!cp ('t405');  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
6273            !!!next-token;            !!!next-token;
6274            redo B;            redo B;
6275            } # INSCOPE
6276    
6277            for (@{$self->{open_elements}}) {
6278              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
6279                !!!cp ('t403');
6280                !!!parse-error (type => 'not closed',
6281                                value => $_->[0]->manakai_local_name,
6282                                token => $token);
6283                last;
6284              } else {
6285                !!!cp ('t404');
6286              }
6287          }          }
6288    
6289            $self->{insertion_mode} = AFTER_BODY_IM;
6290            !!!next-token;
6291            redo B;
6292        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
6293          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
6294            ## up-to-date, though it has same effect as speced.
6295            if (@{$self->{open_elements}} > 1 and
6296                $self->{open_elements}->[1]->[1] & BODY_EL) {
6297            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6298            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6299              !!!cp ('t406');              !!!cp ('t406');
6300              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed',
6301                                value => $self->{open_elements}->[1]->[0]
6302                                    ->manakai_local_name,
6303                                token => $token);
6304            } else {            } else {
6305              !!!cp ('t407');              !!!cp ('t407');
6306            }            }
# Line 5980  sub _tree_construction_main ($) { Line 6309  sub _tree_construction_main ($) {
6309            redo B;            redo B;
6310          } else {          } else {
6311            !!!cp ('t408');            !!!cp ('t408');
6312            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6313            ## Ignore the token            ## Ignore the token
6314            !!!next-token;            !!!next-token;
6315            redo B;            redo B;
# Line 5990  sub _tree_construction_main ($) { Line 6319  sub _tree_construction_main ($) {
6319                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
6320                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
6321                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
6322                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
6323                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6324          ## has an element in scope          ## has an element in scope
6325          my $i;          my $i;
6326          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6327            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6328            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6329              !!!cp ('t410');              !!!cp ('t410');
6330              $i = $_;              $i = $_;
6331              last INSCOPE;              last INSCOPE;
6332            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6333              !!!cp ('t411');              !!!cp ('t411');
6334              last INSCOPE;              last INSCOPE;
6335            }            }
# Line 6011  sub _tree_construction_main ($) { Line 6337  sub _tree_construction_main ($) {
6337    
6338          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
6339            !!!cp ('t413');            !!!cp ('t413');
6340            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6341          } else {          } else {
6342            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
6343            while ({            while ({
# Line 6019  sub _tree_construction_main ($) { Line 6345  sub _tree_construction_main ($) {
6345                    dt => ($token->{tag_name} ne 'dt'),                    dt => ($token->{tag_name} ne 'dt'),
6346                    li => ($token->{tag_name} ne 'li'),                    li => ($token->{tag_name} ne 'li'),
6347                    p => 1,                    p => 1,
6348                   }->{$self->{open_elements}->[-1]->[1]}) {                   }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6349              !!!cp ('t409');              !!!cp ('t409');
6350              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6351            }            }
6352    
6353            ## Step 2.            ## Step 2.
6354            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6355                      ne $token->{tag_name}) {
6356              !!!cp ('t412');              !!!cp ('t412');
6357              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
6358                                value => $self->{open_elements}->[-1]->[0]
6359                                    ->manakai_local_name,
6360                                token => $token);
6361            } else {            } else {
6362              !!!cp ('t414');              !!!cp ('t414');
6363            }            }
# Line 6038  sub _tree_construction_main ($) { Line 6368  sub _tree_construction_main ($) {
6368            ## Step 4.            ## Step 4.
6369            $clear_up_to_marker->()            $clear_up_to_marker->()
6370                if {                if {
6371                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
6372                }->{$token->{tag_name}};                }->{$token->{tag_name}};
6373          }          }
6374          !!!next-token;          !!!next-token;
# Line 6050  sub _tree_construction_main ($) { Line 6380  sub _tree_construction_main ($) {
6380          my $i;          my $i;
6381          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6382            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6383            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
6384              !!!cp ('t418');              !!!cp ('t418');
6385              $i = $_;              $i = $_;
6386              last INSCOPE;              last INSCOPE;
6387            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6388              !!!cp ('t419');              !!!cp ('t419');
6389              last INSCOPE;              last INSCOPE;
6390            }            }
# Line 6065  sub _tree_construction_main ($) { Line 6392  sub _tree_construction_main ($) {
6392    
6393          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
6394            !!!cp ('t421');            !!!cp ('t421');
6395            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6396          } else {          } else {
6397            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
6398            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
6399              !!!cp ('t417');              !!!cp ('t417');
6400              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6401            }            }
6402                        
6403            ## Step 2.            ## Step 2.
6404            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6405                      ne $token->{tag_name}) {
6406              !!!cp ('t417.1');              !!!cp ('t417.1');
6407              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
6408                                value => $self->{open_elements}->[-1]->[0]
6409                                    ->manakai_local_name,
6410                                token => $token);
6411            } else {            } else {
6412              !!!cp ('t420');              !!!cp ('t420');
6413            }              }  
# Line 6096  sub _tree_construction_main ($) { Line 6425  sub _tree_construction_main ($) {
6425          my $i;          my $i;
6426          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6427            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6428            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
6429              !!!cp ('t423');              !!!cp ('t423');
6430              $i = $_;              $i = $_;
6431              last INSCOPE;              last INSCOPE;
6432            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6433              !!!cp ('t424');              !!!cp ('t424');
6434              last INSCOPE;              last INSCOPE;
6435            }            }
# Line 6113  sub _tree_construction_main ($) { Line 6437  sub _tree_construction_main ($) {
6437    
6438          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
6439            !!!cp ('t425.1');            !!!cp ('t425.1');
6440            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6441          } else {          } else {
6442            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
6443            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
6444              !!!cp ('t422');              !!!cp ('t422');
6445              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6446            }            }
6447                        
6448            ## Step 2.            ## Step 2.
6449            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6450                      ne $token->{tag_name}) {
6451              !!!cp ('t425');              !!!cp ('t425');
6452              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6453            } else {            } else {
6454              !!!cp ('t426');              !!!cp ('t426');
6455            }            }
# Line 6142  sub _tree_construction_main ($) { Line 6465  sub _tree_construction_main ($) {
6465          my $i;          my $i;
6466          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6467            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6468            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & P_EL) {
6469              !!!cp ('t410.1');              !!!cp ('t410.1');
6470              $i = $_;              $i = $_;
6471              last INSCOPE;              last INSCOPE;
6472            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6473              !!!cp ('t411.1');              !!!cp ('t411.1');
6474              last INSCOPE;              last INSCOPE;
6475            }            }
6476          } # INSCOPE          } # INSCOPE
6477    
6478          if (defined $i) {          if (defined $i) {
6479            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6480                      ne $token->{tag_name}) {
6481              !!!cp ('t412.1');              !!!cp ('t412.1');
6482              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
6483                                value => $self->{open_elements}->[-1]->[0]
6484                                    ->manakai_local_name,
6485                                token => $token);
6486            } else {            } else {
6487              !!!cp ('t414.1');              !!!cp ('t414.1');
6488            }            }
# Line 6166  sub _tree_construction_main ($) { Line 6490  sub _tree_construction_main ($) {
6490            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6491          } else {          } else {
6492            !!!cp ('t413.1');            !!!cp ('t413.1');
6493            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6494    
6495            !!!cp ('t415.1');            !!!cp ('t415.1');
6496            ## As if <p>, then reprocess the current token            ## As if <p>, then reprocess the current token
6497            my $el;            my $el;
6498            !!!create-element ($el, 'p');            !!!create-element ($el, 'p',, $token);
6499            $insert->($el);            $insert->($el);
6500            ## NOTE: Not inserted into |$self->{open_elements}|.            ## NOTE: Not inserted into |$self->{open_elements}|.
6501          }          }
# Line 6185  sub _tree_construction_main ($) { Line 6509  sub _tree_construction_main ($) {
6509                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
6510                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6511          !!!cp ('t427');          !!!cp ('t427');
6512          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token);
6513          redo B;          redo B;
6514        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
6515          !!!cp ('t428');          !!!cp ('t428');
6516          !!!parse-error (type => 'unmatched end tag:br');          !!!parse-error (type => 'unmatched end tag:br', token => $token);
6517    
6518          ## As if <br>          ## As if <br>
6519          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6520                    
6521          my $el;          my $el;
6522          !!!create-element ($el, 'br');          !!!create-element ($el, 'br',, $token);
6523          $insert->($el);          $insert->($el);
6524                    
6525          ## Ignore the token.          ## Ignore the token.
# Line 6214  sub _tree_construction_main ($) { Line 6538  sub _tree_construction_main ($) {
6538                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
6539                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6540          !!!cp ('t429');          !!!cp ('t429');
6541          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6542          ## Ignore the token          ## Ignore the token
6543          !!!next-token;          !!!next-token;
6544          redo B;          redo B;
# Line 6228  sub _tree_construction_main ($) { Line 6552  sub _tree_construction_main ($) {
6552    
6553          ## Step 2          ## Step 2
6554          S2: {          S2: {
6555            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6556              ## Step 1              ## Step 1
6557              ## generate implied end tags              ## generate implied end tags
6558              while ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
6559                !!!cp ('t430');                !!!cp ('t430');
6560                ## ISSUE: Can this case be reached?                ## ISSUE: Can this case be reached?
6561                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
6562              }              }
6563                    
6564              ## Step 2              ## Step 2
6565              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6566                        ne $token->{tag_name}) {
6567                !!!cp ('t431');                !!!cp ('t431');
6568                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
6569                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
6570                                  value => $self->{open_elements}->[-1]->[0]
6571                                      ->manakai_local_name,
6572                                  token => $token);
6573              } else {              } else {
6574                !!!cp ('t432');                !!!cp ('t432');
6575              }              }
# Line 6255  sub _tree_construction_main ($) { Line 6581  sub _tree_construction_main ($) {
6581              last S2;              last S2;
6582            } else {            } else {
6583              ## Step 3              ## Step 3
6584              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
6585                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
6586                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
6587                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
6588                !!!cp ('t433');                !!!cp ('t433');
6589                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6590                ## Ignore the token                ## Ignore the token
6591                !!!next-token;                !!!next-token;
6592                last S2;                last S2;
# Line 6325  sub set_inner_html ($$$) { Line 6651  sub set_inner_html ($$$) {
6651    
6652      ## Step 8 # MUST      ## Step 8 # MUST
6653      my $i = 0;      my $i = 0;
6654      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
6655      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
6656      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
6657        my $self = shift;        my $self = shift;
6658    
# Line 6335  sub set_inner_html ($$$) { Line 6661  sub set_inner_html ($$$) {
6661    
6662        $self->{next_char} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
6663        $self->{next_char} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
6664        $column++;  
6665          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
6666          $p->{column}++;
6667    
6668        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
6669          $line++;          $p->{line}++;
6670          $column = 0;          $p->{column} = 0;
6671          !!!cp ('i1');          !!!cp ('i1');
6672        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
6673          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
6674          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
6675          $line++;          $p->{line}++;
6676          $column = 0;          $p->{column} = 0;
6677          !!!cp ('i2');          !!!cp ('i2');
6678        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
6679          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
# Line 6361  sub set_inner_html ($$$) { Line 6689  sub set_inner_html ($$$) {
6689            
6690      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
6691        my (%opt) = @_;        my (%opt) = @_;
6692        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
6693          my $column = $opt{column};
6694          if (defined $opt{token} and defined $opt{token}->{line}) {
6695            $line = $opt{token}->{line};
6696            $column = $opt{token}->{column};
6697          }
6698          warn "Parse error ($opt{type}) at line $line column $column\n";
6699      };      };
6700      $p->{parse_error} = sub {      $p->{parse_error} = sub {
6701        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
6702      };      };
6703            
6704      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
# Line 6388  sub set_inner_html ($$$) { Line 6722  sub set_inner_html ($$$) {
6722          unless defined $p->{content_model};          unless defined $p->{content_model};
6723          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
6724    
6725      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
6726          ## TODO: Foreign element OK?
6727    
6728      ## Step 3      ## Step 3
6729      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6398  sub set_inner_html ($$$) { Line 6733  sub set_inner_html ($$$) {
6733      $doc->append_child ($root);      $doc->append_child ($root);
6734    
6735      ## Step 5 # MUST      ## Step 5 # MUST
6736      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
6737    
6738      undef $p->{head_element};      undef $p->{head_element};
6739    
# Line 6444  sub set_inner_html ($$$) { Line 6779  sub set_inner_html ($$$) {
6779      ## ISSUE: mutation events?      ## ISSUE: mutation events?
6780    
6781      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
6782    
6783        delete $p->{parse_error}; # delete loop
6784    } else {    } else {
6785      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";
6786    }    }

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24