/[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.35 by wakaba, Mon Jul 16 03:21:04 2007 UTC revision 1.52 by wakaba, Sat Jul 21 12:27:22 2007 UTC
# Line 150  sub new ($) { Line 150  sub new ($) {
150    return $self;    return $self;
151  } # new  } # new
152    
153    sub CM_ENTITY () { 0b001 } # & markup in data
154    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
155    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
156    
157    sub PLAINTEXT_CONTENT_MODEL () { 0 }
158    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
159    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
160    sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
161    
162  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
163    
164  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
165    my $self = shift;    my $self = shift;
166    $self->{state} = 'data'; # MUST    $self->{state} = 'data'; # MUST
167    $self->{content_model_flag} = 'PCDATA'; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
168    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
169    undef $self->{current_attribute};    undef $self->{current_attribute};
170    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
# Line 194  sub _get_next_token ($) { Line 203  sub _get_next_token ($) {
203    A: {    A: {
204      if ($self->{state} eq 'data') {      if ($self->{state} eq 'data') {
205        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_input_character} == 0x0026) { # &
206          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA
             $self->{content_model_flag} eq 'RCDATA') {  
207            $self->{state} = 'entity data';            $self->{state} = 'entity data';
208            !!!next-input-character;            !!!next-input-character;
209            redo A;            redo A;
# Line 203  sub _get_next_token ($) { Line 211  sub _get_next_token ($) {
211            #            #
212          }          }
213        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_input_character} == 0x002D) { # -
214          if ($self->{content_model_flag} eq 'RCDATA' or          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
             $self->{content_model_flag} eq 'CDATA') {  
215            unless ($self->{escape}) {            unless ($self->{escape}) {
216              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_input_character}->[0] == 0x002D and # -
217                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_input_character}->[1] == 0x0021 and # !
# Line 216  sub _get_next_token ($) { Line 223  sub _get_next_token ($) {
223                    
224          #          #
225        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_input_character} == 0x003C) { # <
226          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
227              (($self->{content_model_flag} eq 'CDATA' or              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
               $self->{content_model_flag} eq 'RCDATA') and  
228               not $self->{escape})) {               not $self->{escape})) {
229            $self->{state} = 'tag open';            $self->{state} = 'tag open';
230            !!!next-input-character;            !!!next-input-character;
# Line 228  sub _get_next_token ($) { Line 234  sub _get_next_token ($) {
234          }          }
235        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
236          if ($self->{escape} and          if ($self->{escape} and
237              ($self->{content_model_flag} eq 'RCDATA' or              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
              $self->{content_model_flag} eq 'CDATA')) {  
238            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_input_character}->[0] == 0x002D and # -
239                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_input_character}->[1] == 0x002D) { # -
240              delete $self->{escape};              delete $self->{escape};
# Line 266  sub _get_next_token ($) { Line 271  sub _get_next_token ($) {
271    
272        redo A;        redo A;
273      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} eq 'tag open') {
274        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
           $self->{content_model_flag} eq 'CDATA') {  
275          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_input_character} == 0x002F) { # /
276            !!!next-input-character;            !!!next-input-character;
277            $self->{state} = 'close tag open';            $self->{state} = 'close tag open';
# Line 280  sub _get_next_token ($) { Line 284  sub _get_next_token ($) {
284    
285            redo A;            redo A;
286          }          }
287        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
288          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_input_character} == 0x0021) { # !
289            $self->{state} = 'markup declaration open';            $self->{state} = 'markup declaration open';
290            !!!next-input-character;            !!!next-input-character;
# Line 327  sub _get_next_token ($) { Line 331  sub _get_next_token ($) {
331            redo A;            redo A;
332          }          }
333        } else {        } else {
334          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
335        }        }
336      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} eq 'close tag open') {
337        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
           $self->{content_model_flag} eq 'CDATA') {  
338          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
339            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
340            my @next_char;            my @next_char;
# Line 429  sub _get_next_token ($) { Line 432  sub _get_next_token ($) {
432                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
433            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
434          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
435            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
436            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
437              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
438            }            }
# Line 456  sub _get_next_token ($) { Line 459  sub _get_next_token ($) {
459                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
460            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
461          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
462            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
463            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
464              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
465            }            }
# Line 504  sub _get_next_token ($) { Line 507  sub _get_next_token ($) {
507                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
508            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
509          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
510            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
511            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
512              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
513            }            }
# Line 544  sub _get_next_token ($) { Line 547  sub _get_next_token ($) {
547                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
548            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
549          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
550            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
551            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
552              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
553            }            }
# Line 568  sub _get_next_token ($) { Line 571  sub _get_next_token ($) {
571        my $before_leave = sub {        my $before_leave = sub {
572          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
573              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
574            !!!parse-error (type => 'dupulicate attribute');            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});
575            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
576          } else {          } else {
577            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
# Line 597  sub _get_next_token ($) { Line 600  sub _get_next_token ($) {
600                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
601            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
602          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
603            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
604            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
605              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
606            }            }
# Line 638  sub _get_next_token ($) { Line 641  sub _get_next_token ($) {
641                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
642            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
643          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
644            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
645            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
646              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
647            }            }
# Line 676  sub _get_next_token ($) { Line 679  sub _get_next_token ($) {
679                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
680            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
681          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
682            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
683            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
684              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
685            }            }
# Line 717  sub _get_next_token ($) { Line 720  sub _get_next_token ($) {
720                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
721            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
722          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
723            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
724            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
725              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
726            }            }
# Line 764  sub _get_next_token ($) { Line 767  sub _get_next_token ($) {
767                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
768            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
769          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
770            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
771            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
772              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
773            }            }
# Line 784  sub _get_next_token ($) { Line 787  sub _get_next_token ($) {
787                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
788            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
789          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
790            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
791            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
792              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
793            }            }
# Line 820  sub _get_next_token ($) { Line 823  sub _get_next_token ($) {
823                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
824            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
825          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
826            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
827            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
828              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
829            }            }
# Line 856  sub _get_next_token ($) { Line 859  sub _get_next_token ($) {
859                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
860            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
861          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
862            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
863            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
864              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
865            }            }
# Line 895  sub _get_next_token ($) { Line 898  sub _get_next_token ($) {
898                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
899            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
900          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
901            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
902            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
903              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
904            }            }
# Line 915  sub _get_next_token ($) { Line 918  sub _get_next_token ($) {
918                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
919            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
920          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
921            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
922            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
923              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
924            }            }
# Line 1644  sub _tokenize_attempt_to_consume_an_enti Line 1647  sub _tokenize_attempt_to_consume_an_enti
1647            redo X;            redo X;
1648          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
1649            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro');
1650              !!!back-next-input-character ($x_char, $self->{next_input_character});
1651            $self->{next_input_character} = 0x0023; # #            $self->{next_input_character} = 0x0023; # #
           !!!back-next-input-character ($x_char);  
1652            return undef;            return undef;
1653          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_input_character} == 0x003B) { # ;
1654            !!!next-input-character;            !!!next-input-character;
# Line 1717  sub _tokenize_attempt_to_consume_an_enti Line 1720  sub _tokenize_attempt_to_consume_an_enti
1720      !!!next-input-character;      !!!next-input-character;
1721    
1722      my $value = $entity_name;      my $value = $entity_name;
1723      my $match;      my $match = 0;
1724      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
1725      our $EntityChar;      our $EntityChar;
1726    
# Line 1737  sub _tokenize_attempt_to_consume_an_enti Line 1740  sub _tokenize_attempt_to_consume_an_enti
1740            $match = 1;            $match = 1;
1741            !!!next-input-character;            !!!next-input-character;
1742            last;            last;
1743          } elsif (not $in_attr) {          } else {
1744            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
1745            $match = -1;            $match = -1;
1746          } else {            !!!next-input-character;
           $value .= chr $self->{next_input_character};  
1747          }          }
1748        } else {        } else {
1749          $value .= chr $self->{next_input_character};          $value .= chr $self->{next_input_character};
1750            $match *= 2;
1751            !!!next-input-character;
1752        }        }
       !!!next-input-character;  
1753      }      }
1754            
1755      if ($match > 0) {      if ($match > 0) {
1756        return {type => 'character', data => $value};        return {type => 'character', data => $value};
1757      } elsif ($match < 0) {      } elsif ($match < 0) {
1758        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc');
1759        return {type => 'character', data => $value};        if ($in_attr and $match < -1) {
1760            return {type => 'character', data => '&'.$entity_name};
1761          } else {
1762            return {type => 'character', data => $value};
1763          }
1764      } else {      } else {
1765        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
1766        ## NOTE: No characters are consumed in the spec.        ## NOTE: No characters are consumed in the spec.
# Line 2060  sub _reset_insertion_mode ($) { Line 2067  sub _reset_insertion_mode ($) {
2067                        th => 'in cell',                        th => 'in cell',
2068                        tr => 'in row',                        tr => 'in row',
2069                        tbody => 'in table body',                        tbody => 'in table body',
2070                        thead => 'in table head',                        thead => 'in table body',
2071                        tfoot => 'in table foot',                        tfoot => 'in table body',
2072                        caption => 'in caption',                        caption => 'in caption',
2073                        colgroup => 'in column group',                        colgroup => 'in column group',
2074                        table => 'in table',                        table => 'in table',
# Line 2096  sub _reset_insertion_mode ($) { Line 2103  sub _reset_insertion_mode ($) {
2103  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
2104    my $self = shift;    my $self = shift;
2105    
   my $previous_insertion_mode;  
   
2106    my $active_formatting_elements = [];    my $active_formatting_elements = [];
2107    
2108    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 2192  sub _tree_construction_main ($) { Line 2197  sub _tree_construction_main ($) {
2197      $insert->($el); # /context node/->append_child ($el)      $insert->($el); # /context node/->append_child ($el)
2198    
2199      ## Step 3      ## Step 3
2200      $self->{content_model_flag} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
2201      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2202    
2203      ## Step 4      ## Step 4
# Line 2210  sub _tree_construction_main ($) { Line 2215  sub _tree_construction_main ($) {
2215      }      }
2216    
2217      ## Step 6      ## Step 6
2218      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2219    
2220      ## Step 7      ## Step 7
2221      if ($token->{type} eq 'end tag' and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} eq 'end tag' and $token->{tag_name} eq $start_tag_name) {
2222        ## Ignore the token        ## Ignore the token
2223        } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2224          !!!parse-error (type => 'in CDATA:#'.$token->{type});
2225        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
2226          !!!parse-error (type => 'in RCDATA:#'.$token->{type});
2227      } else {      } else {
2228        !!!parse-error (type => 'in '.$content_model_flag.':#'.$token->{type});        die "$0: $content_model_flag in parse_rcdata";
2229      }      }
2230      !!!next-token;      !!!next-token;
2231    }; # $parse_rcdata    }; # $parse_rcdata
# Line 2227  sub _tree_construction_main ($) { Line 2236  sub _tree_construction_main ($) {
2236      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes});
2237      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
2238    
2239      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
2240      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2241            
2242      my $text = '';      my $text = '';
# Line 2240  sub _tree_construction_main ($) { Line 2249  sub _tree_construction_main ($) {
2249        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
2250      }      }
2251                                
2252      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2253    
2254      if ($token->{type} eq 'end tag' and      if ($token->{type} eq 'end tag' and
2255          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
# Line 2485  sub _tree_construction_main ($) { Line 2494  sub _tree_construction_main ($) {
2494                         }                         }
2495    }; # $insert_to_foster    }; # $insert_to_foster
2496    
2497      my $insert;
2498    
2499      B: {
2500        if ($token->{type} eq 'DOCTYPE') {
2501          !!!parse-error (type => 'DOCTYPE in the middle');
2502          ## Ignore the token
2503          ## Stay in the phase
2504          !!!next-token;
2505          redo B;
2506        } elsif ($token->{type} eq 'end-of-file') {
2507          if ($self->{insertion_mode} eq 'after html body' or
2508              $self->{insertion_mode} eq 'after html frameset') {
2509            #
2510          } else {
2511            ## Generate implied end tags
2512            if ({
2513                 dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,
2514                 tbody => 1, tfoot=> 1, thead => 1,
2515                }->{$self->{open_elements}->[-1]->[1]}) {
2516              !!!back-token;
2517              $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};
2518              redo B;
2519            }
2520            
2521            if (@{$self->{open_elements}} > 2 or
2522                (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {
2523              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
2524            } elsif (defined $self->{inner_html_node} and
2525                     @{$self->{open_elements}} > 1 and
2526                     $self->{open_elements}->[1]->[1] ne 'body') {
2527              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
2528            }
2529    
2530            ## ISSUE: There is an issue in the spec.
2531          }
2532    
2533          ## Stop parsing
2534          last B;
2535        } elsif ($token->{type} eq 'start tag' and
2536                 $token->{tag_name} eq 'html') {
2537          if ($self->{insertion_mode} eq 'after html body') {
2538            ## Turn into the main phase
2539            !!!parse-error (type => 'after html:html');
2540            $self->{insertion_mode} = 'after body';
2541          } elsif ($self->{insertion_mode} eq 'after html frameset') {
2542            ## Turn into the main phase
2543            !!!parse-error (type => 'after html:html');
2544            $self->{insertion_mode} = 'after frameset';
2545          }
2546    
2547    ## ISSUE: "aa<html>" is not a parse error.
2548    ## ISSUE: "<html>" in fragment is not a parse error.
2549          unless ($token->{first_start_tag}) {
2550            !!!parse-error (type => 'not first start tag');
2551          }
2552          my $top_el = $self->{open_elements}->[0]->[0];
2553          for my $attr_name (keys %{$token->{attributes}}) {
2554            unless ($top_el->has_attribute_ns (undef, $attr_name)) {
2555              $top_el->set_attribute_ns
2556                (undef, [undef, $attr_name],
2557                 $token->{attributes}->{$attr_name}->{value});
2558            }
2559          }
2560          !!!next-token;
2561          redo B;
2562        } elsif ($token->{type} eq 'comment') {
2563          my $comment = $self->{document}->create_comment ($token->{data});
2564          if ($self->{insertion_mode} eq 'after html body' or
2565              $self->{insertion_mode} eq 'after html frameset') {
2566            $self->{document}->append_child ($comment);
2567          } elsif ($self->{insertion_mode} eq 'after body') {
2568            $self->{open_elements}->[0]->[0]->append_child ($comment);
2569          } else {
2570            $self->{open_elements}->[-1]->[0]->append_child ($comment);
2571          }
2572          !!!next-token;
2573          redo B;
2574        } elsif ($self->{insertion_mode} eq 'in head' or
2575                 $self->{insertion_mode} eq 'in head noscript' or
2576                 $self->{insertion_mode} eq 'after head' or
2577                 $self->{insertion_mode} eq 'before head') {
2578          if ($token->{type} eq 'character') {
2579            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
2580              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
2581              unless (length $token->{data}) {
2582                !!!next-token;
2583                redo B;
2584              }
2585            }
2586    
2587            if ($self->{insertion_mode} eq 'before head') {
2588              ## As if <head>
2589              !!!create-element ($self->{head_element}, 'head');
2590              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2591              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2592    
2593              ## Reprocess in the "in head" insertion mode...
2594              pop @{$self->{open_elements}};
2595    
2596              ## Reprocess in the "after head" insertion mode...
2597            } elsif ($self->{insertion_mode} eq 'in head noscript') {
2598              ## As if </noscript>
2599              pop @{$self->{open_elements}};
2600              !!!parse-error (type => 'in noscript:#character');
2601              
2602              ## Reprocess in the "in head" insertion mode...
2603              ## As if </head>
2604              pop @{$self->{open_elements}};
2605    
2606              ## Reprocess in the "after head" insertion mode...
2607            } elsif ($self->{insertion_mode} eq 'in head') {
2608              pop @{$self->{open_elements}};
2609    
2610              ## Reprocess in the "after head" insertion mode...
2611            }
2612    
2613                ## "after head" insertion mode
2614                ## As if <body>
2615                !!!insert-element ('body');
2616                $self->{insertion_mode} = 'in body';
2617                ## reprocess
2618                redo B;
2619              } elsif ($token->{type} eq 'start tag') {
2620                if ($token->{tag_name} eq 'head') {
2621                  if ($self->{insertion_mode} eq 'before head') {
2622                    !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
2623                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2624                    push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
2625                    $self->{insertion_mode} = 'in head';
2626                    !!!next-token;
2627                    redo B;
2628                  } elsif ($self->{insertion_mode} ne 'after head') {
2629                    !!!parse-error (type => 'in head:head'); # or in head noscript
2630                    ## Ignore the token
2631                    !!!next-token;
2632                    redo B;
2633                  } else {
2634                    #
2635                  }
2636                } elsif ($self->{insertion_mode} eq 'before head') {
2637                  ## As if <head>
2638                  !!!create-element ($self->{head_element}, 'head');
2639                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2640                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2641    
2642                  $self->{insertion_mode} = 'in head';
2643                  ## Reprocess in the "in head" insertion mode...
2644                }
2645    
2646                if ($token->{tag_name} eq 'base') {
2647                  if ($self->{insertion_mode} eq 'in head noscript') {
2648                    ## As if </noscript>
2649                    pop @{$self->{open_elements}};
2650                    !!!parse-error (type => 'in noscript:base');
2651                  
2652                    $self->{insertion_mode} = 'in head';
2653                    ## Reprocess in the "in head" insertion mode...
2654                  }
2655    
2656                  ## NOTE: There is a "as if in head" code clone.
2657                  if ($self->{insertion_mode} eq 'after head') {
2658                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2659                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2660                  }
2661                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2662                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2663                  pop @{$self->{open_elements}}
2664                      if $self->{insertion_mode} eq 'after head';
2665                  !!!next-token;
2666                  redo B;
2667                } elsif ($token->{tag_name} eq 'link') {
2668                  ## NOTE: There is a "as if in head" code clone.
2669                  if ($self->{insertion_mode} eq 'after head') {
2670                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2671                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2672                  }
2673                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2674                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2675                  pop @{$self->{open_elements}}
2676                      if $self->{insertion_mode} eq 'after head';
2677                  !!!next-token;
2678                  redo B;
2679                } elsif ($token->{tag_name} eq 'meta') {
2680                  ## NOTE: There is a "as if in head" code clone.
2681                  if ($self->{insertion_mode} eq 'after head') {
2682                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2683                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2684                  }
2685                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2686                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2687    
2688                  unless ($self->{confident}) {
2689                    my $charset;
2690                    if ($token->{attributes}->{charset}) { ## TODO: And if supported
2691                      $charset = $token->{attributes}->{charset}->{value};
2692                    }
2693                    if ($token->{attributes}->{'http-equiv'}) {
2694                      ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
2695                      if ($token->{attributes}->{'http-equiv'}->{value}
2696                          =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
2697                              [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2698                              ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
2699                        $charset = defined $1 ? $1 : defined $2 ? $2 : $3;
2700                      } ## TODO: And if supported
2701                    }
2702                    ## TODO: Change the encoding
2703                  }
2704    
2705                  ## TODO: Extracting |charset| from |meta|.
2706                  pop @{$self->{open_elements}}
2707                      if $self->{insertion_mode} eq 'after head';
2708                  !!!next-token;
2709                  redo B;
2710                } elsif ($token->{tag_name} eq 'title') {
2711                  if ($self->{insertion_mode} eq 'in head noscript') {
2712                    ## As if </noscript>
2713                    pop @{$self->{open_elements}};
2714                    !!!parse-error (type => 'in noscript:title');
2715                  
2716                    $self->{insertion_mode} = 'in head';
2717                    ## Reprocess in the "in head" insertion mode...
2718                  } elsif ($self->{insertion_mode} eq 'after head') {
2719                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2720                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2721                  }
2722    
2723                  ## NOTE: There is a "as if in head" code clone.
2724                  my $parent = defined $self->{head_element} ? $self->{head_element}
2725                      : $self->{open_elements}->[-1]->[0];
2726                  $parse_rcdata->(RCDATA_CONTENT_MODEL,
2727                                  sub { $parent->append_child ($_[0]) });
2728                  pop @{$self->{open_elements}}
2729                      if $self->{insertion_mode} eq 'after head';
2730                  redo B;
2731                } elsif ($token->{tag_name} eq 'style') {
2732                  ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
2733                  ## insertion mode 'in head')
2734                  ## NOTE: There is a "as if in head" code clone.
2735                  if ($self->{insertion_mode} eq 'after head') {
2736                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2737                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2738                  }
2739                  $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
2740                  pop @{$self->{open_elements}}
2741                      if $self->{insertion_mode} eq 'after head';
2742                  redo B;
2743                } elsif ($token->{tag_name} eq 'noscript') {
2744                  if ($self->{insertion_mode} eq 'in head') {
2745                    ## NOTE: and scripting is disalbed
2746                    !!!insert-element ($token->{tag_name}, $token->{attributes});
2747                    $self->{insertion_mode} = 'in head noscript';
2748                    !!!next-token;
2749                    redo B;
2750                  } elsif ($self->{insertion_mode} eq 'in head noscript') {
2751                    !!!parse-error (type => 'in noscript:noscript');
2752                    ## Ignore the token
2753                    !!!next-token;
2754                    redo B;
2755                  } else {
2756                    #
2757                  }
2758                } elsif ($token->{tag_name} eq 'script') {
2759                  if ($self->{insertion_mode} eq 'in head noscript') {
2760                    ## As if </noscript>
2761                    pop @{$self->{open_elements}};
2762                    !!!parse-error (type => 'in noscript:script');
2763                  
2764                    $self->{insertion_mode} = 'in head';
2765                    ## Reprocess in the "in head" insertion mode...
2766                  } elsif ($self->{insertion_mode} eq 'after head') {
2767                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2768                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2769                  }
2770    
2771                  ## NOTE: There is a "as if in head" code clone.
2772                  $script_start_tag->($insert_to_current);
2773                  pop @{$self->{open_elements}}
2774                      if $self->{insertion_mode} eq 'after head';
2775                  redo B;
2776                } elsif ($token->{tag_name} eq 'body' or
2777                         $token->{tag_name} eq 'frameset') {
2778                  if ($self->{insertion_mode} eq 'in head noscript') {
2779                    ## As if </noscript>
2780                    pop @{$self->{open_elements}};
2781                    !!!parse-error (type => 'in noscript:'.$token->{tag_name});
2782                    
2783                    ## Reprocess in the "in head" insertion mode...
2784                    ## As if </head>
2785                    pop @{$self->{open_elements}};
2786                    
2787                    ## Reprocess in the "after head" insertion mode...
2788                  } elsif ($self->{insertion_mode} eq 'in head') {
2789                    pop @{$self->{open_elements}};
2790                    
2791                    ## Reprocess in the "after head" insertion mode...
2792                  }
2793    
2794                  ## "after head" insertion mode
2795                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2796                  $self->{insertion_mode} = 'in '.$token->{tag_name};
2797                  !!!next-token;
2798                  redo B;
2799                } else {
2800                  #
2801                }
2802    
2803                if ($self->{insertion_mode} eq 'in head noscript') {
2804                  ## As if </noscript>
2805                  pop @{$self->{open_elements}};
2806                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
2807                  
2808                  ## Reprocess in the "in head" insertion mode...
2809                  ## As if </head>
2810                  pop @{$self->{open_elements}};
2811    
2812                  ## Reprocess in the "after head" insertion mode...
2813                } elsif ($self->{insertion_mode} eq 'in head') {
2814                  ## As if </head>
2815                  pop @{$self->{open_elements}};
2816    
2817                  ## Reprocess in the "after head" insertion mode...
2818                }
2819    
2820                ## "after head" insertion mode
2821                ## As if <body>
2822                !!!insert-element ('body');
2823                $self->{insertion_mode} = 'in body';
2824                ## reprocess
2825                redo B;
2826              } elsif ($token->{type} eq 'end tag') {
2827                if ($token->{tag_name} eq 'head') {
2828                  if ($self->{insertion_mode} eq 'before head') {
2829                    ## As if <head>
2830                    !!!create-element ($self->{head_element}, 'head');
2831                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2832                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2833    
2834                    ## Reprocess in the "in head" insertion mode...
2835                    pop @{$self->{open_elements}};
2836                    $self->{insertion_mode} = 'after head';
2837                    !!!next-token;
2838                    redo B;
2839                  } elsif ($self->{insertion_mode} eq 'in head noscript') {
2840                    ## As if </noscript>
2841                    pop @{$self->{open_elements}};
2842                    !!!parse-error (type => 'in noscript:script');
2843                    
2844                    ## Reprocess in the "in head" insertion mode...
2845                    pop @{$self->{open_elements}};
2846                    $self->{insertion_mode} = 'after head';
2847                    !!!next-token;
2848                    redo B;
2849                  } elsif ($self->{insertion_mode} eq 'in head') {
2850                    pop @{$self->{open_elements}};
2851                    $self->{insertion_mode} = 'after head';
2852                    !!!next-token;
2853                    redo B;
2854                  } else {
2855                    #
2856                  }
2857                } elsif ($token->{tag_name} eq 'noscript') {
2858                  if ($self->{insertion_mode} eq 'in head noscript') {
2859                    pop @{$self->{open_elements}};
2860                    $self->{insertion_mode} = 'in head';
2861                    !!!next-token;
2862                    redo B;
2863                  } elsif ($self->{insertion_mode} eq 'before head') {
2864                    !!!parse-error (type => 'unmatched end tag:noscript');
2865                    ## Ignore the token ## ISSUE: An issue in the spec.
2866                    !!!next-token;
2867                    redo B;
2868                  } else {
2869                    #
2870                  }
2871                } elsif ({
2872                          body => 1, html => 1,
2873                         }->{$token->{tag_name}}) {
2874                  if ($self->{insertion_mode} eq 'before head') {
2875                    ## As if <head>
2876                    !!!create-element ($self->{head_element}, 'head');
2877                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2878                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2879    
2880                    $self->{insertion_mode} = 'in head';
2881                    ## Reprocess in the "in head" insertion mode...
2882                  } elsif ($self->{insertion_mode} eq 'in head noscript') {
2883                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2884                    ## Ignore the token
2885                    !!!next-token;
2886                    redo B;
2887                  }
2888                  
2889                  #
2890                } elsif ({
2891                          p => 1, br => 1,
2892                         }->{$token->{tag_name}}) {
2893                  if ($self->{insertion_mode} eq 'before head') {
2894                    ## As if <head>
2895                    !!!create-element ($self->{head_element}, 'head');
2896                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2897                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2898    
2899                    $self->{insertion_mode} = 'in head';
2900                    ## Reprocess in the "in head" insertion mode...
2901                  }
2902    
2903                  #
2904                } else {
2905                  if ($self->{insertion_mode} ne 'after head') {
2906                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2907                    ## Ignore the token
2908                    !!!next-token;
2909                    redo B;
2910                  } else {
2911                    #
2912                  }
2913                }
2914    
2915                if ($self->{insertion_mode} eq 'in head noscript') {
2916                  ## As if </noscript>
2917                  pop @{$self->{open_elements}};
2918                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
2919                  
2920                  ## Reprocess in the "in head" insertion mode...
2921                  ## As if </head>
2922                  pop @{$self->{open_elements}};
2923    
2924                  ## Reprocess in the "after head" insertion mode...
2925                } elsif ($self->{insertion_mode} eq 'in head') {
2926                  ## As if </head>
2927                  pop @{$self->{open_elements}};
2928    
2929                  ## Reprocess in the "after head" insertion mode...
2930                } elsif ($self->{insertion_mode} eq 'before head') {
2931                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2932                  ## Ignore the token ## ISSUE: An issue in the spec.
2933                  !!!next-token;
2934                  redo B;
2935                }
2936    
2937                ## "after head" insertion mode
2938                ## As if <body>
2939                !!!insert-element ('body');
2940                $self->{insertion_mode} = 'in body';
2941                ## reprocess
2942                redo B;
2943              } else {
2944                die "$0: $token->{type}: Unknown token type";
2945              }
2946    
2947              ## ISSUE: An issue in the spec.
2948        } elsif ($self->{insertion_mode} eq 'in body' or
2949                 $self->{insertion_mode} eq 'in cell' or
2950                 $self->{insertion_mode} eq 'in caption') {
2951              if ($token->{type} eq 'character') {
2952                ## NOTE: There is a code clone of "character in body".
2953                $reconstruct_active_formatting_elements->($insert_to_current);
2954                
2955                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
2956    
2957                !!!next-token;
2958                redo B;
2959              } elsif ($token->{type} eq 'start tag') {
2960                if ({
2961                     caption => 1, col => 1, colgroup => 1, tbody => 1,
2962                     td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
2963                    }->{$token->{tag_name}}) {
2964                  if ($self->{insertion_mode} eq 'in cell') {
2965                    ## have an element in table scope
2966                    my $tn;
2967                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
2968                      my $node = $self->{open_elements}->[$_];
2969                      if ($node->[1] eq 'td' or $node->[1] eq 'th') {
2970                        $tn = $node->[1];
2971                        last INSCOPE;
2972                      } elsif ({
2973                                table => 1, html => 1,
2974                               }->{$node->[1]}) {
2975                        last INSCOPE;
2976                      }
2977                    } # INSCOPE
2978                      unless (defined $tn) {
2979                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2980                        ## Ignore the token
2981                        !!!next-token;
2982                        redo B;
2983                      }
2984                    
2985                    ## Close the cell
2986                    !!!back-token; # <?>
2987                    $token = {type => 'end tag', tag_name => $tn};
2988                    redo B;
2989                  } elsif ($self->{insertion_mode} eq 'in caption') {
2990                    !!!parse-error (type => 'not closed:caption');
2991                    
2992                    ## As if </caption>
2993                    ## have a table element in table scope
2994                    my $i;
2995                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
2996                      my $node = $self->{open_elements}->[$_];
2997                      if ($node->[1] eq 'caption') {
2998                        $i = $_;
2999                        last INSCOPE;
3000                      } elsif ({
3001                                table => 1, html => 1,
3002                               }->{$node->[1]}) {
3003                        last INSCOPE;
3004                      }
3005                    } # INSCOPE
3006                      unless (defined $i) {
3007                        !!!parse-error (type => 'unmatched end tag:caption');
3008                        ## Ignore the token
3009                        !!!next-token;
3010                        redo B;
3011                      }
3012                    
3013                    ## generate implied end tags
3014                    if ({
3015                         dd => 1, dt => 1, li => 1, p => 1,
3016                         td => 1, th => 1, tr => 1,
3017                         tbody => 1, tfoot=> 1, thead => 1,
3018                        }->{$self->{open_elements}->[-1]->[1]}) {
3019                      !!!back-token; # <?>
3020                      $token = {type => 'end tag', tag_name => 'caption'};
3021                      !!!back-token;
3022                      $token = {type => 'end tag',
3023                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3024                      redo B;
3025                    }
3026    
3027                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3028                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3029                    }
3030                    
3031                    splice @{$self->{open_elements}}, $i;
3032                    
3033                    $clear_up_to_marker->();
3034                    
3035                    $self->{insertion_mode} = 'in table';
3036                    
3037                    ## reprocess
3038                    redo B;
3039                  } else {
3040                    #
3041                  }
3042                } else {
3043                  #
3044                }
3045              } elsif ($token->{type} eq 'end tag') {
3046                if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3047                  if ($self->{insertion_mode} eq 'in cell') {
3048                    ## have an element in table scope
3049                    my $i;
3050                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3051                      my $node = $self->{open_elements}->[$_];
3052                      if ($node->[1] eq $token->{tag_name}) {
3053                        $i = $_;
3054                        last INSCOPE;
3055                      } elsif ({
3056                                table => 1, html => 1,
3057                               }->{$node->[1]}) {
3058                        last INSCOPE;
3059                      }
3060                    } # INSCOPE
3061                      unless (defined $i) {
3062                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3063                        ## Ignore the token
3064                        !!!next-token;
3065                        redo B;
3066                      }
3067                    
3068                    ## generate implied end tags
3069                    if ({
3070                         dd => 1, dt => 1, li => 1, p => 1,
3071                         td => ($token->{tag_name} eq 'th'),
3072                         th => ($token->{tag_name} eq 'td'),
3073                         tr => 1,
3074                         tbody => 1, tfoot=> 1, thead => 1,
3075                        }->{$self->{open_elements}->[-1]->[1]}) {
3076                      !!!back-token;
3077                      $token = {type => 'end tag',
3078                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3079                      redo B;
3080                    }
3081                    
3082                    if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3083                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3084                    }
3085                    
3086                    splice @{$self->{open_elements}}, $i;
3087                    
3088                    $clear_up_to_marker->();
3089                    
3090                    $self->{insertion_mode} = 'in row';
3091                    
3092                    !!!next-token;
3093                    redo B;
3094                  } elsif ($self->{insertion_mode} eq 'in caption') {
3095                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3096                    ## Ignore the token
3097                    !!!next-token;
3098                    redo B;
3099                  } else {
3100                    #
3101                  }
3102                } elsif ($token->{tag_name} eq 'caption') {
3103                  if ($self->{insertion_mode} eq 'in caption') {
3104                    ## have a table element in table scope
3105                    my $i;
3106                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3107                      my $node = $self->{open_elements}->[$_];
3108                      if ($node->[1] eq $token->{tag_name}) {
3109                        $i = $_;
3110                        last INSCOPE;
3111                      } elsif ({
3112                                table => 1, html => 1,
3113                               }->{$node->[1]}) {
3114                        last INSCOPE;
3115                      }
3116                    } # INSCOPE
3117                      unless (defined $i) {
3118                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3119                        ## Ignore the token
3120                        !!!next-token;
3121                        redo B;
3122                      }
3123                    
3124                    ## generate implied end tags
3125                    if ({
3126                         dd => 1, dt => 1, li => 1, p => 1,
3127                         td => 1, th => 1, tr => 1,
3128                         tbody => 1, tfoot=> 1, thead => 1,
3129                        }->{$self->{open_elements}->[-1]->[1]}) {
3130                      !!!back-token;
3131                      $token = {type => 'end tag',
3132                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3133                      redo B;
3134                    }
3135                    
3136                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3137                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3138                    }
3139                    
3140                    splice @{$self->{open_elements}}, $i;
3141                    
3142                    $clear_up_to_marker->();
3143                    
3144                    $self->{insertion_mode} = 'in table';
3145                    
3146                    !!!next-token;
3147                    redo B;
3148                  } elsif ($self->{insertion_mode} eq 'in cell') {
3149                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3150                    ## Ignore the token
3151                    !!!next-token;
3152                    redo B;
3153                  } else {
3154                    #
3155                  }
3156                } elsif ({
3157                          table => 1, tbody => 1, tfoot => 1,
3158                          thead => 1, tr => 1,
3159                         }->{$token->{tag_name}} and
3160                         $self->{insertion_mode} eq 'in cell') {
3161                  ## have an element in table scope
3162                  my $i;
3163                  my $tn;
3164                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3165                    my $node = $self->{open_elements}->[$_];
3166                    if ($node->[1] eq $token->{tag_name}) {
3167                      $i = $_;
3168                      last INSCOPE;
3169                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
3170                      $tn = $node->[1];
3171                      ## NOTE: There is exactly one |td| or |th| element
3172                      ## in scope in the stack of open elements by definition.
3173                    } elsif ({
3174                              table => 1, html => 1,
3175                             }->{$node->[1]}) {
3176                      last INSCOPE;
3177                    }
3178                  } # INSCOPE
3179                  unless (defined $i) {
3180                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3181                    ## Ignore the token
3182                    !!!next-token;
3183                    redo B;
3184                  }
3185    
3186                  ## Close the cell
3187                  !!!back-token; # </?>
3188                  $token = {type => 'end tag', tag_name => $tn};
3189                  redo B;
3190                } elsif ($token->{tag_name} eq 'table' and
3191                         $self->{insertion_mode} eq 'in caption') {
3192                  !!!parse-error (type => 'not closed:caption');
3193    
3194                  ## As if </caption>
3195                  ## have a table element in table scope
3196                  my $i;
3197                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3198                    my $node = $self->{open_elements}->[$_];
3199                    if ($node->[1] eq 'caption') {
3200                      $i = $_;
3201                      last INSCOPE;
3202                    } elsif ({
3203                              table => 1, html => 1,
3204                             }->{$node->[1]}) {
3205                      last INSCOPE;
3206                    }
3207                  } # INSCOPE
3208                  unless (defined $i) {
3209                    !!!parse-error (type => 'unmatched end tag:caption');
3210                    ## Ignore the token
3211                    !!!next-token;
3212                    redo B;
3213                  }
3214                  
3215                  ## generate implied end tags
3216                  if ({
3217                       dd => 1, dt => 1, li => 1, p => 1,
3218                       td => 1, th => 1, tr => 1,
3219                       tbody => 1, tfoot=> 1, thead => 1,
3220                      }->{$self->{open_elements}->[-1]->[1]}) {
3221                    !!!back-token; # </table>
3222                    $token = {type => 'end tag', tag_name => 'caption'};
3223                    !!!back-token;
3224                    $token = {type => 'end tag',
3225                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3226                    redo B;
3227                  }
3228    
3229                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3230                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3231                  }
3232    
3233                  splice @{$self->{open_elements}}, $i;
3234    
3235                  $clear_up_to_marker->();
3236    
3237                  $self->{insertion_mode} = 'in table';
3238    
3239                  ## reprocess
3240                  redo B;
3241                } elsif ({
3242                          body => 1, col => 1, colgroup => 1, html => 1,
3243                         }->{$token->{tag_name}}) {
3244                  if ($self->{insertion_mode} eq 'in cell' or
3245                      $self->{insertion_mode} eq 'in caption') {
3246                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3247                    ## Ignore the token
3248                    !!!next-token;
3249                    redo B;
3250                  } else {
3251                    #
3252                  }
3253                } elsif ({
3254                          tbody => 1, tfoot => 1,
3255                          thead => 1, tr => 1,
3256                         }->{$token->{tag_name}} and
3257                         $self->{insertion_mode} eq 'in caption') {
3258                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3259                  ## Ignore the token
3260                  !!!next-token;
3261                  redo B;
3262                } else {
3263                  #
3264                }
3265          } else {
3266            die "$0: $token->{type}: Unknown token type";
3267          }
3268    
3269          $insert = $insert_to_current;
3270          #
3271        } elsif ($self->{insertion_mode} eq 'in row' or
3272                 $self->{insertion_mode} eq 'in table body' or
3273                 $self->{insertion_mode} eq 'in table') {
3274              if ($token->{type} eq 'character') {
3275                ## NOTE: There are "character in table" code clones.
3276                if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3277                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3278                  
3279                  unless (length $token->{data}) {
3280                    !!!next-token;
3281                    redo B;
3282                  }
3283                }
3284    
3285                !!!parse-error (type => 'in table:#character');
3286    
3287                ## As if in body, but insert into foster parent element
3288                ## ISSUE: Spec says that "whenever a node would be inserted
3289                ## into the current node" while characters might not be
3290                ## result in a new Text node.
3291                $reconstruct_active_formatting_elements->($insert_to_foster);
3292                
3293                if ({
3294                     table => 1, tbody => 1, tfoot => 1,
3295                     thead => 1, tr => 1,
3296                    }->{$self->{open_elements}->[-1]->[1]}) {
3297                  # MUST
3298                  my $foster_parent_element;
3299                  my $next_sibling;
3300                  my $prev_sibling;
3301                  OE: for (reverse 0..$#{$self->{open_elements}}) {
3302                    if ($self->{open_elements}->[$_]->[1] eq 'table') {
3303                      my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3304                      if (defined $parent and $parent->node_type == 1) {
3305                        $foster_parent_element = $parent;
3306                        $next_sibling = $self->{open_elements}->[$_]->[0];
3307                        $prev_sibling = $next_sibling->previous_sibling;
3308                      } else {
3309                        $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
3310                        $prev_sibling = $foster_parent_element->last_child;
3311                      }
3312                      last OE;
3313                    }
3314                  } # OE
3315                  $foster_parent_element = $self->{open_elements}->[0]->[0] and
3316                  $prev_sibling = $foster_parent_element->last_child
3317                    unless defined $foster_parent_element;
3318                  if (defined $prev_sibling and
3319                      $prev_sibling->node_type == 3) {
3320                    $prev_sibling->manakai_append_text ($token->{data});
3321                  } else {
3322                    $foster_parent_element->insert_before
3323                      ($self->{document}->create_text_node ($token->{data}),
3324                       $next_sibling);
3325                  }
3326                } else {
3327                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3328                }
3329                
3330                !!!next-token;
3331                redo B;
3332              } elsif ($token->{type} eq 'start tag') {
3333                if ({
3334                     tr => ($self->{insertion_mode} ne 'in row'),
3335                     th => 1, td => 1,
3336                    }->{$token->{tag_name}}) {
3337                  if ($self->{insertion_mode} eq 'in table') {
3338                    ## Clear back to table context
3339                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3340                           $self->{open_elements}->[-1]->[1] ne 'html') {
3341                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3342                      pop @{$self->{open_elements}};
3343                    }
3344                    
3345                    !!!insert-element ('tbody');
3346                    $self->{insertion_mode} = 'in table body';
3347                    ## reprocess in the "in table body" insertion mode...
3348                  }
3349    
3350                  if ($self->{insertion_mode} eq 'in table body') {
3351                    unless ($token->{tag_name} eq 'tr') {
3352                      !!!parse-error (type => 'missing start tag:tr');
3353                    }
3354                    
3355                    ## Clear back to table body context
3356                    while (not {
3357                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3358                    }->{$self->{open_elements}->[-1]->[1]}) {
3359                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3360                      pop @{$self->{open_elements}};
3361                    }
3362                    
3363                    $self->{insertion_mode} = 'in row';
3364                    if ($token->{tag_name} eq 'tr') {
3365                      !!!insert-element ($token->{tag_name}, $token->{attributes});
3366                      !!!next-token;
3367                      redo B;
3368                    } else {
3369                      !!!insert-element ('tr');
3370                      ## reprocess in the "in row" insertion mode
3371                    }
3372                  }
3373    
3374                  ## Clear back to table row context
3375                  while (not {
3376                    tr => 1, html => 1,
3377                  }->{$self->{open_elements}->[-1]->[1]}) {
3378                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3379                    pop @{$self->{open_elements}};
3380                  }
3381                  
3382                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3383                  $self->{insertion_mode} = 'in cell';
3384    
3385                  push @$active_formatting_elements, ['#marker', ''];
3386                  
3387                  !!!next-token;
3388                  redo B;
3389                } elsif ({
3390                          caption => 1, col => 1, colgroup => 1,
3391                          tbody => 1, tfoot => 1, thead => 1,
3392                          tr => 1, # $self->{insertion_mode} eq 'in row'
3393                         }->{$token->{tag_name}}) {
3394                  if ($self->{insertion_mode} eq 'in row') {
3395                    ## As if </tr>
3396                    ## have an element in table scope
3397                    my $i;
3398                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3399                      my $node = $self->{open_elements}->[$_];
3400                      if ($node->[1] eq 'tr') {
3401                        $i = $_;
3402                        last INSCOPE;
3403                      } elsif ({
3404                                table => 1, html => 1,
3405                               }->{$node->[1]}) {
3406                        last INSCOPE;
3407                      }
3408                    } # INSCOPE
3409                    unless (defined $i) {
3410                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
3411                      ## Ignore the token
3412                      !!!next-token;
3413                      redo B;
3414                    }
3415                    
3416                    ## Clear back to table row context
3417                    while (not {
3418                      tr => 1, html => 1,
3419                    }->{$self->{open_elements}->[-1]->[1]}) {
3420                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3421                      pop @{$self->{open_elements}};
3422                    }
3423                    
3424                    pop @{$self->{open_elements}}; # tr
3425                    $self->{insertion_mode} = 'in table body';
3426                    if ($token->{tag_name} eq 'tr') {
3427                      ## reprocess
3428                      redo B;
3429                    } else {
3430                      ## reprocess in the "in table body" insertion mode...
3431                    }
3432                  }
3433    
3434                  if ($self->{insertion_mode} eq 'in table body') {
3435                    ## have an element in table scope
3436                    my $i;
3437                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3438                      my $node = $self->{open_elements}->[$_];
3439                      if ({
3440                           tbody => 1, thead => 1, tfoot => 1,
3441                          }->{$node->[1]}) {
3442                        $i = $_;
3443                        last INSCOPE;
3444                      } elsif ({
3445                                table => 1, html => 1,
3446                               }->{$node->[1]}) {
3447                        last INSCOPE;
3448                      }
3449                    } # INSCOPE
3450                    unless (defined $i) {
3451                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3452                      ## Ignore the token
3453                      !!!next-token;
3454                      redo B;
3455                    }
3456    
3457                    ## Clear back to table body context
3458                    while (not {
3459                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3460                    }->{$self->{open_elements}->[-1]->[1]}) {
3461                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3462                      pop @{$self->{open_elements}};
3463                    }
3464                    
3465                    ## As if <{current node}>
3466                    ## have an element in table scope
3467                    ## true by definition
3468                    
3469                    ## Clear back to table body context
3470                    ## nop by definition
3471                    
3472                    pop @{$self->{open_elements}};
3473                    $self->{insertion_mode} = 'in table';
3474                    ## reprocess in "in table" insertion mode...
3475                  }
3476    
3477                  if ($token->{tag_name} eq 'col') {
3478                    ## Clear back to table context
3479                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3480                           $self->{open_elements}->[-1]->[1] ne 'html') {
3481                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3482                      pop @{$self->{open_elements}};
3483                    }
3484                    
3485                    !!!insert-element ('colgroup');
3486                    $self->{insertion_mode} = 'in column group';
3487                    ## reprocess
3488                    redo B;
3489                  } elsif ({
3490                            caption => 1,
3491                            colgroup => 1,
3492                            tbody => 1, tfoot => 1, thead => 1,
3493                           }->{$token->{tag_name}}) {
3494                    ## Clear back to table context
3495                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3496                           $self->{open_elements}->[-1]->[1] ne 'html') {
3497                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3498                      pop @{$self->{open_elements}};
3499                    }
3500                    
3501                    push @$active_formatting_elements, ['#marker', '']
3502                        if $token->{tag_name} eq 'caption';
3503                    
3504                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3505                    $self->{insertion_mode} = {
3506                                               caption => 'in caption',
3507                                               colgroup => 'in column group',
3508                                               tbody => 'in table body',
3509                                               tfoot => 'in table body',
3510                                               thead => 'in table body',
3511                                              }->{$token->{tag_name}};
3512                    !!!next-token;
3513                    redo B;
3514                  } else {
3515                    die "$0: in table: <>: $token->{tag_name}";
3516                  }
3517                } elsif ($token->{tag_name} eq 'table') {
3518                  ## NOTE: There are code clones for this "table in table"
3519                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3520    
3521                  ## As if </table>
3522                  ## have a table element in table scope
3523                  my $i;
3524                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3525                    my $node = $self->{open_elements}->[$_];
3526                    if ($node->[1] eq 'table') {
3527                      $i = $_;
3528                      last INSCOPE;
3529                    } elsif ({
3530                              table => 1, html => 1,
3531                             }->{$node->[1]}) {
3532                      last INSCOPE;
3533                    }
3534                  } # INSCOPE
3535                  unless (defined $i) {
3536                    !!!parse-error (type => 'unmatched end tag:table');
3537                    ## Ignore tokens </table><table>
3538                    !!!next-token;
3539                    redo B;
3540                  }
3541                  
3542                  ## generate implied end tags
3543                  if ({
3544                       dd => 1, dt => 1, li => 1, p => 1,
3545                       td => 1, th => 1, tr => 1,
3546                       tbody => 1, tfoot=> 1, thead => 1,
3547                      }->{$self->{open_elements}->[-1]->[1]}) {
3548                    !!!back-token; # <table>
3549                    $token = {type => 'end tag', tag_name => 'table'};
3550                    !!!back-token;
3551                    $token = {type => 'end tag',
3552                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3553                    redo B;
3554                  }
3555    
3556                  if ($self->{open_elements}->[-1]->[1] ne 'table') {
3557                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3558                  }
3559    
3560                  splice @{$self->{open_elements}}, $i;
3561    
3562                  $self->_reset_insertion_mode;
3563    
3564                  ## reprocess
3565                  redo B;
3566                } else {
3567                  #
3568                }
3569              } elsif ($token->{type} eq 'end tag') {
3570                if ($token->{tag_name} eq 'tr' and
3571                    $self->{insertion_mode} eq 'in row') {
3572                  ## have an element in table scope
3573                  my $i;
3574                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3575                    my $node = $self->{open_elements}->[$_];
3576                    if ($node->[1] eq $token->{tag_name}) {
3577                      $i = $_;
3578                      last INSCOPE;
3579                    } elsif ({
3580                              table => 1, html => 1,
3581                             }->{$node->[1]}) {
3582                      last INSCOPE;
3583                    }
3584                  } # INSCOPE
3585                  unless (defined $i) {
3586                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3587                    ## Ignore the token
3588                    !!!next-token;
3589                    redo B;
3590                  }
3591    
3592                  ## Clear back to table row context
3593                  while (not {
3594                    tr => 1, html => 1,
3595                  }->{$self->{open_elements}->[-1]->[1]}) {
3596                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3597                    pop @{$self->{open_elements}};
3598                  }
3599    
3600                  pop @{$self->{open_elements}}; # tr
3601                  $self->{insertion_mode} = 'in table body';
3602                  !!!next-token;
3603                  redo B;
3604                } elsif ($token->{tag_name} eq 'table') {
3605                  if ($self->{insertion_mode} eq 'in row') {
3606                    ## As if </tr>
3607                    ## have an element in table scope
3608                    my $i;
3609                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3610                      my $node = $self->{open_elements}->[$_];
3611                      if ($node->[1] eq 'tr') {
3612                        $i = $_;
3613                        last INSCOPE;
3614                      } elsif ({
3615                                table => 1, html => 1,
3616                               }->{$node->[1]}) {
3617                        last INSCOPE;
3618                      }
3619                    } # INSCOPE
3620                    unless (defined $i) {
3621                      !!!parse-error (type => 'unmatched end tag:'.$token->{type});
3622                      ## Ignore the token
3623                      !!!next-token;
3624                      redo B;
3625                    }
3626                    
3627                    ## Clear back to table row context
3628                    while (not {
3629                      tr => 1, html => 1,
3630                    }->{$self->{open_elements}->[-1]->[1]}) {
3631                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3632                      pop @{$self->{open_elements}};
3633                    }
3634                    
3635                    pop @{$self->{open_elements}}; # tr
3636                    $self->{insertion_mode} = 'in table body';
3637                    ## reprocess in the "in table body" insertion mode...
3638                  }
3639    
3640                  if ($self->{insertion_mode} eq 'in table body') {
3641                    ## have an element in table scope
3642                    my $i;
3643                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3644                      my $node = $self->{open_elements}->[$_];
3645                      if ({
3646                           tbody => 1, thead => 1, tfoot => 1,
3647                          }->{$node->[1]}) {
3648                        $i = $_;
3649                        last INSCOPE;
3650                      } elsif ({
3651                                table => 1, html => 1,
3652                               }->{$node->[1]}) {
3653                        last INSCOPE;
3654                      }
3655                    } # INSCOPE
3656                    unless (defined $i) {
3657                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3658                      ## Ignore the token
3659                      !!!next-token;
3660                      redo B;
3661                    }
3662                    
3663                    ## Clear back to table body context
3664                    while (not {
3665                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3666                    }->{$self->{open_elements}->[-1]->[1]}) {
3667                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3668                      pop @{$self->{open_elements}};
3669                    }
3670                    
3671                    ## As if <{current node}>
3672                    ## have an element in table scope
3673                    ## true by definition
3674                    
3675                    ## Clear back to table body context
3676                    ## nop by definition
3677                    
3678                    pop @{$self->{open_elements}};
3679                    $self->{insertion_mode} = 'in table';
3680                    ## reprocess in the "in table" insertion mode...
3681                  }
3682    
3683                  ## have a table element in table scope
3684                  my $i;
3685                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3686                    my $node = $self->{open_elements}->[$_];
3687                    if ($node->[1] eq $token->{tag_name}) {
3688                      $i = $_;
3689                      last INSCOPE;
3690                    } elsif ({
3691                              table => 1, html => 1,
3692                             }->{$node->[1]}) {
3693                      last INSCOPE;
3694                    }
3695                  } # INSCOPE
3696                  unless (defined $i) {
3697                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3698                    ## Ignore the token
3699                    !!!next-token;
3700                    redo B;
3701                  }
3702    
3703                  ## generate implied end tags
3704                  if ({
3705                       dd => 1, dt => 1, li => 1, p => 1,
3706                       td => 1, th => 1, tr => 1,
3707                       tbody => 1, tfoot=> 1, thead => 1,
3708                      }->{$self->{open_elements}->[-1]->[1]}) {
3709                    !!!back-token;
3710                    $token = {type => 'end tag',
3711                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3712                    redo B;
3713                  }
3714                  
3715                  if ($self->{open_elements}->[-1]->[1] ne 'table') {
3716                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3717                  }
3718                    
3719                  splice @{$self->{open_elements}}, $i;
3720                  
3721                  $self->_reset_insertion_mode;
3722                  
3723                  !!!next-token;
3724                  redo B;
3725                } elsif ({
3726                          tbody => 1, tfoot => 1, thead => 1,
3727                         }->{$token->{tag_name}} and
3728                         ($self->{insertion_mode} eq 'in row' or
3729                          $self->{insertion_mode} eq 'in table body')) {
3730                  if ($self->{insertion_mode} eq 'in row') {
3731                    ## have an element in table scope
3732                    my $i;
3733                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3734                      my $node = $self->{open_elements}->[$_];
3735                      if ($node->[1] eq $token->{tag_name}) {
3736                        $i = $_;
3737                        last INSCOPE;
3738                      } elsif ({
3739                                table => 1, html => 1,
3740                               }->{$node->[1]}) {
3741                        last INSCOPE;
3742                      }
3743                    } # INSCOPE
3744                      unless (defined $i) {
3745                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3746                        ## Ignore the token
3747                        !!!next-token;
3748                        redo B;
3749                      }
3750                    
3751                    ## As if </tr>
3752                    ## have an element in table scope
3753                    my $i;
3754                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3755                      my $node = $self->{open_elements}->[$_];
3756                      if ($node->[1] eq 'tr') {
3757                        $i = $_;
3758                        last INSCOPE;
3759                      } elsif ({
3760                                table => 1, html => 1,
3761                               }->{$node->[1]}) {
3762                        last INSCOPE;
3763                      }
3764                    } # INSCOPE
3765                      unless (defined $i) {
3766                        !!!parse-error (type => 'unmatched end tag:tr');
3767                        ## Ignore the token
3768                        !!!next-token;
3769                        redo B;
3770                      }
3771                    
3772                    ## Clear back to table row context
3773                    while (not {
3774                      tr => 1, html => 1,
3775                    }->{$self->{open_elements}->[-1]->[1]}) {
3776                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3777                      pop @{$self->{open_elements}};
3778                    }
3779                    
3780                    pop @{$self->{open_elements}}; # tr
3781                    $self->{insertion_mode} = 'in table body';
3782                    ## reprocess in the "in table body" insertion mode...
3783                  }
3784    
3785                  ## have an element in table scope
3786                  my $i;
3787                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3788                    my $node = $self->{open_elements}->[$_];
3789                    if ($node->[1] eq $token->{tag_name}) {
3790                      $i = $_;
3791                      last INSCOPE;
3792                    } elsif ({
3793                              table => 1, html => 1,
3794                             }->{$node->[1]}) {
3795                      last INSCOPE;
3796                    }
3797                  } # INSCOPE
3798                  unless (defined $i) {
3799                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3800                    ## Ignore the token
3801                    !!!next-token;
3802                    redo B;
3803                  }
3804    
3805                  ## Clear back to table body context
3806                  while (not {
3807                    tbody => 1, tfoot => 1, thead => 1, html => 1,
3808                  }->{$self->{open_elements}->[-1]->[1]}) {
3809                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3810                    pop @{$self->{open_elements}};
3811                  }
3812    
3813                  pop @{$self->{open_elements}};
3814                  $self->{insertion_mode} = 'in table';
3815                  !!!next-token;
3816                  redo B;
3817                } elsif ({
3818                          body => 1, caption => 1, col => 1, colgroup => 1,
3819                          html => 1, td => 1, th => 1,
3820                          tr => 1, # $self->{insertion_mode} eq 'in row'
3821                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} eq 'in table'
3822                         }->{$token->{tag_name}}) {
3823                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3824                  ## Ignore the token
3825                  !!!next-token;
3826                  redo B;
3827                } else {
3828                  #
3829                }
3830              } else {
3831                die "$0: $token->{type}: Unknown token type";
3832              }
3833    
3834          !!!parse-error (type => 'in table:'.$token->{tag_name});
3835    
3836          $insert = $insert_to_foster;
3837          #
3838        } elsif ($self->{insertion_mode} eq 'in column group') {
3839              if ($token->{type} eq 'character') {
3840                if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3841                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3842                  unless (length $token->{data}) {
3843                    !!!next-token;
3844                    redo B;
3845                  }
3846                }
3847                
3848                #
3849              } elsif ($token->{type} eq 'start tag') {
3850                if ($token->{tag_name} eq 'col') {
3851                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3852                  pop @{$self->{open_elements}};
3853                  !!!next-token;
3854                  redo B;
3855                } else {
3856                  #
3857                }
3858              } elsif ($token->{type} eq 'end tag') {
3859                if ($token->{tag_name} eq 'colgroup') {
3860                  if ($self->{open_elements}->[-1]->[1] eq 'html') {
3861                    !!!parse-error (type => 'unmatched end tag:colgroup');
3862                    ## Ignore the token
3863                    !!!next-token;
3864                    redo B;
3865                  } else {
3866                    pop @{$self->{open_elements}}; # colgroup
3867                    $self->{insertion_mode} = 'in table';
3868                    !!!next-token;
3869                    redo B;            
3870                  }
3871                } elsif ($token->{tag_name} eq 'col') {
3872                  !!!parse-error (type => 'unmatched end tag:col');
3873                  ## Ignore the token
3874                  !!!next-token;
3875                  redo B;
3876                } else {
3877                  #
3878                }
3879              } else {
3880                #
3881              }
3882    
3883              ## As if </colgroup>
3884              if ($self->{open_elements}->[-1]->[1] eq 'html') {
3885                !!!parse-error (type => 'unmatched end tag:colgroup');
3886                ## Ignore the token
3887                !!!next-token;
3888                redo B;
3889              } else {
3890                pop @{$self->{open_elements}}; # colgroup
3891                $self->{insertion_mode} = 'in table';
3892                ## reprocess
3893                redo B;
3894              }
3895        } elsif ($self->{insertion_mode} eq 'in select') {
3896              if ($token->{type} eq 'character') {
3897                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3898                !!!next-token;
3899                redo B;
3900              } elsif ($token->{type} eq 'start tag') {
3901                if ($token->{tag_name} eq 'option') {
3902                  if ($self->{open_elements}->[-1]->[1] eq 'option') {
3903                    ## As if </option>
3904                    pop @{$self->{open_elements}};
3905                  }
3906    
3907                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3908                  !!!next-token;
3909                  redo B;
3910                } elsif ($token->{tag_name} eq 'optgroup') {
3911                  if ($self->{open_elements}->[-1]->[1] eq 'option') {
3912                    ## As if </option>
3913                    pop @{$self->{open_elements}};
3914                  }
3915    
3916                  if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
3917                    ## As if </optgroup>
3918                    pop @{$self->{open_elements}};
3919                  }
3920    
3921                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3922                  !!!next-token;
3923                  redo B;
3924                } elsif ($token->{tag_name} eq 'select') {
3925                  !!!parse-error (type => 'not closed:select');
3926                  ## As if </select> instead
3927                  ## have an element in table scope
3928                  my $i;
3929                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3930                    my $node = $self->{open_elements}->[$_];
3931                    if ($node->[1] eq $token->{tag_name}) {
3932                      $i = $_;
3933                      last INSCOPE;
3934                    } elsif ({
3935                              table => 1, html => 1,
3936                             }->{$node->[1]}) {
3937                      last INSCOPE;
3938                    }
3939                  } # INSCOPE
3940                  unless (defined $i) {
3941                    !!!parse-error (type => 'unmatched end tag:select');
3942                    ## Ignore the token
3943                    !!!next-token;
3944                    redo B;
3945                  }
3946                  
3947                  splice @{$self->{open_elements}}, $i;
3948    
3949                  $self->_reset_insertion_mode;
3950    
3951                  !!!next-token;
3952                  redo B;
3953                } else {
3954                  #
3955                }
3956              } elsif ($token->{type} eq 'end tag') {
3957                if ($token->{tag_name} eq 'optgroup') {
3958                  if ($self->{open_elements}->[-1]->[1] eq 'option' and
3959                      $self->{open_elements}->[-2]->[1] eq 'optgroup') {
3960                    ## As if </option>
3961                    splice @{$self->{open_elements}}, -2;
3962                  } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
3963                    pop @{$self->{open_elements}};
3964                  } else {
3965                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3966                    ## Ignore the token
3967                  }
3968                  !!!next-token;
3969                  redo B;
3970                } elsif ($token->{tag_name} eq 'option') {
3971                  if ($self->{open_elements}->[-1]->[1] eq 'option') {
3972                    pop @{$self->{open_elements}};
3973                  } else {
3974                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3975                    ## Ignore the token
3976                  }
3977                  !!!next-token;
3978                  redo B;
3979                } elsif ($token->{tag_name} eq 'select') {
3980                  ## have an element in table scope
3981                  my $i;
3982                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3983                    my $node = $self->{open_elements}->[$_];
3984                    if ($node->[1] eq $token->{tag_name}) {
3985                      $i = $_;
3986                      last INSCOPE;
3987                    } elsif ({
3988                              table => 1, html => 1,
3989                             }->{$node->[1]}) {
3990                      last INSCOPE;
3991                    }
3992                  } # INSCOPE
3993                  unless (defined $i) {
3994                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3995                    ## Ignore the token
3996                    !!!next-token;
3997                    redo B;
3998                  }
3999                  
4000                  splice @{$self->{open_elements}}, $i;
4001    
4002                  $self->_reset_insertion_mode;
4003    
4004                  !!!next-token;
4005                  redo B;
4006                } elsif ({
4007                          caption => 1, table => 1, tbody => 1,
4008                          tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
4009                         }->{$token->{tag_name}}) {
4010                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4011                  
4012                  ## have an element in table scope
4013                  my $i;
4014                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4015                    my $node = $self->{open_elements}->[$_];
4016                    if ($node->[1] eq $token->{tag_name}) {
4017                      $i = $_;
4018                      last INSCOPE;
4019                    } elsif ({
4020                              table => 1, html => 1,
4021                             }->{$node->[1]}) {
4022                      last INSCOPE;
4023                    }
4024                  } # INSCOPE
4025                  unless (defined $i) {
4026                    ## Ignore the token
4027                    !!!next-token;
4028                    redo B;
4029                  }
4030                  
4031                  ## As if </select>
4032                  ## have an element in table scope
4033                  undef $i;
4034                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4035                    my $node = $self->{open_elements}->[$_];
4036                    if ($node->[1] eq 'select') {
4037                      $i = $_;
4038                      last INSCOPE;
4039                    } elsif ({
4040                              table => 1, html => 1,
4041                             }->{$node->[1]}) {
4042                      last INSCOPE;
4043                    }
4044                  } # INSCOPE
4045                  unless (defined $i) {
4046                    !!!parse-error (type => 'unmatched end tag:select');
4047                    ## Ignore the </select> token
4048                    !!!next-token; ## TODO: ok?
4049                    redo B;
4050                  }
4051                  
4052                  splice @{$self->{open_elements}}, $i;
4053    
4054                  $self->_reset_insertion_mode;
4055    
4056                  ## reprocess
4057                  redo B;
4058                } else {
4059                  #
4060                }
4061              } else {
4062                #
4063              }
4064    
4065              !!!parse-error (type => 'in select:'.$token->{tag_name});
4066              ## Ignore the token
4067              !!!next-token;
4068              redo B;
4069        } elsif ($self->{insertion_mode} eq 'after body' or
4070                 $self->{insertion_mode} eq 'after html body') {
4071          if ($token->{type} eq 'character') {
4072            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4073              my $data = $1;
4074              ## As if in body
4075              $reconstruct_active_formatting_elements->($insert_to_current);
4076                  
4077              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4078              
4079              unless (length $token->{data}) {
4080                !!!next-token;
4081                redo B;
4082              }
4083            }
4084            
4085            if ($self->{insertion_mode} eq 'after html body') {
4086              !!!parse-error (type => 'after html:#character');
4087    
4088              ## Reprocess in the "main" phase, "after body" insertion mode...
4089            }
4090            
4091            ## "after body" insertion mode
4092            !!!parse-error (type => 'after body:#character');
4093    
4094            $self->{insertion_mode} = 'in body';
4095            ## reprocess
4096            redo B;
4097          } elsif ($token->{type} eq 'start tag') {
4098            if ($self->{insertion_mode} eq 'after html body') {
4099              !!!parse-error (type => 'after html:'.$token->{tag_name});
4100              
4101              ## Reprocess in the "main" phase, "after body" insertion mode...
4102            }
4103    
4104            ## "after body" insertion mode
4105            !!!parse-error (type => 'after body:'.$token->{tag_name});
4106    
4107            $self->{insertion_mode} = 'in body';
4108            ## reprocess
4109            redo B;
4110          } elsif ($token->{type} eq 'end tag') {
4111            if ($self->{insertion_mode} eq 'after html body') {
4112              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4113              
4114              $self->{insertion_mode} = 'after body';
4115              ## Reprocess in the "main" phase, "after body" insertion mode...
4116            }
4117    
4118            ## "after body" insertion mode
4119            if ($token->{tag_name} eq 'html') {
4120              if (defined $self->{inner_html_node}) {
4121                !!!parse-error (type => 'unmatched end tag:html');
4122                ## Ignore the token
4123                !!!next-token;
4124                redo B;
4125              } else {
4126                $self->{insertion_mode} = 'after html body';
4127                !!!next-token;
4128                redo B;
4129              }
4130            } else {
4131              !!!parse-error (type => 'after body:/'.$token->{tag_name});
4132    
4133              $self->{insertion_mode} = 'in body';
4134              ## reprocess
4135              redo B;
4136            }
4137          } else {
4138            die "$0: $token->{type}: Unknown token type";
4139          }
4140        } elsif ($self->{insertion_mode} eq 'in frameset' or
4141                 $self->{insertion_mode} eq 'after frameset' or
4142                 $self->{insertion_mode} eq 'after html frameset') {
4143          if ($token->{type} eq 'character') {
4144            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4145              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4146              
4147              unless (length $token->{data}) {
4148                !!!next-token;
4149                redo B;
4150              }
4151            }
4152            
4153            if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
4154              if ($self->{insertion_mode} eq 'in frameset') {
4155                !!!parse-error (type => 'in frameset:#character');
4156              } elsif ($self->{insertion_mode} eq 'after frameset') {
4157                !!!parse-error (type => 'after frameset:#character');
4158              } else { # "after html frameset"
4159                !!!parse-error (type => 'after html:#character');
4160    
4161                $self->{insertion_mode} = 'after frameset';
4162                ## Reprocess in the "main" phase, "after frameset"...
4163                !!!parse-error (type => 'after frameset:#character');
4164              }
4165              
4166              ## Ignore the token.
4167              if (length $token->{data}) {
4168                ## reprocess the rest of characters
4169              } else {
4170                !!!next-token;
4171              }
4172              redo B;
4173            }
4174            
4175            die qq[$0: Character "$token->{data}"];
4176          } elsif ($token->{type} eq 'start tag') {
4177            if ($self->{insertion_mode} eq 'after html frameset') {
4178              !!!parse-error (type => 'after html:'.$token->{tag_name});
4179    
4180              $self->{insertion_mode} = 'after frameset';
4181              ## Process in the "main" phase, "after frameset" insertion mode...
4182            }
4183    
4184            if ($token->{tag_name} eq 'frameset' and
4185                $self->{insertion_mode} eq 'in frameset') {
4186              !!!insert-element ($token->{tag_name}, $token->{attributes});
4187              !!!next-token;
4188              redo B;
4189            } elsif ($token->{tag_name} eq 'frame' and
4190                     $self->{insertion_mode} eq 'in frameset') {
4191              !!!insert-element ($token->{tag_name}, $token->{attributes});
4192              pop @{$self->{open_elements}};
4193              !!!next-token;
4194              redo B;
4195            } elsif ($token->{tag_name} eq 'noframes') {
4196              ## NOTE: As if in body.
4197              $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4198              redo B;
4199            } else {
4200              if ($self->{insertion_mode} eq 'in frameset') {
4201                !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4202              } else {
4203                !!!parse-error (type => 'after frameset:'.$token->{tag_name});
4204              }
4205              ## Ignore the token
4206              !!!next-token;
4207              redo B;
4208            }
4209          } elsif ($token->{type} eq 'end tag') {
4210            if ($self->{insertion_mode} eq 'after html frameset') {
4211              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4212    
4213              $self->{insertion_mode} = 'after frameset';
4214              ## Process in the "main" phase, "after frameset" insertion mode...
4215            }
4216    
4217            if ($token->{tag_name} eq 'frameset' and
4218                $self->{insertion_mode} eq 'in frameset') {
4219              if ($self->{open_elements}->[-1]->[1] eq 'html' and
4220                  @{$self->{open_elements}} == 1) {
4221                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4222                ## Ignore the token
4223                !!!next-token;
4224              } else {
4225                pop @{$self->{open_elements}};
4226                !!!next-token;
4227              }
4228    
4229              if (not defined $self->{inner_html_node} and
4230                  $self->{open_elements}->[-1]->[1] ne 'frameset') {
4231                $self->{insertion_mode} = 'after frameset';
4232              }
4233              redo B;
4234            } elsif ($token->{tag_name} eq 'html' and
4235                     $self->{insertion_mode} eq 'after frameset') {
4236              $self->{insertion_mode} = 'after html frameset';
4237              !!!next-token;
4238              redo B;
4239            } else {
4240              if ($self->{insertion_mode} eq 'in frameset') {
4241                !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
4242              } else {
4243                !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
4244              }
4245              ## Ignore the token
4246              !!!next-token;
4247              redo B;
4248            }
4249          } else {
4250            die "$0: $token->{type}: Unknown token type";
4251          }
4252    
4253          ## ISSUE: An issue in spec here
4254        } else {
4255          die "$0: $self->{insertion_mode}: Unknown insertion mode";
4256        }
4257    
4258        ## "in body" insertion mode
4259    my $in_body = sub {    my $in_body = sub {
     my $insert = shift;  
4260      if ($token->{type} eq 'start tag') {      if ($token->{type} eq 'start tag') {
4261        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
4262          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
# Line 2494  sub _tree_construction_main ($) { Line 4264  sub _tree_construction_main ($) {
4264          return;          return;
4265        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
4266          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
4267          $parse_rcdata->('CDATA', $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4268          return;          return;
4269        } elsif ({        } elsif ({
4270                  base => 1, link => 1,                  base => 1, link => 1,
# Line 2531  sub _tree_construction_main ($) { Line 4301  sub _tree_construction_main ($) {
4301        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
4302          !!!parse-error (type => 'in body:title');          !!!parse-error (type => 'in body:title');
4303          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
4304          $parse_rcdata->('RCDATA', sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
4305            if (defined $self->{head_element}) {            if (defined $self->{head_element}) {
4306              $self->{head_element}->append_child ($_[0]);              $self->{head_element}->append_child ($_[0]);
4307            } else {            } else {
# Line 2727  sub _tree_construction_main ($) { Line 4497  sub _tree_construction_main ($) {
4497                        
4498          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4499                        
4500          $self->{content_model_flag} = 'PLAINTEXT';          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
4501                        
4502          !!!next-token;          !!!next-token;
4503          return;          return;
# Line 2882  sub _tree_construction_main ($) { Line 4652  sub _tree_construction_main ($) {
4652          return;          return;
4653        } elsif ($token->{tag_name} eq 'xmp') {        } elsif ($token->{tag_name} eq 'xmp') {
4654          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4655          $parse_rcdata->('CDATA', $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4656          return;          return;
4657        } elsif ($token->{tag_name} eq 'table') {        } elsif ($token->{tag_name} eq 'table') {
4658          ## has a p element in scope          ## has a p element in scope
# Line 2998  sub _tree_construction_main ($) { Line 4768  sub _tree_construction_main ($) {
4768          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $token->{tag_name}, $token->{attributes});
4769                    
4770          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
4771          $self->{content_model_flag} = 'RCDATA';          $self->{content_model} = RCDATA_CONTENT_MODEL;
4772          delete $self->{escape}; # MUST          delete $self->{escape}; # MUST
4773                    
4774          $insert->($el);          $insert->($el);
# Line 3019  sub _tree_construction_main ($) { Line 4789  sub _tree_construction_main ($) {
4789            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
4790          }          }
4791                    
4792          $self->{content_model_flag} = 'PCDATA';          $self->{content_model} = PCDATA_CONTENT_MODEL;
4793                    
4794          if ($token->{type} eq 'end tag' and          if ($token->{type} eq 'end tag' and
4795              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
# Line 3035  sub _tree_construction_main ($) { Line 4805  sub _tree_construction_main ($) {
4805                  noframes => 1,                  noframes => 1,
4806                  noscript => 0, ## TODO: 1 if scripting is enabled                  noscript => 0, ## TODO: 1 if scripting is enabled
4807                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
4808          $parse_rcdata->('CDATA', $insert);          ## NOTE: There are two "as if in body" code clones.
4809            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4810          return;          return;
4811        } elsif ($token->{tag_name} eq 'select') {        } elsif ($token->{tag_name} eq 'select') {
4812          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
# Line 3329  sub _tree_construction_main ($) { Line 5100  sub _tree_construction_main ($) {
5100        }        }
5101      }      }
5102    }; # $in_body    }; # $in_body
5103        $in_body->($insert_to_current);
5104    B: {      redo B;
     if ($self->{insertion_mode} ne 'trailing end') {  
       if ($token->{type} eq 'DOCTYPE') {  
         !!!parse-error (type => 'in html:#DOCTYPE');  
         ## Ignore the token  
         ## Stay in the phase  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'start tag' and  
                $token->{tag_name} eq 'html') {  
 ## ISSUE: "aa<html>" is not a parse error.  
 ## ISSUE: "<html>" in fragment is not a parse error.  
         unless ($token->{first_start_tag}) {  
           !!!parse-error (type => 'not first start tag');  
         }  
         my $top_el = $self->{open_elements}->[0]->[0];  
         for my $attr_name (keys %{$token->{attributes}}) {  
           unless ($top_el->has_attribute_ns (undef, $attr_name)) {  
             $top_el->set_attribute_ns  
               (undef, [undef, $attr_name],  
                $token->{attributes}->{$attr_name}->{value});  
           }  
         }  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'end-of-file') {  
         ## Generate implied end tags  
         if ({  
              dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,  
              tbody => 1, tfoot=> 1, thead => 1,  
             }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!back-token;  
           $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};  
           redo B;  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!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') {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
   
         ## Stop parsing  
         last B;  
   
         ## ISSUE: There is an issue in the spec.  
       } else {  
         if ($self->{insertion_mode} eq 'before head') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
             ## As if <head>  
             !!!create-element ($self->{head_element}, 'head');  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
             ## reprocess  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};  
             !!!create-element ($self->{head_element}, 'head', $attr);  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
             if ($token->{tag_name} eq 'head') {  
               !!!next-token;  
             #} elsif ({  
             #          base => 1, link => 1, meta => 1,  
             #          script => 1, style => 1, title => 1,  
             #         }->{$token->{tag_name}}) {  
             #  ## reprocess  
             } else {  
               ## reprocess  
             }  
             redo B;  
           } elsif ($token->{type} eq 'end tag') {  
             if ({  
                  head => 1, body => 1, html => 1,  
                  p => 1, br => 1,  
                 }->{$token->{tag_name}}) {  
               ## As if <head>  
               !!!create-element ($self->{head_element}, 'head');  
               $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
               push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               $self->{insertion_mode} = 'in head';  
               ## reprocess  
               redo B;  
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token ## ISSUE: An issue in the spec.  
               !!!next-token;  
               redo B;  
             }  
           } else {  
             die "$0: $token->{type}: Unknown type";  
           }  
         } elsif ($self->{insertion_mode} eq 'in head' or  
                  $self->{insertion_mode} eq 'in head noscript' or  
                  $self->{insertion_mode} eq 'after head') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({base => ($self->{insertion_mode} eq 'in head' or  
                           $self->{insertion_mode} eq 'after head'),  
                  link => 1}->{$token->{tag_name}}) {  
               ## NOTE: There is a "as if in head" code clone.  
               if ($self->{insertion_mode} eq 'after head') {  
                 !!!parse-error (type => 'after head:'.$token->{tag_name});  
                 push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               }  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.  
               pop @{$self->{open_elements}}  
                   if $self->{insertion_mode} eq 'after head';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'meta') {  
               ## NOTE: There is a "as if in head" code clone.  
               if ($self->{insertion_mode} eq 'after head') {  
                 !!!parse-error (type => 'after head:'.$token->{tag_name});  
                 push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               }  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.  
   
               unless ($self->{confident}) {  
                 my $charset;  
                 if ($token->{attributes}->{charset}) { ## TODO: And if supported  
                   $charset = $token->{attributes}->{charset}->{value};  
                 }  
                 if ($token->{attributes}->{'http-equiv'}) {  
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
                   if ($token->{attributes}->{'http-equiv'}->{value}  
                       =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=  
                           [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|  
                           ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {  
                     $charset = defined $1 ? $1 : defined $2 ? $2 : $3;  
                   } ## TODO: And if supported  
                 }  
                 ## TODO: Change the encoding  
               }  
   
               ## TODO: Extracting |charset| from |meta|.  
               pop @{$self->{open_elements}}  
                   if $self->{insertion_mode} eq 'after head';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'title' and  
                      $self->{insertion_mode} eq 'in head') {  
               ## NOTE: There is a "as if in head" code clone.  
               if ($self->{insertion_mode} eq 'after head') {  
                 !!!parse-error (type => 'after head:'.$token->{tag_name});  
                 push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               }  
               my $parent = defined $self->{head_element} ? $self->{head_element}  
                   : $self->{open_elements}->[-1]->[0];  
               $parse_rcdata->('RCDATA', sub { $parent->append_child ($_[0]) });  
               pop @{$self->{open_elements}}  
                   if $self->{insertion_mode} eq 'after head';  
               redo B;  
             } elsif ($token->{tag_name} eq 'style') {  
               ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and  
               ## insertion mode 'in head')  
               ## NOTE: There is a "as if in head" code clone.  
               if ($self->{insertion_mode} eq 'after head') {  
                 !!!parse-error (type => 'after head:'.$token->{tag_name});  
                 push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               }  
               $parse_rcdata->('CDATA', $insert_to_current);  
               pop @{$self->{open_elements}}  
                   if $self->{insertion_mode} eq 'after head';  
               redo B;  
             } elsif ($token->{tag_name} eq 'noscript') {  
               if ($self->{insertion_mode} eq 'in head') {  
                 ## NOTE: and scripting is disalbed  
                 !!!insert-element ($token->{tag_name}, $token->{attributes});  
                 $self->{insertion_mode} = 'in head noscript';  
                 !!!next-token;  
                 redo B;  
               } elsif ($self->{insertion_mode} eq 'in head noscript') {  
                 !!!parse-error (type => 'in noscript:noscript');  
                 ## Ignore the token  
                 redo B;  
               } else {  
                 #  
               }  
             } elsif ($token->{tag_name} eq 'head' and  
                      $self->{insertion_mode} ne 'after head') {  
               !!!parse-error (type => 'in head:head'); # or in head noscript  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } elsif ($self->{insertion_mode} ne 'in head noscript' and  
                      $token->{tag_name} eq 'script') {  
               if ($self->{insertion_mode} eq 'after head') {  
                 !!!parse-error (type => 'after head:'.$token->{tag_name});  
                 push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               }  
               ## NOTE: There is a "as if in head" code clone.  
               $script_start_tag->($insert_to_current);  
               pop @{$self->{open_elements}}  
                   if $self->{insertion_mode} eq 'after head';  
               redo B;  
             } elsif ($self->{insertion_mode} eq 'after head' and  
                      $token->{tag_name} eq 'body') {  
               !!!insert-element ('body', $token->{attributes});  
               $self->{insertion_mode} = 'in body';  
               !!!next-token;  
               redo B;  
             } elsif ($self->{insertion_mode} eq 'after head' and  
                      $token->{tag_name} eq 'frameset') {  
               !!!insert-element ('frameset', $token->{attributes});  
               $self->{insertion_mode} = 'in frameset';  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($self->{insertion_mode} eq 'in head' and  
                 $token->{tag_name} eq 'head') {  
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'after head';  
               !!!next-token;  
               redo B;  
             } elsif ($self->{insertion_mode} eq 'in head noscript' and  
                 $token->{tag_name} eq 'noscript') {  
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in head';  
               !!!next-token;  
               redo B;  
             } elsif ($self->{insertion_mode} eq 'in head' and  
                      {  
                       body => 1, html => 1,  
                       p => 1, br => 1,  
                      }->{$token->{tag_name}}) {  
               #  
             } elsif ($self->{insertion_mode} eq 'in head noscript' and  
                      {  
                       p => 1, br => 1,  
                      }->{$token->{tag_name}}) {  
               #  
             } elsif ($self->{insertion_mode} ne 'after head') {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           ## As if </head> or </noscript> or <body>  
           if ($self->{insertion_mode} eq 'in head') {  
             pop @{$self->{open_elements}};  
             $self->{insertion_mode} = 'after head';  
           } elsif ($self->{insertion_mode} eq 'in head noscript') {  
             pop @{$self->{open_elements}};  
             !!!parse-error (type => 'in noscript:'.(defined $token->{tag_name} ? ($token->{type} eq 'end tag' ? '/' : '') . $token->{tag_name} : '#' . $token->{type}));  
             $self->{insertion_mode} = 'in head';  
           } else { # 'after head'  
             !!!insert-element ('body');  
             $self->{insertion_mode} = 'in body';  
           }  
           ## reprocess  
           redo B;  
   
           ## ISSUE: An issue in the spec.  
         } elsif ($self->{insertion_mode} eq 'in body') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: There is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } else {  
             $in_body->($insert_to_current);  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There are "character in table" code clones.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1,  
                  colgroup => 1,  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$token->{tag_name}}) {  
               ## Clear back to table context  
               while ($self->{open_elements}->[-1]->[1] ne 'table' and  
                      $self->{open_elements}->[-1]->[1] ne 'html') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               push @$active_formatting_elements, ['#marker', '']  
                 if $token->{tag_name} eq 'caption';  
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = {  
                                  caption => 'in caption',  
                                  colgroup => 'in column group',  
                                  tbody => 'in table body',  
                                  tfoot => 'in table body',  
                                  thead => 'in table body',  
                                 }->{$token->{tag_name}};  
               !!!next-token;  
               redo B;  
             } elsif ({  
                       col => 1,  
                       td => 1, th => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## Clear back to table context  
               while ($self->{open_elements}->[-1]->[1] ne 'table' and  
                      $self->{open_elements}->[-1]->[1] ne 'html') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');  
               $self->{insertion_mode} = $token->{tag_name} eq 'col'  
                 ? 'in column group' : 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: There are code clones for this "table in table"  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
   
               ## As if </table>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'table') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:table');  
                 ## Ignore tokens </table><table>  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'table') {  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in caption') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: This is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1, col => 1, colgroup => 1, tbody => 1,  
                  td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,  
                 }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'not closed:caption');  
   
               ## As if </caption>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'caption') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:caption');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <?>  
                 $token = {type => 'end tag', tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'caption') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'caption') {  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'caption') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               !!!parse-error (type => 'not closed:caption');  
   
               ## As if </caption>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'caption') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:caption');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # </table>  
                 $token = {type => 'end tag', tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'caption') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, col => 1, colgroup => 1,  
                       html => 1, tbody => 1, td => 1, tfoot => 1,  
                       th => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
                 
           $in_body->($insert_to_current);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in column group') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'col') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'colgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html') {  
                 !!!parse-error (type => 'unmatched end tag:colgroup');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               } else {  
                 pop @{$self->{open_elements}}; # colgroup  
                 $self->{insertion_mode} = 'in table';  
                 !!!next-token;  
                 redo B;              
               }  
             } elsif ($token->{tag_name} eq 'col') {  
               !!!parse-error (type => 'unmatched end tag:col');  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           ## As if </colgroup>  
           if ($self->{open_elements}->[-1]->[1] eq 'html') {  
             !!!parse-error (type => 'unmatched end tag:colgroup');  
             ## Ignore the token  
             !!!next-token;  
             redo B;  
           } else {  
             pop @{$self->{open_elements}}; # colgroup  
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table body') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
   
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  tr => 1,  
                  th => 1, td => 1,  
                 }->{$token->{tag_name}}) {  
               unless ($token->{tag_name} eq 'tr') {  
                 !!!parse-error (type => 'missing start tag:tr');  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
               $self->{insertion_mode} = 'in row';  
               if ($token->{tag_name} eq 'tr') {  
                 !!!insert-element ($token->{tag_name}, $token->{attributes});  
                 !!!next-token;  
               } else {  
                 !!!insert-element ('tr');  
                 ## reprocess  
               }  
               redo B;  
             } elsif ({  
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ({  
                      tbody => 1, thead => 1, tfoot => 1,  
                     }->{$node->[1]}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               ## reprocess  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: This is a code clone of "table in table"  
               !!!parse-error (type => 'not closed:table');  
   
               ## As if </table>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'table') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:table');  
                 ## Ignore tokens </table><table>  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ({  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ({  
                      tbody => 1, thead => 1, tfoot => 1,  
                     }->{$node->[1]}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, td => 1, th => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           ## As if in table  
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in row') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'th' or  
                 $token->{tag_name} eq 'td') {  
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = 'in cell';  
   
               push @$active_formatting_elements, ['#marker', ''];  
                 
               !!!next-token;  
               redo B;  
             } elsif ({  
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: This is a code clone of "table in table"  
               !!!parse-error (type => 'not closed:table');  
   
               ## As if </table>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'table') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:table');  
                 ## Ignore tokens </table><table>  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'tr') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{type});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       tbody => 1, tfoot => 1, thead => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:tr');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1,  
                       colgroup => 1, html => 1, td => 1, th => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           ## As if in table  
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in cell') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: This is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1, col => 1, colgroup => 1,  
                  tbody => 1, td => 1, tfoot => 1, th => 1,  
                  thead => 1, tr => 1,  
                 }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $tn) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Close the cell  
               !!!back-token; # <?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => ($token->{tag_name} eq 'th'),  
                    th => ($token->{tag_name} eq 'td'),  
                    tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in row';  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1,  
                       colgroup => 1, html => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } elsif ({  
                       table => 1, tbody => 1, tfoot => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   ## NOTE: There is exactly one |td| or |th| element  
                   ## in scope in the stack of open elements by definition.  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           $in_body->($insert_to_current);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in select') {  
           if ($token->{type} eq 'character') {  
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'option') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 ## As if </option>  
                 pop @{$self->{open_elements}};  
               }  
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'optgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 ## As if </option>  
                 pop @{$self->{open_elements}};  
               }  
   
               if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {  
                 ## As if </optgroup>  
                 pop @{$self->{open_elements}};  
               }  
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'select') {  
               !!!parse-error (type => 'not closed:select');  
               ## As if </select> instead  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:select');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'optgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option' and  
                   $self->{open_elements}->[-2]->[1] eq 'optgroup') {  
                 ## As if </option>  
                 splice @{$self->{open_elements}}, -2;  
               } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {  
                 pop @{$self->{open_elements}};  
               } else {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
               }  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'option') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 pop @{$self->{open_elements}};  
               } else {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
               }  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'select') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       caption => 1, table => 1, tbody => 1,  
                       tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## As if </select>  
               ## have an element in table scope  
               undef $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'select') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:select');  
                 ## Ignore the </select> token  
                 !!!next-token; ## TODO: ok?  
                 redo B;  
               }  
                 
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in select:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'after body') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               my $data = $1;  
               ## As if in body  
               $reconstruct_active_formatting_elements->($insert_to_current);  
                 
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
   
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
             !!!parse-error (type => 'after body:#'.$token->{type});  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[0]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             !!!parse-error (type => 'after body:'.$token->{tag_name});  
             #  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'html') {  
               if (defined $self->{inner_html_node}) {  
                 !!!parse-error (type => 'unmatched end tag:html');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               } else {  
                 $previous_insertion_mode = $self->{insertion_mode};  
                 $self->{insertion_mode} = 'trailing end';  
                 !!!next-token;  
                 redo B;  
               }  
             } else {  
               !!!parse-error (type => 'after body:/'.$token->{tag_name});  
             }  
           } else {  
             !!!parse-error (type => 'after body:#'.$token->{type});  
           }  
   
           $self->{insertion_mode} = 'in body';  
           ## reprocess  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in frameset') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
   
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'frameset') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'frame') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'noframes') {  
               $in_body->($insert_to_current);  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'frameset') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html' and  
                   @{$self->{open_elements}} == 1) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
               } else {  
                 pop @{$self->{open_elements}};  
                 !!!next-token;  
               }  
                 
               ## if not inner_html and  
               if ($self->{open_elements}->[-1]->[1] ne 'frameset') {  
                 $self->{insertion_mode} = 'after frameset';  
               }  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           if (defined $token->{tag_name}) {  
             !!!parse-error (type => 'in frameset:'.($token->{type} eq 'end tag' ? '/' : '').$token->{tag_name});  
           } else {  
             !!!parse-error (type => 'in frameset:#'.$token->{type});  
           }  
           ## Ignore the token  
           !!!next-token;  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'after frameset') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
   
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {  
               !!!parse-error (type => 'after frameset:#character');  
   
               ## Ignore the token.  
               if (length $token->{data}) {  
                 ## reprocess the rest of characters  
               } else {  
                 !!!next-token;  
               }  
               redo B;  
             }  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'noframes') {  
               $in_body->($insert_to_current);  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'html') {  
               $previous_insertion_mode = $self->{insertion_mode};  
               $self->{insertion_mode} = 'trailing end';  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             die "$0: $token->{type}: Unknown token type";  
           }  
             
           !!!parse-error (type => 'after frameset:'.($token->{tag_name} eq 'end tag' ? '/' : '').$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           redo B;  
   
           ## ISSUE: An issue in spec there  
         } else {  
           die "$0: $self->{insertion_mode}: Unknown insertion mode";  
         }  
       }  
     } elsif ($self->{insertion_mode} eq 'trailing end') {  
       ## states in the main stage is preserved yet # MUST  
         
       if ($token->{type} eq 'DOCTYPE') {  
         !!!parse-error (type => 'after html:#DOCTYPE');  
         ## Ignore the token  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'comment') {  
         my $comment = $self->{document}->create_comment ($token->{data});  
         $self->{document}->append_child ($comment);  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'character') {  
         if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
           my $data = $1;  
           ## As if in the main phase.  
           ## NOTE: The insertion mode in the main phase  
           ## just before the phase has been changed to the trailing  
           ## end phase is either "after body" or "after frameset".  
           $reconstruct_active_formatting_elements->($insert_to_current);  
             
           $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);  
             
           unless (length $token->{data}) {  
             !!!next-token;  
             redo B;  
           }  
         }  
   
         !!!parse-error (type => 'after html:#character');  
         $self->{insertion_mode} = $previous_insertion_mode;  
         ## reprocess  
         redo B;  
       } elsif ($token->{type} eq 'start tag' or  
                $token->{type} eq 'end tag') {  
         !!!parse-error (type => 'after html:'.($token->{type} eq 'end tag' ? '/' : '').$token->{tag_name});  
         $self->{insertion_mode} = $previous_insertion_mode;  
         ## reprocess  
         redo B;  
       } elsif ($token->{type} eq 'end-of-file') {  
         ## Stop parsing  
         last B;  
       } else {  
         die "$0: $token->{type}: Unknown token";  
       }  
     }  
5105    } # B    } # B
5106    
5107      ## NOTE: The "trailing end" phase in HTML5 is split into
5108      ## two insertion modes: "after html body" and "after html frameset".
5109      ## NOTE: States in the main stage is preserved while
5110      ## the parser stays in the trailing end phase. # MUST
5111    
5112    ## Stop parsing # MUST    ## Stop parsing # MUST
5113        
5114    ## TODO: script stuffs    ## TODO: script stuffs
# Line 5341  sub set_inner_html ($$$) { Line 5193  sub set_inner_html ($$$) {
5193    
5194      ## Step 2      ## Step 2
5195      my $node_ln = $node->local_name;      my $node_ln = $node->local_name;
5196      $p->{content_model_flag} = {      $p->{content_model} = {
5197        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
5198        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
5199        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
5200        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
5201        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
5202        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
5203        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
5204        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
5205        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
5206        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
5207      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
5208         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
5209            unless defined $p->{content_model};
5210            ## ISSUE: What is "the name of the element"? local name?
5211    
5212      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
5213    

Legend:
Removed from v.1.35  
changed lines
  Added in v.1.52

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24