/[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.37 by wakaba, Mon Jul 16 07:03:09 2007 UTC revision 1.49 by wakaba, Sat Jul 21 10:39:45 2007 UTC
# Line 150  sub new ($) { Line 150  sub new ($) {
150    return $self;    return $self;
151  } # new  } # new
152    
153    sub CM_ENTITY () { 0b001 } # & markup in data
154    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
155    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
156    
157    sub PLAINTEXT_CONTENT_MODEL () { 0 }
158    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
159    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
160    sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
161    
162  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
163    
164  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
165    my $self = shift;    my $self = shift;
166    $self->{state} = 'data'; # MUST    $self->{state} = 'data'; # MUST
167    $self->{content_model_flag} = 'PCDATA'; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
168    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
169    undef $self->{current_attribute};    undef $self->{current_attribute};
170    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
# Line 194  sub _get_next_token ($) { Line 203  sub _get_next_token ($) {
203    A: {    A: {
204      if ($self->{state} eq 'data') {      if ($self->{state} eq 'data') {
205        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_input_character} == 0x0026) { # &
206          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA
             $self->{content_model_flag} eq 'RCDATA') {  
207            $self->{state} = 'entity data';            $self->{state} = 'entity data';
208            !!!next-input-character;            !!!next-input-character;
209            redo A;            redo A;
# Line 203  sub _get_next_token ($) { Line 211  sub _get_next_token ($) {
211            #            #
212          }          }
213        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_input_character} == 0x002D) { # -
214          if ($self->{content_model_flag} eq 'RCDATA' or          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
             $self->{content_model_flag} eq 'CDATA') {  
215            unless ($self->{escape}) {            unless ($self->{escape}) {
216              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_input_character}->[0] == 0x002D and # -
217                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_input_character}->[1] == 0x0021 and # !
# Line 216  sub _get_next_token ($) { Line 223  sub _get_next_token ($) {
223                    
224          #          #
225        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_input_character} == 0x003C) { # <
226          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
227              (($self->{content_model_flag} eq 'CDATA' or              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
               $self->{content_model_flag} eq 'RCDATA') and  
228               not $self->{escape})) {               not $self->{escape})) {
229            $self->{state} = 'tag open';            $self->{state} = 'tag open';
230            !!!next-input-character;            !!!next-input-character;
# Line 228  sub _get_next_token ($) { Line 234  sub _get_next_token ($) {
234          }          }
235        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
236          if ($self->{escape} and          if ($self->{escape} and
237              ($self->{content_model_flag} eq 'RCDATA' or              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
              $self->{content_model_flag} eq 'CDATA')) {  
238            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_input_character}->[0] == 0x002D and # -
239                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_input_character}->[1] == 0x002D) { # -
240              delete $self->{escape};              delete $self->{escape};
# Line 266  sub _get_next_token ($) { Line 271  sub _get_next_token ($) {
271    
272        redo A;        redo A;
273      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} eq 'tag open') {
274        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
           $self->{content_model_flag} eq 'CDATA') {  
275          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_input_character} == 0x002F) { # /
276            !!!next-input-character;            !!!next-input-character;
277            $self->{state} = 'close tag open';            $self->{state} = 'close tag open';
# Line 280  sub _get_next_token ($) { Line 284  sub _get_next_token ($) {
284    
285            redo A;            redo A;
286          }          }
287        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
288          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_input_character} == 0x0021) { # !
289            $self->{state} = 'markup declaration open';            $self->{state} = 'markup declaration open';
290            !!!next-input-character;            !!!next-input-character;
# Line 327  sub _get_next_token ($) { Line 331  sub _get_next_token ($) {
331            redo A;            redo A;
332          }          }
333        } else {        } else {
334          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
335        }        }
336      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} eq 'close tag open') {
337        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
           $self->{content_model_flag} eq 'CDATA') {  
338          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
339            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
340            my @next_char;            my @next_char;
# Line 429  sub _get_next_token ($) { Line 432  sub _get_next_token ($) {
432                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
433            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
434          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
435            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
436            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
437              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
438            }            }
# Line 456  sub _get_next_token ($) { Line 459  sub _get_next_token ($) {
459                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
460            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
461          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
462            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
463            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
464              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
465            }            }
# Line 504  sub _get_next_token ($) { Line 507  sub _get_next_token ($) {
507                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
508            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
509          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
510            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
511            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
512              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
513            }            }
# Line 544  sub _get_next_token ($) { Line 547  sub _get_next_token ($) {
547                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
548            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
549          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
550            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
551            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
552              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
553            }            }
# Line 568  sub _get_next_token ($) { Line 571  sub _get_next_token ($) {
571        my $before_leave = sub {        my $before_leave = sub {
572          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
573              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
574            !!!parse-error (type => 'dupulicate attribute');            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});
575            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
576          } else {          } else {
577            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
# Line 597  sub _get_next_token ($) { Line 600  sub _get_next_token ($) {
600                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
601            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
602          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
603            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
604            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
605              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
606            }            }
# Line 638  sub _get_next_token ($) { Line 641  sub _get_next_token ($) {
641                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
642            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
643          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
644            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
645            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
646              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
647            }            }
# Line 676  sub _get_next_token ($) { Line 679  sub _get_next_token ($) {
679                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
680            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
681          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
682            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
683            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
684              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
685            }            }
# Line 717  sub _get_next_token ($) { Line 720  sub _get_next_token ($) {
720                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
721            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
722          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
723            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
724            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
725              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
726            }            }
# Line 764  sub _get_next_token ($) { Line 767  sub _get_next_token ($) {
767                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
768            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
769          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
770            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
771            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
772              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
773            }            }
# Line 784  sub _get_next_token ($) { Line 787  sub _get_next_token ($) {
787                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
788            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
789          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
790            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
791            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
792              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
793            }            }
# Line 820  sub _get_next_token ($) { Line 823  sub _get_next_token ($) {
823                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
824            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
825          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
826            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
827            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
828              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
829            }            }
# Line 856  sub _get_next_token ($) { Line 859  sub _get_next_token ($) {
859                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
860            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
861          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
862            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
863            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
864              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
865            }            }
# Line 895  sub _get_next_token ($) { Line 898  sub _get_next_token ($) {
898                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
899            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
900          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
901            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
902            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
903              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
904            }            }
# Line 915  sub _get_next_token ($) { Line 918  sub _get_next_token ($) {
918                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
919            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
920          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
921            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
922            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
923              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
924            }            }
# Line 2064  sub _reset_insertion_mode ($) { Line 2067  sub _reset_insertion_mode ($) {
2067                        th => 'in cell',                        th => 'in cell',
2068                        tr => 'in row',                        tr => 'in row',
2069                        tbody => 'in table body',                        tbody => 'in table body',
2070                        thead => 'in table head',                        thead => 'in table body',
2071                        tfoot => 'in table foot',                        tfoot => 'in table body',
2072                        caption => 'in caption',                        caption => 'in caption',
2073                        colgroup => 'in column group',                        colgroup => 'in column group',
2074                        table => 'in table',                        table => 'in table',
# Line 2196  sub _tree_construction_main ($) { Line 2199  sub _tree_construction_main ($) {
2199      $insert->($el); # /context node/->append_child ($el)      $insert->($el); # /context node/->append_child ($el)
2200    
2201      ## Step 3      ## Step 3
2202      $self->{content_model_flag} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
2203      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2204    
2205      ## Step 4      ## Step 4
# Line 2214  sub _tree_construction_main ($) { Line 2217  sub _tree_construction_main ($) {
2217      }      }
2218    
2219      ## Step 6      ## Step 6
2220      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2221    
2222      ## Step 7      ## Step 7
2223      if ($token->{type} eq 'end tag' and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} eq 'end tag' and $token->{tag_name} eq $start_tag_name) {
2224        ## Ignore the token        ## Ignore the token
2225        } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2226          !!!parse-error (type => 'in CDATA:#'.$token->{type});
2227        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
2228          !!!parse-error (type => 'in RCDATA:#'.$token->{type});
2229      } else {      } else {
2230        !!!parse-error (type => 'in '.$content_model_flag.':#'.$token->{type});        die "$0: $content_model_flag in parse_rcdata";
2231      }      }
2232      !!!next-token;      !!!next-token;
2233    }; # $parse_rcdata    }; # $parse_rcdata
# Line 2231  sub _tree_construction_main ($) { Line 2238  sub _tree_construction_main ($) {
2238      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes});
2239      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
2240    
2241      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
2242      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2243            
2244      my $text = '';      my $text = '';
# Line 2244  sub _tree_construction_main ($) { Line 2251  sub _tree_construction_main ($) {
2251        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
2252      }      }
2253                                
2254      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2255    
2256      if ($token->{type} eq 'end tag' and      if ($token->{type} eq 'end tag' and
2257          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
# Line 2498  sub _tree_construction_main ($) { Line 2505  sub _tree_construction_main ($) {
2505          return;          return;
2506        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
2507          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
2508          $parse_rcdata->('CDATA', $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
2509          return;          return;
2510        } elsif ({        } elsif ({
2511                  base => 1, link => 1,                  base => 1, link => 1,
# Line 2535  sub _tree_construction_main ($) { Line 2542  sub _tree_construction_main ($) {
2542        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
2543          !!!parse-error (type => 'in body:title');          !!!parse-error (type => 'in body:title');
2544          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
2545          $parse_rcdata->('RCDATA', sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
2546            if (defined $self->{head_element}) {            if (defined $self->{head_element}) {
2547              $self->{head_element}->append_child ($_[0]);              $self->{head_element}->append_child ($_[0]);
2548            } else {            } else {
# Line 2731  sub _tree_construction_main ($) { Line 2738  sub _tree_construction_main ($) {
2738                        
2739          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2740                        
2741          $self->{content_model_flag} = 'PLAINTEXT';          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
2742                        
2743          !!!next-token;          !!!next-token;
2744          return;          return;
# Line 2886  sub _tree_construction_main ($) { Line 2893  sub _tree_construction_main ($) {
2893          return;          return;
2894        } elsif ($token->{tag_name} eq 'xmp') {        } elsif ($token->{tag_name} eq 'xmp') {
2895          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
2896          $parse_rcdata->('CDATA', $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
2897          return;          return;
2898        } elsif ($token->{tag_name} eq 'table') {        } elsif ($token->{tag_name} eq 'table') {
2899          ## has a p element in scope          ## has a p element in scope
# Line 3002  sub _tree_construction_main ($) { Line 3009  sub _tree_construction_main ($) {
3009          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $token->{tag_name}, $token->{attributes});
3010                    
3011          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
3012          $self->{content_model_flag} = 'RCDATA';          $self->{content_model} = RCDATA_CONTENT_MODEL;
3013          delete $self->{escape}; # MUST          delete $self->{escape}; # MUST
3014                    
3015          $insert->($el);          $insert->($el);
# Line 3023  sub _tree_construction_main ($) { Line 3030  sub _tree_construction_main ($) {
3030            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
3031          }          }
3032                    
3033          $self->{content_model_flag} = 'PCDATA';          $self->{content_model} = PCDATA_CONTENT_MODEL;
3034                    
3035          if ($token->{type} eq 'end tag' and          if ($token->{type} eq 'end tag' and
3036              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
# Line 3039  sub _tree_construction_main ($) { Line 3046  sub _tree_construction_main ($) {
3046                  noframes => 1,                  noframes => 1,
3047                  noscript => 0, ## TODO: 1 if scripting is enabled                  noscript => 0, ## TODO: 1 if scripting is enabled
3048                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
3049          $parse_rcdata->('CDATA', $insert);          ## NOTE: There are two "as if in body" code clones.
3050            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
3051          return;          return;
3052        } elsif ($token->{tag_name} eq 'select') {        } elsif ($token->{tag_name} eq 'select') {
3053          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
# Line 3425  sub _tree_construction_main ($) { Line 3433  sub _tree_construction_main ($) {
3433              $self->{insertion_mode} = 'in head';              $self->{insertion_mode} = 'in head';
3434              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
3435                !!!next-token;                !!!next-token;
             #} elsif ({  
             #          base => 1, link => 1, meta => 1,  
             #          script => 1, style => 1, title => 1,  
             #         }->{$token->{tag_name}}) {  
             #  ## reprocess  
3436              } else {              } else {
3437                ## reprocess                ## reprocess
3438              }              }
# Line 3467  sub _tree_construction_main ($) { Line 3470  sub _tree_construction_main ($) {
3470                }                }
3471              }              }
3472                            
3473              #              if ($self->{insertion_mode} eq 'in head noscript') {
3474                  ## As if </noscript>
3475                  pop @{$self->{open_elements}};
3476                  !!!parse-error (type => 'in noscript:#character');
3477                  
3478                  ## Reprocess in the "in head" insertion mode...
3479                  ## As if </head>
3480                  pop @{$self->{open_elements}};
3481    
3482                  ## Reprocess in the "after head" insertion mode...
3483                } elsif ($self->{insertion_mode} eq 'in head') {
3484                  pop @{$self->{open_elements}};
3485    
3486                  ## Reprocess in the "after head" insertion mode...
3487                }
3488    
3489                ## 'after head' insertion mode
3490                ## As if <body>
3491                !!!insert-element ('body');
3492                $self->{insertion_mode} = 'in body';
3493                ## reprocess
3494                redo B;
3495            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
3496              if ({base => ($self->{insertion_mode} eq 'in head' or              if ($token->{tag_name} eq 'base') {
3497                            $self->{insertion_mode} eq 'after head'),                if ($self->{insertion_mode} eq 'in head noscript') {
3498                   link => 1}->{$token->{tag_name}}) {                  ## As if </noscript>
3499                    pop @{$self->{open_elements}};
3500                    !!!parse-error (type => 'in noscript:base');
3501                  
3502                    $self->{insertion_mode} = 'in head';
3503                    ## Reprocess in the "in head" insertion mode...
3504                  }
3505    
3506                  ## NOTE: There is a "as if in head" code clone.
3507                  if ($self->{insertion_mode} eq 'after head') {
3508                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3509                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3510                  }
3511                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3512                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3513                  pop @{$self->{open_elements}}
3514                      if $self->{insertion_mode} eq 'after head';
3515                  !!!next-token;
3516                  redo B;
3517                } elsif ($token->{tag_name} eq 'link') {
3518                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3519                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} eq 'after head') {
3520                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
# Line 3514  sub _tree_construction_main ($) { Line 3557  sub _tree_construction_main ($) {
3557                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} eq 'after head';
3558                !!!next-token;                !!!next-token;
3559                redo B;                redo B;
3560              } elsif ($token->{tag_name} eq 'title' and              } elsif ($token->{tag_name} eq 'title') {
3561                       $self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} eq 'in head noscript') {
3562                ## NOTE: There is a "as if in head" code clone.                  ## As if </noscript>
3563                if ($self->{insertion_mode} eq 'after head') {                  pop @{$self->{open_elements}};
3564                    !!!parse-error (type => 'in noscript:title');
3565                  
3566                    $self->{insertion_mode} = 'in head';
3567                    ## Reprocess in the "in head" insertion mode...
3568                  } elsif ($self->{insertion_mode} eq 'after head') {
3569                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3570                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3571                }                }
3572    
3573                  ## NOTE: There is a "as if in head" code clone.
3574                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
3575                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
3576                $parse_rcdata->('RCDATA', sub { $parent->append_child ($_[0]) });                $parse_rcdata->(RCDATA_CONTENT_MODEL,
3577                                  sub { $parent->append_child ($_[0]) });
3578                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
3579                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} eq 'after head';
3580                redo B;                redo B;
# Line 3535  sub _tree_construction_main ($) { Line 3586  sub _tree_construction_main ($) {
3586                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3587                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3588                }                }
3589                $parse_rcdata->('CDATA', $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
3590                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
3591                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} eq 'after head';
3592                redo B;                redo B;
# Line 3549  sub _tree_construction_main ($) { Line 3600  sub _tree_construction_main ($) {
3600                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} eq 'in head noscript') {
3601                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript:noscript');
3602                  ## Ignore the token                  ## Ignore the token
3603                    !!!next-token;
3604                  redo B;                  redo B;
3605                } else {                } else {
3606                  #                  #
3607                }                }
3608              } elsif ($token->{tag_name} eq 'head' and              } elsif ($token->{tag_name} eq 'head') {
3609                       $self->{insertion_mode} ne 'after head') {                if ($self->{insertion_mode} ne 'after head') {
3610                !!!parse-error (type => 'in head:head'); # or in head noscript                  !!!parse-error (type => 'in head:head'); # or in head noscript
3611                ## Ignore the token                  ## Ignore the token
3612                !!!next-token;                  !!!next-token;
3613                redo B;                  redo B;
3614              } elsif ($self->{insertion_mode} ne 'in head noscript' and                } else {
3615                       $token->{tag_name} eq 'script') {                  #
3616                if ($self->{insertion_mode} eq 'after head') {                }
3617                } elsif ($token->{tag_name} eq 'script') {
3618                  if ($self->{insertion_mode} eq 'in head noscript') {
3619                    ## As if </noscript>
3620                    pop @{$self->{open_elements}};
3621                    !!!parse-error (type => 'in noscript:script');
3622                  
3623                    $self->{insertion_mode} = 'in head';
3624                    ## Reprocess in the "in head" insertion mode...
3625                  } elsif ($self->{insertion_mode} eq 'after head') {
3626                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3627                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3628                }                }
3629    
3630                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3631                $script_start_tag->($insert_to_current);                $script_start_tag->($insert_to_current);
3632                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
3633                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} eq 'after head';
3634                redo B;                redo B;
3635              } elsif ($self->{insertion_mode} eq 'after head' and              } elsif ($token->{tag_name} eq 'body' or
                      $token->{tag_name} eq 'body') {  
               !!!insert-element ('body', $token->{attributes});  
               $self->{insertion_mode} = 'in body';  
               !!!next-token;  
               redo B;  
             } elsif ($self->{insertion_mode} eq 'after head' and  
3636                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
3637                !!!insert-element ('frameset', $token->{attributes});                if ($self->{insertion_mode} eq 'in head noscript') {
3638                $self->{insertion_mode} = 'in frameset';                  ## As if </noscript>
3639                    pop @{$self->{open_elements}};
3640                    !!!parse-error (type => 'in noscript:'.$token->{tag_name});
3641                    
3642                    ## Reprocess in the "in head" insertion mode...
3643                    ## As if </head>
3644                    pop @{$self->{open_elements}};
3645                    
3646                    ## Reprocess in the "after head" insertion mode...
3647                  } elsif ($self->{insertion_mode} eq 'in head') {
3648                    pop @{$self->{open_elements}};
3649                    
3650                    ## Reprocess in the "after head" insertion mode...
3651                  }
3652    
3653                  ## "after head" insertion mode
3654                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3655                  $self->{insertion_mode} = 'in '.$token->{tag_name};
3656                !!!next-token;                !!!next-token;
3657                redo B;                redo B;
3658              } else {              } else {
3659                #                #
3660              }              }
3661            } elsif ($token->{type} eq 'end tag') {  
3662              if ($self->{insertion_mode} eq 'in head' and              if ($self->{insertion_mode} eq 'in head noscript') {
3663                  $token->{tag_name} eq 'head') {                ## As if </noscript>
3664                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3665                $self->{insertion_mode} = 'after head';                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3666                !!!next-token;                
3667                redo B;                ## Reprocess in the "in head" insertion mode...
3668              } elsif ($self->{insertion_mode} eq 'in head noscript' and                ## As if </head>
                 $token->{tag_name} eq 'noscript') {  
3669                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3670                $self->{insertion_mode} = 'in head';  
3671                !!!next-token;                ## Reprocess in the "after head" insertion mode...
3672                redo B;              } elsif ($self->{insertion_mode} eq 'in head') {
3673              } elsif ($self->{insertion_mode} eq 'in head' and                ## As if </head>
3674                       {                pop @{$self->{open_elements}};
3675                        body => 1, html => 1,  
3676                        p => 1, br => 1,                ## Reprocess in the "after head" insertion mode...
                      }->{$token->{tag_name}}) {  
               #  
             } elsif ($self->{insertion_mode} eq 'in head noscript' and  
                      {  
                       p => 1, br => 1,  
                      }->{$token->{tag_name}}) {  
               #  
             } elsif ($self->{insertion_mode} ne 'after head') {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
3677              }              }
           } else {  
             #  
           }  
3678    
3679            ## As if </head> or </noscript> or <body>              ## "after head" insertion mode
3680            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'  
3681              !!!insert-element ('body');              !!!insert-element ('body');
3682              $self->{insertion_mode} = 'in body';              $self->{insertion_mode} = 'in body';
3683            }              ## reprocess
           ## reprocess  
           redo B;  
   
           ## ISSUE: An issue in the spec.  
         } elsif ($self->{insertion_mode} eq 'in body') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } else {  
             $in_body->($insert_to_current);  
3684              redo B;              redo B;
3685            }            } elsif ($token->{type} eq 'end tag') {
3686          } elsif ($self->{insertion_mode} eq 'in table') {              if ($token->{tag_name} eq 'head') {
3687            if ($token->{type} eq 'character') {                if ($self->{insertion_mode} eq 'in head noscript') {
3688              ## NOTE: There are "character in table" code clones.                  ## As if </noscript>
3689              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                  pop @{$self->{open_elements}};
3690                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                  !!!parse-error (type => 'in noscript:script');
3691                    
3692                    $self->{insertion_mode} = 'in head';
3693                    ## Reprocess in the "in head" insertion mode...
3694                  }
3695                                
3696                unless (length $token->{data}) {                if ($self->{insertion_mode} eq 'in head') {
3697                    pop @{$self->{open_elements}};
3698                    $self->{insertion_mode} = 'after head';
3699                  !!!next-token;                  !!!next-token;
3700                  redo B;                  redo B;
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
3701                } else {                } else {
3702                  $foster_parent_element->insert_before                  #
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
3703                }                }
3704              } else {              } elsif ($token->{tag_name} eq 'noscript') {
3705                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                if ($self->{insertion_mode} eq 'in head noscript') {
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1,  
                  colgroup => 1,  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$token->{tag_name}}) {  
               ## Clear back to table context  
               while ($self->{open_elements}->[-1]->[1] ne 'table' and  
                      $self->{open_elements}->[-1]->[1] ne 'html') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
3706                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3707                    $self->{insertion_mode} = 'in head';
3708                    !!!next-token;
3709                    redo B;
3710                  } else {
3711                    #
3712                }                }
   
               push @$active_formatting_elements, ['#marker', '']  
                 if $token->{tag_name} eq 'caption';  
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = {  
                                  caption => 'in caption',  
                                  colgroup => 'in column group',  
                                  tbody => 'in table body',  
                                  tfoot => 'in table body',  
                                  thead => 'in table body',  
                                 }->{$token->{tag_name}};  
               !!!next-token;  
               redo B;  
3713              } elsif ({              } elsif ({
3714                        col => 1,                        body => 1, html => 1,
                       td => 1, th => 1, tr => 1,  
3715                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3716                ## Clear back to table context                if ($self->{insertion_mode} eq 'in head noscript') {
3717                while ($self->{open_elements}->[-1]->[1] ne 'table' and                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3718                       $self->{open_elements}->[-1]->[1] ne 'html') {                  ## Ignore the token
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');  
               $self->{insertion_mode} = $token->{tag_name} eq 'col'  
                 ? 'in column group' : 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: There are code clones for this "table in table"  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
   
               ## As if </table>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'table') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:table');  
                 ## Ignore tokens </table><table>  
3719                  !!!next-token;                  !!!next-token;
3720                  redo B;                  redo B;
3721                  } else { # 'in head', 'after head'
3722                    #
3723                }                }
3724                              } elsif ({
3725                ## generate implied end tags                        p => 1, br => 1,
3726                if ({                       }->{$token->{tag_name}}) {
                    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 {  
3727                #                #
3728              }              } else {
3729            } elsif ($token->{type} eq 'end tag') {                if ($self->{insertion_mode} ne 'after head') {
             if ($token->{tag_name} eq 'table') {  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
3730                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3731                  ## Ignore the token                  ## Ignore the token
3732                  !!!next-token;                  !!!next-token;
3733                  redo B;                  redo B;
3734                  } else {
3735                    #
3736                }                }
3737                              }
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
3738    
3739                splice @{$self->{open_elements}}, $i;              if ($self->{insertion_mode} eq 'in head noscript') {
3740                  ## As if </noscript>
3741                  pop @{$self->{open_elements}};
3742                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3743                  
3744                  ## Reprocess in the "in head" insertion mode...
3745                  ## As if </head>
3746                  pop @{$self->{open_elements}};
3747    
3748                $self->_reset_insertion_mode;                ## Reprocess in the "after head" insertion mode...
3749                } elsif ($self->{insertion_mode} eq 'in head') {
3750                  ## As if </head>
3751                  pop @{$self->{open_elements}};
3752    
3753                !!!next-token;                ## Reprocess in the "after head" insertion mode...
               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 {  
               #  
3754              }              }
3755    
3756                ## "after head" insertion mode
3757                ## As if <body>
3758                !!!insert-element ('body');
3759                $self->{insertion_mode} = 'in body';
3760                ## reprocess
3761                redo B;
3762            } else {            } else {
3763              #              die "$0: $token->{type}: Unknown token type";
3764            }            }
3765    
3766            !!!parse-error (type => 'in table:'.$token->{tag_name});            ## ISSUE: An issue in the spec.
3767            $in_body->($insert_to_foster);          } elsif ($self->{insertion_mode} eq 'in body' or
3768            redo B;                   $self->{insertion_mode} eq 'in cell' or
3769          } elsif ($self->{insertion_mode} eq 'in caption') {                   $self->{insertion_mode} eq 'in caption') {
3770            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
3771              ## NOTE: This is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
3772              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
3773                            
3774              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
# Line 3880  sub _tree_construction_main ($) { Line 3780  sub _tree_construction_main ($) {
3780                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
3781                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3782                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3783                !!!parse-error (type => 'not closed:caption');                if ($self->{insertion_mode} eq 'in cell') {
3784                    ## have an element in table scope
3785                ## As if </caption>                  my $tn;
3786                ## have a table element in table scope                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3787                my $i;                    my $node = $self->{open_elements}->[$_];
3788                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3789                  my $node = $self->{open_elements}->[$_];                      $tn = $node->[1];
3790                  if ($node->[1] eq 'caption') {                      last INSCOPE;
3791                    $i = $_;                    } elsif ({
3792                    last INSCOPE;                              table => 1, html => 1,
3793                  } elsif ({                             }->{$node->[1]}) {
3794                            table => 1, html => 1,                      last INSCOPE;
3795                           }->{$node->[1]}) {                    }
3796                    last INSCOPE;                  } # INSCOPE
3797                  }                    unless (defined $tn) {
3798                } # INSCOPE                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3799                unless (defined $i) {                      ## Ignore the token
3800                  !!!parse-error (type => 'unmatched end tag:caption');                      !!!next-token;
3801                  ## Ignore the token                      redo B;
3802                  !!!next-token;                    }
3803                  redo B;                  
3804                }                  ## Close the cell
                 
               ## 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]}) {  
3805                  !!!back-token; # <?>                  !!!back-token; # <?>
3806                  $token = {type => 'end tag', tag_name => 'caption'};                  $token = {type => 'end tag', tag_name => $tn};
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
3807                  redo B;                  redo B;
3808                }                } elsif ($self->{insertion_mode} eq 'in caption') {
3809                    !!!parse-error (type => 'not closed:caption');
3810                    
3811                    ## As if </caption>
3812                    ## have a table element in table scope
3813                    my $i;
3814                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3815                      my $node = $self->{open_elements}->[$_];
3816                      if ($node->[1] eq 'caption') {
3817                        $i = $_;
3818                        last INSCOPE;
3819                      } elsif ({
3820                                table => 1, html => 1,
3821                               }->{$node->[1]}) {
3822                        last INSCOPE;
3823                      }
3824                    } # INSCOPE
3825                      unless (defined $i) {
3826                        !!!parse-error (type => 'unmatched end tag:caption');
3827                        ## Ignore the token
3828                        !!!next-token;
3829                        redo B;
3830                      }
3831                    
3832                    ## generate implied end tags
3833                    if ({
3834                         dd => 1, dt => 1, li => 1, p => 1,
3835                         td => 1, th => 1, tr => 1,
3836                         tbody => 1, tfoot=> 1, thead => 1,
3837                        }->{$self->{open_elements}->[-1]->[1]}) {
3838                      !!!back-token; # <?>
3839                      $token = {type => 'end tag', tag_name => 'caption'};
3840                      !!!back-token;
3841                      $token = {type => 'end tag',
3842                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3843                      redo B;
3844                    }
3845    
3846                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3847                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3848                    }
3849                    
3850                    splice @{$self->{open_elements}}, $i;
3851                    
3852                    $clear_up_to_marker->();
3853                    
3854                    $self->{insertion_mode} = 'in table';
3855                    
3856                    ## reprocess
3857                    redo B;
3858                  } else {
3859                    #
3860                }                }
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               ## reprocess  
               redo B;  
3861              } else {              } else {
3862                #                #
3863              }              }
3864            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
3865              if ($token->{tag_name} eq 'caption') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3866                ## have a table element in table scope                if ($self->{insertion_mode} eq 'in cell') {
3867                    ## have an element in table scope
3868                    my $i;
3869                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3870                      my $node = $self->{open_elements}->[$_];
3871                      if ($node->[1] eq $token->{tag_name}) {
3872                        $i = $_;
3873                        last INSCOPE;
3874                      } elsif ({
3875                                table => 1, html => 1,
3876                               }->{$node->[1]}) {
3877                        last INSCOPE;
3878                      }
3879                    } # INSCOPE
3880                      unless (defined $i) {
3881                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3882                        ## Ignore the token
3883                        !!!next-token;
3884                        redo B;
3885                      }
3886                    
3887                    ## generate implied end tags
3888                    if ({
3889                         dd => 1, dt => 1, li => 1, p => 1,
3890                         td => ($token->{tag_name} eq 'th'),
3891                         th => ($token->{tag_name} eq 'td'),
3892                         tr => 1,
3893                         tbody => 1, tfoot=> 1, thead => 1,
3894                        }->{$self->{open_elements}->[-1]->[1]}) {
3895                      !!!back-token;
3896                      $token = {type => 'end tag',
3897                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3898                      redo B;
3899                    }
3900                    
3901                    if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3902                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3903                    }
3904                    
3905                    splice @{$self->{open_elements}}, $i;
3906                    
3907                    $clear_up_to_marker->();
3908                    
3909                    $self->{insertion_mode} = 'in row';
3910                    
3911                    !!!next-token;
3912                    redo B;
3913                  } elsif ($self->{insertion_mode} eq 'in caption') {
3914                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3915                    ## Ignore the token
3916                    !!!next-token;
3917                    redo B;
3918                  } else {
3919                    #
3920                  }
3921                } elsif ($token->{tag_name} eq 'caption') {
3922                  if ($self->{insertion_mode} eq 'in caption') {
3923                    ## have a table element in table scope
3924                    my $i;
3925                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3926                      my $node = $self->{open_elements}->[$_];
3927                      if ($node->[1] eq $token->{tag_name}) {
3928                        $i = $_;
3929                        last INSCOPE;
3930                      } elsif ({
3931                                table => 1, html => 1,
3932                               }->{$node->[1]}) {
3933                        last INSCOPE;
3934                      }
3935                    } # INSCOPE
3936                      unless (defined $i) {
3937                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3938                        ## Ignore the token
3939                        !!!next-token;
3940                        redo B;
3941                      }
3942                    
3943                    ## generate implied end tags
3944                    if ({
3945                         dd => 1, dt => 1, li => 1, p => 1,
3946                         td => 1, th => 1, tr => 1,
3947                         tbody => 1, tfoot=> 1, thead => 1,
3948                        }->{$self->{open_elements}->[-1]->[1]}) {
3949                      !!!back-token;
3950                      $token = {type => 'end tag',
3951                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3952                      redo B;
3953                    }
3954                    
3955                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3956                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3957                    }
3958                    
3959                    splice @{$self->{open_elements}}, $i;
3960                    
3961                    $clear_up_to_marker->();
3962                    
3963                    $self->{insertion_mode} = 'in table';
3964                    
3965                    !!!next-token;
3966                    redo B;
3967                  } elsif ($self->{insertion_mode} eq 'in cell') {
3968                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3969                    ## Ignore the token
3970                    !!!next-token;
3971                    redo B;
3972                  } else {
3973                    #
3974                  }
3975                } elsif ({
3976                          table => 1, tbody => 1, tfoot => 1,
3977                          thead => 1, tr => 1,
3978                         }->{$token->{tag_name}} and
3979                         $self->{insertion_mode} eq 'in cell') {
3980                  ## have an element in table scope
3981                my $i;                my $i;
3982                  my $tn;
3983                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3984                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3985                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
3986                    $i = $_;                    $i = $_;
3987                    last INSCOPE;                    last INSCOPE;
3988                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
3989                      $tn = $node->[1];
3990                      ## NOTE: There is exactly one |td| or |th| element
3991                      ## in scope in the stack of open elements by definition.
3992                  } elsif ({                  } elsif ({
3993                            table => 1, html => 1,                            table => 1, html => 1,
3994                           }->{$node->[1]}) {                           }->{$node->[1]}) {
# Line 3953  sub _tree_construction_main ($) { Line 4001  sub _tree_construction_main ($) {
4001                  !!!next-token;                  !!!next-token;
4002                  redo B;                  redo B;
4003                }                }
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'caption') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
4004    
4005                !!!next-token;                ## Close the cell
4006                  !!!back-token; # </?>
4007                  $token = {type => 'end tag', tag_name => $tn};
4008                redo B;                redo B;
4009              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table' and
4010                         $self->{insertion_mode} eq 'in caption') {
4011                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption');
4012    
4013                ## As if </caption>                ## As if </caption>
# Line 4029  sub _tree_construction_main ($) { Line 4058  sub _tree_construction_main ($) {
4058                ## reprocess                ## reprocess
4059                redo B;                redo B;
4060              } elsif ({              } elsif ({
4061                        body => 1, col => 1, colgroup => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
                       html => 1, tbody => 1, td => 1, tfoot => 1,  
                       th => 1, thead => 1, tr => 1,  
4062                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4063                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} eq 'in cell' or
4064                ## Ignore the token                    $self->{insertion_mode} eq 'in caption') {
4065                redo B;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
             } else {  
               #  
             }  
           } else {  
             #  
           }  
                 
           $in_body->($insert_to_current);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in column group') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq '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');  
4066                  ## Ignore the token                  ## Ignore the token
4067                  !!!next-token;                  !!!next-token;
4068                  redo B;                  redo B;
4069                } else {                } else {
4070                  pop @{$self->{open_elements}}; # colgroup                  #
                 $self->{insertion_mode} = 'in table';  
                 !!!next-token;  
                 redo B;              
4071                }                }
4072              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
4073                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
4074                          thead => 1, tr => 1,
4075                         }->{$token->{tag_name}} and
4076                         $self->{insertion_mode} eq 'in caption') {
4077                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4078                ## Ignore the token                ## Ignore the token
4079                !!!next-token;                !!!next-token;
4080                redo B;                redo B;
4081              } else {              } else {
4082                #                #
4083              }              }
4084            } else {            } else {
4085              #              #
4086            }            }
4087              
4088            ## As if </colgroup>            $in_body->($insert_to_current);
4089            if ($self->{open_elements}->[-1]->[1] eq 'html') {            redo B;
4090              !!!parse-error (type => 'unmatched end tag:colgroup');          } elsif ($self->{insertion_mode} eq 'in row' or
4091              ## Ignore the token                   $self->{insertion_mode} eq 'in table body' or
4092              !!!next-token;                   $self->{insertion_mode} eq 'in table') {
             redo B;  
           } else {  
             pop @{$self->{open_elements}}; # colgroup  
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table body') {  
4093            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
4094              ## NOTE: This is a "character in table" code clone.              ## NOTE: There are "character in table" code clones.
4095              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4096                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4097                                
# Line 4121  sub _tree_construction_main ($) { Line 4108  sub _tree_construction_main ($) {
4108              ## into the current node" while characters might not be              ## into the current node" while characters might not be
4109              ## result in a new Text node.              ## result in a new Text node.
4110              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
4111                
4112              if ({              if ({
4113                   table => 1, tbody => 1, tfoot => 1,                   table => 1, tbody => 1, tfoot => 1,
4114                   thead => 1, tr => 1,                   thead => 1, tr => 1,
# Line 4163  sub _tree_construction_main ($) { Line 4150  sub _tree_construction_main ($) {
4150              redo B;              redo B;
4151            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
4152              if ({              if ({
4153                   tr => 1,                   tr => ($self->{insertion_mode} ne 'in row'),
4154                   th => 1, td => 1,                   th => 1, td => 1,
4155                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4156                unless ($token->{tag_name} eq 'tr') {                if ($self->{insertion_mode} eq 'in table') {
4157                  !!!parse-error (type => 'missing start tag:tr');                  ## Clear back to table context
4158                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
4159                           $self->{open_elements}->[-1]->[1] ne 'html') {
4160                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4161                      pop @{$self->{open_elements}};
4162                    }
4163                    
4164                    !!!insert-element ('tbody');
4165                    $self->{insertion_mode} = 'in table body';
4166                    ## reprocess in the "in table body" insertion mode...
4167                }                }
4168    
4169                ## Clear back to table body context                if ($self->{insertion_mode} eq 'in table body') {
4170                    unless ($token->{tag_name} eq 'tr') {
4171                      !!!parse-error (type => 'missing start tag:tr');
4172                    }
4173                    
4174                    ## Clear back to table body context
4175                    while (not {
4176                      tbody => 1, tfoot => 1, thead => 1, html => 1,
4177                    }->{$self->{open_elements}->[-1]->[1]}) {
4178                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4179                      pop @{$self->{open_elements}};
4180                    }
4181                    
4182                    $self->{insertion_mode} = 'in row';
4183                    if ($token->{tag_name} eq 'tr') {
4184                      !!!insert-element ($token->{tag_name}, $token->{attributes});
4185                      !!!next-token;
4186                      redo B;
4187                    } else {
4188                      !!!insert-element ('tr');
4189                      ## reprocess in the "in row" insertion mode
4190                    }
4191                  }
4192    
4193                  ## Clear back to table row context
4194                while (not {                while (not {
4195                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tr => 1, html => 1,
4196                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4197                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4198                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4199                }                }
4200                                
4201                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes});
4202                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = 'in cell';
4203                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
4204                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
4205                } else {                
4206                  !!!insert-element ('tr');                !!!next-token;
                 ## reprocess  
               }  
4207                redo B;                redo B;
4208              } elsif ({              } elsif ({
4209                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
4210                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
4211                          tr => 1, # $self->{insertion_mode} eq 'in row'
4212                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4213                ## have an element in table scope                if ($self->{insertion_mode} eq 'in row') {
4214                my $i;                  ## As if </tr>
4215                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
4216                  my $node = $self->{open_elements}->[$_];                  my $i;
4217                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4218                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
4219                      }->{$node->[1]}) {                    if ($node->[1] eq 'tr') {
4220                    $i = $_;                      $i = $_;
4221                    last INSCOPE;                      last INSCOPE;
4222                  } elsif ({                    } elsif ({
4223                            table => 1, html => 1,                              table => 1, html => 1,
4224                           }->{$node->[1]}) {                             }->{$node->[1]}) {
4225                    last INSCOPE;                      last INSCOPE;
4226                      }
4227                    } # INSCOPE
4228                    unless (defined $i) {
4229                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
4230                      ## Ignore the token
4231                      !!!next-token;
4232                      redo B;
4233                    }
4234                    
4235                    ## Clear back to table row context
4236                    while (not {
4237                      tr => 1, html => 1,
4238                    }->{$self->{open_elements}->[-1]->[1]}) {
4239                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4240                      pop @{$self->{open_elements}};
4241                    }
4242                    
4243                    pop @{$self->{open_elements}}; # tr
4244                    $self->{insertion_mode} = 'in table body';
4245                    if ($token->{tag_name} eq 'tr') {
4246                      ## reprocess
4247                      redo B;
4248                    } else {
4249                      ## reprocess in the "in table body" insertion mode...
4250                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
4251                }                }
4252    
4253                ## Clear back to table body context                if ($self->{insertion_mode} eq 'in table body') {
4254                while (not {                  ## have an element in table scope
4255                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
4256                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4257                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
4258                      if ({
4259                           tbody => 1, thead => 1, tfoot => 1,
4260                          }->{$node->[1]}) {
4261                        $i = $_;
4262                        last INSCOPE;
4263                      } elsif ({
4264                                table => 1, html => 1,
4265                               }->{$node->[1]}) {
4266                        last INSCOPE;
4267                      }
4268                    } # INSCOPE
4269                    unless (defined $i) {
4270                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4271                      ## Ignore the token
4272                      !!!next-token;
4273                      redo B;
4274                    }
4275    
4276                    ## Clear back to table body context
4277                    while (not {
4278                      tbody => 1, tfoot => 1, thead => 1, html => 1,
4279                    }->{$self->{open_elements}->[-1]->[1]}) {
4280                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4281                      pop @{$self->{open_elements}};
4282                    }
4283                    
4284                    ## As if <{current node}>
4285                    ## have an element in table scope
4286                    ## true by definition
4287                    
4288                    ## Clear back to table body context
4289                    ## nop by definition
4290                    
4291                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4292                    $self->{insertion_mode} = 'in table';
4293                    ## reprocess in "in table" insertion mode...
4294                }                }
4295    
4296                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
4297                ## have an element in table scope                  ## Clear back to table context
4298                ## true by definition                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
4299                           $self->{open_elements}->[-1]->[1] ne 'html') {
4300                ## Clear back to table body context                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4301                ## nop by definition                    pop @{$self->{open_elements}};
4302                    }
4303                pop @{$self->{open_elements}};                  
4304                $self->{insertion_mode} = 'in table';                  !!!insert-element ('colgroup');
4305                ## reprocess                  $self->{insertion_mode} = 'in column group';
4306                redo B;                  ## reprocess
4307                    redo B;
4308                  } elsif ({
4309                            caption => 1,
4310                            colgroup => 1,
4311                            tbody => 1, tfoot => 1, thead => 1,
4312                           }->{$token->{tag_name}}) {
4313                    ## Clear back to table context
4314                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
4315                           $self->{open_elements}->[-1]->[1] ne 'html') {
4316                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4317                      pop @{$self->{open_elements}};
4318                    }
4319                    
4320                    push @$active_formatting_elements, ['#marker', '']
4321                        if $token->{tag_name} eq 'caption';
4322                    
4323                    !!!insert-element ($token->{tag_name}, $token->{attributes});
4324                    $self->{insertion_mode} = {
4325                                               caption => 'in caption',
4326                                               colgroup => 'in column group',
4327                                               tbody => 'in table body',
4328                                               tfoot => 'in table body',
4329                                               thead => 'in table body',
4330                                              }->{$token->{tag_name}};
4331                    !!!next-token;
4332                    redo B;
4333                  } else {
4334                    die "$0: in table: <>: $token->{tag_name}";
4335                  }
4336              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4337                ## NOTE: This is a code clone of "table in table"                ## NOTE: There are code clones for this "table in table"
4338                !!!parse-error (type => 'not closed:table');                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4339    
4340                ## As if </table>                ## As if </table>
4341                ## have a table element in table scope                ## have a table element in table scope
# Line 4277  sub _tree_construction_main ($) { Line 4378  sub _tree_construction_main ($) {
4378    
4379                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4380    
4381                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4382    
4383                ## reprocess                ## reprocess
4384                redo B;                redo B;
# Line 4285  sub _tree_construction_main ($) { Line 4386  sub _tree_construction_main ($) {
4386                #                #
4387              }              }
4388            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
4389              if ({              if ($token->{tag_name} eq 'tr' and
4390                   tbody => 1, tfoot => 1, thead => 1,                  $self->{insertion_mode} eq 'in row') {
                 }->{$token->{tag_name}}) {  
4391                ## have an element in table scope                ## have an element in table scope
4392                my $i;                my $i;
4393                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 4308  sub _tree_construction_main ($) { Line 4408  sub _tree_construction_main ($) {
4408                  redo B;                  redo B;
4409                }                }
4410    
4411                ## Clear back to table body context                ## Clear back to table row context
4412                while (not {                while (not {
4413                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tr => 1, html => 1,
4414                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4415                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4416                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4417                }                }
4418    
4419                pop @{$self->{open_elements}};                pop @{$self->{open_elements}}; # tr
4420                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = 'in table body';
4421                !!!next-token;                !!!next-token;
4422                redo B;                redo B;
4423              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4424                ## have an element in table scope                if ($self->{insertion_mode} eq 'in row') {
4425                my $i;                  ## As if </tr>
4426                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
4427                  my $node = $self->{open_elements}->[$_];                  my $i;
4428                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4429                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
4430                      }->{$node->[1]}) {                    if ($node->[1] eq 'tr') {
4431                    $i = $_;                      $i = $_;
4432                    last INSCOPE;                      last INSCOPE;
4433                  } elsif ({                    } elsif ({
4434                            table => 1, html => 1,                              table => 1, html => 1,
4435                           }->{$node->[1]}) {                             }->{$node->[1]}) {
4436                    last INSCOPE;                      last INSCOPE;
4437                      }
4438                    } # INSCOPE
4439                    unless (defined $i) {
4440                      !!!parse-error (type => 'unmatched end tag:'.$token->{type});
4441                      ## Ignore the token
4442                      !!!next-token;
4443                      redo B;
4444                  }                  }
4445                } # INSCOPE                  
4446                unless (defined $i) {                  ## Clear back to table row context
4447                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  while (not {
4448                  ## Ignore the token                    tr => 1, html => 1,
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, td => 1, th => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           ## As if in table  
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in row') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
4449                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4450                # MUST                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4451                my $foster_parent_element;                    pop @{$self->{open_elements}};
4452                my $next_sibling;                  }
4453                my $prev_sibling;                  
4454                OE: for (reverse 0..$#{$self->{open_elements}}) {                  pop @{$self->{open_elements}}; # tr
4455                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  $self->{insertion_mode} = 'in table body';
4456                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                  ## reprocess in the "in table body" insertion mode...
4457                    if (defined $parent and $parent->node_type == 1) {                }
4458                      $foster_parent_element = $parent;  
4459                      $next_sibling = $self->{open_elements}->[$_]->[0];                if ($self->{insertion_mode} eq 'in table body') {
4460                      $prev_sibling = $next_sibling->previous_sibling;                  ## have an element in table scope
4461                    } else {                  my $i;
4462                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4463                      $prev_sibling = $foster_parent_element->last_child;                    my $node = $self->{open_elements}->[$_];
4464                      if ({
4465                           tbody => 1, thead => 1, tfoot => 1,
4466                          }->{$node->[1]}) {
4467                        $i = $_;
4468                        last INSCOPE;
4469                      } elsif ({
4470                                table => 1, html => 1,
4471                               }->{$node->[1]}) {
4472                        last INSCOPE;
4473                    }                    }
4474                    last OE;                  } # INSCOPE
4475                    unless (defined $i) {
4476                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4477                      ## Ignore the token
4478                      !!!next-token;
4479                      redo B;
4480                  }                  }
4481                } # OE                  
4482                $foster_parent_element = $self->{open_elements}->[0]->[0] and                  ## Clear back to table body context
4483                $prev_sibling = $foster_parent_element->last_child                  while (not {
4484                  unless defined $foster_parent_element;                    tbody => 1, tfoot => 1, thead => 1, html => 1,
4485                if (defined $prev_sibling and                  }->{$self->{open_elements}->[-1]->[1]}) {
4486                    $prev_sibling->node_type == 3) {                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4487                  $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 ($token->{tag_name} eq 'th' or  
                 $token->{tag_name} eq 'td') {  
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = 'in cell';  
   
               push @$active_formatting_elements, ['#marker', ''];  
                 
               !!!next-token;  
               redo B;  
             } elsif ({  
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
4488                  }                  }
4489                } # INSCOPE                  
4490                unless (defined $i) {                  ## As if <{current node}>
4491                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  ## have an element in table scope
4492                  ## Ignore the token                  ## true by definition
4493                  !!!next-token;                  
4494                  redo B;                  ## Clear back to table body context
4495                }                  ## nop by definition
4496                    
               ## 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]);  
4497                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4498                    $self->{insertion_mode} = 'in table';
4499                    ## reprocess in the "in table" insertion mode...
4500                }                }
4501    
               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>  
4502                ## have a table element in table scope                ## have a table element in table scope
4503                my $i;                my $i;
4504                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4505                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4506                  if ($node->[1] eq 'table') {                  if ($node->[1] eq $token->{tag_name}) {
4507                    $i = $_;                    $i = $_;
4508                    last INSCOPE;                    last INSCOPE;
4509                  } elsif ({                  } elsif ({
# Line 4514  sub _tree_construction_main ($) { Line 4513  sub _tree_construction_main ($) {
4513                  }                  }
4514                } # INSCOPE                } # INSCOPE
4515                unless (defined $i) {                unless (defined $i) {
4516                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4517                  ## Ignore tokens </table><table>                  ## Ignore the token
4518                  !!!next-token;                  !!!next-token;
4519                  redo B;                  redo B;
4520                }                }
4521                  
4522                ## generate implied end tags                ## generate implied end tags
4523                if ({                if ({
4524                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
4525                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
4526                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
4527                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
4528                  !!!back-token;                  !!!back-token;
4529                  $token = {type => 'end tag',                  $token = {type => 'end tag',
4530                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4531                  redo B;                  redo B;
4532                }                }
4533                  
4534                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
4535                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4536                }                }
4537                    
4538                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4539                  
4540                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4541                  
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'tr') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
4542                !!!next-token;                !!!next-token;
4543                redo B;                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;  
4544              } elsif ({              } elsif ({
4545                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
4546                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
4547                ## have an element in table scope                       ($self->{insertion_mode} eq 'in row' or
4548                my $i;                        $self->{insertion_mode} eq 'in table body')) {
4549                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                if ($self->{insertion_mode} eq 'in row') {
4550                  my $node = $self->{open_elements}->[$_];                  ## have an element in table scope
4551                  if ($node->[1] eq $token->{tag_name}) {                  my $i;
4552                    $i = $_;                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4553                    last INSCOPE;                    my $node = $self->{open_elements}->[$_];
4554                  } elsif ({                    if ($node->[1] eq $token->{tag_name}) {
4555                            table => 1, html => 1,                      $i = $_;
4556                           }->{$node->[1]}) {                      last INSCOPE;
4557                    last INSCOPE;                    } elsif ({
4558                                table => 1, html => 1,
4559                               }->{$node->[1]}) {
4560                        last INSCOPE;
4561                      }
4562                    } # INSCOPE
4563                      unless (defined $i) {
4564                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4565                        ## Ignore the token
4566                        !!!next-token;
4567                        redo B;
4568                      }
4569                    
4570                    ## As if </tr>
4571                    ## have an element in table scope
4572                    my $i;
4573                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4574                      my $node = $self->{open_elements}->[$_];
4575                      if ($node->[1] eq 'tr') {
4576                        $i = $_;
4577                        last INSCOPE;
4578                      } elsif ({
4579                                table => 1, html => 1,
4580                               }->{$node->[1]}) {
4581                        last INSCOPE;
4582                      }
4583                    } # INSCOPE
4584                      unless (defined $i) {
4585                        !!!parse-error (type => 'unmatched end tag:tr');
4586                        ## Ignore the token
4587                        !!!next-token;
4588                        redo B;
4589                      }
4590                    
4591                    ## Clear back to table row context
4592                    while (not {
4593                      tr => 1, html => 1,
4594                    }->{$self->{open_elements}->[-1]->[1]}) {
4595                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4596                      pop @{$self->{open_elements}};
4597                  }                  }
4598                } # INSCOPE                  
4599                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
4600                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = 'in table body';
4601                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
4602                }                }
4603    
               ## As if </tr>  
4604                ## have an element in table scope                ## have an element in table scope
4605                my $i;                my $i;
4606                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4607                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4608                  if ($node->[1] eq 'tr') {                  if ($node->[1] eq $token->{tag_name}) {
4609                    $i = $_;                    $i = $_;
4610                    last INSCOPE;                    last INSCOPE;
4611                  } elsif ({                  } elsif ({
# Line 4653  sub _tree_construction_main ($) { Line 4615  sub _tree_construction_main ($) {
4615                  }                  }
4616                } # INSCOPE                } # INSCOPE
4617                unless (defined $i) {                unless (defined $i) {
4618                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4619                  ## Ignore the token                  ## Ignore the token
4620                  !!!next-token;                  !!!next-token;
4621                  redo B;                  redo B;
4622                }                }
4623    
4624                ## Clear back to table row context                ## Clear back to table body context
4625                while (not {                while (not {
4626                  tr => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
4627                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4628                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4629                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4630                }                }
4631    
4632                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
4633                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = 'in table';
4634                ## reprocess                !!!next-token;
4635                redo B;                redo B;
4636              } elsif ({              } elsif ({
4637                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
4638                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
4639                          tr => 1, # $self->{insertion_mode} eq 'in row'
4640                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} eq 'in table'
4641                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4642                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4643                ## Ignore the token                ## Ignore the token
# Line 4683  sub _tree_construction_main ($) { Line 4647  sub _tree_construction_main ($) {
4647                #                #
4648              }              }
4649            } else {            } else {
4650              #              die "$0: $token->{type}: Unknown token type";
4651            }            }
4652    
           ## As if in table  
4653            !!!parse-error (type => 'in table:'.$token->{tag_name});            !!!parse-error (type => 'in table:'.$token->{tag_name});
4654            $in_body->($insert_to_foster);            $in_body->($insert_to_foster);
4655            redo B;            redo B;
4656          } elsif ($self->{insertion_mode} eq 'in cell') {          } elsif ($self->{insertion_mode} eq 'in column group') {
4657            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
4658              ## NOTE: This is a code clone of "character in body".              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4659              $reconstruct_active_formatting_elements->($insert_to_current);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4660                              unless (length $token->{data}) {
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1, col => 1, colgroup => 1,  
                  tbody => 1, td => 1, tfoot => 1, th => 1,  
                  thead => 1, tr => 1,  
                 }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $tn) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
4661                  !!!next-token;                  !!!next-token;
4662                  redo B;                  redo B;
4663                }                }
4664                }
4665                ## Close the cell              
4666                !!!back-token; # <?>              #
4667                $token = {type => 'end tag', tag_name => $tn};            } elsif ($token->{type} eq 'start tag') {
4668                if ($token->{tag_name} eq 'col') {
4669                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4670                  pop @{$self->{open_elements}};
4671                  !!!next-token;
4672                redo B;                redo B;
4673              } else {              } else {
4674                #                #
4675              }              }
4676            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
4677              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
4678                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] eq 'html') {
4679                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});  
4680                  ## Ignore the token                  ## Ignore the token
4681                  !!!next-token;                  !!!next-token;
4682                  redo B;                  redo B;
4683                  } else {
4684                    pop @{$self->{open_elements}}; # colgroup
4685                    $self->{insertion_mode} = 'in table';
4686                    !!!next-token;
4687                    redo B;            
4688                }                }
4689                              } elsif ($token->{tag_name} eq 'col') {
4690                ## generate implied end tags                !!!parse-error (type => 'unmatched end tag:col');
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => ($token->{tag_name} eq 'th'),  
                    th => ($token->{tag_name} eq 'td'),  
                    tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in row';  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1,  
                       colgroup => 1, html => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
4691                ## Ignore the token                ## Ignore the token
4692                !!!next-token;                !!!next-token;
4693                redo B;                redo B;
             } elsif ({  
                       table => 1, tbody => 1, tfoot => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   ## NOTE: There is exactly one |td| or |th| element  
                   ## in scope in the stack of open elements by definition.  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
4694              } else {              } else {
4695                #                #
4696              }              }
4697            } else {            } else {
4698              #              #
4699            }            }
4700              
4701            $in_body->($insert_to_current);            ## As if </colgroup>
4702            redo B;            if ($self->{open_elements}->[-1]->[1] eq 'html') {
4703                !!!parse-error (type => 'unmatched end tag:colgroup');
4704                ## Ignore the token
4705                !!!next-token;
4706                redo B;
4707              } else {
4708                pop @{$self->{open_elements}}; # colgroup
4709                $self->{insertion_mode} = 'in table';
4710                ## reprocess
4711                redo B;
4712              }
4713          } elsif ($self->{insertion_mode} eq 'in select') {          } elsif ($self->{insertion_mode} eq 'in select') {
4714            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
4715              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
# Line 5073  sub _tree_construction_main ($) { Line 4953  sub _tree_construction_main ($) {
4953            !!!next-token;            !!!next-token;
4954            redo B;            redo B;
4955          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
4956            $in_body->($insert_to_current);            ## NOTE: As if in body.
4957              $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4958            redo B;            redo B;
4959          } else {          } else {
4960            !!!parse-error (type => 'in frameset:'.$token->{tag_name});            !!!parse-error (type => 'in frameset:'.$token->{tag_name});
# Line 5133  sub _tree_construction_main ($) { Line 5014  sub _tree_construction_main ($) {
5014          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
5015        } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} eq 'start tag') {
5016          if ($token->{tag_name} eq 'noframes') {          if ($token->{tag_name} eq 'noframes') {
5017            $in_body->($insert_to_current);            ## NOTE: As if in body.
5018              $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
5019            redo B;            redo B;
5020          } else {          } else {
5021            !!!parse-error (type => 'after frameset:'.$token->{tag_name});            !!!parse-error (type => 'after frameset:'.$token->{tag_name});
# Line 5284  sub set_inner_html ($$$) { Line 5166  sub set_inner_html ($$$) {
5166    
5167      ## Step 2      ## Step 2
5168      my $node_ln = $node->local_name;      my $node_ln = $node->local_name;
5169      $p->{content_model_flag} = {      $p->{content_model} = {
5170        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
5171        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
5172        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
5173        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
5174        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
5175        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
5176        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
5177        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
5178        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
5179        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
5180      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
5181         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
5182            unless defined $p->{content_model};
5183            ## ISSUE: What is "the name of the element"? local name?
5184    
5185      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
5186    

Legend:
Removed from v.1.37  
changed lines
  Added in v.1.49

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24