/[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.44 by wakaba, Sat Jul 21 07:34:32 2007 UTC revision 1.52 by wakaba, Sat Jul 21 12:27:22 2007 UTC
# Line 2067  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 2103  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 2496  sub _tree_construction_main ($) { Line 2494  sub _tree_construction_main ($) {
2494                         }                         }
2495    }; # $insert_to_foster    }; # $insert_to_foster
2496    
2497    my $in_body = sub {    my $insert;
     my $insert = shift;  
     if ($token->{type} eq 'start tag') {  
       if ($token->{tag_name} eq 'script') {  
         ## NOTE: This is an "as if in head" code clone  
         $script_start_tag->($insert);  
         return;  
       } elsif ($token->{tag_name} eq 'style') {  
         ## NOTE: This is an "as if in head" code clone  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         return;  
       } elsif ({  
                 base => 1, link => 1,  
                }->{$token->{tag_name}}) {  
         ## NOTE: This is an "as if in head" code clone, only "-t" differs  
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'meta') {  
         ## NOTE: This is an "as if in head" code clone, only "-t" differs  
         !!!insert-element-t ($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  
         }  
   
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'title') {  
         !!!parse-error (type => 'in body:title');  
         ## NOTE: This is an "as if in head" code clone  
         $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {  
           if (defined $self->{head_element}) {  
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             $insert->($_[0]);  
           }  
         });  
         return;  
       } elsif ($token->{tag_name} eq 'body') {  
         !!!parse-error (type => 'in body:body');  
                 
         if (@{$self->{open_elements}} == 1 or  
             $self->{open_elements}->[1]->[1] ne 'body') {  
           ## Ignore the token  
         } else {  
           my $body_el = $self->{open_elements}->[1]->[0];  
           for my $attr_name (keys %{$token->{attributes}}) {  
             unless ($body_el->has_attribute_ns (undef, $attr_name)) {  
               $body_el->set_attribute_ns  
                 (undef, [undef, $attr_name],  
                  $token->{attributes}->{$attr_name}->{value});  
             }  
           }  
         }  
         !!!next-token;  
         return;  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, p => 1, ul => 1,  
                 pre => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         if ($token->{tag_name} eq 'pre') {  
           !!!next-token;  
           if ($token->{type} eq 'character') {  
             $token->{data} =~ s/^\x0A//;  
             unless (length $token->{data}) {  
               !!!next-token;  
             }  
           }  
         } else {  
           !!!next-token;  
         }  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         if (defined $self->{form_element}) {  
           !!!parse-error (type => 'in form:form');  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           ## has a p element in scope  
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!back-token;  
               $token = {type => 'end tag', tag_name => 'p'};  
               return;  
             } elsif ({  
                       table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'li') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'li') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'plaintext') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{content_model} = PLAINTEXT_CONTENT_MODEL;  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>  
         ## has an element in scope  
         #my $i;  
         #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
         #  my $node = $self->{open_elements}->[$_];  
         #  if ({  
         #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
         #      }->{$node->[1]}) {  
         #    $i = $_;  
         #    last INSCOPE;  
         #  } elsif ({  
         #            table => 1, caption => 1, td => 1, th => 1,  
         #            button => 1, marquee => 1, object => 1, html => 1,  
         #           }->{$node->[1]}) {  
         #    last INSCOPE;  
         #  }  
         #} # INSCOPE  
         #    
         #if (defined $i) {  
         #  !!! parse-error (type => 'in hn:hn');  
         #  splice @{$self->{open_elements}}, $i;  
         #}  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'a') {  
         AFE: for my $i (reverse 0..$#$active_formatting_elements) {  
           my $node = $active_formatting_elements->[$i];  
           if ($node->[1] eq 'a') {  
             !!!parse-error (type => 'in a:a');  
               
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'a'};  
             $formatting_end_tag->($token->{tag_name});  
               
             AFE2: for (reverse 0..$#$active_formatting_elements) {  
               if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {  
                 splice @$active_formatting_elements, $_, 1;  
                 last AFE2;  
               }  
             } # AFE2  
             OE: for (reverse 0..$#{$self->{open_elements}}) {  
               if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {  
                 splice @{$self->{open_elements}}, $_, 1;  
                 last OE;  
               }  
             } # OE  
             last AFE;  
           } elsif ($node->[0] eq '#marker') {  
             last AFE;  
           }  
         } # AFE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
   
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
   
         !!!next-token;  
         return;  
       } elsif ({  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'nobr') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
   
         ## has a |nobr| element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'nobr') {  
             !!!parse-error (type => 'not closed:nobr');  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'nobr'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'button') {  
         ## has a button element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'button') {  
             !!!parse-error (type => 'in button:button');  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'button'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
   
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         return;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = 'in table';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,  
                 image => 1,  
                }->{$token->{tag_name}}) {  
         if ($token->{tag_name} eq 'image') {  
           !!!parse-error (type => 'image');  
           $token->{tag_name} = 'img';  
         }  
   
         ## NOTE: There is an "as if <br>" code clone.  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'input') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         ## TODO: associate with $self->{form_element} if defined  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'isindex') {  
         !!!parse-error (type => 'isindex');  
           
         if (defined $self->{form_element}) {  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           my $at = $token->{attributes};  
           my $form_attrs;  
           $form_attrs->{action} = $at->{action} if $at->{action};  
           my $prompt_attr = $at->{prompt};  
           $at->{name} = {name => 'name', value => 'isindex'};  
           delete $at->{action};  
           delete $at->{prompt};  
           my @tokens = (  
                         {type => 'start tag', tag_name => 'form',  
                          attributes => $form_attrs},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'start tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'label'},  
                        );  
           if ($prompt_attr) {  
             push @tokens, {type => 'character', data => $prompt_attr->{value}};  
           } else {  
             push @tokens, {type => 'character',  
                            data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD  
             ## TODO: make this configurable  
           }  
           push @tokens,  
                         {type => 'start tag', tag_name => 'input', attributes => $at},  
                         #{type => 'character', data => ''}, # SHOULD  
                         {type => 'end tag', tag_name => 'label'},  
                         {type => 'end tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'end tag', tag_name => 'form'};  
           $token = shift @tokens;  
           !!!back-token (@tokens);  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'textarea') {  
         my $tag_name = $token->{tag_name};  
         my $el;  
         !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
           
         ## TODO: $self->{form_element} if defined  
         $self->{content_model} = RCDATA_CONTENT_MODEL;  
         delete $self->{escape}; # MUST  
           
         $insert->($el);  
           
         my $text = '';  
         !!!next-token;  
         if ($token->{type} eq 'character') {  
           $token->{data} =~ s/^\x0A//;  
           unless (length $token->{data}) {  
             !!!next-token;  
           }  
         }  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
           !!!next-token;  
         }  
         if (length $text) {  
           $el->manakai_append_text ($text);  
         }  
           
         $self->{content_model} = PCDATA_CONTENT_MODEL;  
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq $tag_name) {  
           ## Ignore the token  
         } else {  
           !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
         }  
         !!!next-token;  
         return;  
       } elsif ({  
                 iframe => 1,  
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         ## NOTE: There are two "as if in body" code clones.  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         return;  
       } elsif ($token->{tag_name} eq 'select') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $self->{insertion_mode} = 'in select';  
         !!!next-token;  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'in body:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: An issue on HTML5 new elements in the spec.  
       } else {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         !!!next-token;  
         return;  
       }  
     } elsif ($token->{type} eq 'end tag') {  
       if ($token->{tag_name} eq 'body') {  
         if (@{$self->{open_elements}} > 1 and  
             $self->{open_elements}->[1]->[1] eq 'body') {  
           for (@{$self->{open_elements}}) {  
             unless ({  
                        dd => 1, dt => 1, li => 1, p => 1, td => 1,  
                        th => 1, tr => 1, body => 1, html => 1,  
                      tbody => 1, tfoot => 1, thead => 1,  
                     }->{$_->[1]}) {  
               !!!parse-error (type => 'not closed:'.$_->[1]);  
             }  
           }  
   
           $self->{insertion_mode} = 'after body';  
           !!!next-token;  
           return;  
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'html') {  
         if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {  
           ## ISSUE: There is an issue in the spec.  
           if ($self->{open_elements}->[-1]->[1] ne 'body') {  
             !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);  
           }  
           $self->{insertion_mode} = 'after body';  
           ## reprocess  
           return;  
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           return;  
         }  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, pre => 1, ul => 1,  
                 p => 1,  
                 dd => 1, dt => 1, li => 1,  
                 button => 1, marquee => 1, object => 1,  
                }->{$token->{tag_name}}) {  
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq $token->{tag_name}) {  
             ## generate implied end tags  
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE unless $token->{tag_name} eq 'p';  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           if (defined $i) {  
             !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
           } else {  
             !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           }  
         }  
           
         if (defined $i) {  
           splice @{$self->{open_elements}}, $i;  
         } elsif ($token->{tag_name} eq 'p') {  
           ## As if <p>, then reprocess the current token  
           my $el;  
           !!!create-element ($el, 'p');  
           $insert->($el);  
         }  
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         ## has an element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq $token->{tag_name}) {  
             ## 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  
               return;  
             }  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {  
           pop @{$self->{open_elements}};  
         } else {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
   
         undef $self->{form_element};  
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ({  
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             ## 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  
               return;  
             }  
             $i = $_;  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
           
         splice @{$self->{open_elements}}, $i if defined $i;  
         !!!next-token;  
         return;  
       } elsif ({  
                 a => 1,  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 nobr => 1, s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $formatting_end_tag->($token->{tag_name});  
         return;  
       } elsif ($token->{tag_name} eq 'br') {  
         !!!parse-error (type => 'unmatched end tag:br');  
   
         ## As if <br>  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         my $el;  
         !!!create-element ($el, 'br');  
         $insert->($el);  
           
         ## Ignore the token.  
         !!!next-token;  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                 area => 1, basefont => 1, bgsound => 1,  
                 embed => 1, hr => 1, iframe => 1, image => 1,  
                 img => 1, input => 1, isindex => 1, noembed => 1,  
                 noframes => 1, param => 1, select => 1, spacer => 1,  
                 table => 1, textarea => 1, wbr => 1,  
                 noscript => 0, ## TODO: if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: Issue on HTML5 new elements in spec  
           
       } else {  
         ## Step 1  
         my $node_i = -1;  
         my $node = $self->{open_elements}->[$node_i];  
   
         ## Step 2  
         S2: {  
           if ($node->[1] eq $token->{tag_name}) {  
             ## Step 1  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
           
             ## Step 2  
             if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
             }  
               
             ## Step 3  
             splice @{$self->{open_elements}}, $node_i;  
   
             !!!next-token;  
             last S2;  
           } else {  
             ## Step 3  
             if (not $formatting_category->{$node->[1]} and  
                 #not $phrasing_category->{$node->[1]} and  
                 ($special_category->{$node->[1]} or  
                  $scoping_category->{$node->[1]})) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               last S2;  
             }  
           }  
             
           ## Step 4  
           $node_i--;  
           $node = $self->{open_elements}->[$node_i];  
             
           ## Step 5;  
           redo S2;  
         } # S2  
         return;  
       }  
     }  
   }; # $in_body  
2498    
2499    B: {    B: {
2500      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} eq 'DOCTYPE') {
# Line 3350  sub _tree_construction_main ($) { Line 2504  sub _tree_construction_main ($) {
2504        !!!next-token;        !!!next-token;
2505        redo B;        redo B;
2506      } elsif ($token->{type} eq 'end-of-file') {      } elsif ($token->{type} eq 'end-of-file') {
2507        if ($token->{insertion_mode} ne 'trailing end') {        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          ## Generate implied end tags
2512          if ({          if ({
2513               dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,               dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,
# Line 3377  sub _tree_construction_main ($) { Line 2534  sub _tree_construction_main ($) {
2534        last B;        last B;
2535      } elsif ($token->{type} eq 'start tag' and      } elsif ($token->{type} eq 'start tag' and
2536               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
2537        if ($self->{insertion_mode} eq 'trailing end') {        if ($self->{insertion_mode} eq 'after html body') {
2538          ## Turn into the main phase          ## Turn into the main phase
2539          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html');
2540          $self->{insertion_mode} = $previous_insertion_mode;          $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.  ## ISSUE: "aa<html>" is not a parse error.
# Line 3400  sub _tree_construction_main ($) { Line 2561  sub _tree_construction_main ($) {
2561        redo B;        redo B;
2562      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} eq 'comment') {
2563        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2564        if ($self->{insertion_mode} eq 'trailing end') {        if ($self->{insertion_mode} eq 'after html body' or
2565              $self->{insertion_mode} eq 'after html frameset') {
2566          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2567        } elsif ($self->{insertion_mode} eq 'after body') {        } elsif ($self->{insertion_mode} eq 'after body') {
2568          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
# Line 3409  sub _tree_construction_main ($) { Line 2571  sub _tree_construction_main ($) {
2571        }        }
2572        !!!next-token;        !!!next-token;
2573        redo B;        redo B;
2574      } elsif ($self->{insertion_mode} eq 'before head') {      } elsif ($self->{insertion_mode} eq 'in head' or
2575            if ($token->{type} eq 'character') {               $self->{insertion_mode} eq 'in head noscript' or
2576              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {               $self->{insertion_mode} eq 'after head' or
2577                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);               $self->{insertion_mode} eq 'before head') {
2578                unless (length $token->{data}) {        if ($token->{type} eq 'character') {
2579                  !!!next-token;          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
2580                  redo B;            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
2581                }            unless (length $token->{data}) {
2582              }              !!!next-token;
2583              ## As if <head>              redo B;
2584              !!!create-element ($self->{head_element}, 'head');            }
2585              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});          }
2586              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
2587              $self->{insertion_mode} = 'in head';          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              ## reprocess
2618              redo B;              redo B;
2619            } elsif ($token->{type} eq 'start tag') {            } 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';  
2620              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
2621                !!!next-token;                if ($self->{insertion_mode} eq 'before head') {
2622              #} elsif ({                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
2623              #          base => 1, link => 1, meta => 1,                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2624              #          script => 1, style => 1, title => 1,                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
2625              #         }->{$token->{tag_name}}) {                  $self->{insertion_mode} = 'in head';
2626              #  ## reprocess                  !!!next-token;
2627              } else {                  redo B;
2628                ## reprocess                } elsif ($self->{insertion_mode} ne 'after head') {
2629              }                  !!!parse-error (type => 'in head:head'); # or in head noscript
2630              redo B;                  ## Ignore the token
2631            } elsif ($token->{type} eq 'end tag') {                  !!!next-token;
2632              if ({                  redo B;
2633                   head => 1, body => 1, html => 1,                } else {
2634                   p => 1, br => 1,                  #
2635                  }->{$token->{tag_name}}) {                }
2636                } elsif ($self->{insertion_mode} eq 'before head') {
2637                ## As if <head>                ## As if <head>
2638                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
2639                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2640                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2641    
2642                $self->{insertion_mode} = 'in head';                $self->{insertion_mode} = 'in head';
2643                ## reprocess                ## Reprocess in the "in head" insertion mode...
               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;  
2644              }              }
2645            } else {  
2646              die "$0: $token->{type}: Unknown type";              if ($token->{tag_name} eq 'base') {
2647            }                if ($self->{insertion_mode} eq 'in head noscript') {
2648          } elsif ($self->{insertion_mode} eq 'in head' or                  ## As if </noscript>
2649                   $self->{insertion_mode} eq 'in head noscript' or                  pop @{$self->{open_elements}};
2650                   $self->{insertion_mode} eq 'after head') {                  !!!parse-error (type => 'in noscript:base');
2651            if ($token->{type} eq 'character') {                
2652              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                  $self->{insertion_mode} = 'in head';
2653                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                  ## Reprocess in the "in head" insertion mode...
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
2654                }                }
2655              }  
2656                              ## NOTE: There is a "as if in head" code clone.
2657              #                if ($self->{insertion_mode} eq 'after head') {
2658            } elsif ($token->{type} eq 'start tag') {                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2659              if ({base => ($self->{insertion_mode} eq 'in head' or                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2660                            $self->{insertion_mode} eq 'after head'),                }
2661                   link => 1}->{$token->{tag_name}}) {                !!!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.                ## NOTE: There is a "as if in head" code clone.
2669                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} eq 'after head') {
2670                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
# Line 3522  sub _tree_construction_main ($) { Line 2707  sub _tree_construction_main ($) {
2707                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} eq 'after head';
2708                !!!next-token;                !!!next-token;
2709                redo B;                redo B;
2710              } elsif ($token->{tag_name} eq 'title' and              } elsif ($token->{tag_name} eq 'title') {
2711                       $self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} eq 'in head noscript') {
2712                ## NOTE: There is a "as if in head" code clone.                  ## As if </noscript>
2713                if ($self->{insertion_mode} eq 'after head') {                  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});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2720                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  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}                my $parent = defined $self->{head_element} ? $self->{head_element}
2725                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
2726                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL,
# Line 3563  sub _tree_construction_main ($) { Line 2755  sub _tree_construction_main ($) {
2755                } else {                } else {
2756                  #                  #
2757                }                }
2758              } elsif ($token->{tag_name} eq 'head' and              } elsif ($token->{tag_name} eq 'script') {
2759                       $self->{insertion_mode} ne 'after head') {                if ($self->{insertion_mode} eq 'in head noscript') {
2760                !!!parse-error (type => 'in head:head'); # or in head noscript                  ## As if </noscript>
2761                ## Ignore the token                  pop @{$self->{open_elements}};
2762                !!!next-token;                  !!!parse-error (type => 'in noscript:script');
2763                redo B;                
2764              } elsif ($self->{insertion_mode} ne 'in head noscript' and                  $self->{insertion_mode} = 'in head';
2765                       $token->{tag_name} eq 'script') {                  ## Reprocess in the "in head" insertion mode...
2766                if ($self->{insertion_mode} eq 'after head') {                } elsif ($self->{insertion_mode} eq 'after head') {
2767                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2768                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2769                }                }
2770    
2771                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2772                $script_start_tag->($insert_to_current);                $script_start_tag->($insert_to_current);
2773                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2774                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} eq 'after head';
2775                redo B;                redo B;
2776              } elsif ($self->{insertion_mode} eq 'after head' and              } elsif ($token->{tag_name} eq 'body' or
                      $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  
2777                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
2778                !!!insert-element ('frameset', $token->{attributes});                if ($self->{insertion_mode} eq 'in head noscript') {
2779                $self->{insertion_mode} = 'in frameset';                  ## 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;                !!!next-token;
2798                redo B;                redo B;
2799              } else {              } else {
2800                #                #
2801              }              }
2802            } elsif ($token->{type} eq 'end tag') {  
2803              if ($self->{insertion_mode} eq 'in head' and              if ($self->{insertion_mode} eq 'in head noscript') {
2804                  $token->{tag_name} eq 'head') {                ## As if </noscript>
2805                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
2806                $self->{insertion_mode} = 'after head';                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
2807                !!!next-token;                
2808                redo B;                ## Reprocess in the "in head" insertion mode...
2809              } elsif ($self->{insertion_mode} eq 'in head noscript' and                ## As if </head>
                 $token->{tag_name} eq 'noscript') {  
2810                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
2811                $self->{insertion_mode} = 'in head';  
2812                !!!next-token;                ## Reprocess in the "after head" insertion mode...
2813                redo B;              } elsif ($self->{insertion_mode} eq 'in head') {
2814              } elsif ($self->{insertion_mode} eq 'in head' and                ## 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,                        body => 1, html => 1,
                       p => 1, br => 1,  
2873                       }->{$token->{tag_name}}) {                       }->{$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 ($self->{insertion_mode} eq 'in head noscript' and              } elsif ({
                      {  
2891                        p => 1, br => 1,                        p => 1, br => 1,
2892                       }->{$token->{tag_name}}) {                       }->{$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              } elsif ($self->{insertion_mode} ne 'after head') {              } 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});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2932                ## Ignore the token                ## Ignore the token ## ISSUE: An issue in the spec.
2933                !!!next-token;                !!!next-token;
2934                redo B;                redo B;
             } else {  
               #  
2935              }              }
           } else {  
             #  
           }  
2936    
2937            ## As if </head> or </noscript> or <body>              ## "after head" insertion mode
2938            if ($self->{insertion_mode} eq 'in head') {              ## As if <body>
             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'  
2939              !!!insert-element ('body');              !!!insert-element ('body');
2940              $self->{insertion_mode} = 'in body';              $self->{insertion_mode} = 'in body';
2941                ## reprocess
2942                redo B;
2943              } else {
2944                die "$0: $token->{type}: Unknown token type";
2945            }            }
           ## reprocess  
           redo B;  
2946    
2947            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
2948          } elsif ($self->{insertion_mode} eq 'in body' or      } elsif ($self->{insertion_mode} eq 'in body' or
2949                   $self->{insertion_mode} eq 'in cell' or               $self->{insertion_mode} eq 'in cell' or
2950                   $self->{insertion_mode} eq 'in caption') {               $self->{insertion_mode} eq 'in caption') {
2951            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
2952              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
2953              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
# Line 3964  sub _tree_construction_main ($) { Line 3262  sub _tree_construction_main ($) {
3262              } else {              } else {
3263                #                #
3264              }              }
3265            } else {        } else {
3266              #          die "$0: $token->{type}: Unknown token type";
3267            }        }
3268              
3269            $in_body->($insert_to_current);        $insert = $insert_to_current;
3270            redo B;        #
3271          } elsif ($self->{insertion_mode} eq 'in table') {      } 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') {            if ($token->{type} eq 'character') {
3275              ## NOTE: There are "character in table" code clones.              ## NOTE: There are "character in table" code clones.
3276              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
# Line 4031  sub _tree_construction_main ($) { Line 3331  sub _tree_construction_main ($) {
3331              redo B;              redo B;
3332            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
3333              if ({              if ({
3334                   caption => 1,                   tr => ($self->{insertion_mode} ne 'in row'),
3335                   colgroup => 1,                   th => 1, td => 1,
                  tbody => 1, tfoot => 1, thead => 1,  
3336                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3337                ## Clear back to table context                if ($self->{insertion_mode} eq 'in table') {
3338                while ($self->{open_elements}->[-1]->[1] ne 'table' and                  ## Clear back to table context
3339                       $self->{open_elements}->[-1]->[1] ne 'html') {                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
3340                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                         $self->{open_elements}->[-1]->[1] ne 'html') {
3341                  pop @{$self->{open_elements}};                    !!!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                push @$active_formatting_elements, ['#marker', '']                if ($self->{insertion_mode} eq 'in table body') {
3351                  if $token->{tag_name} eq 'caption';                  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});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3383                $self->{insertion_mode} = {                $self->{insertion_mode} = 'in cell';
3384                                   caption => 'in caption',  
3385                                   colgroup => 'in column group',                push @$active_formatting_elements, ['#marker', ''];
3386                                   tbody => 'in table body',                
                                  tfoot => 'in table body',  
                                  thead => 'in table body',  
                                 }->{$token->{tag_name}};  
3387                !!!next-token;                !!!next-token;
3388                redo B;                redo B;
3389              } elsif ({              } elsif ({
3390                        col => 1,                        caption => 1, col => 1, colgroup => 1,
3391                        td => 1, th => 1, tr => 1,                        tbody => 1, tfoot => 1, thead => 1,
3392                          tr => 1, # $self->{insertion_mode} eq 'in row'
3393                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3394                ## Clear back to table context                if ($self->{insertion_mode} eq 'in row') {
3395                while ($self->{open_elements}->[-1]->[1] ne 'table' and                  ## As if </tr>
3396                       $self->{open_elements}->[-1]->[1] ne 'html') {                  ## have an element in table scope
3397                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  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}};                  pop @{$self->{open_elements}};
3473                    $self->{insertion_mode} = 'in table';
3474                    ## reprocess in "in table" insertion mode...
3475                }                }
3476    
3477                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                if ($token->{tag_name} eq 'col') {
3478                $self->{insertion_mode} = $token->{tag_name} eq 'col'                  ## Clear back to table context
3479                  ? 'in column group' : 'in table body';                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
3480                ## reprocess                         $self->{open_elements}->[-1]->[1] ne 'html') {
3481                redo B;                    !!!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') {              } elsif ($token->{tag_name} eq 'table') {
3518                ## NOTE: There are code clones for this "table in table"                ## NOTE: There are code clones for this "table in table"
3519                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
# Line 4124  sub _tree_construction_main ($) { Line 3567  sub _tree_construction_main ($) {
3567                #                #
3568              }              }
3569            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
3570              if ($token->{tag_name} eq 'table') {              if ($token->{tag_name} eq 'tr' and
3571                ## have a table element in table scope                  $self->{insertion_mode} eq 'in row') {
3572                  ## have an element in table scope
3573                my $i;                my $i;
3574                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3575                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
# Line 4144  sub _tree_construction_main ($) { Line 3588  sub _tree_construction_main ($) {
3588                  !!!next-token;                  !!!next-token;
3589                  redo B;                  redo B;
3590                }                }
                 
               ## 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;  
               }  
3591    
3592                if ($self->{open_elements}->[-1]->[1] ne 'table') {                ## 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]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3597                    pop @{$self->{open_elements}};
3598                }                }
3599    
3600                splice @{$self->{open_elements}}, $i;                pop @{$self->{open_elements}}; # tr
3601                  $self->{insertion_mode} = 'in table body';
               $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 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 '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  
3602                !!!next-token;                !!!next-token;
3603                redo B;                redo B;
3604              } else {              } elsif ($token->{tag_name} eq 'table') {
3605                #                if ($self->{insertion_mode} eq 'in row') {
3606              }                  ## As if </tr>
3607            } else {                  ## have an element in table scope
3608              #                  my $i;
3609            }                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3610                      my $node = $self->{open_elements}->[$_];
3611            ## As if </colgroup>                    if ($node->[1] eq 'tr') {
3612            if ($self->{open_elements}->[-1]->[1] eq 'html') {                      $i = $_;
3613              !!!parse-error (type => 'unmatched end tag:colgroup');                      last INSCOPE;
3614              ## Ignore the token                    } elsif ({
3615              !!!next-token;                              table => 1, html => 1,
3616              redo B;                             }->{$node->[1]}) {
3617            } else {                      last INSCOPE;
             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;  
3618                    }                    }
3619                    last OE;                  } # 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                } # OE                  
3627                $foster_parent_element = $self->{open_elements}->[0]->[0] and                  ## Clear back to table row context
3628                $prev_sibling = $foster_parent_element->last_child                  while (not {
3629                  unless defined $foster_parent_element;                    tr => 1, html => 1,
3630                if (defined $prev_sibling and                  }->{$self->{open_elements}->[-1]->[1]}) {
3631                    $prev_sibling->node_type == 3) {                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3632                  $prev_sibling->manakai_append_text ($token->{data});                    pop @{$self->{open_elements}};
               } 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 '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;  
3633                  }                  }
3634                } # INSCOPE                  
3635                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
3636                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = 'in table body';
3637                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
3638                }                }
3639    
3640                ## Clear back to table body context                if ($self->{insertion_mode} eq 'in table body') {
3641                while (not {                  ## have an element in table scope
3642                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
3643                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3644                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    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}};                  pop @{$self->{open_elements}};
3679                    $self->{insertion_mode} = 'in table';
3680                    ## reprocess in the "in table" insertion mode...
3681                }                }
3682    
               ## 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>  
3683                ## have a table element in table scope                ## have a table element in table scope
3684                my $i;                my $i;
3685                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3686                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3687                  if ($node->[1] eq 'table') {                  if ($node->[1] eq $token->{tag_name}) {
3688                    $i = $_;                    $i = $_;
3689                    last INSCOPE;                    last INSCOPE;
3690                  } elsif ({                  } elsif ({
# Line 4392  sub _tree_construction_main ($) { Line 3694  sub _tree_construction_main ($) {
3694                  }                  }
3695                } # INSCOPE                } # INSCOPE
3696                unless (defined $i) {                unless (defined $i) {
3697                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3698                  ## Ignore tokens </table><table>                  ## Ignore the token
3699                  !!!next-token;                  !!!next-token;
3700                  redo B;                  redo B;
3701                }                }
3702                  
3703                ## generate implied end tags                ## generate implied end tags
3704                if ({                if ({
3705                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3706                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3707                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
3708                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
3709                  !!!back-token;                  !!!back-token;
3710                  $token = {type => 'end tag',                  $token = {type => 'end tag',
3711                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3712                  redo B;                  redo B;
3713                }                }
3714                  
3715                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
3716                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3717                }                }
3718                    
3719                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
3720                  
3721                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
3722                  
3723                ## reprocess                !!!next-token;
3724                redo B;                redo B;
3725              } else {              } elsif ({
3726                #                        tbody => 1, tfoot => 1, thead => 1,
3727              }                       }->{$token->{tag_name}} and
3728            } elsif ($token->{type} eq 'end tag') {                       ($self->{insertion_mode} eq 'in row' or
3729              if ({                        $self->{insertion_mode} eq 'in table body')) {
3730                   tbody => 1, tfoot => 1, thead => 1,                if ($self->{insertion_mode} eq 'in row') {
3731                  }->{$token->{tag_name}}) {                  ## have an element in table scope
3732                ## have an element in table scope                  my $i;
3733                my $i;                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3734                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    my $node = $self->{open_elements}->[$_];
3735                  my $node = $self->{open_elements}->[$_];                    if ($node->[1] eq $token->{tag_name}) {
3736                  if ($node->[1] eq $token->{tag_name}) {                      $i = $_;
3737                    $i = $_;                      last INSCOPE;
3738                    last INSCOPE;                    } elsif ({
3739                  } elsif ({                              table => 1, html => 1,
3740                            table => 1, html => 1,                             }->{$node->[1]}) {
3741                           }->{$node->[1]}) {                      last INSCOPE;
3742                    last INSCOPE;                    }
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                } # INSCOPE                  
3780                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
3781                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = 'in table body';
3782                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!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}};  
3783                }                }
3784    
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
3785                ## have an element in table scope                ## have an element in table scope
3786                my $i;                my $i;
3787                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3788                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3789                  if ({                  if ($node->[1] eq $token->{tag_name}) {
                      tbody => 1, thead => 1, tfoot => 1,  
                     }->{$node->[1]}) {  
3790                    $i = $_;                    $i = $_;
3791                    last INSCOPE;                    last INSCOPE;
3792                  } elsif ({                  } elsif ({
# Line 4492  sub _tree_construction_main ($) { Line 3810  sub _tree_construction_main ($) {
3810                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3811                }                }
3812    
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
   
3813                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3814                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = 'in table';
3815                ## reprocess                !!!next-token;
3816                redo B;                redo B;
3817              } elsif ({              } elsif ({
3818                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
3819                        html => 1, td => 1, th => 1, tr => 1,                        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}}) {                       }->{$token->{tag_name}}) {
3823                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3824                ## Ignore the token                ## Ignore the token
# Line 4515  sub _tree_construction_main ($) { Line 3828  sub _tree_construction_main ($) {
3828                #                #
3829              }              }
3830            } else {            } else {
3831              #              die "$0: $token->{type}: Unknown token type";
3832            }            }
3833              
3834            ## As if in table        !!!parse-error (type => 'in table:'.$token->{tag_name});
3835            !!!parse-error (type => 'in table:'.$token->{tag_name});  
3836            $in_body->($insert_to_foster);        $insert = $insert_to_foster;
3837            redo B;        #
3838          } elsif ($self->{insertion_mode} eq 'in row') {      } elsif ($self->{insertion_mode} eq 'in column group') {
3839            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
             ## NOTE: This is a "character in table" code clone.  
3840              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3841                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
                 
3842                unless (length $token->{data}) {                unless (length $token->{data}) {
3843                  !!!next-token;                  !!!next-token;
3844                  redo B;                  redo B;
3845                }                }
3846              }              }
   
             !!!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);  
3847                            
3848              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;  
3849            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
3850              if ($token->{tag_name} eq 'th' or              if ($token->{tag_name} eq 'col') {
                 $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}};  
               }  
                 
3851                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3852                $self->{insertion_mode} = 'in cell';                pop @{$self->{open_elements}};
   
               push @$active_formatting_elements, ['#marker', ''];  
                 
3853                !!!next-token;                !!!next-token;
3854                redo B;                redo B;
3855              } elsif ({              } else {
                       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 {  
3856                #                #
3857              }              }
3858            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
3859              if ($token->{tag_name} eq 'tr') {              if ($token->{tag_name} eq 'colgroup') {
3860                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] eq 'html') {
3861                my $i;                  !!!parse-error (type => 'unmatched end tag:colgroup');
               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});  
3862                  ## Ignore the token                  ## Ignore the token
3863                  !!!next-token;                  !!!next-token;
3864                  redo B;                  redo B;
3865                }                } else {
3866                    pop @{$self->{open_elements}}; # colgroup
3867                ## As if </tr>                  $self->{insertion_mode} = 'in table';
               ## 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  
3868                  !!!next-token;                  !!!next-token;
3869                  redo B;                  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}};  
3870                }                }
3871                } elsif ($token->{tag_name} eq 'col') {
3872                pop @{$self->{open_elements}}; # tr                !!!parse-error (type => 'unmatched end tag:col');
               $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});  
3873                ## Ignore the token                ## Ignore the token
3874                !!!next-token;                !!!next-token;
3875                redo B;                redo B;
3876              } else {              } else {
3877                #                #
3878              }              }
3879            } else {            } else {
3880              #              #
3881            }            }
3882    
3883            ## As if in table            ## As if </colgroup>
3884            !!!parse-error (type => 'in table:'.$token->{tag_name});            if ($self->{open_elements}->[-1]->[1] eq 'html') {
3885            $in_body->($insert_to_foster);              !!!parse-error (type => 'unmatched end tag:colgroup');
3886            redo B;              ## Ignore the token
3887          } elsif ($self->{insertion_mode} eq 'in select') {              !!!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') {            if ($token->{type} eq 'character') {
3897              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3898              !!!next-token;              !!!next-token;
# Line 5005  sub _tree_construction_main ($) { Line 4066  sub _tree_construction_main ($) {
4066            ## Ignore the token            ## Ignore the token
4067            !!!next-token;            !!!next-token;
4068            redo B;            redo B;
4069          } elsif ($self->{insertion_mode} eq 'after body') {      } elsif ($self->{insertion_mode} eq 'after body' or
4070            if ($token->{type} eq 'character') {               $self->{insertion_mode} eq 'after html body') {
4071              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        if ($token->{type} eq 'character') {
4072                my $data = $1;          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4073                ## As if in body            my $data = $1;
4074                $reconstruct_active_formatting_elements->($insert_to_current);            ## As if in body
4075              $reconstruct_active_formatting_elements->($insert_to_current);
4076                                
4077                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $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                unless (length $token->{data}) {            ## Reprocess in the "main" phase, "after body" insertion mode...
4089                  !!!next-token;          }
4090                  redo B;          
4091                }          ## "after body" insertion mode
4092              }          !!!parse-error (type => 'after body:#character');
4093                
4094              #          $self->{insertion_mode} = 'in body';
4095              !!!parse-error (type => 'after body:#character');          ## reprocess
4096            } elsif ($token->{type} eq 'start tag') {          redo B;
4097              !!!parse-error (type => 'after body:'.$token->{tag_name});        } elsif ($token->{type} eq 'start tag') {
4098              #          if ($self->{insertion_mode} eq 'after html body') {
4099            } elsif ($token->{type} eq 'end tag') {            !!!parse-error (type => 'after html:'.$token->{tag_name});
4100              if ($token->{tag_name} eq 'html') {            
4101                if (defined $self->{inner_html_node}) {            ## Reprocess in the "main" phase, "after body" insertion mode...
4102                  !!!parse-error (type => 'unmatched end tag:html');          }
4103                  ## Ignore the token  
4104                  !!!next-token;          ## "after body" insertion mode
4105                  redo B;          !!!parse-error (type => 'after body:'.$token->{tag_name});
4106                } else {  
4107                  $previous_insertion_mode = $self->{insertion_mode};          $self->{insertion_mode} = 'in body';
4108                  $self->{insertion_mode} = 'trailing end';          ## reprocess
4109                  !!!next-token;          redo B;
4110                  redo B;        } elsif ($token->{type} eq 'end tag') {
4111                }          if ($self->{insertion_mode} eq 'after html body') {
4112              } else {            !!!parse-error (type => 'after html:/'.$token->{tag_name});
4113                !!!parse-error (type => 'after body:/'.$token->{tag_name});            
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 {            } else {
4126              die "$0: $token->{type}: Unknown token type";              $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';            $self->{insertion_mode} = 'in body';
4134            ## reprocess            ## reprocess
4135            redo B;            redo B;
4136      } elsif ($self->{insertion_mode} eq 'in frameset') {          }
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') {        if ($token->{type} eq 'character') {
4144          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4145            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4146              
4147            unless (length $token->{data}) {            unless (length $token->{data}) {
4148              !!!next-token;              !!!next-token;
4149              redo B;              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          !!!parse-error (type => 'in frameset:#character');              $self->{insertion_mode} = 'after frameset';
4162          ## Ignore the token              ## Reprocess in the "main" phase, "after frameset"...
4163          !!!next-token;              !!!parse-error (type => 'after frameset:#character');
4164          redo B;            }
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') {        } elsif ($token->{type} eq 'start tag') {
4177          if ($token->{tag_name} eq 'frameset') {          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});            !!!insert-element ($token->{tag_name}, $token->{attributes});
4187            !!!next-token;            !!!next-token;
4188            redo B;            redo B;
4189          } elsif ($token->{tag_name} eq 'frame') {          } elsif ($token->{tag_name} eq 'frame' and
4190                     $self->{insertion_mode} eq 'in frameset') {
4191            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
4192            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4193            !!!next-token;            !!!next-token;
# Line 5078  sub _tree_construction_main ($) { Line 4197  sub _tree_construction_main ($) {
4197            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4198            redo B;            redo B;
4199          } else {          } else {
4200            !!!parse-error (type => 'in frameset:'.$token->{tag_name});            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            ## Ignore the token
4206            !!!next-token;            !!!next-token;
4207            redo B;            redo B;
4208          }          }
4209        } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} eq 'end tag') {
4210          if ($token->{tag_name} eq 'frameset') {          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            if ($self->{open_elements}->[-1]->[1] eq 'html' and
4220                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
4221              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
# Line 5100  sub _tree_construction_main ($) { Line 4231  sub _tree_construction_main ($) {
4231              $self->{insertion_mode} = 'after frameset';              $self->{insertion_mode} = 'after frameset';
4232            }            }
4233            redo B;            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 {          } else {
4240            !!!parse-error (type => 'in frameset:/'.$token->{tag_name});            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            ## Ignore the token
4246            !!!next-token;            !!!next-token;
4247            redo B;            redo B;
# Line 5109  sub _tree_construction_main ($) { Line 4249  sub _tree_construction_main ($) {
4249        } else {        } else {
4250          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4251        }        }
     } 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);  
4252    
4253                unless (length $token->{data}) {        ## ISSUE: An issue in spec here
4254                  !!!next-token;      } else {
4255                  redo B;        die "$0: $self->{insertion_mode}: Unknown insertion mode";
4256                }      }
             }  
4257    
4258              if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {      ## "in body" insertion mode
4259                !!!parse-error (type => 'after frameset:#character');    my $in_body = sub {
4260        if ($token->{type} eq 'start tag') {
4261          if ($token->{tag_name} eq 'script') {
4262            ## NOTE: This is an "as if in head" code clone
4263            $script_start_tag->($insert);
4264            return;
4265          } elsif ($token->{tag_name} eq 'style') {
4266            ## NOTE: This is an "as if in head" code clone
4267            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4268            return;
4269          } elsif ({
4270                    base => 1, link => 1,
4271                   }->{$token->{tag_name}}) {
4272            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4273            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4274            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4275            !!!next-token;
4276            return;
4277          } elsif ($token->{tag_name} eq 'meta') {
4278            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4279            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4280            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4281    
4282                ## Ignore the token.          unless ($self->{confident}) {
4283                if (length $token->{data}) {            my $charset;
4284                  ## reprocess the rest of characters            if ($token->{attributes}->{charset}) { ## TODO: And if supported
4285                } else {              $charset = $token->{attributes}->{charset}->{value};
4286                  !!!next-token;            }
4287                }            if ($token->{attributes}->{'http-equiv'}) {
4288                redo B;              ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
4289              }              if ($token->{attributes}->{'http-equiv'}->{value}
4290                    =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
4291                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4292                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4293                  $charset = defined $1 ? $1 : defined $2 ? $2 : $3;
4294                } ## TODO: And if supported
4295              }
4296              ## TODO: Change the encoding
4297            }
4298    
4299          die qq[$0: Character "$token->{data}"];          !!!next-token;
4300        } elsif ($token->{type} eq 'start tag') {          return;
4301          if ($token->{tag_name} eq 'noframes') {        } elsif ($token->{tag_name} eq 'title') {
4302            ## NOTE: As if in body.          !!!parse-error (type => 'in body:title');
4303            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);          ## NOTE: This is an "as if in head" code clone
4304            redo B;          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
4305          } else {            if (defined $self->{head_element}) {
4306            !!!parse-error (type => 'after frameset:'.$token->{tag_name});              $self->{head_element}->append_child ($_[0]);
4307              } else {
4308                $insert->($_[0]);
4309              }
4310            });
4311            return;
4312          } elsif ($token->{tag_name} eq 'body') {
4313            !!!parse-error (type => 'in body:body');
4314                  
4315            if (@{$self->{open_elements}} == 1 or
4316                $self->{open_elements}->[1]->[1] ne 'body') {
4317            ## Ignore the token            ## Ignore the token
4318            !!!next-token;          } else {
4319            redo B;            my $body_el = $self->{open_elements}->[1]->[0];
4320              for my $attr_name (keys %{$token->{attributes}}) {
4321                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
4322                  $body_el->set_attribute_ns
4323                    (undef, [undef, $attr_name],
4324                     $token->{attributes}->{$attr_name}->{value});
4325                }
4326              }
4327          }          }
4328        } elsif ($token->{type} eq 'end tag') {          !!!next-token;
4329          if ($token->{tag_name} eq 'html') {          return;
4330            $previous_insertion_mode = $self->{insertion_mode};        } elsif ({
4331            $self->{insertion_mode} = 'trailing end';                  address => 1, blockquote => 1, center => 1, dir => 1,
4332                    div => 1, dl => 1, fieldset => 1, listing => 1,
4333                    menu => 1, ol => 1, p => 1, ul => 1,
4334                    pre => 1,
4335                   }->{$token->{tag_name}}) {
4336            ## has a p element in scope
4337            INSCOPE: for (reverse @{$self->{open_elements}}) {
4338              if ($_->[1] eq 'p') {
4339                !!!back-token;
4340                $token = {type => 'end tag', tag_name => 'p'};
4341                return;
4342              } elsif ({
4343                        table => 1, caption => 1, td => 1, th => 1,
4344                        button => 1, marquee => 1, object => 1, html => 1,
4345                       }->{$_->[1]}) {
4346                last INSCOPE;
4347              }
4348            } # INSCOPE
4349              
4350            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4351            if ($token->{tag_name} eq 'pre') {
4352            !!!next-token;            !!!next-token;
4353            redo B;            if ($token->{type} eq 'character') {
4354                $token->{data} =~ s/^\x0A//;
4355                unless (length $token->{data}) {
4356                  !!!next-token;
4357                }
4358              }
4359          } else {          } else {
4360            !!!parse-error (type => 'after frameset:/'.$token->{tag_name});            !!!next-token;
4361            }
4362            return;
4363          } elsif ($token->{tag_name} eq 'form') {
4364            if (defined $self->{form_element}) {
4365              !!!parse-error (type => 'in form:form');
4366            ## Ignore the token            ## Ignore the token
4367            !!!next-token;            !!!next-token;
4368            redo B;            return;
4369            } else {
4370              ## has a p element in scope
4371              INSCOPE: for (reverse @{$self->{open_elements}}) {
4372                if ($_->[1] eq 'p') {
4373                  !!!back-token;
4374                  $token = {type => 'end tag', tag_name => 'p'};
4375                  return;
4376                } elsif ({
4377                          table => 1, caption => 1, td => 1, th => 1,
4378                          button => 1, marquee => 1, object => 1, html => 1,
4379                         }->{$_->[1]}) {
4380                  last INSCOPE;
4381                }
4382              } # INSCOPE
4383                
4384              !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4385              $self->{form_element} = $self->{open_elements}->[-1]->[0];
4386              !!!next-token;
4387              return;
4388          }          }
4389        } else {        } elsif ($token->{tag_name} eq 'li') {
4390          die "$0: $token->{type}: Unknown token type";          ## has a p element in scope
4391        }          INSCOPE: for (reverse @{$self->{open_elements}}) {
4392              if ($_->[1] eq 'p') {
4393                !!!back-token;
4394                $token = {type => 'end tag', tag_name => 'p'};
4395                return;
4396              } elsif ({
4397                        table => 1, caption => 1, td => 1, th => 1,
4398                        button => 1, marquee => 1, object => 1, html => 1,
4399                       }->{$_->[1]}) {
4400                last INSCOPE;
4401              }
4402            } # INSCOPE
4403              
4404            ## Step 1
4405            my $i = -1;
4406            my $node = $self->{open_elements}->[$i];
4407            LI: {
4408              ## Step 2
4409              if ($node->[1] eq 'li') {
4410                if ($i != -1) {
4411                  !!!parse-error (type => 'end tag missing:'.
4412                                  $self->{open_elements}->[-1]->[1]);
4413                }
4414                splice @{$self->{open_elements}}, $i;
4415                last LI;
4416              }
4417              
4418              ## Step 3
4419              if (not $formatting_category->{$node->[1]} and
4420                  #not $phrasing_category->{$node->[1]} and
4421                  ($special_category->{$node->[1]} or
4422                   $scoping_category->{$node->[1]}) and
4423                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4424                last LI;
4425              }
4426              
4427              ## Step 4
4428              $i--;
4429              $node = $self->{open_elements}->[$i];
4430              redo LI;
4431            } # LI
4432              
4433            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4434            !!!next-token;
4435            return;
4436          } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {
4437            ## has a p element in scope
4438            INSCOPE: for (reverse @{$self->{open_elements}}) {
4439              if ($_->[1] eq 'p') {
4440                !!!back-token;
4441                $token = {type => 'end tag', tag_name => 'p'};
4442                return;
4443              } elsif ({
4444                        table => 1, caption => 1, td => 1, th => 1,
4445                        button => 1, marquee => 1, object => 1, html => 1,
4446                       }->{$_->[1]}) {
4447                last INSCOPE;
4448              }
4449            } # INSCOPE
4450              
4451            ## Step 1
4452            my $i = -1;
4453            my $node = $self->{open_elements}->[$i];
4454            LI: {
4455              ## Step 2
4456              if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {
4457                if ($i != -1) {
4458                  !!!parse-error (type => 'end tag missing:'.
4459                                  $self->{open_elements}->[-1]->[1]);
4460                }
4461                splice @{$self->{open_elements}}, $i;
4462                last LI;
4463              }
4464              
4465              ## Step 3
4466              if (not $formatting_category->{$node->[1]} and
4467                  #not $phrasing_category->{$node->[1]} and
4468                  ($special_category->{$node->[1]} or
4469                   $scoping_category->{$node->[1]}) and
4470                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4471                last LI;
4472              }
4473              
4474              ## Step 4
4475              $i--;
4476              $node = $self->{open_elements}->[$i];
4477              redo LI;
4478            } # LI
4479              
4480            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4481            !!!next-token;
4482            return;
4483          } elsif ($token->{tag_name} eq 'plaintext') {
4484            ## has a p element in scope
4485            INSCOPE: for (reverse @{$self->{open_elements}}) {
4486              if ($_->[1] eq 'p') {
4487                !!!back-token;
4488                $token = {type => 'end tag', tag_name => 'p'};
4489                return;
4490              } elsif ({
4491                        table => 1, caption => 1, td => 1, th => 1,
4492                        button => 1, marquee => 1, object => 1, html => 1,
4493                       }->{$_->[1]}) {
4494                last INSCOPE;
4495              }
4496            } # INSCOPE
4497              
4498            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4499              
4500            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
4501              
4502            !!!next-token;
4503            return;
4504          } elsif ({
4505                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4506                   }->{$token->{tag_name}}) {
4507            ## has a p element in scope
4508            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4509              my $node = $self->{open_elements}->[$_];
4510              if ($node->[1] eq 'p') {
4511                !!!back-token;
4512                $token = {type => 'end tag', tag_name => 'p'};
4513                return;
4514              } elsif ({
4515                        table => 1, caption => 1, td => 1, th => 1,
4516                        button => 1, marquee => 1, object => 1, html => 1,
4517                       }->{$node->[1]}) {
4518                last INSCOPE;
4519              }
4520            } # INSCOPE
4521              
4522            ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>
4523            ## has an element in scope
4524            #my $i;
4525            #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4526            #  my $node = $self->{open_elements}->[$_];
4527            #  if ({
4528            #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4529            #      }->{$node->[1]}) {
4530            #    $i = $_;
4531            #    last INSCOPE;
4532            #  } elsif ({
4533            #            table => 1, caption => 1, td => 1, th => 1,
4534            #            button => 1, marquee => 1, object => 1, html => 1,
4535            #           }->{$node->[1]}) {
4536            #    last INSCOPE;
4537            #  }
4538            #} # INSCOPE
4539            #  
4540            #if (defined $i) {
4541            #  !!! parse-error (type => 'in hn:hn');
4542            #  splice @{$self->{open_elements}}, $i;
4543            #}
4544              
4545            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4546              
4547            !!!next-token;
4548            return;
4549          } elsif ($token->{tag_name} eq 'a') {
4550            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
4551              my $node = $active_formatting_elements->[$i];
4552              if ($node->[1] eq 'a') {
4553                !!!parse-error (type => 'in a:a');
4554                
4555                !!!back-token;
4556                $token = {type => 'end tag', tag_name => 'a'};
4557                $formatting_end_tag->($token->{tag_name});
4558                
4559                AFE2: for (reverse 0..$#$active_formatting_elements) {
4560                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
4561                    splice @$active_formatting_elements, $_, 1;
4562                    last AFE2;
4563                  }
4564                } # AFE2
4565                OE: for (reverse 0..$#{$self->{open_elements}}) {
4566                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
4567                    splice @{$self->{open_elements}}, $_, 1;
4568                    last OE;
4569                  }
4570                } # OE
4571                last AFE;
4572              } elsif ($node->[0] eq '#marker') {
4573                last AFE;
4574              }
4575            } # AFE
4576              
4577            $reconstruct_active_formatting_elements->($insert_to_current);
4578    
4579        ## ISSUE: An issue in spec here          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4580      } elsif ($self->{insertion_mode} eq 'trailing end') {          push @$active_formatting_elements, $self->{open_elements}->[-1];
4581        ## states in the main stage is preserved yet # MUST  
4582                  !!!next-token;
4583        if ($token->{type} eq 'character') {          return;
4584          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        } elsif ({
4585            my $data = $1;                  b => 1, big => 1, em => 1, font => 1, i => 1,
4586            ## As if in the main phase.                  s => 1, small => 1, strile => 1,
4587            ## NOTE: The insertion mode in the main phase                  strong => 1, tt => 1, u => 1,
4588            ## just before the phase has been changed to the trailing                 }->{$token->{tag_name}}) {
4589            ## end phase is either "after body" or "after frameset".          $reconstruct_active_formatting_elements->($insert_to_current);
4590            $reconstruct_active_formatting_elements->($insert_to_current);          
4591            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4592            push @$active_formatting_elements, $self->{open_elements}->[-1];
4593            
4594            !!!next-token;
4595            return;
4596          } elsif ($token->{tag_name} eq 'nobr') {
4597            $reconstruct_active_formatting_elements->($insert_to_current);
4598    
4599            ## has a |nobr| element in scope
4600            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4601              my $node = $self->{open_elements}->[$_];
4602              if ($node->[1] eq 'nobr') {
4603                !!!parse-error (type => 'not closed:nobr');
4604                !!!back-token;
4605                $token = {type => 'end tag', tag_name => 'nobr'};
4606                return;
4607              } elsif ({
4608                        table => 1, caption => 1, td => 1, th => 1,
4609                        button => 1, marquee => 1, object => 1, html => 1,
4610                       }->{$node->[1]}) {
4611                last INSCOPE;
4612              }
4613            } # INSCOPE
4614            
4615            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4616            push @$active_formatting_elements, $self->{open_elements}->[-1];
4617            
4618            !!!next-token;
4619            return;
4620          } elsif ($token->{tag_name} eq 'button') {
4621            ## has a button element in scope
4622            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4623              my $node = $self->{open_elements}->[$_];
4624              if ($node->[1] eq 'button') {
4625                !!!parse-error (type => 'in button:button');
4626                !!!back-token;
4627                $token = {type => 'end tag', tag_name => 'button'};
4628                return;
4629              } elsif ({
4630                        table => 1, caption => 1, td => 1, th => 1,
4631                        button => 1, marquee => 1, object => 1, html => 1,
4632                       }->{$node->[1]}) {
4633                last INSCOPE;
4634              }
4635            } # INSCOPE
4636              
4637            $reconstruct_active_formatting_elements->($insert_to_current);
4638              
4639            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4640            push @$active_formatting_elements, ['#marker', ''];
4641    
4642            !!!next-token;
4643            return;
4644          } elsif ($token->{tag_name} eq 'marquee' or
4645                   $token->{tag_name} eq 'object') {
4646            $reconstruct_active_formatting_elements->($insert_to_current);
4647            
4648            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4649            push @$active_formatting_elements, ['#marker', ''];
4650            
4651            !!!next-token;
4652            return;
4653          } elsif ($token->{tag_name} eq 'xmp') {
4654            $reconstruct_active_formatting_elements->($insert_to_current);
4655            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4656            return;
4657          } elsif ($token->{tag_name} eq 'table') {
4658            ## has a p element in scope
4659            INSCOPE: for (reverse @{$self->{open_elements}}) {
4660              if ($_->[1] eq 'p') {
4661                !!!back-token;
4662                $token = {type => 'end tag', tag_name => 'p'};
4663                return;
4664              } elsif ({
4665                        table => 1, caption => 1, td => 1, th => 1,
4666                        button => 1, marquee => 1, object => 1, html => 1,
4667                       }->{$_->[1]}) {
4668                last INSCOPE;
4669              }
4670            } # INSCOPE
4671              
4672            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4673              
4674            $self->{insertion_mode} = 'in table';
4675                        
4676            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);          !!!next-token;
4677            return;
4678          } elsif ({
4679                    area => 1, basefont => 1, bgsound => 1, br => 1,
4680                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
4681                    image => 1,
4682                   }->{$token->{tag_name}}) {
4683            if ($token->{tag_name} eq 'image') {
4684              !!!parse-error (type => 'image');
4685              $token->{tag_name} = 'img';
4686            }
4687    
4688            ## NOTE: There is an "as if <br>" code clone.
4689            $reconstruct_active_formatting_elements->($insert_to_current);
4690            
4691            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4692            pop @{$self->{open_elements}};
4693            
4694            !!!next-token;
4695            return;
4696          } elsif ($token->{tag_name} eq 'hr') {
4697            ## has a p element in scope
4698            INSCOPE: for (reverse @{$self->{open_elements}}) {
4699              if ($_->[1] eq 'p') {
4700                !!!back-token;
4701                $token = {type => 'end tag', tag_name => 'p'};
4702                return;
4703              } elsif ({
4704                        table => 1, caption => 1, td => 1, th => 1,
4705                        button => 1, marquee => 1, object => 1, html => 1,
4706                       }->{$_->[1]}) {
4707                last INSCOPE;
4708              }
4709            } # INSCOPE
4710              
4711            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4712            pop @{$self->{open_elements}};
4713                        
4714            !!!next-token;
4715            return;
4716          } elsif ($token->{tag_name} eq 'input') {
4717            $reconstruct_active_formatting_elements->($insert_to_current);
4718            
4719            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4720            ## TODO: associate with $self->{form_element} if defined
4721            pop @{$self->{open_elements}};
4722            
4723            !!!next-token;
4724            return;
4725          } elsif ($token->{tag_name} eq 'isindex') {
4726            !!!parse-error (type => 'isindex');
4727            
4728            if (defined $self->{form_element}) {
4729              ## Ignore the token
4730              !!!next-token;
4731              return;
4732            } else {
4733              my $at = $token->{attributes};
4734              my $form_attrs;
4735              $form_attrs->{action} = $at->{action} if $at->{action};
4736              my $prompt_attr = $at->{prompt};
4737              $at->{name} = {name => 'name', value => 'isindex'};
4738              delete $at->{action};
4739              delete $at->{prompt};
4740              my @tokens = (
4741                            {type => 'start tag', tag_name => 'form',
4742                             attributes => $form_attrs},
4743                            {type => 'start tag', tag_name => 'hr'},
4744                            {type => 'start tag', tag_name => 'p'},
4745                            {type => 'start tag', tag_name => 'label'},
4746                           );
4747              if ($prompt_attr) {
4748                push @tokens, {type => 'character', data => $prompt_attr->{value}};
4749              } else {
4750                push @tokens, {type => 'character',
4751                               data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
4752                ## TODO: make this configurable
4753              }
4754              push @tokens,
4755                            {type => 'start tag', tag_name => 'input', attributes => $at},
4756                            #{type => 'character', data => ''}, # SHOULD
4757                            {type => 'end tag', tag_name => 'label'},
4758                            {type => 'end tag', tag_name => 'p'},
4759                            {type => 'start tag', tag_name => 'hr'},
4760                            {type => 'end tag', tag_name => 'form'};
4761              $token = shift @tokens;
4762              !!!back-token (@tokens);
4763              return;
4764            }
4765          } elsif ($token->{tag_name} eq 'textarea') {
4766            my $tag_name = $token->{tag_name};
4767            my $el;
4768            !!!create-element ($el, $token->{tag_name}, $token->{attributes});
4769            
4770            ## TODO: $self->{form_element} if defined
4771            $self->{content_model} = RCDATA_CONTENT_MODEL;
4772            delete $self->{escape}; # MUST
4773            
4774            $insert->($el);
4775            
4776            my $text = '';
4777            !!!next-token;
4778            if ($token->{type} eq 'character') {
4779              $token->{data} =~ s/^\x0A//;
4780            unless (length $token->{data}) {            unless (length $token->{data}) {
4781              !!!next-token;              !!!next-token;
             redo B;  
4782            }            }
4783          }          }
4784            while ($token->{type} eq 'character') {
4785              $text .= $token->{data};
4786              !!!next-token;
4787            }
4788            if (length $text) {
4789              $el->manakai_append_text ($text);
4790            }
4791            
4792            $self->{content_model} = PCDATA_CONTENT_MODEL;
4793            
4794            if ($token->{type} eq 'end tag' and
4795                $token->{tag_name} eq $tag_name) {
4796              ## Ignore the token
4797            } else {
4798              !!!parse-error (type => 'in RCDATA:#'.$token->{type});
4799            }
4800            !!!next-token;
4801            return;
4802          } elsif ({
4803                    iframe => 1,
4804                    noembed => 1,
4805                    noframes => 1,
4806                    noscript => 0, ## TODO: 1 if scripting is enabled
4807                   }->{$token->{tag_name}}) {
4808            ## NOTE: There are two "as if in body" code clones.
4809            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4810            return;
4811          } elsif ($token->{tag_name} eq 'select') {
4812            $reconstruct_active_formatting_elements->($insert_to_current);
4813            
4814            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4815            
4816            $self->{insertion_mode} = 'in select';
4817            !!!next-token;
4818            return;
4819          } elsif ({
4820                    caption => 1, col => 1, colgroup => 1, frame => 1,
4821                    frameset => 1, head => 1, option => 1, optgroup => 1,
4822                    tbody => 1, td => 1, tfoot => 1, th => 1,
4823                    thead => 1, tr => 1,
4824                   }->{$token->{tag_name}}) {
4825            !!!parse-error (type => 'in body:'.$token->{tag_name});
4826            ## Ignore the token
4827            !!!next-token;
4828            return;
4829            
4830            ## ISSUE: An issue on HTML5 new elements in the spec.
4831          } else {
4832            $reconstruct_active_formatting_elements->($insert_to_current);
4833            
4834            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4835            
4836            !!!next-token;
4837            return;
4838          }
4839        } elsif ($token->{type} eq 'end tag') {
4840          if ($token->{tag_name} eq 'body') {
4841            if (@{$self->{open_elements}} > 1 and
4842                $self->{open_elements}->[1]->[1] eq 'body') {
4843              for (@{$self->{open_elements}}) {
4844                unless ({
4845                           dd => 1, dt => 1, li => 1, p => 1, td => 1,
4846                           th => 1, tr => 1, body => 1, html => 1,
4847                         tbody => 1, tfoot => 1, thead => 1,
4848                        }->{$_->[1]}) {
4849                  !!!parse-error (type => 'not closed:'.$_->[1]);
4850                }
4851              }
4852    
4853          !!!parse-error (type => 'after html:#character');            $self->{insertion_mode} = 'after body';
4854          $self->{insertion_mode} = $previous_insertion_mode;            !!!next-token;
4855          ## reprocess            return;
4856          redo B;          } else {
4857        } elsif ($token->{type} eq 'start tag') {            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4858          !!!parse-error (type => 'after html:'.$token->{tag_name});            ## Ignore the token
4859          $self->{insertion_mode} = $previous_insertion_mode;            !!!next-token;
4860          ## reprocess            return;
4861          redo B;          }
4862        } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{tag_name} eq 'html') {
4863          !!!parse-error (type => 'after html:/'.$token->{tag_name});          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
4864          $self->{insertion_mode} = $previous_insertion_mode;            ## ISSUE: There is an issue in the spec.
4865          ## reprocess            if ($self->{open_elements}->[-1]->[1] ne 'body') {
4866          redo B;              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
4867              }
4868              $self->{insertion_mode} = 'after body';
4869              ## reprocess
4870              return;
4871            } else {
4872              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4873              ## Ignore the token
4874              !!!next-token;
4875              return;
4876            }
4877          } elsif ({
4878                    address => 1, blockquote => 1, center => 1, dir => 1,
4879                    div => 1, dl => 1, fieldset => 1, listing => 1,
4880                    menu => 1, ol => 1, pre => 1, ul => 1,
4881                    p => 1,
4882                    dd => 1, dt => 1, li => 1,
4883                    button => 1, marquee => 1, object => 1,
4884                   }->{$token->{tag_name}}) {
4885            ## has an element in scope
4886            my $i;
4887            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4888              my $node = $self->{open_elements}->[$_];
4889              if ($node->[1] eq $token->{tag_name}) {
4890                ## generate implied end tags
4891                if ({
4892                     dd => ($token->{tag_name} ne 'dd'),
4893                     dt => ($token->{tag_name} ne 'dt'),
4894                     li => ($token->{tag_name} ne 'li'),
4895                     p => ($token->{tag_name} ne 'p'),
4896                     td => 1, th => 1, tr => 1,
4897                     tbody => 1, tfoot=> 1, thead => 1,
4898                    }->{$self->{open_elements}->[-1]->[1]}) {
4899                  !!!back-token;
4900                  $token = {type => 'end tag',
4901                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4902                  return;
4903                }
4904                $i = $_;
4905                last INSCOPE unless $token->{tag_name} eq 'p';
4906              } elsif ({
4907                        table => 1, caption => 1, td => 1, th => 1,
4908                        button => 1, marquee => 1, object => 1, html => 1,
4909                       }->{$node->[1]}) {
4910                last INSCOPE;
4911              }
4912            } # INSCOPE
4913            
4914            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
4915              if (defined $i) {
4916                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4917              } else {
4918                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4919              }
4920            }
4921            
4922            if (defined $i) {
4923              splice @{$self->{open_elements}}, $i;
4924            } elsif ($token->{tag_name} eq 'p') {
4925              ## As if <p>, then reprocess the current token
4926              my $el;
4927              !!!create-element ($el, 'p');
4928              $insert->($el);
4929            }
4930            $clear_up_to_marker->()
4931              if {
4932                button => 1, marquee => 1, object => 1,
4933              }->{$token->{tag_name}};
4934            !!!next-token;
4935            return;
4936          } elsif ($token->{tag_name} eq 'form') {
4937            ## has an element in scope
4938            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4939              my $node = $self->{open_elements}->[$_];
4940              if ($node->[1] eq $token->{tag_name}) {
4941                ## generate implied end tags
4942                if ({
4943                     dd => 1, dt => 1, li => 1, p => 1,
4944                     td => 1, th => 1, tr => 1,
4945                     tbody => 1, tfoot=> 1, thead => 1,
4946                    }->{$self->{open_elements}->[-1]->[1]}) {
4947                  !!!back-token;
4948                  $token = {type => 'end tag',
4949                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4950                  return;
4951                }
4952                last INSCOPE;
4953              } elsif ({
4954                        table => 1, caption => 1, td => 1, th => 1,
4955                        button => 1, marquee => 1, object => 1, html => 1,
4956                       }->{$node->[1]}) {
4957                last INSCOPE;
4958              }
4959            } # INSCOPE
4960            
4961            if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
4962              pop @{$self->{open_elements}};
4963            } else {
4964              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4965            }
4966    
4967            undef $self->{form_element};
4968            !!!next-token;
4969            return;
4970          } elsif ({
4971                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4972                   }->{$token->{tag_name}}) {
4973            ## has an element in scope
4974            my $i;
4975            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4976              my $node = $self->{open_elements}->[$_];
4977              if ({
4978                   h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4979                  }->{$node->[1]}) {
4980                ## generate implied end tags
4981                if ({
4982                     dd => 1, dt => 1, li => 1, p => 1,
4983                     td => 1, th => 1, tr => 1,
4984                     tbody => 1, tfoot=> 1, thead => 1,
4985                    }->{$self->{open_elements}->[-1]->[1]}) {
4986                  !!!back-token;
4987                  $token = {type => 'end tag',
4988                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4989                  return;
4990                }
4991                $i = $_;
4992                last INSCOPE;
4993              } elsif ({
4994                        table => 1, caption => 1, td => 1, th => 1,
4995                        button => 1, marquee => 1, object => 1, html => 1,
4996                       }->{$node->[1]}) {
4997                last INSCOPE;
4998              }
4999            } # INSCOPE
5000            
5001            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5002              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5003            }
5004            
5005            splice @{$self->{open_elements}}, $i if defined $i;
5006            !!!next-token;
5007            return;
5008          } elsif ({
5009                    a => 1,
5010                    b => 1, big => 1, em => 1, font => 1, i => 1,
5011                    nobr => 1, s => 1, small => 1, strile => 1,
5012                    strong => 1, tt => 1, u => 1,
5013                   }->{$token->{tag_name}}) {
5014            $formatting_end_tag->($token->{tag_name});
5015            return;
5016          } elsif ($token->{tag_name} eq 'br') {
5017            !!!parse-error (type => 'unmatched end tag:br');
5018    
5019            ## As if <br>
5020            $reconstruct_active_formatting_elements->($insert_to_current);
5021            
5022            my $el;
5023            !!!create-element ($el, 'br');
5024            $insert->($el);
5025            
5026            ## Ignore the token.
5027            !!!next-token;
5028            return;
5029          } elsif ({
5030                    caption => 1, col => 1, colgroup => 1, frame => 1,
5031                    frameset => 1, head => 1, option => 1, optgroup => 1,
5032                    tbody => 1, td => 1, tfoot => 1, th => 1,
5033                    thead => 1, tr => 1,
5034                    area => 1, basefont => 1, bgsound => 1,
5035                    embed => 1, hr => 1, iframe => 1, image => 1,
5036                    img => 1, input => 1, isindex => 1, noembed => 1,
5037                    noframes => 1, param => 1, select => 1, spacer => 1,
5038                    table => 1, textarea => 1, wbr => 1,
5039                    noscript => 0, ## TODO: if scripting is enabled
5040                   }->{$token->{tag_name}}) {
5041            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5042            ## Ignore the token
5043            !!!next-token;
5044            return;
5045            
5046            ## ISSUE: Issue on HTML5 new elements in spec
5047            
5048        } else {        } else {
5049          die "$0: $token->{type}: Unknown token";          ## Step 1
5050            my $node_i = -1;
5051            my $node = $self->{open_elements}->[$node_i];
5052    
5053            ## Step 2
5054            S2: {
5055              if ($node->[1] eq $token->{tag_name}) {
5056                ## Step 1
5057                ## generate implied end tags
5058                if ({
5059                     dd => 1, dt => 1, li => 1, p => 1,
5060                     td => 1, th => 1, tr => 1,
5061                     tbody => 1, tfoot=> 1, thead => 1,
5062                    }->{$self->{open_elements}->[-1]->[1]}) {
5063                  !!!back-token;
5064                  $token = {type => 'end tag',
5065                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5066                  return;
5067                }
5068            
5069                ## Step 2
5070                if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
5071                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5072                }
5073                
5074                ## Step 3
5075                splice @{$self->{open_elements}}, $node_i;
5076    
5077                !!!next-token;
5078                last S2;
5079              } else {
5080                ## Step 3
5081                if (not $formatting_category->{$node->[1]} and
5082                    #not $phrasing_category->{$node->[1]} and
5083                    ($special_category->{$node->[1]} or
5084                     $scoping_category->{$node->[1]})) {
5085                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5086                  ## Ignore the token
5087                  !!!next-token;
5088                  last S2;
5089                }
5090              }
5091              
5092              ## Step 4
5093              $node_i--;
5094              $node = $self->{open_elements}->[$node_i];
5095              
5096              ## Step 5;
5097              redo S2;
5098            } # S2
5099            return;
5100        }        }
     } else {  
       die "$0: $self->{insertion_mode}: Unknown insertion mode";  
5101      }      }
5102      }; # $in_body
5103        $in_body->($insert_to_current);
5104        redo B;
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

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24