/[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.43 by wakaba, Sat Jul 21 07:21:44 2007 UTC revision 1.54 by wakaba, Sat Aug 11 06:37:12 2007 UTC
# Line 159  sub CDATA_CONTENT_MODEL () { CM_LIMITED_ Line 159  sub CDATA_CONTENT_MODEL () { CM_LIMITED_
159  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
160  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
161    
162    sub AFTER_HTML_IMS () { 0b100 }
163    sub HEAD_IMS ()       { 0b1000 }
164    sub BODY_IMS ()       { 0b10000 }
165    sub BODY_TABLE_IMS () { 0b100000 | BODY_IMS }
166    sub TABLE_IMS ()      { 0b1000000 }
167    sub ROW_IMS ()        { 0b10000000 | TABLE_IMS }
168    sub BODY_AFTER_IMS () { 0b100000000 }
169    sub FRAME_IMS ()      { 0b1000000000 }
170    
171    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
172    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
173    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
174    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
175    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
176    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
177    sub IN_BODY_IM () { BODY_IMS }
178    sub IN_CELL_IM () { BODY_TABLE_IMS | 0b01 }
179    sub IN_CAPTION_IM () { BODY_TABLE_IMS | 0b10 }
180    sub IN_ROW_IM () { ROW_IMS | 0b01 }
181    sub IN_TABLE_BODY_IM () { ROW_IMS | 0b10 }
182    sub IN_TABLE_IM () { TABLE_IMS }
183    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
184    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
185    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
186    sub IN_SELECT_IM () { 0b01 }
187    sub IN_COLUMN_GROUP_IM () { 0b10 }
188    
189  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
190    
191  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
# Line 1806  sub _construct_tree ($) { Line 1833  sub _construct_tree ($) {
1833        
1834    !!!next-token;    !!!next-token;
1835    
1836    $self->{insertion_mode} = 'before head';    $self->{insertion_mode} = BEFORE_HEAD_IM;
1837    undef $self->{form_element};    undef $self->{form_element};
1838    undef $self->{head_element};    undef $self->{head_element};
1839    $self->{open_elements} = [];    $self->{open_elements} = [];
# Line 2062  sub _reset_insertion_mode ($) { Line 2089  sub _reset_insertion_mode ($) {
2089            
2090        ## Step 4..13        ## Step 4..13
2091        my $new_mode = {        my $new_mode = {
2092                        select => 'in select',                        select => IN_SELECT_IM,
2093                        td => 'in cell',                        td => IN_CELL_IM,
2094                        th => 'in cell',                        th => IN_CELL_IM,
2095                        tr => 'in row',                        tr => IN_ROW_IM,
2096                        tbody => 'in table body',                        tbody => IN_TABLE_BODY_IM,
2097                        thead => 'in table head',                        thead => IN_TABLE_BODY_IM,
2098                        tfoot => 'in table foot',                        tfoot => IN_TABLE_BODY_IM,
2099                        caption => 'in caption',                        caption => IN_CAPTION_IM,
2100                        colgroup => 'in column group',                        colgroup => IN_COLUMN_GROUP_IM,
2101                        table => 'in table',                        table => IN_TABLE_IM,
2102                        head => 'in body', # not in head!                        head => IN_BODY_IM, # not in head!
2103                        body => 'in body',                        body => IN_BODY_IM,
2104                        frameset => 'in frameset',                        frameset => IN_FRAMESET_IM,
2105                       }->{$node->[1]};                       }->{$node->[1]};
2106        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2107                
2108        ## Step 14        ## Step 14
2109        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2110          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2111            $self->{insertion_mode} = 'before head';            $self->{insertion_mode} = BEFORE_HEAD_IM;
2112          } else {          } else {
2113            $self->{insertion_mode} = 'after head';            $self->{insertion_mode} = AFTER_HEAD_IM;
2114          }          }
2115          return;          return;
2116        }        }
2117                
2118        ## Step 15        ## Step 15
2119        $self->{insertion_mode} = 'in body' and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
2120                
2121        ## Step 16        ## Step 16
2122        $i--;        $i--;
# Line 2103  sub _reset_insertion_mode ($) { Line 2130  sub _reset_insertion_mode ($) {
2130  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
2131    my $self = shift;    my $self = shift;
2132    
   my $previous_insertion_mode;  
   
2133    my $active_formatting_elements = [];    my $active_formatting_elements = [];
2134    
2135    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 2496  sub _tree_construction_main ($) { Line 2521  sub _tree_construction_main ($) {
2521                         }                         }
2522    }; # $insert_to_foster    }; # $insert_to_foster
2523    
2524    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}}) {  
         $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  
2525    
2526    B: {    B: {
2527      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} eq 'DOCTYPE') {
# Line 3349  sub _tree_construction_main ($) { Line 2531  sub _tree_construction_main ($) {
2531        !!!next-token;        !!!next-token;
2532        redo B;        redo B;
2533      } elsif ($token->{type} eq 'end-of-file') {      } elsif ($token->{type} eq 'end-of-file') {
2534        if ($token->{insertion_mode} ne 'trailing end') {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM or
2535              $self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
2536            #
2537          } else {
2538          ## Generate implied end tags          ## Generate implied end tags
2539          if ({          if ({
2540               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 3376  sub _tree_construction_main ($) { Line 2561  sub _tree_construction_main ($) {
2561        last B;        last B;
2562      } elsif ($token->{type} eq 'start tag' and      } elsif ($token->{type} eq 'start tag' and
2563               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
2564        if ($self->{insertion_mode} eq 'trailing end') {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
2565          ## Turn into the main phase          ## Turn into the main phase
2566          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html');
2567          $self->{insertion_mode} = $previous_insertion_mode;          $self->{insertion_mode} = AFTER_BODY_IM;
2568          } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
2569            ## Turn into the main phase
2570            !!!parse-error (type => 'after html:html');
2571            $self->{insertion_mode} = AFTER_FRAMESET_IM;
2572        }        }
2573    
2574  ## ISSUE: "aa<html>" is not a parse error.  ## ISSUE: "aa<html>" is not a parse error.
# Line 3399  sub _tree_construction_main ($) { Line 2588  sub _tree_construction_main ($) {
2588        redo B;        redo B;
2589      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} eq 'comment') {
2590        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2591        if ($self->{insertion_mode} eq 'trailing end') {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM or
2592              $self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
2593          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2594        } elsif ($self->{insertion_mode} eq 'after body') {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
2595          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
2596        } else {        } else {
2597          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
2598        }        }
2599        !!!next-token;        !!!next-token;
2600        redo B;        redo B;
2601      } elsif ($self->{insertion_mode} eq 'before head') {      } elsif ($self->{insertion_mode} == IN_HEAD_IM or
2602            if ($token->{type} eq 'character') {               $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM or
2603              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {               $self->{insertion_mode} == AFTER_HEAD_IM or
2604                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);               $self->{insertion_mode} == BEFORE_HEAD_IM) {
2605                unless (length $token->{data}) {        if ($token->{type} eq 'character') {
2606                  !!!next-token;          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
2607                  redo B;            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
2608                }            unless (length $token->{data}) {
2609              }              !!!next-token;
2610              ## As if <head>              redo B;
2611              !!!create-element ($self->{head_element}, 'head');            }
2612              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});          }
2613              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
2614              $self->{insertion_mode} = 'in head';          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2615              ## As if <head>
2616              !!!create-element ($self->{head_element}, 'head');
2617              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2618              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2619    
2620              ## Reprocess in the "in head" insertion mode...
2621              pop @{$self->{open_elements}};
2622    
2623              ## Reprocess in the "after head" insertion mode...
2624            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2625              ## As if </noscript>
2626              pop @{$self->{open_elements}};
2627              !!!parse-error (type => 'in noscript:#character');
2628              
2629              ## Reprocess in the "in head" insertion mode...
2630              ## As if </head>
2631              pop @{$self->{open_elements}};
2632    
2633              ## Reprocess in the "after head" insertion mode...
2634            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2635              pop @{$self->{open_elements}};
2636    
2637              ## Reprocess in the "after head" insertion mode...
2638            }
2639    
2640                ## "after head" insertion mode
2641                ## As if <body>
2642                !!!insert-element ('body');
2643                $self->{insertion_mode} = IN_BODY_IM;
2644              ## reprocess              ## reprocess
2645              redo B;              redo B;
2646            } 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';  
2647              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
2648                !!!next-token;                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2649              #} elsif ({                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
2650              #          base => 1, link => 1, meta => 1,                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2651              #          script => 1, style => 1, title => 1,                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
2652              #         }->{$token->{tag_name}}) {                  $self->{insertion_mode} = IN_HEAD_IM;
2653              #  ## reprocess                  !!!next-token;
2654              } else {                  redo B;
2655                ## reprocess                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2656              }                  #
2657              redo B;                } else {
2658            } elsif ($token->{type} eq 'end tag') {                  !!!parse-error (type => 'in head:head'); # or in head noscript
2659              if ({                  ## Ignore the token
2660                   head => 1, body => 1, html => 1,                  !!!next-token;
2661                   p => 1, br => 1,                  redo B;
2662                  }->{$token->{tag_name}}) {                }
2663                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2664                ## As if <head>                ## As if <head>
2665                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
2666                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2667                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2668                $self->{insertion_mode} = 'in head';  
2669                ## reprocess                $self->{insertion_mode} = IN_HEAD_IM;
2670                redo B;                ## Reprocess in the "in head" insertion mode...
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token ## ISSUE: An issue in the spec.  
               !!!next-token;  
               redo B;  
2671              }              }
2672            } else {  
2673              die "$0: $token->{type}: Unknown type";              if ($token->{tag_name} eq 'base') {
2674            }                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2675          } elsif ($self->{insertion_mode} eq 'in head' or                  ## As if </noscript>
2676                   $self->{insertion_mode} eq 'in head noscript' or                  pop @{$self->{open_elements}};
2677                   $self->{insertion_mode} eq 'after head') {                  !!!parse-error (type => 'in noscript:base');
2678            if ($token->{type} eq 'character') {                
2679              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                  $self->{insertion_mode} = IN_HEAD_IM;
2680                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                  ## Reprocess in the "in head" insertion mode...
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
2681                }                }
2682              }  
2683                              ## NOTE: There is a "as if in head" code clone.
2684              #                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2685            } elsif ($token->{type} eq 'start tag') {                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2686              if ({base => ($self->{insertion_mode} eq 'in head' or                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2687                            $self->{insertion_mode} eq 'after head'),                }
2688                   link => 1}->{$token->{tag_name}}) {                !!!insert-element ($token->{tag_name}, $token->{attributes});
2689                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2690                  pop @{$self->{open_elements}}
2691                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2692                  !!!next-token;
2693                  redo B;
2694                } elsif ($token->{tag_name} eq 'link') {
2695                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2696                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2697                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2698                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2699                }                }
2700                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
2701                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2702                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2703                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2704                !!!next-token;                !!!next-token;
2705                redo B;                redo B;
2706              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
2707                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2708                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2709                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2710                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2711                }                }
# Line 3518  sub _tree_construction_main ($) { Line 2731  sub _tree_construction_main ($) {
2731    
2732                ## TODO: Extracting |charset| from |meta|.                ## TODO: Extracting |charset| from |meta|.
2733                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2734                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2735                !!!next-token;                !!!next-token;
2736                redo B;                redo B;
2737              } elsif ($token->{tag_name} eq 'title' and              } elsif ($token->{tag_name} eq 'title') {
2738                       $self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2739                ## NOTE: There is a "as if in head" code clone.                  ## As if </noscript>
2740                if ($self->{insertion_mode} eq 'after head') {                  pop @{$self->{open_elements}};
2741                    !!!parse-error (type => 'in noscript:title');
2742                  
2743                    $self->{insertion_mode} = IN_HEAD_IM;
2744                    ## Reprocess in the "in head" insertion mode...
2745                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2746                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2747                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2748                }                }
2749    
2750                  ## NOTE: There is a "as if in head" code clone.
2751                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
2752                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
2753                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL,
2754                                sub { $parent->append_child ($_[0]) });                                sub { $parent->append_child ($_[0]) });
2755                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2756                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2757                redo B;                redo B;
2758              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
2759                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
2760                ## insertion mode 'in head')                ## insertion mode IN_HEAD_IM)
2761                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2762                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2763                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2764                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2765                }                }
2766                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
2767                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2768                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2769                redo B;                redo B;
2770              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
2771                if ($self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_IM) {
2772                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
2773                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2774                  $self->{insertion_mode} = 'in head noscript';                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
2775                  !!!next-token;                  !!!next-token;
2776                  redo B;                  redo B;
2777                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2778                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript:noscript');
2779                  ## Ignore the token                  ## Ignore the token
2780                  !!!next-token;                  !!!next-token;
# Line 3562  sub _tree_construction_main ($) { Line 2782  sub _tree_construction_main ($) {
2782                } else {                } else {
2783                  #                  #
2784                }                }
2785              } elsif ($token->{tag_name} eq 'head' and              } elsif ($token->{tag_name} eq 'script') {
2786                       $self->{insertion_mode} ne 'after head') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2787                !!!parse-error (type => 'in head:head'); # or in head noscript                  ## As if </noscript>
2788                ## Ignore the token                  pop @{$self->{open_elements}};
2789                !!!next-token;                  !!!parse-error (type => 'in noscript:script');
2790                redo B;                
2791              } elsif ($self->{insertion_mode} ne 'in head noscript' and                  $self->{insertion_mode} = IN_HEAD_IM;
2792                       $token->{tag_name} eq 'script') {                  ## Reprocess in the "in head" insertion mode...
2793                if ($self->{insertion_mode} eq 'after head') {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2794                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2795                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2796                }                }
2797    
2798                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2799                $script_start_tag->($insert_to_current);                $script_start_tag->($insert_to_current);
2800                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2801                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
               redo B;  
             } elsif ($self->{insertion_mode} eq 'after head' and  
                      $token->{tag_name} eq 'body') {  
               !!!insert-element ('body', $token->{attributes});  
               $self->{insertion_mode} = 'in body';  
               !!!next-token;  
2802                redo B;                redo B;
2803              } elsif ($self->{insertion_mode} eq 'after head' and              } elsif ($token->{tag_name} eq 'body' or
2804                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
2805                !!!insert-element ('frameset', $token->{attributes});                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2806                $self->{insertion_mode} = 'in frameset';                  ## As if </noscript>
2807                    pop @{$self->{open_elements}};
2808                    !!!parse-error (type => 'in noscript:'.$token->{tag_name});
2809                    
2810                    ## Reprocess in the "in head" insertion mode...
2811                    ## As if </head>
2812                    pop @{$self->{open_elements}};
2813                    
2814                    ## Reprocess in the "after head" insertion mode...
2815                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2816                    pop @{$self->{open_elements}};
2817                    
2818                    ## Reprocess in the "after head" insertion mode...
2819                  }
2820    
2821                  ## "after head" insertion mode
2822                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2823                  if ($token->{tag_name} eq 'body') {
2824                    $self->{insertion_mode} = IN_BODY_IM;
2825                  } elsif ($token->{tag_name} eq 'frameset') {
2826                    $self->{insertion_mode} = IN_FRAMESET_IM;
2827                  } else {
2828                    die "$0: tag name: $self->{tag_name}";
2829                  }
2830                !!!next-token;                !!!next-token;
2831                redo B;                redo B;
2832              } else {              } else {
2833                #                #
2834              }              }
2835            } elsif ($token->{type} eq 'end tag') {  
2836              if ($self->{insertion_mode} eq 'in head' and              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2837                  $token->{tag_name} eq 'head') {                ## As if </noscript>
2838                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
2839                $self->{insertion_mode} = 'after head';                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
2840                !!!next-token;                
2841                redo B;                ## Reprocess in the "in head" insertion mode...
2842              } elsif ($self->{insertion_mode} eq 'in head noscript' and                ## As if </head>
                 $token->{tag_name} eq 'noscript') {  
2843                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
2844                $self->{insertion_mode} = 'in head';  
2845                !!!next-token;                ## Reprocess in the "after head" insertion mode...
2846                redo B;              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2847              } elsif ($self->{insertion_mode} eq 'in head' and                ## As if </head>
2848                       {                pop @{$self->{open_elements}};
2849    
2850                  ## Reprocess in the "after head" insertion mode...
2851                }
2852    
2853                ## "after head" insertion mode
2854                ## As if <body>
2855                !!!insert-element ('body');
2856                $self->{insertion_mode} = IN_BODY_IM;
2857                ## reprocess
2858                redo B;
2859              } elsif ($token->{type} eq 'end tag') {
2860                if ($token->{tag_name} eq 'head') {
2861                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2862                    ## As if <head>
2863                    !!!create-element ($self->{head_element}, 'head');
2864                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2865                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2866    
2867                    ## Reprocess in the "in head" insertion mode...
2868                    pop @{$self->{open_elements}};
2869                    $self->{insertion_mode} = AFTER_HEAD_IM;
2870                    !!!next-token;
2871                    redo B;
2872                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2873                    ## As if </noscript>
2874                    pop @{$self->{open_elements}};
2875                    !!!parse-error (type => 'in noscript:script');
2876                    
2877                    ## Reprocess in the "in head" insertion mode...
2878                    pop @{$self->{open_elements}};
2879                    $self->{insertion_mode} = AFTER_HEAD_IM;
2880                    !!!next-token;
2881                    redo B;
2882                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2883                    pop @{$self->{open_elements}};
2884                    $self->{insertion_mode} = AFTER_HEAD_IM;
2885                    !!!next-token;
2886                    redo B;
2887                  } else {
2888                    #
2889                  }
2890                } elsif ($token->{tag_name} eq 'noscript') {
2891                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2892                    pop @{$self->{open_elements}};
2893                    $self->{insertion_mode} = IN_HEAD_IM;
2894                    !!!next-token;
2895                    redo B;
2896                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2897                    !!!parse-error (type => 'unmatched end tag:noscript');
2898                    ## Ignore the token ## ISSUE: An issue in the spec.
2899                    !!!next-token;
2900                    redo B;
2901                  } else {
2902                    #
2903                  }
2904                } elsif ({
2905                        body => 1, html => 1,                        body => 1, html => 1,
                       p => 1, br => 1,  
2906                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
2907                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2908                    ## As if <head>
2909                    !!!create-element ($self->{head_element}, 'head');
2910                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2911                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2912    
2913                    $self->{insertion_mode} = IN_HEAD_IM;
2914                    ## Reprocess in the "in head" insertion mode...
2915                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2916                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2917                    ## Ignore the token
2918                    !!!next-token;
2919                    redo B;
2920                  }
2921                  
2922                #                #
2923              } elsif ($self->{insertion_mode} eq 'in head noscript' and              } elsif ({
                      {  
2924                        p => 1, br => 1,                        p => 1, br => 1,
2925                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
2926                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2927                    ## As if <head>
2928                    !!!create-element ($self->{head_element}, 'head');
2929                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2930                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2931    
2932                    $self->{insertion_mode} = IN_HEAD_IM;
2933                    ## Reprocess in the "in head" insertion mode...
2934                  }
2935    
2936                #                #
2937              } elsif ($self->{insertion_mode} ne 'after head') {              } else {
2938                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2939                    #
2940                  } else {
2941                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2942                    ## Ignore the token
2943                    !!!next-token;
2944                    redo B;
2945                  }
2946                }
2947    
2948                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2949                  ## As if </noscript>
2950                  pop @{$self->{open_elements}};
2951                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
2952                  
2953                  ## Reprocess in the "in head" insertion mode...
2954                  ## As if </head>
2955                  pop @{$self->{open_elements}};
2956    
2957                  ## Reprocess in the "after head" insertion mode...
2958                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2959                  ## As if </head>
2960                  pop @{$self->{open_elements}};
2961    
2962                  ## Reprocess in the "after head" insertion mode...
2963                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2964                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2965                ## Ignore the token                ## Ignore the token ## ISSUE: An issue in the spec.
2966                !!!next-token;                !!!next-token;
2967                redo B;                redo B;
             } else {  
               #  
2968              }              }
           } else {  
             #  
           }  
2969    
2970            ## As if </head> or </noscript> or <body>              ## "after head" insertion mode
2971            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'  
2972              !!!insert-element ('body');              !!!insert-element ('body');
2973              $self->{insertion_mode} = 'in body';              $self->{insertion_mode} = IN_BODY_IM;
2974                ## reprocess
2975                redo B;
2976              } else {
2977                die "$0: $token->{type}: Unknown token type";
2978            }            }
           ## reprocess  
           redo B;  
2979    
2980            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
2981          } elsif ($self->{insertion_mode} eq 'in body' or      } elsif ($self->{insertion_mode} == IN_BODY_IM or
2982                   $self->{insertion_mode} eq 'in cell' or               $self->{insertion_mode} == IN_CELL_IM or
2983                   $self->{insertion_mode} eq 'in caption') {               $self->{insertion_mode} == IN_CAPTION_IM) {
2984            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
2985              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
2986              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
# Line 3662  sub _tree_construction_main ($) { Line 2994  sub _tree_construction_main ($) {
2994                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
2995                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
2996                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
2997                if ($self->{insertion_mode} eq 'in cell') {                if ($self->{insertion_mode} == IN_CELL_IM) {
2998                  ## have an element in table scope                  ## have an element in table scope
2999                  my $tn;                  my $tn;
3000                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3687  sub _tree_construction_main ($) { Line 3019  sub _tree_construction_main ($) {
3019                  !!!back-token; # <?>                  !!!back-token; # <?>
3020                  $token = {type => 'end tag', tag_name => $tn};                  $token = {type => 'end tag', tag_name => $tn};
3021                  redo B;                  redo B;
3022                } elsif ($self->{insertion_mode} eq 'in caption') {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3023                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption');
3024                                    
3025                  ## As if </caption>                  ## As if </caption>
# Line 3733  sub _tree_construction_main ($) { Line 3065  sub _tree_construction_main ($) {
3065                                    
3066                  $clear_up_to_marker->();                  $clear_up_to_marker->();
3067                                    
3068                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3069                                    
3070                  ## reprocess                  ## reprocess
3071                  redo B;                  redo B;
# Line 3745  sub _tree_construction_main ($) { Line 3077  sub _tree_construction_main ($) {
3077              }              }
3078            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
3079              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3080                if ($self->{insertion_mode} eq 'in cell') {                if ($self->{insertion_mode} == IN_CELL_IM) {
3081                  ## have an element in table scope                  ## have an element in table scope
3082                  my $i;                  my $i;
3083                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3788  sub _tree_construction_main ($) { Line 3120  sub _tree_construction_main ($) {
3120                                    
3121                  $clear_up_to_marker->();                  $clear_up_to_marker->();
3122                                    
3123                  $self->{insertion_mode} = 'in row';                  $self->{insertion_mode} = IN_ROW_IM;
3124                                    
3125                  !!!next-token;                  !!!next-token;
3126                  redo B;                  redo B;
3127                } elsif ($self->{insertion_mode} eq 'in caption') {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3128                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3129                  ## Ignore the token                  ## Ignore the token
3130                  !!!next-token;                  !!!next-token;
# Line 3801  sub _tree_construction_main ($) { Line 3133  sub _tree_construction_main ($) {
3133                  #                  #
3134                }                }
3135              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
3136                if ($self->{insertion_mode} eq 'in caption') {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
3137                  ## have a table element in table scope                  ## have a table element in table scope
3138                  my $i;                  my $i;
3139                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3842  sub _tree_construction_main ($) { Line 3174  sub _tree_construction_main ($) {
3174                                    
3175                  $clear_up_to_marker->();                  $clear_up_to_marker->();
3176                                    
3177                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3178                                    
3179                  !!!next-token;                  !!!next-token;
3180                  redo B;                  redo B;
3181                } elsif ($self->{insertion_mode} eq 'in cell') {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
3182                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3183                  ## Ignore the token                  ## Ignore the token
3184                  !!!next-token;                  !!!next-token;
# Line 3858  sub _tree_construction_main ($) { Line 3190  sub _tree_construction_main ($) {
3190                        table => 1, tbody => 1, tfoot => 1,                        table => 1, tbody => 1, tfoot => 1,
3191                        thead => 1, tr => 1,                        thead => 1, tr => 1,
3192                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
3193                       $self->{insertion_mode} eq 'in cell') {                       $self->{insertion_mode} == IN_CELL_IM) {
3194                ## have an element in table scope                ## have an element in table scope
3195                my $i;                my $i;
3196                my $tn;                my $tn;
# Line 3889  sub _tree_construction_main ($) { Line 3221  sub _tree_construction_main ($) {
3221                $token = {type => 'end tag', tag_name => $tn};                $token = {type => 'end tag', tag_name => $tn};
3222                redo B;                redo B;
3223              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
3224                       $self->{insertion_mode} eq 'in caption') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
3225                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption');
3226    
3227                ## As if </caption>                ## As if </caption>
# Line 3935  sub _tree_construction_main ($) { Line 3267  sub _tree_construction_main ($) {
3267    
3268                $clear_up_to_marker->();                $clear_up_to_marker->();
3269    
3270                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
3271    
3272                ## reprocess                ## reprocess
3273                redo B;                redo B;
3274              } elsif ({              } elsif ({
3275                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
3276                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3277                if ($self->{insertion_mode} eq 'in cell' or                if ($self->{insertion_mode} == IN_CELL_IM or
3278                    $self->{insertion_mode} eq 'in caption') {                    $self->{insertion_mode} == IN_CAPTION_IM) {
3279                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3280                  ## Ignore the token                  ## Ignore the token
3281                  !!!next-token;                  !!!next-token;
# Line 3955  sub _tree_construction_main ($) { Line 3287  sub _tree_construction_main ($) {
3287                        tbody => 1, tfoot => 1,                        tbody => 1, tfoot => 1,
3288                        thead => 1, tr => 1,                        thead => 1, tr => 1,
3289                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
3290                       $self->{insertion_mode} eq 'in caption') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
3291                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3292                ## Ignore the token                ## Ignore the token
3293                !!!next-token;                !!!next-token;
# Line 3963  sub _tree_construction_main ($) { Line 3295  sub _tree_construction_main ($) {
3295              } else {              } else {
3296                #                #
3297              }              }
3298            } else {        } else {
3299              #          die "$0: $token->{type}: Unknown token type";
3300            }        }
3301              
3302            $in_body->($insert_to_current);        $insert = $insert_to_current;
3303            redo B;        #
3304          } elsif ($self->{insertion_mode} eq 'in table') {      } elsif ($self->{insertion_mode} == IN_ROW_IM or
3305                 $self->{insertion_mode} == IN_TABLE_BODY_IM or
3306                 $self->{insertion_mode} == IN_TABLE_IM) {
3307            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
3308              ## NOTE: There are "character in table" code clones.              ## NOTE: There are "character in table" code clones.
3309              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
# Line 4030  sub _tree_construction_main ($) { Line 3364  sub _tree_construction_main ($) {
3364              redo B;              redo B;
3365            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
3366              if ({              if ({
3367                   caption => 1,                   tr => ($self->{insertion_mode} != IN_ROW_IM),
3368                   colgroup => 1,                   th => 1, td => 1,
                  tbody => 1, tfoot => 1, thead => 1,  
3369                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3370                ## Clear back to table context                if ($self->{insertion_mode} == IN_TABLE_IM) {
3371                while ($self->{open_elements}->[-1]->[1] ne 'table' and                  ## Clear back to table context
3372                       $self->{open_elements}->[-1]->[1] ne 'html') {                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
3373                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                         $self->{open_elements}->[-1]->[1] ne 'html') {
3374                  pop @{$self->{open_elements}};                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3375                      pop @{$self->{open_elements}};
3376                    }
3377                    
3378                    !!!insert-element ('tbody');
3379                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3380                    ## reprocess in the "in table body" insertion mode...
3381                }                }
3382    
3383                push @$active_formatting_elements, ['#marker', '']                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3384                  if $token->{tag_name} eq 'caption';                  unless ($token->{tag_name} eq 'tr') {
3385                      !!!parse-error (type => 'missing start tag:tr');
3386                    }
3387                    
3388                    ## Clear back to table body context
3389                    while (not {
3390                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3391                    }->{$self->{open_elements}->[-1]->[1]}) {
3392                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3393                      pop @{$self->{open_elements}};
3394                    }
3395                    
3396                    $self->{insertion_mode} = IN_ROW_IM;
3397                    if ($token->{tag_name} eq 'tr') {
3398                      !!!insert-element ($token->{tag_name}, $token->{attributes});
3399                      !!!next-token;
3400                      redo B;
3401                    } else {
3402                      !!!insert-element ('tr');
3403                      ## reprocess in the "in row" insertion mode
3404                    }
3405                  }
3406    
3407                  ## Clear back to table row context
3408                  while (not {
3409                    tr => 1, html => 1,
3410                  }->{$self->{open_elements}->[-1]->[1]}) {
3411                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3412                    pop @{$self->{open_elements}};
3413                  }
3414                  
3415                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3416                $self->{insertion_mode} = {                $self->{insertion_mode} = IN_CELL_IM;
3417                                   caption => 'in caption',  
3418                                   colgroup => 'in column group',                push @$active_formatting_elements, ['#marker', ''];
3419                                   tbody => 'in table body',                
                                  tfoot => 'in table body',  
                                  thead => 'in table body',  
                                 }->{$token->{tag_name}};  
3420                !!!next-token;                !!!next-token;
3421                redo B;                redo B;
3422              } elsif ({              } elsif ({
3423                        col => 1,                        caption => 1, col => 1, colgroup => 1,
3424                        td => 1, th => 1, tr => 1,                        tbody => 1, tfoot => 1, thead => 1,
3425                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3426                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3427                ## Clear back to table context                if ($self->{insertion_mode} == IN_ROW_IM) {
3428                while ($self->{open_elements}->[-1]->[1] ne 'table' and                  ## As if </tr>
3429                       $self->{open_elements}->[-1]->[1] ne 'html') {                  ## have an element in table scope
3430                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  my $i;
3431                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3432                      my $node = $self->{open_elements}->[$_];
3433                      if ($node->[1] eq 'tr') {
3434                        $i = $_;
3435                        last INSCOPE;
3436                      } elsif ({
3437                                table => 1, html => 1,
3438                               }->{$node->[1]}) {
3439                        last INSCOPE;
3440                      }
3441                    } # INSCOPE
3442                    unless (defined $i) {
3443                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
3444                      ## Ignore the token
3445                      !!!next-token;
3446                      redo B;
3447                    }
3448                    
3449                    ## Clear back to table row context
3450                    while (not {
3451                      tr => 1, html => 1,
3452                    }->{$self->{open_elements}->[-1]->[1]}) {
3453                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3454                      pop @{$self->{open_elements}};
3455                    }
3456                    
3457                    pop @{$self->{open_elements}}; # tr
3458                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3459                    if ($token->{tag_name} eq 'tr') {
3460                      ## reprocess
3461                      redo B;
3462                    } else {
3463                      ## reprocess in the "in table body" insertion mode...
3464                    }
3465                  }
3466    
3467                  if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3468                    ## have an element in table scope
3469                    my $i;
3470                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3471                      my $node = $self->{open_elements}->[$_];
3472                      if ({
3473                           tbody => 1, thead => 1, tfoot => 1,
3474                          }->{$node->[1]}) {
3475                        $i = $_;
3476                        last INSCOPE;
3477                      } elsif ({
3478                                table => 1, html => 1,
3479                               }->{$node->[1]}) {
3480                        last INSCOPE;
3481                      }
3482                    } # INSCOPE
3483                    unless (defined $i) {
3484                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3485                      ## Ignore the token
3486                      !!!next-token;
3487                      redo B;
3488                    }
3489    
3490                    ## Clear back to table body context
3491                    while (not {
3492                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3493                    }->{$self->{open_elements}->[-1]->[1]}) {
3494                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3495                      pop @{$self->{open_elements}};
3496                    }
3497                    
3498                    ## As if <{current node}>
3499                    ## have an element in table scope
3500                    ## true by definition
3501                    
3502                    ## Clear back to table body context
3503                    ## nop by definition
3504                    
3505                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3506                    $self->{insertion_mode} = IN_TABLE_IM;
3507                    ## reprocess in "in table" insertion mode...
3508                }                }
3509    
3510                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                if ($token->{tag_name} eq 'col') {
3511                $self->{insertion_mode} = $token->{tag_name} eq 'col'                  ## Clear back to table context
3512                  ? 'in column group' : 'in table body';                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
3513                ## reprocess                         $self->{open_elements}->[-1]->[1] ne 'html') {
3514                redo B;                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3515                      pop @{$self->{open_elements}};
3516                    }
3517                    
3518                    !!!insert-element ('colgroup');
3519                    $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
3520                    ## reprocess
3521                    redo B;
3522                  } elsif ({
3523                            caption => 1,
3524                            colgroup => 1,
3525                            tbody => 1, tfoot => 1, thead => 1,
3526                           }->{$token->{tag_name}}) {
3527                    ## Clear back to table context
3528                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3529                           $self->{open_elements}->[-1]->[1] ne 'html') {
3530                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3531                      pop @{$self->{open_elements}};
3532                    }
3533                    
3534                    push @$active_formatting_elements, ['#marker', '']
3535                        if $token->{tag_name} eq 'caption';
3536                    
3537                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3538                    $self->{insertion_mode} = {
3539                                               caption => IN_CAPTION_IM,
3540                                               colgroup => IN_COLUMN_GROUP_IM,
3541                                               tbody => IN_TABLE_BODY_IM,
3542                                               tfoot => IN_TABLE_BODY_IM,
3543                                               thead => IN_TABLE_BODY_IM,
3544                                              }->{$token->{tag_name}};
3545                    !!!next-token;
3546                    redo B;
3547                  } else {
3548                    die "$0: in table: <>: $token->{tag_name}";
3549                  }
3550              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3551                ## NOTE: There are code clones for this "table in table"                ## NOTE: There are code clones for this "table in table"
3552                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
# Line 4123  sub _tree_construction_main ($) { Line 3600  sub _tree_construction_main ($) {
3600                #                #
3601              }              }
3602            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
3603              if ($token->{tag_name} eq 'table') {              if ($token->{tag_name} eq 'tr' and
3604                ## have a table element in table scope                  $self->{insertion_mode} == IN_ROW_IM) {
3605                  ## have an element in table scope
3606                my $i;                my $i;
3607                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3608                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
# Line 4143  sub _tree_construction_main ($) { Line 3621  sub _tree_construction_main ($) {
3621                  !!!next-token;                  !!!next-token;
3622                  redo B;                  redo B;
3623                }                }
                 
               ## 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;  
               }  
3624    
3625                if ($self->{open_elements}->[-1]->[1] ne 'table') {                ## Clear back to table row context
3626                  while (not {
3627                    tr => 1, html => 1,
3628                  }->{$self->{open_elements}->[-1]->[1]}) {
3629                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3630                    pop @{$self->{open_elements}};
3631                }                }
3632    
3633                splice @{$self->{open_elements}}, $i;                pop @{$self->{open_elements}}; # tr
3634                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
               $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  
3635                !!!next-token;                !!!next-token;
3636                redo B;                redo B;
3637              } else {              } elsif ($token->{tag_name} eq 'table') {
3638                #                if ($self->{insertion_mode} == IN_ROW_IM) {
3639              }                  ## As if </tr>
3640            } else {                  ## have an element in table scope
3641              #                  my $i;
3642            }                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3643                      my $node = $self->{open_elements}->[$_];
3644            ## As if </colgroup>                    if ($node->[1] eq 'tr') {
3645            if ($self->{open_elements}->[-1]->[1] eq 'html') {                      $i = $_;
3646              !!!parse-error (type => 'unmatched end tag:colgroup');                      last INSCOPE;
3647              ## Ignore the token                    } elsif ({
3648              !!!next-token;                              table => 1, html => 1,
3649              redo B;                             }->{$node->[1]}) {
3650            } 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;  
3651                    }                    }
3652                    last OE;                  } # INSCOPE
3653                    unless (defined $i) {
3654                      !!!parse-error (type => 'unmatched end tag:'.$token->{type});
3655                      ## Ignore the token
3656                      !!!next-token;
3657                      redo B;
3658                  }                  }
3659                } # OE                  
3660                $foster_parent_element = $self->{open_elements}->[0]->[0] and                  ## Clear back to table row context
3661                $prev_sibling = $foster_parent_element->last_child                  while (not {
3662                  unless defined $foster_parent_element;                    tr => 1, html => 1,
3663                if (defined $prev_sibling and                  }->{$self->{open_elements}->[-1]->[1]}) {
3664                    $prev_sibling->node_type == 3) {                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3665                  $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;  
3666                  }                  }
3667                } # INSCOPE                  
3668                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
3669                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3670                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
3671                }                }
3672    
3673                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3674                while (not {                  ## have an element in table scope
3675                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
3676                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3677                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
3678                      if ({
3679                           tbody => 1, thead => 1, tfoot => 1,
3680                          }->{$node->[1]}) {
3681                        $i = $_;
3682                        last INSCOPE;
3683                      } elsif ({
3684                                table => 1, html => 1,
3685                               }->{$node->[1]}) {
3686                        last INSCOPE;
3687                      }
3688                    } # INSCOPE
3689                    unless (defined $i) {
3690                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3691                      ## Ignore the token
3692                      !!!next-token;
3693                      redo B;
3694                    }
3695                    
3696                    ## Clear back to table body context
3697                    while (not {
3698                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3699                    }->{$self->{open_elements}->[-1]->[1]}) {
3700                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3701                      pop @{$self->{open_elements}};
3702                    }
3703                    
3704                    ## As if <{current node}>
3705                    ## have an element in table scope
3706                    ## true by definition
3707                    
3708                    ## Clear back to table body context
3709                    ## nop by definition
3710                    
3711                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3712                    $self->{insertion_mode} = IN_TABLE_IM;
3713                    ## reprocess in the "in table" insertion mode...
3714                }                }
3715    
               ## 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>  
3716                ## have a table element in table scope                ## have a table element in table scope
3717                my $i;                my $i;
3718                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3719                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3720                  if ($node->[1] eq 'table') {                  if ($node->[1] eq $token->{tag_name}) {
3721                    $i = $_;                    $i = $_;
3722                    last INSCOPE;                    last INSCOPE;
3723                  } elsif ({                  } elsif ({
# Line 4391  sub _tree_construction_main ($) { Line 3727  sub _tree_construction_main ($) {
3727                  }                  }
3728                } # INSCOPE                } # INSCOPE
3729                unless (defined $i) {                unless (defined $i) {
3730                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3731                  ## Ignore tokens </table><table>                  ## Ignore the token
3732                  !!!next-token;                  !!!next-token;
3733                  redo B;                  redo B;
3734                }                }
3735                  
3736                ## generate implied end tags                ## generate implied end tags
3737                if ({                if ({
3738                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3739                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3740                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
3741                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
3742                  !!!back-token;                  !!!back-token;
3743                  $token = {type => 'end tag',                  $token = {type => 'end tag',
3744                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3745                  redo B;                  redo B;
3746                }                }
3747                  
3748                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
3749                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3750                }                }
3751                    
3752                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
3753                  
3754                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
3755                  
3756                ## reprocess                !!!next-token;
3757                redo B;                redo B;
3758              } else {              } elsif ({
3759                #                        tbody => 1, tfoot => 1, thead => 1,
3760              }                       }->{$token->{tag_name}} and
3761            } elsif ($token->{type} eq 'end tag') {                       ($self->{insertion_mode} == IN_ROW_IM or
3762              if ({                        $self->{insertion_mode} == IN_TABLE_BODY_IM)) {
3763                   tbody => 1, tfoot => 1, thead => 1,                if ($self->{insertion_mode} == IN_ROW_IM) {
3764                  }->{$token->{tag_name}}) {                  ## have an element in table scope
3765                ## have an element in table scope                  my $i;
3766                my $i;                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3767                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    my $node = $self->{open_elements}->[$_];
3768                  my $node = $self->{open_elements}->[$_];                    if ($node->[1] eq $token->{tag_name}) {
3769                  if ($node->[1] eq $token->{tag_name}) {                      $i = $_;
3770                    $i = $_;                      last INSCOPE;
3771                    last INSCOPE;                    } elsif ({
3772                  } elsif ({                              table => 1, html => 1,
3773                            table => 1, html => 1,                             }->{$node->[1]}) {
3774                           }->{$node->[1]}) {                      last INSCOPE;
3775                    last INSCOPE;                    }
3776                    } # INSCOPE
3777                      unless (defined $i) {
3778                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3779                        ## Ignore the token
3780                        !!!next-token;
3781                        redo B;
3782                      }
3783                    
3784                    ## As if </tr>
3785                    ## have an element in table scope
3786                    my $i;
3787                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3788                      my $node = $self->{open_elements}->[$_];
3789                      if ($node->[1] eq 'tr') {
3790                        $i = $_;
3791                        last INSCOPE;
3792                      } elsif ({
3793                                table => 1, html => 1,
3794                               }->{$node->[1]}) {
3795                        last INSCOPE;
3796                      }
3797                    } # INSCOPE
3798                      unless (defined $i) {
3799                        !!!parse-error (type => 'unmatched end tag:tr');
3800                        ## Ignore the token
3801                        !!!next-token;
3802                        redo B;
3803                      }
3804                    
3805                    ## Clear back to table row context
3806                    while (not {
3807                      tr => 1, html => 1,
3808                    }->{$self->{open_elements}->[-1]->[1]}) {
3809                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3810                      pop @{$self->{open_elements}};
3811                  }                  }
3812                } # INSCOPE                  
3813                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
3814                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3815                  ## 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}};  
3816                }                }
3817    
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
3818                ## have an element in table scope                ## have an element in table scope
3819                my $i;                my $i;
3820                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3821                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3822                  if ({                  if ($node->[1] eq $token->{tag_name}) {
                      tbody => 1, thead => 1, tfoot => 1,  
                     }->{$node->[1]}) {  
3823                    $i = $_;                    $i = $_;
3824                    last INSCOPE;                    last INSCOPE;
3825                  } elsif ({                  } elsif ({
# Line 4491  sub _tree_construction_main ($) { Line 3843  sub _tree_construction_main ($) {
3843                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3844                }                }
3845    
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
   
3846                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3847                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
3848                ## reprocess                !!!next-token;
3849                redo B;                redo B;
3850              } elsif ({              } elsif ({
3851                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
3852                        html => 1, td => 1, th => 1, tr => 1,                        html => 1, td => 1, th => 1,
3853                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3854                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
3855                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3856                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3857                ## Ignore the token                ## Ignore the token
# Line 4514  sub _tree_construction_main ($) { Line 3861  sub _tree_construction_main ($) {
3861                #                #
3862              }              }
3863            } else {            } else {
3864              #              die "$0: $token->{type}: Unknown token type";
3865            }            }
3866              
3867            ## As if in table        !!!parse-error (type => 'in table:'.$token->{tag_name});
3868            !!!parse-error (type => 'in table:'.$token->{tag_name});  
3869            $in_body->($insert_to_foster);        $insert = $insert_to_foster;
3870            redo B;        #
3871          } elsif ($self->{insertion_mode} eq 'in row') {      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
3872            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
             ## NOTE: This is a "character in table" code clone.  
3873              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3874                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
                 
3875                unless (length $token->{data}) {                unless (length $token->{data}) {
3876                  !!!next-token;                  !!!next-token;
3877                  redo B;                  redo B;
3878                }                }
3879              }              }
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
3880                            
3881              !!!next-token;              #
             redo B;  
3882            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
3883              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}};  
               }  
                 
3884                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3885                $self->{insertion_mode} = 'in cell';                pop @{$self->{open_elements}};
   
               push @$active_formatting_elements, ['#marker', ''];  
                 
3886                !!!next-token;                !!!next-token;
3887                redo B;                redo B;
3888              } 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 {  
3889                #                #
3890              }              }
3891            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
3892              if ($token->{tag_name} eq 'tr') {              if ($token->{tag_name} eq 'colgroup') {
3893                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] eq 'html') {
3894                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});  
3895                  ## Ignore the token                  ## Ignore the token
3896                  !!!next-token;                  !!!next-token;
3897                  redo B;                  redo B;
3898                }                } else {
3899                    pop @{$self->{open_elements}}; # colgroup
3900                ## As if </tr>                  $self->{insertion_mode} = IN_TABLE_IM;
               ## 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  
3901                  !!!next-token;                  !!!next-token;
3902                  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}};  
3903                }                }
3904                } elsif ($token->{tag_name} eq 'col') {
3905                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});  
3906                ## Ignore the token                ## Ignore the token
3907                !!!next-token;                !!!next-token;
3908                redo B;                redo B;
3909              } else {              } else {
3910                #                #
3911              }              }
3912            } else {            } else {
3913              #              #
3914            }            }
3915    
3916            ## As if in table            ## As if </colgroup>
3917            !!!parse-error (type => 'in table:'.$token->{tag_name});            if ($self->{open_elements}->[-1]->[1] eq 'html') {
3918            $in_body->($insert_to_foster);              !!!parse-error (type => 'unmatched end tag:colgroup');
3919            redo B;              ## Ignore the token
3920          } elsif ($self->{insertion_mode} eq 'in select') {              !!!next-token;
3921                redo B;
3922              } else {
3923                pop @{$self->{open_elements}}; # colgroup
3924                $self->{insertion_mode} = IN_TABLE_IM;
3925                ## reprocess
3926                redo B;
3927              }
3928        } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
3929            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
3930              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3931              !!!next-token;              !!!next-token;
# Line 5004  sub _tree_construction_main ($) { Line 4099  sub _tree_construction_main ($) {
4099            ## Ignore the token            ## Ignore the token
4100            !!!next-token;            !!!next-token;
4101            redo B;            redo B;
4102          } elsif ($self->{insertion_mode} eq 'after body') {      } elsif ($self->{insertion_mode} == AFTER_BODY_IM or
4103            if ($token->{type} eq 'character') {               $self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4104              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        if ($token->{type} eq 'character') {
4105                my $data = $1;          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4106                ## As if in body            my $data = $1;
4107                $reconstruct_active_formatting_elements->($insert_to_current);            ## As if in body
4108              $reconstruct_active_formatting_elements->($insert_to_current);
4109                                
4110                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4111              
4112              unless (length $token->{data}) {
4113                !!!next-token;
4114                redo B;
4115              }
4116            }
4117            
4118            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4119              !!!parse-error (type => 'after html:#character');
4120    
4121                unless (length $token->{data}) {            ## Reprocess in the "main" phase, "after body" insertion mode...
4122                  !!!next-token;          }
4123                  redo B;          
4124                }          ## "after body" insertion mode
4125              }          !!!parse-error (type => 'after body:#character');
4126                
4127              #          $self->{insertion_mode} = IN_BODY_IM;
4128              !!!parse-error (type => 'after body:#character');          ## reprocess
4129            } elsif ($token->{type} eq 'start tag') {          redo B;
4130              !!!parse-error (type => 'after body:'.$token->{tag_name});        } elsif ($token->{type} eq 'start tag') {
4131              #          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4132            } elsif ($token->{type} eq 'end tag') {            !!!parse-error (type => 'after html:'.$token->{tag_name});
4133              if ($token->{tag_name} eq 'html') {            
4134                if (defined $self->{inner_html_node}) {            ## Reprocess in the "main" phase, "after body" insertion mode...
4135                  !!!parse-error (type => 'unmatched end tag:html');          }
4136                  ## Ignore the token  
4137                  !!!next-token;          ## "after body" insertion mode
4138                  redo B;          !!!parse-error (type => 'after body:'.$token->{tag_name});
4139                } else {  
4140                  $previous_insertion_mode = $self->{insertion_mode};          $self->{insertion_mode} = IN_BODY_IM;
4141                  $self->{insertion_mode} = 'trailing end';          ## reprocess
4142                  !!!next-token;          redo B;
4143                  redo B;        } elsif ($token->{type} eq 'end tag') {
4144                }          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4145              } else {            !!!parse-error (type => 'after html:/'.$token->{tag_name});
4146                !!!parse-error (type => 'after body:/'.$token->{tag_name});            
4147              }            $self->{insertion_mode} = AFTER_BODY_IM;
4148              ## Reprocess in the "main" phase, "after body" insertion mode...
4149            }
4150    
4151            ## "after body" insertion mode
4152            if ($token->{tag_name} eq 'html') {
4153              if (defined $self->{inner_html_node}) {
4154                !!!parse-error (type => 'unmatched end tag:html');
4155                ## Ignore the token
4156                !!!next-token;
4157                redo B;
4158            } else {            } else {
4159              die "$0: $token->{type}: Unknown token type";              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
4160                !!!next-token;
4161                redo B;
4162            }            }
4163            } else {
4164              !!!parse-error (type => 'after body:/'.$token->{tag_name});
4165    
4166            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
4167            ## reprocess            ## reprocess
4168            redo B;            redo B;
4169      } elsif ($self->{insertion_mode} eq 'in frameset') {          }
4170          } else {
4171            die "$0: $token->{type}: Unknown token type";
4172          }
4173        } elsif ($self->{insertion_mode} == IN_FRAMESET_IM or
4174                 $self->{insertion_mode} == AFTER_FRAMESET_IM or
4175                 $self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4176        if ($token->{type} eq 'character') {        if ($token->{type} eq 'character') {
4177          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4178            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4179              
4180            unless (length $token->{data}) {            unless (length $token->{data}) {
4181              !!!next-token;              !!!next-token;
4182              redo B;              redo B;
4183            }            }
4184          }          }
4185            
4186          !!!parse-error (type => 'in frameset:#character');          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
4187          ## Ignore the token            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4188          !!!next-token;              !!!parse-error (type => 'in frameset:#character');
4189          redo B;            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4190                !!!parse-error (type => 'after frameset:#character');
4191              } else { # "after html frameset"
4192                !!!parse-error (type => 'after html:#character');
4193    
4194                $self->{insertion_mode} = AFTER_FRAMESET_IM;
4195                ## Reprocess in the "main" phase, "after frameset"...
4196                !!!parse-error (type => 'after frameset:#character');
4197              }
4198              
4199              ## Ignore the token.
4200              if (length $token->{data}) {
4201                ## reprocess the rest of characters
4202              } else {
4203                !!!next-token;
4204              }
4205              redo B;
4206            }
4207            
4208            die qq[$0: Character "$token->{data}"];
4209        } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} eq 'start tag') {
4210          if ($token->{tag_name} eq 'frameset') {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4211              !!!parse-error (type => 'after html:'.$token->{tag_name});
4212    
4213              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4214              ## Process in the "main" phase, "after frameset" insertion mode...
4215            }
4216    
4217            if ($token->{tag_name} eq 'frameset' and
4218                $self->{insertion_mode} == IN_FRAMESET_IM) {
4219            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
4220            !!!next-token;            !!!next-token;
4221            redo B;            redo B;
4222          } elsif ($token->{tag_name} eq 'frame') {          } elsif ($token->{tag_name} eq 'frame' and
4223                     $self->{insertion_mode} == IN_FRAMESET_IM) {
4224            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
4225            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4226            !!!next-token;            !!!next-token;
4227            redo B;            redo B;
4228          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
4229            $in_body->($insert_to_current);            ## NOTE: As if in body.
4230              $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4231            redo B;            redo B;
4232          } else {          } else {
4233            !!!parse-error (type => 'in frameset:'.$token->{tag_name});            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4234                !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4235              } else {
4236                !!!parse-error (type => 'after frameset:'.$token->{tag_name});
4237              }
4238            ## Ignore the token            ## Ignore the token
4239            !!!next-token;            !!!next-token;
4240            redo B;            redo B;
4241          }          }
4242        } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} eq 'end tag') {
4243          if ($token->{tag_name} eq 'frameset') {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4244              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4245    
4246              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4247              ## Process in the "main" phase, "after frameset" insertion mode...
4248            }
4249    
4250            if ($token->{tag_name} eq 'frameset' and
4251                $self->{insertion_mode} == IN_FRAMESET_IM) {
4252            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] eq 'html' and
4253                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
4254              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
# Line 5095  sub _tree_construction_main ($) { Line 4261  sub _tree_construction_main ($) {
4261    
4262            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
4263                $self->{open_elements}->[-1]->[1] ne 'frameset') {                $self->{open_elements}->[-1]->[1] ne 'frameset') {
4264              $self->{insertion_mode} = 'after frameset';              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4265            }            }
4266            redo B;            redo B;
4267            } elsif ($token->{tag_name} eq 'html' and
4268                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
4269              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
4270              !!!next-token;
4271              redo B;
4272          } else {          } else {
4273            !!!parse-error (type => 'in frameset:/'.$token->{tag_name});            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4274                !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
4275              } else {
4276                !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
4277              }
4278            ## Ignore the token            ## Ignore the token
4279            !!!next-token;            !!!next-token;
4280            redo B;            redo B;
# Line 5107  sub _tree_construction_main ($) { Line 4282  sub _tree_construction_main ($) {
4282        } else {        } else {
4283          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4284        }        }
     } 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);  
4285    
4286                unless (length $token->{data}) {        ## ISSUE: An issue in spec here
4287                  !!!next-token;      } else {
4288                  redo B;        die "$0: $self->{insertion_mode}: Unknown insertion mode";
4289                }      }
             }  
4290    
4291              if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {      ## "in body" insertion mode
4292                !!!parse-error (type => 'after frameset:#character');      if ($token->{type} eq 'start tag') {
4293          if ($token->{tag_name} eq 'script') {
4294            ## NOTE: This is an "as if in head" code clone
4295            $script_start_tag->($insert);
4296            redo B;
4297          } elsif ($token->{tag_name} eq 'style') {
4298            ## NOTE: This is an "as if in head" code clone
4299            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4300            redo B;
4301          } elsif ({
4302                    base => 1, link => 1,
4303                   }->{$token->{tag_name}}) {
4304            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4305            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4306            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4307            !!!next-token;
4308            redo B;
4309          } elsif ($token->{tag_name} eq 'meta') {
4310            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4311            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4312            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4313    
4314                ## Ignore the token.          unless ($self->{confident}) {
4315                if (length $token->{data}) {            my $charset;
4316                  ## reprocess the rest of characters            if ($token->{attributes}->{charset}) { ## TODO: And if supported
4317                } else {              $charset = $token->{attributes}->{charset}->{value};
4318                  !!!next-token;            }
4319                }            if ($token->{attributes}->{'http-equiv'}) {
4320                redo B;              ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
4321              }              if ($token->{attributes}->{'http-equiv'}->{value}
4322                    =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
4323                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4324                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4325                  $charset = defined $1 ? $1 : defined $2 ? $2 : $3;
4326                } ## TODO: And if supported
4327              }
4328              ## TODO: Change the encoding
4329            }
4330    
4331          die qq[$0: Character "$token->{data}"];          !!!next-token;
4332        } elsif ($token->{type} eq 'start tag') {          redo B;
4333          if ($token->{tag_name} eq 'noframes') {        } elsif ($token->{tag_name} eq 'title') {
4334            $in_body->($insert_to_current);          !!!parse-error (type => 'in body:title');
4335            redo B;          ## NOTE: This is an "as if in head" code clone
4336          } else {          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
4337            !!!parse-error (type => 'after frameset:'.$token->{tag_name});            if (defined $self->{head_element}) {
4338                $self->{head_element}->append_child ($_[0]);
4339              } else {
4340                $insert->($_[0]);
4341              }
4342            });
4343            redo B;
4344          } elsif ($token->{tag_name} eq 'body') {
4345            !!!parse-error (type => 'in body:body');
4346                  
4347            if (@{$self->{open_elements}} == 1 or
4348                $self->{open_elements}->[1]->[1] ne 'body') {
4349            ## Ignore the token            ## Ignore the token
4350            } else {
4351              my $body_el = $self->{open_elements}->[1]->[0];
4352              for my $attr_name (keys %{$token->{attributes}}) {
4353                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
4354                  $body_el->set_attribute_ns
4355                    (undef, [undef, $attr_name],
4356                     $token->{attributes}->{$attr_name}->{value});
4357                }
4358              }
4359            }
4360            !!!next-token;
4361            redo B;
4362          } elsif ({
4363                    address => 1, blockquote => 1, center => 1, dir => 1,
4364                    div => 1, dl => 1, fieldset => 1, listing => 1,
4365                    menu => 1, ol => 1, p => 1, ul => 1,
4366                    pre => 1,
4367                   }->{$token->{tag_name}}) {
4368            ## has a p element in scope
4369            INSCOPE: for (reverse @{$self->{open_elements}}) {
4370              if ($_->[1] eq 'p') {
4371                !!!back-token;
4372                $token = {type => 'end tag', tag_name => 'p'};
4373                redo B;
4374              } elsif ({
4375                        table => 1, caption => 1, td => 1, th => 1,
4376                        button => 1, marquee => 1, object => 1, html => 1,
4377                       }->{$_->[1]}) {
4378                last INSCOPE;
4379              }
4380            } # INSCOPE
4381              
4382            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4383            if ($token->{tag_name} eq 'pre') {
4384              !!!next-token;
4385              if ($token->{type} eq 'character') {
4386                $token->{data} =~ s/^\x0A//;
4387                unless (length $token->{data}) {
4388                  !!!next-token;
4389                }
4390              }
4391            } else {
4392            !!!next-token;            !!!next-token;
           redo B;  
4393          }          }
4394        } elsif ($token->{type} eq 'end tag') {          redo B;
4395          if ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'form') {
4396            $previous_insertion_mode = $self->{insertion_mode};          if (defined $self->{form_element}) {
4397            $self->{insertion_mode} = 'trailing end';            !!!parse-error (type => 'in form:form');
4398              ## Ignore the token
4399            !!!next-token;            !!!next-token;
4400            redo B;            redo B;
4401          } else {          } else {
4402            !!!parse-error (type => 'after frameset:/'.$token->{tag_name});            ## has a p element in scope
4403            ## Ignore the token            INSCOPE: for (reverse @{$self->{open_elements}}) {
4404                if ($_->[1] eq 'p') {
4405                  !!!back-token;
4406                  $token = {type => 'end tag', tag_name => 'p'};
4407                  redo B;
4408                } elsif ({
4409                          table => 1, caption => 1, td => 1, th => 1,
4410                          button => 1, marquee => 1, object => 1, html => 1,
4411                         }->{$_->[1]}) {
4412                  last INSCOPE;
4413                }
4414              } # INSCOPE
4415                
4416              !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4417              $self->{form_element} = $self->{open_elements}->[-1]->[0];
4418            !!!next-token;            !!!next-token;
4419            redo B;            redo B;
4420          }          }
4421        } else {        } elsif ($token->{tag_name} eq 'li') {
4422          die "$0: $token->{type}: Unknown token type";          ## has a p element in scope
4423        }          INSCOPE: for (reverse @{$self->{open_elements}}) {
4424              if ($_->[1] eq 'p') {
4425                !!!back-token;
4426                $token = {type => 'end tag', tag_name => 'p'};
4427                redo B;
4428              } elsif ({
4429                        table => 1, caption => 1, td => 1, th => 1,
4430                        button => 1, marquee => 1, object => 1, html => 1,
4431                       }->{$_->[1]}) {
4432                last INSCOPE;
4433              }
4434            } # INSCOPE
4435              
4436            ## Step 1
4437            my $i = -1;
4438            my $node = $self->{open_elements}->[$i];
4439            LI: {
4440              ## Step 2
4441              if ($node->[1] eq 'li') {
4442                if ($i != -1) {
4443                  !!!parse-error (type => 'end tag missing:'.
4444                                  $self->{open_elements}->[-1]->[1]);
4445                }
4446                splice @{$self->{open_elements}}, $i;
4447                last LI;
4448              }
4449              
4450              ## Step 3
4451              if (not $formatting_category->{$node->[1]} and
4452                  #not $phrasing_category->{$node->[1]} and
4453                  ($special_category->{$node->[1]} or
4454                   $scoping_category->{$node->[1]}) and
4455                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4456                last LI;
4457              }
4458              
4459              ## Step 4
4460              $i--;
4461              $node = $self->{open_elements}->[$i];
4462              redo LI;
4463            } # LI
4464              
4465            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4466            !!!next-token;
4467            redo B;
4468          } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {
4469            ## has a p element in scope
4470            INSCOPE: for (reverse @{$self->{open_elements}}) {
4471              if ($_->[1] eq 'p') {
4472                !!!back-token;
4473                $token = {type => 'end tag', tag_name => 'p'};
4474                redo B;
4475              } elsif ({
4476                        table => 1, caption => 1, td => 1, th => 1,
4477                        button => 1, marquee => 1, object => 1, html => 1,
4478                       }->{$_->[1]}) {
4479                last INSCOPE;
4480              }
4481            } # INSCOPE
4482              
4483            ## Step 1
4484            my $i = -1;
4485            my $node = $self->{open_elements}->[$i];
4486            LI: {
4487              ## Step 2
4488              if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {
4489                if ($i != -1) {
4490                  !!!parse-error (type => 'end tag missing:'.
4491                                  $self->{open_elements}->[-1]->[1]);
4492                }
4493                splice @{$self->{open_elements}}, $i;
4494                last LI;
4495              }
4496              
4497              ## Step 3
4498              if (not $formatting_category->{$node->[1]} and
4499                  #not $phrasing_category->{$node->[1]} and
4500                  ($special_category->{$node->[1]} or
4501                   $scoping_category->{$node->[1]}) and
4502                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4503                last LI;
4504              }
4505              
4506              ## Step 4
4507              $i--;
4508              $node = $self->{open_elements}->[$i];
4509              redo LI;
4510            } # LI
4511              
4512            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4513            !!!next-token;
4514            redo B;
4515          } elsif ($token->{tag_name} eq 'plaintext') {
4516            ## has a p element in scope
4517            INSCOPE: for (reverse @{$self->{open_elements}}) {
4518              if ($_->[1] eq 'p') {
4519                !!!back-token;
4520                $token = {type => 'end tag', tag_name => 'p'};
4521                redo B;
4522              } elsif ({
4523                        table => 1, caption => 1, td => 1, th => 1,
4524                        button => 1, marquee => 1, object => 1, html => 1,
4525                       }->{$_->[1]}) {
4526                last INSCOPE;
4527              }
4528            } # INSCOPE
4529              
4530            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4531              
4532            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
4533              
4534            !!!next-token;
4535            redo B;
4536          } elsif ({
4537                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4538                   }->{$token->{tag_name}}) {
4539            ## has a p element in scope
4540            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4541              my $node = $self->{open_elements}->[$_];
4542              if ($node->[1] eq 'p') {
4543                !!!back-token;
4544                $token = {type => 'end tag', tag_name => 'p'};
4545                redo B;
4546              } elsif ({
4547                        table => 1, caption => 1, td => 1, th => 1,
4548                        button => 1, marquee => 1, object => 1, html => 1,
4549                       }->{$node->[1]}) {
4550                last INSCOPE;
4551              }
4552            } # INSCOPE
4553              
4554            ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>
4555            ## has an element in scope
4556            #my $i;
4557            #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4558            #  my $node = $self->{open_elements}->[$_];
4559            #  if ({
4560            #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4561            #      }->{$node->[1]}) {
4562            #    $i = $_;
4563            #    last INSCOPE;
4564            #  } elsif ({
4565            #            table => 1, caption => 1, td => 1, th => 1,
4566            #            button => 1, marquee => 1, object => 1, html => 1,
4567            #           }->{$node->[1]}) {
4568            #    last INSCOPE;
4569            #  }
4570            #} # INSCOPE
4571            #  
4572            #if (defined $i) {
4573            #  !!! parse-error (type => 'in hn:hn');
4574            #  splice @{$self->{open_elements}}, $i;
4575            #}
4576              
4577            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4578              
4579            !!!next-token;
4580            redo B;
4581          } elsif ($token->{tag_name} eq 'a') {
4582            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
4583              my $node = $active_formatting_elements->[$i];
4584              if ($node->[1] eq 'a') {
4585                !!!parse-error (type => 'in a:a');
4586                
4587                !!!back-token;
4588                $token = {type => 'end tag', tag_name => 'a'};
4589                $formatting_end_tag->($token->{tag_name});
4590                
4591                AFE2: for (reverse 0..$#$active_formatting_elements) {
4592                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
4593                    splice @$active_formatting_elements, $_, 1;
4594                    last AFE2;
4595                  }
4596                } # AFE2
4597                OE: for (reverse 0..$#{$self->{open_elements}}) {
4598                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
4599                    splice @{$self->{open_elements}}, $_, 1;
4600                    last OE;
4601                  }
4602                } # OE
4603                last AFE;
4604              } elsif ($node->[0] eq '#marker') {
4605                last AFE;
4606              }
4607            } # AFE
4608              
4609            $reconstruct_active_formatting_elements->($insert_to_current);
4610    
4611        ## ISSUE: An issue in spec here          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4612      } elsif ($self->{insertion_mode} eq 'trailing end') {          push @$active_formatting_elements, $self->{open_elements}->[-1];
4613        ## states in the main stage is preserved yet # MUST  
4614                  !!!next-token;
4615        if ($token->{type} eq 'character') {          redo B;
4616          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        } elsif ({
4617            my $data = $1;                  b => 1, big => 1, em => 1, font => 1, i => 1,
4618            ## As if in the main phase.                  s => 1, small => 1, strile => 1,
4619            ## NOTE: The insertion mode in the main phase                  strong => 1, tt => 1, u => 1,
4620            ## just before the phase has been changed to the trailing                 }->{$token->{tag_name}}) {
4621            ## end phase is either "after body" or "after frameset".          $reconstruct_active_formatting_elements->($insert_to_current);
4622            $reconstruct_active_formatting_elements->($insert_to_current);          
4623            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4624            push @$active_formatting_elements, $self->{open_elements}->[-1];
4625            
4626            !!!next-token;
4627            redo B;
4628          } elsif ($token->{tag_name} eq 'nobr') {
4629            $reconstruct_active_formatting_elements->($insert_to_current);
4630    
4631            ## has a |nobr| element in scope
4632            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4633              my $node = $self->{open_elements}->[$_];
4634              if ($node->[1] eq 'nobr') {
4635                !!!parse-error (type => 'not closed:nobr');
4636                !!!back-token;
4637                $token = {type => 'end tag', tag_name => 'nobr'};
4638                redo B;
4639              } elsif ({
4640                        table => 1, caption => 1, td => 1, th => 1,
4641                        button => 1, marquee => 1, object => 1, html => 1,
4642                       }->{$node->[1]}) {
4643                last INSCOPE;
4644              }
4645            } # INSCOPE
4646            
4647            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4648            push @$active_formatting_elements, $self->{open_elements}->[-1];
4649            
4650            !!!next-token;
4651            redo B;
4652          } elsif ($token->{tag_name} eq 'button') {
4653            ## has a button element in scope
4654            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4655              my $node = $self->{open_elements}->[$_];
4656              if ($node->[1] eq 'button') {
4657                !!!parse-error (type => 'in button:button');
4658                !!!back-token;
4659                $token = {type => 'end tag', tag_name => 'button'};
4660                redo B;
4661              } elsif ({
4662                        table => 1, caption => 1, td => 1, th => 1,
4663                        button => 1, marquee => 1, object => 1, html => 1,
4664                       }->{$node->[1]}) {
4665                last INSCOPE;
4666              }
4667            } # INSCOPE
4668              
4669            $reconstruct_active_formatting_elements->($insert_to_current);
4670              
4671            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4672            push @$active_formatting_elements, ['#marker', ''];
4673    
4674            !!!next-token;
4675            redo B;
4676          } elsif ($token->{tag_name} eq 'marquee' or
4677                   $token->{tag_name} eq 'object') {
4678            $reconstruct_active_formatting_elements->($insert_to_current);
4679            
4680            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4681            push @$active_formatting_elements, ['#marker', ''];
4682            
4683            !!!next-token;
4684            redo B;
4685          } elsif ($token->{tag_name} eq 'xmp') {
4686            $reconstruct_active_formatting_elements->($insert_to_current);
4687            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4688            redo B;
4689          } elsif ($token->{tag_name} eq 'table') {
4690            ## has a p element in scope
4691            INSCOPE: for (reverse @{$self->{open_elements}}) {
4692              if ($_->[1] eq 'p') {
4693                !!!back-token;
4694                $token = {type => 'end tag', tag_name => 'p'};
4695                redo B;
4696              } elsif ({
4697                        table => 1, caption => 1, td => 1, th => 1,
4698                        button => 1, marquee => 1, object => 1, html => 1,
4699                       }->{$_->[1]}) {
4700                last INSCOPE;
4701              }
4702            } # INSCOPE
4703              
4704            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4705              
4706            $self->{insertion_mode} = IN_TABLE_IM;
4707              
4708            !!!next-token;
4709            redo B;
4710          } elsif ({
4711                    area => 1, basefont => 1, bgsound => 1, br => 1,
4712                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
4713                    image => 1,
4714                   }->{$token->{tag_name}}) {
4715            if ($token->{tag_name} eq 'image') {
4716              !!!parse-error (type => 'image');
4717              $token->{tag_name} = 'img';
4718            }
4719    
4720            ## NOTE: There is an "as if <br>" code clone.
4721            $reconstruct_active_formatting_elements->($insert_to_current);
4722            
4723            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4724            pop @{$self->{open_elements}};
4725            
4726            !!!next-token;
4727            redo B;
4728          } elsif ($token->{tag_name} eq 'hr') {
4729            ## has a p element in scope
4730            INSCOPE: for (reverse @{$self->{open_elements}}) {
4731              if ($_->[1] eq 'p') {
4732                !!!back-token;
4733                $token = {type => 'end tag', tag_name => 'p'};
4734                redo B;
4735              } elsif ({
4736                        table => 1, caption => 1, td => 1, th => 1,
4737                        button => 1, marquee => 1, object => 1, html => 1,
4738                       }->{$_->[1]}) {
4739                last INSCOPE;
4740              }
4741            } # INSCOPE
4742                        
4743            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4744            pop @{$self->{open_elements}};
4745                        
4746            !!!next-token;
4747            redo B;
4748          } elsif ($token->{tag_name} eq 'input') {
4749            $reconstruct_active_formatting_elements->($insert_to_current);
4750            
4751            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4752            ## TODO: associate with $self->{form_element} if defined
4753            pop @{$self->{open_elements}};
4754            
4755            !!!next-token;
4756            redo B;
4757          } elsif ($token->{tag_name} eq 'isindex') {
4758            !!!parse-error (type => 'isindex');
4759            
4760            if (defined $self->{form_element}) {
4761              ## Ignore the token
4762              !!!next-token;
4763              redo B;
4764            } else {
4765              my $at = $token->{attributes};
4766              my $form_attrs;
4767              $form_attrs->{action} = $at->{action} if $at->{action};
4768              my $prompt_attr = $at->{prompt};
4769              $at->{name} = {name => 'name', value => 'isindex'};
4770              delete $at->{action};
4771              delete $at->{prompt};
4772              my @tokens = (
4773                            {type => 'start tag', tag_name => 'form',
4774                             attributes => $form_attrs},
4775                            {type => 'start tag', tag_name => 'hr'},
4776                            {type => 'start tag', tag_name => 'p'},
4777                            {type => 'start tag', tag_name => 'label'},
4778                           );
4779              if ($prompt_attr) {
4780                push @tokens, {type => 'character', data => $prompt_attr->{value}};
4781              } else {
4782                push @tokens, {type => 'character',
4783                               data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
4784                ## TODO: make this configurable
4785              }
4786              push @tokens,
4787                            {type => 'start tag', tag_name => 'input', attributes => $at},
4788                            #{type => 'character', data => ''}, # SHOULD
4789                            {type => 'end tag', tag_name => 'label'},
4790                            {type => 'end tag', tag_name => 'p'},
4791                            {type => 'start tag', tag_name => 'hr'},
4792                            {type => 'end tag', tag_name => 'form'};
4793              $token = shift @tokens;
4794              !!!back-token (@tokens);
4795              redo B;
4796            }
4797          } elsif ($token->{tag_name} eq 'textarea') {
4798            my $tag_name = $token->{tag_name};
4799            my $el;
4800            !!!create-element ($el, $token->{tag_name}, $token->{attributes});
4801            
4802            ## TODO: $self->{form_element} if defined
4803            $self->{content_model} = RCDATA_CONTENT_MODEL;
4804            delete $self->{escape}; # MUST
4805            
4806            $insert->($el);
4807            
4808            my $text = '';
4809            !!!next-token;
4810            if ($token->{type} eq 'character') {
4811              $token->{data} =~ s/^\x0A//;
4812            unless (length $token->{data}) {            unless (length $token->{data}) {
4813              !!!next-token;              !!!next-token;
             redo B;  
4814            }            }
4815          }          }
4816            while ($token->{type} eq 'character') {
4817              $text .= $token->{data};
4818              !!!next-token;
4819            }
4820            if (length $text) {
4821              $el->manakai_append_text ($text);
4822            }
4823            
4824            $self->{content_model} = PCDATA_CONTENT_MODEL;
4825            
4826            if ($token->{type} eq 'end tag' and
4827                $token->{tag_name} eq $tag_name) {
4828              ## Ignore the token
4829            } else {
4830              !!!parse-error (type => 'in RCDATA:#'.$token->{type});
4831            }
4832            !!!next-token;
4833            redo B;
4834          } elsif ({
4835                    iframe => 1,
4836                    noembed => 1,
4837                    noframes => 1,
4838                    noscript => 0, ## TODO: 1 if scripting is enabled
4839                   }->{$token->{tag_name}}) {
4840            ## NOTE: There are two "as if in body" code clones.
4841            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4842            redo B;
4843          } elsif ($token->{tag_name} eq 'select') {
4844            $reconstruct_active_formatting_elements->($insert_to_current);
4845            
4846            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4847            
4848            $self->{insertion_mode} = IN_SELECT_IM;
4849            !!!next-token;
4850            redo B;
4851          } elsif ({
4852                    caption => 1, col => 1, colgroup => 1, frame => 1,
4853                    frameset => 1, head => 1, option => 1, optgroup => 1,
4854                    tbody => 1, td => 1, tfoot => 1, th => 1,
4855                    thead => 1, tr => 1,
4856                   }->{$token->{tag_name}}) {
4857            !!!parse-error (type => 'in body:'.$token->{tag_name});
4858            ## Ignore the token
4859            !!!next-token;
4860            redo B;
4861            
4862            ## ISSUE: An issue on HTML5 new elements in the spec.
4863          } else {
4864            $reconstruct_active_formatting_elements->($insert_to_current);
4865            
4866            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4867            
4868            !!!next-token;
4869            redo B;
4870          }
4871        } elsif ($token->{type} eq 'end tag') {
4872          if ($token->{tag_name} eq 'body') {
4873            if (@{$self->{open_elements}} > 1 and
4874                $self->{open_elements}->[1]->[1] eq 'body') {
4875              for (@{$self->{open_elements}}) {
4876                unless ({
4877                           dd => 1, dt => 1, li => 1, p => 1, td => 1,
4878                           th => 1, tr => 1, body => 1, html => 1,
4879                         tbody => 1, tfoot => 1, thead => 1,
4880                        }->{$_->[1]}) {
4881                  !!!parse-error (type => 'not closed:'.$_->[1]);
4882                }
4883              }
4884    
4885          !!!parse-error (type => 'after html:#character');            $self->{insertion_mode} = AFTER_BODY_IM;
4886          $self->{insertion_mode} = $previous_insertion_mode;            !!!next-token;
4887          ## reprocess            redo B;
4888            } else {
4889              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4890              ## Ignore the token
4891              !!!next-token;
4892              redo B;
4893            }
4894          } elsif ($token->{tag_name} eq 'html') {
4895            if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
4896              ## ISSUE: There is an issue in the spec.
4897              if ($self->{open_elements}->[-1]->[1] ne 'body') {
4898                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
4899              }
4900              $self->{insertion_mode} = AFTER_BODY_IM;
4901              ## reprocess
4902              redo B;
4903            } else {
4904              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4905              ## Ignore the token
4906              !!!next-token;
4907              redo B;
4908            }
4909          } elsif ({
4910                    address => 1, blockquote => 1, center => 1, dir => 1,
4911                    div => 1, dl => 1, fieldset => 1, listing => 1,
4912                    menu => 1, ol => 1, pre => 1, ul => 1,
4913                    p => 1,
4914                    dd => 1, dt => 1, li => 1,
4915                    button => 1, marquee => 1, object => 1,
4916                   }->{$token->{tag_name}}) {
4917            ## has an element in scope
4918            my $i;
4919            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4920              my $node = $self->{open_elements}->[$_];
4921              if ($node->[1] eq $token->{tag_name}) {
4922                ## generate implied end tags
4923                if ({
4924                     dd => ($token->{tag_name} ne 'dd'),
4925                     dt => ($token->{tag_name} ne 'dt'),
4926                     li => ($token->{tag_name} ne 'li'),
4927                     p => ($token->{tag_name} ne 'p'),
4928                     td => 1, th => 1, tr => 1,
4929                     tbody => 1, tfoot=> 1, thead => 1,
4930                    }->{$self->{open_elements}->[-1]->[1]}) {
4931                  !!!back-token;
4932                  $token = {type => 'end tag',
4933                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4934                  redo B;
4935                }
4936                $i = $_;
4937                last INSCOPE unless $token->{tag_name} eq 'p';
4938              } elsif ({
4939                        table => 1, caption => 1, td => 1, th => 1,
4940                        button => 1, marquee => 1, object => 1, html => 1,
4941                       }->{$node->[1]}) {
4942                last INSCOPE;
4943              }
4944            } # INSCOPE
4945            
4946            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
4947              if (defined $i) {
4948                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4949              } else {
4950                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4951              }
4952            }
4953            
4954            if (defined $i) {
4955              splice @{$self->{open_elements}}, $i;
4956            } elsif ($token->{tag_name} eq 'p') {
4957              ## As if <p>, then reprocess the current token
4958              my $el;
4959              !!!create-element ($el, 'p');
4960              $insert->($el);
4961            }
4962            $clear_up_to_marker->()
4963              if {
4964                button => 1, marquee => 1, object => 1,
4965              }->{$token->{tag_name}};
4966            !!!next-token;
4967          redo B;          redo B;
4968        } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{tag_name} eq 'form') {
4969          !!!parse-error (type => 'after html:'.$token->{tag_name});          ## has an element in scope
4970          $self->{insertion_mode} = $previous_insertion_mode;          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4971          ## reprocess            my $node = $self->{open_elements}->[$_];
4972              if ($node->[1] eq $token->{tag_name}) {
4973                ## generate implied end tags
4974                if ({
4975                     dd => 1, dt => 1, li => 1, p => 1,
4976                     td => 1, th => 1, tr => 1,
4977                     tbody => 1, tfoot=> 1, thead => 1,
4978                    }->{$self->{open_elements}->[-1]->[1]}) {
4979                  !!!back-token;
4980                  $token = {type => 'end tag',
4981                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4982                  redo B;
4983                }
4984                last INSCOPE;
4985              } elsif ({
4986                        table => 1, caption => 1, td => 1, th => 1,
4987                        button => 1, marquee => 1, object => 1, html => 1,
4988                       }->{$node->[1]}) {
4989                last INSCOPE;
4990              }
4991            } # INSCOPE
4992            
4993            if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
4994              pop @{$self->{open_elements}};
4995            } else {
4996              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4997            }
4998    
4999            undef $self->{form_element};
5000            !!!next-token;
5001          redo B;          redo B;
5002        } elsif ($token->{type} eq 'end tag') {        } elsif ({
5003          !!!parse-error (type => 'after html:/'.$token->{tag_name});                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5004          $self->{insertion_mode} = $previous_insertion_mode;                 }->{$token->{tag_name}}) {
5005          ## reprocess          ## has an element in scope
5006            my $i;
5007            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5008              my $node = $self->{open_elements}->[$_];
5009              if ({
5010                   h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5011                  }->{$node->[1]}) {
5012                ## generate implied end tags
5013                if ({
5014                     dd => 1, dt => 1, li => 1, p => 1,
5015                     td => 1, th => 1, tr => 1,
5016                     tbody => 1, tfoot=> 1, thead => 1,
5017                    }->{$self->{open_elements}->[-1]->[1]}) {
5018                  !!!back-token;
5019                  $token = {type => 'end tag',
5020                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5021                  redo B;
5022                }
5023                $i = $_;
5024                last INSCOPE;
5025              } elsif ({
5026                        table => 1, caption => 1, td => 1, th => 1,
5027                        button => 1, marquee => 1, object => 1, html => 1,
5028                       }->{$node->[1]}) {
5029                last INSCOPE;
5030              }
5031            } # INSCOPE
5032            
5033            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5034              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5035            }
5036            
5037            splice @{$self->{open_elements}}, $i if defined $i;
5038            !!!next-token;
5039            redo B;
5040          } elsif ({
5041                    a => 1,
5042                    b => 1, big => 1, em => 1, font => 1, i => 1,
5043                    nobr => 1, s => 1, small => 1, strile => 1,
5044                    strong => 1, tt => 1, u => 1,
5045                   }->{$token->{tag_name}}) {
5046            $formatting_end_tag->($token->{tag_name});
5047            redo B;
5048          } elsif ($token->{tag_name} eq 'br') {
5049            !!!parse-error (type => 'unmatched end tag:br');
5050    
5051            ## As if <br>
5052            $reconstruct_active_formatting_elements->($insert_to_current);
5053            
5054            my $el;
5055            !!!create-element ($el, 'br');
5056            $insert->($el);
5057            
5058            ## Ignore the token.
5059            !!!next-token;
5060            redo B;
5061          } elsif ({
5062                    caption => 1, col => 1, colgroup => 1, frame => 1,
5063                    frameset => 1, head => 1, option => 1, optgroup => 1,
5064                    tbody => 1, td => 1, tfoot => 1, th => 1,
5065                    thead => 1, tr => 1,
5066                    area => 1, basefont => 1, bgsound => 1,
5067                    embed => 1, hr => 1, iframe => 1, image => 1,
5068                    img => 1, input => 1, isindex => 1, noembed => 1,
5069                    noframes => 1, param => 1, select => 1, spacer => 1,
5070                    table => 1, textarea => 1, wbr => 1,
5071                    noscript => 0, ## TODO: if scripting is enabled
5072                   }->{$token->{tag_name}}) {
5073            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5074            ## Ignore the token
5075            !!!next-token;
5076          redo B;          redo B;
5077            
5078            ## ISSUE: Issue on HTML5 new elements in spec
5079            
5080        } else {        } else {
5081          die "$0: $token->{type}: Unknown token";          ## Step 1
5082            my $node_i = -1;
5083            my $node = $self->{open_elements}->[$node_i];
5084    
5085            ## Step 2
5086            S2: {
5087              if ($node->[1] eq $token->{tag_name}) {
5088                ## Step 1
5089                ## generate implied end tags
5090                if ({
5091                     dd => 1, dt => 1, li => 1, p => 1,
5092                     td => 1, th => 1, tr => 1,
5093                     tbody => 1, tfoot=> 1, thead => 1,
5094                    }->{$self->{open_elements}->[-1]->[1]}) {
5095                  !!!back-token;
5096                  $token = {type => 'end tag',
5097                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5098                  redo B;
5099                }
5100            
5101                ## Step 2
5102                if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
5103                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5104                }
5105                
5106                ## Step 3
5107                splice @{$self->{open_elements}}, $node_i;
5108    
5109                !!!next-token;
5110                last S2;
5111              } else {
5112                ## Step 3
5113                if (not $formatting_category->{$node->[1]} and
5114                    #not $phrasing_category->{$node->[1]} and
5115                    ($special_category->{$node->[1]} or
5116                     $scoping_category->{$node->[1]})) {
5117                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5118                  ## Ignore the token
5119                  !!!next-token;
5120                  last S2;
5121                }
5122              }
5123              
5124              ## Step 4
5125              $node_i--;
5126              $node = $self->{open_elements}->[$node_i];
5127              
5128              ## Step 5;
5129              redo S2;
5130            } # S2
5131            redo B;
5132        }        }
     } else {  
       die "$0: $self->{insertion_mode}: Unknown insertion mode";  
5133      }      }
5134        redo B;
5135    } # B    } # B
5136    
5137      ## NOTE: The "trailing end" phase in HTML5 is split into
5138      ## two insertion modes: "after html body" and "after html frameset".
5139      ## NOTE: States in the main stage is preserved while
5140      ## the parser stays in the trailing end phase. # MUST
5141    
5142    ## Stop parsing # MUST    ## Stop parsing # MUST
5143        
5144    ## TODO: script stuffs    ## TODO: script stuffs

Legend:
Removed from v.1.43  
changed lines
  Added in v.1.54

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24