/[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.47 by wakaba, Sat Jul 21 09:12:43 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          if ($self->{insertion_mode} eq 'before head') {          $self->{open_elements}->[-1]->[0]->append_child ($comment);
3409          }
3410          !!!next-token;
3411          redo B;
3412        } elsif ($self->{insertion_mode} eq 'before head') {
3413            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
3414              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3415                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
# Line 3281  sub _tree_construction_main ($) { Line 3425  sub _tree_construction_main ($) {
3425              $self->{insertion_mode} = 'in head';              $self->{insertion_mode} = 'in head';
3426              ## reprocess              ## reprocess
3427              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;  
3428            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
3429              my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};              my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};
3430              !!!create-element ($self->{head_element}, 'head', $attr);              !!!create-element ($self->{head_element}, 'head', $attr);
# Line 3304  sub _tree_construction_main ($) { Line 3443  sub _tree_construction_main ($) {
3443              }              }
3444              redo B;              redo B;
3445            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
3446              if ({head => 1, body => 1, html => 1}->{$token->{tag_name}}) {              if ({
3447                     head => 1, body => 1, html => 1,
3448                     p => 1, br => 1,
3449                    }->{$token->{tag_name}}) {
3450                ## As if <head>                ## As if <head>
3451                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
3452                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
# Line 3334  sub _tree_construction_main ($) { Line 3476  sub _tree_construction_main ($) {
3476              }              }
3477                            
3478              #              #
           } 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;  
3479            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
3480              if ({base => ($self->{insertion_mode} eq 'in head' or              if ({base => ($self->{insertion_mode} eq 'in head' or
3481                            $self->{insertion_mode} eq 'after head'),                            $self->{insertion_mode} eq 'after head'),
3482                   link => 1, meta => 1}->{$token->{tag_name}}) {                   link => 1}->{$token->{tag_name}}) {
3483                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3484                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} eq 'after head') {
3485                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
# Line 3354  sub _tree_construction_main ($) { Line 3491  sub _tree_construction_main ($) {
3491                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} eq 'after head';
3492                !!!next-token;                !!!next-token;
3493                redo B;                redo B;
3494                } elsif ($token->{tag_name} eq 'meta') {
3495                  ## NOTE: There is a "as if in head" code clone.
3496                  if ($self->{insertion_mode} eq 'after head') {
3497                    !!!parse-error (type => 'after head:'.$token->{tag_name});
3498                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3499                  }
3500                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3501                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3502    
3503                  unless ($self->{confident}) {
3504                    my $charset;
3505                    if ($token->{attributes}->{charset}) { ## TODO: And if supported
3506                      $charset = $token->{attributes}->{charset}->{value};
3507                    }
3508                    if ($token->{attributes}->{'http-equiv'}) {
3509                      ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
3510                      if ($token->{attributes}->{'http-equiv'}->{value}
3511                          =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
3512                              [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
3513                              ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
3514                        $charset = defined $1 ? $1 : defined $2 ? $2 : $3;
3515                      } ## TODO: And if supported
3516                    }
3517                    ## TODO: Change the encoding
3518                  }
3519    
3520                  ## TODO: Extracting |charset| from |meta|.
3521                  pop @{$self->{open_elements}}
3522                      if $self->{insertion_mode} eq 'after head';
3523                  !!!next-token;
3524                  redo B;
3525              } elsif ($token->{tag_name} eq 'title' and              } elsif ($token->{tag_name} eq 'title' and
3526                       $self->{insertion_mode} eq 'in head') {                       $self->{insertion_mode} eq 'in head') {
3527                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
# Line 3361  sub _tree_construction_main ($) { Line 3529  sub _tree_construction_main ($) {
3529                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3530                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3531                }                }
3532                $parse_rcdata->('RCDATA', $insert_to_current);                my $parent = defined $self->{head_element} ? $self->{head_element}
3533                      : $self->{open_elements}->[-1]->[0];
3534                  $parse_rcdata->(RCDATA_CONTENT_MODEL,
3535                                  sub { $parent->append_child ($_[0]) });
3536                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
3537                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} eq 'after head';
3538                redo B;                redo B;
# Line 3373  sub _tree_construction_main ($) { Line 3544  sub _tree_construction_main ($) {
3544                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3545                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3546                }                }
3547                $parse_rcdata->('CDATA', $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
3548                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
3549                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} eq 'after head';
3550                redo B;                redo B;
# Line 3385  sub _tree_construction_main ($) { Line 3556  sub _tree_construction_main ($) {
3556                  !!!next-token;                  !!!next-token;
3557                  redo B;                  redo B;
3558                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} eq 'in head noscript') {
3559                  !!!parse-error (type => 'noscript in noscript');                  !!!parse-error (type => 'in noscript:noscript');
3560                  ## Ignore the token                  ## Ignore the token
3561                    !!!next-token;
3562                  redo B;                  redo B;
3563                } else {                } else {
3564                  #                  #
# Line 3437  sub _tree_construction_main ($) { Line 3609  sub _tree_construction_main ($) {
3609                !!!next-token;                !!!next-token;
3610                redo B;                redo B;
3611              } elsif ($self->{insertion_mode} eq 'in head' and              } elsif ($self->{insertion_mode} eq 'in head' and
3612                       ($token->{tag_name} eq 'body' or                       {
3613                        $token->{tag_name} eq 'html')) {                        body => 1, html => 1,
3614                          p => 1, br => 1,
3615                         }->{$token->{tag_name}}) {
3616                  #
3617                } elsif ($self->{insertion_mode} eq 'in head noscript' and
3618                         {
3619                          p => 1, br => 1,
3620                         }->{$token->{tag_name}}) {
3621                #                #
3622              } elsif ($self->{insertion_mode} ne 'after head') {              } elsif ($self->{insertion_mode} ne 'after head') {
3623                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
# Line 3468  sub _tree_construction_main ($) { Line 3647  sub _tree_construction_main ($) {
3647            redo B;            redo B;
3648    
3649            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
3650          } elsif ($self->{insertion_mode} eq 'in body') {          } elsif ($self->{insertion_mode} eq 'in body' or
3651                     $self->{insertion_mode} eq 'in cell' or
3652                     $self->{insertion_mode} eq 'in caption') {
3653            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
3654              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
3655              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
# Line 3477  sub _tree_construction_main ($) { Line 3658  sub _tree_construction_main ($) {
3658    
3659              !!!next-token;              !!!next-token;
3660              redo B;              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);  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There are "character in table" code clones.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!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;  
3661            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
3662              if ({              if ({
3663                   caption => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
3664                   colgroup => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
                  tbody => 1, tfoot => 1, thead => 1,  
3665                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3666                ## Clear back to table context                if ($self->{insertion_mode} eq 'in cell') {
3667                while ($self->{open_elements}->[-1]->[1] ne 'table' and                  ## have an element in table scope
3668                       $self->{open_elements}->[-1]->[1] ne 'html') {                  my $tn;
3669                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3670                  pop @{$self->{open_elements}};                    my $node = $self->{open_elements}->[$_];
3671                }                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3672                        $tn = $node->[1];
3673                push @$active_formatting_elements, ['#marker', '']                      last INSCOPE;
3674                  if $token->{tag_name} eq 'caption';                    } elsif ({
3675                                table => 1, html => 1,
3676                !!!insert-element ($token->{tag_name}, $token->{attributes});                             }->{$node->[1]}) {
3677                $self->{insertion_mode} = {                      last INSCOPE;
3678                                   caption => 'in caption',                    }
3679                                   colgroup => 'in column group',                  } # INSCOPE
3680                                   tbody => 'in table body',                    unless (defined $tn) {
3681                                   tfoot => 'in table body',                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3682                                   thead => 'in table body',                      ## Ignore the token
3683                                  }->{$token->{tag_name}};                      !!!next-token;
3684                !!!next-token;                      redo B;
3685                redo B;                    }
3686              } elsif ({                  
3687                        col => 1,                  ## Close the cell
3688                        td => 1, th => 1, tr => 1,                  !!!back-token; # <?>
3689                       }->{$token->{tag_name}}) {                  $token = {type => 'end tag', tag_name => $tn};
3690                ## Clear back to table context                  redo B;
3691                while ($self->{open_elements}->[-1]->[1] ne 'table' and                } elsif ($self->{insertion_mode} eq 'in caption') {
3692                       $self->{open_elements}->[-1]->[1] ne 'html') {                  !!!parse-error (type => 'not closed:caption');
3693                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  
3694                  pop @{$self->{open_elements}};                  ## As if </caption>
3695                }                  ## have a table element in table scope
3696                    my $i;
3697                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3698                $self->{insertion_mode} = $token->{tag_name} eq 'col'                    my $node = $self->{open_elements}->[$_];
3699                  ? 'in column group' : 'in table body';                    if ($node->[1] eq 'caption') {
3700                ## reprocess                      $i = $_;
3701                redo B;                      last INSCOPE;
3702              } elsif ($token->{tag_name} eq 'table') {                    } elsif ({
3703                ## NOTE: There are code clones for this "table in table"                              table => 1, html => 1,
3704                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                             }->{$node->[1]}) {
3705                        last INSCOPE;
3706                      }
3707                    } # INSCOPE
3708                      unless (defined $i) {
3709                        !!!parse-error (type => 'unmatched end tag:caption');
3710                        ## Ignore the token
3711                        !!!next-token;
3712                        redo B;
3713                      }
3714                    
3715                    ## generate implied end tags
3716                    if ({
3717                         dd => 1, dt => 1, li => 1, p => 1,
3718                         td => 1, th => 1, tr => 1,
3719                         tbody => 1, tfoot=> 1, thead => 1,
3720                        }->{$self->{open_elements}->[-1]->[1]}) {
3721                      !!!back-token; # <?>
3722                      $token = {type => 'end tag', tag_name => 'caption'};
3723                      !!!back-token;
3724                      $token = {type => 'end tag',
3725                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3726                      redo B;
3727                    }
3728    
3729                ## As if </table>                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3730                ## have a table element in table scope                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'table') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
3731                  }                  }
3732                } # INSCOPE                  
3733                unless (defined $i) {                  splice @{$self->{open_elements}}, $i;
3734                  !!!parse-error (type => 'unmatched end tag:table');                  
3735                  ## Ignore tokens </table><table>                  $clear_up_to_marker->();
3736                  !!!next-token;                  
3737                  redo B;                  $self->{insertion_mode} = 'in table';
3738                }                  
3739                                  ## reprocess
               ## 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; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
3740                  redo B;                  redo B;
3741                  } else {
3742                    #
3743                }                }
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
3744              } else {              } else {
3745                #                #
3746              }              }
3747            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
3748              if ($token->{tag_name} eq 'table') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3749                ## have a table element in table scope                if ($self->{insertion_mode} eq 'in cell') {
3750                my $i;                  ## have an element in table scope
3751                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  my $i;
3752                  my $node = $self->{open_elements}->[$_];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3753                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
3754                    $i = $_;                    if ($node->[1] eq $token->{tag_name}) {
3755                    last INSCOPE;                      $i = $_;
3756                  } elsif ({                      last INSCOPE;
3757                            table => 1, html => 1,                    } elsif ({
3758                           }->{$node->[1]}) {                              table => 1, html => 1,
3759                    last INSCOPE;                             }->{$node->[1]}) {
3760                        last INSCOPE;
3761                      }
3762                    } # INSCOPE
3763                      unless (defined $i) {
3764                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3765                        ## Ignore the token
3766                        !!!next-token;
3767                        redo B;
3768                      }
3769                    
3770                    ## generate implied end tags
3771                    if ({
3772                         dd => 1, dt => 1, li => 1, p => 1,
3773                         td => ($token->{tag_name} eq 'th'),
3774                         th => ($token->{tag_name} eq 'td'),
3775                         tr => 1,
3776                         tbody => 1, tfoot=> 1, thead => 1,
3777                        }->{$self->{open_elements}->[-1]->[1]}) {
3778                      !!!back-token;
3779                      $token = {type => 'end tag',
3780                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3781                      redo B;
3782                  }                  }
3783                } # INSCOPE                  
3784                unless (defined $i) {                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3785                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3786                    }
3787                    
3788                    splice @{$self->{open_elements}}, $i;
3789                    
3790                    $clear_up_to_marker->();
3791                    
3792                    $self->{insertion_mode} = 'in row';
3793                    
3794                    !!!next-token;
3795                    redo B;
3796                  } elsif ($self->{insertion_mode} eq 'in caption') {
3797                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3798                  ## Ignore the token                  ## Ignore the token
3799                  !!!next-token;                  !!!next-token;
3800                  redo B;                  redo B;
3801                  } else {
3802                    #
3803                }                }
3804                              } elsif ($token->{tag_name} eq 'caption') {
3805                ## generate implied end tags                if ($self->{insertion_mode} eq 'in caption') {
3806                if ({                  ## have a table element in table scope
3807                     dd => 1, dt => 1, li => 1, p => 1,                  my $i;
3808                     td => 1, th => 1, tr => 1,                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3809                    }->{$self->{open_elements}->[-1]->[1]}) {                    my $node = $self->{open_elements}->[$_];
3810                  !!!back-token;                    if ($node->[1] eq $token->{tag_name}) {
3811                  $token = {type => 'end tag',                      $i = $_;
3812                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                      last INSCOPE;
3813                  redo B;                    } elsif ({
3814                }                              table => 1, html => 1,
3815                               }->{$node->[1]}) {
3816                if ($self->{open_elements}->[-1]->[1] ne 'table') {                      last INSCOPE;
3817                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    }
3818                }                  } # INSCOPE
3819                      unless (defined $i) {
3820                splice @{$self->{open_elements}}, $i;                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3821                        ## Ignore the token
3822                $self->_reset_insertion_mode;                      !!!next-token;
3823                        redo B;
3824                !!!next-token;                    }
3825                redo B;                  
3826              } elsif ({                  ## generate implied end tags
3827                        body => 1, caption => 1, col => 1, colgroup => 1,                  if ({
3828                        html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,                       dd => 1, dt => 1, li => 1, p => 1,
3829                        thead => 1, tr => 1,                       td => 1, th => 1, tr => 1,
3830                       }->{$token->{tag_name}}) {                       tbody => 1, tfoot=> 1, thead => 1,
3831                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      }->{$self->{open_elements}->[-1]->[1]}) {
3832                ## Ignore the token                    !!!back-token;
3833                !!!next-token;                    $token = {type => 'end tag',
3834                redo B;                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3835              } else {                    redo B;
               #  
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in caption') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: This is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1, col => 1, colgroup => 1, tbody => 1,  
                  td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,  
                 }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'not closed:caption');  
   
               ## As if </caption>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'caption') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
3836                  }                  }
3837                } # INSCOPE                  
3838                unless (defined $i) {                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3839                  !!!parse-error (type => 'unmatched end tag:caption');                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3840                  ## Ignore the token                  }
3841                    
3842                    splice @{$self->{open_elements}}, $i;
3843                    
3844                    $clear_up_to_marker->();
3845                    
3846                    $self->{insertion_mode} = 'in table';
3847                    
3848                  !!!next-token;                  !!!next-token;
3849                  redo B;                  redo B;
3850                }                } elsif ($self->{insertion_mode} eq 'in cell') {
3851                                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3852                ## generate implied end tags                  ## Ignore the token
3853                if ({                  !!!next-token;
                    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 => 'caption'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
3854                  redo B;                  redo B;
3855                  } else {
3856                    #
3857                }                }
3858                } elsif ({
3859                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                        table => 1, tbody => 1, tfoot => 1,
3860                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                        thead => 1, tr => 1,
3861                }                       }->{$token->{tag_name}} and
3862                         $self->{insertion_mode} eq 'in cell') {
3863                splice @{$self->{open_elements}}, $i;                ## have an element in table scope
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'caption') {  
               ## have a table element in table scope  
3864                my $i;                my $i;
3865                  my $tn;
3866                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3867                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3868                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
3869                    $i = $_;                    $i = $_;
3870                    last INSCOPE;                    last INSCOPE;
3871                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
3872                      $tn = $node->[1];
3873                      ## NOTE: There is exactly one |td| or |th| element
3874                      ## in scope in the stack of open elements by definition.
3875                  } elsif ({                  } elsif ({
3876                            table => 1, html => 1,                            table => 1, html => 1,
3877                           }->{$node->[1]}) {                           }->{$node->[1]}) {
# Line 3798  sub _tree_construction_main ($) { Line 3884  sub _tree_construction_main ($) {
3884                  !!!next-token;                  !!!next-token;
3885                  redo B;                  redo B;
3886                }                }
                 
               ## 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;  
               }  
3887    
3888                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                ## Close the cell
3889                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!back-token; # </?>
3890                }                $token = {type => 'end tag', tag_name => $tn};
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               !!!next-token;  
3891                redo B;                redo B;
3892              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table' and
3893                         $self->{insertion_mode} eq 'in caption') {
3894                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption');
3895    
3896                ## As if </caption>                ## As if </caption>
# Line 3850  sub _tree_construction_main ($) { Line 3918  sub _tree_construction_main ($) {
3918                if ({                if ({
3919                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3920                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3921                       tbody => 1, tfoot=> 1, thead => 1,
3922                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3923                  !!!back-token; # </table>                  !!!back-token; # </table>
3924                  $token = {type => 'end tag', tag_name => 'caption'};                  $token = {type => 'end tag', tag_name => 'caption'};
# Line 3872  sub _tree_construction_main ($) { Line 3941  sub _tree_construction_main ($) {
3941                ## reprocess                ## reprocess
3942                redo B;                redo B;
3943              } elsif ({              } elsif ({
3944                        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,  
3945                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3946                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} eq 'in cell' or
3947                ## Ignore the token                    $self->{insertion_mode} eq 'in caption') {
3948                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');  
3949                  ## Ignore the token                  ## Ignore the token
3950                  !!!next-token;                  !!!next-token;
3951                  redo B;                  redo B;
3952                } else {                } else {
3953                  pop @{$self->{open_elements}}; # colgroup                  #
                 $self->{insertion_mode} = 'in table';  
                 !!!next-token;  
                 redo B;              
3954                }                }
3955              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
3956                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
3957                          thead => 1, tr => 1,
3958                         }->{$token->{tag_name}} and
3959                         $self->{insertion_mode} eq 'in caption') {
3960                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3961                ## Ignore the token                ## Ignore the token
3962                !!!next-token;                !!!next-token;
3963                redo B;                redo B;
3964              } else {              } else {
3965                #                #
3966              }              }
3967            } else {            } else {
3968              #              #
3969            }            }
3970              
3971            ## As if </colgroup>            $in_body->($insert_to_current);
3972            if ($self->{open_elements}->[-1]->[1] eq 'html') {            redo B;
3973              !!!parse-error (type => 'unmatched end tag:colgroup');          } elsif ($self->{insertion_mode} eq 'in row' or
3974              ## Ignore the token                   $self->{insertion_mode} eq 'in table body' or
3975              !!!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') {  
3976            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
3977              ## NOTE: This is a "character in table" code clone.              ## NOTE: There are "character in table" code clones.
3978              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3979                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3980                                
# Line 3969  sub _tree_construction_main ($) { Line 3991  sub _tree_construction_main ($) {
3991              ## into the current node" while characters might not be              ## into the current node" while characters might not be
3992              ## result in a new Text node.              ## result in a new Text node.
3993              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
3994                
3995              if ({              if ({
3996                   table => 1, tbody => 1, tfoot => 1,                   table => 1, tbody => 1, tfoot => 1,
3997                   thead => 1, tr => 1,                   thead => 1, tr => 1,
# Line 4009  sub _tree_construction_main ($) { Line 4031  sub _tree_construction_main ($) {
4031                            
4032              !!!next-token;              !!!next-token;
4033              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;  
4034            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
4035              if ({              if ({
4036                   tr => 1,                   tr => ($self->{insertion_mode} ne 'in row'),
4037                   th => 1, td => 1,                   th => 1, td => 1,
4038                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4039                unless ($token->{tag_name} eq 'tr') {                if ($self->{insertion_mode} eq 'in table') {
4040                  !!!parse-error (type => 'missing start tag:tr');                  ## Clear back to table context
4041                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
4042                           $self->{open_elements}->[-1]->[1] ne 'html') {
4043                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4044                      pop @{$self->{open_elements}};
4045                    }
4046                    
4047                    !!!insert-element ('tbody');
4048                    $self->{insertion_mode} = 'in table body';
4049                    ## reprocess in the "in table body" insertion mode...
4050                }                }
4051    
4052                ## Clear back to table body context                if ($self->{insertion_mode} eq 'in table body') {
4053                while (not {                  unless ($token->{tag_name} eq 'tr') {
4054                  tbody => 1, tfoot => 1, thead => 1, html => 1,                    !!!parse-error (type => 'missing start tag:tr');
4055                }->{$self->{open_elements}->[-1]->[1]}) {                  }
4056                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  
4057                  pop @{$self->{open_elements}};                  ## Clear back to table body context
4058                }                  while (not {
4059                                    tbody => 1, tfoot => 1, thead => 1, html => 1,
4060                $self->{insertion_mode} = 'in row';                  }->{$self->{open_elements}->[-1]->[1]}) {
4061                if ($token->{tag_name} eq 'tr') {                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4062                  !!!insert-element ($token->{tag_name}, $token->{attributes});                    pop @{$self->{open_elements}};
4063                  !!!next-token;                  }
4064                } else {                  
4065                  !!!insert-element ('tr');                  $self->{insertion_mode} = 'in row';
4066                  ## reprocess                  if ($token->{tag_name} eq 'tr') {
4067                }                    !!!insert-element ($token->{tag_name}, $token->{attributes});
4068                redo B;                    !!!next-token;
4069              } elsif ({                    redo B;
4070                        caption => 1, col => 1, colgroup => 1,                  } else {
4071                        tbody => 1, tfoot => 1, thead => 1,                    !!!insert-element ('tr');
4072                       }->{$token->{tag_name}}) {                    ## reprocess in the "in row" insertion mode
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ({  
                      tbody => 1, thead => 1, tfoot => 1,  
                     }->{$node->[1]}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
4073                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
4074                }                }
4075    
4076                ## Clear back to table body context                ## Clear back to table row context
4077                while (not {                while (not {
4078                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tr => 1, html => 1,
4079                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4080                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4081                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4082                }                }
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               ## reprocess  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: This is a code clone of "table in table"  
               !!!parse-error (type => 'not closed:table');  
   
               ## As if </table>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'table') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:table');  
                 ## Ignore tokens </table><table>  
                 !!!next-token;  
                 redo B;  
               }  
4083                                
4084                ## generate implied end tags                !!!insert-element ($token->{tag_name}, $token->{attributes});
4085                if ({                $self->{insertion_mode} = 'in cell';
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
4086    
4087                ## reprocess                push @$active_formatting_elements, ['#marker', ''];
4088                  
4089                  !!!next-token;
4090                redo B;                redo B;
4091              } else {              } elsif ({
4092                #                        caption => 1, col => 1, colgroup => 1,
4093              }                        tbody => 1, tfoot => 1, thead => 1, tr => 1,
4094            } elsif ($token->{type} eq 'end tag') {                       }->{$token->{tag_name}} and
4095              if ({                       $self->{insertion_mode} eq 'in row') {
4096                   tbody => 1, tfoot => 1, thead => 1,                ## As if </tr>
                 }->{$token->{tag_name}}) {  
4097                ## have an element in table scope                ## have an element in table scope
4098                my $i;                my $i;
4099                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4100                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4101                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq 'tr') {
4102                    $i = $_;                    $i = $_;
4103                    last INSCOPE;                    last INSCOPE;
4104                  } elsif ({                  } elsif ({
# Line 4155  sub _tree_construction_main ($) { Line 4108  sub _tree_construction_main ($) {
4108                  }                  }
4109                } # INSCOPE                } # INSCOPE
4110                unless (defined $i) {                unless (defined $i) {
4111                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
4112                  ## Ignore the token                  ## Ignore the token
4113                  !!!next-token;                  !!!next-token;
4114                  redo B;                  redo B;
4115                }                }
4116    
4117                ## Clear back to table body context                ## Clear back to table row context
4118                while (not {                while (not {
4119                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tr => 1, html => 1,
4120                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4121                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4122                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4123                }                }
4124    
4125                pop @{$self->{open_elements}};                pop @{$self->{open_elements}}; # tr
4126                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = 'in table body';
4127                !!!next-token;                ## reprocess
4128                redo B;                redo B;
4129              } elsif ($token->{tag_name} eq 'table') {              } elsif ({
4130                          caption => 1, col => 1, colgroup => 1,
4131                          tbody => 1, tfoot => 1, thead => 1,
4132                         }->{$token->{tag_name}} and
4133                         $self->{insertion_mode} eq 'in table body') {
4134                ## have an element in table scope                ## have an element in table scope
4135                my $i;                my $i;
4136                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 4215  sub _tree_construction_main ($) { Line 4172  sub _tree_construction_main ($) {
4172                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = 'in table';
4173                ## reprocess                ## reprocess
4174                redo B;                redo B;
4175              } elsif ({              } elsif ($token->{tag_name} eq 'col' and
4176                        body => 1, caption => 1, col => 1, colgroup => 1,                       $self->{insertion_mode} eq 'in table') {
4177                        html => 1, td => 1, th => 1, tr => 1,                ## Clear back to table context
4178                       }->{$token->{tag_name}}) {                while ($self->{open_elements}->[-1]->[1] ne 'table' and
4179                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                       $self->{open_elements}->[-1]->[1] ne 'html') {
               ## 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,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!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]}) {  
4180                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4181                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4182                }                }
                 
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = 'in cell';  
4183    
4184                push @$active_formatting_elements, ['#marker', ''];                !!!insert-element ('colgroup');
4185                                $self->{insertion_mode} = 'in column group';
4186                !!!next-token;                ## reprocess
4187                redo B;                redo B;
4188              } elsif ({              } elsif ({
4189                        caption => 1, col => 1, colgroup => 1,                        caption => 1,
4190                        tbody => 1, tfoot => 1, thead => 1, tr => 1,                        colgroup => 1,
4191                       }->{$token->{tag_name}}) {                        tbody => 1, tfoot => 1, thead => 1,
4192                ## As if </tr>                       }->{$token->{tag_name}} and
4193                ## have an element in table scope                      $self->{insertion_mode} eq 'in table') {
4194                my $i;                ## Clear back to table context
4195                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                while ($self->{open_elements}->[-1]->[1] ne 'table' and
4196                  my $node = $self->{open_elements}->[$_];                       $self->{open_elements}->[-1]->[1] ne 'html') {
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
4197                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4198                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4199                }                }
4200    
4201                pop @{$self->{open_elements}}; # tr                push @$active_formatting_elements, ['#marker', '']
4202                $self->{insertion_mode} = 'in table body';                  if $token->{tag_name} eq 'caption';
4203                ## reprocess  
4204                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4205                  $self->{insertion_mode} = {
4206                                     caption => 'in caption',
4207                                     colgroup => 'in column group',
4208                                     tbody => 'in table body',
4209                                     tfoot => 'in table body',
4210                                     thead => 'in table body',
4211                                    }->{$token->{tag_name}};
4212                  !!!next-token;
4213                redo B;                redo B;
4214              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4215                ## NOTE: This is a code clone of "table in table"                ## NOTE: There are code clones for this "table in table"
4216                !!!parse-error (type => 'not closed:table');                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4217    
4218                ## As if </table>                ## As if </table>
4219                ## have a table element in table scope                ## have a table element in table scope
# Line 4383  sub _tree_construction_main ($) { Line 4240  sub _tree_construction_main ($) {
4240                if ({                if ({
4241                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
4242                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
4243                       tbody => 1, tfoot=> 1, thead => 1,
4244                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
4245                  !!!back-token; # <table>                  !!!back-token; # <table>
4246                  $token = {type => 'end tag', tag_name => 'table'};                  $token = {type => 'end tag', tag_name => 'table'};
# Line 4398  sub _tree_construction_main ($) { Line 4256  sub _tree_construction_main ($) {
4256    
4257                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4258    
4259                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
4260    
4261                ## reprocess                ## reprocess
4262                redo B;                redo B;
# Line 4406  sub _tree_construction_main ($) { Line 4264  sub _tree_construction_main ($) {
4264                #                #
4265              }              }
4266            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
4267              if ($token->{tag_name} eq 'tr') {              if ($token->{tag_name} eq 'tr' and
4268                    $self->{insertion_mode} eq 'in row') {
4269                ## have an element in table scope                ## have an element in table scope
4270                my $i;                my $i;
4271                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 4440  sub _tree_construction_main ($) { Line 4299  sub _tree_construction_main ($) {
4299                !!!next-token;                !!!next-token;
4300                redo B;                redo B;
4301              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
4302                ## As if </tr>                if ($self->{insertion_mode} eq 'in row') {
4303                ## have an element in table scope                  ## As if </tr>
4304                    ## have an element in table scope
4305                    my $i;
4306                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4307                      my $node = $self->{open_elements}->[$_];
4308                      if ($node->[1] eq 'tr') {
4309                        $i = $_;
4310                        last INSCOPE;
4311                      } elsif ({
4312                                table => 1, html => 1,
4313                               }->{$node->[1]}) {
4314                        last INSCOPE;
4315                      }
4316                    } # INSCOPE
4317                      unless (defined $i) {
4318                        !!!parse-error (type => 'unmatched end tag:'.$token->{type});
4319                        ## Ignore the token
4320                        !!!next-token;
4321                        redo B;
4322                      }
4323                    
4324                    ## Clear back to table row context
4325                    while (not {
4326                      tr => 1, html => 1,
4327                    }->{$self->{open_elements}->[-1]->[1]}) {
4328                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4329                      pop @{$self->{open_elements}};
4330                    }
4331                    
4332                    pop @{$self->{open_elements}}; # tr
4333                    $self->{insertion_mode} = 'in table body';
4334                    ## reprocess in the "in table body" insertion mode...
4335                  }
4336    
4337                  if ($self->{insertion_mode} eq 'in table body') {
4338                    ## have an element in table scope
4339                    my $i;
4340                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4341                      my $node = $self->{open_elements}->[$_];
4342                      if ({
4343                           tbody => 1, thead => 1, tfoot => 1,
4344                          }->{$node->[1]}) {
4345                        $i = $_;
4346                        last INSCOPE;
4347                      } elsif ({
4348                                table => 1, html => 1,
4349                               }->{$node->[1]}) {
4350                        last INSCOPE;
4351                      }
4352                    } # INSCOPE
4353                    unless (defined $i) {
4354                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4355                      ## Ignore the token
4356                      !!!next-token;
4357                      redo B;
4358                    }
4359                    
4360                    ## Clear back to table body context
4361                    while (not {
4362                      tbody => 1, tfoot => 1, thead => 1, html => 1,
4363                    }->{$self->{open_elements}->[-1]->[1]}) {
4364                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4365                      pop @{$self->{open_elements}};
4366                    }
4367                    
4368                    ## As if <{current node}>
4369                    ## have an element in table scope
4370                    ## true by definition
4371                    
4372                    ## Clear back to table body context
4373                    ## nop by definition
4374                    
4375                    pop @{$self->{open_elements}};
4376                    $self->{insertion_mode} = 'in table';
4377                    ## reprocess in the "in table" insertion mode...
4378                  }
4379    
4380                  ## have a table element in table scope
4381                my $i;                my $i;
4382                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4383                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4384                  if ($node->[1] eq 'tr') {                  if ($node->[1] eq $token->{tag_name}) {
4385                    $i = $_;                    $i = $_;
4386                    last INSCOPE;                    last INSCOPE;
4387                  } elsif ({                  } elsif ({
# Line 4455  sub _tree_construction_main ($) { Line 4391  sub _tree_construction_main ($) {
4391                  }                  }
4392                } # INSCOPE                } # INSCOPE
4393                unless (defined $i) {                unless (defined $i) {
4394                  !!!parse-error (type => 'unmatched end tag:'.$token->{type});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4395                  ## Ignore the token                  ## Ignore the token
4396                  !!!next-token;                  !!!next-token;
4397                  redo B;                  redo B;
4398                }                }
4399    
4400                ## Clear back to table row context                ## generate implied end tags
4401                while (not {                if ({
4402                  tr => 1, html => 1,                     dd => 1, dt => 1, li => 1, p => 1,
4403                }->{$self->{open_elements}->[-1]->[1]}) {                     td => 1, th => 1, tr => 1,
4404                       tbody => 1, tfoot=> 1, thead => 1,
4405                      }->{$self->{open_elements}->[-1]->[1]}) {
4406                    !!!back-token;
4407                    $token = {type => 'end tag',
4408                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4409                    redo B;
4410                  }
4411                  
4412                  if ($self->{open_elements}->[-1]->[1] ne 'table') {
4413                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
                 pop @{$self->{open_elements}};  
4414                }                }
4415                    
4416                pop @{$self->{open_elements}}; # tr                splice @{$self->{open_elements}}, $i;
4417                $self->{insertion_mode} = 'in table body';                
4418                ## reprocess                $self->_reset_insertion_mode;
4419                  
4420                  !!!next-token;
4421                redo B;                redo B;
4422              } elsif ({              } elsif ({
4423                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
4424                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
4425                ## have an element in table scope                       ($self->{insertion_mode} eq 'in row' or
4426                my $i;                        $self->{insertion_mode} eq 'in table body')) {
4427                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                if ($self->{insertion_mode} eq 'in row') {
4428                  my $node = $self->{open_elements}->[$_];                  ## have an element in table scope
4429                  if ($node->[1] eq $token->{tag_name}) {                  my $i;
4430                    $i = $_;                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4431                    last INSCOPE;                    my $node = $self->{open_elements}->[$_];
4432                  } elsif ({                    if ($node->[1] eq $token->{tag_name}) {
4433                            table => 1, html => 1,                      $i = $_;
4434                           }->{$node->[1]}) {                      last INSCOPE;
4435                    last INSCOPE;                    } elsif ({
4436                                table => 1, html => 1,
4437                               }->{$node->[1]}) {
4438                        last INSCOPE;
4439                      }
4440                    } # INSCOPE
4441                      unless (defined $i) {
4442                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4443                        ## Ignore the token
4444                        !!!next-token;
4445                        redo B;
4446                      }
4447                    
4448                    ## As if </tr>
4449                    ## have an element in table scope
4450                    my $i;
4451                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4452                      my $node = $self->{open_elements}->[$_];
4453                      if ($node->[1] eq 'tr') {
4454                        $i = $_;
4455                        last INSCOPE;
4456                      } elsif ({
4457                                table => 1, html => 1,
4458                               }->{$node->[1]}) {
4459                        last INSCOPE;
4460                      }
4461                    } # INSCOPE
4462                      unless (defined $i) {
4463                        !!!parse-error (type => 'unmatched end tag:tr');
4464                        ## Ignore the token
4465                        !!!next-token;
4466                        redo B;
4467                      }
4468                    
4469                    ## Clear back to table row context
4470                    while (not {
4471                      tr => 1, html => 1,
4472                    }->{$self->{open_elements}->[-1]->[1]}) {
4473                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4474                      pop @{$self->{open_elements}};
4475                  }                  }
4476                } # INSCOPE                  
4477                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
4478                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = 'in table body';
4479                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
4480                }                }
4481    
               ## As if </tr>  
4482                ## have an element in table scope                ## have an element in table scope
4483                my $i;                my $i;
4484                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4485                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4486                  if ($node->[1] eq 'tr') {                  if ($node->[1] eq $token->{tag_name}) {
4487                    $i = $_;                    $i = $_;
4488                    last INSCOPE;                    last INSCOPE;
4489                  } elsif ({                  } elsif ({
# Line 4511  sub _tree_construction_main ($) { Line 4493  sub _tree_construction_main ($) {
4493                  }                  }
4494                } # INSCOPE                } # INSCOPE
4495                unless (defined $i) {                unless (defined $i) {
4496                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4497                  ## Ignore the token                  ## Ignore the token
4498                  !!!next-token;                  !!!next-token;
4499                  redo B;                  redo B;
4500                }                }
4501    
4502                ## Clear back to table row context                ## Clear back to table body context
4503                while (not {                while (not {
4504                  tr => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
4505                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4506                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4507                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4508                }                }
4509    
4510                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
4511                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = 'in table';
4512                ## reprocess                !!!next-token;
4513                redo B;                redo B;
4514              } elsif ({              } elsif ({
4515                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
4516                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
4517                          tr => 1, # $self->{insertion_mode} eq 'in row'
4518                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} eq 'in table'
4519                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4520                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4521                ## Ignore the token                ## Ignore the token
# Line 4541  sub _tree_construction_main ($) { Line 4525  sub _tree_construction_main ($) {
4525                #                #
4526              }              }
4527            } else {            } else {
4528              #              die "$0: $token->{type}: Unknown token type";
4529            }            }
4530    
           ## As if in table  
4531            !!!parse-error (type => 'in table:'.$token->{tag_name});            !!!parse-error (type => 'in table:'.$token->{tag_name});
4532            $in_body->($insert_to_foster);            $in_body->($insert_to_foster);
4533            redo B;            redo B;
4534          } elsif ($self->{insertion_mode} eq 'in cell') {          } elsif ($self->{insertion_mode} eq 'in column group') {
4535            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
4536              ## NOTE: This is a code clone of "character in body".              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4537              $reconstruct_active_formatting_elements->($insert_to_current);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4538                              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  
4539                  !!!next-token;                  !!!next-token;
4540                  redo B;                  redo B;
4541                }                }
4542                }
4543                ## Close the cell              
4544                !!!back-token; # <?>              #
4545                $token = {type => 'end tag', tag_name => $tn};            } elsif ($token->{type} eq 'start tag') {
4546                if ($token->{tag_name} eq 'col') {
4547                  !!!insert-element ($token->{tag_name}, $token->{attributes});
4548                  pop @{$self->{open_elements}};
4549                  !!!next-token;
4550                redo B;                redo B;
4551              } else {              } else {
4552                #                #
4553              }              }
4554            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
4555              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
4556                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] eq 'html') {
4557                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});  
4558                  ## Ignore the token                  ## Ignore the token
4559                  !!!next-token;                  !!!next-token;
4560                  redo B;                  redo B;
4561                  } else {
4562                    pop @{$self->{open_elements}}; # colgroup
4563                    $self->{insertion_mode} = 'in table';
4564                    !!!next-token;
4565                    redo B;            
4566                }                }
4567                              } elsif ($token->{tag_name} eq 'col') {
4568                ## 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});  
4569                ## Ignore the token                ## Ignore the token
4570                !!!next-token;                !!!next-token;
4571                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;  
4572              } else {              } else {
4573                #                #
4574              }              }
4575            } else {            } else {
4576              #              #
4577            }            }
4578              
4579            $in_body->($insert_to_current);            ## As if </colgroup>
4580            redo B;            if ($self->{open_elements}->[-1]->[1] eq 'html') {
4581                !!!parse-error (type => 'unmatched end tag:colgroup');
4582                ## Ignore the token
4583                !!!next-token;
4584                redo B;
4585              } else {
4586                pop @{$self->{open_elements}}; # colgroup
4587                $self->{insertion_mode} = 'in table';
4588                ## reprocess
4589                redo B;
4590              }
4591          } elsif ($self->{insertion_mode} eq 'in select') {          } elsif ($self->{insertion_mode} eq 'in select') {
4592            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
4593              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4594              !!!next-token;              !!!next-token;
4595              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;  
4596            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
4597              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
4598                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
# Line 4875  sub _tree_construction_main ($) { Line 4765  sub _tree_construction_main ($) {
4765          } elsif ($self->{insertion_mode} eq 'after body') {          } elsif ($self->{insertion_mode} eq 'after body') {
4766            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
4767              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4768                  my $data = $1;
4769                ## As if in body                ## As if in body
4770                $reconstruct_active_formatting_elements->($insert_to_current);                $reconstruct_active_formatting_elements->($insert_to_current);
4771                                
4772                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4773    
4774                unless (length $token->{data}) {                unless (length $token->{data}) {
4775                  !!!next-token;                  !!!next-token;
# Line 4887  sub _tree_construction_main ($) { Line 4778  sub _tree_construction_main ($) {
4778              }              }
4779                            
4780              #              #
4781              !!!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;  
4782            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
4783              !!!parse-error (type => 'after body:'.$token->{tag_name});              !!!parse-error (type => 'after body:'.$token->{tag_name});
4784              #              #
# Line 4904  sub _tree_construction_main ($) { Line 4790  sub _tree_construction_main ($) {
4790                  !!!next-token;                  !!!next-token;
4791                  redo B;                  redo B;
4792                } else {                } else {
4793                  $phase = 'trailing end';                  $previous_insertion_mode = $self->{insertion_mode};
4794                    $self->{insertion_mode} = 'trailing end';
4795                  !!!next-token;                  !!!next-token;
4796                  redo B;                  redo B;
4797                }                }
# Line 4912  sub _tree_construction_main ($) { Line 4799  sub _tree_construction_main ($) {
4799                !!!parse-error (type => 'after body:/'.$token->{tag_name});                !!!parse-error (type => 'after body:/'.$token->{tag_name});
4800              }              }
4801            } else {            } else {
4802              !!!parse-error (type => 'after body:#'.$token->{type});              die "$0: $token->{type}: Unknown token type";
4803            }            }
4804    
4805            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = 'in body';
4806            ## reprocess            ## reprocess
4807            redo B;            redo B;
4808          } elsif ($self->{insertion_mode} eq 'in frameset') {      } elsif ($self->{insertion_mode} eq 'in frameset') {
4809            if ($token->{type} eq 'character') {        if ($token->{type} eq 'character') {
4810              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4811                $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;  
               }  
             }  
4812    
4813              #            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);  
4814              !!!next-token;              !!!next-token;
4815              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 {  
             #  
4816            }            }
4817                      }
4818            if (defined $token->{tag_name}) {  
4819              !!!parse-error (type => 'in frameset:'.$token->{tag_name});          !!!parse-error (type => 'in frameset:#character');
4820            ## Ignore the token
4821            !!!next-token;
4822            redo B;
4823          } elsif ($token->{type} eq 'start tag') {
4824            if ($token->{tag_name} eq 'frameset') {
4825              !!!insert-element ($token->{tag_name}, $token->{attributes});
4826              !!!next-token;
4827              redo B;
4828            } elsif ($token->{tag_name} eq 'frame') {
4829              !!!insert-element ($token->{tag_name}, $token->{attributes});
4830              pop @{$self->{open_elements}};
4831              !!!next-token;
4832              redo B;
4833            } elsif ($token->{tag_name} eq 'noframes') {
4834              ## NOTE: As if in body.
4835              $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4836              redo B;
4837            } else {
4838              !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4839              ## Ignore the token
4840              !!!next-token;
4841              redo B;
4842            }
4843          } elsif ($token->{type} eq 'end tag') {
4844            if ($token->{tag_name} eq 'frameset') {
4845              if ($self->{open_elements}->[-1]->[1] eq 'html' and
4846                  @{$self->{open_elements}} == 1) {
4847                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4848                ## Ignore the token
4849                !!!next-token;
4850            } else {            } else {
4851              !!!parse-error (type => 'in frameset:#'.$token->{type});              pop @{$self->{open_elements}};
4852                !!!next-token;
4853            }            }
4854    
4855              if (not defined $self->{inner_html_node} and
4856                  $self->{open_elements}->[-1]->[1] ne 'frameset') {
4857                $self->{insertion_mode} = 'after frameset';
4858              }
4859              redo B;
4860            } else {
4861              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
4862            ## Ignore the token            ## Ignore the token
4863            !!!next-token;            !!!next-token;
4864            redo B;            redo B;
4865          } elsif ($self->{insertion_mode} eq 'after frameset') {          }
4866            if ($token->{type} eq 'character') {        } else {
4867            die "$0: $token->{type}: Unknown token type";
4868          }
4869        } elsif ($self->{insertion_mode} eq 'after frameset') {
4870          if ($token->{type} eq 'character') {
4871              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4872                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4873    
4874                unless (length $token->{data}) {                unless (length $token->{data}) {
4875                  !!!next-token;                  !!!next-token;
# Line 4994  sub _tree_construction_main ($) { Line 4877  sub _tree_construction_main ($) {
4877                }                }
4878              }              }
4879    
4880              #              if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
4881            } elsif ($token->{type} eq 'comment') {                !!!parse-error (type => 'after frameset:#character');
4882              my $comment = $self->{document}->create_comment ($token->{data});  
4883              $self->{open_elements}->[-1]->[0]->append_child ($comment);                ## Ignore the token.
4884              !!!next-token;                if (length $token->{data}) {
4885              redo B;                  ## reprocess the rest of characters
4886            } elsif ($token->{type} eq 'start tag') {                } else {
4887              if ($token->{tag_name} eq 'noframes') {                  !!!next-token;
4888                $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;  
4889                redo B;                redo B;
             } else {  
               #  
4890              }              }
4891            } else {  
4892              #          die qq[$0: Character "$token->{data}"];
4893            }        } elsif ($token->{type} eq 'start tag') {
4894                      if ($token->{tag_name} eq 'noframes') {
4895            if (defined $token->{tag_name}) {            ## NOTE: As if in body.
4896              !!!parse-error (type => 'after frameset:'.$token->{tag_name});            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4897            } else {            redo B;
4898              !!!parse-error (type => 'after frameset:#'.$token->{type});          } else {
4899            }            !!!parse-error (type => 'after frameset:'.$token->{tag_name});
4900            ## Ignore the token            ## Ignore the token
4901            !!!next-token;            !!!next-token;
4902            redo B;            redo B;
4903            }
4904            ## ISSUE: An issue in spec there        } elsif ($token->{type} eq 'end tag') {
4905            if ($token->{tag_name} eq 'html') {
4906              $previous_insertion_mode = $self->{insertion_mode};
4907              $self->{insertion_mode} = 'trailing end';
4908              !!!next-token;
4909              redo B;
4910          } else {          } else {
4911            die "$0: $self->{insertion_mode}: Unknown insertion mode";            !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
4912              ## Ignore the token
4913              !!!next-token;
4914              redo B;
4915          }          }
4916          } else {
4917            die "$0: $token->{type}: Unknown token type";
4918        }        }
4919      } elsif ($phase eq 'trailing end') {  
4920          ## ISSUE: An issue in spec here
4921        } elsif ($self->{insertion_mode} eq 'trailing end') {
4922        ## states in the main stage is preserved yet # MUST        ## states in the main stage is preserved yet # MUST
4923                
4924        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') {  
4925          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4926            my $data = $1;            my $data = $1;
4927            ## As if in the main phase.            ## As if in the main phase.
4928            ## NOTE: The insertion mode in the main phase            ## NOTE: The insertion mode in the main phase
4929            ## just before the phase has been changed to the trailing            ## just before the phase has been changed to the trailing
4930            ## end phase is either "after body" or "after frameset".            ## end phase is either "after body" or "after frameset".
4931            $reconstruct_active_formatting_elements->($insert_to_current)            $reconstruct_active_formatting_elements->($insert_to_current);
             if $phase eq 'main';  
4932                        
4933            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);
4934                        
# Line 5065  sub _tree_construction_main ($) { Line 4939  sub _tree_construction_main ($) {
4939          }          }
4940    
4941          !!!parse-error (type => 'after html:#character');          !!!parse-error (type => 'after html:#character');
4942          $phase = 'main';          $self->{insertion_mode} = $previous_insertion_mode;
4943          ## reprocess          ## reprocess
4944          redo B;          redo B;
4945        } elsif ($token->{type} eq 'start tag' or        } elsif ($token->{type} eq 'start tag') {
                $token->{type} eq 'end tag') {  
4946          !!!parse-error (type => 'after html:'.$token->{tag_name});          !!!parse-error (type => 'after html:'.$token->{tag_name});
4947          $phase = 'main';          $self->{insertion_mode} = $previous_insertion_mode;
4948            ## reprocess
4949            redo B;
4950          } elsif ($token->{type} eq 'end tag') {
4951            !!!parse-error (type => 'after html:/'.$token->{tag_name});
4952            $self->{insertion_mode} = $previous_insertion_mode;
4953          ## reprocess          ## reprocess
4954          redo B;          redo B;
       } elsif ($token->{type} eq 'end-of-file') {  
         ## Stop parsing  
         last B;  
4955        } else {        } else {
4956          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token";
4957        }        }
4958        } else {
4959          die "$0: $self->{insertion_mode}: Unknown insertion mode";
4960      }      }
4961    } # B    } # B
4962    
# Line 5167  sub set_inner_html ($$$) { Line 5044  sub set_inner_html ($$$) {
5044    
5045      ## Step 2      ## Step 2
5046      my $node_ln = $node->local_name;      my $node_ln = $node->local_name;
5047      $p->{content_model_flag} = {      $p->{content_model} = {
5048        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
5049        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
5050        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
5051        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
5052        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
5053        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
5054        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
5055        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
5056        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
5057        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
5058      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
5059         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
5060            unless defined $p->{content_model};
5061            ## ISSUE: What is "the name of the element"? local name?
5062    
5063      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
5064    
# Line 5279  sub get_inner_html ($$$) { Line 5158  sub get_inner_html ($$$) {
5158            
5159      my $nt = $child->node_type;      my $nt = $child->node_type;
5160      if ($nt == 1) { # Element      if ($nt == 1) { # Element
5161        my $tag_name = lc $child->tag_name; ## ISSUE: Definition of "lowercase"        my $tag_name = $child->tag_name; ## TODO: manakai_tag_name
5162        $s .= '<' . $tag_name;        $s .= '<' . $tag_name;
5163          ## NOTE: Non-HTML case:
5164        ## ISSUE: Non-html elements        ## <http://permalink.gmane.org/gmane.org.w3c.whatwg.discuss/11191>
5165    
5166        my @attrs = @{$child->attributes}; # sort order MUST be stable        my @attrs = @{$child->attributes}; # sort order MUST be stable
5167        for my $attr (@attrs) { # order is implementation dependent        for my $attr (@attrs) { # order is implementation dependent
5168          my $attr_name = lc $attr->name; ## ISSUE: Definition of "lowercase"          my $attr_name = $attr->name; ## TODO: manakai_name
5169          $s .= ' ' . $attr_name . '="';          $s .= ' ' . $attr_name . '="';
5170          my $attr_value = $attr->value;          my $attr_value = $attr->value;
5171          ## escape          ## escape
# Line 5310  sub get_inner_html ($$$) { Line 5189  sub get_inner_html ($$$) {
5189        if (not $in_cdata and {        if (not $in_cdata and {
5190          style => 1, script => 1, xmp => 1, iframe => 1,          style => 1, script => 1, xmp => 1, iframe => 1,
5191          noembed => 1, noframes => 1, noscript => 1,          noembed => 1, noframes => 1, noscript => 1,
5192            plaintext => 1,
5193        }->{$tag_name}) {        }->{$tag_name}) {
5194          unshift @node, 'cdata-out';          unshift @node, 'cdata-out';
5195          $in_cdata = 1;          $in_cdata = 1;

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24