/[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.32 by wakaba, Sun Jul 1 06:18:57 2007 UTC revision 1.43 by wakaba, Sat Jul 21 07:21:44 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 705  sub _get_next_token ($) { Line 708  sub _get_next_token ($) {
708            #            #
709          } else {          } else {
710            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
711              ## TODO: Different error type for <aa / bb> than <aa/>
712          }          }
713          $self->{state} = 'before attribute name';          $self->{state} = 'before attribute name';
714          # next-input-character is already done          # next-input-character is already done
# Line 716  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 763  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 783  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 819  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 855  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 894  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 914  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 1084  sub _get_next_token ($) { Line 1088  sub _get_next_token ($) {
1088          redo A;          redo A;
1089        } else {        } else {
1090          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1091              .= chr ($self->{next_input_character});              .= '-' . chr ($self->{next_input_character});
1092          $self->{state} = 'comment';          $self->{state} = 'comment';
1093          !!!next-input-character;          !!!next-input-character;
1094          redo A;          redo A;
# Line 1643  sub _tokenize_attempt_to_consume_an_enti Line 1647  sub _tokenize_attempt_to_consume_an_enti
1647            redo X;            redo X;
1648          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
1649            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro');
1650              !!!back-next-input-character ($x_char, $self->{next_input_character});
1651            $self->{next_input_character} = 0x0023; # #            $self->{next_input_character} = 0x0023; # #
           !!!back-next-input-character ($x_char);  
1652            return undef;            return undef;
1653          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_input_character} == 0x003B) { # ;
1654            !!!next-input-character;            !!!next-input-character;
# Line 1716  sub _tokenize_attempt_to_consume_an_enti Line 1720  sub _tokenize_attempt_to_consume_an_enti
1720      !!!next-input-character;      !!!next-input-character;
1721    
1722      my $value = $entity_name;      my $value = $entity_name;
1723      my $match;      my $match = 0;
1724      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
1725      our $EntityChar;      our $EntityChar;
1726    
# Line 1736  sub _tokenize_attempt_to_consume_an_enti Line 1740  sub _tokenize_attempt_to_consume_an_enti
1740            $match = 1;            $match = 1;
1741            !!!next-input-character;            !!!next-input-character;
1742            last;            last;
1743          } elsif (not $in_attr) {          } else {
1744            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
1745            $match = -1;            $match = -1;
1746          } else {            !!!next-input-character;
           $value .= chr $self->{next_input_character};  
1747          }          }
1748        } else {        } else {
1749          $value .= chr $self->{next_input_character};          $value .= chr $self->{next_input_character};
1750            $match *= 2;
1751            !!!next-input-character;
1752        }        }
       !!!next-input-character;  
1753      }      }
1754            
1755      if ($match > 0) {      if ($match > 0) {
1756        return {type => 'character', data => $value};        return {type => 'character', data => $value};
1757      } elsif ($match < 0) {      } elsif ($match < 0) {
1758        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc');
1759        return {type => 'character', data => $value};        if ($in_attr and $match < -1) {
1760            return {type => 'character', data => '&'.$entity_name};
1761          } else {
1762            return {type => 'character', data => $value};
1763          }
1764      } else {      } else {
1765        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
1766        ## NOTE: No characters are consumed in the spec.        ## NOTE: No characters are consumed in the spec.
# Line 2017  sub _tree_construction_root_element ($) Line 2025  sub _tree_construction_root_element ($)
2025        my $root_element; !!!create-element ($root_element, 'html');        my $root_element; !!!create-element ($root_element, 'html');
2026        $self->{document}->append_child ($root_element);        $self->{document}->append_child ($root_element);
2027        push @{$self->{open_elements}}, [$root_element, 'html'];        push @{$self->{open_elements}}, [$root_element, 'html'];
       #$phase = 'main';  
2028        ## reprocess        ## reprocess
2029        #redo B;        #redo B;
2030        return;        return; ## Go to the main phase.
2031    } # B    } # B
2032  } # _tree_construction_root_element  } # _tree_construction_root_element
2033    
# Line 2096  sub _reset_insertion_mode ($) { Line 2103  sub _reset_insertion_mode ($) {
2103  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
2104    my $self = shift;    my $self = shift;
2105    
2106    my $phase = 'main';    my $previous_insertion_mode;
2107    
2108    my $active_formatting_elements = [];    my $active_formatting_elements = [];
2109    
# Line 2192  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 2210  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 2227  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 2240  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 2494  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, meta => 1,                  base => 1, link => 1,
2512                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
2513          ## NOTE: This is an "as if in head" code clone, only "-t" differs          ## NOTE: This is an "as if in head" code clone, only "-t" differs
2514          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2515          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2516          !!!next-token;          !!!next-token;
2517          ## TODO: Extracting |charset| from |meta|.          return;
2518          } elsif ($token->{tag_name} eq 'meta') {
2519            ## NOTE: This is an "as if in head" code clone, only "-t" differs
2520            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2521            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2522    
2523            unless ($self->{confident}) {
2524              my $charset;
2525              if ($token->{attributes}->{charset}) { ## TODO: And if supported
2526                $charset = $token->{attributes}->{charset}->{value};
2527              }
2528              if ($token->{attributes}->{'http-equiv'}) {
2529                ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
2530                if ($token->{attributes}->{'http-equiv'}->{value}
2531                    =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
2532                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2533                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
2534                  $charset = defined $1 ? $1 : defined $2 ? $2 : $3;
2535                } ## TODO: And if supported
2536              }
2537              ## TODO: Change the encoding
2538            }
2539    
2540            !!!next-token;
2541          return;          return;
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 2704  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 2859  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 2975  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 2996  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 3012  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);          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
3050          return;          return;
3051        } elsif ($token->{tag_name} eq 'select') {        } elsif ($token->{tag_name} eq 'select') {
3052          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
# Line 3308  sub _tree_construction_main ($) { Line 3342  sub _tree_construction_main ($) {
3342    }; # $in_body    }; # $in_body
3343    
3344    B: {    B: {
3345      if ($phase eq 'main') {      if ($token->{type} eq 'DOCTYPE') {
3346        if ($token->{type} eq 'DOCTYPE') {        !!!parse-error (type => 'DOCTYPE in the middle');
3347          !!!parse-error (type => 'in html:#DOCTYPE');        ## Ignore the token
3348          ## Ignore the token        ## Stay in the phase
3349          ## Stay in the phase        !!!next-token;
3350          !!!next-token;        redo B;
3351          redo B;      } elsif ($token->{type} eq 'end-of-file') {
3352        } elsif ($token->{type} eq 'start tag' and        if ($token->{insertion_mode} ne 'trailing end') {
                $token->{tag_name} eq 'html') {  
 ## ISSUE: "aa<html>" is not a parse error.  
 ## ISSUE: "<html>" in fragment is not a parse error.  
         unless ($token->{first_start_tag}) {  
           !!!parse-error (type => 'not first start tag');  
         }  
         my $top_el = $self->{open_elements}->[0]->[0];  
         for my $attr_name (keys %{$token->{attributes}}) {  
           unless ($top_el->has_attribute_ns (undef, $attr_name)) {  
             $top_el->set_attribute_ns  
               (undef, [undef, $attr_name],  
                $token->{attributes}->{$attr_name}->{value});  
           }  
         }  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'end-of-file') {  
3353          ## Generate implied end tags          ## Generate implied end tags
3354          if ({          if ({
3355               dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,               dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,
# Line 3352  sub _tree_construction_main ($) { Line 3369  sub _tree_construction_main ($) {
3369            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3370          }          }
3371    
         ## Stop parsing  
         last B;  
   
3372          ## ISSUE: There is an issue in the spec.          ## ISSUE: There is an issue in the spec.
3373          }
3374    
3375          ## Stop parsing
3376          last B;
3377        } elsif ($token->{type} eq 'start tag' and
3378                 $token->{tag_name} eq 'html') {
3379          if ($self->{insertion_mode} eq 'trailing end') {
3380            ## Turn into the main phase
3381            !!!parse-error (type => 'after html:html');
3382            $self->{insertion_mode} = $previous_insertion_mode;
3383          }
3384    
3385    ## ISSUE: "aa<html>" is not a parse error.
3386    ## ISSUE: "<html>" in fragment is not a parse error.
3387          unless ($token->{first_start_tag}) {
3388            !!!parse-error (type => 'not first start tag');
3389          }
3390          my $top_el = $self->{open_elements}->[0]->[0];
3391          for my $attr_name (keys %{$token->{attributes}}) {
3392            unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3393              $top_el->set_attribute_ns
3394                (undef, [undef, $attr_name],
3395                 $token->{attributes}->{$attr_name}->{value});
3396            }
3397          }
3398          !!!next-token;
3399          redo B;
3400        } elsif ($token->{type} eq 'comment') {
3401          my $comment = $self->{document}->create_comment ($token->{data});
3402          if ($self->{insertion_mode} eq 'trailing end') {
3403            $self->{document}->append_child ($comment);
3404          } elsif ($self->{insertion_mode} eq 'after body') {
3405            $self->{open_elements}->[0]->[0]->append_child ($comment);
3406        } else {        } else {
3407          if ($self->{insertion_mode} eq 'before head') {          $self->{open_elements}->[-1]->[0]->append_child ($comment);
3408          }
3409          !!!next-token;
3410          redo B;
3411        } elsif ($self->{insertion_mode} eq 'before head') {
3412            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
3413              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3414                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
# Line 3373  sub _tree_construction_main ($) { Line 3424  sub _tree_construction_main ($) {
3424              $self->{insertion_mode} = 'in head';              $self->{insertion_mode} = 'in head';
3425              ## reprocess              ## reprocess
3426              redo B;              redo B;
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
3427            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
3428              my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};              my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};
3429              !!!create-element ($self->{head_element}, 'head', $attr);              !!!create-element ($self->{head_element}, 'head', $attr);
# Line 3429  sub _tree_construction_main ($) { Line 3475  sub _tree_construction_main ($) {
3475              }              }
3476                            
3477              #              #
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
3478            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
3479              if ({base => ($self->{insertion_mode} eq 'in head' or              if ({base => ($self->{insertion_mode} eq 'in head' or
3480                            $self->{insertion_mode} eq 'after head'),                            $self->{insertion_mode} eq 'after head'),
3481                   link => 1, meta => 1}->{$token->{tag_name}}) {                   link => 1}->{$token->{tag_name}}) {
3482                  ## NOTE: There is a "as if in head" code clone.
3483                  if ($self->{insertion_mode} eq 'after head') {
3484                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3485                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3486                  }
3487                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3488                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3489                  pop @{$self->{open_elements}}
3490                      if $self->{insertion_mode} eq 'after head';
3491                  !!!next-token;
3492                  redo B;
3493                } elsif ($token->{tag_name} eq 'meta') {
3494                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3495                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} eq 'after head') {
3496                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
# Line 3445  sub _tree_construction_main ($) { Line 3498  sub _tree_construction_main ($) {
3498                }                }
3499                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3500                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3501    
3502                  unless ($self->{confident}) {
3503                    my $charset;
3504                    if ($token->{attributes}->{charset}) { ## TODO: And if supported
3505                      $charset = $token->{attributes}->{charset}->{value};
3506                    }
3507                    if ($token->{attributes}->{'http-equiv'}) {
3508                      ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
3509                      if ($token->{attributes}->{'http-equiv'}->{value}
3510                          =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
3511                              [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
3512                              ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
3513                        $charset = defined $1 ? $1 : defined $2 ? $2 : $3;
3514                      } ## TODO: And if supported
3515                    }
3516                    ## TODO: Change the encoding
3517                  }
3518    
3519                ## TODO: Extracting |charset| from |meta|.                ## TODO: Extracting |charset| from |meta|.
3520                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
3521                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} eq 'after head';
# Line 3459  sub _tree_construction_main ($) { Line 3530  sub _tree_construction_main ($) {
3530                }                }
3531                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
3532                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
3533                $parse_rcdata->('RCDATA', sub { $parent->append_child ($_[0]) });                $parse_rcdata->(RCDATA_CONTENT_MODEL,
3534                                  sub { $parent->append_child ($_[0]) });
3535                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
3536                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} eq 'after head';
3537                redo B;                redo B;
# Line 3471  sub _tree_construction_main ($) { Line 3543  sub _tree_construction_main ($) {
3543                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3544                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3545                }                }
3546                $parse_rcdata->('CDATA', $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
3547                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
3548                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} eq 'after head';
3549                redo B;                redo B;
# Line 3485  sub _tree_construction_main ($) { Line 3557  sub _tree_construction_main ($) {
3557                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} eq 'in head noscript') {
3558                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript:noscript');
3559                  ## Ignore the token                  ## Ignore the token
3560                    !!!next-token;
3561                  redo B;                  redo B;
3562                } else {                } else {
3563                  #                  #
# Line 3573  sub _tree_construction_main ($) { Line 3646  sub _tree_construction_main ($) {
3646            redo B;            redo B;
3647    
3648            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
3649          } elsif ($self->{insertion_mode} eq 'in body') {          } elsif ($self->{insertion_mode} eq 'in body' or
3650                     $self->{insertion_mode} eq 'in cell' or
3651                     $self->{insertion_mode} eq 'in caption') {
3652            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
3653              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
3654              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
# Line 3582  sub _tree_construction_main ($) { Line 3657  sub _tree_construction_main ($) {
3657    
3658              !!!next-token;              !!!next-token;
3659              redo B;              redo B;
3660            } elsif ($token->{type} eq 'comment') {            } elsif ($token->{type} eq 'start tag') {
3661              ## NOTE: There is a code clone of "comment in body".              if ({
3662              my $comment = $self->{document}->create_comment ($token->{data});                   caption => 1, col => 1, colgroup => 1, tbody => 1,
3663              $self->{open_elements}->[-1]->[0]->append_child ($comment);                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3664              !!!next-token;                  }->{$token->{tag_name}}) {
3665              redo B;                if ($self->{insertion_mode} eq 'in cell') {
3666                    ## have an element in table scope
3667                    my $tn;
3668                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3669                      my $node = $self->{open_elements}->[$_];
3670                      if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3671                        $tn = $node->[1];
3672                        last INSCOPE;
3673                      } elsif ({
3674                                table => 1, html => 1,
3675                               }->{$node->[1]}) {
3676                        last INSCOPE;
3677                      }
3678                    } # INSCOPE
3679                      unless (defined $tn) {
3680                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3681                        ## Ignore the token
3682                        !!!next-token;
3683                        redo B;
3684                      }
3685                    
3686                    ## Close the cell
3687                    !!!back-token; # <?>
3688                    $token = {type => 'end tag', tag_name => $tn};
3689                    redo B;
3690                  } elsif ($self->{insertion_mode} eq 'in caption') {
3691                    !!!parse-error (type => 'not closed:caption');
3692                    
3693                    ## As if </caption>
3694                    ## have a table element in table scope
3695                    my $i;
3696                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3697                      my $node = $self->{open_elements}->[$_];
3698                      if ($node->[1] eq 'caption') {
3699                        $i = $_;
3700                        last INSCOPE;
3701                      } elsif ({
3702                                table => 1, html => 1,
3703                               }->{$node->[1]}) {
3704                        last INSCOPE;
3705                      }
3706                    } # INSCOPE
3707                      unless (defined $i) {
3708                        !!!parse-error (type => 'unmatched end tag:caption');
3709                        ## Ignore the token
3710                        !!!next-token;
3711                        redo B;
3712                      }
3713                    
3714                    ## generate implied end tags
3715                    if ({
3716                         dd => 1, dt => 1, li => 1, p => 1,
3717                         td => 1, th => 1, tr => 1,
3718                         tbody => 1, tfoot=> 1, thead => 1,
3719                        }->{$self->{open_elements}->[-1]->[1]}) {
3720                      !!!back-token; # <?>
3721                      $token = {type => 'end tag', tag_name => 'caption'};
3722                      !!!back-token;
3723                      $token = {type => 'end tag',
3724                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3725                      redo B;
3726                    }
3727    
3728                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3729                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3730                    }
3731                    
3732                    splice @{$self->{open_elements}}, $i;
3733                    
3734                    $clear_up_to_marker->();
3735                    
3736                    $self->{insertion_mode} = 'in table';
3737                    
3738                    ## reprocess
3739                    redo B;
3740                  } else {
3741                    #
3742                  }
3743                } else {
3744                  #
3745                }
3746              } elsif ($token->{type} eq 'end tag') {
3747                if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3748                  if ($self->{insertion_mode} eq 'in cell') {
3749                    ## have an element in table scope
3750                    my $i;
3751                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3752                      my $node = $self->{open_elements}->[$_];
3753                      if ($node->[1] eq $token->{tag_name}) {
3754                        $i = $_;
3755                        last INSCOPE;
3756                      } elsif ({
3757                                table => 1, html => 1,
3758                               }->{$node->[1]}) {
3759                        last INSCOPE;
3760                      }
3761                    } # INSCOPE
3762                      unless (defined $i) {
3763                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3764                        ## Ignore the token
3765                        !!!next-token;
3766                        redo B;
3767                      }
3768                    
3769                    ## generate implied end tags
3770                    if ({
3771                         dd => 1, dt => 1, li => 1, p => 1,
3772                         td => ($token->{tag_name} eq 'th'),
3773                         th => ($token->{tag_name} eq 'td'),
3774                         tr => 1,
3775                         tbody => 1, tfoot=> 1, thead => 1,
3776                        }->{$self->{open_elements}->[-1]->[1]}) {
3777                      !!!back-token;
3778                      $token = {type => 'end tag',
3779                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3780                      redo B;
3781                    }
3782                    
3783                    if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3784                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3785                    }
3786                    
3787                    splice @{$self->{open_elements}}, $i;
3788                    
3789                    $clear_up_to_marker->();
3790                    
3791                    $self->{insertion_mode} = 'in row';
3792                    
3793                    !!!next-token;
3794                    redo B;
3795                  } elsif ($self->{insertion_mode} eq 'in caption') {
3796                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3797                    ## Ignore the token
3798                    !!!next-token;
3799                    redo B;
3800                  } else {
3801                    #
3802                  }
3803                } elsif ($token->{tag_name} eq 'caption') {
3804                  if ($self->{insertion_mode} eq 'in caption') {
3805                    ## have a table element in table scope
3806                    my $i;
3807                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3808                      my $node = $self->{open_elements}->[$_];
3809                      if ($node->[1] eq $token->{tag_name}) {
3810                        $i = $_;
3811                        last INSCOPE;
3812                      } elsif ({
3813                                table => 1, html => 1,
3814                               }->{$node->[1]}) {
3815                        last INSCOPE;
3816                      }
3817                    } # INSCOPE
3818                      unless (defined $i) {
3819                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3820                        ## Ignore the token
3821                        !!!next-token;
3822                        redo B;
3823                      }
3824                    
3825                    ## generate implied end tags
3826                    if ({
3827                         dd => 1, dt => 1, li => 1, p => 1,
3828                         td => 1, th => 1, tr => 1,
3829                         tbody => 1, tfoot=> 1, thead => 1,
3830                        }->{$self->{open_elements}->[-1]->[1]}) {
3831                      !!!back-token;
3832                      $token = {type => 'end tag',
3833                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3834                      redo B;
3835                    }
3836                    
3837                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3838                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3839                    }
3840                    
3841                    splice @{$self->{open_elements}}, $i;
3842                    
3843                    $clear_up_to_marker->();
3844                    
3845                    $self->{insertion_mode} = 'in table';
3846                    
3847                    !!!next-token;
3848                    redo B;
3849                  } elsif ($self->{insertion_mode} eq 'in cell') {
3850                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3851                    ## Ignore the token
3852                    !!!next-token;
3853                    redo B;
3854                  } else {
3855                    #
3856                  }
3857                } elsif ({
3858                          table => 1, tbody => 1, tfoot => 1,
3859                          thead => 1, tr => 1,
3860                         }->{$token->{tag_name}} and
3861                         $self->{insertion_mode} eq 'in cell') {
3862                  ## have an element in table scope
3863                  my $i;
3864                  my $tn;
3865                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3866                    my $node = $self->{open_elements}->[$_];
3867                    if ($node->[1] eq $token->{tag_name}) {
3868                      $i = $_;
3869                      last INSCOPE;
3870                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
3871                      $tn = $node->[1];
3872                      ## NOTE: There is exactly one |td| or |th| element
3873                      ## in scope in the stack of open elements by definition.
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                  ## Close the cell
3888                  !!!back-token; # </?>
3889                  $token = {type => 'end tag', tag_name => $tn};
3890                  redo B;
3891                } elsif ($token->{tag_name} eq 'table' and
3892                         $self->{insertion_mode} eq 'in caption') {
3893                  !!!parse-error (type => 'not closed:caption');
3894    
3895                  ## As if </caption>
3896                  ## have a table element in table scope
3897                  my $i;
3898                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3899                    my $node = $self->{open_elements}->[$_];
3900                    if ($node->[1] eq 'caption') {
3901                      $i = $_;
3902                      last INSCOPE;
3903                    } elsif ({
3904                              table => 1, html => 1,
3905                             }->{$node->[1]}) {
3906                      last INSCOPE;
3907                    }
3908                  } # INSCOPE
3909                  unless (defined $i) {
3910                    !!!parse-error (type => 'unmatched end tag:caption');
3911                    ## Ignore the token
3912                    !!!next-token;
3913                    redo B;
3914                  }
3915                  
3916                  ## generate implied end tags
3917                  if ({
3918                       dd => 1, dt => 1, li => 1, p => 1,
3919                       td => 1, th => 1, tr => 1,
3920                       tbody => 1, tfoot=> 1, thead => 1,
3921                      }->{$self->{open_elements}->[-1]->[1]}) {
3922                    !!!back-token; # </table>
3923                    $token = {type => 'end tag', tag_name => 'caption'};
3924                    !!!back-token;
3925                    $token = {type => 'end tag',
3926                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3927                    redo B;
3928                  }
3929    
3930                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3931                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3932                  }
3933    
3934                  splice @{$self->{open_elements}}, $i;
3935    
3936                  $clear_up_to_marker->();
3937    
3938                  $self->{insertion_mode} = 'in table';
3939    
3940                  ## reprocess
3941                  redo B;
3942                } elsif ({
3943                          body => 1, col => 1, colgroup => 1, html => 1,
3944                         }->{$token->{tag_name}}) {
3945                  if ($self->{insertion_mode} eq 'in cell' or
3946                      $self->{insertion_mode} eq 'in caption') {
3947                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3948                    ## Ignore the token
3949                    !!!next-token;
3950                    redo B;
3951                  } else {
3952                    #
3953                  }
3954                } elsif ({
3955                          tbody => 1, tfoot => 1,
3956                          thead => 1, tr => 1,
3957                         }->{$token->{tag_name}} and
3958                         $self->{insertion_mode} eq 'in caption') {
3959                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3960                  ## Ignore the token
3961                  !!!next-token;
3962                  redo B;
3963                } else {
3964                  #
3965                }
3966            } else {            } else {
3967              $in_body->($insert_to_current);              #
             redo B;  
3968            }            }
3969              
3970              $in_body->($insert_to_current);
3971              redo B;
3972          } elsif ($self->{insertion_mode} eq 'in table') {          } elsif ($self->{insertion_mode} eq 'in table') {
3973            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
3974              ## NOTE: There are "character in table" code clones.              ## NOTE: There are "character in table" code clones.
# Line 3651  sub _tree_construction_main ($) { Line 4028  sub _tree_construction_main ($) {
4028                            
4029              !!!next-token;              !!!next-token;
4030              redo B;              redo B;
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
4031            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
4032              if ({              if ({
4033                   caption => 1,                   caption => 1,
# Line 3813  sub _tree_construction_main ($) { Line 4185  sub _tree_construction_main ($) {
4185            !!!parse-error (type => 'in table:'.$token->{tag_name});            !!!parse-error (type => 'in table:'.$token->{tag_name});
4186            $in_body->($insert_to_foster);            $in_body->($insert_to_foster);
4187            redo B;            redo B;
         } elsif ($self->{insertion_mode} eq 'in caption') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: This is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1, col => 1, colgroup => 1, tbody => 1,  
                  td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,  
                 }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'not closed:caption');  
   
               ## As if </caption>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'caption') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:caption');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <?>  
                 $token = {type => 'end tag', tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'caption') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'caption') {  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'caption') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               !!!parse-error (type => 'not closed:caption');  
   
               ## As if </caption>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'caption') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:caption');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # </table>  
                 $token = {type => 'end tag', tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'caption') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, col => 1, colgroup => 1,  
                       html => 1, tbody => 1, td => 1, tfoot => 1,  
                       th => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
                 
           $in_body->($insert_to_current);  
           redo B;  
4188          } elsif ($self->{insertion_mode} eq 'in column group') {          } elsif ($self->{insertion_mode} eq 'in column group') {
4189            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
4190              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
# Line 4009  sub _tree_construction_main ($) { Line 4196  sub _tree_construction_main ($) {
4196              }              }
4197                            
4198              #              #
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
4199            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
4200              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
4201                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
# Line 4119  sub _tree_construction_main ($) { Line 4301  sub _tree_construction_main ($) {
4301                            
4302              !!!next-token;              !!!next-token;
4303              redo B;              redo B;
           } elsif ($token->{type} eq 'comment') {  
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
4304            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
4305              if ({              if ({
4306                   tr => 1,                   tr => 1,
# Line 4404  sub _tree_construction_main ($) { Line 4580  sub _tree_construction_main ($) {
4580                            
4581              !!!next-token;              !!!next-token;
4582              redo B;              redo B;
           } elsif ($token->{type} eq 'comment') {  
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
4583            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
4584              if ($token->{tag_name} eq 'th' or              if ($token->{tag_name} eq 'th' or
4585                  $token->{tag_name} eq 'td') {                  $token->{tag_name} eq 'td') {
# Line 4660  sub _tree_construction_main ($) { Line 4830  sub _tree_construction_main ($) {
4830            !!!parse-error (type => 'in table:'.$token->{tag_name});            !!!parse-error (type => 'in table:'.$token->{tag_name});
4831            $in_body->($insert_to_foster);            $in_body->($insert_to_foster);
4832            redo B;            redo B;
         } elsif ($self->{insertion_mode} eq 'in cell') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: This is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1, col => 1, colgroup => 1,  
                  tbody => 1, td => 1, tfoot => 1, th => 1,  
                  thead => 1, tr => 1,  
                 }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $tn) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Close the cell  
               !!!back-token; # <?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => ($token->{tag_name} eq 'th'),  
                    th => ($token->{tag_name} eq 'td'),  
                    tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in row';  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1,  
                       colgroup => 1, html => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } elsif ({  
                       table => 1, tbody => 1, tfoot => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   ## NOTE: There is exactly one |td| or |th| element  
                   ## in scope in the stack of open elements by definition.  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           $in_body->($insert_to_current);  
           redo B;  
4833          } elsif ($self->{insertion_mode} eq 'in select') {          } elsif ($self->{insertion_mode} eq 'in select') {
4834            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
4835              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4836              !!!next-token;              !!!next-token;
4837              redo B;              redo B;
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
4838            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
4839              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
4840                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
# Line 4988  sub _tree_construction_main ($) { Line 5007  sub _tree_construction_main ($) {
5007          } elsif ($self->{insertion_mode} eq 'after body') {          } elsif ($self->{insertion_mode} eq 'after body') {
5008            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
5009              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5010                  my $data = $1;
5011                ## As if in body                ## As if in body
5012                $reconstruct_active_formatting_elements->($insert_to_current);                $reconstruct_active_formatting_elements->($insert_to_current);
5013                                
5014                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5015    
5016                unless (length $token->{data}) {                unless (length $token->{data}) {
5017                  !!!next-token;                  !!!next-token;
# Line 5000  sub _tree_construction_main ($) { Line 5020  sub _tree_construction_main ($) {
5020              }              }
5021                            
5022              #              #
5023              !!!parse-error (type => 'after body:#'.$token->{type});              !!!parse-error (type => 'after body:#character');
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[0]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
5024            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
5025              !!!parse-error (type => 'after body:'.$token->{tag_name});              !!!parse-error (type => 'after body:'.$token->{tag_name});
5026              #              #
# Line 5017  sub _tree_construction_main ($) { Line 5032  sub _tree_construction_main ($) {
5032                  !!!next-token;                  !!!next-token;
5033                  redo B;                  redo B;
5034                } else {                } else {
5035                  $phase = 'trailing end';                  $previous_insertion_mode = $self->{insertion_mode};
5036                    $self->{insertion_mode} = 'trailing end';
5037                  !!!next-token;                  !!!next-token;
5038                  redo B;                  redo B;
5039                }                }
# Line 5025  sub _tree_construction_main ($) { Line 5041  sub _tree_construction_main ($) {
5041                !!!parse-error (type => 'after body:/'.$token->{tag_name});                !!!parse-error (type => 'after body:/'.$token->{tag_name});
5042              }              }
5043            } else {            } else {
5044              !!!parse-error (type => 'after body:#'.$token->{type});              die "$0: $token->{type}: Unknown token type";
5045            }            }
5046    
5047            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = 'in body';
5048            ## reprocess            ## reprocess
5049            redo B;            redo B;
5050          } elsif ($self->{insertion_mode} eq 'in frameset') {      } elsif ($self->{insertion_mode} eq 'in frameset') {
5051            if ($token->{type} eq 'character') {        if ($token->{type} eq 'character') {
5052              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5053                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
   
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
5054    
5055              #            unless (length $token->{data}) {
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
5056              !!!next-token;              !!!next-token;
5057              redo B;              redo B;
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'frameset') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'frame') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'noframes') {  
               $in_body->($insert_to_current);  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'frameset') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html' and  
                   @{$self->{open_elements}} == 1) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
               } else {  
                 pop @{$self->{open_elements}};  
                 !!!next-token;  
               }  
                 
               ## if not inner_html and  
               if ($self->{open_elements}->[-1]->[1] ne 'frameset') {  
                 $self->{insertion_mode} = 'after frameset';  
               }  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
5058            }            }
5059                      }
5060            if (defined $token->{tag_name}) {  
5061              !!!parse-error (type => 'in frameset:'.($token->{type} eq 'end tag' ? '/' : '').$token->{tag_name});          !!!parse-error (type => 'in frameset:#character');
5062            ## Ignore the token
5063            !!!next-token;
5064            redo B;
5065          } elsif ($token->{type} eq 'start tag') {
5066            if ($token->{tag_name} eq 'frameset') {
5067              !!!insert-element ($token->{tag_name}, $token->{attributes});
5068              !!!next-token;
5069              redo B;
5070            } elsif ($token->{tag_name} eq 'frame') {
5071              !!!insert-element ($token->{tag_name}, $token->{attributes});
5072              pop @{$self->{open_elements}};
5073              !!!next-token;
5074              redo B;
5075            } elsif ($token->{tag_name} eq 'noframes') {
5076              $in_body->($insert_to_current);
5077              redo B;
5078            } else {
5079              !!!parse-error (type => 'in frameset:'.$token->{tag_name});
5080              ## Ignore the token
5081              !!!next-token;
5082              redo B;
5083            }
5084          } elsif ($token->{type} eq 'end tag') {
5085            if ($token->{tag_name} eq 'frameset') {
5086              if ($self->{open_elements}->[-1]->[1] eq 'html' and
5087                  @{$self->{open_elements}} == 1) {
5088                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5089                ## Ignore the token
5090                !!!next-token;
5091            } else {            } else {
5092              !!!parse-error (type => 'in frameset:#'.$token->{type});              pop @{$self->{open_elements}};
5093                !!!next-token;
5094              }
5095    
5096              if (not defined $self->{inner_html_node} and
5097                  $self->{open_elements}->[-1]->[1] ne 'frameset') {
5098                $self->{insertion_mode} = 'after frameset';
5099            }            }
5100              redo B;
5101            } else {
5102              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
5103            ## Ignore the token            ## Ignore the token
5104            !!!next-token;            !!!next-token;
5105            redo B;            redo B;
5106          } elsif ($self->{insertion_mode} eq 'after frameset') {          }
5107            if ($token->{type} eq 'character') {        } else {
5108            die "$0: $token->{type}: Unknown token type";
5109          }
5110        } elsif ($self->{insertion_mode} eq 'after frameset') {
5111          if ($token->{type} eq 'character') {
5112              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5113                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5114    
5115                unless (length $token->{data}) {                unless (length $token->{data}) {
5116                  !!!next-token;                  !!!next-token;
# Line 5107  sub _tree_construction_main ($) { Line 5118  sub _tree_construction_main ($) {
5118                }                }
5119              }              }
5120    
5121              #              if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
5122            } elsif ($token->{type} eq 'comment') {                !!!parse-error (type => 'after frameset:#character');
5123              my $comment = $self->{document}->create_comment ($token->{data});  
5124              $self->{open_elements}->[-1]->[0]->append_child ($comment);                ## Ignore the token.
5125              !!!next-token;                if (length $token->{data}) {
5126              redo B;                  ## reprocess the rest of characters
5127            } elsif ($token->{type} eq 'start tag') {                } else {
5128              if ($token->{tag_name} eq 'noframes') {                  !!!next-token;
5129                $in_body->($insert_to_current);                }
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'html') {  
               $phase = 'trailing end';  
               !!!next-token;  
5130                redo B;                redo B;
             } else {  
               #  
5131              }              }
5132            } else {  
5133              #          die qq[$0: Character "$token->{data}"];
5134            }        } elsif ($token->{type} eq 'start tag') {
5135                      if ($token->{tag_name} eq 'noframes') {
5136            if (defined $token->{tag_name}) {            $in_body->($insert_to_current);
5137              !!!parse-error (type => 'after frameset:'.($token->{tag_name} eq 'end tag' ? '/' : '').$token->{tag_name});            redo B;
5138            } else {          } else {
5139              !!!parse-error (type => 'after frameset:#'.$token->{type});            !!!parse-error (type => 'after frameset:'.$token->{tag_name});
           }  
5140            ## Ignore the token            ## Ignore the token
5141            !!!next-token;            !!!next-token;
5142            redo B;            redo B;
5143            }
5144            ## ISSUE: An issue in spec there        } elsif ($token->{type} eq 'end tag') {
5145            if ($token->{tag_name} eq 'html') {
5146              $previous_insertion_mode = $self->{insertion_mode};
5147              $self->{insertion_mode} = 'trailing end';
5148              !!!next-token;
5149              redo B;
5150          } else {          } else {
5151            die "$0: $self->{insertion_mode}: Unknown insertion mode";            !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
5152              ## Ignore the token
5153              !!!next-token;
5154              redo B;
5155          }          }
5156          } else {
5157            die "$0: $token->{type}: Unknown token type";
5158        }        }
5159      } elsif ($phase eq 'trailing end') {  
5160          ## ISSUE: An issue in spec here
5161        } elsif ($self->{insertion_mode} eq 'trailing end') {
5162        ## states in the main stage is preserved yet # MUST        ## states in the main stage is preserved yet # MUST
5163                
5164        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} eq 'character') {
         !!!parse-error (type => 'after html:#DOCTYPE');  
         ## Ignore the token  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'comment') {  
         my $comment = $self->{document}->create_comment ($token->{data});  
         $self->{document}->append_child ($comment);  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'character') {  
5165          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5166            my $data = $1;            my $data = $1;
5167            ## As if in the main phase.            ## As if in the main phase.
5168            ## NOTE: The insertion mode in the main phase            ## NOTE: The insertion mode in the main phase
5169            ## just before the phase has been changed to the trailing            ## just before the phase has been changed to the trailing
5170            ## end phase is either "after body" or "after frameset".            ## end phase is either "after body" or "after frameset".
5171            $reconstruct_active_formatting_elements->($insert_to_current)            $reconstruct_active_formatting_elements->($insert_to_current);
             if $phase eq 'main';  
5172                        
5173            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);
5174                        
# Line 5178  sub _tree_construction_main ($) { Line 5179  sub _tree_construction_main ($) {
5179          }          }
5180    
5181          !!!parse-error (type => 'after html:#character');          !!!parse-error (type => 'after html:#character');
5182          $phase = 'main';          $self->{insertion_mode} = $previous_insertion_mode;
5183          ## reprocess          ## reprocess
5184          redo B;          redo B;
5185        } elsif ($token->{type} eq 'start tag' or        } elsif ($token->{type} eq 'start tag') {
5186                 $token->{type} eq 'end tag') {          !!!parse-error (type => 'after html:'.$token->{tag_name});
5187          !!!parse-error (type => 'after html:'.($token->{type} eq 'end tag' ? '/' : '').$token->{tag_name});          $self->{insertion_mode} = $previous_insertion_mode;
5188          $phase = 'main';          ## reprocess
5189            redo B;
5190          } elsif ($token->{type} eq 'end tag') {
5191            !!!parse-error (type => 'after html:/'.$token->{tag_name});
5192            $self->{insertion_mode} = $previous_insertion_mode;
5193          ## reprocess          ## reprocess
5194          redo B;          redo B;
       } elsif ($token->{type} eq 'end-of-file') {  
         ## Stop parsing  
         last B;  
5195        } else {        } else {
5196          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token";
5197        }        }
5198        } else {
5199          die "$0: $self->{insertion_mode}: Unknown insertion mode";
5200      }      }
5201    } # B    } # B
5202    
# Line 5280  sub set_inner_html ($$$) { Line 5284  sub set_inner_html ($$$) {
5284    
5285      ## Step 2      ## Step 2
5286      my $node_ln = $node->local_name;      my $node_ln = $node->local_name;
5287      $p->{content_model_flag} = {      $p->{content_model} = {
5288        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
5289        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
5290        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
5291        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
5292        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
5293        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
5294        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
5295        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
5296        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
5297        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
5298      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
5299         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
5300            unless defined $p->{content_model};
5301            ## ISSUE: What is "the name of the element"? local name?
5302    
5303      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
5304    

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24