/[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.25 by wakaba, Sun Jun 24 05:12:11 2007 UTC revision 1.51 by wakaba, Sat Jul 21 11:46:41 2007 UTC
# Line 7  our $VERSION=do{my @r=(q$Revision$=~/\d+ Line 7  our $VERSION=do{my @r=(q$Revision$=~/\d+
7  ## doc.write ('');  ## doc.write ('');
8  ## alert (doc.compatMode);  ## alert (doc.compatMode);
9    
10    ## ISSUE: HTML5 revision 967 says that the encoding layer MUST NOT
11    ## strip BOM and the HTML layer MUST ignore it.  Whether we can do it
12    ## is not yet clear.
13    ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?
14    ## "{U+FEFF}..." in GB18030?
15    
16  my $permitted_slash_tag_name = {  my $permitted_slash_tag_name = {
17    base => 1,    base => 1,
18    link => 1,    link => 1,
# Line 144  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 188  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 197  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 210  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 222  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 247  sub _get_next_token ($) { Line 258  sub _get_next_token ($) {
258      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} eq 'entity data') {
259        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
260                
261        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);
262    
263        $self->{state} = 'data';        $self->{state} = 'data';
264        # next-input-character is already done        # next-input-character is already done
# Line 260  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 274  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 321  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>
340            my @next_char;            my @next_char;
341            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
342              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_input_character};
# Line 418  sub _get_next_token ($) { Line 428  sub _get_next_token ($) {
428          redo A;          redo A;
429        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
430          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
431              $self->{current_token}->{first_start_tag}
432                  = 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 443  sub _get_next_token ($) { Line 455  sub _get_next_token ($) {
455        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
456          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
457          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
458              $self->{current_token}->{first_start_tag}
459                  = 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 489  sub _get_next_token ($) { Line 503  sub _get_next_token ($) {
503          redo A;          redo A;
504        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
505          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
506              $self->{current_token}->{first_start_tag}
507                  = 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 527  sub _get_next_token ($) { Line 543  sub _get_next_token ($) {
543        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
544          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
545          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
546              $self->{current_token}->{first_start_tag}
547                  = 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 553  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 578  sub _get_next_token ($) { Line 596  sub _get_next_token ($) {
596        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
597          $before_leave->();          $before_leave->();
598          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
599              $self->{current_token}->{first_start_tag}
600                  = 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 617  sub _get_next_token ($) { Line 637  sub _get_next_token ($) {
637          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
638          $before_leave->();          $before_leave->();
639          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
640              $self->{current_token}->{first_start_tag}
641                  = 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 653  sub _get_next_token ($) { Line 675  sub _get_next_token ($) {
675          redo A;          redo A;
676        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
677          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
678              $self->{current_token}->{first_start_tag}
679                  = 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 684  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 691  sub _get_next_token ($) { Line 716  sub _get_next_token ($) {
716        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
717          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
718          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
719              $self->{current_token}->{first_start_tag}
720                  = 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 736  sub _get_next_token ($) { Line 763  sub _get_next_token ($) {
763          redo A;          redo A;
764        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
765          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
766              $self->{current_token}->{first_start_tag}
767                  = 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 754  sub _get_next_token ($) { Line 783  sub _get_next_token ($) {
783        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
784          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
785          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
786              $self->{current_token}->{first_start_tag}
787                  = 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 788  sub _get_next_token ($) { Line 819  sub _get_next_token ($) {
819        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
820          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
821          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
822              $self->{current_token}->{first_start_tag}
823                  = 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 822  sub _get_next_token ($) { Line 855  sub _get_next_token ($) {
855        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
856          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
857          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
858              $self->{current_token}->{first_start_tag}
859                  = 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 859  sub _get_next_token ($) { Line 894  sub _get_next_token ($) {
894          redo A;          redo A;
895        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
896          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
897              $self->{current_token}->{first_start_tag}
898                  = 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 877  sub _get_next_token ($) { Line 914  sub _get_next_token ($) {
914        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
915          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
916          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
917              $self->{current_token}->{first_start_tag}
918                  = 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 899  sub _get_next_token ($) { Line 938  sub _get_next_token ($) {
938          redo A;          redo A;
939        }        }
940      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} eq 'entity in attribute value') {
941        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);
942    
943        unless (defined $token) {        unless (defined $token) {
944          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
# Line 990  sub _get_next_token ($) { Line 1029  sub _get_next_token ($) {
1029          }          }
1030        }        }
1031    
1032        !!!parse-error (type => 'bogus comment open');        !!!parse-error (type => 'bogus comment');
1033        $self->{next_input_character} = shift @next_char;        $self->{next_input_character} = shift @next_char;
1034        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1035        $self->{state} = 'bogus comment';        $self->{state} = 'bogus comment';
# Line 1049  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 1409  sub _get_next_token ($) { Line 1448  sub _get_next_token ($) {
1448          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1449    
1450          $self->{state} = 'data';          $self->{state} = 'data';
1451          ## recomsume          ## reconsume
1452    
1453          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1454          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1452  sub _get_next_token ($) { Line 1491  sub _get_next_token ($) {
1491          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1492    
1493          $self->{state} = 'data';          $self->{state} = 'data';
1494          ## recomsume          ## reconsume
1495    
1496          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1497          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1498    
1499          redo A;          redo A;
1500        } else {        } else {
1501          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after SYSTEM');
1502          $self->{state} = 'bogus DOCTYPE';          $self->{state} = 'bogus DOCTYPE';
1503          !!!next-input-character;          !!!next-input-character;
1504          redo A;          redo A;
# Line 1527  sub _get_next_token ($) { Line 1566  sub _get_next_token ($) {
1566          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1567    
1568          $self->{state} = 'data';          $self->{state} = 'data';
1569          ## recomsume          ## reconsume
1570    
1571          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1572          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1570  sub _get_next_token ($) { Line 1609  sub _get_next_token ($) {
1609    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
1610  } # _get_next_token  } # _get_next_token
1611    
1612  sub _tokenize_attempt_to_consume_an_entity ($) {  sub _tokenize_attempt_to_consume_an_entity ($$) {
1613    my $self = shift;    my ($self, $in_attr) = @_;
1614    
1615    if ({    if ({
1616         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
# Line 1584  sub _tokenize_attempt_to_consume_an_enti Line 1623  sub _tokenize_attempt_to_consume_an_enti
1623      !!!next-input-character;      !!!next-input-character;
1624      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_input_character} == 0x0078 or # x
1625          $self->{next_input_character} == 0x0058) { # X          $self->{next_input_character} == 0x0058) { # X
1626        my $num;        my $code;
1627        X: {        X: {
1628          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_input_character};
1629          !!!next-input-character;          !!!next-input-character;
1630          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_input_character} and
1631              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_input_character} <= 0x0039) { # 0..9
1632            $num ||= 0;            $code ||= 0;
1633            $num *= 0x10;            $code *= 0x10;
1634            $num += $self->{next_input_character} - 0x0030;            $code += $self->{next_input_character} - 0x0030;
1635            redo X;            redo X;
1636          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
1637                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_input_character} <= 0x0066) { # a..f
1638            ## ISSUE: the spec says U+0078, which is apparently incorrect            $code ||= 0;
1639            $num ||= 0;            $code *= 0x10;
1640            $num *= 0x10;            $code += $self->{next_input_character} - 0x0060 + 9;
           $num += $self->{next_input_character} - 0x0060 + 9;  
1641            redo X;            redo X;
1642          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
1643                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_input_character} <= 0x0046) { # A..F
1644            ## ISSUE: the spec says U+0058, which is apparently incorrect            $code ||= 0;
1645            $num ||= 0;            $code *= 0x10;
1646            $num *= 0x10;            $code += $self->{next_input_character} - 0x0040 + 9;
           $num += $self->{next_input_character} - 0x0040 + 9;  
1647            redo X;            redo X;
1648          } elsif (not defined $num) { # 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 1619  sub _tokenize_attempt_to_consume_an_enti Line 1656  sub _tokenize_attempt_to_consume_an_enti
1656            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc');
1657          }          }
1658    
1659          ## TODO: check the definition for |a valid Unicode character|.          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1660          ## <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8189>            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1661          if ($num > 1114111 or $num == 0) {            $code = 0xFFFD;
1662            $num = 0xFFFD; # REPLACEMENT CHARACTER          } elsif ($code > 0x10FFFF) {
1663            ## ISSUE: Why this is not an error?            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1664          } elsif (0x80 <= $num and $num <= 0x9F) {            $code = 0xFFFD;
1665            !!!parse-error (type => sprintf 'c1 entity:U+%04X', $num);          } elsif ($code == 0x000D) {
1666            $num = $c1_entity_char->{$num};            !!!parse-error (type => 'CR character reference');
1667              $code = 0x000A;
1668            } elsif (0x80 <= $code and $code <= 0x9F) {
1669              !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
1670              $code = $c1_entity_char->{$code};
1671          }          }
1672    
1673          return {type => 'character', data => chr $num};          return {type => 'character', data => chr $code};
1674        } # X        } # X
1675      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_input_character} and
1676               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_input_character} <= 0x0039) { # 0..9
# Line 1650  sub _tokenize_attempt_to_consume_an_enti Line 1691  sub _tokenize_attempt_to_consume_an_enti
1691          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc');
1692        }        }
1693    
1694        ## TODO: check the definition for |a valid Unicode character|.        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1695        if ($code > 1114111 or $code == 0) {          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1696          $code = 0xFFFD; # REPLACEMENT CHARACTER          $code = 0xFFFD;
1697          ## ISSUE: Why this is not an error?        } elsif ($code > 0x10FFFF) {
1698            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1699            $code = 0xFFFD;
1700          } elsif ($code == 0x000D) {
1701            !!!parse-error (type => 'CR character reference');
1702            $code = 0x000A;
1703        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
1704          !!!parse-error (type => sprintf 'c1 entity:U+%04X', $code);          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
1705          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
1706        }        }
1707                
# Line 1674  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 1689  sub _tokenize_attempt_to_consume_an_enti Line 1735  sub _tokenize_attempt_to_consume_an_enti
1735              $self->{next_input_character} == 0x003B)) { # ;              $self->{next_input_character} == 0x003B)) { # ;
1736        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_input_character};
1737        if (defined $EntityChar->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
         $value = $EntityChar->{$entity_name};  
1738          if ($self->{next_input_character} == 0x003B) { # ;          if ($self->{next_input_character} == 0x003B) { # ;
1739              $value = $EntityChar->{$entity_name};
1740            $match = 1;            $match = 1;
1741            !!!next-input-character;            !!!next-input-character;
1742            last;            last;
1743          } else {          } else {
1744              $value = $EntityChar->{$entity_name};
1745            $match = -1;            $match = -1;
1746              !!!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 => '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.
1767        !!!back-token ({type => 'character', data => $value});        return {type => 'character', data => '&'.$value};
       return undef;  
1768      }      }
1769    } else {    } else {
1770      ## no characters are consumed      ## no characters are consumed
# Line 1907  sub _tree_construction_initial ($) { Line 1959  sub _tree_construction_initial ($) {
1959      } elsif ($token->{type} eq 'character') {      } elsif ($token->{type} eq 'character') {
1960        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
1961          ## Ignore the token          ## Ignore the token
1962    
1963          unless (length $token->{data}) {          unless (length $token->{data}) {
1964            ## Stay in the phase            ## Stay in the phase
1965            !!!next-token;            !!!next-token;
# Line 1949  sub _tree_construction_root_element ($) Line 2002  sub _tree_construction_root_element ($)
2002          !!!next-token;          !!!next-token;
2003          redo B;          redo B;
2004        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} eq 'character') {
2005          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2006            $self->{document}->manakai_append_text ($1);            ## Ignore the token.
2007            ## ISSUE: DOM3 Core does not allow Document > Text  
2008            unless (length $token->{data}) {            unless (length $token->{data}) {
2009              ## Stay in the phase              ## Stay in the phase
2010              !!!next-token;              !!!next-token;
# Line 1972  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 1991  sub _reset_insertion_mode ($) { Line 2043  sub _reset_insertion_mode ($) {
2043            
2044      ## Step 3      ## Step 3
2045      S3: {      S3: {
2046        $last = 1 if $self->{open_elements}->[0]->[0] eq $node->[0];        ## ISSUE: Oops! "If node is the first node in the stack of open
2047        if (defined $self->{inner_html_node}) {        ## elements, then set last to true. If the context element of the
2048          if ($self->{inner_html_node}->[1] eq 'td' or        ## HTML fragment parsing algorithm is neither a td element nor a
2049              $self->{inner_html_node}->[1] eq 'th') {        ## th element, then set node to the context element. (fragment case)":
2050            #        ## The second "if" is in the scope of the first "if"!?
2051          } else {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2052            $node = $self->{inner_html_node};          $last = 1;
2053            if (defined $self->{inner_html_node}) {
2054              if ($self->{inner_html_node}->[1] eq 'td' or
2055                  $self->{inner_html_node}->[1] eq 'th') {
2056                #
2057              } else {
2058                $node = $self->{inner_html_node};
2059              }
2060          }          }
2061        }        }
2062            
# Line 2008  sub _reset_insertion_mode ($) { Line 2067  sub _reset_insertion_mode ($) {
2067                        th => 'in cell',                        th => 'in cell',
2068                        tr => 'in row',                        tr => 'in row',
2069                        tbody => 'in table body',                        tbody => 'in table body',
2070                        thead => 'in table head',                        thead => 'in table body',
2071                        tfoot => 'in table foot',                        tfoot => 'in table body',
2072                        caption => 'in caption',                        caption => 'in caption',
2073                        colgroup => 'in column group',                        colgroup => 'in column group',
2074                        table => 'in table',                        table => 'in table',
# Line 2044  sub _reset_insertion_mode ($) { Line 2103  sub _reset_insertion_mode ($) {
2103  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
2104    my $self = shift;    my $self = shift;
2105    
   my $phase = 'main';  
   
2106    my $active_formatting_elements = [];    my $active_formatting_elements = [];
2107    
2108    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 2140  sub _tree_construction_main ($) { Line 2197  sub _tree_construction_main ($) {
2197      $insert->($el); # /context node/->append_child ($el)      $insert->($el); # /context node/->append_child ($el)
2198    
2199      ## Step 3      ## Step 3
2200      $self->{content_model_flag} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
2201      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2202    
2203      ## Step 4      ## Step 4
# Line 2158  sub _tree_construction_main ($) { Line 2215  sub _tree_construction_main ($) {
2215      }      }
2216    
2217      ## Step 6      ## Step 6
2218      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2219    
2220      ## Step 7      ## Step 7
2221      if ($token->{type} eq 'end tag' and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} eq 'end tag' and $token->{tag_name} eq $start_tag_name) {
2222        ## Ignore the token        ## Ignore the token
2223        } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2224          !!!parse-error (type => 'in CDATA:#'.$token->{type});
2225        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
2226          !!!parse-error (type => 'in RCDATA:#'.$token->{type});
2227      } else {      } else {
2228        !!!parse-error (type => 'in '.$content_model_flag.':#'.$token->{type});        die "$0: $content_model_flag in parse_rcdata";
2229      }      }
2230      !!!next-token;      !!!next-token;
2231    }; # $parse_rcdata    }; # $parse_rcdata
# Line 2175  sub _tree_construction_main ($) { Line 2236  sub _tree_construction_main ($) {
2236      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes});
2237      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
2238    
2239      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
2240      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2241            
2242      my $text = '';      my $text = '';
# Line 2188  sub _tree_construction_main ($) { Line 2249  sub _tree_construction_main ($) {
2249        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
2250      }      }
2251                                
2252      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2253    
2254      if ($token->{type} eq 'end tag' and      if ($token->{type} eq 'end tag' and
2255          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
# Line 2442  sub _tree_construction_main ($) { Line 2503  sub _tree_construction_main ($) {
2503          return;          return;
2504        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
2505          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
2506          $parse_rcdata->('CDATA', $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
2507          return;          return;
2508        } elsif ({        } elsif ({
2509                  base => 1, link => 1, meta => 1,                  base => 1, link => 1,
2510                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
2511          ## 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
2512          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2513          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2514          !!!next-token;          !!!next-token;
2515          return;          return;
2516          } elsif ($token->{tag_name} eq 'meta') {
2517            ## NOTE: This is an "as if in head" code clone, only "-t" differs
2518            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2519            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2520    
2521            unless ($self->{confident}) {
2522              my $charset;
2523              if ($token->{attributes}->{charset}) { ## TODO: And if supported
2524                $charset = $token->{attributes}->{charset}->{value};
2525              }
2526              if ($token->{attributes}->{'http-equiv'}) {
2527                ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
2528                if ($token->{attributes}->{'http-equiv'}->{value}
2529                    =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
2530                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2531                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
2532                  $charset = defined $1 ? $1 : defined $2 ? $2 : $3;
2533                } ## TODO: And if supported
2534              }
2535              ## TODO: Change the encoding
2536            }
2537    
2538            !!!next-token;
2539            return;
2540        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
2541          !!!parse-error (type => 'in body:title');          !!!parse-error (type => 'in body:title');
2542          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
2543          $parse_rcdata->('RCDATA', $insert);          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
2544              if (defined $self->{head_element}) {
2545                $self->{head_element}->append_child ($_[0]);
2546              } else {
2547                $insert->($_[0]);
2548              }
2549            });
2550          return;          return;
2551        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
2552          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body');
# Line 2558  sub _tree_construction_main ($) { Line 2649  sub _tree_construction_main ($) {
2649              if ($i != -1) {              if ($i != -1) {
2650                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'end tag missing:'.
2651                                $self->{open_elements}->[-1]->[1]);                                $self->{open_elements}->[-1]->[1]);
               ## TODO: test  
2652              }              }
2653              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
2654              last LI;              last LI;
# Line 2606  sub _tree_construction_main ($) { Line 2696  sub _tree_construction_main ($) {
2696              if ($i != -1) {              if ($i != -1) {
2697                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'end tag missing:'.
2698                                $self->{open_elements}->[-1]->[1]);                                $self->{open_elements}->[-1]->[1]);
               ## TODO: test  
2699              }              }
2700              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
2701              last LI;              last LI;
# Line 2647  sub _tree_construction_main ($) { Line 2736  sub _tree_construction_main ($) {
2736                        
2737          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2738                        
2739          $self->{content_model_flag} = 'PLAINTEXT';          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
2740                        
2741          !!!next-token;          !!!next-token;
2742          return;          return;
# Line 2750  sub _tree_construction_main ($) { Line 2839  sub _tree_construction_main ($) {
2839          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
2840            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
2841            if ($node->[1] eq 'nobr') {            if ($node->[1] eq 'nobr') {
2842                !!!parse-error (type => 'not closed:nobr');
2843              !!!back-token;              !!!back-token;
2844              $token = {type => 'end tag', tag_name => 'nobr'};              $token = {type => 'end tag', tag_name => 'nobr'};
2845              return;              return;
# Line 2801  sub _tree_construction_main ($) { Line 2891  sub _tree_construction_main ($) {
2891          return;          return;
2892        } elsif ($token->{tag_name} eq 'xmp') {        } elsif ($token->{tag_name} eq 'xmp') {
2893          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
2894          $parse_rcdata->('CDATA', $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
2895          return;          return;
2896        } elsif ($token->{tag_name} eq 'table') {        } elsif ($token->{tag_name} eq 'table') {
2897          ## has a p element in scope          ## has a p element in scope
# Line 2833  sub _tree_construction_main ($) { Line 2923  sub _tree_construction_main ($) {
2923            !!!parse-error (type => 'image');            !!!parse-error (type => 'image');
2924            $token->{tag_name} = 'img';            $token->{tag_name} = 'img';
2925          }          }
2926            
2927            ## NOTE: There is an "as if <br>" code clone.
2928          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
2929                    
2930          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
# Line 2916  sub _tree_construction_main ($) { Line 3007  sub _tree_construction_main ($) {
3007          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $token->{tag_name}, $token->{attributes});
3008                    
3009          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
3010          $self->{content_model_flag} = 'RCDATA';          $self->{content_model} = RCDATA_CONTENT_MODEL;
3011          delete $self->{escape}; # MUST          delete $self->{escape}; # MUST
3012                    
3013          $insert->($el);          $insert->($el);
# Line 2937  sub _tree_construction_main ($) { Line 3028  sub _tree_construction_main ($) {
3028            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
3029          }          }
3030                    
3031          $self->{content_model_flag} = 'PCDATA';          $self->{content_model} = PCDATA_CONTENT_MODEL;
3032                    
3033          if ($token->{type} eq 'end tag' and          if ($token->{type} eq 'end tag' and
3034              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
# Line 2953  sub _tree_construction_main ($) { Line 3044  sub _tree_construction_main ($) {
3044                  noframes => 1,                  noframes => 1,
3045                  noscript => 0, ## TODO: 1 if scripting is enabled                  noscript => 0, ## TODO: 1 if scripting is enabled
3046                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
3047          $parse_rcdata->('CDATA', $insert);          ## NOTE: There are two "as if in body" code clones.
3048            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
3049          return;          return;
3050        } elsif ($token->{tag_name} eq 'select') {        } elsif ($token->{tag_name} eq 'select') {
3051          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
# Line 2991  sub _tree_construction_main ($) { Line 3083  sub _tree_construction_main ($) {
3083              unless ({              unless ({
3084                         dd => 1, dt => 1, li => 1, p => 1, td => 1,                         dd => 1, dt => 1, li => 1, p => 1, td => 1,
3085                         th => 1, tr => 1, body => 1, html => 1,                         th => 1, tr => 1, body => 1, html => 1,
3086                         tbody => 1, tfoot => 1, thead => 1,
3087                      }->{$_->[1]}) {                      }->{$_->[1]}) {
3088                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!parse-error (type => 'not closed:'.$_->[1]);
3089              }              }
# Line 3040  sub _tree_construction_main ($) { Line 3133  sub _tree_construction_main ($) {
3133                   li => ($token->{tag_name} ne 'li'),                   li => ($token->{tag_name} ne 'li'),
3134                   p => ($token->{tag_name} ne 'p'),                   p => ($token->{tag_name} ne 'p'),
3135                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
3136                     tbody => 1, tfoot=> 1, thead => 1,
3137                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
3138                !!!back-token;                !!!back-token;
3139                $token = {type => 'end tag',                $token = {type => 'end tag',
# Line 3057  sub _tree_construction_main ($) { Line 3151  sub _tree_construction_main ($) {
3151          } # INSCOPE          } # INSCOPE
3152                    
3153          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3154            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            if (defined $i) {
3155                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3156              } else {
3157                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3158              }
3159          }          }
3160                    
3161          splice @{$self->{open_elements}}, $i if defined $i;          if (defined $i) {
3162              splice @{$self->{open_elements}}, $i;
3163            } elsif ($token->{tag_name} eq 'p') {
3164              ## As if <p>, then reprocess the current token
3165              my $el;
3166              !!!create-element ($el, 'p');
3167              $insert->($el);
3168            }
3169          $clear_up_to_marker->()          $clear_up_to_marker->()
3170            if {            if {
3171              button => 1, marquee => 1, object => 1,              button => 1, marquee => 1, object => 1,
# Line 3076  sub _tree_construction_main ($) { Line 3181  sub _tree_construction_main ($) {
3181              if ({              if ({
3182                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
3183                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
3184                     tbody => 1, tfoot=> 1, thead => 1,
3185                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
3186                !!!back-token;                !!!back-token;
3187                $token = {type => 'end tag',                $token = {type => 'end tag',
# Line 3114  sub _tree_construction_main ($) { Line 3220  sub _tree_construction_main ($) {
3220              if ({              if ({
3221                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
3222                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
3223                     tbody => 1, tfoot=> 1, thead => 1,
3224                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
3225                !!!back-token;                !!!back-token;
3226                $token = {type => 'end tag',                $token = {type => 'end tag',
# Line 3144  sub _tree_construction_main ($) { Line 3251  sub _tree_construction_main ($) {
3251                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
3252                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
3253          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token->{tag_name});
3254  ## TODO: <http://html5.org/tools/web-apps-tracker?from=883&to=884>          return;
3255          } elsif ($token->{tag_name} eq 'br') {
3256            !!!parse-error (type => 'unmatched end tag:br');
3257    
3258            ## As if <br>
3259            $reconstruct_active_formatting_elements->($insert_to_current);
3260            
3261            my $el;
3262            !!!create-element ($el, 'br');
3263            $insert->($el);
3264            
3265            ## Ignore the token.
3266            !!!next-token;
3267          return;          return;
3268        } elsif ({        } elsif ({
3269                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
3270                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
3271                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
3272                  thead => 1, tr => 1,                  thead => 1, tr => 1,
3273                  area => 1, basefont => 1, bgsound => 1, br => 1,                  area => 1, basefont => 1, bgsound => 1,
3274                  embed => 1, hr => 1, iframe => 1, image => 1,                  embed => 1, hr => 1, iframe => 1, image => 1,
3275                  img => 1, input => 1, isindex => 1, noembed => 1,                  img => 1, input => 1, isindex => 1, noembed => 1,
3276                  noframes => 1, param => 1, select => 1, spacer => 1,                  noframes => 1, param => 1, select => 1, spacer => 1,
# Line 3178  sub _tree_construction_main ($) { Line 3297  sub _tree_construction_main ($) {
3297              if ({              if ({
3298                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
3299                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
3300                     tbody => 1, tfoot=> 1, thead => 1,
3301                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
3302                !!!back-token;                !!!back-token;
3303                $token = {type => 'end tag',                $token = {type => 'end tag',
# Line 3221  sub _tree_construction_main ($) { Line 3341  sub _tree_construction_main ($) {
3341    }; # $in_body    }; # $in_body
3342    
3343    B: {    B: {
3344      if ($phase eq 'main') {      if ($token->{type} eq 'DOCTYPE') {
3345        if ($token->{type} eq 'DOCTYPE') {        !!!parse-error (type => 'DOCTYPE in the middle');
3346          !!!parse-error (type => 'in html:#DOCTYPE');        ## Ignore the token
3347          ## Ignore the token        ## Stay in the phase
3348          ## Stay in the phase        !!!next-token;
3349          !!!next-token;        redo B;
3350          redo B;      } elsif ($token->{type} eq 'end-of-file') {
3351        } elsif ($token->{type} eq 'start tag' and        if ($self->{insertion_mode} eq 'after html body' or
3352                 $token->{tag_name} eq 'html') {            $self->{insertion_mode} eq 'after html frameset') {
3353          ## TODO: unless it is the first start tag token, parse-error          #
3354          my $top_el = $self->{open_elements}->[0]->[0];        } else {
         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') {  
3355          ## Generate implied end tags          ## Generate implied end tags
3356          if ({          if ({
3357               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,
3358                 tbody => 1, tfoot=> 1, thead => 1,
3359              }->{$self->{open_elements}->[-1]->[1]}) {              }->{$self->{open_elements}->[-1]->[1]}) {
3360            !!!back-token;            !!!back-token;
3361            $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};            $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};
# Line 3260  sub _tree_construction_main ($) { Line 3371  sub _tree_construction_main ($) {
3371            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3372          }          }
3373    
         ## Stop parsing  
         last B;  
   
3374          ## ISSUE: There is an issue in the spec.          ## ISSUE: There is an issue in the spec.
3375          }
3376    
3377          ## Stop parsing
3378          last B;
3379        } elsif ($token->{type} eq 'start tag' and
3380                 $token->{tag_name} eq 'html') {
3381          if ($self->{insertion_mode} eq 'after html body') {
3382            ## Turn into the main phase
3383            !!!parse-error (type => 'after html:html');
3384            $self->{insertion_mode} = 'after body';
3385          } elsif ($self->{insertion_mode} eq 'after html frameset') {
3386            ## Turn into the main phase
3387            !!!parse-error (type => 'after html:html');
3388            $self->{insertion_mode} = 'after frameset';
3389          }
3390    
3391    ## ISSUE: "aa<html>" is not a parse error.
3392    ## ISSUE: "<html>" in fragment is not a parse error.
3393          unless ($token->{first_start_tag}) {
3394            !!!parse-error (type => 'not first start tag');
3395          }
3396          my $top_el = $self->{open_elements}->[0]->[0];
3397          for my $attr_name (keys %{$token->{attributes}}) {
3398            unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3399              $top_el->set_attribute_ns
3400                (undef, [undef, $attr_name],
3401                 $token->{attributes}->{$attr_name}->{value});
3402            }
3403          }
3404          !!!next-token;
3405          redo B;
3406        } elsif ($token->{type} eq 'comment') {
3407          my $comment = $self->{document}->create_comment ($token->{data});
3408          if ($self->{insertion_mode} eq 'after html body' or
3409              $self->{insertion_mode} eq 'after html frameset') {
3410            $self->{document}->append_child ($comment);
3411          } elsif ($self->{insertion_mode} eq 'after body') {
3412            $self->{open_elements}->[0]->[0]->append_child ($comment);
3413        } else {        } else {
3414            $self->{open_elements}->[-1]->[0]->append_child ($comment);
3415          }
3416          !!!next-token;
3417          redo B;
3418        } elsif ($self->{insertion_mode} eq 'in head' or
3419                 $self->{insertion_mode} eq 'in head noscript' or
3420                 $self->{insertion_mode} eq 'after head' or
3421                 $self->{insertion_mode} eq 'before head') {
3422          if ($token->{type} eq 'character') {
3423            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3424              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3425              unless (length $token->{data}) {
3426                !!!next-token;
3427                redo B;
3428              }
3429            }
3430    
3431          if ($self->{insertion_mode} eq 'before head') {          if ($self->{insertion_mode} eq 'before head') {
3432            if ($token->{type} eq 'character') {            ## As if <head>
3433              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {            !!!create-element ($self->{head_element}, 'head');
3434                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3435                unless (length $token->{data}) {            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3436                  !!!next-token;  
3437                  redo B;            ## Reprocess in the "in head" insertion mode...
3438                }            pop @{$self->{open_elements}};
3439              }  
3440              ## As if <head>            ## Reprocess in the "after head" insertion mode...
3441              !!!create-element ($self->{head_element}, 'head');          } elsif ($self->{insertion_mode} eq 'in head noscript') {
3442              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            ## As if </noscript>
3443              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            pop @{$self->{open_elements}};
3444              $self->{insertion_mode} = 'in head';            !!!parse-error (type => 'in noscript:#character');
3445              
3446              ## Reprocess in the "in head" insertion mode...
3447              ## As if </head>
3448              pop @{$self->{open_elements}};
3449    
3450              ## Reprocess in the "after head" insertion mode...
3451            } elsif ($self->{insertion_mode} eq 'in head') {
3452              pop @{$self->{open_elements}};
3453    
3454              ## Reprocess in the "after head" insertion mode...
3455            }
3456    
3457                ## "after head" insertion mode
3458                ## As if <body>
3459                !!!insert-element ('body');
3460                $self->{insertion_mode} = 'in body';
3461              ## reprocess              ## reprocess
3462              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;  
3463            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
             my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};  
             !!!create-element ($self->{head_element}, 'head', $attr);  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
3464              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
3465                !!!next-token;                if ($self->{insertion_mode} eq 'before head') {
3466              #} elsif ({                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
3467              #          base => 1, link => 1, meta => 1,                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3468              #          script => 1, style => 1, title => 1,                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
3469              #         }->{$token->{tag_name}}) {                  $self->{insertion_mode} = 'in head';
3470              #  ## reprocess                  !!!next-token;
3471              } else {                  redo B;
3472                ## reprocess                } elsif ($self->{insertion_mode} ne 'after head') {
3473              }                  !!!parse-error (type => 'in head:head'); # or in head noscript
3474              redo B;                  ## Ignore the token
3475            } elsif ($token->{type} eq 'end tag') {                  !!!next-token;
3476              if ({head => 1, body => 1, html => 1}->{$token->{tag_name}}) {                  redo B;
3477                  } else {
3478                    #
3479                  }
3480                } elsif ($self->{insertion_mode} eq 'before head') {
3481                ## As if <head>                ## As if <head>
3482                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
3483                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3484                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3485    
3486                $self->{insertion_mode} = 'in head';                $self->{insertion_mode} = 'in head';
3487                ## reprocess                ## Reprocess in the "in head" insertion mode...
               redo B;  
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token ## ISSUE: An issue in the spec.  
               !!!next-token;  
               redo B;  
3488              }              }
3489            } else {  
3490              die "$0: $token->{type}: Unknown type";              if ($token->{tag_name} eq 'base') {
3491            }                if ($self->{insertion_mode} eq 'in head noscript') {
3492          } elsif ($self->{insertion_mode} eq 'in head' or                  ## As if </noscript>
3493                   $self->{insertion_mode} eq 'in head noscript' or                  pop @{$self->{open_elements}};
3494                   $self->{insertion_mode} eq 'after head') {                  !!!parse-error (type => 'in noscript:base');
3495            if ($token->{type} eq 'character') {                
3496              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                  $self->{insertion_mode} = 'in head';
3497                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                  ## Reprocess in the "in head" insertion mode...
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
3498                }                }
3499              }  
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({base => ($self->{insertion_mode} eq 'in head' or  
                           $self->{insertion_mode} eq 'after head'),  
                  link => 1, meta => 1}->{$token->{tag_name}}) {  
3500                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3501                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} eq 'after head') {
3502                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
# Line 3354  sub _tree_construction_main ($) { Line 3508  sub _tree_construction_main ($) {
3508                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} eq 'after head';
3509                !!!next-token;                !!!next-token;
3510                redo B;                redo B;
3511              } elsif ($token->{tag_name} eq 'title' and              } elsif ($token->{tag_name} eq 'link') {
                      $self->{insertion_mode} eq 'in head') {  
3512                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3513                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} eq 'after head') {
3514                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3515                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3516                }                }
3517                $parse_rcdata->('RCDATA', $insert_to_current);                !!!insert-element ($token->{tag_name}, $token->{attributes});
3518                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3519                  pop @{$self->{open_elements}}
3520                      if $self->{insertion_mode} eq 'after head';
3521                  !!!next-token;
3522                  redo B;
3523                } elsif ($token->{tag_name} eq 'meta') {
3524                  ## NOTE: There is a "as if in head" code clone.
3525                  if ($self->{insertion_mode} eq 'after head') {
3526                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3527                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3528                  }
3529                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3530                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3531    
3532                  unless ($self->{confident}) {
3533                    my $charset;
3534                    if ($token->{attributes}->{charset}) { ## TODO: And if supported
3535                      $charset = $token->{attributes}->{charset}->{value};
3536                    }
3537                    if ($token->{attributes}->{'http-equiv'}) {
3538                      ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
3539                      if ($token->{attributes}->{'http-equiv'}->{value}
3540                          =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
3541                              [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
3542                              ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
3543                        $charset = defined $1 ? $1 : defined $2 ? $2 : $3;
3544                      } ## TODO: And if supported
3545                    }
3546                    ## TODO: Change the encoding
3547                  }
3548    
3549                  ## TODO: Extracting |charset| from |meta|.
3550                  pop @{$self->{open_elements}}
3551                      if $self->{insertion_mode} eq 'after head';
3552                  !!!next-token;
3553                  redo B;
3554                } elsif ($token->{tag_name} eq 'title') {
3555                  if ($self->{insertion_mode} eq 'in head noscript') {
3556                    ## As if </noscript>
3557                    pop @{$self->{open_elements}};
3558                    !!!parse-error (type => 'in noscript:title');
3559                  
3560                    $self->{insertion_mode} = 'in head';
3561                    ## Reprocess in the "in head" insertion mode...
3562                  } elsif ($self->{insertion_mode} eq 'after head') {
3563                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3564                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3565                  }
3566    
3567                  ## NOTE: There is a "as if in head" code clone.
3568                  my $parent = defined $self->{head_element} ? $self->{head_element}
3569                      : $self->{open_elements}->[-1]->[0];
3570                  $parse_rcdata->(RCDATA_CONTENT_MODEL,
3571                                  sub { $parent->append_child ($_[0]) });
3572                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
3573                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} eq 'after head';
3574                redo B;                redo B;
# Line 3373  sub _tree_construction_main ($) { Line 3580  sub _tree_construction_main ($) {
3580                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3581                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3582                }                }
3583                $parse_rcdata->('CDATA', $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
3584                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
3585                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} eq 'after head';
3586                redo B;                redo B;
# Line 3385  sub _tree_construction_main ($) { Line 3592  sub _tree_construction_main ($) {
3592                  !!!next-token;                  !!!next-token;
3593                  redo B;                  redo B;
3594                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} eq 'in head noscript') {
3595                  !!!parse-error (type => 'noscript in noscript');                  !!!parse-error (type => 'in noscript:noscript');
3596                  ## Ignore the token                  ## Ignore the token
3597                    !!!next-token;
3598                  redo B;                  redo B;
3599                } else {                } else {
3600                  #                  #
3601                }                }
3602              } elsif ($token->{tag_name} eq 'head' and              } elsif ($token->{tag_name} eq 'script') {
3603                       $self->{insertion_mode} ne 'after head') {                if ($self->{insertion_mode} eq 'in head noscript') {
3604                !!!parse-error (type => 'in head:head'); # or in head noscript                  ## As if </noscript>
3605                ## Ignore the token                  pop @{$self->{open_elements}};
3606                !!!next-token;                  !!!parse-error (type => 'in noscript:script');
3607                redo B;                
3608              } elsif ($self->{insertion_mode} ne 'in head noscript' and                  $self->{insertion_mode} = 'in head';
3609                       $token->{tag_name} eq 'script') {                  ## Reprocess in the "in head" insertion mode...
3610                if ($self->{insertion_mode} eq 'after head') {                } elsif ($self->{insertion_mode} eq 'after head') {
3611                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3612                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3613                }                }
3614    
3615                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3616                $script_start_tag->($insert_to_current);                $script_start_tag->($insert_to_current);
3617                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
3618                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} eq 'after head';
3619                redo B;                redo B;
3620              } elsif ($self->{insertion_mode} eq 'after head' and              } elsif ($token->{tag_name} eq 'body' or
                      $token->{tag_name} eq 'body') {  
               !!!insert-element ('body', $token->{attributes});  
               $self->{insertion_mode} = 'in body';  
               !!!next-token;  
               redo B;  
             } elsif ($self->{insertion_mode} eq 'after head' and  
3621                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
3622                !!!insert-element ('frameset', $token->{attributes});                if ($self->{insertion_mode} eq 'in head noscript') {
3623                $self->{insertion_mode} = 'in frameset';                  ## As if </noscript>
3624                    pop @{$self->{open_elements}};
3625                    !!!parse-error (type => 'in noscript:'.$token->{tag_name});
3626                    
3627                    ## Reprocess in the "in head" insertion mode...
3628                    ## As if </head>
3629                    pop @{$self->{open_elements}};
3630                    
3631                    ## Reprocess in the "after head" insertion mode...
3632                  } elsif ($self->{insertion_mode} eq 'in head') {
3633                    pop @{$self->{open_elements}};
3634                    
3635                    ## Reprocess in the "after head" insertion mode...
3636                  }
3637    
3638                  ## "after head" insertion mode
3639                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3640                  $self->{insertion_mode} = 'in '.$token->{tag_name};
3641                !!!next-token;                !!!next-token;
3642                redo B;                redo B;
3643              } else {              } else {
3644                #                #
3645              }              }
3646            } elsif ($token->{type} eq 'end tag') {  
3647              if ($self->{insertion_mode} eq 'in head' and              if ($self->{insertion_mode} eq 'in head noscript') {
3648                  $token->{tag_name} eq 'head') {                ## As if </noscript>
3649                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3650                $self->{insertion_mode} = 'after head';                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3651                !!!next-token;                
3652                redo B;                ## Reprocess in the "in head" insertion mode...
3653              } elsif ($self->{insertion_mode} eq 'in head noscript' and                ## As if </head>
                 $token->{tag_name} eq 'noscript') {  
3654                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3655                $self->{insertion_mode} = 'in head';  
3656                !!!next-token;                ## Reprocess in the "after head" insertion mode...
3657                redo B;              } elsif ($self->{insertion_mode} eq 'in head') {
3658              } elsif ($self->{insertion_mode} eq 'in head' and                ## As if </head>
3659                       ($token->{tag_name} eq 'body' or                pop @{$self->{open_elements}};
3660                        $token->{tag_name} eq 'html')) {  
3661                #                ## Reprocess in the "after head" insertion mode...
             } elsif ($self->{insertion_mode} ne 'after head') {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
3662              }              }
           } else {  
             #  
           }  
3663    
3664            ## As if </head> or </noscript> or <body>              ## "after head" insertion mode
3665            if ($self->{insertion_mode} eq 'in head') {              ## As if <body>
             pop @{$self->{open_elements}};  
             $self->{insertion_mode} = 'after head';  
           } elsif ($self->{insertion_mode} eq 'in head noscript') {  
             pop @{$self->{open_elements}};  
             !!!parse-error (type => 'in noscript:'.(defined $token->{tag_name} ? ($token->{type} eq 'end tag' ? '/' : '') . $token->{tag_name} : '#' . $token->{type}));  
             $self->{insertion_mode} = 'in head';  
           } else { # 'after head'  
3666              !!!insert-element ('body');              !!!insert-element ('body');
3667              $self->{insertion_mode} = 'in body';              $self->{insertion_mode} = 'in body';
3668            }              ## reprocess
           ## reprocess  
           redo B;  
   
           ## ISSUE: An issue in the spec.  
         } elsif ($self->{insertion_mode} eq 'in body') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: There is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } else {  
             $in_body->($insert_to_current);  
3669              redo B;              redo B;
3670            }            } elsif ($token->{type} eq 'end tag') {
3671          } elsif ($self->{insertion_mode} eq 'in table') {              if ($token->{tag_name} eq 'head') {
3672            if ($token->{type} eq 'character') {                if ($self->{insertion_mode} eq 'before head') {
3673              ## NOTE: There are "character in table" code clones.                  ## As if <head>
3674              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                  !!!create-element ($self->{head_element}, 'head');
3675                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3676                                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3677                unless (length $token->{data}) {  
3678                    ## Reprocess in the "in head" insertion mode...
3679                    pop @{$self->{open_elements}};
3680                    $self->{insertion_mode} = 'after head';
3681                    !!!next-token;
3682                    redo B;
3683                  } elsif ($self->{insertion_mode} eq 'in head noscript') {
3684                    ## As if </noscript>
3685                    pop @{$self->{open_elements}};
3686                    !!!parse-error (type => 'in noscript:script');
3687                    
3688                    ## Reprocess in the "in head" insertion mode...
3689                    pop @{$self->{open_elements}};
3690                    $self->{insertion_mode} = 'after head';
3691                    !!!next-token;
3692                    redo B;
3693                  } elsif ($self->{insertion_mode} eq 'in head') {
3694                    pop @{$self->{open_elements}};
3695                    $self->{insertion_mode} = 'after head';
3696                  !!!next-token;                  !!!next-token;
3697                  redo B;                  redo B;
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
3698                } else {                } else {
3699                  $foster_parent_element->insert_before                  #
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
3700                }                }
3701              } else {              } elsif ($token->{tag_name} eq 'noscript') {
3702                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                if ($self->{insertion_mode} eq 'in head noscript') {
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1,  
                  colgroup => 1,  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$token->{tag_name}}) {  
               ## Clear back to table context  
               while ($self->{open_elements}->[-1]->[1] ne 'table' and  
                      $self->{open_elements}->[-1]->[1] ne 'html') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
3703                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3704                    $self->{insertion_mode} = 'in head';
3705                    !!!next-token;
3706                    redo B;
3707                  } elsif ($self->{insertion_mode} eq 'before head') {
3708                    !!!parse-error (type => 'unmatched end tag:noscript');
3709                    ## Ignore the token ## ISSUE: An issue in the spec.
3710                    !!!next-token;
3711                    redo B;
3712                  } else {
3713                    #
3714                }                }
   
               push @$active_formatting_elements, ['#marker', '']  
                 if $token->{tag_name} eq 'caption';  
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = {  
                                  caption => 'in caption',  
                                  colgroup => 'in column group',  
                                  tbody => 'in table body',  
                                  tfoot => 'in table body',  
                                  thead => 'in table body',  
                                 }->{$token->{tag_name}};  
               !!!next-token;  
               redo B;  
3715              } elsif ({              } elsif ({
3716                        col => 1,                        body => 1, html => 1,
                       td => 1, th => 1, tr => 1,  
3717                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3718                ## Clear back to table context                if ($self->{insertion_mode} eq 'before head') {
3719                while ($self->{open_elements}->[-1]->[1] ne 'table' and                  ## As if <head>
3720                       $self->{open_elements}->[-1]->[1] ne 'html') {                  !!!create-element ($self->{head_element}, 'head');
3721                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3722                  pop @{$self->{open_elements}};                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
               }  
   
               !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');  
               $self->{insertion_mode} = $token->{tag_name} eq 'col'  
                 ? 'in column group' : 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: There are code clones for this "table in table"  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
3723    
3724                ## As if </table>                  $self->{insertion_mode} = 'in head';
3725                ## have a table element in table scope                  ## Reprocess in the "in head" insertion mode...
3726                my $i;                } elsif ($self->{insertion_mode} eq 'in head noscript') {
3727                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3728                  my $node = $self->{open_elements}->[$_];                  ## Ignore the token
                 if ($node->[1] eq 'table') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:table');  
                 ## Ignore tokens </table><table>  
3729                  !!!next-token;                  !!!next-token;
3730                  redo B;                  redo B;
3731                }                }
3732                                
3733                ## generate implied end tags                #
3734                if ({              } elsif ({
3735                     dd => 1, dt => 1, li => 1, p => 1,                        p => 1, br => 1,
3736                     td => 1, th => 1, tr => 1,                       }->{$token->{tag_name}}) {
3737                    }->{$self->{open_elements}->[-1]->[1]}) {                if ($self->{insertion_mode} eq 'before head') {
3738                  !!!back-token; # <table>                  ## As if <head>
3739                  $token = {type => 'end tag', tag_name => 'table'};                  !!!create-element ($self->{head_element}, 'head');
3740                  !!!back-token;                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3741                  $token = {type => 'end tag',                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
3742    
3743                if ($self->{open_elements}->[-1]->[1] ne 'table') {                  $self->{insertion_mode} = 'in head';
3744                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  ## Reprocess in the "in head" insertion mode...
3745                }                }
3746    
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
3747                #                #
3748              }              } else {
3749            } elsif ($token->{type} eq 'end tag') {                if ($self->{insertion_mode} ne 'after head') {
             if ($token->{tag_name} eq 'table') {  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
3750                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3751                  ## Ignore the token                  ## Ignore the token
3752                  !!!next-token;                  !!!next-token;
3753                  redo B;                  redo B;
3754                  } else {
3755                    #
3756                }                }
3757                              }
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
3758    
3759                splice @{$self->{open_elements}}, $i;              if ($self->{insertion_mode} eq 'in head noscript') {
3760                  ## As if </noscript>
3761                  pop @{$self->{open_elements}};
3762                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3763                  
3764                  ## Reprocess in the "in head" insertion mode...
3765                  ## As if </head>
3766                  pop @{$self->{open_elements}};
3767    
3768                $self->_reset_insertion_mode;                ## Reprocess in the "after head" insertion mode...
3769                } elsif ($self->{insertion_mode} eq 'in head') {
3770                  ## As if </head>
3771                  pop @{$self->{open_elements}};
3772    
3773                !!!next-token;                ## Reprocess in the "after head" insertion mode...
3774                redo B;              } elsif ($self->{insertion_mode} eq 'before head') {
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
3775                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3776                ## Ignore the token                ## Ignore the token ## ISSUE: An issue in the spec.
3777                !!!next-token;                !!!next-token;
3778                redo B;                redo B;
             } else {  
               #  
3779              }              }
3780    
3781                ## "after head" insertion mode
3782                ## As if <body>
3783                !!!insert-element ('body');
3784                $self->{insertion_mode} = 'in body';
3785                ## reprocess
3786                redo B;
3787            } else {            } else {
3788              #              die "$0: $token->{type}: Unknown token type";
3789            }            }
3790    
3791            !!!parse-error (type => 'in table:'.$token->{tag_name});            ## ISSUE: An issue in the spec.
3792            $in_body->($insert_to_foster);      } elsif ($self->{insertion_mode} eq 'in body' or
3793            redo B;               $self->{insertion_mode} eq 'in cell' or
3794          } elsif ($self->{insertion_mode} eq 'in caption') {               $self->{insertion_mode} eq 'in caption') {
3795            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
3796              ## NOTE: This is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
3797              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
3798                            
3799              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3800    
3801              !!!next-token;              !!!next-token;
3802              redo B;              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;  
3803            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
3804              if ({              if ({
3805                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
3806                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3807                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3808                !!!parse-error (type => 'not closed:caption');                if ($self->{insertion_mode} eq 'in cell') {
3809                    ## have an element in table scope
3810                ## As if </caption>                  my $tn;
3811                ## have a table element in table scope                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3812                my $i;                    my $node = $self->{open_elements}->[$_];
3813                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3814                  my $node = $self->{open_elements}->[$_];                      $tn = $node->[1];
3815                  if ($node->[1] eq 'caption') {                      last INSCOPE;
3816                    $i = $_;                    } elsif ({
3817                    last INSCOPE;                              table => 1, html => 1,
3818                  } elsif ({                             }->{$node->[1]}) {
3819                            table => 1, html => 1,                      last INSCOPE;
3820                           }->{$node->[1]}) {                    }
3821                    last INSCOPE;                  } # INSCOPE
3822                  }                    unless (defined $tn) {
3823                } # INSCOPE                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3824                unless (defined $i) {                      ## Ignore the token
3825                  !!!parse-error (type => 'unmatched end tag:caption');                      !!!next-token;
3826                  ## Ignore the token                      redo B;
3827                  !!!next-token;                    }
3828                  redo B;                  
3829                }                  ## Close the cell
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
3830                  !!!back-token; # <?>                  !!!back-token; # <?>
3831                  $token = {type => 'end tag', tag_name => 'caption'};                  $token = {type => 'end tag', tag_name => $tn};
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
3832                  redo B;                  redo B;
3833                }                } elsif ($self->{insertion_mode} eq 'in caption') {
3834                    !!!parse-error (type => 'not closed:caption');
3835                    
3836                    ## As if </caption>
3837                    ## have a table element in table scope
3838                    my $i;
3839                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3840                      my $node = $self->{open_elements}->[$_];
3841                      if ($node->[1] eq 'caption') {
3842                        $i = $_;
3843                        last INSCOPE;
3844                      } elsif ({
3845                                table => 1, html => 1,
3846                               }->{$node->[1]}) {
3847                        last INSCOPE;
3848                      }
3849                    } # INSCOPE
3850                      unless (defined $i) {
3851                        !!!parse-error (type => 'unmatched end tag:caption');
3852                        ## Ignore the token
3853                        !!!next-token;
3854                        redo B;
3855                      }
3856                    
3857                    ## generate implied end tags
3858                    if ({
3859                         dd => 1, dt => 1, li => 1, p => 1,
3860                         td => 1, th => 1, tr => 1,
3861                         tbody => 1, tfoot=> 1, thead => 1,
3862                        }->{$self->{open_elements}->[-1]->[1]}) {
3863                      !!!back-token; # <?>
3864                      $token = {type => 'end tag', tag_name => 'caption'};
3865                      !!!back-token;
3866                      $token = {type => 'end tag',
3867                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3868                      redo B;
3869                    }
3870    
3871                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3872                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3873                    }
3874                    
3875                    splice @{$self->{open_elements}}, $i;
3876                    
3877                    $clear_up_to_marker->();
3878                    
3879                    $self->{insertion_mode} = 'in table';
3880                    
3881                    ## reprocess
3882                    redo B;
3883                  } else {
3884                    #
3885                }                }
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               ## reprocess  
               redo B;  
3886              } else {              } else {
3887                #                #
3888              }              }
3889            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
3890              if ($token->{tag_name} eq 'caption') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3891                ## have a table element in table scope                if ($self->{insertion_mode} eq 'in cell') {
3892                    ## have an element in table scope
3893                    my $i;
3894                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3895                      my $node = $self->{open_elements}->[$_];
3896                      if ($node->[1] eq $token->{tag_name}) {
3897                        $i = $_;
3898                        last INSCOPE;
3899                      } elsif ({
3900                                table => 1, html => 1,
3901                               }->{$node->[1]}) {
3902                        last INSCOPE;
3903                      }
3904                    } # INSCOPE
3905                      unless (defined $i) {
3906                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3907                        ## Ignore the token
3908                        !!!next-token;
3909                        redo B;
3910                      }
3911                    
3912                    ## generate implied end tags
3913                    if ({
3914                         dd => 1, dt => 1, li => 1, p => 1,
3915                         td => ($token->{tag_name} eq 'th'),
3916                         th => ($token->{tag_name} eq 'td'),
3917                         tr => 1,
3918                         tbody => 1, tfoot=> 1, thead => 1,
3919                        }->{$self->{open_elements}->[-1]->[1]}) {
3920                      !!!back-token;
3921                      $token = {type => 'end tag',
3922                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3923                      redo B;
3924                    }
3925                    
3926                    if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3927                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3928                    }
3929                    
3930                    splice @{$self->{open_elements}}, $i;
3931                    
3932                    $clear_up_to_marker->();
3933                    
3934                    $self->{insertion_mode} = 'in row';
3935                    
3936                    !!!next-token;
3937                    redo B;
3938                  } elsif ($self->{insertion_mode} eq 'in caption') {
3939                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3940                    ## Ignore the token
3941                    !!!next-token;
3942                    redo B;
3943                  } else {
3944                    #
3945                  }
3946                } elsif ($token->{tag_name} eq 'caption') {
3947                  if ($self->{insertion_mode} eq 'in caption') {
3948                    ## have a table element in table scope
3949                    my $i;
3950                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3951                      my $node = $self->{open_elements}->[$_];
3952                      if ($node->[1] eq $token->{tag_name}) {
3953                        $i = $_;
3954                        last INSCOPE;
3955                      } elsif ({
3956                                table => 1, html => 1,
3957                               }->{$node->[1]}) {
3958                        last INSCOPE;
3959                      }
3960                    } # INSCOPE
3961                      unless (defined $i) {
3962                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3963                        ## Ignore the token
3964                        !!!next-token;
3965                        redo B;
3966                      }
3967                    
3968                    ## generate implied end tags
3969                    if ({
3970                         dd => 1, dt => 1, li => 1, p => 1,
3971                         td => 1, th => 1, tr => 1,
3972                         tbody => 1, tfoot=> 1, thead => 1,
3973                        }->{$self->{open_elements}->[-1]->[1]}) {
3974                      !!!back-token;
3975                      $token = {type => 'end tag',
3976                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3977                      redo B;
3978                    }
3979                    
3980                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3981                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3982                    }
3983                    
3984                    splice @{$self->{open_elements}}, $i;
3985                    
3986                    $clear_up_to_marker->();
3987                    
3988                    $self->{insertion_mode} = 'in table';
3989                    
3990                    !!!next-token;
3991                    redo B;
3992                  } elsif ($self->{insertion_mode} eq 'in cell') {
3993                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3994                    ## Ignore the token
3995                    !!!next-token;
3996                    redo B;
3997                  } else {
3998                    #
3999                  }
4000                } elsif ({
4001                          table => 1, tbody => 1, tfoot => 1,
4002                          thead => 1, tr => 1,
4003                         }->{$token->{tag_name}} and
4004                         $self->{insertion_mode} eq 'in cell') {
4005                  ## have an element in table scope
4006                my $i;                my $i;
4007                  my $tn;
4008                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4009                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4010                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4011                    $i = $_;                    $i = $_;
4012                    last INSCOPE;                    last INSCOPE;
4013                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
4014                      $tn = $node->[1];
4015                      ## NOTE: There is exactly one |td| or |th| element
4016                      ## in scope in the stack of open elements by definition.
4017                  } elsif ({                  } elsif ({
4018                            table => 1, html => 1,                            table => 1, html => 1,
4019                           }->{$node->[1]}) {                           }->{$node->[1]}) {
# Line 3798  sub _tree_construction_main ($) { Line 4026  sub _tree_construction_main ($) {
4026                  !!!next-token;                  !!!next-token;
4027                  redo B;                  redo B;
4028                }                }
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
4029    
4030                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                ## Close the cell
4031                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!back-token; # </?>
4032                }                $token = {type => 'end tag', tag_name => $tn};
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               !!!next-token;  
4033                redo B;                redo B;
4034              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table' and
4035                         $self->{insertion_mode} eq 'in caption') {
4036                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption');
4037    
4038                ## As if </caption>                ## As if </caption>
# Line 3850  sub _tree_construction_main ($) { Line 4060  sub _tree_construction_main ($) {
4060                if ({                if ({
4061                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
4062                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
4063                       tbody => 1, tfoot=> 1, thead => 1,
4064                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
4065                  !!!back-token; # </table>                  !!!back-token; # </table>
4066                  $token = {type => 'end tag', tag_name => 'caption'};                  $token = {type => 'end tag', tag_name => 'caption'};
# Line 3872  sub _tree_construction_main ($) { Line 4083  sub _tree_construction_main ($) {
4083                ## reprocess                ## reprocess
4084                redo B;                redo B;
4085              } elsif ({              } elsif ({
4086                        body => 1, col => 1, colgroup => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
                       html => 1, tbody => 1, td => 1, tfoot => 1,  
                       th => 1, thead => 1, tr => 1,  
4087                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4088                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} eq 'in cell' or
4089                ## Ignore the token                    $self->{insertion_mode} eq 'in caption') {
4090                redo B;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
             } else {  
               #  
             }  
           } else {  
             #  
           }  
                 
           $in_body->($insert_to_current);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in column group') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'col') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'colgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html') {  
                 !!!parse-error (type => 'unmatched end tag:colgroup');  
4091                  ## Ignore the token                  ## Ignore the token
4092                  !!!next-token;                  !!!next-token;
4093                  redo B;                  redo B;
4094                } else {                } else {
4095                  pop @{$self->{open_elements}}; # colgroup                  #
                 $self->{insertion_mode} = 'in table';  
                 !!!next-token;  
                 redo B;              
4096                }                }
4097              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
4098                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
4099                          thead => 1, tr => 1,
4100                         }->{$token->{tag_name}} and
4101                         $self->{insertion_mode} eq 'in caption') {
4102                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4103                ## Ignore the token                ## Ignore the token
4104                !!!next-token;                !!!next-token;
4105                redo B;                redo B;
4106              } else {              } else {
4107                #                #
4108              }              }
4109            } else {            } else {
4110              #              #
4111            }            }
4112              
4113            ## As if </colgroup>            $in_body->($insert_to_current);
4114            if ($self->{open_elements}->[-1]->[1] eq 'html') {            redo B;
4115              !!!parse-error (type => 'unmatched end tag:colgroup');      } elsif ($self->{insertion_mode} eq 'in row' or
4116              ## Ignore the token               $self->{insertion_mode} eq 'in table body' or
4117              !!!next-token;               $self->{insertion_mode} eq 'in table') {
             redo B;  
           } else {  
             pop @{$self->{open_elements}}; # colgroup  
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table body') {  
4118            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
4119              ## NOTE: This is a "character in table" code clone.              ## NOTE: There are "character in table" code clones.
4120              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4121                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4122                                
# Line 3969  sub _tree_construction_main ($) { Line 4133  sub _tree_construction_main ($) {
4133              ## into the current node" while characters might not be              ## into the current node" while characters might not be
4134              ## result in a new Text node.              ## result in a new Text node.
4135              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
4136                
4137              if ({              if ({
4138                   table => 1, tbody => 1, tfoot => 1,                   table => 1, tbody => 1, tfoot => 1,
4139                   thead => 1, tr => 1,                   thead => 1, tr => 1,
# Line 4009  sub _tree_construction_main ($) { Line 4173  sub _tree_construction_main ($) {
4173                            
4174              !!!next-token;              !!!next-token;
4175              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;  
4176            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
4177              if ({              if ({
4178                   tr => 1,                   tr => ($self->{insertion_mode} ne 'in row'),
4179                   th => 1, td => 1,                   th => 1, td => 1,
4180                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4181                unless ($token->{tag_name} eq 'tr') {                if ($self->{insertion_mode} eq 'in table') {
4182                  !!!parse-error (type => 'missing start tag:tr');                  ## Clear back to table context
4183                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
4184                           $self->{open_elements}->[-1]->[1] ne 'html') {
4185                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4186                      pop @{$self->{open_elements}};
4187                    }
4188                    
4189                    !!!insert-element ('tbody');
4190                    $self->{insertion_mode} = 'in table body';
4191                    ## reprocess in the "in table body" insertion mode...
4192                }                }
4193    
4194                ## Clear back to table body context                if ($self->{insertion_mode} eq 'in table body') {
4195                    unless ($token->{tag_name} eq 'tr') {
4196                      !!!parse-error (type => 'missing start tag:tr');
4197                    }
4198                    
4199                    ## Clear back to table body context
4200                    while (not {
4201                      tbody => 1, tfoot => 1, thead => 1, html => 1,
4202                    }->{$self->{open_elements}->[-1]->[1]}) {
4203                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4204                      pop @{$self->{open_elements}};
4205                    }
4206                    
4207                    $self->{insertion_mode} = 'in row';
4208                    if ($token->{tag_name} eq 'tr') {
4209                      !!!insert-element ($token->{tag_name}, $token->{attributes});
4210                      !!!next-token;
4211                      redo B;
4212                    } else {
4213                      !!!insert-element ('tr');
4214                      ## reprocess in the "in row" insertion mode
4215                    }
4216                  }
4217    
4218                  ## Clear back to table row context
4219                while (not {                while (not {
4220                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tr => 1, html => 1,
4221                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4222                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4223                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4224                }                }
4225                                
4226                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes});
4227                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = 'in cell';
4228                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
4229                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
4230                } else {                
4231                  !!!insert-element ('tr');                !!!next-token;
                 ## reprocess  
               }  
4232                redo B;                redo B;
4233              } elsif ({              } elsif ({
4234                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
4235                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
4236                          tr => 1, # $self->{insertion_mode} eq 'in row'
4237                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4238                ## have an element in table scope                if ($self->{insertion_mode} eq 'in row') {
4239                my $i;                  ## As if </tr>
4240                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
4241                  my $node = $self->{open_elements}->[$_];                  my $i;
4242                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4243                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
4244                      }->{$node->[1]}) {                    if ($node->[1] eq 'tr') {
4245                    $i = $_;                      $i = $_;
4246                    last INSCOPE;                      last INSCOPE;
4247                  } elsif ({                    } elsif ({
4248                            table => 1, html => 1,                              table => 1, html => 1,
4249                           }->{$node->[1]}) {                             }->{$node->[1]}) {
4250                    last INSCOPE;                      last INSCOPE;
4251                      }
4252                    } # INSCOPE
4253                    unless (defined $i) {
4254                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
4255                      ## Ignore the token
4256                      !!!next-token;
4257                      redo B;
4258                    }
4259                    
4260                    ## Clear back to table row context
4261                    while (not {
4262                      tr => 1, html => 1,
4263                    }->{$self->{open_elements}->[-1]->[1]}) {
4264                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4265                      pop @{$self->{open_elements}};
4266                    }
4267                    
4268                    pop @{$self->{open_elements}}; # tr
4269                    $self->{insertion_mode} = 'in table body';
4270                    if ($token->{tag_name} eq 'tr') {
4271                      ## reprocess
4272                      redo B;
4273                    } else {
4274                      ## reprocess in the "in table body" insertion mode...
4275                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
4276                }                }
4277    
4278                ## Clear back to table body context                if ($self->{insertion_mode} eq 'in table body') {
4279                while (not {                  ## have an element in table scope
4280                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
4281                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4282                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
4283                      if ({
4284                           tbody => 1, thead => 1, tfoot => 1,
4285                          }->{$node->[1]}) {
4286                        $i = $_;
4287                        last INSCOPE;
4288                      } elsif ({
4289                                table => 1, html => 1,
4290                               }->{$node->[1]}) {
4291                        last INSCOPE;
4292                      }
4293                    } # INSCOPE
4294                    unless (defined $i) {
4295                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4296                      ## Ignore the token
4297                      !!!next-token;
4298                      redo B;
4299                    }
4300    
4301                    ## Clear back to table body context
4302                    while (not {
4303                      tbody => 1, tfoot => 1, thead => 1, html => 1,
4304                    }->{$self->{open_elements}->[-1]->[1]}) {
4305                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4306                      pop @{$self->{open_elements}};
4307                    }
4308                    
4309                    ## As if <{current node}>
4310                    ## have an element in table scope
4311                    ## true by definition
4312                    
4313                    ## Clear back to table body context
4314                    ## nop by definition
4315                    
4316                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4317                    $self->{insertion_mode} = 'in table';
4318                    ## reprocess in "in table" insertion mode...
4319                }                }
4320    
4321                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
4322                ## have an element in table scope                  ## Clear back to table context
4323                ## true by definition                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
4324                           $self->{open_elements}->[-1]->[1] ne 'html') {
4325                ## Clear back to table body context                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4326                ## nop by definition                    pop @{$self->{open_elements}};
4327                    }
4328                pop @{$self->{open_elements}};                  
4329                $self->{insertion_mode} = 'in table';                  !!!insert-element ('colgroup');
4330                ## reprocess                  $self->{insertion_mode} = 'in column group';
4331                redo B;                  ## reprocess
4332                    redo B;
4333                  } elsif ({
4334                            caption => 1,
4335                            colgroup => 1,
4336                            tbody => 1, tfoot => 1, thead => 1,
4337                           }->{$token->{tag_name}}) {
4338                    ## Clear back to table context
4339                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
4340                           $self->{open_elements}->[-1]->[1] ne 'html') {
4341                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4342                      pop @{$self->{open_elements}};
4343                    }
4344                    
4345                    push @$active_formatting_elements, ['#marker', '']
4346                        if $token->{tag_name} eq 'caption';
4347                    
4348                    !!!insert-element ($token->{tag_name}, $token->{attributes});
4349                    $self->{insertion_mode} = {
4350                                               caption => 'in caption',
4351                                               colgroup => 'in column group',
4352                                               tbody => 'in table body',
4353                                               tfoot => 'in table body',
4354                                               thead => 'in table body',
4355                                              }->{$token->{tag_name}};
4356                    !!!next-token;
4357                    redo B;
4358                  } else {
4359                    die "$0: in table: <>: $token->{tag_name}";
4360                  }
4361              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4362                ## NOTE: This is a code clone of "table in table"                ## NOTE: There are code clones for this "table in table"
4363                !!!parse-error (type => 'not closed:table');                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4364    
4365                ## As if </table>                ## As if </table>
4366                ## have a table element in table scope                ## have a table element in table scope
# Line 4115  sub _tree_construction_main ($) { Line 4387  sub _tree_construction_main ($) {
4387                if ({                if ({
4388                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
4389                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
4390                       tbody => 1, tfoot=> 1, thead => 1,
4391                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
4392                  !!!back-token; # <table>                  !!!back-token; # <table>
4393                  $token = {type => 'end tag', tag_name => 'table'};                  $token = {type => 'end tag', tag_name => 'table'};
# Line 4130  sub _tree_construction_main ($) { Line 4403  sub _tree_construction_main ($) {
4403    
4404                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4405    
4406                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4407    
4408                ## reprocess                ## reprocess
4409                redo B;                redo B;
# Line 4138  sub _tree_construction_main ($) { Line 4411  sub _tree_construction_main ($) {
4411                #                #
4412              }              }
4413            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
4414              if ({              if ($token->{tag_name} eq 'tr' and
4415                   tbody => 1, tfoot => 1, thead => 1,                  $self->{insertion_mode} eq 'in row') {
                 }->{$token->{tag_name}}) {  
4416                ## have an element in table scope                ## have an element in table scope
4417                my $i;                my $i;
4418                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 4161  sub _tree_construction_main ($) { Line 4433  sub _tree_construction_main ($) {
4433                  redo B;                  redo B;
4434                }                }
4435    
4436                ## Clear back to table body context                ## Clear back to table row context
4437                while (not {                while (not {
4438                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tr => 1, html => 1,
4439                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4440                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4441                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4442                }                }
4443    
4444                pop @{$self->{open_elements}};                pop @{$self->{open_elements}}; # tr
4445                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = 'in table body';
4446                !!!next-token;                !!!next-token;
4447                redo B;                redo B;
4448              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4449                ## have an element in table scope                if ($self->{insertion_mode} eq 'in row') {
4450                my $i;                  ## As if </tr>
4451                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
4452                  my $node = $self->{open_elements}->[$_];                  my $i;
4453                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4454                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
4455                      }->{$node->[1]}) {                    if ($node->[1] eq 'tr') {
4456                    $i = $_;                      $i = $_;
4457                    last INSCOPE;                      last INSCOPE;
4458                  } elsif ({                    } elsif ({
4459                            table => 1, html => 1,                              table => 1, html => 1,
4460                           }->{$node->[1]}) {                             }->{$node->[1]}) {
4461                    last INSCOPE;                      last INSCOPE;
4462                      }
4463                    } # INSCOPE
4464                    unless (defined $i) {
4465                      !!!parse-error (type => 'unmatched end tag:'.$token->{type});
4466                      ## Ignore the token
4467                      !!!next-token;
4468                      redo B;
4469                  }                  }
4470                } # INSCOPE                  
4471                unless (defined $i) {                  ## Clear back to table row context
4472                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  while (not {
4473                  ## Ignore the token                    tr => 1, html => 1,
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, td => 1, th => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           ## As if in table  
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in row') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
4474                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4475                # MUST                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4476                my $foster_parent_element;                    pop @{$self->{open_elements}};
4477                my $next_sibling;                  }
4478                my $prev_sibling;                  
4479                OE: for (reverse 0..$#{$self->{open_elements}}) {                  pop @{$self->{open_elements}}; # tr
4480                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  $self->{insertion_mode} = 'in table body';
4481                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                  ## reprocess in the "in table body" insertion mode...
4482                    if (defined $parent and $parent->node_type == 1) {                }
4483                      $foster_parent_element = $parent;  
4484                      $next_sibling = $self->{open_elements}->[$_]->[0];                if ($self->{insertion_mode} eq 'in table body') {
4485                      $prev_sibling = $next_sibling->previous_sibling;                  ## have an element in table scope
4486                    } else {                  my $i;
4487                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4488                      $prev_sibling = $foster_parent_element->last_child;                    my $node = $self->{open_elements}->[$_];
4489                      if ({
4490                           tbody => 1, thead => 1, tfoot => 1,
4491                          }->{$node->[1]}) {
4492                        $i = $_;
4493                        last INSCOPE;
4494                      } elsif ({
4495                                table => 1, html => 1,
4496                               }->{$node->[1]}) {
4497                        last INSCOPE;
4498                    }                    }
4499                    last OE;                  } # INSCOPE
4500                    unless (defined $i) {
4501                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4502                      ## Ignore the token
4503                      !!!next-token;
4504                      redo B;
4505                  }                  }
4506                } # OE                  
4507                $foster_parent_element = $self->{open_elements}->[0]->[0] and                  ## Clear back to table body context
4508                $prev_sibling = $foster_parent_element->last_child                  while (not {
4509                  unless defined $foster_parent_element;                    tbody => 1, tfoot => 1, thead => 1, html => 1,
4510                if (defined $prev_sibling and                  }->{$self->{open_elements}->[-1]->[1]}) {
4511                    $prev_sibling->node_type == 3) {                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4512                  $prev_sibling->manakai_append_text ($token->{data});                    pop @{$self->{open_elements}};
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'th' or  
                 $token->{tag_name} eq 'td') {  
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = 'in cell';  
   
               push @$active_formatting_elements, ['#marker', ''];  
                 
               !!!next-token;  
               redo B;  
             } elsif ({  
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
4513                  }                  }
4514                } # INSCOPE                  
4515                unless (defined $i) {                  ## As if <{current node}>
4516                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  ## have an element in table scope
4517                  ## Ignore the token                  ## true by definition
4518                  !!!next-token;                  
4519                  redo B;                  ## Clear back to table body context
4520                }                  ## nop by definition
4521                    
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4522                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4523                    $self->{insertion_mode} = 'in table';
4524                    ## reprocess in the "in table" insertion mode...
4525                }                }
4526    
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: This is a code clone of "table in table"  
               !!!parse-error (type => 'not closed:table');  
   
               ## As if </table>  
4527                ## have a table element in table scope                ## have a table element in table scope
4528                my $i;                my $i;
4529                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4530                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4531                  if ($node->[1] eq 'table') {                  if ($node->[1] eq $token->{tag_name}) {
4532                    $i = $_;                    $i = $_;
4533                    last INSCOPE;                    last INSCOPE;
4534                  } elsif ({                  } elsif ({
# Line 4373  sub _tree_construction_main ($) { Line 4538  sub _tree_construction_main ($) {
4538                  }                  }
4539                } # INSCOPE                } # INSCOPE
4540                unless (defined $i) {                unless (defined $i) {
4541                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4542                  ## Ignore tokens </table><table>                  ## Ignore the token
4543                  !!!next-token;                  !!!next-token;
4544                  redo B;                  redo B;
4545                }                }
4546                  
4547                ## generate implied end tags                ## generate implied end tags
4548                if ({                if ({
4549                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
4550                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
4551                       tbody => 1, tfoot=> 1, thead => 1,
4552                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
4553                  !!!back-token;                  !!!back-token;
4554                  $token = {type => 'end tag',                  $token = {type => 'end tag',
4555                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4556                  redo B;                  redo B;
4557                }                }
4558                  
4559                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
4560                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4561                }                }
4562                    
4563                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4564                  
4565                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4566                  
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'tr') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
4567                !!!next-token;                !!!next-token;
4568                redo B;                redo B;
             } elsif ($token->{tag_name} eq 'table') {  
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{type});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
4569              } elsif ({              } elsif ({
4570                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
4571                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
4572                ## have an element in table scope                       ($self->{insertion_mode} eq 'in row' or
4573                my $i;                        $self->{insertion_mode} eq 'in table body')) {
4574                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                if ($self->{insertion_mode} eq 'in row') {
4575                  my $node = $self->{open_elements}->[$_];                  ## have an element in table scope
4576                  if ($node->[1] eq $token->{tag_name}) {                  my $i;
4577                    $i = $_;                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4578                    last INSCOPE;                    my $node = $self->{open_elements}->[$_];
4579                  } elsif ({                    if ($node->[1] eq $token->{tag_name}) {
4580                            table => 1, html => 1,                      $i = $_;
4581                           }->{$node->[1]}) {                      last INSCOPE;
4582                    last INSCOPE;                    } elsif ({
4583                                table => 1, html => 1,
4584                               }->{$node->[1]}) {
4585                        last INSCOPE;
4586                      }
4587                    } # INSCOPE
4588                      unless (defined $i) {
4589                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4590                        ## Ignore the token
4591                        !!!next-token;
4592                        redo B;
4593                      }
4594                    
4595                    ## As if </tr>
4596                    ## have an element in table scope
4597                    my $i;
4598                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4599                      my $node = $self->{open_elements}->[$_];
4600                      if ($node->[1] eq 'tr') {
4601                        $i = $_;
4602                        last INSCOPE;
4603                      } elsif ({
4604                                table => 1, html => 1,
4605                               }->{$node->[1]}) {
4606                        last INSCOPE;
4607                      }
4608                    } # INSCOPE
4609                      unless (defined $i) {
4610                        !!!parse-error (type => 'unmatched end tag:tr');
4611                        ## Ignore the token
4612                        !!!next-token;
4613                        redo B;
4614                      }
4615                    
4616                    ## Clear back to table row context
4617                    while (not {
4618                      tr => 1, html => 1,
4619                    }->{$self->{open_elements}->[-1]->[1]}) {
4620                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4621                      pop @{$self->{open_elements}};
4622                  }                  }
4623                } # INSCOPE                  
4624                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
4625                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = 'in table body';
4626                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
4627                }                }
4628    
               ## As if </tr>  
4629                ## have an element in table scope                ## have an element in table scope
4630                my $i;                my $i;
4631                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4632                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4633                  if ($node->[1] eq 'tr') {                  if ($node->[1] eq $token->{tag_name}) {
4634                    $i = $_;                    $i = $_;
4635                    last INSCOPE;                    last INSCOPE;
4636                  } elsif ({                  } elsif ({
# Line 4511  sub _tree_construction_main ($) { Line 4640  sub _tree_construction_main ($) {
4640                  }                  }
4641                } # INSCOPE                } # INSCOPE
4642                unless (defined $i) {                unless (defined $i) {
4643                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4644                  ## Ignore the token                  ## Ignore the token
4645                  !!!next-token;                  !!!next-token;
4646                  redo B;                  redo B;
4647                }                }
4648    
4649                ## Clear back to table row context                ## Clear back to table body context
4650                while (not {                while (not {
4651                  tr => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
4652                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4653                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4654                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4655                }                }
4656    
4657                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
4658                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = 'in table';
4659                ## reprocess                !!!next-token;
4660                redo B;                redo B;
4661              } elsif ({              } elsif ({
4662                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
4663                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
4664                          tr => 1, # $self->{insertion_mode} eq 'in row'
4665                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} eq 'in table'
4666                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4667                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4668                ## Ignore the token                ## Ignore the token
# Line 4541  sub _tree_construction_main ($) { Line 4672  sub _tree_construction_main ($) {
4672                #                #
4673              }              }
4674            } else {            } else {
4675              #              die "$0: $token->{type}: Unknown token type";
4676            }            }
4677    
           ## As if in table  
4678            !!!parse-error (type => 'in table:'.$token->{tag_name});            !!!parse-error (type => 'in table:'.$token->{tag_name});
4679            $in_body->($insert_to_foster);            $in_body->($insert_to_foster);
4680            redo B;            redo B;
4681          } elsif ($self->{insertion_mode} eq 'in cell') {      } elsif ($self->{insertion_mode} eq 'in column group') {
4682            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
4683              ## NOTE: This is a code clone of "character in body".              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4684              $reconstruct_active_formatting_elements->($insert_to_current);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4685                              unless (length $token->{data}) {
             $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  
4686                  !!!next-token;                  !!!next-token;
4687                  redo B;                  redo B;
4688                }                }
4689                }
4690                ## Close the cell              
4691                !!!back-token; # <?>              #
4692                $token = {type => 'end tag', tag_name => $tn};            } elsif ($token->{type} eq 'start tag') {
4693                if ($token->{tag_name} eq 'col') {
4694                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4695                  pop @{$self->{open_elements}};
4696                  !!!next-token;
4697                redo B;                redo B;
4698              } else {              } else {
4699                #                #
4700              }              }
4701            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
4702              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
4703                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] eq 'html') {
4704                my $i;                  !!!parse-error (type => 'unmatched end tag:colgroup');
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
4705                  ## Ignore the token                  ## Ignore the token
4706                  !!!next-token;                  !!!next-token;
4707                  redo B;                  redo B;
4708                  } else {
4709                    pop @{$self->{open_elements}}; # colgroup
4710                    $self->{insertion_mode} = 'in table';
4711                    !!!next-token;
4712                    redo B;            
4713                }                }
4714                              } elsif ($token->{tag_name} eq 'col') {
4715                ## generate implied end tags                !!!parse-error (type => 'unmatched end tag:col');
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => ($token->{tag_name} eq 'th'),  
                    th => ($token->{tag_name} eq 'td'),  
                    tr => 1,  
                   }->{$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});  
4716                ## Ignore the token                ## Ignore the token
4717                !!!next-token;                !!!next-token;
4718                redo B;                redo B;
             } elsif ({  
                       table => 1, tbody => 1, tfoot => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   ## NOTE: There is exactly one |td| or |th| element  
                   ## in scope in the stack of open elements by definition.  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
4719              } else {              } else {
4720                #                #
4721              }              }
4722            } else {            } else {
4723              #              #
4724            }            }
4725              
4726            $in_body->($insert_to_current);            ## As if </colgroup>
4727            redo B;            if ($self->{open_elements}->[-1]->[1] eq 'html') {
4728          } elsif ($self->{insertion_mode} eq 'in select') {              !!!parse-error (type => 'unmatched end tag:colgroup');
4729            if ($token->{type} eq 'character') {              ## Ignore the token
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
4730              !!!next-token;              !!!next-token;
4731              redo B;              redo B;
4732            } elsif ($token->{type} eq 'comment') {            } else {
4733              my $comment = $self->{document}->create_comment ($token->{data});              pop @{$self->{open_elements}}; # colgroup
4734              $self->{open_elements}->[-1]->[0]->append_child ($comment);              $self->{insertion_mode} = 'in table';
4735                ## reprocess
4736                redo B;
4737              }
4738        } elsif ($self->{insertion_mode} eq 'in select') {
4739              if ($token->{type} eq 'character') {
4740                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4741              !!!next-token;              !!!next-token;
4742              redo B;              redo B;
4743            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
# Line 4872  sub _tree_construction_main ($) { Line 4909  sub _tree_construction_main ($) {
4909            ## Ignore the token            ## Ignore the token
4910            !!!next-token;            !!!next-token;
4911            redo B;            redo B;
4912          } elsif ($self->{insertion_mode} eq 'after body') {      } elsif ($self->{insertion_mode} eq 'after body' or
4913            if ($token->{type} eq 'character') {               $self->{insertion_mode} eq 'after html body') {
4914              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        if ($token->{type} eq 'character') {
4915                ## As if in body          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4916                $reconstruct_active_formatting_elements->($insert_to_current);            my $data = $1;
4917              ## As if in body
4918              $reconstruct_active_formatting_elements->($insert_to_current);
4919                                
4920                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4921              
4922              unless (length $token->{data}) {
4923                !!!next-token;
4924                redo B;
4925              }
4926            }
4927            
4928            if ($self->{insertion_mode} eq 'after html body') {
4929              !!!parse-error (type => 'after html:#character');
4930    
4931                unless (length $token->{data}) {            ## Reprocess in the "main" phase, "after body" insertion mode...
4932                  !!!next-token;          }
4933                  redo B;          
4934                }          ## "after body" insertion mode
4935              }          !!!parse-error (type => 'after body:#character');
4936                
4937              #          $self->{insertion_mode} = 'in body';
4938              !!!parse-error (type => 'after body:#'.$token->{type});          ## reprocess
4939            } elsif ($token->{type} eq 'comment') {          redo B;
4940              my $comment = $self->{document}->create_comment ($token->{data});        } elsif ($token->{type} eq 'start tag') {
4941              $self->{open_elements}->[0]->[0]->append_child ($comment);          if ($self->{insertion_mode} eq 'after html body') {
4942              !!!parse-error (type => 'after html:'.$token->{tag_name});
4943              
4944              ## Reprocess in the "main" phase, "after body" insertion mode...
4945            }
4946    
4947            ## "after body" insertion mode
4948            !!!parse-error (type => 'after body:'.$token->{tag_name});
4949    
4950            $self->{insertion_mode} = 'in body';
4951            ## reprocess
4952            redo B;
4953          } elsif ($token->{type} eq 'end tag') {
4954            if ($self->{insertion_mode} eq 'after html body') {
4955              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4956              
4957              $self->{insertion_mode} = 'after body';
4958              ## Reprocess in the "main" phase, "after body" insertion mode...
4959            }
4960    
4961            ## "after body" insertion mode
4962            if ($token->{tag_name} eq 'html') {
4963              if (defined $self->{inner_html_node}) {
4964                !!!parse-error (type => 'unmatched end tag:html');
4965                ## Ignore the token
4966              !!!next-token;              !!!next-token;
4967              redo B;              redo B;
           } elsif ($token->{type} eq 'start tag') {  
             !!!parse-error (type => 'after body:'.$token->{tag_name});  
             #  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'html') {  
               if (defined $self->{inner_html_node}) {  
                 !!!parse-error (type => 'unmatched end tag:html');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               } else {  
                 $phase = 'trailing end';  
                 !!!next-token;  
                 redo B;  
               }  
             } else {  
               !!!parse-error (type => 'after body:/'.$token->{tag_name});  
             }  
4968            } else {            } else {
4969              !!!parse-error (type => 'after body:#'.$token->{type});              $self->{insertion_mode} = 'after html body';
4970                !!!next-token;
4971                redo B;
4972            }            }
4973            } else {
4974              !!!parse-error (type => 'after body:/'.$token->{tag_name});
4975    
4976            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = 'in body';
4977            ## reprocess            ## reprocess
4978            redo B;            redo B;
4979          } elsif ($self->{insertion_mode} eq 'in frameset') {          }
4980            if ($token->{type} eq 'character') {        } else {
4981              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          die "$0: $token->{type}: Unknown token type";
4982                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});        }
4983        } elsif ($self->{insertion_mode} eq 'in frameset' or
4984                unless (length $token->{data}) {               $self->{insertion_mode} eq 'after frameset' or
4985                  !!!next-token;               $self->{insertion_mode} eq 'after html frameset') {
4986                  redo B;        if ($token->{type} eq 'character') {
4987                }          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4988              }            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4989              
4990              #            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);  
4991              !!!next-token;              !!!next-token;
4992              redo B;              redo B;
4993            } elsif ($token->{type} eq 'start tag') {            }
4994              if ($token->{tag_name} eq 'frameset') {          }
4995                !!!insert-element ($token->{tag_name}, $token->{attributes});          
4996                !!!next-token;          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
4997                redo B;            if ($self->{insertion_mode} eq 'in frameset') {
4998              } elsif ($token->{tag_name} eq 'frame') {              !!!parse-error (type => 'in frameset:#character');
4999                !!!insert-element ($token->{tag_name}, $token->{attributes});            } elsif ($self->{insertion_mode} eq 'after frameset') {
5000                pop @{$self->{open_elements}};              !!!parse-error (type => 'after frameset:#character');
5001                !!!next-token;            } else { # "after html frameset"
5002                redo B;              !!!parse-error (type => 'after html:#character');
5003              } elsif ($token->{tag_name} eq 'noframes') {  
5004                $in_body->($insert_to_current);              $self->{insertion_mode} = 'after frameset';
5005                redo B;              ## Reprocess in the "main" phase, "after frameset"...
5006              } else {              !!!parse-error (type => 'after frameset:#character');
               #  
             }  
           } 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 {  
             #  
5007            }            }
5008                        
5009            if (defined $token->{tag_name}) {            ## Ignore the token.
5010              if (length $token->{data}) {
5011                ## reprocess the rest of characters
5012              } else {
5013                !!!next-token;
5014              }
5015              redo B;
5016            }
5017            
5018            die qq[$0: Character "$token->{data}"];
5019          } elsif ($token->{type} eq 'start tag') {
5020            if ($self->{insertion_mode} eq 'after html frameset') {
5021              !!!parse-error (type => 'after html:'.$token->{tag_name});
5022    
5023              $self->{insertion_mode} = 'after frameset';
5024              ## Process in the "main" phase, "after frameset" insertion mode...
5025            }
5026    
5027            if ($token->{tag_name} eq 'frameset' and
5028                $self->{insertion_mode} eq 'in frameset') {
5029              !!!insert-element ($token->{tag_name}, $token->{attributes});
5030              !!!next-token;
5031              redo B;
5032            } elsif ($token->{tag_name} eq 'frame' and
5033                     $self->{insertion_mode} eq 'in frameset') {
5034              !!!insert-element ($token->{tag_name}, $token->{attributes});
5035              pop @{$self->{open_elements}};
5036              !!!next-token;
5037              redo B;
5038            } elsif ($token->{tag_name} eq 'noframes') {
5039              ## NOTE: As if in body.
5040              $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
5041              redo B;
5042            } else {
5043              if ($self->{insertion_mode} eq 'in frameset') {
5044              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.$token->{tag_name});
5045            } else {            } else {
5046              !!!parse-error (type => 'in frameset:#'.$token->{type});              !!!parse-error (type => 'after frameset:'.$token->{tag_name});
5047            }            }
5048            ## Ignore the token            ## Ignore the token
5049            !!!next-token;            !!!next-token;
5050            redo B;            redo B;
5051          } elsif ($self->{insertion_mode} eq 'after frameset') {          }
5052            if ($token->{type} eq 'character') {        } elsif ($token->{type} eq 'end tag') {
5053              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($self->{insertion_mode} eq 'after html frameset') {
5054                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            !!!parse-error (type => 'after html:/'.$token->{tag_name});
5055    
5056                unless (length $token->{data}) {            $self->{insertion_mode} = 'after frameset';
5057                  !!!next-token;            ## Process in the "main" phase, "after frameset" insertion mode...
5058                  redo B;          }
               }  
             }  
5059    
5060              #          if ($token->{tag_name} eq 'frameset' and
5061            } elsif ($token->{type} eq 'comment') {              $self->{insertion_mode} eq 'in frameset') {
5062              my $comment = $self->{document}->create_comment ($token->{data});            if ($self->{open_elements}->[-1]->[1] eq 'html' and
5063              $self->{open_elements}->[-1]->[0]->append_child ($comment);                @{$self->{open_elements}} == 1) {
5064                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5065                ## Ignore the token
5066              !!!next-token;              !!!next-token;
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'noframes') {  
               $in_body->($insert_to_current);  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'html') {  
               $phase = 'trailing end';  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
5067            } else {            } else {
5068              #              pop @{$self->{open_elements}};
5069                !!!next-token;
5070            }            }
5071              
5072            if (defined $token->{tag_name}) {            if (not defined $self->{inner_html_node} and
5073              !!!parse-error (type => 'after frameset:'.$token->{tag_name});                $self->{open_elements}->[-1]->[1] ne 'frameset') {
5074            } else {              $self->{insertion_mode} = 'after frameset';
             !!!parse-error (type => 'after frameset:#'.$token->{type});  
5075            }            }
5076            ## Ignore the token            redo B;
5077            } elsif ($token->{tag_name} eq 'html' and
5078                     $self->{insertion_mode} eq 'after frameset') {
5079              $self->{insertion_mode} = 'after html frameset';
5080            !!!next-token;            !!!next-token;
5081            redo B;            redo B;
   
           ## ISSUE: An issue in spec there  
5082          } else {          } else {
5083            die "$0: $self->{insertion_mode}: Unknown insertion mode";            if ($self->{insertion_mode} eq 'in frameset') {
5084          }              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
5085        }            } else {
5086      } elsif ($phase eq 'trailing end') {              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
       ## states in the main stage is preserved yet # MUST  
         
       if ($token->{type} eq 'DOCTYPE') {  
         !!!parse-error (type => 'after html:#DOCTYPE');  
         ## Ignore the token  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'comment') {  
         my $comment = $self->{document}->create_comment ($token->{data});  
         $self->{document}->append_child ($comment);  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'character') {  
         if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
           my $data = $1;  
           ## As if in the main phase.  
           ## NOTE: The insertion mode in the main phase  
           ## just before the phase has been changed to the trailing  
           ## end phase is either "after body" or "after frameset".  
           $reconstruct_active_formatting_elements->($insert_to_current)  
             if $phase eq 'main';  
             
           $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);  
             
           unless (length $token->{data}) {  
             !!!next-token;  
             redo B;  
5087            }            }
5088              ## Ignore the token
5089              !!!next-token;
5090              redo B;
5091          }          }
   
         !!!parse-error (type => 'after html:#character');  
         $phase = 'main';  
         ## reprocess  
         redo B;  
       } elsif ($token->{type} eq 'start tag' or  
                $token->{type} eq 'end tag') {  
         !!!parse-error (type => 'after html:'.$token->{tag_name});  
         $phase = 'main';  
         ## reprocess  
         redo B;  
       } elsif ($token->{type} eq 'end-of-file') {  
         ## Stop parsing  
         last B;  
5092        } else {        } else {
5093          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
5094        }        }
5095    
5096          ## ISSUE: An issue in spec here
5097        } else {
5098          die "$0: $self->{insertion_mode}: Unknown insertion mode";
5099      }      }
5100    } # B    } # B
5101    
5102      ## NOTE: The "trailing end" phase in HTML5 is split into
5103      ## two insertion modes: "after html body" and "after html frameset".
5104      ## NOTE: States in the main stage is preserved while
5105      ## the parser stays in the trailing end phase. # MUST
5106    
5107    ## Stop parsing # MUST    ## Stop parsing # MUST
5108        
5109    ## TODO: script stuffs    ## TODO: script stuffs
# Line 5167  sub set_inner_html ($$$) { Line 5188  sub set_inner_html ($$$) {
5188    
5189      ## Step 2      ## Step 2
5190      my $node_ln = $node->local_name;      my $node_ln = $node->local_name;
5191      $p->{content_model_flag} = {      $p->{content_model} = {
5192        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
5193        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
5194        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
5195        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
5196        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
5197        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
5198        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
5199        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
5200        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
5201        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
5202      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
5203         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
5204            unless defined $p->{content_model};
5205            ## ISSUE: What is "the name of the element"? local name?
5206    
5207      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
5208    
# Line 5279  sub get_inner_html ($$$) { Line 5302  sub get_inner_html ($$$) {
5302            
5303      my $nt = $child->node_type;      my $nt = $child->node_type;
5304      if ($nt == 1) { # Element      if ($nt == 1) { # Element
5305        my $tag_name = lc $child->tag_name; ## ISSUE: Definition of "lowercase"        my $tag_name = $child->tag_name; ## TODO: manakai_tag_name
5306        $s .= '<' . $tag_name;        $s .= '<' . $tag_name;
5307          ## NOTE: Non-HTML case:
5308        ## ISSUE: Non-html elements        ## <http://permalink.gmane.org/gmane.org.w3c.whatwg.discuss/11191>
5309    
5310        my @attrs = @{$child->attributes}; # sort order MUST be stable        my @attrs = @{$child->attributes}; # sort order MUST be stable
5311        for my $attr (@attrs) { # order is implementation dependent        for my $attr (@attrs) { # order is implementation dependent
5312          my $attr_name = lc $attr->name; ## ISSUE: Definition of "lowercase"          my $attr_name = $attr->name; ## TODO: manakai_name
5313          $s .= ' ' . $attr_name . '="';          $s .= ' ' . $attr_name . '="';
5314          my $attr_value = $attr->value;          my $attr_value = $attr->value;
5315          ## escape          ## escape
# Line 5310  sub get_inner_html ($$$) { Line 5333  sub get_inner_html ($$$) {
5333        if (not $in_cdata and {        if (not $in_cdata and {
5334          style => 1, script => 1, xmp => 1, iframe => 1,          style => 1, script => 1, xmp => 1, iframe => 1,
5335          noembed => 1, noframes => 1, noscript => 1,          noembed => 1, noframes => 1, noscript => 1,
5336            plaintext => 1,
5337        }->{$tag_name}) {        }->{$tag_name}) {
5338          unshift @node, 'cdata-out';          unshift @node, 'cdata-out';
5339          $in_cdata = 1;          $in_cdata = 1;

Legend:
Removed from v.1.25  
changed lines
  Added in v.1.51

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24