/[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.50 by wakaba, Sat Jul 21 10:59:40 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    
2106    my $phase = 'main';    my $previous_insertion_mode;
2107    
2108    my $active_formatting_elements = [];    my $active_formatting_elements = [];
2109    
# Line 2140  sub _tree_construction_main ($) { Line 2199  sub _tree_construction_main ($) {
2199      $insert->($el); # /context node/->append_child ($el)      $insert->($el); # /context node/->append_child ($el)
2200    
2201      ## Step 3      ## Step 3
2202      $self->{content_model_flag} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
2203      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2204    
2205      ## Step 4      ## Step 4
# Line 2158  sub _tree_construction_main ($) { Line 2217  sub _tree_construction_main ($) {
2217      }      }
2218    
2219      ## Step 6      ## Step 6
2220      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2221    
2222      ## Step 7      ## Step 7
2223      if ($token->{type} eq 'end tag' and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} eq 'end tag' and $token->{tag_name} eq $start_tag_name) {
2224        ## Ignore the token        ## Ignore the token
2225        } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2226          !!!parse-error (type => 'in CDATA:#'.$token->{type});
2227        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
2228          !!!parse-error (type => 'in RCDATA:#'.$token->{type});
2229      } else {      } else {
2230        !!!parse-error (type => 'in '.$content_model_flag.':#'.$token->{type});        die "$0: $content_model_flag in parse_rcdata";
2231      }      }
2232      !!!next-token;      !!!next-token;
2233    }; # $parse_rcdata    }; # $parse_rcdata
# Line 2175  sub _tree_construction_main ($) { Line 2238  sub _tree_construction_main ($) {
2238      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes});
2239      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
2240    
2241      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
2242      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2243            
2244      my $text = '';      my $text = '';
# Line 2188  sub _tree_construction_main ($) { Line 2251  sub _tree_construction_main ($) {
2251        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
2252      }      }
2253                                
2254      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2255    
2256      if ($token->{type} eq 'end tag' and      if ($token->{type} eq 'end tag' and
2257          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
# Line 2442  sub _tree_construction_main ($) { Line 2505  sub _tree_construction_main ($) {
2505          return;          return;
2506        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
2507          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
2508          $parse_rcdata->('CDATA', $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
2509          return;          return;
2510        } elsif ({        } elsif ({
2511                  base => 1, link => 1, meta => 1,                  base => 1, link => 1,
2512                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
2513          ## NOTE: This is an "as if in head" code clone, only "-t" differs          ## NOTE: This is an "as if in head" code clone, only "-t" differs
2514          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2515          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2516          !!!next-token;          !!!next-token;
2517          return;          return;
2518          } elsif ($token->{tag_name} eq 'meta') {
2519            ## NOTE: This is an "as if in head" code clone, only "-t" differs
2520            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2521            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2522    
2523            unless ($self->{confident}) {
2524              my $charset;
2525              if ($token->{attributes}->{charset}) { ## TODO: And if supported
2526                $charset = $token->{attributes}->{charset}->{value};
2527              }
2528              if ($token->{attributes}->{'http-equiv'}) {
2529                ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
2530                if ($token->{attributes}->{'http-equiv'}->{value}
2531                    =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
2532                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2533                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
2534                  $charset = defined $1 ? $1 : defined $2 ? $2 : $3;
2535                } ## TODO: And if supported
2536              }
2537              ## TODO: Change the encoding
2538            }
2539    
2540            !!!next-token;
2541            return;
2542        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
2543          !!!parse-error (type => 'in body:title');          !!!parse-error (type => 'in body:title');
2544          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
2545          $parse_rcdata->('RCDATA', $insert);          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
2546              if (defined $self->{head_element}) {
2547                $self->{head_element}->append_child ($_[0]);
2548              } else {
2549                $insert->($_[0]);
2550              }
2551            });
2552          return;          return;
2553        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
2554          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body');
# Line 2558  sub _tree_construction_main ($) { Line 2651  sub _tree_construction_main ($) {
2651              if ($i != -1) {              if ($i != -1) {
2652                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'end tag missing:'.
2653                                $self->{open_elements}->[-1]->[1]);                                $self->{open_elements}->[-1]->[1]);
               ## TODO: test  
2654              }              }
2655              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
2656              last LI;              last LI;
# Line 2606  sub _tree_construction_main ($) { Line 2698  sub _tree_construction_main ($) {
2698              if ($i != -1) {              if ($i != -1) {
2699                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'end tag missing:'.
2700                                $self->{open_elements}->[-1]->[1]);                                $self->{open_elements}->[-1]->[1]);
               ## TODO: test  
2701              }              }
2702              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
2703              last LI;              last LI;
# Line 2647  sub _tree_construction_main ($) { Line 2738  sub _tree_construction_main ($) {
2738                        
2739          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
2740                        
2741          $self->{content_model_flag} = 'PLAINTEXT';          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
2742                        
2743          !!!next-token;          !!!next-token;
2744          return;          return;
# Line 2750  sub _tree_construction_main ($) { Line 2841  sub _tree_construction_main ($) {
2841          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
2842            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
2843            if ($node->[1] eq 'nobr') {            if ($node->[1] eq 'nobr') {
2844                !!!parse-error (type => 'not closed:nobr');
2845              !!!back-token;              !!!back-token;
2846              $token = {type => 'end tag', tag_name => 'nobr'};              $token = {type => 'end tag', tag_name => 'nobr'};
2847              return;              return;
# Line 2801  sub _tree_construction_main ($) { Line 2893  sub _tree_construction_main ($) {
2893          return;          return;
2894        } elsif ($token->{tag_name} eq 'xmp') {        } elsif ($token->{tag_name} eq 'xmp') {
2895          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
2896          $parse_rcdata->('CDATA', $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
2897          return;          return;
2898        } elsif ($token->{tag_name} eq 'table') {        } elsif ($token->{tag_name} eq 'table') {
2899          ## has a p element in scope          ## has a p element in scope
# Line 2833  sub _tree_construction_main ($) { Line 2925  sub _tree_construction_main ($) {
2925            !!!parse-error (type => 'image');            !!!parse-error (type => 'image');
2926            $token->{tag_name} = 'img';            $token->{tag_name} = 'img';
2927          }          }
2928            
2929            ## NOTE: There is an "as if <br>" code clone.
2930          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
2931                    
2932          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
# Line 2916  sub _tree_construction_main ($) { Line 3009  sub _tree_construction_main ($) {
3009          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $token->{tag_name}, $token->{attributes});
3010                    
3011          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
3012          $self->{content_model_flag} = 'RCDATA';          $self->{content_model} = RCDATA_CONTENT_MODEL;
3013          delete $self->{escape}; # MUST          delete $self->{escape}; # MUST
3014                    
3015          $insert->($el);          $insert->($el);
# Line 2937  sub _tree_construction_main ($) { Line 3030  sub _tree_construction_main ($) {
3030            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
3031          }          }
3032                    
3033          $self->{content_model_flag} = 'PCDATA';          $self->{content_model} = PCDATA_CONTENT_MODEL;
3034                    
3035          if ($token->{type} eq 'end tag' and          if ($token->{type} eq 'end tag' and
3036              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
# Line 2953  sub _tree_construction_main ($) { Line 3046  sub _tree_construction_main ($) {
3046                  noframes => 1,                  noframes => 1,
3047                  noscript => 0, ## TODO: 1 if scripting is enabled                  noscript => 0, ## TODO: 1 if scripting is enabled
3048                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
3049          $parse_rcdata->('CDATA', $insert);          ## NOTE: There are two "as if in body" code clones.
3050            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
3051          return;          return;
3052        } elsif ($token->{tag_name} eq 'select') {        } elsif ($token->{tag_name} eq 'select') {
3053          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
# Line 2991  sub _tree_construction_main ($) { Line 3085  sub _tree_construction_main ($) {
3085              unless ({              unless ({
3086                         dd => 1, dt => 1, li => 1, p => 1, td => 1,                         dd => 1, dt => 1, li => 1, p => 1, td => 1,
3087                         th => 1, tr => 1, body => 1, html => 1,                         th => 1, tr => 1, body => 1, html => 1,
3088                         tbody => 1, tfoot => 1, thead => 1,
3089                      }->{$_->[1]}) {                      }->{$_->[1]}) {
3090                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!parse-error (type => 'not closed:'.$_->[1]);
3091              }              }
# Line 3040  sub _tree_construction_main ($) { Line 3135  sub _tree_construction_main ($) {
3135                   li => ($token->{tag_name} ne 'li'),                   li => ($token->{tag_name} ne 'li'),
3136                   p => ($token->{tag_name} ne 'p'),                   p => ($token->{tag_name} ne 'p'),
3137                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
3138                     tbody => 1, tfoot=> 1, thead => 1,
3139                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
3140                !!!back-token;                !!!back-token;
3141                $token = {type => 'end tag',                $token = {type => 'end tag',
# Line 3057  sub _tree_construction_main ($) { Line 3153  sub _tree_construction_main ($) {
3153          } # INSCOPE          } # INSCOPE
3154                    
3155          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3156            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            if (defined $i) {
3157                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3158              } else {
3159                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3160              }
3161          }          }
3162                    
3163          splice @{$self->{open_elements}}, $i if defined $i;          if (defined $i) {
3164              splice @{$self->{open_elements}}, $i;
3165            } elsif ($token->{tag_name} eq 'p') {
3166              ## As if <p>, then reprocess the current token
3167              my $el;
3168              !!!create-element ($el, 'p');
3169              $insert->($el);
3170            }
3171          $clear_up_to_marker->()          $clear_up_to_marker->()
3172            if {            if {
3173              button => 1, marquee => 1, object => 1,              button => 1, marquee => 1, object => 1,
# Line 3076  sub _tree_construction_main ($) { Line 3183  sub _tree_construction_main ($) {
3183              if ({              if ({
3184                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
3185                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
3186                     tbody => 1, tfoot=> 1, thead => 1,
3187                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
3188                !!!back-token;                !!!back-token;
3189                $token = {type => 'end tag',                $token = {type => 'end tag',
# Line 3114  sub _tree_construction_main ($) { Line 3222  sub _tree_construction_main ($) {
3222              if ({              if ({
3223                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
3224                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
3225                     tbody => 1, tfoot=> 1, thead => 1,
3226                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
3227                !!!back-token;                !!!back-token;
3228                $token = {type => 'end tag',                $token = {type => 'end tag',
# Line 3144  sub _tree_construction_main ($) { Line 3253  sub _tree_construction_main ($) {
3253                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
3254                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
3255          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token->{tag_name});
3256  ## TODO: <http://html5.org/tools/web-apps-tracker?from=883&to=884>          return;
3257          } elsif ($token->{tag_name} eq 'br') {
3258            !!!parse-error (type => 'unmatched end tag:br');
3259    
3260            ## As if <br>
3261            $reconstruct_active_formatting_elements->($insert_to_current);
3262            
3263            my $el;
3264            !!!create-element ($el, 'br');
3265            $insert->($el);
3266            
3267            ## Ignore the token.
3268            !!!next-token;
3269          return;          return;
3270        } elsif ({        } elsif ({
3271                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
3272                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
3273                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
3274                  thead => 1, tr => 1,                  thead => 1, tr => 1,
3275                  area => 1, basefont => 1, bgsound => 1, br => 1,                  area => 1, basefont => 1, bgsound => 1,
3276                  embed => 1, hr => 1, iframe => 1, image => 1,                  embed => 1, hr => 1, iframe => 1, image => 1,
3277                  img => 1, input => 1, isindex => 1, noembed => 1,                  img => 1, input => 1, isindex => 1, noembed => 1,
3278                  noframes => 1, param => 1, select => 1, spacer => 1,                  noframes => 1, param => 1, select => 1, spacer => 1,
# Line 3178  sub _tree_construction_main ($) { Line 3299  sub _tree_construction_main ($) {
3299              if ({              if ({
3300                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
3301                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
3302                     tbody => 1, tfoot=> 1, thead => 1,
3303                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
3304                !!!back-token;                !!!back-token;
3305                $token = {type => 'end tag',                $token = {type => 'end tag',
# Line 3221  sub _tree_construction_main ($) { Line 3343  sub _tree_construction_main ($) {
3343    }; # $in_body    }; # $in_body
3344    
3345    B: {    B: {
3346      if ($phase eq 'main') {      if ($token->{type} eq 'DOCTYPE') {
3347        if ($token->{type} eq 'DOCTYPE') {        !!!parse-error (type => 'DOCTYPE in the middle');
3348          !!!parse-error (type => 'in html:#DOCTYPE');        ## Ignore the token
3349          ## Ignore the token        ## Stay in the phase
3350          ## Stay in the phase        !!!next-token;
3351          !!!next-token;        redo B;
3352          redo B;      } elsif ($token->{type} eq 'end-of-file') {
3353        } elsif ($token->{type} eq 'start tag' and        if ($token->{insertion_mode} ne 'trailing end') {
                $token->{tag_name} eq 'html') {  
         ## TODO: unless it is the first start tag token, parse-error  
         my $top_el = $self->{open_elements}->[0]->[0];  
         for my $attr_name (keys %{$token->{attributes}}) {  
           unless ($top_el->has_attribute_ns (undef, $attr_name)) {  
             $top_el->set_attribute_ns  
               (undef, [undef, $attr_name],  
                $token->{attributes}->{$attr_name}->{value});  
           }  
         }  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'end-of-file') {  
3354          ## Generate implied end tags          ## Generate implied end tags
3355          if ({          if ({
3356               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,
3357                 tbody => 1, tfoot=> 1, thead => 1,
3358              }->{$self->{open_elements}->[-1]->[1]}) {              }->{$self->{open_elements}->[-1]->[1]}) {
3359            !!!back-token;            !!!back-token;
3360            $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 3370  sub _tree_construction_main ($) {
3370            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3371          }          }
3372    
         ## Stop parsing  
         last B;  
   
3373          ## ISSUE: There is an issue in the spec.          ## ISSUE: There is an issue in the spec.
3374          }
3375    
3376          ## Stop parsing
3377          last B;
3378        } elsif ($token->{type} eq 'start tag' and
3379                 $token->{tag_name} eq 'html') {
3380          if ($self->{insertion_mode} eq 'trailing end') {
3381            ## Turn into the main phase
3382            !!!parse-error (type => 'after html:html');
3383            $self->{insertion_mode} = $previous_insertion_mode;
3384          }
3385    
3386    ## ISSUE: "aa<html>" is not a parse error.
3387    ## ISSUE: "<html>" in fragment is not a parse error.
3388          unless ($token->{first_start_tag}) {
3389            !!!parse-error (type => 'not first start tag');
3390          }
3391          my $top_el = $self->{open_elements}->[0]->[0];
3392          for my $attr_name (keys %{$token->{attributes}}) {
3393            unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3394              $top_el->set_attribute_ns
3395                (undef, [undef, $attr_name],
3396                 $token->{attributes}->{$attr_name}->{value});
3397            }
3398          }
3399          !!!next-token;
3400          redo B;
3401        } elsif ($token->{type} eq 'comment') {
3402          my $comment = $self->{document}->create_comment ($token->{data});
3403          if ($self->{insertion_mode} eq 'trailing end') {
3404            $self->{document}->append_child ($comment);
3405          } elsif ($self->{insertion_mode} eq 'after body') {
3406            $self->{open_elements}->[0]->[0]->append_child ($comment);
3407        } else {        } else {
3408            $self->{open_elements}->[-1]->[0]->append_child ($comment);
3409          }
3410          !!!next-token;
3411          redo B;
3412        } elsif ($self->{insertion_mode} eq 'in head' or
3413                 $self->{insertion_mode} eq 'in head noscript' or
3414                 $self->{insertion_mode} eq 'after head' or
3415                 $self->{insertion_mode} eq 'before head') {
3416          if ($token->{type} eq 'character') {
3417            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3418              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3419              unless (length $token->{data}) {
3420                !!!next-token;
3421                redo B;
3422              }
3423            }
3424    
3425          if ($self->{insertion_mode} eq 'before head') {          if ($self->{insertion_mode} eq 'before head') {
3426            if ($token->{type} eq 'character') {            ## As if <head>
3427              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {            !!!create-element ($self->{head_element}, 'head');
3428                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3429                unless (length $token->{data}) {            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3430                  !!!next-token;  
3431                  redo B;            ## Reprocess in the "in head" insertion mode...
3432                }            pop @{$self->{open_elements}};
3433              }  
3434              ## As if <head>            ## Reprocess in the "after head" insertion mode...
3435              !!!create-element ($self->{head_element}, 'head');          } elsif ($self->{insertion_mode} eq 'in head noscript') {
3436              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            ## As if </noscript>
3437              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            pop @{$self->{open_elements}};
3438              $self->{insertion_mode} = 'in head';            !!!parse-error (type => 'in noscript:#character');
3439              
3440              ## Reprocess in the "in head" insertion mode...
3441              ## As if </head>
3442              pop @{$self->{open_elements}};
3443    
3444              ## Reprocess in the "after head" insertion mode...
3445            } elsif ($self->{insertion_mode} eq 'in head') {
3446              pop @{$self->{open_elements}};
3447    
3448              ## Reprocess in the "after head" insertion mode...
3449            }
3450    
3451                ## "after head" insertion mode
3452                ## As if <body>
3453                !!!insert-element ('body');
3454                $self->{insertion_mode} = 'in body';
3455              ## reprocess              ## reprocess
3456              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;  
3457            } 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';  
3458              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
3459                !!!next-token;                if ($self->{insertion_mode} eq 'before head') {
3460              #} elsif ({                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
3461              #          base => 1, link => 1, meta => 1,                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3462              #          script => 1, style => 1, title => 1,                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
3463              #         }->{$token->{tag_name}}) {                  $self->{insertion_mode} = 'in head';
3464              #  ## reprocess                  !!!next-token;
3465              } else {                  redo B;
3466                ## reprocess                } elsif ($self->{insertion_mode} ne 'after head') {
3467              }                  !!!parse-error (type => 'in head:head'); # or in head noscript
3468              redo B;                  ## Ignore the token
3469            } elsif ($token->{type} eq 'end tag') {                  !!!next-token;
3470              if ({head => 1, body => 1, html => 1}->{$token->{tag_name}}) {                  redo B;
3471                  } else {
3472                    #
3473                  }
3474                } elsif ($self->{insertion_mode} eq 'before head') {
3475                ## As if <head>                ## As if <head>
3476                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
3477                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3478                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3479    
3480                $self->{insertion_mode} = 'in head';                $self->{insertion_mode} = 'in head';
3481                ## 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;  
3482              }              }
3483            } else {  
3484              die "$0: $token->{type}: Unknown type";              if ($token->{tag_name} eq 'base') {
3485            }                if ($self->{insertion_mode} eq 'in head noscript') {
3486          } elsif ($self->{insertion_mode} eq 'in head' or                  ## As if </noscript>
3487                   $self->{insertion_mode} eq 'in head noscript' or                  pop @{$self->{open_elements}};
3488                   $self->{insertion_mode} eq 'after head') {                  !!!parse-error (type => 'in noscript:base');
3489            if ($token->{type} eq 'character') {                
3490              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                  $self->{insertion_mode} = 'in head';
3491                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                  ## Reprocess in the "in head" insertion mode...
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
3492                }                }
3493              }  
               
             #  
           } 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}}) {  
3494                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3495                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} eq 'after head') {
3496                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
# Line 3354  sub _tree_construction_main ($) { Line 3502  sub _tree_construction_main ($) {
3502                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} eq 'after head';
3503                !!!next-token;                !!!next-token;
3504                redo B;                redo B;
3505              } elsif ($token->{tag_name} eq 'title' and              } elsif ($token->{tag_name} eq 'link') {
                      $self->{insertion_mode} eq 'in head') {  
3506                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3507                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} eq 'after head') {
3508                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3509                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3510                }                }
3511                $parse_rcdata->('RCDATA', $insert_to_current);                !!!insert-element ($token->{tag_name}, $token->{attributes});
3512                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3513                  pop @{$self->{open_elements}}
3514                      if $self->{insertion_mode} eq 'after head';
3515                  !!!next-token;
3516                  redo B;
3517                } elsif ($token->{tag_name} eq 'meta') {
3518                  ## NOTE: There is a "as if in head" code clone.
3519                  if ($self->{insertion_mode} eq 'after head') {
3520                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3521                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3522                  }
3523                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3524                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3525    
3526                  unless ($self->{confident}) {
3527                    my $charset;
3528                    if ($token->{attributes}->{charset}) { ## TODO: And if supported
3529                      $charset = $token->{attributes}->{charset}->{value};
3530                    }
3531                    if ($token->{attributes}->{'http-equiv'}) {
3532                      ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
3533                      if ($token->{attributes}->{'http-equiv'}->{value}
3534                          =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
3535                              [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
3536                              ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
3537                        $charset = defined $1 ? $1 : defined $2 ? $2 : $3;
3538                      } ## TODO: And if supported
3539                    }
3540                    ## TODO: Change the encoding
3541                  }
3542    
3543                  ## TODO: Extracting |charset| from |meta|.
3544                  pop @{$self->{open_elements}}
3545                      if $self->{insertion_mode} eq 'after head';
3546                  !!!next-token;
3547                  redo B;
3548                } elsif ($token->{tag_name} eq 'title') {
3549                  if ($self->{insertion_mode} eq 'in head noscript') {
3550                    ## As if </noscript>
3551                    pop @{$self->{open_elements}};
3552                    !!!parse-error (type => 'in noscript:title');
3553                  
3554                    $self->{insertion_mode} = 'in head';
3555                    ## Reprocess in the "in head" insertion mode...
3556                  } elsif ($self->{insertion_mode} eq 'after head') {
3557                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3558                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3559                  }
3560    
3561                  ## NOTE: There is a "as if in head" code clone.
3562                  my $parent = defined $self->{head_element} ? $self->{head_element}
3563                      : $self->{open_elements}->[-1]->[0];
3564                  $parse_rcdata->(RCDATA_CONTENT_MODEL,
3565                                  sub { $parent->append_child ($_[0]) });
3566                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
3567                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} eq 'after head';
3568                redo B;                redo B;
# Line 3373  sub _tree_construction_main ($) { Line 3574  sub _tree_construction_main ($) {
3574                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3575                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3576                }                }
3577                $parse_rcdata->('CDATA', $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
3578                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
3579                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} eq 'after head';
3580                redo B;                redo B;
# Line 3385  sub _tree_construction_main ($) { Line 3586  sub _tree_construction_main ($) {
3586                  !!!next-token;                  !!!next-token;
3587                  redo B;                  redo B;
3588                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} eq 'in head noscript') {
3589                  !!!parse-error (type => 'noscript in noscript');                  !!!parse-error (type => 'in noscript:noscript');
3590                  ## Ignore the token                  ## Ignore the token
3591                    !!!next-token;
3592                  redo B;                  redo B;
3593                } else {                } else {
3594                  #                  #
3595                }                }
3596              } elsif ($token->{tag_name} eq 'head' and              } elsif ($token->{tag_name} eq 'script') {
3597                       $self->{insertion_mode} ne 'after head') {                if ($self->{insertion_mode} eq 'in head noscript') {
3598                !!!parse-error (type => 'in head:head'); # or in head noscript                  ## As if </noscript>
3599                ## Ignore the token                  pop @{$self->{open_elements}};
3600                !!!next-token;                  !!!parse-error (type => 'in noscript:script');
3601                redo B;                
3602              } elsif ($self->{insertion_mode} ne 'in head noscript' and                  $self->{insertion_mode} = 'in head';
3603                       $token->{tag_name} eq 'script') {                  ## Reprocess in the "in head" insertion mode...
3604                if ($self->{insertion_mode} eq 'after head') {                } elsif ($self->{insertion_mode} eq 'after head') {
3605                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3606                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3607                }                }
3608    
3609                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3610                $script_start_tag->($insert_to_current);                $script_start_tag->($insert_to_current);
3611                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
3612                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} eq 'after head';
3613                redo B;                redo B;
3614              } 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  
3615                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
3616                !!!insert-element ('frameset', $token->{attributes});                if ($self->{insertion_mode} eq 'in head noscript') {
3617                $self->{insertion_mode} = 'in frameset';                  ## As if </noscript>
3618                    pop @{$self->{open_elements}};
3619                    !!!parse-error (type => 'in noscript:'.$token->{tag_name});
3620                    
3621                    ## Reprocess in the "in head" insertion mode...
3622                    ## As if </head>
3623                    pop @{$self->{open_elements}};
3624                    
3625                    ## Reprocess in the "after head" insertion mode...
3626                  } elsif ($self->{insertion_mode} eq 'in head') {
3627                    pop @{$self->{open_elements}};
3628                    
3629                    ## Reprocess in the "after head" insertion mode...
3630                  }
3631    
3632                  ## "after head" insertion mode
3633                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3634                  $self->{insertion_mode} = 'in '.$token->{tag_name};
3635                !!!next-token;                !!!next-token;
3636                redo B;                redo B;
3637              } else {              } else {
3638                #                #
3639              }              }
3640            } elsif ($token->{type} eq 'end tag') {  
3641              if ($self->{insertion_mode} eq 'in head' and              if ($self->{insertion_mode} eq 'in head noscript') {
3642                  $token->{tag_name} eq 'head') {                ## As if </noscript>
3643                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3644                $self->{insertion_mode} = 'after head';                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3645                !!!next-token;                
3646                redo B;                ## Reprocess in the "in head" insertion mode...
3647              } elsif ($self->{insertion_mode} eq 'in head noscript' and                ## As if </head>
                 $token->{tag_name} eq 'noscript') {  
3648                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3649                $self->{insertion_mode} = 'in head';  
3650                !!!next-token;                ## Reprocess in the "after head" insertion mode...
3651                redo B;              } elsif ($self->{insertion_mode} eq 'in head') {
3652              } elsif ($self->{insertion_mode} eq 'in head' and                ## As if </head>
3653                       ($token->{tag_name} eq 'body' or                pop @{$self->{open_elements}};
3654                        $token->{tag_name} eq 'html')) {  
3655                #                ## 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 {  
               #  
3656              }              }
           } else {  
             #  
           }  
3657    
3658            ## As if </head> or </noscript> or <body>              ## "after head" insertion mode
3659            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'  
3660              !!!insert-element ('body');              !!!insert-element ('body');
3661              $self->{insertion_mode} = 'in body';              $self->{insertion_mode} = 'in body';
3662            }              ## 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);  
3663              redo B;              redo B;
3664            }            } elsif ($token->{type} eq 'end tag') {
3665          } elsif ($self->{insertion_mode} eq 'in table') {              if ($token->{tag_name} eq 'head') {
3666            if ($token->{type} eq 'character') {                if ($self->{insertion_mode} eq 'before head') {
3667              ## NOTE: There are "character in table" code clones.                  ## As if <head>
3668              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                  !!!create-element ($self->{head_element}, 'head');
3669                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3670                                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3671                unless (length $token->{data}) {  
3672                    ## Reprocess in the "in head" insertion mode...
3673                    pop @{$self->{open_elements}};
3674                    $self->{insertion_mode} = 'after head';
3675                    !!!next-token;
3676                    redo B;
3677                  } elsif ($self->{insertion_mode} eq 'in head noscript') {
3678                    ## As if </noscript>
3679                    pop @{$self->{open_elements}};
3680                    !!!parse-error (type => 'in noscript:script');
3681                    
3682                    ## Reprocess in the "in head" insertion mode...
3683                    pop @{$self->{open_elements}};
3684                    $self->{insertion_mode} = 'after head';
3685                    !!!next-token;
3686                    redo B;
3687                  } elsif ($self->{insertion_mode} eq 'in head') {
3688                    pop @{$self->{open_elements}};
3689                    $self->{insertion_mode} = 'after head';
3690                  !!!next-token;                  !!!next-token;
3691                  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});  
3692                } else {                } else {
3693                  $foster_parent_element->insert_before                  #
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
3694                }                }
3695              } else {              } elsif ($token->{tag_name} eq 'noscript') {
3696                $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]);  
3697                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3698                    $self->{insertion_mode} = 'in head';
3699                    !!!next-token;
3700                    redo B;
3701                  } elsif ($self->{insertion_mode} eq 'before head') {
3702                    !!!parse-error (type => 'unmatched end tag:noscript');
3703                    ## Ignore the token ## ISSUE: An issue in the spec.
3704                    !!!next-token;
3705                    redo B;
3706                  } else {
3707                    #
3708                }                }
   
               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;  
3709              } elsif ({              } elsif ({
3710                        col => 1,                        body => 1, html => 1,
                       td => 1, th => 1, tr => 1,  
3711                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3712                ## Clear back to table context                if ($self->{insertion_mode} eq 'before head') {
3713                while ($self->{open_elements}->[-1]->[1] ne 'table' and                  ## As if <head>
3714                       $self->{open_elements}->[-1]->[1] ne 'html') {                  !!!create-element ($self->{head_element}, 'head');
3715                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3716                  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]);  
3717    
3718                ## As if </table>                  $self->{insertion_mode} = 'in head';
3719                ## have a table element in table scope                  ## Reprocess in the "in head" insertion mode...
3720                my $i;                } elsif ($self->{insertion_mode} eq 'in head noscript') {
3721                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3722                  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>  
3723                  !!!next-token;                  !!!next-token;
3724                  redo B;                  redo B;
3725                }                }
3726                                
3727                ## generate implied end tags                #
3728                if ({              } elsif ({
3729                     dd => 1, dt => 1, li => 1, p => 1,                        p => 1, br => 1,
3730                     td => 1, th => 1, tr => 1,                       }->{$token->{tag_name}}) {
3731                    }->{$self->{open_elements}->[-1]->[1]}) {                if ($self->{insertion_mode} eq 'before head') {
3732                  !!!back-token; # <table>                  ## As if <head>
3733                  $token = {type => 'end tag', tag_name => 'table'};                  !!!create-element ($self->{head_element}, 'head');
3734                  !!!back-token;                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3735                  $token = {type => 'end tag',                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
3736    
3737                if ($self->{open_elements}->[-1]->[1] ne 'table') {                  $self->{insertion_mode} = 'in head';
3738                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  ## Reprocess in the "in head" insertion mode...
3739                }                }
3740    
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
3741                #                #
3742              }              } else {
3743            } 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) {  
3744                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3745                  ## Ignore the token                  ## Ignore the token
3746                  !!!next-token;                  !!!next-token;
3747                  redo B;                  redo B;
3748                  } else {
3749                    #
3750                }                }
3751                              }
               ## 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]);  
               }  
3752    
3753                splice @{$self->{open_elements}}, $i;              if ($self->{insertion_mode} eq 'in head noscript') {
3754                  ## As if </noscript>
3755                  pop @{$self->{open_elements}};
3756                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
3757                  
3758                  ## Reprocess in the "in head" insertion mode...
3759                  ## As if </head>
3760                  pop @{$self->{open_elements}};
3761    
3762                $self->_reset_insertion_mode;                ## Reprocess in the "after head" insertion mode...
3763                } elsif ($self->{insertion_mode} eq 'in head') {
3764                  ## As if </head>
3765                  pop @{$self->{open_elements}};
3766    
3767                !!!next-token;                ## Reprocess in the "after head" insertion mode...
3768                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}}) {  
3769                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3770                ## Ignore the token                ## Ignore the token ## ISSUE: An issue in the spec.
3771                !!!next-token;                !!!next-token;
3772                redo B;                redo B;
             } else {  
               #  
3773              }              }
3774    
3775                ## "after head" insertion mode
3776                ## As if <body>
3777                !!!insert-element ('body');
3778                $self->{insertion_mode} = 'in body';
3779                ## reprocess
3780                redo B;
3781            } else {            } else {
3782              #              die "$0: $token->{type}: Unknown token type";
3783            }            }
3784    
3785            !!!parse-error (type => 'in table:'.$token->{tag_name});            ## ISSUE: An issue in the spec.
3786            $in_body->($insert_to_foster);      } elsif ($self->{insertion_mode} eq 'in body' or
3787            redo B;               $self->{insertion_mode} eq 'in cell' or
3788          } elsif ($self->{insertion_mode} eq 'in caption') {               $self->{insertion_mode} eq 'in caption') {
3789            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
3790              ## NOTE: This is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
3791              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
3792                            
3793              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3794    
3795              !!!next-token;              !!!next-token;
3796              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;  
3797            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
3798              if ({              if ({
3799                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
3800                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3801                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3802                !!!parse-error (type => 'not closed:caption');                if ($self->{insertion_mode} eq 'in cell') {
3803                    ## have an element in table scope
3804                ## As if </caption>                  my $tn;
3805                ## have a table element in table scope                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3806                my $i;                    my $node = $self->{open_elements}->[$_];
3807                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3808                  my $node = $self->{open_elements}->[$_];                      $tn = $node->[1];
3809                  if ($node->[1] eq 'caption') {                      last INSCOPE;
3810                    $i = $_;                    } elsif ({
3811                    last INSCOPE;                              table => 1, html => 1,
3812                  } elsif ({                             }->{$node->[1]}) {
3813                            table => 1, html => 1,                      last INSCOPE;
3814                           }->{$node->[1]}) {                    }
3815                    last INSCOPE;                  } # INSCOPE
3816                  }                    unless (defined $tn) {
3817                } # INSCOPE                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3818                unless (defined $i) {                      ## Ignore the token
3819                  !!!parse-error (type => 'unmatched end tag:caption');                      !!!next-token;
3820                  ## Ignore the token                      redo B;
3821                  !!!next-token;                    }
3822                  redo B;                  
3823                }                  ## 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]}) {  
3824                  !!!back-token; # <?>                  !!!back-token; # <?>
3825                  $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  
3826                  redo B;                  redo B;
3827                }                } elsif ($self->{insertion_mode} eq 'in caption') {
3828                    !!!parse-error (type => 'not closed:caption');
3829                    
3830                    ## As if </caption>
3831                    ## have a table element in table scope
3832                    my $i;
3833                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3834                      my $node = $self->{open_elements}->[$_];
3835                      if ($node->[1] eq 'caption') {
3836                        $i = $_;
3837                        last INSCOPE;
3838                      } elsif ({
3839                                table => 1, html => 1,
3840                               }->{$node->[1]}) {
3841                        last INSCOPE;
3842                      }
3843                    } # INSCOPE
3844                      unless (defined $i) {
3845                        !!!parse-error (type => 'unmatched end tag:caption');
3846                        ## Ignore the token
3847                        !!!next-token;
3848                        redo B;
3849                      }
3850                    
3851                    ## generate implied end tags
3852                    if ({
3853                         dd => 1, dt => 1, li => 1, p => 1,
3854                         td => 1, th => 1, tr => 1,
3855                         tbody => 1, tfoot=> 1, thead => 1,
3856                        }->{$self->{open_elements}->[-1]->[1]}) {
3857                      !!!back-token; # <?>
3858                      $token = {type => 'end tag', tag_name => 'caption'};
3859                      !!!back-token;
3860                      $token = {type => 'end tag',
3861                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3862                      redo B;
3863                    }
3864    
3865                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3866                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3867                    }
3868                    
3869                    splice @{$self->{open_elements}}, $i;
3870                    
3871                    $clear_up_to_marker->();
3872                    
3873                    $self->{insertion_mode} = 'in table';
3874                    
3875                    ## reprocess
3876                    redo B;
3877                  } else {
3878                    #
3879                }                }
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               ## reprocess  
               redo B;  
3880              } else {              } else {
3881                #                #
3882              }              }
3883            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
3884              if ($token->{tag_name} eq 'caption') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3885                ## have a table element in table scope                if ($self->{insertion_mode} eq 'in cell') {
3886                    ## have an element in table scope
3887                    my $i;
3888                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3889                      my $node = $self->{open_elements}->[$_];
3890                      if ($node->[1] eq $token->{tag_name}) {
3891                        $i = $_;
3892                        last INSCOPE;
3893                      } elsif ({
3894                                table => 1, html => 1,
3895                               }->{$node->[1]}) {
3896                        last INSCOPE;
3897                      }
3898                    } # INSCOPE
3899                      unless (defined $i) {
3900                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3901                        ## Ignore the token
3902                        !!!next-token;
3903                        redo B;
3904                      }
3905                    
3906                    ## generate implied end tags
3907                    if ({
3908                         dd => 1, dt => 1, li => 1, p => 1,
3909                         td => ($token->{tag_name} eq 'th'),
3910                         th => ($token->{tag_name} eq 'td'),
3911                         tr => 1,
3912                         tbody => 1, tfoot=> 1, thead => 1,
3913                        }->{$self->{open_elements}->[-1]->[1]}) {
3914                      !!!back-token;
3915                      $token = {type => 'end tag',
3916                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3917                      redo B;
3918                    }
3919                    
3920                    if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3921                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3922                    }
3923                    
3924                    splice @{$self->{open_elements}}, $i;
3925                    
3926                    $clear_up_to_marker->();
3927                    
3928                    $self->{insertion_mode} = 'in row';
3929                    
3930                    !!!next-token;
3931                    redo B;
3932                  } elsif ($self->{insertion_mode} eq 'in caption') {
3933                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3934                    ## Ignore the token
3935                    !!!next-token;
3936                    redo B;
3937                  } else {
3938                    #
3939                  }
3940                } elsif ($token->{tag_name} eq 'caption') {
3941                  if ($self->{insertion_mode} eq 'in caption') {
3942                    ## have a table element in table scope
3943                    my $i;
3944                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3945                      my $node = $self->{open_elements}->[$_];
3946                      if ($node->[1] eq $token->{tag_name}) {
3947                        $i = $_;
3948                        last INSCOPE;
3949                      } elsif ({
3950                                table => 1, html => 1,
3951                               }->{$node->[1]}) {
3952                        last INSCOPE;
3953                      }
3954                    } # INSCOPE
3955                      unless (defined $i) {
3956                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3957                        ## Ignore the token
3958                        !!!next-token;
3959                        redo B;
3960                      }
3961                    
3962                    ## generate implied end tags
3963                    if ({
3964                         dd => 1, dt => 1, li => 1, p => 1,
3965                         td => 1, th => 1, tr => 1,
3966                         tbody => 1, tfoot=> 1, thead => 1,
3967                        }->{$self->{open_elements}->[-1]->[1]}) {
3968                      !!!back-token;
3969                      $token = {type => 'end tag',
3970                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3971                      redo B;
3972                    }
3973                    
3974                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3975                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3976                    }
3977                    
3978                    splice @{$self->{open_elements}}, $i;
3979                    
3980                    $clear_up_to_marker->();
3981                    
3982                    $self->{insertion_mode} = 'in table';
3983                    
3984                    !!!next-token;
3985                    redo B;
3986                  } elsif ($self->{insertion_mode} eq 'in cell') {
3987                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3988                    ## Ignore the token
3989                    !!!next-token;
3990                    redo B;
3991                  } else {
3992                    #
3993                  }
3994                } elsif ({
3995                          table => 1, tbody => 1, tfoot => 1,
3996                          thead => 1, tr => 1,
3997                         }->{$token->{tag_name}} and
3998                         $self->{insertion_mode} eq 'in cell') {
3999                  ## have an element in table scope
4000                my $i;                my $i;
4001                  my $tn;
4002                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4003                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4004                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4005                    $i = $_;                    $i = $_;
4006                    last INSCOPE;                    last INSCOPE;
4007                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
4008                      $tn = $node->[1];
4009                      ## NOTE: There is exactly one |td| or |th| element
4010                      ## in scope in the stack of open elements by definition.
4011                  } elsif ({                  } elsif ({
4012                            table => 1, html => 1,                            table => 1, html => 1,
4013                           }->{$node->[1]}) {                           }->{$node->[1]}) {
# Line 3798  sub _tree_construction_main ($) { Line 4020  sub _tree_construction_main ($) {
4020                  !!!next-token;                  !!!next-token;
4021                  redo B;                  redo B;
4022                }                }
                 
               ## 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 'caption') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
4023    
4024                !!!next-token;                ## Close the cell
4025                  !!!back-token; # </?>
4026                  $token = {type => 'end tag', tag_name => $tn};
4027                redo B;                redo B;
4028              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table' and
4029                         $self->{insertion_mode} eq 'in caption') {
4030                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption');
4031    
4032                ## As if </caption>                ## As if </caption>
# Line 3850  sub _tree_construction_main ($) { Line 4054  sub _tree_construction_main ($) {
4054                if ({                if ({
4055                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
4056                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
4057                       tbody => 1, tfoot=> 1, thead => 1,
4058                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
4059                  !!!back-token; # </table>                  !!!back-token; # </table>
4060                  $token = {type => 'end tag', tag_name => 'caption'};                  $token = {type => 'end tag', tag_name => 'caption'};
# Line 3872  sub _tree_construction_main ($) { Line 4077  sub _tree_construction_main ($) {
4077                ## reprocess                ## reprocess
4078                redo B;                redo B;
4079              } elsif ({              } elsif ({
4080                        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,  
4081                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4082                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} eq 'in cell' or
4083                ## Ignore the token                    $self->{insertion_mode} eq 'in caption') {
4084                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');  
4085                  ## Ignore the token                  ## Ignore the token
4086                  !!!next-token;                  !!!next-token;
4087                  redo B;                  redo B;
4088                } else {                } else {
4089                  pop @{$self->{open_elements}}; # colgroup                  #
                 $self->{insertion_mode} = 'in table';  
                 !!!next-token;  
                 redo B;              
4090                }                }
4091              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
4092                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
4093                          thead => 1, tr => 1,
4094                         }->{$token->{tag_name}} and
4095                         $self->{insertion_mode} eq 'in caption') {
4096                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4097                ## Ignore the token                ## Ignore the token
4098                !!!next-token;                !!!next-token;
4099                redo B;                redo B;
4100              } else {              } else {
4101                #                #
4102              }              }
4103            } else {            } else {
4104              #              #
4105            }            }
4106              
4107            ## As if </colgroup>            $in_body->($insert_to_current);
4108            if ($self->{open_elements}->[-1]->[1] eq 'html') {            redo B;
4109              !!!parse-error (type => 'unmatched end tag:colgroup');          } elsif ($self->{insertion_mode} eq 'in row' or
4110              ## Ignore the token                   $self->{insertion_mode} eq 'in table body' or
4111              !!!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') {  
4112            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
4113              ## NOTE: This is a "character in table" code clone.              ## NOTE: There are "character in table" code clones.
4114              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4115                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4116                                
# Line 3969  sub _tree_construction_main ($) { Line 4127  sub _tree_construction_main ($) {
4127              ## into the current node" while characters might not be              ## into the current node" while characters might not be
4128              ## result in a new Text node.              ## result in a new Text node.
4129              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
4130                
4131              if ({              if ({
4132                   table => 1, tbody => 1, tfoot => 1,                   table => 1, tbody => 1, tfoot => 1,
4133                   thead => 1, tr => 1,                   thead => 1, tr => 1,
# Line 4009  sub _tree_construction_main ($) { Line 4167  sub _tree_construction_main ($) {
4167                            
4168              !!!next-token;              !!!next-token;
4169              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;  
4170            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
4171              if ({              if ({
4172                   tr => 1,                   tr => ($self->{insertion_mode} ne 'in row'),
4173                   th => 1, td => 1,                   th => 1, td => 1,
4174                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4175                unless ($token->{tag_name} eq 'tr') {                if ($self->{insertion_mode} eq 'in table') {
4176                  !!!parse-error (type => 'missing start tag:tr');                  ## Clear back to table context
4177                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
4178                           $self->{open_elements}->[-1]->[1] ne 'html') {
4179                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4180                      pop @{$self->{open_elements}};
4181                    }
4182                    
4183                    !!!insert-element ('tbody');
4184                    $self->{insertion_mode} = 'in table body';
4185                    ## reprocess in the "in table body" insertion mode...
4186                }                }
4187    
4188                ## Clear back to table body context                if ($self->{insertion_mode} eq 'in table body') {
4189                    unless ($token->{tag_name} eq 'tr') {
4190                      !!!parse-error (type => 'missing start tag:tr');
4191                    }
4192                    
4193                    ## Clear back to table body context
4194                    while (not {
4195                      tbody => 1, tfoot => 1, thead => 1, html => 1,
4196                    }->{$self->{open_elements}->[-1]->[1]}) {
4197                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4198                      pop @{$self->{open_elements}};
4199                    }
4200                    
4201                    $self->{insertion_mode} = 'in row';
4202                    if ($token->{tag_name} eq 'tr') {
4203                      !!!insert-element ($token->{tag_name}, $token->{attributes});
4204                      !!!next-token;
4205                      redo B;
4206                    } else {
4207                      !!!insert-element ('tr');
4208                      ## reprocess in the "in row" insertion mode
4209                    }
4210                  }
4211    
4212                  ## Clear back to table row context
4213                while (not {                while (not {
4214                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tr => 1, html => 1,
4215                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4216                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4217                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4218                }                }
4219                                
4220                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes});
4221                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = 'in cell';
4222                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
4223                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
4224                } else {                
4225                  !!!insert-element ('tr');                !!!next-token;
                 ## reprocess  
               }  
4226                redo B;                redo B;
4227              } elsif ({              } elsif ({
4228                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
4229                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
4230                          tr => 1, # $self->{insertion_mode} eq 'in row'
4231                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4232                ## have an element in table scope                if ($self->{insertion_mode} eq 'in row') {
4233                my $i;                  ## As if </tr>
4234                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
4235                  my $node = $self->{open_elements}->[$_];                  my $i;
4236                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4237                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
4238                      }->{$node->[1]}) {                    if ($node->[1] eq 'tr') {
4239                    $i = $_;                      $i = $_;
4240                    last INSCOPE;                      last INSCOPE;
4241                  } elsif ({                    } elsif ({
4242                            table => 1, html => 1,                              table => 1, html => 1,
4243                           }->{$node->[1]}) {                             }->{$node->[1]}) {
4244                    last INSCOPE;                      last INSCOPE;
4245                      }
4246                    } # INSCOPE
4247                    unless (defined $i) {
4248                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
4249                      ## Ignore the token
4250                      !!!next-token;
4251                      redo B;
4252                    }
4253                    
4254                    ## Clear back to table row context
4255                    while (not {
4256                      tr => 1, html => 1,
4257                    }->{$self->{open_elements}->[-1]->[1]}) {
4258                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4259                      pop @{$self->{open_elements}};
4260                    }
4261                    
4262                    pop @{$self->{open_elements}}; # tr
4263                    $self->{insertion_mode} = 'in table body';
4264                    if ($token->{tag_name} eq 'tr') {
4265                      ## reprocess
4266                      redo B;
4267                    } else {
4268                      ## reprocess in the "in table body" insertion mode...
4269                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
4270                }                }
4271    
4272                ## Clear back to table body context                if ($self->{insertion_mode} eq 'in table body') {
4273                while (not {                  ## have an element in table scope
4274                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
4275                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4276                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
4277                      if ({
4278                           tbody => 1, thead => 1, tfoot => 1,
4279                          }->{$node->[1]}) {
4280                        $i = $_;
4281                        last INSCOPE;
4282                      } elsif ({
4283                                table => 1, html => 1,
4284                               }->{$node->[1]}) {
4285                        last INSCOPE;
4286                      }
4287                    } # INSCOPE
4288                    unless (defined $i) {
4289                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4290                      ## Ignore the token
4291                      !!!next-token;
4292                      redo B;
4293                    }
4294    
4295                    ## Clear back to table body context
4296                    while (not {
4297                      tbody => 1, tfoot => 1, thead => 1, html => 1,
4298                    }->{$self->{open_elements}->[-1]->[1]}) {
4299                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4300                      pop @{$self->{open_elements}};
4301                    }
4302                    
4303                    ## As if <{current node}>
4304                    ## have an element in table scope
4305                    ## true by definition
4306                    
4307                    ## Clear back to table body context
4308                    ## nop by definition
4309                    
4310                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4311                    $self->{insertion_mode} = 'in table';
4312                    ## reprocess in "in table" insertion mode...
4313                }                }
4314    
4315                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
4316                ## have an element in table scope                  ## Clear back to table context
4317                ## true by definition                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
4318                           $self->{open_elements}->[-1]->[1] ne 'html') {
4319                ## Clear back to table body context                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4320                ## nop by definition                    pop @{$self->{open_elements}};
4321                    }
4322                pop @{$self->{open_elements}};                  
4323                $self->{insertion_mode} = 'in table';                  !!!insert-element ('colgroup');
4324                ## reprocess                  $self->{insertion_mode} = 'in column group';
4325                redo B;                  ## reprocess
4326                    redo B;
4327                  } elsif ({
4328                            caption => 1,
4329                            colgroup => 1,
4330                            tbody => 1, tfoot => 1, thead => 1,
4331                           }->{$token->{tag_name}}) {
4332                    ## Clear back to table context
4333                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
4334                           $self->{open_elements}->[-1]->[1] ne 'html') {
4335                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4336                      pop @{$self->{open_elements}};
4337                    }
4338                    
4339                    push @$active_formatting_elements, ['#marker', '']
4340                        if $token->{tag_name} eq 'caption';
4341                    
4342                    !!!insert-element ($token->{tag_name}, $token->{attributes});
4343                    $self->{insertion_mode} = {
4344                                               caption => 'in caption',
4345                                               colgroup => 'in column group',
4346                                               tbody => 'in table body',
4347                                               tfoot => 'in table body',
4348                                               thead => 'in table body',
4349                                              }->{$token->{tag_name}};
4350                    !!!next-token;
4351                    redo B;
4352                  } else {
4353                    die "$0: in table: <>: $token->{tag_name}";
4354                  }
4355              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4356                ## NOTE: This is a code clone of "table in table"                ## NOTE: There are code clones for this "table in table"
4357                !!!parse-error (type => 'not closed:table');                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4358    
4359                ## As if </table>                ## As if </table>
4360                ## have a table element in table scope                ## have a table element in table scope
# Line 4115  sub _tree_construction_main ($) { Line 4381  sub _tree_construction_main ($) {
4381                if ({                if ({
4382                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
4383                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
4384                       tbody => 1, tfoot=> 1, thead => 1,
4385                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
4386                  !!!back-token; # <table>                  !!!back-token; # <table>
4387                  $token = {type => 'end tag', tag_name => 'table'};                  $token = {type => 'end tag', tag_name => 'table'};
# Line 4130  sub _tree_construction_main ($) { Line 4397  sub _tree_construction_main ($) {
4397    
4398                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4399    
4400                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4401    
4402                ## reprocess                ## reprocess
4403                redo B;                redo B;
# Line 4138  sub _tree_construction_main ($) { Line 4405  sub _tree_construction_main ($) {
4405                #                #
4406              }              }
4407            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
4408              if ({              if ($token->{tag_name} eq 'tr' and
4409                   tbody => 1, tfoot => 1, thead => 1,                  $self->{insertion_mode} eq 'in row') {
                 }->{$token->{tag_name}}) {  
4410                ## have an element in table scope                ## have an element in table scope
4411                my $i;                my $i;
4412                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 4161  sub _tree_construction_main ($) { Line 4427  sub _tree_construction_main ($) {
4427                  redo B;                  redo B;
4428                }                }
4429    
4430                ## Clear back to table body context                ## Clear back to table row context
4431                while (not {                while (not {
4432                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tr => 1, html => 1,
4433                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4434                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4435                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4436                }                }
4437    
4438                pop @{$self->{open_elements}};                pop @{$self->{open_elements}}; # tr
4439                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = 'in table body';
4440                !!!next-token;                !!!next-token;
4441                redo B;                redo B;
4442              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4443                ## have an element in table scope                if ($self->{insertion_mode} eq 'in row') {
4444                my $i;                  ## As if </tr>
4445                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
4446                  my $node = $self->{open_elements}->[$_];                  my $i;
4447                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4448                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
4449                      }->{$node->[1]}) {                    if ($node->[1] eq 'tr') {
4450                    $i = $_;                      $i = $_;
4451                    last INSCOPE;                      last INSCOPE;
4452                  } elsif ({                    } elsif ({
4453                            table => 1, html => 1,                              table => 1, html => 1,
4454                           }->{$node->[1]}) {                             }->{$node->[1]}) {
4455                    last INSCOPE;                      last INSCOPE;
4456                      }
4457                    } # INSCOPE
4458                    unless (defined $i) {
4459                      !!!parse-error (type => 'unmatched end tag:'.$token->{type});
4460                      ## Ignore the token
4461                      !!!next-token;
4462                      redo B;
4463                  }                  }
4464                } # INSCOPE                  
4465                unless (defined $i) {                  ## Clear back to table row context
4466                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  while (not {
4467                  ## 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,  
4468                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4469                # MUST                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4470                my $foster_parent_element;                    pop @{$self->{open_elements}};
4471                my $next_sibling;                  }
4472                my $prev_sibling;                  
4473                OE: for (reverse 0..$#{$self->{open_elements}}) {                  pop @{$self->{open_elements}}; # tr
4474                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  $self->{insertion_mode} = 'in table body';
4475                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                  ## reprocess in the "in table body" insertion mode...
4476                    if (defined $parent and $parent->node_type == 1) {                }
4477                      $foster_parent_element = $parent;  
4478                      $next_sibling = $self->{open_elements}->[$_]->[0];                if ($self->{insertion_mode} eq 'in table body') {
4479                      $prev_sibling = $next_sibling->previous_sibling;                  ## have an element in table scope
4480                    } else {                  my $i;
4481                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4482                      $prev_sibling = $foster_parent_element->last_child;                    my $node = $self->{open_elements}->[$_];
4483                      if ({
4484                           tbody => 1, thead => 1, tfoot => 1,
4485                          }->{$node->[1]}) {
4486                        $i = $_;
4487                        last INSCOPE;
4488                      } elsif ({
4489                                table => 1, html => 1,
4490                               }->{$node->[1]}) {
4491                        last INSCOPE;
4492                    }                    }
4493                    last OE;                  } # INSCOPE
4494                    unless (defined $i) {
4495                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4496                      ## Ignore the token
4497                      !!!next-token;
4498                      redo B;
4499                  }                  }
4500                } # OE                  
4501                $foster_parent_element = $self->{open_elements}->[0]->[0] and                  ## Clear back to table body context
4502                $prev_sibling = $foster_parent_element->last_child                  while (not {
4503                  unless defined $foster_parent_element;                    tbody => 1, tfoot => 1, thead => 1, html => 1,
4504                if (defined $prev_sibling and                  }->{$self->{open_elements}->[-1]->[1]}) {
4505                    $prev_sibling->node_type == 3) {                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4506                  $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;  
4507                  }                  }
4508                } # INSCOPE                  
4509                unless (defined $i) {                  ## As if <{current node}>
4510                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  ## have an element in table scope
4511                  ## Ignore the token                  ## true by definition
4512                  !!!next-token;                  
4513                  redo B;                  ## Clear back to table body context
4514                }                  ## nop by definition
4515                    
               ## 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]);  
4516                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4517                    $self->{insertion_mode} = 'in table';
4518                    ## reprocess in the "in table" insertion mode...
4519                }                }
4520    
               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>  
4521                ## have a table element in table scope                ## have a table element in table scope
4522                my $i;                my $i;
4523                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4524                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4525                  if ($node->[1] eq 'table') {                  if ($node->[1] eq $token->{tag_name}) {
4526                    $i = $_;                    $i = $_;
4527                    last INSCOPE;                    last INSCOPE;
4528                  } elsif ({                  } elsif ({
# Line 4373  sub _tree_construction_main ($) { Line 4532  sub _tree_construction_main ($) {
4532                  }                  }
4533                } # INSCOPE                } # INSCOPE
4534                unless (defined $i) {                unless (defined $i) {
4535                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4536                  ## Ignore tokens </table><table>                  ## Ignore the token
4537                  !!!next-token;                  !!!next-token;
4538                  redo B;                  redo B;
4539                }                }
4540                  
4541                ## generate implied end tags                ## generate implied end tags
4542                if ({                if ({
4543                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
4544                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
4545                       tbody => 1, tfoot=> 1, thead => 1,
4546                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
4547                  !!!back-token;                  !!!back-token;
4548                  $token = {type => 'end tag',                  $token = {type => 'end tag',
4549                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4550                  redo B;                  redo B;
4551                }                }
4552                  
4553                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
4554                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4555                }                }
4556                    
4557                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4558                  
4559                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4560                  
               ## 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';  
4561                !!!next-token;                !!!next-token;
4562                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;  
4563              } elsif ({              } elsif ({
4564                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
4565                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
4566                ## have an element in table scope                       ($self->{insertion_mode} eq 'in row' or
4567                my $i;                        $self->{insertion_mode} eq 'in table body')) {
4568                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                if ($self->{insertion_mode} eq 'in row') {
4569                  my $node = $self->{open_elements}->[$_];                  ## have an element in table scope
4570                  if ($node->[1] eq $token->{tag_name}) {                  my $i;
4571                    $i = $_;                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4572                    last INSCOPE;                    my $node = $self->{open_elements}->[$_];
4573                  } elsif ({                    if ($node->[1] eq $token->{tag_name}) {
4574                            table => 1, html => 1,                      $i = $_;
4575                           }->{$node->[1]}) {                      last INSCOPE;
4576                    last INSCOPE;                    } elsif ({
4577                                table => 1, html => 1,
4578                               }->{$node->[1]}) {
4579                        last INSCOPE;
4580                      }
4581                    } # INSCOPE
4582                      unless (defined $i) {
4583                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4584                        ## Ignore the token
4585                        !!!next-token;
4586                        redo B;
4587                      }
4588                    
4589                    ## As if </tr>
4590                    ## have an element in table scope
4591                    my $i;
4592                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4593                      my $node = $self->{open_elements}->[$_];
4594                      if ($node->[1] eq 'tr') {
4595                        $i = $_;
4596                        last INSCOPE;
4597                      } elsif ({
4598                                table => 1, html => 1,
4599                               }->{$node->[1]}) {
4600                        last INSCOPE;
4601                      }
4602                    } # INSCOPE
4603                      unless (defined $i) {
4604                        !!!parse-error (type => 'unmatched end tag:tr');
4605                        ## Ignore the token
4606                        !!!next-token;
4607                        redo B;
4608                      }
4609                    
4610                    ## Clear back to table row context
4611                    while (not {
4612                      tr => 1, html => 1,
4613                    }->{$self->{open_elements}->[-1]->[1]}) {
4614                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4615                      pop @{$self->{open_elements}};
4616                  }                  }
4617                } # INSCOPE                  
4618                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
4619                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = 'in table body';
4620                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
4621                }                }
4622    
               ## As if </tr>  
4623                ## have an element in table scope                ## have an element in table scope
4624                my $i;                my $i;
4625                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4626                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4627                  if ($node->[1] eq 'tr') {                  if ($node->[1] eq $token->{tag_name}) {
4628                    $i = $_;                    $i = $_;
4629                    last INSCOPE;                    last INSCOPE;
4630                  } elsif ({                  } elsif ({
# Line 4511  sub _tree_construction_main ($) { Line 4634  sub _tree_construction_main ($) {
4634                  }                  }
4635                } # INSCOPE                } # INSCOPE
4636                unless (defined $i) {                unless (defined $i) {
4637                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4638                  ## Ignore the token                  ## Ignore the token
4639                  !!!next-token;                  !!!next-token;
4640                  redo B;                  redo B;
4641                }                }
4642    
4643                ## Clear back to table row context                ## Clear back to table body context
4644                while (not {                while (not {
4645                  tr => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
4646                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4647                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4648                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4649                }                }
4650    
4651                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
4652                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = 'in table';
4653                ## reprocess                !!!next-token;
4654                redo B;                redo B;
4655              } elsif ({              } elsif ({
4656                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
4657                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
4658                          tr => 1, # $self->{insertion_mode} eq 'in row'
4659                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} eq 'in table'
4660                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4661                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4662                ## Ignore the token                ## Ignore the token
# Line 4541  sub _tree_construction_main ($) { Line 4666  sub _tree_construction_main ($) {
4666                #                #
4667              }              }
4668            } else {            } else {
4669              #              die "$0: $token->{type}: Unknown token type";
4670            }            }
4671    
           ## As if in table  
4672            !!!parse-error (type => 'in table:'.$token->{tag_name});            !!!parse-error (type => 'in table:'.$token->{tag_name});
4673            $in_body->($insert_to_foster);            $in_body->($insert_to_foster);
4674            redo B;            redo B;
4675          } elsif ($self->{insertion_mode} eq 'in cell') {          } elsif ($self->{insertion_mode} eq 'in column group') {
4676            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
4677              ## NOTE: This is a code clone of "character in body".              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4678              $reconstruct_active_formatting_elements->($insert_to_current);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4679                              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  
4680                  !!!next-token;                  !!!next-token;
4681                  redo B;                  redo B;
4682                }                }
4683                }
4684                ## Close the cell              
4685                !!!back-token; # <?>              #
4686                $token = {type => 'end tag', tag_name => $tn};            } elsif ($token->{type} eq 'start tag') {
4687                if ($token->{tag_name} eq 'col') {
4688                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4689                  pop @{$self->{open_elements}};
4690                  !!!next-token;
4691                redo B;                redo B;
4692              } else {              } else {
4693                #                #
4694              }              }
4695            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
4696              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
4697                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] eq 'html') {
4698                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});  
4699                  ## Ignore the token                  ## Ignore the token
4700                  !!!next-token;                  !!!next-token;
4701                  redo B;                  redo B;
4702                  } else {
4703                    pop @{$self->{open_elements}}; # colgroup
4704                    $self->{insertion_mode} = 'in table';
4705                    !!!next-token;
4706                    redo B;            
4707                }                }
4708                              } elsif ($token->{tag_name} eq 'col') {
4709                ## 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});  
4710                ## Ignore the token                ## Ignore the token
4711                !!!next-token;                !!!next-token;
4712                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;  
4713              } else {              } else {
4714                #                #
4715              }              }
4716            } else {            } else {
4717              #              #
4718            }            }
4719              
4720            $in_body->($insert_to_current);            ## As if </colgroup>
4721            redo B;            if ($self->{open_elements}->[-1]->[1] eq 'html') {
4722                !!!parse-error (type => 'unmatched end tag:colgroup');
4723                ## Ignore the token
4724                !!!next-token;
4725                redo B;
4726              } else {
4727                pop @{$self->{open_elements}}; # colgroup
4728                $self->{insertion_mode} = 'in table';
4729                ## reprocess
4730                redo B;
4731              }
4732          } elsif ($self->{insertion_mode} eq 'in select') {          } elsif ($self->{insertion_mode} eq 'in select') {
4733            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
4734              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4735              !!!next-token;              !!!next-token;
4736              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;  
4737            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
4738              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
4739                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
# Line 4875  sub _tree_construction_main ($) { Line 4906  sub _tree_construction_main ($) {
4906          } elsif ($self->{insertion_mode} eq 'after body') {          } elsif ($self->{insertion_mode} eq 'after body') {
4907            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
4908              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4909                  my $data = $1;
4910                ## As if in body                ## As if in body
4911                $reconstruct_active_formatting_elements->($insert_to_current);                $reconstruct_active_formatting_elements->($insert_to_current);
4912                                
4913                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4914    
4915                unless (length $token->{data}) {                unless (length $token->{data}) {
4916                  !!!next-token;                  !!!next-token;
# Line 4887  sub _tree_construction_main ($) { Line 4919  sub _tree_construction_main ($) {
4919              }              }
4920                            
4921              #              #
4922              !!!parse-error (type => 'after body:#'.$token->{type});              !!!parse-error (type => 'after body:#character');
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[0]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
4923            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
4924              !!!parse-error (type => 'after body:'.$token->{tag_name});              !!!parse-error (type => 'after body:'.$token->{tag_name});
4925              #              #
# Line 4904  sub _tree_construction_main ($) { Line 4931  sub _tree_construction_main ($) {
4931                  !!!next-token;                  !!!next-token;
4932                  redo B;                  redo B;
4933                } else {                } else {
4934                  $phase = 'trailing end';                  $previous_insertion_mode = $self->{insertion_mode};
4935                    $self->{insertion_mode} = 'trailing end';
4936                  !!!next-token;                  !!!next-token;
4937                  redo B;                  redo B;
4938                }                }
# Line 4912  sub _tree_construction_main ($) { Line 4940  sub _tree_construction_main ($) {
4940                !!!parse-error (type => 'after body:/'.$token->{tag_name});                !!!parse-error (type => 'after body:/'.$token->{tag_name});
4941              }              }
4942            } else {            } else {
4943              !!!parse-error (type => 'after body:#'.$token->{type});              die "$0: $token->{type}: Unknown token type";
4944            }            }
4945    
4946            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = 'in body';
4947            ## reprocess            ## reprocess
4948            redo B;            redo B;
4949          } elsif ($self->{insertion_mode} eq 'in frameset') {      } elsif ($self->{insertion_mode} eq 'in frameset') {
4950            if ($token->{type} eq 'character') {        if ($token->{type} eq 'character') {
4951              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4952                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
   
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
4953    
4954              #            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);  
4955              !!!next-token;              !!!next-token;
4956              redo B;              redo B;
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'frameset') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'frame') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'noframes') {  
               $in_body->($insert_to_current);  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'frameset') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html' and  
                   @{$self->{open_elements}} == 1) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
               } else {  
                 pop @{$self->{open_elements}};  
                 !!!next-token;  
               }  
                 
               ## if not inner_html and  
               if ($self->{open_elements}->[-1]->[1] ne 'frameset') {  
                 $self->{insertion_mode} = 'after frameset';  
               }  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
4957            }            }
4958                      }
4959            if (defined $token->{tag_name}) {  
4960              !!!parse-error (type => 'in frameset:'.$token->{tag_name});          !!!parse-error (type => 'in frameset:#character');
4961            ## Ignore the token
4962            !!!next-token;
4963            redo B;
4964          } elsif ($token->{type} eq 'start tag') {
4965            if ($token->{tag_name} eq 'frameset') {
4966              !!!insert-element ($token->{tag_name}, $token->{attributes});
4967              !!!next-token;
4968              redo B;
4969            } elsif ($token->{tag_name} eq 'frame') {
4970              !!!insert-element ($token->{tag_name}, $token->{attributes});
4971              pop @{$self->{open_elements}};
4972              !!!next-token;
4973              redo B;
4974            } elsif ($token->{tag_name} eq 'noframes') {
4975              ## NOTE: As if in body.
4976              $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4977              redo B;
4978            } else {
4979              !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4980              ## Ignore the token
4981              !!!next-token;
4982              redo B;
4983            }
4984          } elsif ($token->{type} eq 'end tag') {
4985            if ($token->{tag_name} eq 'frameset') {
4986              if ($self->{open_elements}->[-1]->[1] eq 'html' and
4987                  @{$self->{open_elements}} == 1) {
4988                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4989                ## Ignore the token
4990                !!!next-token;
4991            } else {            } else {
4992              !!!parse-error (type => 'in frameset:#'.$token->{type});              pop @{$self->{open_elements}};
4993                !!!next-token;
4994            }            }
4995    
4996              if (not defined $self->{inner_html_node} and
4997                  $self->{open_elements}->[-1]->[1] ne 'frameset') {
4998                $self->{insertion_mode} = 'after frameset';
4999              }
5000              redo B;
5001            } else {
5002              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
5003            ## Ignore the token            ## Ignore the token
5004            !!!next-token;            !!!next-token;
5005            redo B;            redo B;
5006          } elsif ($self->{insertion_mode} eq 'after frameset') {          }
5007            if ($token->{type} eq 'character') {        } else {
5008            die "$0: $token->{type}: Unknown token type";
5009          }
5010        } elsif ($self->{insertion_mode} eq 'after frameset') {
5011          if ($token->{type} eq 'character') {
5012              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5013                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5014    
5015                unless (length $token->{data}) {                unless (length $token->{data}) {
5016                  !!!next-token;                  !!!next-token;
# Line 4994  sub _tree_construction_main ($) { Line 5018  sub _tree_construction_main ($) {
5018                }                }
5019              }              }
5020    
5021              #              if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
5022            } elsif ($token->{type} eq 'comment') {                !!!parse-error (type => 'after frameset:#character');
5023              my $comment = $self->{document}->create_comment ($token->{data});  
5024              $self->{open_elements}->[-1]->[0]->append_child ($comment);                ## Ignore the token.
5025              !!!next-token;                if (length $token->{data}) {
5026              redo B;                  ## reprocess the rest of characters
5027            } elsif ($token->{type} eq 'start tag') {                } else {
5028              if ($token->{tag_name} eq 'noframes') {                  !!!next-token;
5029                $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;  
5030                redo B;                redo B;
             } else {  
               #  
5031              }              }
5032            } else {  
5033              #          die qq[$0: Character "$token->{data}"];
5034            }        } elsif ($token->{type} eq 'start tag') {
5035                      if ($token->{tag_name} eq 'noframes') {
5036            if (defined $token->{tag_name}) {            ## NOTE: As if in body.
5037              !!!parse-error (type => 'after frameset:'.$token->{tag_name});            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
5038            } else {            redo B;
5039              !!!parse-error (type => 'after frameset:#'.$token->{type});          } else {
5040            }            !!!parse-error (type => 'after frameset:'.$token->{tag_name});
5041            ## Ignore the token            ## Ignore the token
5042            !!!next-token;            !!!next-token;
5043            redo B;            redo B;
5044            }
5045            ## ISSUE: An issue in spec there        } elsif ($token->{type} eq 'end tag') {
5046            if ($token->{tag_name} eq 'html') {
5047              $previous_insertion_mode = $self->{insertion_mode};
5048              $self->{insertion_mode} = 'trailing end';
5049              !!!next-token;
5050              redo B;
5051          } else {          } else {
5052            die "$0: $self->{insertion_mode}: Unknown insertion mode";            !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
5053              ## Ignore the token
5054              !!!next-token;
5055              redo B;
5056          }          }
5057          } else {
5058            die "$0: $token->{type}: Unknown token type";
5059        }        }
5060      } elsif ($phase eq 'trailing end') {  
5061          ## ISSUE: An issue in spec here
5062        } elsif ($self->{insertion_mode} eq 'trailing end') {
5063        ## states in the main stage is preserved yet # MUST        ## states in the main stage is preserved yet # MUST
5064                
5065        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} eq 'character') {
         !!!parse-error (type => 'after html:#DOCTYPE');  
         ## Ignore the token  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'comment') {  
         my $comment = $self->{document}->create_comment ($token->{data});  
         $self->{document}->append_child ($comment);  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'character') {  
5066          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5067            my $data = $1;            my $data = $1;
5068            ## As if in the main phase.            ## As if in the main phase.
5069            ## NOTE: The insertion mode in the main phase            ## NOTE: The insertion mode in the main phase
5070            ## just before the phase has been changed to the trailing            ## just before the phase has been changed to the trailing
5071            ## end phase is either "after body" or "after frameset".            ## end phase is either "after body" or "after frameset".
5072            $reconstruct_active_formatting_elements->($insert_to_current)            $reconstruct_active_formatting_elements->($insert_to_current);
             if $phase eq 'main';  
5073                        
5074            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);
5075                        
# Line 5065  sub _tree_construction_main ($) { Line 5080  sub _tree_construction_main ($) {
5080          }          }
5081    
5082          !!!parse-error (type => 'after html:#character');          !!!parse-error (type => 'after html:#character');
5083          $phase = 'main';          $self->{insertion_mode} = $previous_insertion_mode;
5084          ## reprocess          ## reprocess
5085          redo B;          redo B;
5086        } elsif ($token->{type} eq 'start tag' or        } elsif ($token->{type} eq 'start tag') {
                $token->{type} eq 'end tag') {  
5087          !!!parse-error (type => 'after html:'.$token->{tag_name});          !!!parse-error (type => 'after html:'.$token->{tag_name});
5088          $phase = 'main';          $self->{insertion_mode} = $previous_insertion_mode;
5089            ## reprocess
5090            redo B;
5091          } elsif ($token->{type} eq 'end tag') {
5092            !!!parse-error (type => 'after html:/'.$token->{tag_name});
5093            $self->{insertion_mode} = $previous_insertion_mode;
5094          ## reprocess          ## reprocess
5095          redo B;          redo B;
       } elsif ($token->{type} eq 'end-of-file') {  
         ## Stop parsing  
         last B;  
5096        } else {        } else {
5097          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token";
5098        }        }
5099        } else {
5100          die "$0: $self->{insertion_mode}: Unknown insertion mode";
5101      }      }
5102    } # B    } # B
5103    
# Line 5167  sub set_inner_html ($$$) { Line 5185  sub set_inner_html ($$$) {
5185    
5186      ## Step 2      ## Step 2
5187      my $node_ln = $node->local_name;      my $node_ln = $node->local_name;
5188      $p->{content_model_flag} = {      $p->{content_model} = {
5189        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
5190        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
5191        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
5192        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
5193        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
5194        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
5195        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
5196        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
5197        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
5198        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
5199      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
5200         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
5201            unless defined $p->{content_model};
5202            ## ISSUE: What is "the name of the element"? local name?
5203    
5204      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
5205    
# Line 5279  sub get_inner_html ($$$) { Line 5299  sub get_inner_html ($$$) {
5299            
5300      my $nt = $child->node_type;      my $nt = $child->node_type;
5301      if ($nt == 1) { # Element      if ($nt == 1) { # Element
5302        my $tag_name = lc $child->tag_name; ## ISSUE: Definition of "lowercase"        my $tag_name = $child->tag_name; ## TODO: manakai_tag_name
5303        $s .= '<' . $tag_name;        $s .= '<' . $tag_name;
5304          ## NOTE: Non-HTML case:
5305        ## ISSUE: Non-html elements        ## <http://permalink.gmane.org/gmane.org.w3c.whatwg.discuss/11191>
5306    
5307        my @attrs = @{$child->attributes}; # sort order MUST be stable        my @attrs = @{$child->attributes}; # sort order MUST be stable
5308        for my $attr (@attrs) { # order is implementation dependent        for my $attr (@attrs) { # order is implementation dependent
5309          my $attr_name = lc $attr->name; ## ISSUE: Definition of "lowercase"          my $attr_name = $attr->name; ## TODO: manakai_name
5310          $s .= ' ' . $attr_name . '="';          $s .= ' ' . $attr_name . '="';
5311          my $attr_value = $attr->value;          my $attr_value = $attr->value;
5312          ## escape          ## escape
# Line 5310  sub get_inner_html ($$$) { Line 5330  sub get_inner_html ($$$) {
5330        if (not $in_cdata and {        if (not $in_cdata and {
5331          style => 1, script => 1, xmp => 1, iframe => 1,          style => 1, script => 1, xmp => 1, iframe => 1,
5332          noembed => 1, noframes => 1, noscript => 1,          noembed => 1, noframes => 1, noscript => 1,
5333            plaintext => 1,
5334        }->{$tag_name}) {        }->{$tag_name}) {
5335          unshift @node, 'cdata-out';          unshift @node, 'cdata-out';
5336          $in_cdata = 1;          $in_cdata = 1;

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24