/[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.24 by wakaba, Sat Jun 23 16:42:43 2007 UTC revision 1.53 by wakaba, Sat Jul 21 12:37:57 2007 UTC
# Line 7  our $VERSION=do{my @r=(q$Revision$=~/\d+ Line 7  our $VERSION=do{my @r=(q$Revision$=~/\d+
7  ## doc.write ('');  ## doc.write ('');
8  ## alert (doc.compatMode);  ## alert (doc.compatMode);
9    
10    ## ISSUE: HTML5 revision 967 says that the encoding layer MUST NOT
11    ## strip BOM and the HTML layer MUST ignore it.  Whether we can do it
12    ## is not yet clear.
13    ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?
14    ## "{U+FEFF}..." in GB18030?
15    
16  my $permitted_slash_tag_name = {  my $permitted_slash_tag_name = {
17    base => 1,    base => 1,
18    link => 1,    link => 1,
# Line 144  sub new ($) { Line 150  sub new ($) {
150    return $self;    return $self;
151  } # new  } # new
152    
153    sub CM_ENTITY () { 0b001 } # & markup in data
154    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
155    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
156    
157    sub PLAINTEXT_CONTENT_MODEL () { 0 }
158    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
159    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
160    sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
161    
162  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
163    
164  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
165    my $self = shift;    my $self = shift;
166    $self->{state} = 'data'; # MUST    $self->{state} = 'data'; # MUST
167    $self->{content_model_flag} = 'PCDATA'; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
168    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
169    undef $self->{current_attribute};    undef $self->{current_attribute};
170    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
# Line 188  sub _get_next_token ($) { Line 203  sub _get_next_token ($) {
203    A: {    A: {
204      if ($self->{state} eq 'data') {      if ($self->{state} eq 'data') {
205        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_input_character} == 0x0026) { # &
206          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA
             $self->{content_model_flag} eq 'RCDATA') {  
207            $self->{state} = 'entity data';            $self->{state} = 'entity data';
208            !!!next-input-character;            !!!next-input-character;
209            redo A;            redo A;
# Line 197  sub _get_next_token ($) { Line 211  sub _get_next_token ($) {
211            #            #
212          }          }
213        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_input_character} == 0x002D) { # -
214          if ($self->{content_model_flag} eq 'RCDATA' or          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
             $self->{content_model_flag} eq 'CDATA') {  
215            unless ($self->{escape}) {            unless ($self->{escape}) {
216              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_input_character}->[0] == 0x002D and # -
217                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_input_character}->[1] == 0x0021 and # !
# Line 210  sub _get_next_token ($) { Line 223  sub _get_next_token ($) {
223                    
224          #          #
225        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_input_character} == 0x003C) { # <
226          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
227              (($self->{content_model_flag} eq 'CDATA' or              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
               $self->{content_model_flag} eq 'RCDATA') and  
228               not $self->{escape})) {               not $self->{escape})) {
229            $self->{state} = 'tag open';            $self->{state} = 'tag open';
230            !!!next-input-character;            !!!next-input-character;
# Line 222  sub _get_next_token ($) { Line 234  sub _get_next_token ($) {
234          }          }
235        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
236          if ($self->{escape} and          if ($self->{escape} and
237              ($self->{content_model_flag} eq 'RCDATA' or              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
              $self->{content_model_flag} eq 'CDATA')) {  
238            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_input_character}->[0] == 0x002D and # -
239                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_input_character}->[1] == 0x002D) { # -
240              delete $self->{escape};              delete $self->{escape};
# Line 247  sub _get_next_token ($) { Line 258  sub _get_next_token ($) {
258      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} eq 'entity data') {
259        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
260                
261        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);
262    
263        $self->{state} = 'data';        $self->{state} = 'data';
264        # next-input-character is already done        # next-input-character is already done
# Line 260  sub _get_next_token ($) { Line 271  sub _get_next_token ($) {
271    
272        redo A;        redo A;
273      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} eq 'tag open') {
274        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
           $self->{content_model_flag} eq 'CDATA') {  
275          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_input_character} == 0x002F) { # /
276            !!!next-input-character;            !!!next-input-character;
277            $self->{state} = 'close tag open';            $self->{state} = 'close tag open';
# Line 274  sub _get_next_token ($) { Line 284  sub _get_next_token ($) {
284    
285            redo A;            redo A;
286          }          }
287        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
288          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_input_character} == 0x0021) { # !
289            $self->{state} = 'markup declaration open';            $self->{state} = 'markup declaration open';
290            !!!next-input-character;            !!!next-input-character;
# Line 321  sub _get_next_token ($) { Line 331  sub _get_next_token ($) {
331            redo A;            redo A;
332          }          }
333        } else {        } else {
334          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
335        }        }
336      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} eq 'close tag open') {
337        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
           $self->{content_model_flag} eq 'CDATA') {  
338          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
339              ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
340            my @next_char;            my @next_char;
341            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
342              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_input_character};
# Line 418  sub _get_next_token ($) { Line 428  sub _get_next_token ($) {
428          redo A;          redo A;
429        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
430          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
431              $self->{current_token}->{first_start_tag}
432                  = not defined $self->{last_emitted_start_tag_name};
433            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
434          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
435            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
436            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
437              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
438            }            }
# Line 443  sub _get_next_token ($) { Line 455  sub _get_next_token ($) {
455        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
456          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
457          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
458              $self->{current_token}->{first_start_tag}
459                  = not defined $self->{last_emitted_start_tag_name};
460            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
461          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
462            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
463            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
464              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
465            }            }
# Line 489  sub _get_next_token ($) { Line 503  sub _get_next_token ($) {
503          redo A;          redo A;
504        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
505          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
506              $self->{current_token}->{first_start_tag}
507                  = not defined $self->{last_emitted_start_tag_name};
508            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
509          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
510            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
511            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
512              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
513            }            }
# Line 527  sub _get_next_token ($) { Line 543  sub _get_next_token ($) {
543        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
544          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
545          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
546              $self->{current_token}->{first_start_tag}
547                  = not defined $self->{last_emitted_start_tag_name};
548            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
549          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
550            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
551            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
552              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
553            }            }
# Line 553  sub _get_next_token ($) { Line 571  sub _get_next_token ($) {
571        my $before_leave = sub {        my $before_leave = sub {
572          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
573              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
574            !!!parse-error (type => 'dupulicate attribute');            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});
575            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
576          } else {          } else {
577            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
# Line 578  sub _get_next_token ($) { Line 596  sub _get_next_token ($) {
596        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
597          $before_leave->();          $before_leave->();
598          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
599              $self->{current_token}->{first_start_tag}
600                  = not defined $self->{last_emitted_start_tag_name};
601            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
602          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
603            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
604            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
605              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
606            }            }
# Line 617  sub _get_next_token ($) { Line 637  sub _get_next_token ($) {
637          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
638          $before_leave->();          $before_leave->();
639          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
640              $self->{current_token}->{first_start_tag}
641                  = not defined $self->{last_emitted_start_tag_name};
642            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
643          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
644            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
645            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
646              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
647            }            }
# Line 653  sub _get_next_token ($) { Line 675  sub _get_next_token ($) {
675          redo A;          redo A;
676        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
677          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
678              $self->{current_token}->{first_start_tag}
679                  = not defined $self->{last_emitted_start_tag_name};
680            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
681          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
682            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
683            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
684              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
685            }            }
# Line 684  sub _get_next_token ($) { Line 708  sub _get_next_token ($) {
708            #            #
709          } else {          } else {
710            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
711              ## TODO: Different error type for <aa / bb> than <aa/>
712          }          }
713          $self->{state} = 'before attribute name';          $self->{state} = 'before attribute name';
714          # next-input-character is already done          # next-input-character is already done
# Line 691  sub _get_next_token ($) { Line 716  sub _get_next_token ($) {
716        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
717          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
718          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
719              $self->{current_token}->{first_start_tag}
720                  = not defined $self->{last_emitted_start_tag_name};
721            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
722          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
723            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
724            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
725              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
726            }            }
# Line 736  sub _get_next_token ($) { Line 763  sub _get_next_token ($) {
763          redo A;          redo A;
764        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
765          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
766              $self->{current_token}->{first_start_tag}
767                  = not defined $self->{last_emitted_start_tag_name};
768            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
769          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
770            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
771            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
772              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
773            }            }
# Line 754  sub _get_next_token ($) { Line 783  sub _get_next_token ($) {
783        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
784          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
785          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
786              $self->{current_token}->{first_start_tag}
787                  = not defined $self->{last_emitted_start_tag_name};
788            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
789          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
790            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
791            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
792              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
793            }            }
# Line 788  sub _get_next_token ($) { Line 819  sub _get_next_token ($) {
819        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
820          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
821          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
822              $self->{current_token}->{first_start_tag}
823                  = not defined $self->{last_emitted_start_tag_name};
824            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
825          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
826            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
827            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
828              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
829            }            }
# Line 822  sub _get_next_token ($) { Line 855  sub _get_next_token ($) {
855        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
856          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
857          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
858              $self->{current_token}->{first_start_tag}
859                  = not defined $self->{last_emitted_start_tag_name};
860            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
861          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
862            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
863            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
864              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
865            }            }
# Line 859  sub _get_next_token ($) { Line 894  sub _get_next_token ($) {
894          redo A;          redo A;
895        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
896          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
897              $self->{current_token}->{first_start_tag}
898                  = not defined $self->{last_emitted_start_tag_name};
899            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
900          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
901            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
902            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
903              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
904            }            }
# Line 877  sub _get_next_token ($) { Line 914  sub _get_next_token ($) {
914        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
915          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
916          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
917              $self->{current_token}->{first_start_tag}
918                  = not defined $self->{last_emitted_start_tag_name};
919            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
920          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
921            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
922            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
923              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
924            }            }
# Line 899  sub _get_next_token ($) { Line 938  sub _get_next_token ($) {
938          redo A;          redo A;
939        }        }
940      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} eq 'entity in attribute value') {
941        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);
942    
943        unless (defined $token) {        unless (defined $token) {
944          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
# Line 990  sub _get_next_token ($) { Line 1029  sub _get_next_token ($) {
1029          }          }
1030        }        }
1031    
1032        !!!parse-error (type => 'bogus comment open');        !!!parse-error (type => 'bogus comment');
1033        $self->{next_input_character} = shift @next_char;        $self->{next_input_character} = shift @next_char;
1034        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1035        $self->{state} = 'bogus comment';        $self->{state} = 'bogus comment';
# Line 1049  sub _get_next_token ($) { Line 1088  sub _get_next_token ($) {
1088          redo A;          redo A;
1089        } else {        } else {
1090          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1091              .= chr ($self->{next_input_character});              .= '-' . chr ($self->{next_input_character});
1092          $self->{state} = 'comment';          $self->{state} = 'comment';
1093          !!!next-input-character;          !!!next-input-character;
1094          redo A;          redo A;
# Line 1409  sub _get_next_token ($) { Line 1448  sub _get_next_token ($) {
1448          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1449    
1450          $self->{state} = 'data';          $self->{state} = 'data';
1451          ## recomsume          ## reconsume
1452    
1453          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1454          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1452  sub _get_next_token ($) { Line 1491  sub _get_next_token ($) {
1491          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1492    
1493          $self->{state} = 'data';          $self->{state} = 'data';
1494          ## recomsume          ## reconsume
1495    
1496          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1497          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1498    
1499          redo A;          redo A;
1500        } else {        } else {
1501          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after SYSTEM');
1502          $self->{state} = 'bogus DOCTYPE';          $self->{state} = 'bogus DOCTYPE';
1503          !!!next-input-character;          !!!next-input-character;
1504          redo A;          redo A;
# Line 1527  sub _get_next_token ($) { Line 1566  sub _get_next_token ($) {
1566          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1567    
1568          $self->{state} = 'data';          $self->{state} = 'data';
1569          ## recomsume          ## reconsume
1570    
1571          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1572          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1570  sub _get_next_token ($) { Line 1609  sub _get_next_token ($) {
1609    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
1610  } # _get_next_token  } # _get_next_token
1611    
1612  sub _tokenize_attempt_to_consume_an_entity ($) {  sub _tokenize_attempt_to_consume_an_entity ($$) {
1613    my $self = shift;    my ($self, $in_attr) = @_;
1614    
1615    if ({    if ({
1616         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
# Line 1584  sub _tokenize_attempt_to_consume_an_enti Line 1623  sub _tokenize_attempt_to_consume_an_enti
1623      !!!next-input-character;      !!!next-input-character;
1624      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_input_character} == 0x0078 or # x
1625          $self->{next_input_character} == 0x0058) { # X          $self->{next_input_character} == 0x0058) { # X
1626        my $num;        my $code;
1627        X: {        X: {
1628          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_input_character};
1629          !!!next-input-character;          !!!next-input-character;
1630          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_input_character} and
1631              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_input_character} <= 0x0039) { # 0..9
1632            $num ||= 0;            $code ||= 0;
1633            $num *= 0x10;            $code *= 0x10;
1634            $num += $self->{next_input_character} - 0x0030;            $code += $self->{next_input_character} - 0x0030;
1635            redo X;            redo X;
1636          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
1637                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_input_character} <= 0x0066) { # a..f
1638            ## ISSUE: the spec says U+0078, which is apparently incorrect            $code ||= 0;
1639            $num ||= 0;            $code *= 0x10;
1640            $num *= 0x10;            $code += $self->{next_input_character} - 0x0060 + 9;
           $num += $self->{next_input_character} - 0x0060 + 9;  
1641            redo X;            redo X;
1642          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
1643                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_input_character} <= 0x0046) { # A..F
1644            ## ISSUE: the spec says U+0058, which is apparently incorrect            $code ||= 0;
1645            $num ||= 0;            $code *= 0x10;
1646            $num *= 0x10;            $code += $self->{next_input_character} - 0x0040 + 9;
           $num += $self->{next_input_character} - 0x0040 + 9;  
1647            redo X;            redo X;
1648          } elsif (not defined $num) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
1649            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro');
1650              !!!back-next-input-character ($x_char, $self->{next_input_character});
1651            $self->{next_input_character} = 0x0023; # #            $self->{next_input_character} = 0x0023; # #
           !!!back-next-input-character ($x_char);  
1652            return undef;            return undef;
1653          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_input_character} == 0x003B) { # ;
1654            !!!next-input-character;            !!!next-input-character;
# Line 1619  sub _tokenize_attempt_to_consume_an_enti Line 1656  sub _tokenize_attempt_to_consume_an_enti
1656            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc');
1657          }          }
1658    
1659          ## TODO: check the definition for |a valid Unicode character|.          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1660          ## <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8189>            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1661          if ($num > 1114111 or $num == 0) {            $code = 0xFFFD;
1662            $num = 0xFFFD; # REPLACEMENT CHARACTER          } elsif ($code > 0x10FFFF) {
1663            ## ISSUE: Why this is not an error?            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1664          } elsif (0x80 <= $num and $num <= 0x9F) {            $code = 0xFFFD;
1665            !!!parse-error (type => sprintf 'c1 entity:U+%04X', $num);          } elsif ($code == 0x000D) {
1666            $num = $c1_entity_char->{$num};            !!!parse-error (type => 'CR character reference');
1667              $code = 0x000A;
1668            } elsif (0x80 <= $code and $code <= 0x9F) {
1669              !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
1670              $code = $c1_entity_char->{$code};
1671          }          }
1672    
1673          return {type => 'character', data => chr $num};          return {type => 'character', data => chr $code};
1674        } # X        } # X
1675      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_input_character} and
1676               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_input_character} <= 0x0039) { # 0..9
# Line 1650  sub _tokenize_attempt_to_consume_an_enti Line 1691  sub _tokenize_attempt_to_consume_an_enti
1691          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc');
1692        }        }
1693    
1694        ## TODO: check the definition for |a valid Unicode character|.        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1695        if ($code > 1114111 or $code == 0) {          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1696          $code = 0xFFFD; # REPLACEMENT CHARACTER          $code = 0xFFFD;
1697          ## ISSUE: Why this is not an error?        } elsif ($code > 0x10FFFF) {
1698            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1699            $code = 0xFFFD;
1700          } elsif ($code == 0x000D) {
1701            !!!parse-error (type => 'CR character reference');
1702            $code = 0x000A;
1703        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
1704          !!!parse-error (type => sprintf 'c1 entity:U+%04X', $code);          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
1705          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
1706        }        }
1707                
# Line 1674  sub _tokenize_attempt_to_consume_an_enti Line 1720  sub _tokenize_attempt_to_consume_an_enti
1720      !!!next-input-character;      !!!next-input-character;
1721    
1722      my $value = $entity_name;      my $value = $entity_name;
1723      my $match;      my $match = 0;
1724      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
1725      our $EntityChar;      our $EntityChar;
1726    
# Line 1689  sub _tokenize_attempt_to_consume_an_enti Line 1735  sub _tokenize_attempt_to_consume_an_enti
1735              $self->{next_input_character} == 0x003B)) { # ;              $self->{next_input_character} == 0x003B)) { # ;
1736        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_input_character};
1737        if (defined $EntityChar->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
         $value = $EntityChar->{$entity_name};  
1738          if ($self->{next_input_character} == 0x003B) { # ;          if ($self->{next_input_character} == 0x003B) { # ;
1739              $value = $EntityChar->{$entity_name};
1740            $match = 1;            $match = 1;
1741            !!!next-input-character;            !!!next-input-character;
1742            last;            last;
1743          } else {          } else {
1744              $value = $EntityChar->{$entity_name};
1745            $match = -1;            $match = -1;
1746              !!!next-input-character;
1747          }          }
1748        } else {        } else {
1749          $value .= chr $self->{next_input_character};          $value .= chr $self->{next_input_character};
1750            $match *= 2;
1751            !!!next-input-character;
1752        }        }
       !!!next-input-character;  
1753      }      }
1754            
1755      if ($match > 0) {      if ($match > 0) {
1756        return {type => 'character', data => $value};        return {type => 'character', data => $value};
1757      } elsif ($match < 0) {      } elsif ($match < 0) {
1758        !!!parse-error (type => 'refc');        !!!parse-error (type => 'no refc');
1759        return {type => 'character', data => $value};        if ($in_attr and $match < -1) {
1760            return {type => 'character', data => '&'.$entity_name};
1761          } else {
1762            return {type => 'character', data => $value};
1763          }
1764      } else {      } else {
1765        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
1766        ## NOTE: No characters are consumed in the spec.        ## NOTE: No characters are consumed in the spec.
1767        !!!back-token ({type => 'character', data => $value});        return {type => 'character', data => '&'.$value};
       return undef;  
1768      }      }
1769    } else {    } else {
1770      ## no characters are consumed      ## no characters are consumed
# Line 1907  sub _tree_construction_initial ($) { Line 1959  sub _tree_construction_initial ($) {
1959      } elsif ($token->{type} eq 'character') {      } elsif ($token->{type} eq 'character') {
1960        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
1961          ## Ignore the token          ## Ignore the token
1962    
1963          unless (length $token->{data}) {          unless (length $token->{data}) {
1964            ## Stay in the phase            ## Stay in the phase
1965            !!!next-token;            !!!next-token;
# Line 1949  sub _tree_construction_root_element ($) Line 2002  sub _tree_construction_root_element ($)
2002          !!!next-token;          !!!next-token;
2003          redo B;          redo B;
2004        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} eq 'character') {
2005          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2006            $self->{document}->manakai_append_text ($1);            ## Ignore the token.
2007            ## ISSUE: DOM3 Core does not allow Document > Text  
2008            unless (length $token->{data}) {            unless (length $token->{data}) {
2009              ## Stay in the phase              ## Stay in the phase
2010              !!!next-token;              !!!next-token;
# Line 1972  sub _tree_construction_root_element ($) Line 2025  sub _tree_construction_root_element ($)
2025        my $root_element; !!!create-element ($root_element, 'html');        my $root_element; !!!create-element ($root_element, 'html');
2026        $self->{document}->append_child ($root_element);        $self->{document}->append_child ($root_element);
2027        push @{$self->{open_elements}}, [$root_element, 'html'];        push @{$self->{open_elements}}, [$root_element, 'html'];
       #$phase = 'main';  
2028        ## reprocess        ## reprocess
2029        #redo B;        #redo B;
2030        return;        return; ## Go to the main phase.
2031    } # B    } # B
2032  } # _tree_construction_root_element  } # _tree_construction_root_element
2033    
# Line 1991  sub _reset_insertion_mode ($) { Line 2043  sub _reset_insertion_mode ($) {
2043            
2044      ## Step 3      ## Step 3
2045      S3: {      S3: {
2046        $last = 1 if $self->{open_elements}->[0]->[0] eq $node->[0];        ## ISSUE: Oops! "If node is the first node in the stack of open
2047        if (defined $self->{inner_html_node}) {        ## elements, then set last to true. If the context element of the
2048          if ($self->{inner_html_node}->[1] eq 'td' or        ## HTML fragment parsing algorithm is neither a td element nor a
2049              $self->{inner_html_node}->[1] eq 'th') {        ## th element, then set node to the context element. (fragment case)":
2050            #        ## The second "if" is in the scope of the first "if"!?
2051          } else {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2052            $node = $self->{inner_html_node};          $last = 1;
2053            if (defined $self->{inner_html_node}) {
2054              if ($self->{inner_html_node}->[1] eq 'td' or
2055                  $self->{inner_html_node}->[1] eq 'th') {
2056                #
2057              } else {
2058                $node = $self->{inner_html_node};
2059              }
2060          }          }
2061        }        }
2062            
# Line 2008  sub _reset_insertion_mode ($) { Line 2067  sub _reset_insertion_mode ($) {
2067                        th => 'in cell',                        th => 'in cell',
2068                        tr => 'in row',                        tr => 'in row',
2069                        tbody => 'in table body',                        tbody => 'in table body',
2070                        thead => 'in table head',                        thead => 'in table body',
2071                        tfoot => 'in table foot',                        tfoot => 'in table body',
2072                        caption => 'in caption',                        caption => 'in caption',
2073                        colgroup => 'in column group',                        colgroup => 'in column group',
2074                        table => 'in table',                        table => 'in table',
# Line 2044  sub _reset_insertion_mode ($) { Line 2103  sub _reset_insertion_mode ($) {
2103  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
2104    my $self = shift;    my $self = shift;
2105    
   my $phase = 'main';  
   
2106    my $active_formatting_elements = [];    my $active_formatting_elements = [];
2107    
2108    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 2128  sub _tree_construction_main ($) { Line 2185  sub _tree_construction_main ($) {
2185      }      }
2186    }; # $clear_up_to_marker    }; # $clear_up_to_marker
2187    
2188    my $style_start_tag = sub {    my $parse_rcdata = sub ($$) {
2189      my $style_el; !!!create-element ($style_el, 'style', $token->{attributes});      my ($content_model_flag, $insert) = @_;
2190      ## $self->{insertion_mode} eq 'in head' and ... (always true)  
2191      (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})      ## Step 1
2192       ? $self->{head_element} : $self->{open_elements}->[-1]->[0])      my $start_tag_name = $token->{tag_name};
2193        ->append_child ($style_el);      my $el;
2194      $self->{content_model_flag} = 'CDATA';      !!!create-element ($el, $start_tag_name, $token->{attributes});
2195    
2196        ## Step 2
2197        $insert->($el); # /context node/->append_child ($el)
2198    
2199        ## Step 3
2200        $self->{content_model} = $content_model_flag; # CDATA or RCDATA
2201      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2202                  
2203        ## Step 4
2204      my $text = '';      my $text = '';
2205      !!!next-token;      !!!next-token;
2206      while ($token->{type} eq 'character') {      while ($token->{type} eq 'character') { # or until stop tokenizing
2207        $text .= $token->{data};        $text .= $token->{data};
2208        !!!next-token;        !!!next-token;
2209      } # stop if non-character token or tokenizer stops tokenising      }
2210    
2211        ## Step 5
2212      if (length $text) {      if (length $text) {
2213        $style_el->manakai_append_text ($text);        my $text = $self->{document}->create_text_node ($text);
2214          $el->append_child ($text);
2215      }      }
2216        
2217      $self->{content_model_flag} = 'PCDATA';      ## Step 6
2218                      $self->{content_model} = PCDATA_CONTENT_MODEL;
2219      if ($token->{type} eq 'end tag' and $token->{tag_name} eq 'style') {  
2220        ## Step 7
2221        if ($token->{type} eq 'end tag' and $token->{tag_name} eq $start_tag_name) {
2222        ## Ignore the token        ## Ignore the token
2223      } else {      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2224        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type});
2225        ## ISSUE: And ignore?      } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
2226          !!!parse-error (type => 'in RCDATA:#'.$token->{type});
2227        } else {
2228          die "$0: $content_model_flag in parse_rcdata";
2229      }      }
2230      !!!next-token;      !!!next-token;
2231    }; # $style_start_tag    }; # $parse_rcdata
2232    
2233    my $script_start_tag = sub {    my $script_start_tag = sub ($) {
2234        my $insert = $_[0];
2235      my $script_el;      my $script_el;
2236      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes});
2237      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
2238    
2239      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
2240      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2241            
2242      my $text = '';      my $text = '';
# Line 2176  sub _tree_construction_main ($) { Line 2249  sub _tree_construction_main ($) {
2249        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
2250      }      }
2251                                
2252      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2253    
2254      if ($token->{type} eq 'end tag' and      if ($token->{type} eq 'end tag' and
2255          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
# Line 2192  sub _tree_construction_main ($) { Line 2265  sub _tree_construction_main ($) {
2265      } else {      } else {
2266        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
2267        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
2268          
2269        (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})        $insert->($script_el);
        ? $self->{head_element} : $self->{open_elements}->[-1]->[0])->append_child ($script_el);  
2270                
2271        ## TODO: insertion point = $old_insertion_point (might be "undefined")        ## TODO: insertion point = $old_insertion_point (might be "undefined")
2272                
# Line 2388  sub _tree_construction_main ($) { Line 2460  sub _tree_construction_main ($) {
2460    }; # $formatting_end_tag    }; # $formatting_end_tag
2461    
2462    my $insert_to_current = sub {    my $insert_to_current = sub {
2463      $self->{open_elements}->[-1]->[0]->append_child (shift);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
2464    }; # $insert_to_current    }; # $insert_to_current
2465    
2466    my $insert_to_foster = sub {    my $insert_to_foster = sub {
# Line 2422  sub _tree_construction_main ($) { Line 2494  sub _tree_construction_main ($) {
2494                         }                         }
2495    }; # $insert_to_foster    }; # $insert_to_foster
2496    
2497    my $in_body = sub {    my $insert;
2498      my $insert = shift;  
2499      if ($token->{type} eq 'start tag') {    B: {
2500        if ($token->{tag_name} eq 'script') {      if ($token->{type} eq 'DOCTYPE') {
2501          $script_start_tag->();        !!!parse-error (type => 'DOCTYPE in the middle');
2502          return;        ## Ignore the token
2503        } elsif ($token->{tag_name} eq 'style') {        ## Stay in the phase
2504          $style_start_tag->();        !!!next-token;
2505          return;        redo B;
2506        } elsif ({      } elsif ($token->{type} eq 'end-of-file') {
2507                  base => 1, link => 1, meta => 1,        if ($self->{insertion_mode} eq 'after html body' or
2508                 }->{$token->{tag_name}}) {            $self->{insertion_mode} eq 'after html frameset') {
2509          ## NOTE: This is an "as if in head" code clone          #
2510          my $el;        } else {
2511          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          ## Generate implied end tags
2512          if ($self->{insertion_mode} eq 'in head' and          if ({
2513              defined $self->{head_element}) {               dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,
2514            $self->{head_element}->append_child ($el);               tbody => 1, tfoot=> 1, thead => 1,
2515          } else {              }->{$self->{open_elements}->[-1]->[1]}) {
2516            $insert->($el);            !!!back-token;
2517              $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};
2518              redo B;
2519          }          }
2520                    
2521          !!!next-token;          if (@{$self->{open_elements}} > 2 or
2522          return;              (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {
2523        } elsif ($token->{tag_name} eq 'title') {            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
2524          !!!parse-error (type => 'in body:title');          } elsif (defined $self->{inner_html_node} and
2525          ## NOTE: There is an "as if in head" code clone                   @{$self->{open_elements}} > 1 and
2526          my $title_el;                   $self->{open_elements}->[1]->[1] ne 'body') {
2527          !!!create-element ($title_el, 'title', $token->{attributes});            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
2528          (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])          }
2529            ->append_child ($title_el);  
2530          $self->{content_model_flag} = 'RCDATA';          ## ISSUE: There is an issue in the spec.
2531          delete $self->{escape}; # MUST        }
2532            
2533          my $text = '';        ## Stop parsing
2534          !!!next-token;        last B;
2535          while ($token->{type} eq 'character') {      } elsif ($token->{type} eq 'start tag' and
2536            $text .= $token->{data};               $token->{tag_name} eq 'html') {
2537          if ($self->{insertion_mode} eq 'after html body') {
2538            ## Turn into the main phase
2539            !!!parse-error (type => 'after html:html');
2540            $self->{insertion_mode} = 'after body';
2541          } elsif ($self->{insertion_mode} eq 'after html frameset') {
2542            ## Turn into the main phase
2543            !!!parse-error (type => 'after html:html');
2544            $self->{insertion_mode} = 'after frameset';
2545          }
2546    
2547    ## ISSUE: "aa<html>" is not a parse error.
2548    ## ISSUE: "<html>" in fragment is not a parse error.
2549          unless ($token->{first_start_tag}) {
2550            !!!parse-error (type => 'not first start tag');
2551          }
2552          my $top_el = $self->{open_elements}->[0]->[0];
2553          for my $attr_name (keys %{$token->{attributes}}) {
2554            unless ($top_el->has_attribute_ns (undef, $attr_name)) {
2555              $top_el->set_attribute_ns
2556                (undef, [undef, $attr_name],
2557                 $token->{attributes}->{$attr_name}->{value});
2558            }
2559          }
2560          !!!next-token;
2561          redo B;
2562        } elsif ($token->{type} eq 'comment') {
2563          my $comment = $self->{document}->create_comment ($token->{data});
2564          if ($self->{insertion_mode} eq 'after html body' or
2565              $self->{insertion_mode} eq 'after html frameset') {
2566            $self->{document}->append_child ($comment);
2567          } elsif ($self->{insertion_mode} eq 'after body') {
2568            $self->{open_elements}->[0]->[0]->append_child ($comment);
2569          } else {
2570            $self->{open_elements}->[-1]->[0]->append_child ($comment);
2571          }
2572          !!!next-token;
2573          redo B;
2574        } elsif ($self->{insertion_mode} eq 'in head' or
2575                 $self->{insertion_mode} eq 'in head noscript' or
2576                 $self->{insertion_mode} eq 'after head' or
2577                 $self->{insertion_mode} eq 'before head') {
2578          if ($token->{type} eq 'character') {
2579            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
2580              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
2581              unless (length $token->{data}) {
2582                !!!next-token;
2583                redo B;
2584              }
2585            }
2586    
2587            if ($self->{insertion_mode} eq 'before head') {
2588              ## As if <head>
2589              !!!create-element ($self->{head_element}, 'head');
2590              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2591              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2592    
2593              ## Reprocess in the "in head" insertion mode...
2594              pop @{$self->{open_elements}};
2595    
2596              ## Reprocess in the "after head" insertion mode...
2597            } elsif ($self->{insertion_mode} eq 'in head noscript') {
2598              ## As if </noscript>
2599              pop @{$self->{open_elements}};
2600              !!!parse-error (type => 'in noscript:#character');
2601              
2602              ## Reprocess in the "in head" insertion mode...
2603              ## As if </head>
2604              pop @{$self->{open_elements}};
2605    
2606              ## Reprocess in the "after head" insertion mode...
2607            } elsif ($self->{insertion_mode} eq 'in head') {
2608              pop @{$self->{open_elements}};
2609    
2610              ## Reprocess in the "after head" insertion mode...
2611            }
2612    
2613                ## "after head" insertion mode
2614                ## As if <body>
2615                !!!insert-element ('body');
2616                $self->{insertion_mode} = 'in body';
2617                ## reprocess
2618                redo B;
2619              } elsif ($token->{type} eq 'start tag') {
2620                if ($token->{tag_name} eq 'head') {
2621                  if ($self->{insertion_mode} eq 'before head') {
2622                    !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
2623                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2624                    push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
2625                    $self->{insertion_mode} = 'in head';
2626                    !!!next-token;
2627                    redo B;
2628                  } elsif ($self->{insertion_mode} ne 'after head') {
2629                    !!!parse-error (type => 'in head:head'); # or in head noscript
2630                    ## Ignore the token
2631                    !!!next-token;
2632                    redo B;
2633                  } else {
2634                    #
2635                  }
2636                } elsif ($self->{insertion_mode} eq 'before head') {
2637                  ## As if <head>
2638                  !!!create-element ($self->{head_element}, 'head');
2639                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2640                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2641    
2642                  $self->{insertion_mode} = 'in head';
2643                  ## Reprocess in the "in head" insertion mode...
2644                }
2645    
2646                if ($token->{tag_name} eq 'base') {
2647                  if ($self->{insertion_mode} eq 'in head noscript') {
2648                    ## As if </noscript>
2649                    pop @{$self->{open_elements}};
2650                    !!!parse-error (type => 'in noscript:base');
2651                  
2652                    $self->{insertion_mode} = 'in head';
2653                    ## Reprocess in the "in head" insertion mode...
2654                  }
2655    
2656                  ## NOTE: There is a "as if in head" code clone.
2657                  if ($self->{insertion_mode} eq 'after head') {
2658                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2659                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2660                  }
2661                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2662                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2663                  pop @{$self->{open_elements}}
2664                      if $self->{insertion_mode} eq 'after head';
2665                  !!!next-token;
2666                  redo B;
2667                } elsif ($token->{tag_name} eq 'link') {
2668                  ## NOTE: There is a "as if in head" code clone.
2669                  if ($self->{insertion_mode} eq 'after head') {
2670                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2671                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2672                  }
2673                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2674                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2675                  pop @{$self->{open_elements}}
2676                      if $self->{insertion_mode} eq 'after head';
2677                  !!!next-token;
2678                  redo B;
2679                } elsif ($token->{tag_name} eq 'meta') {
2680                  ## NOTE: There is a "as if in head" code clone.
2681                  if ($self->{insertion_mode} eq 'after head') {
2682                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2683                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2684                  }
2685                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2686                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2687    
2688                  unless ($self->{confident}) {
2689                    my $charset;
2690                    if ($token->{attributes}->{charset}) { ## TODO: And if supported
2691                      $charset = $token->{attributes}->{charset}->{value};
2692                    }
2693                    if ($token->{attributes}->{'http-equiv'}) {
2694                      ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
2695                      if ($token->{attributes}->{'http-equiv'}->{value}
2696                          =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
2697                              [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2698                              ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
2699                        $charset = defined $1 ? $1 : defined $2 ? $2 : $3;
2700                      } ## TODO: And if supported
2701                    }
2702                    ## TODO: Change the encoding
2703                  }
2704    
2705                  ## TODO: Extracting |charset| from |meta|.
2706                  pop @{$self->{open_elements}}
2707                      if $self->{insertion_mode} eq 'after head';
2708                  !!!next-token;
2709                  redo B;
2710                } elsif ($token->{tag_name} eq 'title') {
2711                  if ($self->{insertion_mode} eq 'in head noscript') {
2712                    ## As if </noscript>
2713                    pop @{$self->{open_elements}};
2714                    !!!parse-error (type => 'in noscript:title');
2715                  
2716                    $self->{insertion_mode} = 'in head';
2717                    ## Reprocess in the "in head" insertion mode...
2718                  } elsif ($self->{insertion_mode} eq 'after head') {
2719                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2720                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2721                  }
2722    
2723                  ## NOTE: There is a "as if in head" code clone.
2724                  my $parent = defined $self->{head_element} ? $self->{head_element}
2725                      : $self->{open_elements}->[-1]->[0];
2726                  $parse_rcdata->(RCDATA_CONTENT_MODEL,
2727                                  sub { $parent->append_child ($_[0]) });
2728                  pop @{$self->{open_elements}}
2729                      if $self->{insertion_mode} eq 'after head';
2730                  redo B;
2731                } elsif ($token->{tag_name} eq 'style') {
2732                  ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
2733                  ## insertion mode 'in head')
2734                  ## NOTE: There is a "as if in head" code clone.
2735                  if ($self->{insertion_mode} eq 'after head') {
2736                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2737                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2738                  }
2739                  $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
2740                  pop @{$self->{open_elements}}
2741                      if $self->{insertion_mode} eq 'after head';
2742                  redo B;
2743                } elsif ($token->{tag_name} eq 'noscript') {
2744                  if ($self->{insertion_mode} eq 'in head') {
2745                    ## NOTE: and scripting is disalbed
2746                    !!!insert-element ($token->{tag_name}, $token->{attributes});
2747                    $self->{insertion_mode} = 'in head noscript';
2748                    !!!next-token;
2749                    redo B;
2750                  } elsif ($self->{insertion_mode} eq 'in head noscript') {
2751                    !!!parse-error (type => 'in noscript:noscript');
2752                    ## Ignore the token
2753                    !!!next-token;
2754                    redo B;
2755                  } else {
2756                    #
2757                  }
2758                } elsif ($token->{tag_name} eq 'script') {
2759                  if ($self->{insertion_mode} eq 'in head noscript') {
2760                    ## As if </noscript>
2761                    pop @{$self->{open_elements}};
2762                    !!!parse-error (type => 'in noscript:script');
2763                  
2764                    $self->{insertion_mode} = 'in head';
2765                    ## Reprocess in the "in head" insertion mode...
2766                  } elsif ($self->{insertion_mode} eq 'after head') {
2767                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2768                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2769                  }
2770    
2771                  ## NOTE: There is a "as if in head" code clone.
2772                  $script_start_tag->($insert_to_current);
2773                  pop @{$self->{open_elements}}
2774                      if $self->{insertion_mode} eq 'after head';
2775                  redo B;
2776                } elsif ($token->{tag_name} eq 'body' or
2777                         $token->{tag_name} eq 'frameset') {
2778                  if ($self->{insertion_mode} eq 'in head noscript') {
2779                    ## As if </noscript>
2780                    pop @{$self->{open_elements}};
2781                    !!!parse-error (type => 'in noscript:'.$token->{tag_name});
2782                    
2783                    ## Reprocess in the "in head" insertion mode...
2784                    ## As if </head>
2785                    pop @{$self->{open_elements}};
2786                    
2787                    ## Reprocess in the "after head" insertion mode...
2788                  } elsif ($self->{insertion_mode} eq 'in head') {
2789                    pop @{$self->{open_elements}};
2790                    
2791                    ## Reprocess in the "after head" insertion mode...
2792                  }
2793    
2794                  ## "after head" insertion mode
2795                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2796                  $self->{insertion_mode} = 'in '.$token->{tag_name};
2797                  !!!next-token;
2798                  redo B;
2799                } else {
2800                  #
2801                }
2802    
2803                if ($self->{insertion_mode} eq 'in head noscript') {
2804                  ## As if </noscript>
2805                  pop @{$self->{open_elements}};
2806                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
2807                  
2808                  ## Reprocess in the "in head" insertion mode...
2809                  ## As if </head>
2810                  pop @{$self->{open_elements}};
2811    
2812                  ## Reprocess in the "after head" insertion mode...
2813                } elsif ($self->{insertion_mode} eq 'in head') {
2814                  ## As if </head>
2815                  pop @{$self->{open_elements}};
2816    
2817                  ## Reprocess in the "after head" insertion mode...
2818                }
2819    
2820                ## "after head" insertion mode
2821                ## As if <body>
2822                !!!insert-element ('body');
2823                $self->{insertion_mode} = 'in body';
2824                ## reprocess
2825                redo B;
2826              } elsif ($token->{type} eq 'end tag') {
2827                if ($token->{tag_name} eq 'head') {
2828                  if ($self->{insertion_mode} eq 'before head') {
2829                    ## As if <head>
2830                    !!!create-element ($self->{head_element}, 'head');
2831                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2832                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2833    
2834                    ## Reprocess in the "in head" insertion mode...
2835                    pop @{$self->{open_elements}};
2836                    $self->{insertion_mode} = 'after head';
2837                    !!!next-token;
2838                    redo B;
2839                  } elsif ($self->{insertion_mode} eq 'in head noscript') {
2840                    ## As if </noscript>
2841                    pop @{$self->{open_elements}};
2842                    !!!parse-error (type => 'in noscript:script');
2843                    
2844                    ## Reprocess in the "in head" insertion mode...
2845                    pop @{$self->{open_elements}};
2846                    $self->{insertion_mode} = 'after head';
2847                    !!!next-token;
2848                    redo B;
2849                  } elsif ($self->{insertion_mode} eq 'in head') {
2850                    pop @{$self->{open_elements}};
2851                    $self->{insertion_mode} = 'after head';
2852                    !!!next-token;
2853                    redo B;
2854                  } else {
2855                    #
2856                  }
2857                } elsif ($token->{tag_name} eq 'noscript') {
2858                  if ($self->{insertion_mode} eq 'in head noscript') {
2859                    pop @{$self->{open_elements}};
2860                    $self->{insertion_mode} = 'in head';
2861                    !!!next-token;
2862                    redo B;
2863                  } elsif ($self->{insertion_mode} eq 'before head') {
2864                    !!!parse-error (type => 'unmatched end tag:noscript');
2865                    ## Ignore the token ## ISSUE: An issue in the spec.
2866                    !!!next-token;
2867                    redo B;
2868                  } else {
2869                    #
2870                  }
2871                } elsif ({
2872                          body => 1, html => 1,
2873                         }->{$token->{tag_name}}) {
2874                  if ($self->{insertion_mode} eq 'before head') {
2875                    ## As if <head>
2876                    !!!create-element ($self->{head_element}, 'head');
2877                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2878                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2879    
2880                    $self->{insertion_mode} = 'in head';
2881                    ## Reprocess in the "in head" insertion mode...
2882                  } elsif ($self->{insertion_mode} eq 'in head noscript') {
2883                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2884                    ## Ignore the token
2885                    !!!next-token;
2886                    redo B;
2887                  }
2888                  
2889                  #
2890                } elsif ({
2891                          p => 1, br => 1,
2892                         }->{$token->{tag_name}}) {
2893                  if ($self->{insertion_mode} eq 'before head') {
2894                    ## As if <head>
2895                    !!!create-element ($self->{head_element}, 'head');
2896                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2897                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2898    
2899                    $self->{insertion_mode} = 'in head';
2900                    ## Reprocess in the "in head" insertion mode...
2901                  }
2902    
2903                  #
2904                } else {
2905                  if ($self->{insertion_mode} ne 'after head') {
2906                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2907                    ## Ignore the token
2908                    !!!next-token;
2909                    redo B;
2910                  } else {
2911                    #
2912                  }
2913                }
2914    
2915                if ($self->{insertion_mode} eq 'in head noscript') {
2916                  ## As if </noscript>
2917                  pop @{$self->{open_elements}};
2918                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
2919                  
2920                  ## Reprocess in the "in head" insertion mode...
2921                  ## As if </head>
2922                  pop @{$self->{open_elements}};
2923    
2924                  ## Reprocess in the "after head" insertion mode...
2925                } elsif ($self->{insertion_mode} eq 'in head') {
2926                  ## As if </head>
2927                  pop @{$self->{open_elements}};
2928    
2929                  ## Reprocess in the "after head" insertion mode...
2930                } elsif ($self->{insertion_mode} eq 'before head') {
2931                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2932                  ## Ignore the token ## ISSUE: An issue in the spec.
2933                  !!!next-token;
2934                  redo B;
2935                }
2936    
2937                ## "after head" insertion mode
2938                ## As if <body>
2939                !!!insert-element ('body');
2940                $self->{insertion_mode} = 'in body';
2941                ## reprocess
2942                redo B;
2943              } else {
2944                die "$0: $token->{type}: Unknown token type";
2945              }
2946    
2947              ## ISSUE: An issue in the spec.
2948        } elsif ($self->{insertion_mode} eq 'in body' or
2949                 $self->{insertion_mode} eq 'in cell' or
2950                 $self->{insertion_mode} eq 'in caption') {
2951              if ($token->{type} eq 'character') {
2952                ## NOTE: There is a code clone of "character in body".
2953                $reconstruct_active_formatting_elements->($insert_to_current);
2954                
2955                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
2956    
2957                !!!next-token;
2958                redo B;
2959              } elsif ($token->{type} eq 'start tag') {
2960                if ({
2961                     caption => 1, col => 1, colgroup => 1, tbody => 1,
2962                     td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
2963                    }->{$token->{tag_name}}) {
2964                  if ($self->{insertion_mode} eq 'in cell') {
2965                    ## have an element in table scope
2966                    my $tn;
2967                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
2968                      my $node = $self->{open_elements}->[$_];
2969                      if ($node->[1] eq 'td' or $node->[1] eq 'th') {
2970                        $tn = $node->[1];
2971                        last INSCOPE;
2972                      } elsif ({
2973                                table => 1, html => 1,
2974                               }->{$node->[1]}) {
2975                        last INSCOPE;
2976                      }
2977                    } # INSCOPE
2978                      unless (defined $tn) {
2979                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2980                        ## Ignore the token
2981                        !!!next-token;
2982                        redo B;
2983                      }
2984                    
2985                    ## Close the cell
2986                    !!!back-token; # <?>
2987                    $token = {type => 'end tag', tag_name => $tn};
2988                    redo B;
2989                  } elsif ($self->{insertion_mode} eq 'in caption') {
2990                    !!!parse-error (type => 'not closed:caption');
2991                    
2992                    ## As if </caption>
2993                    ## have a table element in table scope
2994                    my $i;
2995                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
2996                      my $node = $self->{open_elements}->[$_];
2997                      if ($node->[1] eq 'caption') {
2998                        $i = $_;
2999                        last INSCOPE;
3000                      } elsif ({
3001                                table => 1, html => 1,
3002                               }->{$node->[1]}) {
3003                        last INSCOPE;
3004                      }
3005                    } # INSCOPE
3006                      unless (defined $i) {
3007                        !!!parse-error (type => 'unmatched end tag:caption');
3008                        ## Ignore the token
3009                        !!!next-token;
3010                        redo B;
3011                      }
3012                    
3013                    ## generate implied end tags
3014                    if ({
3015                         dd => 1, dt => 1, li => 1, p => 1,
3016                         td => 1, th => 1, tr => 1,
3017                         tbody => 1, tfoot=> 1, thead => 1,
3018                        }->{$self->{open_elements}->[-1]->[1]}) {
3019                      !!!back-token; # <?>
3020                      $token = {type => 'end tag', tag_name => 'caption'};
3021                      !!!back-token;
3022                      $token = {type => 'end tag',
3023                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3024                      redo B;
3025                    }
3026    
3027                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3028                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3029                    }
3030                    
3031                    splice @{$self->{open_elements}}, $i;
3032                    
3033                    $clear_up_to_marker->();
3034                    
3035                    $self->{insertion_mode} = 'in table';
3036                    
3037                    ## reprocess
3038                    redo B;
3039                  } else {
3040                    #
3041                  }
3042                } else {
3043                  #
3044                }
3045              } elsif ($token->{type} eq 'end tag') {
3046                if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3047                  if ($self->{insertion_mode} eq 'in cell') {
3048                    ## have an element in table scope
3049                    my $i;
3050                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3051                      my $node = $self->{open_elements}->[$_];
3052                      if ($node->[1] eq $token->{tag_name}) {
3053                        $i = $_;
3054                        last INSCOPE;
3055                      } elsif ({
3056                                table => 1, html => 1,
3057                               }->{$node->[1]}) {
3058                        last INSCOPE;
3059                      }
3060                    } # INSCOPE
3061                      unless (defined $i) {
3062                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3063                        ## Ignore the token
3064                        !!!next-token;
3065                        redo B;
3066                      }
3067                    
3068                    ## generate implied end tags
3069                    if ({
3070                         dd => 1, dt => 1, li => 1, p => 1,
3071                         td => ($token->{tag_name} eq 'th'),
3072                         th => ($token->{tag_name} eq 'td'),
3073                         tr => 1,
3074                         tbody => 1, tfoot=> 1, thead => 1,
3075                        }->{$self->{open_elements}->[-1]->[1]}) {
3076                      !!!back-token;
3077                      $token = {type => 'end tag',
3078                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3079                      redo B;
3080                    }
3081                    
3082                    if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3083                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3084                    }
3085                    
3086                    splice @{$self->{open_elements}}, $i;
3087                    
3088                    $clear_up_to_marker->();
3089                    
3090                    $self->{insertion_mode} = 'in row';
3091                    
3092                    !!!next-token;
3093                    redo B;
3094                  } elsif ($self->{insertion_mode} eq 'in caption') {
3095                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3096                    ## Ignore the token
3097                    !!!next-token;
3098                    redo B;
3099                  } else {
3100                    #
3101                  }
3102                } elsif ($token->{tag_name} eq 'caption') {
3103                  if ($self->{insertion_mode} eq 'in caption') {
3104                    ## have a table element in table scope
3105                    my $i;
3106                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3107                      my $node = $self->{open_elements}->[$_];
3108                      if ($node->[1] eq $token->{tag_name}) {
3109                        $i = $_;
3110                        last INSCOPE;
3111                      } elsif ({
3112                                table => 1, html => 1,
3113                               }->{$node->[1]}) {
3114                        last INSCOPE;
3115                      }
3116                    } # INSCOPE
3117                      unless (defined $i) {
3118                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3119                        ## Ignore the token
3120                        !!!next-token;
3121                        redo B;
3122                      }
3123                    
3124                    ## generate implied end tags
3125                    if ({
3126                         dd => 1, dt => 1, li => 1, p => 1,
3127                         td => 1, th => 1, tr => 1,
3128                         tbody => 1, tfoot=> 1, thead => 1,
3129                        }->{$self->{open_elements}->[-1]->[1]}) {
3130                      !!!back-token;
3131                      $token = {type => 'end tag',
3132                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3133                      redo B;
3134                    }
3135                    
3136                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3137                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3138                    }
3139                    
3140                    splice @{$self->{open_elements}}, $i;
3141                    
3142                    $clear_up_to_marker->();
3143                    
3144                    $self->{insertion_mode} = 'in table';
3145                    
3146                    !!!next-token;
3147                    redo B;
3148                  } elsif ($self->{insertion_mode} eq 'in cell') {
3149                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3150                    ## Ignore the token
3151                    !!!next-token;
3152                    redo B;
3153                  } else {
3154                    #
3155                  }
3156                } elsif ({
3157                          table => 1, tbody => 1, tfoot => 1,
3158                          thead => 1, tr => 1,
3159                         }->{$token->{tag_name}} and
3160                         $self->{insertion_mode} eq 'in cell') {
3161                  ## have an element in table scope
3162                  my $i;
3163                  my $tn;
3164                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3165                    my $node = $self->{open_elements}->[$_];
3166                    if ($node->[1] eq $token->{tag_name}) {
3167                      $i = $_;
3168                      last INSCOPE;
3169                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
3170                      $tn = $node->[1];
3171                      ## NOTE: There is exactly one |td| or |th| element
3172                      ## in scope in the stack of open elements by definition.
3173                    } elsif ({
3174                              table => 1, html => 1,
3175                             }->{$node->[1]}) {
3176                      last INSCOPE;
3177                    }
3178                  } # INSCOPE
3179                  unless (defined $i) {
3180                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3181                    ## Ignore the token
3182                    !!!next-token;
3183                    redo B;
3184                  }
3185    
3186                  ## Close the cell
3187                  !!!back-token; # </?>
3188                  $token = {type => 'end tag', tag_name => $tn};
3189                  redo B;
3190                } elsif ($token->{tag_name} eq 'table' and
3191                         $self->{insertion_mode} eq 'in caption') {
3192                  !!!parse-error (type => 'not closed:caption');
3193    
3194                  ## As if </caption>
3195                  ## have a table element in table scope
3196                  my $i;
3197                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3198                    my $node = $self->{open_elements}->[$_];
3199                    if ($node->[1] eq 'caption') {
3200                      $i = $_;
3201                      last INSCOPE;
3202                    } elsif ({
3203                              table => 1, html => 1,
3204                             }->{$node->[1]}) {
3205                      last INSCOPE;
3206                    }
3207                  } # INSCOPE
3208                  unless (defined $i) {
3209                    !!!parse-error (type => 'unmatched end tag:caption');
3210                    ## Ignore the token
3211                    !!!next-token;
3212                    redo B;
3213                  }
3214                  
3215                  ## generate implied end tags
3216                  if ({
3217                       dd => 1, dt => 1, li => 1, p => 1,
3218                       td => 1, th => 1, tr => 1,
3219                       tbody => 1, tfoot=> 1, thead => 1,
3220                      }->{$self->{open_elements}->[-1]->[1]}) {
3221                    !!!back-token; # </table>
3222                    $token = {type => 'end tag', tag_name => 'caption'};
3223                    !!!back-token;
3224                    $token = {type => 'end tag',
3225                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3226                    redo B;
3227                  }
3228    
3229                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3230                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3231                  }
3232    
3233                  splice @{$self->{open_elements}}, $i;
3234    
3235                  $clear_up_to_marker->();
3236    
3237                  $self->{insertion_mode} = 'in table';
3238    
3239                  ## reprocess
3240                  redo B;
3241                } elsif ({
3242                          body => 1, col => 1, colgroup => 1, html => 1,
3243                         }->{$token->{tag_name}}) {
3244                  if ($self->{insertion_mode} eq 'in cell' or
3245                      $self->{insertion_mode} eq 'in caption') {
3246                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3247                    ## Ignore the token
3248                    !!!next-token;
3249                    redo B;
3250                  } else {
3251                    #
3252                  }
3253                } elsif ({
3254                          tbody => 1, tfoot => 1,
3255                          thead => 1, tr => 1,
3256                         }->{$token->{tag_name}} and
3257                         $self->{insertion_mode} eq 'in caption') {
3258                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3259                  ## Ignore the token
3260                  !!!next-token;
3261                  redo B;
3262                } else {
3263                  #
3264                }
3265          } else {
3266            die "$0: $token->{type}: Unknown token type";
3267          }
3268    
3269          $insert = $insert_to_current;
3270          #
3271        } elsif ($self->{insertion_mode} eq 'in row' or
3272                 $self->{insertion_mode} eq 'in table body' or
3273                 $self->{insertion_mode} eq 'in table') {
3274              if ($token->{type} eq 'character') {
3275                ## NOTE: There are "character in table" code clones.
3276                if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3277                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3278                  
3279                  unless (length $token->{data}) {
3280                    !!!next-token;
3281                    redo B;
3282                  }
3283                }
3284    
3285                !!!parse-error (type => 'in table:#character');
3286    
3287                ## As if in body, but insert into foster parent element
3288                ## ISSUE: Spec says that "whenever a node would be inserted
3289                ## into the current node" while characters might not be
3290                ## result in a new Text node.
3291                $reconstruct_active_formatting_elements->($insert_to_foster);
3292                
3293                if ({
3294                     table => 1, tbody => 1, tfoot => 1,
3295                     thead => 1, tr => 1,
3296                    }->{$self->{open_elements}->[-1]->[1]}) {
3297                  # MUST
3298                  my $foster_parent_element;
3299                  my $next_sibling;
3300                  my $prev_sibling;
3301                  OE: for (reverse 0..$#{$self->{open_elements}}) {
3302                    if ($self->{open_elements}->[$_]->[1] eq 'table') {
3303                      my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3304                      if (defined $parent and $parent->node_type == 1) {
3305                        $foster_parent_element = $parent;
3306                        $next_sibling = $self->{open_elements}->[$_]->[0];
3307                        $prev_sibling = $next_sibling->previous_sibling;
3308                      } else {
3309                        $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
3310                        $prev_sibling = $foster_parent_element->last_child;
3311                      }
3312                      last OE;
3313                    }
3314                  } # OE
3315                  $foster_parent_element = $self->{open_elements}->[0]->[0] and
3316                  $prev_sibling = $foster_parent_element->last_child
3317                    unless defined $foster_parent_element;
3318                  if (defined $prev_sibling and
3319                      $prev_sibling->node_type == 3) {
3320                    $prev_sibling->manakai_append_text ($token->{data});
3321                  } else {
3322                    $foster_parent_element->insert_before
3323                      ($self->{document}->create_text_node ($token->{data}),
3324                       $next_sibling);
3325                  }
3326                } else {
3327                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3328                }
3329                
3330                !!!next-token;
3331                redo B;
3332              } elsif ($token->{type} eq 'start tag') {
3333                if ({
3334                     tr => ($self->{insertion_mode} ne 'in row'),
3335                     th => 1, td => 1,
3336                    }->{$token->{tag_name}}) {
3337                  if ($self->{insertion_mode} eq 'in table') {
3338                    ## Clear back to table context
3339                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3340                           $self->{open_elements}->[-1]->[1] ne 'html') {
3341                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3342                      pop @{$self->{open_elements}};
3343                    }
3344                    
3345                    !!!insert-element ('tbody');
3346                    $self->{insertion_mode} = 'in table body';
3347                    ## reprocess in the "in table body" insertion mode...
3348                  }
3349    
3350                  if ($self->{insertion_mode} eq 'in table body') {
3351                    unless ($token->{tag_name} eq 'tr') {
3352                      !!!parse-error (type => 'missing start tag:tr');
3353                    }
3354                    
3355                    ## Clear back to table body context
3356                    while (not {
3357                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3358                    }->{$self->{open_elements}->[-1]->[1]}) {
3359                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3360                      pop @{$self->{open_elements}};
3361                    }
3362                    
3363                    $self->{insertion_mode} = 'in row';
3364                    if ($token->{tag_name} eq 'tr') {
3365                      !!!insert-element ($token->{tag_name}, $token->{attributes});
3366                      !!!next-token;
3367                      redo B;
3368                    } else {
3369                      !!!insert-element ('tr');
3370                      ## reprocess in the "in row" insertion mode
3371                    }
3372                  }
3373    
3374                  ## Clear back to table row context
3375                  while (not {
3376                    tr => 1, html => 1,
3377                  }->{$self->{open_elements}->[-1]->[1]}) {
3378                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3379                    pop @{$self->{open_elements}};
3380                  }
3381                  
3382                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3383                  $self->{insertion_mode} = 'in cell';
3384    
3385                  push @$active_formatting_elements, ['#marker', ''];
3386                  
3387                  !!!next-token;
3388                  redo B;
3389                } elsif ({
3390                          caption => 1, col => 1, colgroup => 1,
3391                          tbody => 1, tfoot => 1, thead => 1,
3392                          tr => 1, # $self->{insertion_mode} eq 'in row'
3393                         }->{$token->{tag_name}}) {
3394                  if ($self->{insertion_mode} eq 'in row') {
3395                    ## As if </tr>
3396                    ## have an element in table scope
3397                    my $i;
3398                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3399                      my $node = $self->{open_elements}->[$_];
3400                      if ($node->[1] eq 'tr') {
3401                        $i = $_;
3402                        last INSCOPE;
3403                      } elsif ({
3404                                table => 1, html => 1,
3405                               }->{$node->[1]}) {
3406                        last INSCOPE;
3407                      }
3408                    } # INSCOPE
3409                    unless (defined $i) {
3410                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
3411                      ## Ignore the token
3412                      !!!next-token;
3413                      redo B;
3414                    }
3415                    
3416                    ## Clear back to table row context
3417                    while (not {
3418                      tr => 1, html => 1,
3419                    }->{$self->{open_elements}->[-1]->[1]}) {
3420                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3421                      pop @{$self->{open_elements}};
3422                    }
3423                    
3424                    pop @{$self->{open_elements}}; # tr
3425                    $self->{insertion_mode} = 'in table body';
3426                    if ($token->{tag_name} eq 'tr') {
3427                      ## reprocess
3428                      redo B;
3429                    } else {
3430                      ## reprocess in the "in table body" insertion mode...
3431                    }
3432                  }
3433    
3434                  if ($self->{insertion_mode} eq 'in table body') {
3435                    ## have an element in table scope
3436                    my $i;
3437                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3438                      my $node = $self->{open_elements}->[$_];
3439                      if ({
3440                           tbody => 1, thead => 1, tfoot => 1,
3441                          }->{$node->[1]}) {
3442                        $i = $_;
3443                        last INSCOPE;
3444                      } elsif ({
3445                                table => 1, html => 1,
3446                               }->{$node->[1]}) {
3447                        last INSCOPE;
3448                      }
3449                    } # INSCOPE
3450                    unless (defined $i) {
3451                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3452                      ## Ignore the token
3453                      !!!next-token;
3454                      redo B;
3455                    }
3456    
3457                    ## Clear back to table body context
3458                    while (not {
3459                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3460                    }->{$self->{open_elements}->[-1]->[1]}) {
3461                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3462                      pop @{$self->{open_elements}};
3463                    }
3464                    
3465                    ## As if <{current node}>
3466                    ## have an element in table scope
3467                    ## true by definition
3468                    
3469                    ## Clear back to table body context
3470                    ## nop by definition
3471                    
3472                    pop @{$self->{open_elements}};
3473                    $self->{insertion_mode} = 'in table';
3474                    ## reprocess in "in table" insertion mode...
3475                  }
3476    
3477                  if ($token->{tag_name} eq 'col') {
3478                    ## Clear back to table context
3479                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3480                           $self->{open_elements}->[-1]->[1] ne 'html') {
3481                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3482                      pop @{$self->{open_elements}};
3483                    }
3484                    
3485                    !!!insert-element ('colgroup');
3486                    $self->{insertion_mode} = 'in column group';
3487                    ## reprocess
3488                    redo B;
3489                  } elsif ({
3490                            caption => 1,
3491                            colgroup => 1,
3492                            tbody => 1, tfoot => 1, thead => 1,
3493                           }->{$token->{tag_name}}) {
3494                    ## Clear back to table context
3495                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3496                           $self->{open_elements}->[-1]->[1] ne 'html') {
3497                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3498                      pop @{$self->{open_elements}};
3499                    }
3500                    
3501                    push @$active_formatting_elements, ['#marker', '']
3502                        if $token->{tag_name} eq 'caption';
3503                    
3504                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3505                    $self->{insertion_mode} = {
3506                                               caption => 'in caption',
3507                                               colgroup => 'in column group',
3508                                               tbody => 'in table body',
3509                                               tfoot => 'in table body',
3510                                               thead => 'in table body',
3511                                              }->{$token->{tag_name}};
3512                    !!!next-token;
3513                    redo B;
3514                  } else {
3515                    die "$0: in table: <>: $token->{tag_name}";
3516                  }
3517                } elsif ($token->{tag_name} eq 'table') {
3518                  ## NOTE: There are code clones for this "table in table"
3519                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3520    
3521                  ## As if </table>
3522                  ## have a table element in table scope
3523                  my $i;
3524                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3525                    my $node = $self->{open_elements}->[$_];
3526                    if ($node->[1] eq 'table') {
3527                      $i = $_;
3528                      last INSCOPE;
3529                    } elsif ({
3530                              table => 1, html => 1,
3531                             }->{$node->[1]}) {
3532                      last INSCOPE;
3533                    }
3534                  } # INSCOPE
3535                  unless (defined $i) {
3536                    !!!parse-error (type => 'unmatched end tag:table');
3537                    ## Ignore tokens </table><table>
3538                    !!!next-token;
3539                    redo B;
3540                  }
3541                  
3542                  ## generate implied end tags
3543                  if ({
3544                       dd => 1, dt => 1, li => 1, p => 1,
3545                       td => 1, th => 1, tr => 1,
3546                       tbody => 1, tfoot=> 1, thead => 1,
3547                      }->{$self->{open_elements}->[-1]->[1]}) {
3548                    !!!back-token; # <table>
3549                    $token = {type => 'end tag', tag_name => 'table'};
3550                    !!!back-token;
3551                    $token = {type => 'end tag',
3552                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3553                    redo B;
3554                  }
3555    
3556                  if ($self->{open_elements}->[-1]->[1] ne 'table') {
3557                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3558                  }
3559    
3560                  splice @{$self->{open_elements}}, $i;
3561    
3562                  $self->_reset_insertion_mode;
3563    
3564                  ## reprocess
3565                  redo B;
3566                } else {
3567                  #
3568                }
3569              } elsif ($token->{type} eq 'end tag') {
3570                if ($token->{tag_name} eq 'tr' and
3571                    $self->{insertion_mode} eq 'in row') {
3572                  ## have an element in table scope
3573                  my $i;
3574                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3575                    my $node = $self->{open_elements}->[$_];
3576                    if ($node->[1] eq $token->{tag_name}) {
3577                      $i = $_;
3578                      last INSCOPE;
3579                    } elsif ({
3580                              table => 1, html => 1,
3581                             }->{$node->[1]}) {
3582                      last INSCOPE;
3583                    }
3584                  } # INSCOPE
3585                  unless (defined $i) {
3586                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3587                    ## Ignore the token
3588                    !!!next-token;
3589                    redo B;
3590                  }
3591    
3592                  ## Clear back to table row context
3593                  while (not {
3594                    tr => 1, html => 1,
3595                  }->{$self->{open_elements}->[-1]->[1]}) {
3596                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3597                    pop @{$self->{open_elements}};
3598                  }
3599    
3600                  pop @{$self->{open_elements}}; # tr
3601                  $self->{insertion_mode} = 'in table body';
3602                  !!!next-token;
3603                  redo B;
3604                } elsif ($token->{tag_name} eq 'table') {
3605                  if ($self->{insertion_mode} eq 'in row') {
3606                    ## As if </tr>
3607                    ## have an element in table scope
3608                    my $i;
3609                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3610                      my $node = $self->{open_elements}->[$_];
3611                      if ($node->[1] eq 'tr') {
3612                        $i = $_;
3613                        last INSCOPE;
3614                      } elsif ({
3615                                table => 1, html => 1,
3616                               }->{$node->[1]}) {
3617                        last INSCOPE;
3618                      }
3619                    } # INSCOPE
3620                    unless (defined $i) {
3621                      !!!parse-error (type => 'unmatched end tag:'.$token->{type});
3622                      ## Ignore the token
3623                      !!!next-token;
3624                      redo B;
3625                    }
3626                    
3627                    ## Clear back to table row context
3628                    while (not {
3629                      tr => 1, html => 1,
3630                    }->{$self->{open_elements}->[-1]->[1]}) {
3631                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3632                      pop @{$self->{open_elements}};
3633                    }
3634                    
3635                    pop @{$self->{open_elements}}; # tr
3636                    $self->{insertion_mode} = 'in table body';
3637                    ## reprocess in the "in table body" insertion mode...
3638                  }
3639    
3640                  if ($self->{insertion_mode} eq 'in table body') {
3641                    ## have an element in table scope
3642                    my $i;
3643                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3644                      my $node = $self->{open_elements}->[$_];
3645                      if ({
3646                           tbody => 1, thead => 1, tfoot => 1,
3647                          }->{$node->[1]}) {
3648                        $i = $_;
3649                        last INSCOPE;
3650                      } elsif ({
3651                                table => 1, html => 1,
3652                               }->{$node->[1]}) {
3653                        last INSCOPE;
3654                      }
3655                    } # INSCOPE
3656                    unless (defined $i) {
3657                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3658                      ## Ignore the token
3659                      !!!next-token;
3660                      redo B;
3661                    }
3662                    
3663                    ## Clear back to table body context
3664                    while (not {
3665                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3666                    }->{$self->{open_elements}->[-1]->[1]}) {
3667                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3668                      pop @{$self->{open_elements}};
3669                    }
3670                    
3671                    ## As if <{current node}>
3672                    ## have an element in table scope
3673                    ## true by definition
3674                    
3675                    ## Clear back to table body context
3676                    ## nop by definition
3677                    
3678                    pop @{$self->{open_elements}};
3679                    $self->{insertion_mode} = 'in table';
3680                    ## reprocess in the "in table" insertion mode...
3681                  }
3682    
3683                  ## have a table element in table scope
3684                  my $i;
3685                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3686                    my $node = $self->{open_elements}->[$_];
3687                    if ($node->[1] eq $token->{tag_name}) {
3688                      $i = $_;
3689                      last INSCOPE;
3690                    } elsif ({
3691                              table => 1, html => 1,
3692                             }->{$node->[1]}) {
3693                      last INSCOPE;
3694                    }
3695                  } # INSCOPE
3696                  unless (defined $i) {
3697                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3698                    ## Ignore the token
3699                    !!!next-token;
3700                    redo B;
3701                  }
3702    
3703                  ## generate implied end tags
3704                  if ({
3705                       dd => 1, dt => 1, li => 1, p => 1,
3706                       td => 1, th => 1, tr => 1,
3707                       tbody => 1, tfoot=> 1, thead => 1,
3708                      }->{$self->{open_elements}->[-1]->[1]}) {
3709                    !!!back-token;
3710                    $token = {type => 'end tag',
3711                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3712                    redo B;
3713                  }
3714                  
3715                  if ($self->{open_elements}->[-1]->[1] ne 'table') {
3716                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3717                  }
3718                    
3719                  splice @{$self->{open_elements}}, $i;
3720                  
3721                  $self->_reset_insertion_mode;
3722                  
3723                  !!!next-token;
3724                  redo B;
3725                } elsif ({
3726                          tbody => 1, tfoot => 1, thead => 1,
3727                         }->{$token->{tag_name}} and
3728                         ($self->{insertion_mode} eq 'in row' or
3729                          $self->{insertion_mode} eq 'in table body')) {
3730                  if ($self->{insertion_mode} eq 'in row') {
3731                    ## have an element in table scope
3732                    my $i;
3733                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3734                      my $node = $self->{open_elements}->[$_];
3735                      if ($node->[1] eq $token->{tag_name}) {
3736                        $i = $_;
3737                        last INSCOPE;
3738                      } elsif ({
3739                                table => 1, html => 1,
3740                               }->{$node->[1]}) {
3741                        last INSCOPE;
3742                      }
3743                    } # INSCOPE
3744                      unless (defined $i) {
3745                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3746                        ## Ignore the token
3747                        !!!next-token;
3748                        redo B;
3749                      }
3750                    
3751                    ## As if </tr>
3752                    ## have an element in table scope
3753                    my $i;
3754                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3755                      my $node = $self->{open_elements}->[$_];
3756                      if ($node->[1] eq 'tr') {
3757                        $i = $_;
3758                        last INSCOPE;
3759                      } elsif ({
3760                                table => 1, html => 1,
3761                               }->{$node->[1]}) {
3762                        last INSCOPE;
3763                      }
3764                    } # INSCOPE
3765                      unless (defined $i) {
3766                        !!!parse-error (type => 'unmatched end tag:tr');
3767                        ## Ignore the token
3768                        !!!next-token;
3769                        redo B;
3770                      }
3771                    
3772                    ## Clear back to table row context
3773                    while (not {
3774                      tr => 1, html => 1,
3775                    }->{$self->{open_elements}->[-1]->[1]}) {
3776                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3777                      pop @{$self->{open_elements}};
3778                    }
3779                    
3780                    pop @{$self->{open_elements}}; # tr
3781                    $self->{insertion_mode} = 'in table body';
3782                    ## reprocess in the "in table body" insertion mode...
3783                  }
3784    
3785                  ## have an element in table scope
3786                  my $i;
3787                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3788                    my $node = $self->{open_elements}->[$_];
3789                    if ($node->[1] eq $token->{tag_name}) {
3790                      $i = $_;
3791                      last INSCOPE;
3792                    } elsif ({
3793                              table => 1, html => 1,
3794                             }->{$node->[1]}) {
3795                      last INSCOPE;
3796                    }
3797                  } # INSCOPE
3798                  unless (defined $i) {
3799                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3800                    ## Ignore the token
3801                    !!!next-token;
3802                    redo B;
3803                  }
3804    
3805                  ## Clear back to table body context
3806                  while (not {
3807                    tbody => 1, tfoot => 1, thead => 1, html => 1,
3808                  }->{$self->{open_elements}->[-1]->[1]}) {
3809                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3810                    pop @{$self->{open_elements}};
3811                  }
3812    
3813                  pop @{$self->{open_elements}};
3814                  $self->{insertion_mode} = 'in table';
3815                  !!!next-token;
3816                  redo B;
3817                } elsif ({
3818                          body => 1, caption => 1, col => 1, colgroup => 1,
3819                          html => 1, td => 1, th => 1,
3820                          tr => 1, # $self->{insertion_mode} eq 'in row'
3821                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} eq 'in table'
3822                         }->{$token->{tag_name}}) {
3823                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3824                  ## Ignore the token
3825                  !!!next-token;
3826                  redo B;
3827                } else {
3828                  #
3829                }
3830              } else {
3831                die "$0: $token->{type}: Unknown token type";
3832              }
3833    
3834          !!!parse-error (type => 'in table:'.$token->{tag_name});
3835    
3836          $insert = $insert_to_foster;
3837          #
3838        } elsif ($self->{insertion_mode} eq 'in column group') {
3839              if ($token->{type} eq 'character') {
3840                if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3841                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3842                  unless (length $token->{data}) {
3843                    !!!next-token;
3844                    redo B;
3845                  }
3846                }
3847                
3848                #
3849              } elsif ($token->{type} eq 'start tag') {
3850                if ($token->{tag_name} eq 'col') {
3851                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3852                  pop @{$self->{open_elements}};
3853                  !!!next-token;
3854                  redo B;
3855                } else {
3856                  #
3857                }
3858              } elsif ($token->{type} eq 'end tag') {
3859                if ($token->{tag_name} eq 'colgroup') {
3860                  if ($self->{open_elements}->[-1]->[1] eq 'html') {
3861                    !!!parse-error (type => 'unmatched end tag:colgroup');
3862                    ## Ignore the token
3863                    !!!next-token;
3864                    redo B;
3865                  } else {
3866                    pop @{$self->{open_elements}}; # colgroup
3867                    $self->{insertion_mode} = 'in table';
3868                    !!!next-token;
3869                    redo B;            
3870                  }
3871                } elsif ($token->{tag_name} eq 'col') {
3872                  !!!parse-error (type => 'unmatched end tag:col');
3873                  ## Ignore the token
3874                  !!!next-token;
3875                  redo B;
3876                } else {
3877                  #
3878                }
3879              } else {
3880                #
3881              }
3882    
3883              ## As if </colgroup>
3884              if ($self->{open_elements}->[-1]->[1] eq 'html') {
3885                !!!parse-error (type => 'unmatched end tag:colgroup');
3886                ## Ignore the token
3887                !!!next-token;
3888                redo B;
3889              } else {
3890                pop @{$self->{open_elements}}; # colgroup
3891                $self->{insertion_mode} = 'in table';
3892                ## reprocess
3893                redo B;
3894              }
3895        } elsif ($self->{insertion_mode} eq 'in select') {
3896              if ($token->{type} eq 'character') {
3897                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3898                !!!next-token;
3899                redo B;
3900              } elsif ($token->{type} eq 'start tag') {
3901                if ($token->{tag_name} eq 'option') {
3902                  if ($self->{open_elements}->[-1]->[1] eq 'option') {
3903                    ## As if </option>
3904                    pop @{$self->{open_elements}};
3905                  }
3906    
3907                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3908                  !!!next-token;
3909                  redo B;
3910                } elsif ($token->{tag_name} eq 'optgroup') {
3911                  if ($self->{open_elements}->[-1]->[1] eq 'option') {
3912                    ## As if </option>
3913                    pop @{$self->{open_elements}};
3914                  }
3915    
3916                  if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
3917                    ## As if </optgroup>
3918                    pop @{$self->{open_elements}};
3919                  }
3920    
3921                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3922                  !!!next-token;
3923                  redo B;
3924                } elsif ($token->{tag_name} eq 'select') {
3925                  !!!parse-error (type => 'not closed:select');
3926                  ## As if </select> instead
3927                  ## have an element in table scope
3928                  my $i;
3929                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3930                    my $node = $self->{open_elements}->[$_];
3931                    if ($node->[1] eq $token->{tag_name}) {
3932                      $i = $_;
3933                      last INSCOPE;
3934                    } elsif ({
3935                              table => 1, html => 1,
3936                             }->{$node->[1]}) {
3937                      last INSCOPE;
3938                    }
3939                  } # INSCOPE
3940                  unless (defined $i) {
3941                    !!!parse-error (type => 'unmatched end tag:select');
3942                    ## Ignore the token
3943                    !!!next-token;
3944                    redo B;
3945                  }
3946                  
3947                  splice @{$self->{open_elements}}, $i;
3948    
3949                  $self->_reset_insertion_mode;
3950    
3951                  !!!next-token;
3952                  redo B;
3953                } else {
3954                  #
3955                }
3956              } elsif ($token->{type} eq 'end tag') {
3957                if ($token->{tag_name} eq 'optgroup') {
3958                  if ($self->{open_elements}->[-1]->[1] eq 'option' and
3959                      $self->{open_elements}->[-2]->[1] eq 'optgroup') {
3960                    ## As if </option>
3961                    splice @{$self->{open_elements}}, -2;
3962                  } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
3963                    pop @{$self->{open_elements}};
3964                  } else {
3965                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3966                    ## Ignore the token
3967                  }
3968                  !!!next-token;
3969                  redo B;
3970                } elsif ($token->{tag_name} eq 'option') {
3971                  if ($self->{open_elements}->[-1]->[1] eq 'option') {
3972                    pop @{$self->{open_elements}};
3973                  } else {
3974                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3975                    ## Ignore the token
3976                  }
3977                  !!!next-token;
3978                  redo B;
3979                } elsif ($token->{tag_name} eq 'select') {
3980                  ## have an element in table scope
3981                  my $i;
3982                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3983                    my $node = $self->{open_elements}->[$_];
3984                    if ($node->[1] eq $token->{tag_name}) {
3985                      $i = $_;
3986                      last INSCOPE;
3987                    } elsif ({
3988                              table => 1, html => 1,
3989                             }->{$node->[1]}) {
3990                      last INSCOPE;
3991                    }
3992                  } # INSCOPE
3993                  unless (defined $i) {
3994                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3995                    ## Ignore the token
3996                    !!!next-token;
3997                    redo B;
3998                  }
3999                  
4000                  splice @{$self->{open_elements}}, $i;
4001    
4002                  $self->_reset_insertion_mode;
4003    
4004                  !!!next-token;
4005                  redo B;
4006                } elsif ({
4007                          caption => 1, table => 1, tbody => 1,
4008                          tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
4009                         }->{$token->{tag_name}}) {
4010                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4011                  
4012                  ## have an element in table scope
4013                  my $i;
4014                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4015                    my $node = $self->{open_elements}->[$_];
4016                    if ($node->[1] eq $token->{tag_name}) {
4017                      $i = $_;
4018                      last INSCOPE;
4019                    } elsif ({
4020                              table => 1, html => 1,
4021                             }->{$node->[1]}) {
4022                      last INSCOPE;
4023                    }
4024                  } # INSCOPE
4025                  unless (defined $i) {
4026                    ## Ignore the token
4027                    !!!next-token;
4028                    redo B;
4029                  }
4030                  
4031                  ## As if </select>
4032                  ## have an element in table scope
4033                  undef $i;
4034                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4035                    my $node = $self->{open_elements}->[$_];
4036                    if ($node->[1] eq 'select') {
4037                      $i = $_;
4038                      last INSCOPE;
4039                    } elsif ({
4040                              table => 1, html => 1,
4041                             }->{$node->[1]}) {
4042                      last INSCOPE;
4043                    }
4044                  } # INSCOPE
4045                  unless (defined $i) {
4046                    !!!parse-error (type => 'unmatched end tag:select');
4047                    ## Ignore the </select> token
4048                    !!!next-token; ## TODO: ok?
4049                    redo B;
4050                  }
4051                  
4052                  splice @{$self->{open_elements}}, $i;
4053    
4054                  $self->_reset_insertion_mode;
4055    
4056                  ## reprocess
4057                  redo B;
4058                } else {
4059                  #
4060                }
4061              } else {
4062                #
4063              }
4064    
4065              !!!parse-error (type => 'in select:'.$token->{tag_name});
4066              ## Ignore the token
4067            !!!next-token;            !!!next-token;
4068              redo B;
4069        } elsif ($self->{insertion_mode} eq 'after body' or
4070                 $self->{insertion_mode} eq 'after html body') {
4071          if ($token->{type} eq 'character') {
4072            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4073              my $data = $1;
4074              ## As if in body
4075              $reconstruct_active_formatting_elements->($insert_to_current);
4076                  
4077              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4078              
4079              unless (length $token->{data}) {
4080                !!!next-token;
4081                redo B;
4082              }
4083          }          }
4084          if (length $text) {          
4085            $title_el->manakai_append_text ($text);          if ($self->{insertion_mode} eq 'after html body') {
4086              !!!parse-error (type => 'after html:#character');
4087    
4088              ## Reprocess in the "main" phase, "after body" insertion mode...
4089            }
4090            
4091            ## "after body" insertion mode
4092            !!!parse-error (type => 'after body:#character');
4093    
4094            $self->{insertion_mode} = 'in body';
4095            ## reprocess
4096            redo B;
4097          } elsif ($token->{type} eq 'start tag') {
4098            if ($self->{insertion_mode} eq 'after html body') {
4099              !!!parse-error (type => 'after html:'.$token->{tag_name});
4100              
4101              ## Reprocess in the "main" phase, "after body" insertion mode...
4102            }
4103    
4104            ## "after body" insertion mode
4105            !!!parse-error (type => 'after body:'.$token->{tag_name});
4106    
4107            $self->{insertion_mode} = 'in body';
4108            ## reprocess
4109            redo B;
4110          } elsif ($token->{type} eq 'end tag') {
4111            if ($self->{insertion_mode} eq 'after html body') {
4112              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4113              
4114              $self->{insertion_mode} = 'after body';
4115              ## Reprocess in the "main" phase, "after body" insertion mode...
4116            }
4117    
4118            ## "after body" insertion mode
4119            if ($token->{tag_name} eq 'html') {
4120              if (defined $self->{inner_html_node}) {
4121                !!!parse-error (type => 'unmatched end tag:html');
4122                ## Ignore the token
4123                !!!next-token;
4124                redo B;
4125              } else {
4126                $self->{insertion_mode} = 'after html body';
4127                !!!next-token;
4128                redo B;
4129              }
4130            } else {
4131              !!!parse-error (type => 'after body:/'.$token->{tag_name});
4132    
4133              $self->{insertion_mode} = 'in body';
4134              ## reprocess
4135              redo B;
4136            }
4137          } else {
4138            die "$0: $token->{type}: Unknown token type";
4139          }
4140        } elsif ($self->{insertion_mode} eq 'in frameset' or
4141                 $self->{insertion_mode} eq 'after frameset' or
4142                 $self->{insertion_mode} eq 'after html frameset') {
4143          if ($token->{type} eq 'character') {
4144            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4145              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4146              
4147              unless (length $token->{data}) {
4148                !!!next-token;
4149                redo B;
4150              }
4151          }          }
4152                    
4153          $self->{content_model_flag} = 'PCDATA';          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
4154              if ($self->{insertion_mode} eq 'in frameset') {
4155                !!!parse-error (type => 'in frameset:#character');
4156              } elsif ($self->{insertion_mode} eq 'after frameset') {
4157                !!!parse-error (type => 'after frameset:#character');
4158              } else { # "after html frameset"
4159                !!!parse-error (type => 'after html:#character');
4160    
4161                $self->{insertion_mode} = 'after frameset';
4162                ## Reprocess in the "main" phase, "after frameset"...
4163                !!!parse-error (type => 'after frameset:#character');
4164              }
4165              
4166              ## Ignore the token.
4167              if (length $token->{data}) {
4168                ## reprocess the rest of characters
4169              } else {
4170                !!!next-token;
4171              }
4172              redo B;
4173            }
4174                    
4175          if ($token->{type} eq 'end tag' and          die qq[$0: Character "$token->{data}"];
4176              $token->{tag_name} eq 'title') {        } elsif ($token->{type} eq 'start tag') {
4177            if ($self->{insertion_mode} eq 'after html frameset') {
4178              !!!parse-error (type => 'after html:'.$token->{tag_name});
4179    
4180              $self->{insertion_mode} = 'after frameset';
4181              ## Process in the "main" phase, "after frameset" insertion mode...
4182            }
4183    
4184            if ($token->{tag_name} eq 'frameset' and
4185                $self->{insertion_mode} eq 'in frameset') {
4186              !!!insert-element ($token->{tag_name}, $token->{attributes});
4187              !!!next-token;
4188              redo B;
4189            } elsif ($token->{tag_name} eq 'frame' and
4190                     $self->{insertion_mode} eq 'in frameset') {
4191              !!!insert-element ($token->{tag_name}, $token->{attributes});
4192              pop @{$self->{open_elements}};
4193              !!!next-token;
4194              redo B;
4195            } elsif ($token->{tag_name} eq 'noframes') {
4196              ## NOTE: As if in body.
4197              $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4198              redo B;
4199            } else {
4200              if ($self->{insertion_mode} eq 'in frameset') {
4201                !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4202              } else {
4203                !!!parse-error (type => 'after frameset:'.$token->{tag_name});
4204              }
4205            ## Ignore the token            ## Ignore the token
4206              !!!next-token;
4207              redo B;
4208            }
4209          } elsif ($token->{type} eq 'end tag') {
4210            if ($self->{insertion_mode} eq 'after html frameset') {
4211              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4212    
4213              $self->{insertion_mode} = 'after frameset';
4214              ## Process in the "main" phase, "after frameset" insertion mode...
4215            }
4216    
4217            if ($token->{tag_name} eq 'frameset' and
4218                $self->{insertion_mode} eq 'in frameset') {
4219              if ($self->{open_elements}->[-1]->[1] eq 'html' and
4220                  @{$self->{open_elements}} == 1) {
4221                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4222                ## Ignore the token
4223                !!!next-token;
4224              } else {
4225                pop @{$self->{open_elements}};
4226                !!!next-token;
4227              }
4228    
4229              if (not defined $self->{inner_html_node} and
4230                  $self->{open_elements}->[-1]->[1] ne 'frameset') {
4231                $self->{insertion_mode} = 'after frameset';
4232              }
4233              redo B;
4234            } elsif ($token->{tag_name} eq 'html' and
4235                     $self->{insertion_mode} eq 'after frameset') {
4236              $self->{insertion_mode} = 'after html frameset';
4237              !!!next-token;
4238              redo B;
4239          } else {          } else {
4240            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            if ($self->{insertion_mode} eq 'in frameset') {
4241            ## ISSUE: And ignore?              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
4242              } else {
4243                !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
4244              }
4245              ## Ignore the token
4246              !!!next-token;
4247              redo B;
4248          }          }
4249          } else {
4250            die "$0: $token->{type}: Unknown token type";
4251          }
4252    
4253          ## ISSUE: An issue in spec here
4254        } else {
4255          die "$0: $self->{insertion_mode}: Unknown insertion mode";
4256        }
4257    
4258        ## "in body" insertion mode
4259        if ($token->{type} eq 'start tag') {
4260          if ($token->{tag_name} eq 'script') {
4261            ## NOTE: This is an "as if in head" code clone
4262            $script_start_tag->($insert);
4263            redo B;
4264          } elsif ($token->{tag_name} eq 'style') {
4265            ## NOTE: This is an "as if in head" code clone
4266            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4267            redo B;
4268          } elsif ({
4269                    base => 1, link => 1,
4270                   }->{$token->{tag_name}}) {
4271            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4272            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4273            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4274          !!!next-token;          !!!next-token;
4275          return;          redo B;
4276          } elsif ($token->{tag_name} eq 'meta') {
4277            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4278            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4279            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4280    
4281            unless ($self->{confident}) {
4282              my $charset;
4283              if ($token->{attributes}->{charset}) { ## TODO: And if supported
4284                $charset = $token->{attributes}->{charset}->{value};
4285              }
4286              if ($token->{attributes}->{'http-equiv'}) {
4287                ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
4288                if ($token->{attributes}->{'http-equiv'}->{value}
4289                    =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
4290                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4291                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4292                  $charset = defined $1 ? $1 : defined $2 ? $2 : $3;
4293                } ## TODO: And if supported
4294              }
4295              ## TODO: Change the encoding
4296            }
4297    
4298            !!!next-token;
4299            redo B;
4300          } elsif ($token->{tag_name} eq 'title') {
4301            !!!parse-error (type => 'in body:title');
4302            ## NOTE: This is an "as if in head" code clone
4303            $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
4304              if (defined $self->{head_element}) {
4305                $self->{head_element}->append_child ($_[0]);
4306              } else {
4307                $insert->($_[0]);
4308              }
4309            });
4310            redo B;
4311        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
4312          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body');
4313                                
# Line 2494  sub _tree_construction_main ($) { Line 4325  sub _tree_construction_main ($) {
4325            }            }
4326          }          }
4327          !!!next-token;          !!!next-token;
4328          return;          redo B;
4329        } elsif ({        } elsif ({
4330                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
4331                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
# Line 2506  sub _tree_construction_main ($) { Line 4337  sub _tree_construction_main ($) {
4337            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4338              !!!back-token;              !!!back-token;
4339              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => 'end tag', tag_name => 'p'};
4340              return;              redo B;
4341            } elsif ({            } elsif ({
4342                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4343                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2527  sub _tree_construction_main ($) { Line 4358  sub _tree_construction_main ($) {
4358          } else {          } else {
4359            !!!next-token;            !!!next-token;
4360          }          }
4361          return;          redo B;
4362        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
4363          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
4364            !!!parse-error (type => 'in form:form');            !!!parse-error (type => 'in form:form');
4365            ## Ignore the token            ## Ignore the token
4366            !!!next-token;            !!!next-token;
4367            return;            redo B;
4368          } else {          } else {
4369            ## has a p element in scope            ## has a p element in scope
4370            INSCOPE: for (reverse @{$self->{open_elements}}) {            INSCOPE: for (reverse @{$self->{open_elements}}) {
4371              if ($_->[1] eq 'p') {              if ($_->[1] eq 'p') {
4372                !!!back-token;                !!!back-token;
4373                $token = {type => 'end tag', tag_name => 'p'};                $token = {type => 'end tag', tag_name => 'p'};
4374                return;                redo B;
4375              } elsif ({              } elsif ({
4376                        table => 1, caption => 1, td => 1, th => 1,                        table => 1, caption => 1, td => 1, th => 1,
4377                        button => 1, marquee => 1, object => 1, html => 1,                        button => 1, marquee => 1, object => 1, html => 1,
# Line 2552  sub _tree_construction_main ($) { Line 4383  sub _tree_construction_main ($) {
4383            !!!insert-element-t ($token->{tag_name}, $token->{attributes});            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4384            $self->{form_element} = $self->{open_elements}->[-1]->[0];            $self->{form_element} = $self->{open_elements}->[-1]->[0];
4385            !!!next-token;            !!!next-token;
4386            return;            redo B;
4387          }          }
4388        } elsif ($token->{tag_name} eq 'li') {        } elsif ($token->{tag_name} eq 'li') {
4389          ## has a p element in scope          ## has a p element in scope
# Line 2560  sub _tree_construction_main ($) { Line 4391  sub _tree_construction_main ($) {
4391            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4392              !!!back-token;              !!!back-token;
4393              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => 'end tag', tag_name => 'p'};
4394              return;              redo B;
4395            } elsif ({            } elsif ({
4396                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4397                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2578  sub _tree_construction_main ($) { Line 4409  sub _tree_construction_main ($) {
4409              if ($i != -1) {              if ($i != -1) {
4410                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'end tag missing:'.
4411                                $self->{open_elements}->[-1]->[1]);                                $self->{open_elements}->[-1]->[1]);
               ## TODO: test  
4412              }              }
4413              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
4414              last LI;              last LI;
# Line 2601  sub _tree_construction_main ($) { Line 4431  sub _tree_construction_main ($) {
4431                        
4432          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4433          !!!next-token;          !!!next-token;
4434          return;          redo B;
4435        } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {        } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {
4436          ## has a p element in scope          ## has a p element in scope
4437          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4438            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4439              !!!back-token;              !!!back-token;
4440              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => 'end tag', tag_name => 'p'};
4441              return;              redo B;
4442            } elsif ({            } elsif ({
4443                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4444                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2626  sub _tree_construction_main ($) { Line 4456  sub _tree_construction_main ($) {
4456              if ($i != -1) {              if ($i != -1) {
4457                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'end tag missing:'.
4458                                $self->{open_elements}->[-1]->[1]);                                $self->{open_elements}->[-1]->[1]);
               ## TODO: test  
4459              }              }
4460              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
4461              last LI;              last LI;
# Line 2649  sub _tree_construction_main ($) { Line 4478  sub _tree_construction_main ($) {
4478                        
4479          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4480          !!!next-token;          !!!next-token;
4481          return;          redo B;
4482        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
4483          ## has a p element in scope          ## has a p element in scope
4484          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4485            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4486              !!!back-token;              !!!back-token;
4487              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => 'end tag', tag_name => 'p'};
4488              return;              redo B;
4489            } elsif ({            } elsif ({
4490                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4491                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2667  sub _tree_construction_main ($) { Line 4496  sub _tree_construction_main ($) {
4496                        
4497          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4498                        
4499          $self->{content_model_flag} = 'PLAINTEXT';          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
4500                        
4501          !!!next-token;          !!!next-token;
4502          return;          redo B;
4503        } elsif ({        } elsif ({
4504                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4505                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 2680  sub _tree_construction_main ($) { Line 4509  sub _tree_construction_main ($) {
4509            if ($node->[1] eq 'p') {            if ($node->[1] eq 'p') {
4510              !!!back-token;              !!!back-token;
4511              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => 'end tag', tag_name => 'p'};
4512              return;              redo B;
4513            } elsif ({            } elsif ({
4514                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4515                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2715  sub _tree_construction_main ($) { Line 4544  sub _tree_construction_main ($) {
4544          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4545                        
4546          !!!next-token;          !!!next-token;
4547          return;          redo B;
4548        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
4549          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
4550            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
# Line 2750  sub _tree_construction_main ($) { Line 4579  sub _tree_construction_main ($) {
4579          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
4580    
4581          !!!next-token;          !!!next-token;
4582          return;          redo B;
4583        } elsif ({        } elsif ({
4584                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
4585                  s => 1, small => 1, strile => 1,                  s => 1, small => 1, strile => 1,
# Line 2762  sub _tree_construction_main ($) { Line 4591  sub _tree_construction_main ($) {
4591          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
4592                    
4593          !!!next-token;          !!!next-token;
4594          return;          redo B;
4595        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
4596          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4597    
# Line 2770  sub _tree_construction_main ($) { Line 4599  sub _tree_construction_main ($) {
4599          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4600            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
4601            if ($node->[1] eq 'nobr') {            if ($node->[1] eq 'nobr') {
4602                !!!parse-error (type => 'not closed:nobr');
4603              !!!back-token;              !!!back-token;
4604              $token = {type => 'end tag', tag_name => 'nobr'};              $token = {type => 'end tag', tag_name => 'nobr'};
4605              return;              redo B;
4606            } elsif ({            } elsif ({
4607                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4608                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2785  sub _tree_construction_main ($) { Line 4615  sub _tree_construction_main ($) {
4615          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
4616                    
4617          !!!next-token;          !!!next-token;
4618          return;          redo B;
4619        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
4620          ## has a button element in scope          ## has a button element in scope
4621          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 2794  sub _tree_construction_main ($) { Line 4624  sub _tree_construction_main ($) {
4624              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button');
4625              !!!back-token;              !!!back-token;
4626              $token = {type => 'end tag', tag_name => 'button'};              $token = {type => 'end tag', tag_name => 'button'};
4627              return;              redo B;
4628            } elsif ({            } elsif ({
4629                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4630                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2809  sub _tree_construction_main ($) { Line 4639  sub _tree_construction_main ($) {
4639          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
4640    
4641          !!!next-token;          !!!next-token;
4642          return;          redo B;
4643        } elsif ($token->{tag_name} eq 'marquee' or        } elsif ($token->{tag_name} eq 'marquee' or
4644                 $token->{tag_name} eq 'object') {                 $token->{tag_name} eq 'object') {
4645          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
# Line 2818  sub _tree_construction_main ($) { Line 4648  sub _tree_construction_main ($) {
4648          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
4649                    
4650          !!!next-token;          !!!next-token;
4651          return;          redo B;
4652        } elsif ($token->{tag_name} eq 'xmp') {        } elsif ($token->{tag_name} eq 'xmp') {
4653          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4654                    $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4655          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          redo B;
           
         $self->{content_model_flag} = 'CDATA';  
         delete $self->{escape}; # MUST  
           
         !!!next-token;  
         return;  
4656        } elsif ($token->{tag_name} eq 'table') {        } elsif ($token->{tag_name} eq 'table') {
4657          ## has a p element in scope          ## has a p element in scope
4658          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4659            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4660              !!!back-token;              !!!back-token;
4661              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => 'end tag', tag_name => 'p'};
4662              return;              redo B;
4663            } elsif ({            } elsif ({
4664                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4665                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2849  sub _tree_construction_main ($) { Line 4673  sub _tree_construction_main ($) {
4673          $self->{insertion_mode} = 'in table';          $self->{insertion_mode} = 'in table';
4674                        
4675          !!!next-token;          !!!next-token;
4676          return;          redo B;
4677        } elsif ({        } elsif ({
4678                  area => 1, basefont => 1, bgsound => 1, br => 1,                  area => 1, basefont => 1, bgsound => 1, br => 1,
4679                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
# Line 2859  sub _tree_construction_main ($) { Line 4683  sub _tree_construction_main ($) {
4683            !!!parse-error (type => 'image');            !!!parse-error (type => 'image');
4684            $token->{tag_name} = 'img';            $token->{tag_name} = 'img';
4685          }          }
4686            
4687            ## NOTE: There is an "as if <br>" code clone.
4688          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4689                    
4690          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4691          pop @{$self->{open_elements}};          pop @{$self->{open_elements}};
4692                    
4693          !!!next-token;          !!!next-token;
4694          return;          redo B;
4695        } elsif ($token->{tag_name} eq 'hr') {        } elsif ($token->{tag_name} eq 'hr') {
4696          ## has a p element in scope          ## has a p element in scope
4697          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4698            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4699              !!!back-token;              !!!back-token;
4700              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => 'end tag', tag_name => 'p'};
4701              return;              redo B;
4702            } elsif ({            } elsif ({
4703                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4704                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2886  sub _tree_construction_main ($) { Line 4711  sub _tree_construction_main ($) {
4711          pop @{$self->{open_elements}};          pop @{$self->{open_elements}};
4712                        
4713          !!!next-token;          !!!next-token;
4714          return;          redo B;
4715        } elsif ($token->{tag_name} eq 'input') {        } elsif ($token->{tag_name} eq 'input') {
4716          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4717                    
# Line 2895  sub _tree_construction_main ($) { Line 4720  sub _tree_construction_main ($) {
4720          pop @{$self->{open_elements}};          pop @{$self->{open_elements}};
4721                    
4722          !!!next-token;          !!!next-token;
4723          return;          redo B;
4724        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
4725          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex');
4726                    
4727          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
4728            ## Ignore the token            ## Ignore the token
4729            !!!next-token;            !!!next-token;
4730            return;            redo B;
4731          } else {          } else {
4732            my $at = $token->{attributes};            my $at = $token->{attributes};
4733            my $form_attrs;            my $form_attrs;
# Line 2934  sub _tree_construction_main ($) { Line 4759  sub _tree_construction_main ($) {
4759                          {type => 'end tag', tag_name => 'form'};                          {type => 'end tag', tag_name => 'form'};
4760            $token = shift @tokens;            $token = shift @tokens;
4761            !!!back-token (@tokens);            !!!back-token (@tokens);
4762            return;            redo B;
4763          }          }
4764        } elsif ({        } elsif ($token->{tag_name} eq 'textarea') {
                 textarea => 1,  
                 iframe => 1,  
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
4765          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
4766          my $el;          my $el;
4767          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $token->{tag_name}, $token->{attributes});
4768                    
4769          if ($token->{tag_name} eq 'textarea') {          ## TODO: $self->{form_element} if defined
4770            ## TODO: $self->{form_element} if defined          $self->{content_model} = RCDATA_CONTENT_MODEL;
           $self->{content_model_flag} = 'RCDATA';  
         } else {  
           $self->{content_model_flag} = 'CDATA';  
         }  
4771          delete $self->{escape}; # MUST          delete $self->{escape}; # MUST
4772                    
4773          $insert->($el);          $insert->($el);
4774                    
4775          my $text = '';          my $text = '';
4776          if ($token->{tag_name} eq 'textarea') {          !!!next-token;
4777            !!!next-token;          if ($token->{type} eq 'character') {
4778            if ($token->{type} eq 'character') {            $token->{data} =~ s/^\x0A//;
4779              $token->{data} =~ s/^\x0A//;            unless (length $token->{data}) {
4780              unless (length $token->{data}) {              !!!next-token;
               !!!next-token;  
             }  
4781            }            }
         } else {  
           !!!next-token;  
4782          }          }
4783          while ($token->{type} eq 'character') {          while ($token->{type} eq 'character') {
4784            $text .= $token->{data};            $text .= $token->{data};
# Line 2977  sub _tree_construction_main ($) { Line 4788  sub _tree_construction_main ($) {
4788            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
4789          }          }
4790                    
4791          $self->{content_model_flag} = 'PCDATA';          $self->{content_model} = PCDATA_CONTENT_MODEL;
4792                    
4793          if ($token->{type} eq 'end tag' and          if ($token->{type} eq 'end tag' and
4794              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
4795            ## Ignore the token            ## Ignore the token
4796          } else {          } else {
4797            if ($token->{tag_name} eq 'textarea') {            !!!parse-error (type => 'in RCDATA:#'.$token->{type});
             !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
           } else {  
             !!!parse-error (type => 'in CDATA:#'.$token->{type});  
           }  
           ## ISSUE: And ignore?  
4798          }          }
4799          !!!next-token;          !!!next-token;
4800          return;          redo B;
4801          } elsif ({
4802                    iframe => 1,
4803                    noembed => 1,
4804                    noframes => 1,
4805                    noscript => 0, ## TODO: 1 if scripting is enabled
4806                   }->{$token->{tag_name}}) {
4807            ## NOTE: There are two "as if in body" code clones.
4808            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4809            redo B;
4810        } elsif ($token->{tag_name} eq 'select') {        } elsif ($token->{tag_name} eq 'select') {
4811          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4812                    
# Line 2999  sub _tree_construction_main ($) { Line 4814  sub _tree_construction_main ($) {
4814                    
4815          $self->{insertion_mode} = 'in select';          $self->{insertion_mode} = 'in select';
4816          !!!next-token;          !!!next-token;
4817          return;          redo B;
4818        } elsif ({        } elsif ({
4819                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
4820                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 3009  sub _tree_construction_main ($) { Line 4824  sub _tree_construction_main ($) {
4824          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body:'.$token->{tag_name});
4825          ## Ignore the token          ## Ignore the token
4826          !!!next-token;          !!!next-token;
4827          return;          redo B;
4828                    
4829          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
4830        } else {        } else {
# Line 3018  sub _tree_construction_main ($) { Line 4833  sub _tree_construction_main ($) {
4833          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4834                    
4835          !!!next-token;          !!!next-token;
4836          return;          redo B;
4837        }        }
4838      } elsif ($token->{type} eq 'end tag') {      } elsif ($token->{type} eq 'end tag') {
4839        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
# Line 3028  sub _tree_construction_main ($) { Line 4843  sub _tree_construction_main ($) {
4843              unless ({              unless ({
4844                         dd => 1, dt => 1, li => 1, p => 1, td => 1,                         dd => 1, dt => 1, li => 1, p => 1, td => 1,
4845                         th => 1, tr => 1, body => 1, html => 1,                         th => 1, tr => 1, body => 1, html => 1,
4846                         tbody => 1, tfoot => 1, thead => 1,
4847                      }->{$_->[1]}) {                      }->{$_->[1]}) {
4848                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!parse-error (type => 'not closed:'.$_->[1]);
4849              }              }
# Line 3035  sub _tree_construction_main ($) { Line 4851  sub _tree_construction_main ($) {
4851    
4852            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = 'after body';
4853            !!!next-token;            !!!next-token;
4854            return;            redo B;
4855          } else {          } else {
4856            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4857            ## Ignore the token            ## Ignore the token
4858            !!!next-token;            !!!next-token;
4859            return;            redo B;
4860          }          }
4861        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
4862          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
# Line 3050  sub _tree_construction_main ($) { Line 4866  sub _tree_construction_main ($) {
4866            }            }
4867            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = 'after body';
4868            ## reprocess            ## reprocess
4869            return;            redo B;
4870          } else {          } else {
4871            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4872            ## Ignore the token            ## Ignore the token
4873            !!!next-token;            !!!next-token;
4874            return;            redo B;
4875          }          }
4876        } elsif ({        } elsif ({
4877                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
# Line 3077  sub _tree_construction_main ($) { Line 4893  sub _tree_construction_main ($) {
4893                   li => ($token->{tag_name} ne 'li'),                   li => ($token->{tag_name} ne 'li'),
4894                   p => ($token->{tag_name} ne 'p'),                   p => ($token->{tag_name} ne 'p'),
4895                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
4896                     tbody => 1, tfoot=> 1, thead => 1,
4897                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4898                !!!back-token;                !!!back-token;
4899                $token = {type => 'end tag',                $token = {type => 'end tag',
4900                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4901                return;                redo B;
4902              }              }
4903              $i = $_;              $i = $_;
4904              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE unless $token->{tag_name} eq 'p';
# Line 3094  sub _tree_construction_main ($) { Line 4911  sub _tree_construction_main ($) {
4911          } # INSCOPE          } # INSCOPE
4912                    
4913          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
4914            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            if (defined $i) {
4915                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4916              } else {
4917                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4918              }
4919          }          }
4920                    
4921          splice @{$self->{open_elements}}, $i if defined $i;          if (defined $i) {
4922              splice @{$self->{open_elements}}, $i;
4923            } elsif ($token->{tag_name} eq 'p') {
4924              ## As if <p>, then reprocess the current token
4925              my $el;
4926              !!!create-element ($el, 'p');
4927              $insert->($el);
4928            }
4929          $clear_up_to_marker->()          $clear_up_to_marker->()
4930            if {            if {
4931              button => 1, marquee => 1, object => 1,              button => 1, marquee => 1, object => 1,
4932            }->{$token->{tag_name}};            }->{$token->{tag_name}};
4933          !!!next-token;          !!!next-token;
4934          return;          redo B;
4935        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
4936          ## has an element in scope          ## has an element in scope
4937          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3113  sub _tree_construction_main ($) { Line 4941  sub _tree_construction_main ($) {
4941              if ({              if ({
4942                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
4943                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
4944                     tbody => 1, tfoot=> 1, thead => 1,
4945                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4946                !!!back-token;                !!!back-token;
4947                $token = {type => 'end tag',                $token = {type => 'end tag',
4948                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4949                return;                redo B;
4950              }              }
4951              last INSCOPE;              last INSCOPE;
4952            } elsif ({            } elsif ({
# Line 3136  sub _tree_construction_main ($) { Line 4965  sub _tree_construction_main ($) {
4965    
4966          undef $self->{form_element};          undef $self->{form_element};
4967          !!!next-token;          !!!next-token;
4968          return;          redo B;
4969        } elsif ({        } elsif ({
4970                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4971                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 3151  sub _tree_construction_main ($) { Line 4980  sub _tree_construction_main ($) {
4980              if ({              if ({
4981                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
4982                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
4983                     tbody => 1, tfoot=> 1, thead => 1,
4984                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4985                !!!back-token;                !!!back-token;
4986                $token = {type => 'end tag',                $token = {type => 'end tag',
4987                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4988                return;                redo B;
4989              }              }
4990              $i = $_;              $i = $_;
4991              last INSCOPE;              last INSCOPE;
# Line 3173  sub _tree_construction_main ($) { Line 5003  sub _tree_construction_main ($) {
5003                    
5004          splice @{$self->{open_elements}}, $i if defined $i;          splice @{$self->{open_elements}}, $i if defined $i;
5005          !!!next-token;          !!!next-token;
5006          return;          redo B;
5007        } elsif ({        } elsif ({
5008                  a => 1,                  a => 1,
5009                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 3181  sub _tree_construction_main ($) { Line 5011  sub _tree_construction_main ($) {
5011                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
5012                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5013          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token->{tag_name});
5014  ## TODO: <http://html5.org/tools/web-apps-tracker?from=883&to=884>          redo B;
5015          return;        } elsif ($token->{tag_name} eq 'br') {
5016            !!!parse-error (type => 'unmatched end tag:br');
5017    
5018            ## As if <br>
5019            $reconstruct_active_formatting_elements->($insert_to_current);
5020            
5021            my $el;
5022            !!!create-element ($el, 'br');
5023            $insert->($el);
5024            
5025            ## Ignore the token.
5026            !!!next-token;
5027            redo B;
5028        } elsif ({        } elsif ({
5029                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
5030                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
5031                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
5032                  thead => 1, tr => 1,                  thead => 1, tr => 1,
5033                  area => 1, basefont => 1, bgsound => 1, br => 1,                  area => 1, basefont => 1, bgsound => 1,
5034                  embed => 1, hr => 1, iframe => 1, image => 1,                  embed => 1, hr => 1, iframe => 1, image => 1,
5035                  img => 1, input => 1, isindex => 1, noembed => 1,                  img => 1, input => 1, isindex => 1, noembed => 1,
5036                  noframes => 1, param => 1, select => 1, spacer => 1,                  noframes => 1, param => 1, select => 1, spacer => 1,
# Line 3198  sub _tree_construction_main ($) { Line 5040  sub _tree_construction_main ($) {
5040          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5041          ## Ignore the token          ## Ignore the token
5042          !!!next-token;          !!!next-token;
5043          return;          redo B;
5044                    
5045          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
5046                    
# Line 3215  sub _tree_construction_main ($) { Line 5057  sub _tree_construction_main ($) {
5057              if ({              if ({
5058                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
5059                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
5060                     tbody => 1, tfoot=> 1, thead => 1,
5061                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5062                !!!back-token;                !!!back-token;
5063                $token = {type => 'end tag',                $token = {type => 'end tag',
5064                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5065                return;                redo B;
5066              }              }
5067                    
5068              ## Step 2              ## Step 2
# Line 3238  sub _tree_construction_main ($) { Line 5081  sub _tree_construction_main ($) {
5081                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
5082                  ($special_category->{$node->[1]} or                  ($special_category->{$node->[1]} or
5083                   $scoping_category->{$node->[1]})) {                   $scoping_category->{$node->[1]})) {
5084                !!!parse-error (type => 'not closed:'.$node->[1]);                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5085                ## Ignore the token                ## Ignore the token
5086                !!!next-token;                !!!next-token;
5087                last S2;                last S2;
# Line 3252  sub _tree_construction_main ($) { Line 5095  sub _tree_construction_main ($) {
5095            ## Step 5;            ## Step 5;
5096            redo S2;            redo S2;
5097          } # S2          } # S2
5098          return;          redo B;
       }  
     }  
   }; # $in_body  
   
   B: {  
     if ($phase eq 'main') {  
       if ($token->{type} eq 'DOCTYPE') {  
         !!!parse-error (type => 'in html:#DOCTYPE');  
         ## Ignore the token  
         ## Stay in the phase  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'start tag' and  
                $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') {  
         ## 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]};  
           redo B;  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
   
         ## Stop parsing  
         last B;  
   
         ## ISSUE: There is an issue in the spec.  
       } else {  
         if ($self->{insertion_mode} eq 'before head') {  
           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;  
               }  
             }  
             ## As if <head>  
             !!!create-element ($self->{head_element}, 'head');  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
             ## reprocess  
             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') {  
             my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};  
             !!!create-element ($self->{head_element}, 'head', $attr);  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
             if ($token->{tag_name} eq 'head') {  
               !!!next-token;  
             #} elsif ({  
             #          base => 1, link => 1, meta => 1,  
             #          script => 1, style => 1, title => 1,  
             #         }->{$token->{tag_name}}) {  
             #  ## reprocess  
             } else {  
               ## reprocess  
             }  
             redo B;  
           } elsif ($token->{type} eq 'end tag') {  
             if ({head => 1, body => 1, html => 1}->{$token->{tag_name}}) {  
               ## As if <head>  
               !!!create-element ($self->{head_element}, 'head');  
               $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
               push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               $self->{insertion_mode} = 'in head';  
               ## reprocess  
               redo B;  
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token ## ISSUE: An issue in the spec.  
               !!!next-token;  
               redo B;  
             }  
           } else {  
             die "$0: $token->{type}: Unknown type";  
           }  
         } elsif ($self->{insertion_mode} eq 'in head') {  
           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 'title') {  
               ## NOTE: There is an "as if in head" code clone  
               my $title_el;  
               !!!create-element ($title_el, 'title', $token->{attributes});  
               (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
                 ->append_child ($title_el);  
               $self->{content_model_flag} = 'RCDATA';  
               delete $self->{escape}; # MUST  
   
               my $text = '';  
               !!!next-token;  
               while ($token->{type} eq 'character') {  
                 $text .= $token->{data};  
                 !!!next-token;  
               }  
               if (length $text) {  
                 $title_el->manakai_append_text ($text);  
               }  
                 
               $self->{content_model_flag} = 'PCDATA';  
                 
               if ($token->{type} eq 'end tag' and  
                   $token->{tag_name} eq 'title') {  
                 ## Ignore the token  
               } else {  
                 !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
                 ## ISSUE: And ignore?  
               }  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'style') {  
               $style_start_tag->();  
               redo B;  
             } elsif ($token->{tag_name} eq 'script') {  
               $script_start_tag->();  
               redo B;  
             } elsif ({base => 1, link => 1, meta => 1}->{$token->{tag_name}}) {  
               ## NOTE: There are "as if in head" code clones  
               my $el;  
               !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
               if ($self->{insertion_mode} eq 'in head' and  
                   defined $self->{head_element}) {  
                 $self->{head_element}->append_child ($el);  
               } else {  
                 $self->{open_elements}->[-1]->[0]->append_child ($el);  
               }  
   
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'head') {  
               !!!parse-error (type => 'in head:head');  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'head') {  
               if ($self->{open_elements}->[-1]->[1] eq 'head') {  
                 pop @{$self->{open_elements}};  
               } else {  
                 !!!parse-error (type => 'unmatched end tag:head');  
               }  
               $self->{insertion_mode} = 'after head';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'body' or  
                      $token->{tag_name} eq 'html') {  
               #  
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             }  
           } else {  
             #  
           }  
   
           if ($self->{open_elements}->[-1]->[1] eq 'head') {  
             ## As if </head>  
             pop @{$self->{open_elements}};  
           }  
           $self->{insertion_mode} = 'after head';  
           ## reprocess  
           redo B;  
   
           ## ISSUE: An issue in the spec.  
         } elsif ($self->{insertion_mode} eq 'after head') {  
           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 'body') {  
               !!!insert-element ('body', $token->{attributes});  
               $self->{insertion_mode} = 'in body';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'frameset') {  
               !!!insert-element ('frameset', $token->{attributes});  
               $self->{insertion_mode} = 'in frameset';  
               !!!next-token;  
               redo B;  
             } elsif ({  
                       base => 1, link => 1, meta => 1,  
                       script => 1, style => 1, title => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'after head:'.$token->{tag_name});  
               $self->{insertion_mode} = 'in head';  
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           ## As if <body>  
           !!!insert-element ('body');  
           $self->{insertion_mode} = 'in body';  
           ## reprocess  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in body') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: There is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } else {  
             $in_body->($insert_to_current);  
             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;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1,  
                  colgroup => 1,  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$token->{tag_name}}) {  
               ## Clear back to table context  
               while ($self->{open_elements}->[-1]->[1] ne 'table' and  
                      $self->{open_elements}->[-1]->[1] ne 'html') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               push @$active_formatting_elements, ['#marker', '']  
                 if $token->{tag_name} eq 'caption';  
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = {  
                                  caption => 'in caption',  
                                  colgroup => 'in column group',  
                                  tbody => 'in table body',  
                                  tfoot => 'in table body',  
                                  thead => 'in table body',  
                                 }->{$token->{tag_name}};  
               !!!next-token;  
               redo B;  
             } elsif ({  
                       col => 1,  
                       td => 1, th => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## Clear back to table context  
               while ($self->{open_elements}->[-1]->[1] ne 'table' and  
                      $self->{open_elements}->[-1]->[1] ne 'html') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');  
               $self->{insertion_mode} = $token->{tag_name} eq 'col'  
                 ? 'in column group' : 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: There are code clones for this "table in table"  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
   
               ## As if </table>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'table') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:table');  
                 ## Ignore tokens </table><table>  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## 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  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'table') {  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } 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;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:caption');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <?>  
                 $token = {type => 'end tag', tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'caption') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'caption') {  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'caption') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               !!!parse-error (type => 'not closed:caption');  
   
               ## As if </caption>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'caption') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:caption');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # </table>  
                 $token = {type => 'end tag', tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'caption') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, col => 1, colgroup => 1,  
                       html => 1, tbody => 1, td => 1, tfoot => 1,  
                       th => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
                 
           $in_body->($insert_to_current);  
           redo B;  
         } 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');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               } else {  
                 pop @{$self->{open_elements}}; # colgroup  
                 $self->{insertion_mode} = 'in table';  
                 !!!next-token;  
                 redo B;              
               }  
             } elsif ($token->{tag_name} eq 'col') {  
               !!!parse-error (type => 'unmatched end tag:col');  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           ## As if </colgroup>  
           if ($self->{open_elements}->[-1]->[1] eq 'html') {  
             !!!parse-error (type => 'unmatched end tag:colgroup');  
             ## Ignore the token  
             !!!next-token;  
             redo B;  
           } else {  
             pop @{$self->{open_elements}}; # colgroup  
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table body') {  
           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 ({  
                  tr => 1,  
                  th => 1, td => 1,  
                 }->{$token->{tag_name}}) {  
               unless ($token->{tag_name} eq 'tr') {  
                 !!!parse-error (type => 'missing start tag:tr');  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
               $self->{insertion_mode} = 'in row';  
               if ($token->{tag_name} eq 'tr') {  
                 !!!insert-element ($token->{tag_name}, $token->{attributes});  
                 !!!next-token;  
               } else {  
                 !!!insert-element ('tr');  
                 ## reprocess  
               }  
               redo B;  
             } elsif ({  
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1,  
                      }->{$token->{tag_name}}) {  
               ## 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;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               ## reprocess  
               redo B;  
             } elsif ($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;  
               }  
                 
               ## 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  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ({  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## 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;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, td => 1, th => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           ## As if in table  
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in row') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$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]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = 'in cell';  
   
               push @$active_formatting_elements, ['#marker', ''];  
                 
               !!!next-token;  
               redo B;  
             } elsif ({  
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # 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]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
             } 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;  
               }  
                 
               ## 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  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'tr') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{type});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       tbody => 1, tfoot => 1, thead => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:tr');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1,  
                       colgroup => 1, html => 1, td => 1, th => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           ## As if in table  
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in cell') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: This is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1, col => 1, colgroup => 1,  
                  tbody => 1, td => 1, tfoot => 1, th => 1,  
                  thead => 1, tr => 1,  
                 }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $tn) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Close the cell  
               !!!back-token; # <?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => ($token->{tag_name} eq 'th'),  
                    th => ($token->{tag_name} eq 'td'),  
                    tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in row';  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1,  
                       colgroup => 1, html => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } elsif ({  
                       table => 1, tbody => 1, tfoot => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   ## NOTE: There is exactly one |td| or |th| element  
                   ## in scope in the stack of open elements by definition.  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           $in_body->($insert_to_current);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in select') {  
           if ($token->{type} eq 'character') {  
             $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;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'option') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 ## As if </option>  
                 pop @{$self->{open_elements}};  
               }  
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'optgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 ## As if </option>  
                 pop @{$self->{open_elements}};  
               }  
   
               if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {  
                 ## As if </optgroup>  
                 pop @{$self->{open_elements}};  
               }  
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'select') {  
               !!!parse-error (type => 'not closed:select');  
               ## As if </select> instead  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:select');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'optgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option' and  
                   $self->{open_elements}->[-2]->[1] eq 'optgroup') {  
                 ## As if </option>  
                 splice @{$self->{open_elements}}, -2;  
               } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {  
                 pop @{$self->{open_elements}};  
               } else {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
               }  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'option') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 pop @{$self->{open_elements}};  
               } else {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
               }  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'select') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       caption => 1, table => 1, tbody => 1,  
                       tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## As if </select>  
               ## have an element in table scope  
               undef $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'select') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:select');  
                 ## Ignore the </select> token  
                 !!!next-token; ## TODO: ok?  
                 redo B;  
               }  
                 
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in select:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'after body') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               ## As if in body  
               $reconstruct_active_formatting_elements->($insert_to_current);  
                 
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
             !!!parse-error (type => 'after body:#'.$token->{type});  
           } 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;  
           } elsif ($token->{type} eq 'start tag') {  
             !!!parse-error (type => 'after body:'.$token->{tag_name});  
             #  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'html') {  
               if (defined $self->{inner_html_node}) {  
                 !!!parse-error (type => 'unmatched end tag:html');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               } else {  
                 $phase = 'trailing end';  
                 !!!next-token;  
                 redo B;  
               }  
             } else {  
               !!!parse-error (type => 'after body:/'.$token->{tag_name});  
             }  
           } else {  
             !!!parse-error (type => 'after body:#'.$token->{type});  
           }  
   
           $self->{insertion_mode} = 'in body';  
           ## reprocess  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in frameset') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
               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 '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 {  
             #  
           }  
             
           if (defined $token->{tag_name}) {  
             !!!parse-error (type => 'in frameset:'.$token->{tag_name});  
           } else {  
             !!!parse-error (type => 'in frameset:#'.$token->{type});  
           }  
           ## Ignore the token  
           !!!next-token;  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'after frameset') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
               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 'noframes') {  
               $in_body->($insert_to_current);  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'html') {  
               $phase = 'trailing end';  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           if (defined $token->{tag_name}) {  
             !!!parse-error (type => 'after frameset:'.$token->{tag_name});  
           } else {  
             !!!parse-error (type => 'after frameset:#'.$token->{type});  
           }  
           ## Ignore the token  
           !!!next-token;  
           redo B;  
   
           ## ISSUE: An issue in spec there  
         } else {  
           die "$0: $self->{insertion_mode}: Unknown insertion mode";  
         }  
       }  
     } elsif ($phase eq 'trailing end') {  
       ## states in the main stage is preserved yet # MUST  
         
       if ($token->{type} eq 'DOCTYPE') {  
         !!!parse-error (type => 'after html:#DOCTYPE');  
         ## Ignore the token  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'comment') {  
         my $comment = $self->{document}->create_comment ($token->{data});  
         $self->{document}->append_child ($comment);  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'character') {  
         if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
           my $data = $1;  
           ## As if in the main phase.  
           ## NOTE: The insertion mode in the main phase  
           ## just before the phase has been changed to the trailing  
           ## end phase is either "after body" or "after frameset".  
           $reconstruct_active_formatting_elements->($insert_to_current)  
             if $phase eq 'main';  
             
           $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);  
             
           unless (length $token->{data}) {  
             !!!next-token;  
             redo B;  
           }  
         }  
   
         !!!parse-error (type => 'after html:#character');  
         $phase = 'main';  
         ## reprocess  
         redo B;  
       } elsif ($token->{type} eq 'start tag' or  
                $token->{type} eq 'end tag') {  
         !!!parse-error (type => 'after html:'.$token->{tag_name});  
         $phase = 'main';  
         ## reprocess  
         redo B;  
       } elsif ($token->{type} eq 'end-of-file') {  
         ## Stop parsing  
         last B;  
       } else {  
         die "$0: $token->{type}: Unknown token";  
5099        }        }
5100      }      }
5101        redo B;
5102    } # B    } # B
5103    
5104      ## NOTE: The "trailing end" phase in HTML5 is split into
5105      ## two insertion modes: "after html body" and "after html frameset".
5106      ## NOTE: States in the main stage is preserved while
5107      ## the parser stays in the trailing end phase. # MUST
5108    
5109    ## Stop parsing # MUST    ## Stop parsing # MUST
5110        
5111    ## TODO: script stuffs    ## TODO: script stuffs
# Line 5210  sub set_inner_html ($$$) { Line 5190  sub set_inner_html ($$$) {
5190    
5191      ## Step 2      ## Step 2
5192      my $node_ln = $node->local_name;      my $node_ln = $node->local_name;
5193      $p->{content_model_flag} = {      $p->{content_model} = {
5194        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
5195        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
5196        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
5197        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
5198        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
5199        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
5200        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
5201        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
5202        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
5203        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
5204      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
5205         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
5206            unless defined $p->{content_model};
5207            ## ISSUE: What is "the name of the element"? local name?
5208    
5209      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
5210    
# Line 5322  sub get_inner_html ($$$) { Line 5304  sub get_inner_html ($$$) {
5304            
5305      my $nt = $child->node_type;      my $nt = $child->node_type;
5306      if ($nt == 1) { # Element      if ($nt == 1) { # Element
5307        my $tag_name = lc $child->tag_name; ## ISSUE: Definition of "lowercase"        my $tag_name = $child->tag_name; ## TODO: manakai_tag_name
5308        $s .= '<' . $tag_name;        $s .= '<' . $tag_name;
5309          ## NOTE: Non-HTML case:
5310        ## ISSUE: Non-html elements        ## <http://permalink.gmane.org/gmane.org.w3c.whatwg.discuss/11191>
5311    
5312        my @attrs = @{$child->attributes}; # sort order MUST be stable        my @attrs = @{$child->attributes}; # sort order MUST be stable
5313        for my $attr (@attrs) { # order is implementation dependent        for my $attr (@attrs) { # order is implementation dependent
5314          my $attr_name = lc $attr->name; ## ISSUE: Definition of "lowercase"          my $attr_name = $attr->name; ## TODO: manakai_name
5315          $s .= ' ' . $attr_name . '="';          $s .= ' ' . $attr_name . '="';
5316          my $attr_value = $attr->value;          my $attr_value = $attr->value;
5317          ## escape          ## escape
# Line 5353  sub get_inner_html ($$$) { Line 5335  sub get_inner_html ($$$) {
5335        if (not $in_cdata and {        if (not $in_cdata and {
5336          style => 1, script => 1, xmp => 1, iframe => 1,          style => 1, script => 1, xmp => 1, iframe => 1,
5337          noembed => 1, noframes => 1, noscript => 1,          noembed => 1, noframes => 1, noscript => 1,
5338            plaintext => 1,
5339        }->{$tag_name}) {        }->{$tag_name}) {
5340          unshift @node, 'cdata-out';          unshift @node, 'cdata-out';
5341          $in_cdata = 1;          $in_cdata = 1;

Legend:
Removed from v.1.24  
changed lines
  Added in v.1.53

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24