/[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.20 by wakaba, Sat Jun 23 14:25:05 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
338            $self->{content_model_flag} eq 'CDATA') {          if (defined $self->{last_emitted_start_tag_name}) {
339          my @next_char;            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
340          TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            my @next_char;
341              TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
342                push @next_char, $self->{next_input_character};
343                my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
344                my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
345                if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {
346                  !!!next-input-character;
347                  next TAGNAME;
348                } else {
349                  $self->{next_input_character} = shift @next_char; # reconsume
350                  !!!back-next-input-character (@next_char);
351                  $self->{state} = 'data';
352    
353                  !!!emit ({type => 'character', data => '</'});
354      
355                  redo A;
356                }
357              }
358            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_input_character};
359            my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);        
360            my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;            unless ($self->{next_input_character} == 0x0009 or # HT
361            if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {                    $self->{next_input_character} == 0x000A or # LF
362              !!!next-input-character;                    $self->{next_input_character} == 0x000B or # VT
363              next TAGNAME;                    $self->{next_input_character} == 0x000C or # FF
364            } else {                    $self->{next_input_character} == 0x0020 or # SP
365                      $self->{next_input_character} == 0x003E or # >
366                      $self->{next_input_character} == 0x002F or # /
367                      $self->{next_input_character} == -1) {
368              $self->{next_input_character} = shift @next_char; # reconsume              $self->{next_input_character} = shift @next_char; # reconsume
369              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
370              $self->{state} = 'data';              $self->{state} = 'data';
   
371              !!!emit ({type => 'character', data => '</'});              !!!emit ({type => 'character', data => '</'});
   
372              redo A;              redo A;
373              } else {
374                $self->{next_input_character} = shift @next_char;
375                !!!back-next-input-character (@next_char);
376                # and consume...
377            }            }
378          }          } else {
379          push @next_char, $self->{next_input_character};            ## No start tag token has ever been emitted
380                  # next-input-character is already done
         unless ($self->{next_input_character} == 0x0009 or # HT  
                 $self->{next_input_character} == 0x000A or # LF  
                 $self->{next_input_character} == 0x000B or # VT  
                 $self->{next_input_character} == 0x000C or # FF  
                 $self->{next_input_character} == 0x0020 or # SP  
                 $self->{next_input_character} == 0x003E or # >  
                 $self->{next_input_character} == 0x002F or # /  
                 $self->{next_input_character} == -1) {  
           $self->{next_input_character} = shift @next_char; # reconsume  
           !!!back-next-input-character (@next_char);  
381            $self->{state} = 'data';            $self->{state} = 'data';
   
382            !!!emit ({type => 'character', data => '</'});            !!!emit ({type => 'character', data => '</'});
   
383            redo A;            redo A;
         } else {  
           $self->{next_input_character} = shift @next_char;  
           !!!back-next-input-character (@next_char);  
           # and consume...  
384          }          }
385        }        }
386                
# Line 412  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 425  sub _get_next_token ($) { Line 443  sub _get_next_token ($) {
443          !!!next-input-character;          !!!next-input-character;
444    
445          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
446    
447          redo A;          redo A;
448        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 438  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 451  sub _get_next_token ($) { Line 470  sub _get_next_token ($) {
470          # reconsume          # reconsume
471    
472          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
473    
474          redo A;          redo A;
475        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
# Line 485  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 498  sub _get_next_token ($) { Line 518  sub _get_next_token ($) {
518          !!!next-input-character;          !!!next-input-character;
519    
520          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
521    
522          redo A;          redo A;
523        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 524  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 537  sub _get_next_token ($) { Line 558  sub _get_next_token ($) {
558          # reconsume          # reconsume
559    
560          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
561    
562          redo A;          redo A;
563        } else {        } else {
# Line 551  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 576  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 589  sub _get_next_token ($) { Line 611  sub _get_next_token ($) {
611          !!!next-input-character;          !!!next-input-character;
612    
613          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
614    
615          redo A;          redo A;
616        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 616  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 629  sub _get_next_token ($) { Line 652  sub _get_next_token ($) {
652          # reconsume          # reconsume
653    
654          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
655    
656          redo A;          redo A;
657        } else {        } else {
# 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 666  sub _get_next_token ($) { Line 690  sub _get_next_token ($) {
690          !!!next-input-character;          !!!next-input-character;
691    
692          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
693    
694          redo A;          redo A;
695        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_input_character} and
# Line 685  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 692  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 705  sub _get_next_token ($) { Line 731  sub _get_next_token ($) {
731          # reconsume          # reconsume
732    
733          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
734    
735          redo A;          redo A;
736        } else {        } else {
# Line 738  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 751  sub _get_next_token ($) { Line 778  sub _get_next_token ($) {
778          !!!next-input-character;          !!!next-input-character;
779    
780          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
781    
782          redo A;          redo A;
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 770  sub _get_next_token ($) { Line 798  sub _get_next_token ($) {
798          ## reconsume          ## reconsume
799    
800          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
801    
802          redo A;          redo A;
803        } else {        } else {
# Line 792  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 805  sub _get_next_token ($) { Line 834  sub _get_next_token ($) {
834          ## reconsume          ## reconsume
835    
836          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
837    
838          redo A;          redo A;
839        } else {        } else {
# Line 827  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 840  sub _get_next_token ($) { Line 870  sub _get_next_token ($) {
870          ## reconsume          ## reconsume
871    
872          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
873    
874          redo A;          redo A;
875        } else {        } else {
# Line 865  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 878  sub _get_next_token ($) { Line 909  sub _get_next_token ($) {
909          !!!next-input-character;          !!!next-input-character;
910    
911          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
912    
913          redo A;          redo A;
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 897  sub _get_next_token ($) { Line 929  sub _get_next_token ($) {
929          ## reconsume          ## reconsume
930    
931          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
         undef $self->{current_token};  
932    
933          redo A;          redo A;
934        } else {        } else {
# Line 907  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 956  sub _get_next_token ($) { Line 987  sub _get_next_token ($) {
987          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_input_character};
988          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_input_character} == 0x002D) { # -
989            $self->{current_token} = {type => 'comment', data => ''};            $self->{current_token} = {type => 'comment', data => ''};
990            $self->{state} = 'comment';            $self->{state} = 'comment start';
991            !!!next-input-character;            !!!next-input-character;
992            redo A;            redo A;
993          }          }
# Line 998  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 1006  sub _get_next_token ($) { Line 1037  sub _get_next_token ($) {
1037                
1038        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1039        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?
1040        } elsif ($self->{state} eq 'comment start') {
1041          if ($self->{next_input_character} == 0x002D) { # -
1042            $self->{state} = 'comment start dash';
1043            !!!next-input-character;
1044            redo A;
1045          } elsif ($self->{next_input_character} == 0x003E) { # >
1046            !!!parse-error (type => 'bogus comment');
1047            $self->{state} = 'data';
1048            !!!next-input-character;
1049    
1050            !!!emit ($self->{current_token}); # comment
1051    
1052            redo A;
1053          } elsif ($self->{next_input_character} == -1) {
1054            !!!parse-error (type => 'unclosed comment');
1055            $self->{state} = 'data';
1056            ## reconsume
1057    
1058            !!!emit ($self->{current_token}); # comment
1059    
1060            redo A;
1061          } else {
1062            $self->{current_token}->{data} # comment
1063                .= chr ($self->{next_input_character});
1064            $self->{state} = 'comment';
1065            !!!next-input-character;
1066            redo A;
1067          }
1068        } elsif ($self->{state} eq 'comment start dash') {
1069          if ($self->{next_input_character} == 0x002D) { # -
1070            $self->{state} = 'comment end';
1071            !!!next-input-character;
1072            redo A;
1073          } elsif ($self->{next_input_character} == 0x003E) { # >
1074            !!!parse-error (type => 'bogus comment');
1075            $self->{state} = 'data';
1076            !!!next-input-character;
1077    
1078            !!!emit ($self->{current_token}); # comment
1079    
1080            redo A;
1081          } elsif ($self->{next_input_character} == -1) {
1082            !!!parse-error (type => 'unclosed comment');
1083            $self->{state} = 'data';
1084            ## reconsume
1085    
1086            !!!emit ($self->{current_token}); # comment
1087    
1088            redo A;
1089          } else {
1090            $self->{current_token}->{data} # comment
1091                .= '-' . chr ($self->{next_input_character});
1092            $self->{state} = 'comment';
1093            !!!next-input-character;
1094            redo A;
1095          }
1096      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} eq 'comment') {
1097        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1098          $self->{state} = 'comment dash';          $self->{state} = 'comment end dash';
1099          !!!next-input-character;          !!!next-input-character;
1100          redo A;          redo A;
1101        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
# Line 1017  sub _get_next_token ($) { Line 1104  sub _get_next_token ($) {
1104          ## reconsume          ## reconsume
1105    
1106          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1107    
1108          redo A;          redo A;
1109        } else {        } else {
# Line 1026  sub _get_next_token ($) { Line 1112  sub _get_next_token ($) {
1112          !!!next-input-character;          !!!next-input-character;
1113          redo A;          redo A;
1114        }        }
1115      } elsif ($self->{state} eq 'comment dash') {      } elsif ($self->{state} eq 'comment end dash') {
1116        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1117          $self->{state} = 'comment end';          $self->{state} = 'comment end';
1118          !!!next-input-character;          !!!next-input-character;
# Line 1037  sub _get_next_token ($) { Line 1123  sub _get_next_token ($) {
1123          ## reconsume          ## reconsume
1124    
1125          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1126    
1127          redo A;          redo A;
1128        } else {        } else {
# Line 1052  sub _get_next_token ($) { Line 1137  sub _get_next_token ($) {
1137          !!!next-input-character;          !!!next-input-character;
1138    
1139          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1140    
1141          redo A;          redo A;
1142        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_input_character} == 0x002D) { # -
# Line 1067  sub _get_next_token ($) { Line 1151  sub _get_next_token ($) {
1151          ## reconsume          ## reconsume
1152    
1153          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
         undef $self->{current_token};  
1154    
1155          redo A;          redo A;
1156        } else {        } else {
# Line 1142  sub _get_next_token ($) { Line 1225  sub _get_next_token ($) {
1225          !!!next-input-character;          !!!next-input-character;
1226    
1227          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1228    
1229          redo A;          redo A;
1230        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
# Line 1152  sub _get_next_token ($) { Line 1234  sub _get_next_token ($) {
1234    
1235          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1236          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1237    
1238          redo A;          redo A;
1239        } else {        } else {
# Line 1176  sub _get_next_token ($) { Line 1257  sub _get_next_token ($) {
1257          !!!next-input-character;          !!!next-input-character;
1258    
1259          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1260    
1261          redo A;          redo A;
1262        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
# Line 1186  sub _get_next_token ($) { Line 1266  sub _get_next_token ($) {
1266    
1267          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1268          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1269    
1270          redo A;          redo A;
1271        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{next_input_character} == 0x0050 or # P
# Line 1278  sub _get_next_token ($) { Line 1357  sub _get_next_token ($) {
1357    
1358          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1359          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1360    
1361          redo A;          redo A;
1362        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
# Line 1289  sub _get_next_token ($) { Line 1367  sub _get_next_token ($) {
1367    
1368          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1369          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1370    
1371          redo A;          redo A;
1372        } else {        } else {
# Line 1311  sub _get_next_token ($) { Line 1388  sub _get_next_token ($) {
1388    
1389          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1390          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1391    
1392          redo A;          redo A;
1393        } else {        } else {
# Line 1334  sub _get_next_token ($) { Line 1410  sub _get_next_token ($) {
1410    
1411          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1412          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1413    
1414          redo A;          redo A;
1415        } else {        } else {
# Line 1367  sub _get_next_token ($) { Line 1442  sub _get_next_token ($) {
1442          !!!next-input-character;          !!!next-input-character;
1443    
1444          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1445    
1446          redo A;          redo A;
1447        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
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
         undef $self->{current_token};  
1455    
1456          redo A;          redo A;
1457        } else {        } else {
# Line 1412  sub _get_next_token ($) { Line 1485  sub _get_next_token ($) {
1485    
1486          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1487          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1488    
1489          redo A;          redo A;
1490        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
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
         undef $self->{current_token};  
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 1445  sub _get_next_token ($) { Line 1516  sub _get_next_token ($) {
1516    
1517          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1518          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1519    
1520          redo A;          redo A;
1521        } else {        } else {
# Line 1468  sub _get_next_token ($) { Line 1538  sub _get_next_token ($) {
1538    
1539          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1540          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1541    
1542          redo A;          redo A;
1543        } else {        } else {
# Line 1491  sub _get_next_token ($) { Line 1560  sub _get_next_token ($) {
1560          !!!next-input-character;          !!!next-input-character;
1561    
1562          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1563    
1564          redo A;          redo A;
1565        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
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
         undef $self->{current_token};  
1573    
1574          redo A;          redo A;
1575        } else {        } else {
# Line 1518  sub _get_next_token ($) { Line 1585  sub _get_next_token ($) {
1585    
1586          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1587          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1588    
1589          redo A;          redo A;
1590        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
# Line 1528  sub _get_next_token ($) { Line 1594  sub _get_next_token ($) {
1594    
1595          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1596          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
         undef $self->{current_token};  
1597    
1598          redo A;          redo A;
1599        } else {        } else {
# Line 1544  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 1558  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 1593  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 1624  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 1648  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 1663  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 1881  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 1923  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 1946  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 1965  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 1982  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 2018  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 2102  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 2150  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 2166  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 2362  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 2396  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;
     my $insert = shift;  
     if ($token->{type} eq 'start tag') {  
       if ($token->{tag_name} eq 'script') {  
         $script_start_tag->();  
         return;  
       } elsif ($token->{tag_name} eq 'style') {  
         $style_start_tag->();  
         return;  
       } elsif ({  
                 base => 1, link => 1, meta => 1,  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'in body:'.$token->{tag_name});  
         ## NOTE: This is an "as if in head" code clone  
         my $el;  
         !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
         if (defined $self->{head_element}) {  
           $self->{head_element}->append_child ($el);  
         } else {  
           $insert->($el);  
         }  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'title') {  
         !!!parse-error (type => 'in body: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;  
         return;  
       } elsif ($token->{tag_name} eq 'body') {  
         !!!parse-error (type => 'in body:body');  
                 
         if (@{$self->{open_elements}} == 1 or  
             $self->{open_elements}->[1]->[1] ne 'body') {  
           ## Ignore the token  
         } else {  
           my $body_el = $self->{open_elements}->[1]->[0];  
           for my $attr_name (keys %{$token->{attributes}}) {  
             unless ($body_el->has_attribute_ns (undef, $attr_name)) {  
               $body_el->set_attribute_ns  
                 (undef, [undef, $attr_name],  
                  $token->{attributes}->{$attr_name}->{value});  
             }  
           }  
         }  
         !!!next-token;  
         return;  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, p => 1, ul => 1,  
                 pre => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         if ($token->{tag_name} eq 'pre') {  
           !!!next-token;  
           if ($token->{type} eq 'character') {  
             $token->{data} =~ s/^\x0A//;  
             unless (length $token->{data}) {  
               !!!next-token;  
             }  
           }  
         } else {  
           !!!next-token;  
         }  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         if (defined $self->{form_element}) {  
           !!!parse-error (type => 'in form:form');  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           ## has a p element in scope  
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!back-token;  
               $token = {type => 'end tag', tag_name => 'p'};  
               return;  
             } elsif ({  
                       table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'li') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'li') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
               ## TODO: test  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
               ## TODO: test  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'plaintext') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{content_model_flag} = 'PLAINTEXT';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ({  
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             $i = $_;  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         if (defined $i) {  
           !!!parse-error (type => 'in hn:hn');  
           splice @{$self->{open_elements}}, $i;  
         }  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'a') {  
         AFE: for my $i (reverse 0..$#$active_formatting_elements) {  
           my $node = $active_formatting_elements->[$i];  
           if ($node->[1] eq 'a') {  
             !!!parse-error (type => 'in a:a');  
               
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'a'};  
             $formatting_end_tag->($token->{tag_name});  
               
             AFE2: for (reverse 0..$#$active_formatting_elements) {  
               if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {  
                 splice @$active_formatting_elements, $_, 1;  
                 last AFE2;  
               }  
             } # AFE2  
             OE: for (reverse 0..$#{$self->{open_elements}}) {  
               if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {  
                 splice @{$self->{open_elements}}, $_, 1;  
                 last OE;  
               }  
             } # OE  
             last AFE;  
           } elsif ($node->[0] eq '#marker') {  
             last AFE;  
           }  
         } # AFE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
   
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
   
         !!!next-token;  
         return;  
       } elsif ({  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'nobr') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
   
         ## has a |nobr| element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'nobr') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'nobr'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'button') {  
         ## has a button element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'button') {  
             !!!parse-error (type => 'in button:button');  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'button'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
   
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $self->{content_model_flag} = 'CDATA';  
         delete $self->{escape}; # MUST  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = 'in table';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,  
                 image => 1,  
                }->{$token->{tag_name}}) {  
         if ($token->{tag_name} eq 'image') {  
           !!!parse-error (type => 'image');  
           $token->{tag_name} = 'img';  
         }  
           
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'input') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         ## TODO: associate with $self->{form_element} if defined  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'isindex') {  
         !!!parse-error (type => 'isindex');  
           
         if (defined $self->{form_element}) {  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           my $at = $token->{attributes};  
           $at->{name} = {name => 'name', value => 'isindex'};  
           my @tokens = (  
                         {type => 'start tag', tag_name => 'form'},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'start tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'label'},  
                         {type => 'character',  
                          data => 'This is a searchable index. Insert your search keywords here: '}, # SHOULD  
                         ## TODO: make this configurable  
                         {type => 'start tag', tag_name => 'input', attributes => $at},  
                         #{type => 'character', data => ''}, # SHOULD  
                         {type => 'end tag', tag_name => 'label'},  
                         {type => 'end tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'end tag', tag_name => 'form'},  
                        );  
           $token = shift @tokens;  
           !!!back-token (@tokens);  
           return;  
         }  
       } elsif ({  
                 textarea => 1,  
                 iframe => 1,  
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         my $tag_name = $token->{tag_name};  
         my $el;  
         !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
           
         if ($token->{tag_name} eq 'textarea') {  
           ## TODO: $self->{form_element} if defined  
           $self->{content_model_flag} = 'RCDATA';  
         } else {  
           $self->{content_model_flag} = 'CDATA';  
         }  
         delete $self->{escape}; # MUST  
           
         $insert->($el);  
           
         my $text = '';  
         if ($token->{tag_name} eq 'textarea') {  
           !!!next-token;  
           if ($token->{type} eq 'character') {  
             $token->{data} =~ s/^\x0A//;  
             unless (length $token->{data}) {  
               !!!next-token;  
             }  
           }  
         } else {  
           !!!next-token;  
         }  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
           !!!next-token;  
         }  
         if (length $text) {  
           $el->manakai_append_text ($text);  
         }  
           
         $self->{content_model_flag} = 'PCDATA';  
           
         if ($token->{type} eq 'end tag' and  
             $token->{tag_name} eq $tag_name) {  
           ## Ignore the token  
         } else {  
           if ($token->{tag_name} eq 'textarea') {  
             !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
           } else {  
             !!!parse-error (type => 'in CDATA:#'.$token->{type});  
           }  
           ## ISSUE: And ignore?  
         }  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'select') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $self->{insertion_mode} = 'in select';  
         !!!next-token;  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'in body:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: An issue on HTML5 new elements in the spec.  
       } else {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         !!!next-token;  
         return;  
       }  
     } elsif ($token->{type} eq 'end tag') {  
       if ($token->{tag_name} eq 'body') {  
         if (@{$self->{open_elements}} > 1 and  
             $self->{open_elements}->[1]->[1] eq 'body') {  
           for (@{$self->{open_elements}}) {  
             unless ({  
                        dd => 1, dt => 1, li => 1, p => 1, td => 1,  
                        th => 1, tr => 1, body => 1, html => 1,  
                     }->{$_->[1]}) {  
               !!!parse-error (type => 'not closed:'.$_->[1]);  
             }  
           }  
   
           $self->{insertion_mode} = 'after body';  
           !!!next-token;  
           return;  
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'html') {  
         if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {  
           ## ISSUE: There is an issue in the spec.  
           if ($self->{open_elements}->[-1]->[1] ne 'body') {  
             !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);  
           }  
           $self->{insertion_mode} = 'after body';  
           ## reprocess  
           return;  
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           return;  
         }  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, pre => 1, ul => 1,  
                 p => 1,  
                 dd => 1, dt => 1, li => 1,  
                 button => 1, marquee => 1, object => 1,  
                }->{$token->{tag_name}}) {  
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq $token->{tag_name}) {  
             ## generate implied end tags  
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  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  
               return;  
             }  
             $i = $_;  
             last INSCOPE unless $token->{tag_name} eq 'p';  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         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 if defined $i;  
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         ## has an element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq $token->{tag_name}) {  
             ## 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  
               return;  
             }  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {  
           pop @{$self->{open_elements}};  
         } else {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
   
         undef $self->{form_element};  
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ({  
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             ## 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  
               return;  
             }  
             $i = $_;  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         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 if defined $i;  
         !!!next-token;  
         return;  
       } elsif ({  
                 a => 1,  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 nobr => 1, s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $formatting_end_tag->($token->{tag_name});  
 ## TODO: <http://html5.org/tools/web-apps-tracker?from=883&to=884>  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, hr => 1, iframe => 1, image => 1,  
                 img => 1, input => 1, isindex => 1, noembed => 1,  
                 noframes => 1, param => 1, select => 1, spacer => 1,  
                 table => 1, textarea => 1, wbr => 1,  
                 noscript => 0, ## TODO: if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: Issue on HTML5 new elements in spec  
           
       } else {  
         ## Step 1  
         my $node_i = -1;  
         my $node = $self->{open_elements}->[$node_i];  
   
         ## Step 2  
         S2: {  
           if ($node->[1] eq $token->{tag_name}) {  
             ## Step 1  
             ## 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  
               return;  
             }  
           
             ## Step 2  
             if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
             }  
               
             ## Step 3  
             splice @{$self->{open_elements}}, $node_i;  
   
             !!!next-token;  
             last S2;  
           } else {  
             ## Step 3  
             if (not $formatting_category->{$node->[1]} and  
                 #not $phrasing_category->{$node->[1]} and  
                 ($special_category->{$node->[1]} or  
                  $scoping_category->{$node->[1]})) {  
               !!!parse-error (type => 'not closed:'.$node->[1]);  
               ## Ignore the token  
               !!!next-token;  
               last S2;  
             }  
           }  
             
           ## Step 4  
           $node_i--;  
           $node = $self->{open_elements}->[$node_i];  
             
           ## Step 5;  
           redo S2;  
         } # S2  
         return;  
       }  
     }  
   }; # $in_body  
2498    
2499    B: {    B: {
2500      if ($phase eq 'main') {      if ($token->{type} eq 'DOCTYPE') {
2501        if ($token->{type} eq 'DOCTYPE') {        !!!parse-error (type => 'DOCTYPE in the middle');
2502          !!!parse-error (type => 'in html:#DOCTYPE');        ## Ignore the token
2503          ## Ignore the token        ## Stay in the phase
2504          ## Stay in the phase        !!!next-token;
2505          !!!next-token;        redo B;
2506          redo B;      } elsif ($token->{type} eq 'end-of-file') {
2507        } elsif ($token->{type} eq 'start tag' and        if ($self->{insertion_mode} eq 'after html body' or
2508                 $token->{tag_name} eq 'html') {            $self->{insertion_mode} eq 'after html frameset') {
2509          ## TODO: unless it is the first start tag token, parse-error          #
2510          my $top_el = $self->{open_elements}->[0]->[0];        } else {
         for my $attr_name (keys %{$token->{attributes}}) {  
           unless ($top_el->has_attribute_ns (undef, $attr_name)) {  
             $top_el->set_attribute_ns  
               (undef, [undef, $attr_name],  
                $token->{attributes}->{$attr_name}->{value});  
           }  
         }  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'end-of-file') {  
2511          ## Generate implied end tags          ## Generate implied end tags
2512          if ({          if ({
2513               dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,               dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,
2514                 tbody => 1, tfoot=> 1, thead => 1,
2515              }->{$self->{open_elements}->[-1]->[1]}) {              }->{$self->{open_elements}->[-1]->[1]}) {
2516            !!!back-token;            !!!back-token;
2517            $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};            $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};
# Line 3259  sub _tree_construction_main ($) { Line 2527  sub _tree_construction_main ($) {
2527            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
2528          }          }
2529    
         ## Stop parsing  
         last B;  
   
2530          ## ISSUE: There is an issue in the spec.          ## ISSUE: There is an issue in the spec.
2531          }
2532    
2533          ## Stop parsing
2534          last B;
2535        } elsif ($token->{type} eq 'start tag' and
2536                 $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 {        } 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') {          if ($self->{insertion_mode} eq 'before head') {
2588            if ($token->{type} eq 'character') {            ## As if <head>
2589              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {            !!!create-element ($self->{head_element}, 'head');
2590                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2591                unless (length $token->{data}) {            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2592                  !!!next-token;  
2593                  redo B;            ## Reprocess in the "in head" insertion mode...
2594                }            pop @{$self->{open_elements}};
2595              }  
2596              ## As if <head>            ## Reprocess in the "after head" insertion mode...
2597              !!!create-element ($self->{head_element}, 'head');          } elsif ($self->{insertion_mode} eq 'in head noscript') {
2598              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            ## As if </noscript>
2599              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            pop @{$self->{open_elements}};
2600              $self->{insertion_mode} = 'in head';            !!!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              ## reprocess
2618              redo B;              redo B;
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
2619            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
             my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};  
             !!!create-element ($self->{head_element}, 'head', $attr);  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
2620              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
2621                !!!next-token;                if ($self->{insertion_mode} eq 'before head') {
2622              #} elsif ({                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
2623              #          base => 1, link => 1, meta => 1,                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2624              #          script => 1, style => 1, title => 1,                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
2625              #         }->{$token->{tag_name}}) {                  $self->{insertion_mode} = 'in head';
2626              #  ## reprocess                  !!!next-token;
2627              } else {                  redo B;
2628                ## reprocess                } elsif ($self->{insertion_mode} ne 'after head') {
2629              }                  !!!parse-error (type => 'in head:head'); # or in head noscript
2630              redo B;                  ## Ignore the token
2631            } elsif ($token->{type} eq 'end tag') {                  !!!next-token;
2632              if ($token->{tag_name} eq 'html') {                  redo B;
2633                  } else {
2634                    #
2635                  }
2636                } elsif ($self->{insertion_mode} eq 'before head') {
2637                ## As if <head>                ## As if <head>
2638                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
2639                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2640                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2641    
2642                $self->{insertion_mode} = 'in head';                $self->{insertion_mode} = 'in head';
2643                ## reprocess                ## Reprocess in the "in head" insertion mode...
               redo B;  
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
2644              }              }
           } 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  
2645    
2646                my $text = '';              if ($token->{tag_name} eq 'base') {
2647                !!!next-token;                if ($self->{insertion_mode} eq 'in head noscript') {
2648                while ($token->{type} eq 'character') {                  ## As if </noscript>
2649                  $text .= $token->{data};                  pop @{$self->{open_elements}};
2650                  !!!next-token;                  !!!parse-error (type => 'in noscript:base');
               }  
               if (length $text) {  
                 $title_el->manakai_append_text ($text);  
               }  
                 
               $self->{content_model_flag} = 'PCDATA';  
2651                                
2652                if ($token->{type} eq 'end tag' and                  $self->{insertion_mode} = 'in head';
2653                    $token->{tag_name} eq 'title') {                  ## Reprocess in the "in head" insertion mode...
                 ## Ignore the token  
               } else {  
                 !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
                 ## ISSUE: And ignore?  
2654                }                }
               !!!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});  
               (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
                 ->append_child ($el);  
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;                !!!next-token;
2666                redo B;                redo B;
2667              } elsif ($token->{tag_name} eq 'head') {              } elsif ($token->{tag_name} eq 'link') {
2668                !!!parse-error (type => 'in head:head');                ## NOTE: There is a "as if in head" code clone.
2669                ## Ignore the token                if ($self->{insertion_mode} eq 'after head') {
2670                !!!next-token;                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2671                redo B;                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
             } 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');  
2672                }                }
2673                $self->{insertion_mode} = 'after head';                !!!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;                !!!next-token;
2678                redo B;                redo B;
2679              } elsif ($token->{tag_name} eq 'html') {              } elsif ($token->{tag_name} eq 'meta') {
2680                #                ## NOTE: There is a "as if in head" code clone.
2681              } else {                if ($self->{insertion_mode} eq 'after head') {
2682                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2683                ## Ignore the token                  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;                !!!next-token;
2709                redo B;                redo B;
2710              }              } elsif ($token->{tag_name} eq 'title') {
2711            } else {                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            if ($self->{open_elements}->[-1]->[1] eq 'head') {                
2716              ## As if </head>                  $self->{insertion_mode} = 'in head';
2717              pop @{$self->{open_elements}};                  ## Reprocess in the "in head" insertion mode...
2718            }                } elsif ($self->{insertion_mode} eq 'after head') {
2719            $self->{insertion_mode} = 'after head';                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2720            ## reprocess                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2721            redo B;                }
2722    
2723            ## ISSUE: An issue in the spec.                ## NOTE: There is a "as if in head" code clone.
2724          } elsif ($self->{insertion_mode} eq 'after head') {                my $parent = defined $self->{head_element} ? $self->{head_element}
2725            if ($token->{type} eq 'character') {                    : $self->{open_elements}->[-1]->[0];
2726              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                $parse_rcdata->(RCDATA_CONTENT_MODEL,
2727                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                                sub { $parent->append_child ($_[0]) });
2728                unless (length $token->{data}) {                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;                  !!!next-token;
2749                  redo B;                  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            } elsif ($token->{type} eq 'comment') {                  pop @{$self->{open_elements}};
2762              my $comment = $self->{document}->create_comment ($token->{data});                  !!!parse-error (type => 'in noscript:script');
2763              $self->{open_elements}->[-1]->[0]->append_child ($comment);                
2764              !!!next-token;                  $self->{insertion_mode} = 'in head';
2765              redo B;                  ## Reprocess in the "in head" insertion mode...
2766            } elsif ($token->{type} eq 'start tag') {                } elsif ($self->{insertion_mode} eq 'after head') {
2767              if ($token->{tag_name} eq 'body') {                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2768                !!!insert-element ('body', $token->{attributes});                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2769                $self->{insertion_mode} = 'in body';                }
2770                !!!next-token;  
2771                redo B;                ## NOTE: There is a "as if in head" code clone.
2772              } elsif ($token->{tag_name} eq 'frameset') {                $script_start_tag->($insert_to_current);
2773                !!!insert-element ('frameset', $token->{attributes});                pop @{$self->{open_elements}}
2774                $self->{insertion_mode} = 'in frameset';                    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;                !!!next-token;
2798                redo B;                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;  
2799              } else {              } else {
2800                #                #
2801              }              }
           } 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});  
2802    
2803              !!!next-token;              if ($self->{insertion_mode} eq 'in head noscript') {
2804              redo B;                ## As if </noscript>
2805            } elsif ($token->{type} eq 'comment') {                pop @{$self->{open_elements}};
2806              ## NOTE: There is a code clone of "comment in body".                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
             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);  
2807                                
2808                unless (length $token->{data}) {                ## Reprocess in the "in head" insertion mode...
2809                  !!!next-token;                ## As if </head>
2810                  redo B;                pop @{$self->{open_elements}};
               }  
             }  
2811    
2812              !!!parse-error (type => 'in table:#character');                ## 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              ## As if in body, but insert into foster parent element                ## Reprocess in the "after head" insertion mode...
             ## 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});  
2818              }              }
               
             !!!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}};  
               }  
2819    
2820                push @$active_formatting_elements, ['#marker', '']              ## "after head" insertion mode
2821                  if $token->{tag_name} eq 'caption';              ## 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                !!!insert-element ($token->{tag_name}, $token->{attributes});                  ## Reprocess in the "in head" insertion mode...
2835                $self->{insertion_mode} = {                  pop @{$self->{open_elements}};
2836                                   caption => 'in caption',                  $self->{insertion_mode} = 'after head';
2837                                   colgroup => 'in column group',                  !!!next-token;
2838                                   tbody => 'in table body',                  redo B;
2839                                   tfoot => 'in table body',                } elsif ($self->{insertion_mode} eq 'in head noscript') {
2840                                   thead => 'in table body',                  ## As if </noscript>
2841                                  }->{$token->{tag_name}};                  pop @{$self->{open_elements}};
2842                !!!next-token;                  !!!parse-error (type => 'in noscript:script');
2843                redo B;                  
2844              } elsif ({                  ## Reprocess in the "in head" insertion mode...
2845                        col => 1,                  pop @{$self->{open_elements}};
2846                        td => 1, th => 1, tr => 1,                  $self->{insertion_mode} = 'after head';
2847                       }->{$token->{tag_name}}) {                  !!!next-token;
2848                ## Clear back to table context                  redo B;
2849                while ($self->{open_elements}->[-1]->[1] ne 'table' and                } elsif ($self->{insertion_mode} eq 'in head') {
                      $self->{open_elements}->[-1]->[1] ne 'html') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
2850                  pop @{$self->{open_elements}};                  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                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                if ($self->{insertion_mode} eq 'in head noscript') {
2859                $self->{insertion_mode} = $token->{tag_name} eq 'col'                  pop @{$self->{open_elements}};
2860                  ? 'in column group' : 'in table body';                  $self->{insertion_mode} = 'in head';
               ## 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>  
2861                  !!!next-token;                  !!!next-token;
2862                  redo B;                  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                ## generate implied end tags                        body => 1, html => 1,
2873                if ({                       }->{$token->{tag_name}}) {
2874                     dd => 1, dt => 1, li => 1, p => 1,                if ($self->{insertion_mode} eq 'before head') {
2875                     td => 1, th => 1, tr => 1,                  ## As if <head>
2876                    }->{$self->{open_elements}->[-1]->[1]}) {                  !!!create-element ($self->{head_element}, 'head');
2877                  !!!back-token; # <table>                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2878                  $token = {type => 'end tag', tag_name => 'table'};                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2879                  !!!back-token;  
2880                  $token = {type => 'end tag',                  $self->{insertion_mode} = 'in head';
2881                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                  ## 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;                  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                if ($self->{open_elements}->[-1]->[1] ne 'table') {                  $self->{insertion_mode} = 'in head';
2900                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  ## Reprocess in the "in head" insertion mode...
2901                }                }
2902    
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
2903                #                #
2904              }              } else {
2905            } elsif ($token->{type} eq 'end tag') {                if ($self->{insertion_mode} ne 'after head') {
             if ($token->{tag_name} eq 'table') {  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
2906                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2907                  ## Ignore the token                  ## Ignore the token
2908                  !!!next-token;                  !!!next-token;
2909                  redo B;                  redo B;
2910                  } else {
2911                    #
2912                }                }
2913                              }
               ## 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]);  
               }  
2914    
2915                splice @{$self->{open_elements}}, $i;              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                $self->_reset_insertion_mode;                ## 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                !!!next-token;                ## Reprocess in the "after head" insertion mode...
2930                redo B;              } elsif ($self->{insertion_mode} eq 'before head') {
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
2931                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2932                ## Ignore the token                ## Ignore the token ## ISSUE: An issue in the spec.
2933                !!!next-token;                !!!next-token;
2934                redo B;                redo B;
             } else {  
               #  
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 {            } else {
2944              #              die "$0: $token->{type}: Unknown token type";
2945            }            }
2946    
2947            !!!parse-error (type => 'in table:'.$token->{tag_name});            ## ISSUE: An issue in the spec.
2948            $in_body->($insert_to_foster);      } elsif ($self->{insertion_mode} eq 'in body' or
2949            redo B;               $self->{insertion_mode} eq 'in cell' or
2950          } elsif ($self->{insertion_mode} eq 'in caption') {               $self->{insertion_mode} eq 'in caption') {
2951            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
2952              ## NOTE: This is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
2953              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
2954                            
2955              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
2956    
2957              !!!next-token;              !!!next-token;
2958              redo B;              redo B;
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: This is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
2959            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
2960              if ({              if ({
2961                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
2962                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
2963                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
2964                !!!parse-error (type => 'not closed:caption');                if ($self->{insertion_mode} eq 'in cell') {
2965                    ## have an element in table scope
2966                ## As if </caption>                  my $tn;
2967                ## have a table element in table scope                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
2968                my $i;                    my $node = $self->{open_elements}->[$_];
2969                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {
2970                  my $node = $self->{open_elements}->[$_];                      $tn = $node->[1];
2971                  if ($node->[1] eq 'caption') {                      last INSCOPE;
2972                    $i = $_;                    } elsif ({
2973                    last INSCOPE;                              table => 1, html => 1,
2974                  } elsif ({                             }->{$node->[1]}) {
2975                            table => 1, html => 1,                      last INSCOPE;
2976                           }->{$node->[1]}) {                    }
2977                    last INSCOPE;                  } # INSCOPE
2978                  }                    unless (defined $tn) {
2979                } # INSCOPE                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2980                unless (defined $i) {                      ## Ignore the token
2981                  !!!parse-error (type => 'unmatched end tag:caption');                      !!!next-token;
2982                  ## Ignore the token                      redo B;
2983                  !!!next-token;                    }
2984                  redo B;                  
2985                }                  ## Close the cell
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
2986                  !!!back-token; # <?>                  !!!back-token; # <?>
2987                  $token = {type => 'end tag', tag_name => 'caption'};                  $token = {type => 'end tag', tag_name => $tn};
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
2988                  redo B;                  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') {                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3028                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!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                }                }
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               ## reprocess  
               redo B;  
3042              } else {              } else {
3043                #                #
3044              }              }
3045            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
3046              if ($token->{tag_name} eq 'caption') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3047                ## have a table element in table scope                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;                my $i;
3163                  my $tn;
3164                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3165                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3166                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
3167                    $i = $_;                    $i = $_;
3168                    last INSCOPE;                    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 ({                  } elsif ({
3174                            table => 1, html => 1,                            table => 1, html => 1,
3175                           }->{$node->[1]}) {                           }->{$node->[1]}) {
# Line 3798  sub _tree_construction_main ($) { Line 3182  sub _tree_construction_main ($) {
3182                  !!!next-token;                  !!!next-token;
3183                  redo B;                  redo B;
3184                }                }
                 
               ## 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;  
3185    
3186                $clear_up_to_marker->();                ## Close the cell
3187                  !!!back-token; # </?>
3188                $self->{insertion_mode} = 'in table';                $token = {type => 'end tag', tag_name => $tn};
   
               !!!next-token;  
3189                redo B;                redo B;
3190              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table' and
3191                         $self->{insertion_mode} eq 'in caption') {
3192                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption');
3193    
3194                ## As if </caption>                ## As if </caption>
# Line 3850  sub _tree_construction_main ($) { Line 3216  sub _tree_construction_main ($) {
3216                if ({                if ({
3217                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3218                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3219                       tbody => 1, tfoot=> 1, thead => 1,
3220                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3221                  !!!back-token; # </table>                  !!!back-token; # </table>
3222                  $token = {type => 'end tag', tag_name => 'caption'};                  $token = {type => 'end tag', tag_name => 'caption'};
# Line 3872  sub _tree_construction_main ($) { Line 3239  sub _tree_construction_main ($) {
3239                ## reprocess                ## reprocess
3240                redo B;                redo B;
3241              } elsif ({              } elsif ({
3242                        body => 1, col => 1, colgroup => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
                       html => 1, tbody => 1, td => 1, tfoot => 1,  
                       th => 1, thead => 1, tr => 1,  
3243                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3244                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} eq 'in cell' or
3245                ## Ignore the token                    $self->{insertion_mode} eq 'in caption') {
3246                redo B;                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
             } else {  
               #  
             }  
           } else {  
             #  
           }  
                 
           $in_body->($insert_to_current);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in column group') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'col') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'colgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html') {  
                 !!!parse-error (type => 'unmatched end tag:colgroup');  
3247                  ## Ignore the token                  ## Ignore the token
3248                  !!!next-token;                  !!!next-token;
3249                  redo B;                  redo B;
3250                } else {                } else {
3251                  pop @{$self->{open_elements}}; # colgroup                  #
                 $self->{insertion_mode} = 'in table';  
                 !!!next-token;  
                 redo B;              
3252                }                }
3253              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
3254                !!!parse-error (type => 'unmatched end tag:col');                        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                ## Ignore the token
3260                !!!next-token;                !!!next-token;
3261                redo B;                redo B;
3262              } else {              } else {
3263                #                #
3264              }              }
3265            } else {        } else {
3266              #          die "$0: $token->{type}: Unknown token type";
3267            }        }
3268    
3269            ## As if </colgroup>        $insert = $insert_to_current;
3270            if ($self->{open_elements}->[-1]->[1] eq 'html') {        #
3271              !!!parse-error (type => 'unmatched end tag:colgroup');      } elsif ($self->{insertion_mode} eq 'in row' or
3272              ## Ignore the token               $self->{insertion_mode} eq 'in table body' or
3273              !!!next-token;               $self->{insertion_mode} eq 'in table') {
             redo B;  
           } else {  
             pop @{$self->{open_elements}}; # colgroup  
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table body') {  
3274            if ($token->{type} eq 'character') {            if ($token->{type} eq 'character') {
3275              ## NOTE: This is a "character in table" code clone.              ## NOTE: There are "character in table" code clones.
3276              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3277                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3278                                
# Line 3969  sub _tree_construction_main ($) { Line 3289  sub _tree_construction_main ($) {
3289              ## into the current node" while characters might not be              ## into the current node" while characters might not be
3290              ## result in a new Text node.              ## result in a new Text node.
3291              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
3292                
3293              if ({              if ({
3294                   table => 1, tbody => 1, tfoot => 1,                   table => 1, tbody => 1, tfoot => 1,
3295                   thead => 1, tr => 1,                   thead => 1, tr => 1,
# Line 4009  sub _tree_construction_main ($) { Line 3329  sub _tree_construction_main ($) {
3329                            
3330              !!!next-token;              !!!next-token;
3331              redo B;              redo B;
           } elsif ($token->{type} eq 'comment') {  
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
3332            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
3333              if ({              if ({
3334                   tr => 1,                   tr => ($self->{insertion_mode} ne 'in row'),
3335                   th => 1, td => 1,                   th => 1, td => 1,
3336                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3337                unless ($token->{tag_name} eq 'tr') {                if ($self->{insertion_mode} eq 'in table') {
3338                  !!!parse-error (type => 'missing start tag:tr');                  ## 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                ## Clear back to table body context                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 {                while (not {
3376                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tr => 1, html => 1,
3377                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
3378                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3379                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3380                }                }
3381                                
3382                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes});
3383                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = 'in cell';
3384                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
3385                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
3386                } else {                
3387                  !!!insert-element ('tr');                !!!next-token;
                 ## reprocess  
               }  
3388                redo B;                redo B;
3389              } elsif ({              } elsif ({
3390                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
3391                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3392                          tr => 1, # $self->{insertion_mode} eq 'in row'
3393                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3394                ## have an element in table scope                if ($self->{insertion_mode} eq 'in row') {
3395                my $i;                  ## As if </tr>
3396                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
3397                  my $node = $self->{open_elements}->[$_];                  my $i;
3398                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3399                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
3400                      }->{$node->[1]}) {                    if ($node->[1] eq 'tr') {
3401                    $i = $_;                      $i = $_;
3402                    last INSCOPE;                      last INSCOPE;
3403                  } elsif ({                    } elsif ({
3404                            table => 1, html => 1,                              table => 1, html => 1,
3405                           }->{$node->[1]}) {                             }->{$node->[1]}) {
3406                    last INSCOPE;                      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                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
3432                }                }
3433    
3434                ## Clear back to table body context                if ($self->{insertion_mode} eq 'in table body') {
3435                while (not {                  ## have an element in table scope
3436                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
3437                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3438                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    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}};                  pop @{$self->{open_elements}};
3473                    $self->{insertion_mode} = 'in table';
3474                    ## reprocess in "in table" insertion mode...
3475                }                }
3476    
3477                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
3478                ## have an element in table scope                  ## Clear back to table context
3479                ## true by definition                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
3480                           $self->{open_elements}->[-1]->[1] ne 'html') {
3481                ## Clear back to table body context                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3482                ## nop by definition                    pop @{$self->{open_elements}};
3483                    }
3484                pop @{$self->{open_elements}};                  
3485                $self->{insertion_mode} = 'in table';                  !!!insert-element ('colgroup');
3486                ## reprocess                  $self->{insertion_mode} = 'in column group';
3487                redo B;                  ## 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') {              } elsif ($token->{tag_name} eq 'table') {
3518                ## NOTE: This is a code clone of "table in table"                ## NOTE: There are code clones for this "table in table"
3519                !!!parse-error (type => 'not closed:table');                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3520    
3521                ## As if </table>                ## As if </table>
3522                ## have a table element in table scope                ## have a table element in table scope
# Line 4115  sub _tree_construction_main ($) { Line 3543  sub _tree_construction_main ($) {
3543                if ({                if ({
3544                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3545                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3546                       tbody => 1, tfoot=> 1, thead => 1,
3547                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3548                  !!!back-token; # <table>                  !!!back-token; # <table>
3549                  $token = {type => 'end tag', tag_name => 'table'};                  $token = {type => 'end tag', tag_name => 'table'};
# Line 4130  sub _tree_construction_main ($) { Line 3559  sub _tree_construction_main ($) {
3559    
3560                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
3561    
3562                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
3563    
3564                ## reprocess                ## reprocess
3565                redo B;                redo B;
# Line 4138  sub _tree_construction_main ($) { Line 3567  sub _tree_construction_main ($) {
3567                #                #
3568              }              }
3569            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
3570              if ({              if ($token->{tag_name} eq 'tr' and
3571                   tbody => 1, tfoot => 1, thead => 1,                  $self->{insertion_mode} eq 'in row') {
                 }->{$token->{tag_name}}) {  
3572                ## have an element in table scope                ## have an element in table scope
3573                my $i;                my $i;
3574                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 4161  sub _tree_construction_main ($) { Line 3589  sub _tree_construction_main ($) {
3589                  redo B;                  redo B;
3590                }                }
3591    
3592                ## Clear back to table body context                ## Clear back to table row context
3593                while (not {                while (not {
3594                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tr => 1, html => 1,
3595                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
3596                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3597                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3598                }                }
3599    
3600                pop @{$self->{open_elements}};                pop @{$self->{open_elements}}; # tr
3601                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = 'in table body';
3602                !!!next-token;                !!!next-token;
3603                redo B;                redo B;
3604              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3605                ## have an element in table scope                if ($self->{insertion_mode} eq 'in row') {
3606                my $i;                  ## As if </tr>
3607                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
3608                  my $node = $self->{open_elements}->[$_];                  my $i;
3609                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3610                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
3611                      }->{$node->[1]}) {                    if ($node->[1] eq 'tr') {
3612                    $i = $_;                      $i = $_;
3613                    last INSCOPE;                      last INSCOPE;
3614                  } elsif ({                    } elsif ({
3615                            table => 1, html => 1,                              table => 1, html => 1,
3616                           }->{$node->[1]}) {                             }->{$node->[1]}) {
3617                    last INSCOPE;                      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                } # INSCOPE                  
3627                unless (defined $i) {                  ## Clear back to table row context
3628                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  while (not {
3629                  ## Ignore the token                    tr => 1, html => 1,
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, td => 1, th => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           ## As if in table  
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in row') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
3630                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
3631                # MUST                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3632                my $foster_parent_element;                    pop @{$self->{open_elements}};
3633                my $next_sibling;                  }
3634                my $prev_sibling;                  
3635                OE: for (reverse 0..$#{$self->{open_elements}}) {                  pop @{$self->{open_elements}}; # tr
3636                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  $self->{insertion_mode} = 'in table body';
3637                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                  ## reprocess in the "in table body" insertion mode...
3638                    if (defined $parent and $parent->node_type == 1) {                }
3639                      $foster_parent_element = $parent;  
3640                      $next_sibling = $self->{open_elements}->[$_]->[0];                if ($self->{insertion_mode} eq 'in table body') {
3641                      $prev_sibling = $next_sibling->previous_sibling;                  ## have an element in table scope
3642                    } else {                  my $i;
3643                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3644                      $prev_sibling = $foster_parent_element->last_child;                    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                    last OE;                  } # 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                } # OE                  
3663                $foster_parent_element = $self->{open_elements}->[0]->[0] and                  ## Clear back to table body context
3664                $prev_sibling = $foster_parent_element->last_child                  while (not {
3665                  unless defined $foster_parent_element;                    tbody => 1, tfoot => 1, thead => 1, html => 1,
3666                if (defined $prev_sibling and                  }->{$self->{open_elements}->[-1]->[1]}) {
3667                    $prev_sibling->node_type == 3) {                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3668                  $prev_sibling->manakai_append_text ($token->{data});                    pop @{$self->{open_elements}};
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'th' or  
                 $token->{tag_name} eq 'td') {  
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = 'in cell';  
   
               push @$active_formatting_elements, ['#marker', ''];  
                 
               !!!next-token;  
               redo B;  
             } elsif ({  
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
3669                  }                  }
3670                } # INSCOPE                  
3671                unless (defined $i) {                  ## As if <{current node}>
3672                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  ## have an element in table scope
3673                  ## Ignore the token                  ## true by definition
3674                  !!!next-token;                  
3675                  redo B;                  ## Clear back to table body context
3676                }                  ## nop by definition
3677                    
               ## 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]);  
3678                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3679                    $self->{insertion_mode} = 'in table';
3680                    ## reprocess in the "in table" insertion mode...
3681                }                }
3682    
               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>  
3683                ## have a table element in table scope                ## have a table element in table scope
3684                my $i;                my $i;
3685                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3686                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3687                  if ($node->[1] eq 'table') {                  if ($node->[1] eq $token->{tag_name}) {
3688                    $i = $_;                    $i = $_;
3689                    last INSCOPE;                    last INSCOPE;
3690                  } elsif ({                  } elsif ({
# Line 4373  sub _tree_construction_main ($) { Line 3694  sub _tree_construction_main ($) {
3694                  }                  }
3695                } # INSCOPE                } # INSCOPE
3696                unless (defined $i) {                unless (defined $i) {
3697                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3698                  ## Ignore tokens </table><table>                  ## Ignore the token
3699                  !!!next-token;                  !!!next-token;
3700                  redo B;                  redo B;
3701                }                }
3702                  
3703                ## generate implied end tags                ## generate implied end tags
3704                if ({                if ({
3705                     dd => 1, dt => 1, li => 1, p => 1,                     dd => 1, dt => 1, li => 1, p => 1,
3706                     td => 1, th => 1, tr => 1,                     td => 1, th => 1, tr => 1,
3707                       tbody => 1, tfoot=> 1, thead => 1,
3708                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
3709                  !!!back-token;                  !!!back-token;
3710                  $token = {type => 'end tag',                  $token = {type => 'end tag',
3711                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3712                  redo B;                  redo B;
3713                }                }
3714                  
3715                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
3716                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3717                }                }
3718                    
3719                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
3720                  
3721                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
3722                  
               ## 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';  
3723                !!!next-token;                !!!next-token;
3724                redo B;                redo B;
             } elsif ($token->{tag_name} eq 'table') {  
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{type});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
3725              } elsif ({              } elsif ({
3726                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3727                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
3728                ## have an element in table scope                       ($self->{insertion_mode} eq 'in row' or
3729                my $i;                        $self->{insertion_mode} eq 'in table body')) {
3730                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                if ($self->{insertion_mode} eq 'in row') {
3731                  my $node = $self->{open_elements}->[$_];                  ## have an element in table scope
3732                  if ($node->[1] eq $token->{tag_name}) {                  my $i;
3733                    $i = $_;                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3734                    last INSCOPE;                    my $node = $self->{open_elements}->[$_];
3735                  } elsif ({                    if ($node->[1] eq $token->{tag_name}) {
3736                            table => 1, html => 1,                      $i = $_;
3737                           }->{$node->[1]}) {                      last INSCOPE;
3738                    last INSCOPE;                    } 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                } # INSCOPE                  
3780                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
3781                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = 'in table body';
3782                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
3783                }                }
3784    
               ## As if </tr>  
3785                ## have an element in table scope                ## have an element in table scope
3786                my $i;                my $i;
3787                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3788                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
3789                  if ($node->[1] eq 'tr') {                  if ($node->[1] eq $token->{tag_name}) {
3790                    $i = $_;                    $i = $_;
3791                    last INSCOPE;                    last INSCOPE;
3792                  } elsif ({                  } elsif ({
# Line 4511  sub _tree_construction_main ($) { Line 3796  sub _tree_construction_main ($) {
3796                  }                  }
3797                } # INSCOPE                } # INSCOPE
3798                unless (defined $i) {                unless (defined $i) {
3799                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3800                  ## Ignore the token                  ## Ignore the token
3801                  !!!next-token;                  !!!next-token;
3802                  redo B;                  redo B;
3803                }                }
3804    
3805                ## Clear back to table row context                ## Clear back to table body context
3806                while (not {                while (not {
3807                  tr => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
3808                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
3809                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3810                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3811                }                }
3812    
3813                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
3814                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = 'in table';
3815                ## reprocess                !!!next-token;
3816                redo B;                redo B;
3817              } elsif ({              } elsif ({
3818                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
3819                        colgroup => 1, html => 1, td => 1, th => 1,                        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}}) {                       }->{$token->{tag_name}}) {
3823                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3824                ## Ignore the token                ## Ignore the token
# Line 4541  sub _tree_construction_main ($) { Line 3828  sub _tree_construction_main ($) {
3828                #                #
3829              }              }
3830            } else {            } else {
3831              #              die "$0: $token->{type}: Unknown token type";
3832            }            }
3833    
3834            ## As if in table        !!!parse-error (type => 'in table:'.$token->{tag_name});
           !!!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});  
3835    
3836              !!!next-token;        $insert = $insert_to_foster;
3837              redo B;        #
3838            } elsif ($token->{type} eq 'comment') {      } elsif ($self->{insertion_mode} eq 'in column group') {
3839              ## NOTE: This is a code clone of "comment in body".            if ($token->{type} eq 'character') {
3840              my $comment = $self->{document}->create_comment ($token->{data});              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3841              $self->{open_elements}->[-1]->[0]->append_child ($comment);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3842              !!!next-token;                unless (length $token->{data}) {
             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  
3843                  !!!next-token;                  !!!next-token;
3844                  redo B;                  redo B;
3845                }                }
3846                }
3847                ## Close the cell              
3848                !!!back-token; # <?>              #
3849                $token = {type => 'end tag', tag_name => $tn};            } 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;                redo B;
3855              } else {              } else {
3856                #                #
3857              }              }
3858            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} eq 'end tag') {
3859              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
3860                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] eq 'html') {
3861                my $i;                  !!!parse-error (type => 'unmatched end tag:colgroup');
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
3862                  ## Ignore the token                  ## Ignore the token
3863                  !!!next-token;                  !!!next-token;
3864                  redo B;                  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                ## generate implied end tags                !!!parse-error (type => 'unmatched end tag:col');
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => ($token->{tag_name} eq 'th'),  
                    th => ($token->{tag_name} eq 'td'),  
                    tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in row';  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1,  
                       colgroup => 1, html => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
3873                ## Ignore the token                ## Ignore the token
3874                !!!next-token;                !!!next-token;
3875                redo B;                redo B;
             } elsif ({  
                       table => 1, tbody => 1, tfoot => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   ## NOTE: There is exactly one |td| or |th| element  
                   ## in scope in the stack of open elements by definition.  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
3876              } else {              } else {
3877                #                #
3878              }              }
3879            } else {            } else {
3880              #              #
3881            }            }
3882              
3883            $in_body->($insert_to_current);            ## As if </colgroup>
3884            redo B;            if ($self->{open_elements}->[-1]->[1] eq 'html') {
3885          } elsif ($self->{insertion_mode} eq 'in select') {              !!!parse-error (type => 'unmatched end tag:colgroup');
3886            if ($token->{type} eq 'character') {              ## Ignore the token
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
3887              !!!next-token;              !!!next-token;
3888              redo B;              redo B;
3889            } elsif ($token->{type} eq 'comment') {            } else {
3890              my $comment = $self->{document}->create_comment ($token->{data});              pop @{$self->{open_elements}}; # colgroup
3891              $self->{open_elements}->[-1]->[0]->append_child ($comment);              $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;              !!!next-token;
3899              redo B;              redo B;
3900            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} eq 'start tag') {
# Line 4872  sub _tree_construction_main ($) { Line 4066  sub _tree_construction_main ($) {
4066            ## Ignore the token            ## Ignore the token
4067            !!!next-token;            !!!next-token;
4068            redo B;            redo B;
4069          } elsif ($self->{insertion_mode} eq 'after body') {      } elsif ($self->{insertion_mode} eq 'after body' or
4070            if ($token->{type} eq 'character') {               $self->{insertion_mode} eq 'after html body') {
4071              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        if ($token->{type} eq 'character') {
4072                ## As if in body          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4073                $reconstruct_active_formatting_elements->($insert_to_current);            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 ($token->{data});            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4078              
4079              unless (length $token->{data}) {
4080                !!!next-token;
4081                redo B;
4082              }
4083            }
4084            
4085            if ($self->{insertion_mode} eq 'after html body') {
4086              !!!parse-error (type => 'after html:#character');
4087    
4088                unless (length $token->{data}) {            ## Reprocess in the "main" phase, "after body" insertion mode...
4089                  !!!next-token;          }
4090                  redo B;          
4091                }          ## "after body" insertion mode
4092              }          !!!parse-error (type => 'after body:#character');
4093                
4094              #          $self->{insertion_mode} = 'in body';
4095              !!!parse-error (type => 'after body:#'.$token->{type});          ## reprocess
4096            } elsif ($token->{type} eq 'comment') {          redo B;
4097              my $comment = $self->{document}->create_comment ($token->{data});        } elsif ($token->{type} eq 'start tag') {
4098              $self->{open_elements}->[0]->[0]->append_child ($comment);          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;              !!!next-token;
4124              redo B;              redo B;
           } elsif ($token->{type} eq 'start tag') {  
             !!!parse-error (type => 'after body:'.$token->{tag_name});  
             #  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'html') {  
               if (defined $self->{inner_html_node}) {  
                 !!!parse-error (type => 'unmatched end tag:html');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               } else {  
                 $phase = 'trailing end';  
                 !!!next-token;  
                 redo B;  
               }  
             } else {  
               !!!parse-error (type => 'after body:/'.$token->{tag_name});  
             }  
4125            } else {            } else {
4126              !!!parse-error (type => 'after body:#'.$token->{type});              $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';            $self->{insertion_mode} = 'in body';
4134            ## reprocess            ## reprocess
4135            redo B;            redo B;
4136          } elsif ($self->{insertion_mode} eq 'in frameset') {          }
4137            if ($token->{type} eq 'character') {        } else {
4138              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          die "$0: $token->{type}: Unknown token type";
4139                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});        }
4140        } elsif ($self->{insertion_mode} eq 'in frameset' or
4141                unless (length $token->{data}) {               $self->{insertion_mode} eq 'after frameset' or
4142                  !!!next-token;               $self->{insertion_mode} eq 'after html frameset') {
4143                  redo B;        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}) {
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
4148              !!!next-token;              !!!next-token;
4149              redo B;              redo B;
4150            } elsif ($token->{type} eq 'start tag') {            }
4151              if ($token->{tag_name} eq 'frameset') {          }
4152                !!!insert-element ($token->{tag_name}, $token->{attributes});          
4153                !!!next-token;          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
4154                redo B;            if ($self->{insertion_mode} eq 'in frameset') {
4155              } elsif ($token->{tag_name} eq 'frame') {              !!!parse-error (type => 'in frameset:#character');
4156                !!!insert-element ($token->{tag_name}, $token->{attributes});            } elsif ($self->{insertion_mode} eq 'after frameset') {
4157                pop @{$self->{open_elements}};              !!!parse-error (type => 'after frameset:#character');
4158                !!!next-token;            } else { # "after html frameset"
4159                redo B;              !!!parse-error (type => 'after html:#character');
4160              } elsif ($token->{tag_name} eq 'noframes') {  
4161                $in_body->($insert_to_current);              $self->{insertion_mode} = 'after frameset';
4162                redo B;              ## Reprocess in the "main" phase, "after frameset"...
4163              } else {              !!!parse-error (type => 'after frameset:#character');
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'frameset') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html' and  
                   @{$self->{open_elements}} == 1) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
               } else {  
                 pop @{$self->{open_elements}};  
                 !!!next-token;  
               }  
                 
               ## if not inner_html and  
               if ($self->{open_elements}->[-1]->[1] ne 'frameset') {  
                 $self->{insertion_mode} = 'after frameset';  
               }  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
4164            }            }
4165                        
4166            if (defined $token->{tag_name}) {            ## 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            die qq[$0: Character "$token->{data}"];
4176          } 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});              !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4202            } else {            } else {
4203              !!!parse-error (type => 'in frameset:#'.$token->{type});              !!!parse-error (type => 'after frameset:'.$token->{tag_name});
4204            }            }
4205            ## Ignore the token            ## Ignore the token
4206            !!!next-token;            !!!next-token;
4207            redo B;            redo B;
4208          } elsif ($self->{insertion_mode} eq 'after frameset') {          }
4209            if ($token->{type} eq 'character') {        } elsif ($token->{type} eq 'end tag') {
4210              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($self->{insertion_mode} eq 'after html frameset') {
4211                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            !!!parse-error (type => 'after html:/'.$token->{tag_name});
4212    
4213                unless (length $token->{data}) {            $self->{insertion_mode} = 'after frameset';
4214                  !!!next-token;            ## Process in the "main" phase, "after frameset" insertion mode...
4215                  redo B;          }
               }  
             }  
4216    
4217              #          if ($token->{tag_name} eq 'frameset' and
4218            } elsif ($token->{type} eq 'comment') {              $self->{insertion_mode} eq 'in frameset') {
4219              my $comment = $self->{document}->create_comment ($token->{data});            if ($self->{open_elements}->[-1]->[1] eq 'html' and
4220              $self->{open_elements}->[-1]->[0]->append_child ($comment);                @{$self->{open_elements}} == 1) {
4221                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4222                ## Ignore the token
4223              !!!next-token;              !!!next-token;
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'noframes') {  
               $in_body->($insert_to_current);  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'html') {  
               $phase = 'trailing end';  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
4224            } else {            } else {
4225              #              pop @{$self->{open_elements}};
4226                !!!next-token;
4227            }            }
4228              
4229            if (defined $token->{tag_name}) {            if (not defined $self->{inner_html_node} and
4230              !!!parse-error (type => 'after frameset:'.$token->{tag_name});                $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 {
4240              if ($self->{insertion_mode} eq 'in frameset') {
4241                !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
4242            } else {            } else {
4243              !!!parse-error (type => 'after frameset:#'.$token->{type});              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
4244            }            }
4245            ## Ignore the token            ## Ignore the token
4246            !!!next-token;            !!!next-token;
4247            redo B;            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            ## ISSUE: An issue in spec there      ## "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;
4275            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') {
4312            !!!parse-error (type => 'in body:body');
4313                  
4314            if (@{$self->{open_elements}} == 1 or
4315                $self->{open_elements}->[1]->[1] ne 'body') {
4316              ## Ignore the token
4317          } else {          } else {
4318            die "$0: $self->{insertion_mode}: Unknown insertion mode";            my $body_el = $self->{open_elements}->[1]->[0];
4319              for my $attr_name (keys %{$token->{attributes}}) {
4320                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
4321                  $body_el->set_attribute_ns
4322                    (undef, [undef, $attr_name],
4323                     $token->{attributes}->{$attr_name}->{value});
4324                }
4325              }
4326          }          }
       }  
     } 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  
4327          !!!next-token;          !!!next-token;
4328          redo B;          redo B;
4329        } elsif ($token->{type} eq 'comment') {        } elsif ({
4330          my $comment = $self->{document}->create_comment ($token->{data});                  address => 1, blockquote => 1, center => 1, dir => 1,
4331          $self->{document}->append_child ($comment);                  div => 1, dl => 1, fieldset => 1, listing => 1,
4332                    menu => 1, ol => 1, p => 1, ul => 1,
4333                    pre => 1,
4334                   }->{$token->{tag_name}}) {
4335            ## has a p element in scope
4336            INSCOPE: for (reverse @{$self->{open_elements}}) {
4337              if ($_->[1] eq 'p') {
4338                !!!back-token;
4339                $token = {type => 'end tag', tag_name => 'p'};
4340                redo B;
4341              } elsif ({
4342                        table => 1, caption => 1, td => 1, th => 1,
4343                        button => 1, marquee => 1, object => 1, html => 1,
4344                       }->{$_->[1]}) {
4345                last INSCOPE;
4346              }
4347            } # INSCOPE
4348              
4349            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4350            if ($token->{tag_name} eq 'pre') {
4351              !!!next-token;
4352              if ($token->{type} eq 'character') {
4353                $token->{data} =~ s/^\x0A//;
4354                unless (length $token->{data}) {
4355                  !!!next-token;
4356                }
4357              }
4358            } else {
4359              !!!next-token;
4360            }
4361            redo B;
4362          } elsif ($token->{tag_name} eq 'form') {
4363            if (defined $self->{form_element}) {
4364              !!!parse-error (type => 'in form:form');
4365              ## Ignore the token
4366              !!!next-token;
4367              redo B;
4368            } else {
4369              ## has a p element in scope
4370              INSCOPE: for (reverse @{$self->{open_elements}}) {
4371                if ($_->[1] eq 'p') {
4372                  !!!back-token;
4373                  $token = {type => 'end tag', tag_name => 'p'};
4374                  redo B;
4375                } elsif ({
4376                          table => 1, caption => 1, td => 1, th => 1,
4377                          button => 1, marquee => 1, object => 1, html => 1,
4378                         }->{$_->[1]}) {
4379                  last INSCOPE;
4380                }
4381              } # INSCOPE
4382                
4383              !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4384              $self->{form_element} = $self->{open_elements}->[-1]->[0];
4385              !!!next-token;
4386              redo B;
4387            }
4388          } elsif ($token->{tag_name} eq 'li') {
4389            ## has a p element in scope
4390            INSCOPE: for (reverse @{$self->{open_elements}}) {
4391              if ($_->[1] eq 'p') {
4392                !!!back-token;
4393                $token = {type => 'end tag', tag_name => 'p'};
4394                redo B;
4395              } elsif ({
4396                        table => 1, caption => 1, td => 1, th => 1,
4397                        button => 1, marquee => 1, object => 1, html => 1,
4398                       }->{$_->[1]}) {
4399                last INSCOPE;
4400              }
4401            } # INSCOPE
4402              
4403            ## Step 1
4404            my $i = -1;
4405            my $node = $self->{open_elements}->[$i];
4406            LI: {
4407              ## Step 2
4408              if ($node->[1] eq 'li') {
4409                if ($i != -1) {
4410                  !!!parse-error (type => 'end tag missing:'.
4411                                  $self->{open_elements}->[-1]->[1]);
4412                }
4413                splice @{$self->{open_elements}}, $i;
4414                last LI;
4415              }
4416              
4417              ## Step 3
4418              if (not $formatting_category->{$node->[1]} and
4419                  #not $phrasing_category->{$node->[1]} and
4420                  ($special_category->{$node->[1]} or
4421                   $scoping_category->{$node->[1]}) and
4422                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4423                last LI;
4424              }
4425              
4426              ## Step 4
4427              $i--;
4428              $node = $self->{open_elements}->[$i];
4429              redo LI;
4430            } # LI
4431              
4432            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4433          !!!next-token;          !!!next-token;
4434          redo B;          redo B;
4435        } elsif ($token->{type} eq 'character') {        } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {
4436          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          ## has a p element in scope
4437            my $data = $1;          INSCOPE: for (reverse @{$self->{open_elements}}) {
4438            ## As if in the main phase.            if ($_->[1] eq 'p') {
4439            ## NOTE: The insertion mode in the main phase              !!!back-token;
4440            ## just before the phase has been changed to the trailing              $token = {type => 'end tag', tag_name => 'p'};
4441            ## end phase is either "after body" or "after frameset".              redo B;
4442            $reconstruct_active_formatting_elements->($insert_to_current)            } elsif ({
4443              if $phase eq 'main';                      table => 1, caption => 1, td => 1, th => 1,
4444                        button => 1, marquee => 1, object => 1, html => 1,
4445                       }->{$_->[1]}) {
4446                last INSCOPE;
4447              }
4448            } # INSCOPE
4449                        
4450            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);          ## Step 1
4451            my $i = -1;
4452            my $node = $self->{open_elements}->[$i];
4453            LI: {
4454              ## Step 2
4455              if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {
4456                if ($i != -1) {
4457                  !!!parse-error (type => 'end tag missing:'.
4458                                  $self->{open_elements}->[-1]->[1]);
4459                }
4460                splice @{$self->{open_elements}}, $i;
4461                last LI;
4462              }
4463                        
4464              ## Step 3
4465              if (not $formatting_category->{$node->[1]} and
4466                  #not $phrasing_category->{$node->[1]} and
4467                  ($special_category->{$node->[1]} or
4468                   $scoping_category->{$node->[1]}) and
4469                  $node->[1] ne 'address' and $node->[1] ne 'div') {
4470                last LI;
4471              }
4472              
4473              ## Step 4
4474              $i--;
4475              $node = $self->{open_elements}->[$i];
4476              redo LI;
4477            } # LI
4478              
4479            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4480            !!!next-token;
4481            redo B;
4482          } elsif ($token->{tag_name} eq 'plaintext') {
4483            ## has a p element in scope
4484            INSCOPE: for (reverse @{$self->{open_elements}}) {
4485              if ($_->[1] eq 'p') {
4486                !!!back-token;
4487                $token = {type => 'end tag', tag_name => 'p'};
4488                redo B;
4489              } elsif ({
4490                        table => 1, caption => 1, td => 1, th => 1,
4491                        button => 1, marquee => 1, object => 1, html => 1,
4492                       }->{$_->[1]}) {
4493                last INSCOPE;
4494              }
4495            } # INSCOPE
4496              
4497            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4498              
4499            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
4500              
4501            !!!next-token;
4502            redo B;
4503          } elsif ({
4504                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4505                   }->{$token->{tag_name}}) {
4506            ## has a p element in scope
4507            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4508              my $node = $self->{open_elements}->[$_];
4509              if ($node->[1] eq 'p') {
4510                !!!back-token;
4511                $token = {type => 'end tag', tag_name => 'p'};
4512                redo B;
4513              } elsif ({
4514                        table => 1, caption => 1, td => 1, th => 1,
4515                        button => 1, marquee => 1, object => 1, html => 1,
4516                       }->{$node->[1]}) {
4517                last INSCOPE;
4518              }
4519            } # INSCOPE
4520              
4521            ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>
4522            ## has an element in scope
4523            #my $i;
4524            #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4525            #  my $node = $self->{open_elements}->[$_];
4526            #  if ({
4527            #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4528            #      }->{$node->[1]}) {
4529            #    $i = $_;
4530            #    last INSCOPE;
4531            #  } elsif ({
4532            #            table => 1, caption => 1, td => 1, th => 1,
4533            #            button => 1, marquee => 1, object => 1, html => 1,
4534            #           }->{$node->[1]}) {
4535            #    last INSCOPE;
4536            #  }
4537            #} # INSCOPE
4538            #  
4539            #if (defined $i) {
4540            #  !!! parse-error (type => 'in hn:hn');
4541            #  splice @{$self->{open_elements}}, $i;
4542            #}
4543              
4544            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4545              
4546            !!!next-token;
4547            redo B;
4548          } elsif ($token->{tag_name} eq 'a') {
4549            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
4550              my $node = $active_formatting_elements->[$i];
4551              if ($node->[1] eq 'a') {
4552                !!!parse-error (type => 'in a:a');
4553                
4554                !!!back-token;
4555                $token = {type => 'end tag', tag_name => 'a'};
4556                $formatting_end_tag->($token->{tag_name});
4557                
4558                AFE2: for (reverse 0..$#$active_formatting_elements) {
4559                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
4560                    splice @$active_formatting_elements, $_, 1;
4561                    last AFE2;
4562                  }
4563                } # AFE2
4564                OE: for (reverse 0..$#{$self->{open_elements}}) {
4565                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
4566                    splice @{$self->{open_elements}}, $_, 1;
4567                    last OE;
4568                  }
4569                } # OE
4570                last AFE;
4571              } elsif ($node->[0] eq '#marker') {
4572                last AFE;
4573              }
4574            } # AFE
4575              
4576            $reconstruct_active_formatting_elements->($insert_to_current);
4577    
4578            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4579            push @$active_formatting_elements, $self->{open_elements}->[-1];
4580    
4581            !!!next-token;
4582            redo B;
4583          } elsif ({
4584                    b => 1, big => 1, em => 1, font => 1, i => 1,
4585                    s => 1, small => 1, strile => 1,
4586                    strong => 1, tt => 1, u => 1,
4587                   }->{$token->{tag_name}}) {
4588            $reconstruct_active_formatting_elements->($insert_to_current);
4589            
4590            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4591            push @$active_formatting_elements, $self->{open_elements}->[-1];
4592            
4593            !!!next-token;
4594            redo B;
4595          } elsif ($token->{tag_name} eq 'nobr') {
4596            $reconstruct_active_formatting_elements->($insert_to_current);
4597    
4598            ## has a |nobr| element in scope
4599            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4600              my $node = $self->{open_elements}->[$_];
4601              if ($node->[1] eq 'nobr') {
4602                !!!parse-error (type => 'not closed:nobr');
4603                !!!back-token;
4604                $token = {type => 'end tag', tag_name => 'nobr'};
4605                redo B;
4606              } elsif ({
4607                        table => 1, caption => 1, td => 1, th => 1,
4608                        button => 1, marquee => 1, object => 1, html => 1,
4609                       }->{$node->[1]}) {
4610                last INSCOPE;
4611              }
4612            } # INSCOPE
4613            
4614            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4615            push @$active_formatting_elements, $self->{open_elements}->[-1];
4616            
4617            !!!next-token;
4618            redo B;
4619          } elsif ($token->{tag_name} eq 'button') {
4620            ## has a button element in scope
4621            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4622              my $node = $self->{open_elements}->[$_];
4623              if ($node->[1] eq 'button') {
4624                !!!parse-error (type => 'in button:button');
4625                !!!back-token;
4626                $token = {type => 'end tag', tag_name => 'button'};
4627                redo B;
4628              } elsif ({
4629                        table => 1, caption => 1, td => 1, th => 1,
4630                        button => 1, marquee => 1, object => 1, html => 1,
4631                       }->{$node->[1]}) {
4632                last INSCOPE;
4633              }
4634            } # INSCOPE
4635              
4636            $reconstruct_active_formatting_elements->($insert_to_current);
4637              
4638            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4639            push @$active_formatting_elements, ['#marker', ''];
4640    
4641            !!!next-token;
4642            redo B;
4643          } elsif ($token->{tag_name} eq 'marquee' or
4644                   $token->{tag_name} eq 'object') {
4645            $reconstruct_active_formatting_elements->($insert_to_current);
4646            
4647            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4648            push @$active_formatting_elements, ['#marker', ''];
4649            
4650            !!!next-token;
4651            redo B;
4652          } elsif ($token->{tag_name} eq 'xmp') {
4653            $reconstruct_active_formatting_elements->($insert_to_current);
4654            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4655            redo B;
4656          } elsif ($token->{tag_name} eq 'table') {
4657            ## has a p element in scope
4658            INSCOPE: for (reverse @{$self->{open_elements}}) {
4659              if ($_->[1] eq 'p') {
4660                !!!back-token;
4661                $token = {type => 'end tag', tag_name => 'p'};
4662                redo B;
4663              } elsif ({
4664                        table => 1, caption => 1, td => 1, th => 1,
4665                        button => 1, marquee => 1, object => 1, html => 1,
4666                       }->{$_->[1]}) {
4667                last INSCOPE;
4668              }
4669            } # INSCOPE
4670              
4671            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4672              
4673            $self->{insertion_mode} = 'in table';
4674              
4675            !!!next-token;
4676            redo B;
4677          } elsif ({
4678                    area => 1, basefont => 1, bgsound => 1, br => 1,
4679                    embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
4680                    image => 1,
4681                   }->{$token->{tag_name}}) {
4682            if ($token->{tag_name} eq 'image') {
4683              !!!parse-error (type => 'image');
4684              $token->{tag_name} = 'img';
4685            }
4686    
4687            ## NOTE: There is an "as if <br>" code clone.
4688            $reconstruct_active_formatting_elements->($insert_to_current);
4689            
4690            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4691            pop @{$self->{open_elements}};
4692            
4693            !!!next-token;
4694            redo B;
4695          } elsif ($token->{tag_name} eq 'hr') {
4696            ## has a p element in scope
4697            INSCOPE: for (reverse @{$self->{open_elements}}) {
4698              if ($_->[1] eq 'p') {
4699                !!!back-token;
4700                $token = {type => 'end tag', tag_name => 'p'};
4701                redo B;
4702              } elsif ({
4703                        table => 1, caption => 1, td => 1, th => 1,
4704                        button => 1, marquee => 1, object => 1, html => 1,
4705                       }->{$_->[1]}) {
4706                last INSCOPE;
4707              }
4708            } # INSCOPE
4709              
4710            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4711            pop @{$self->{open_elements}};
4712              
4713            !!!next-token;
4714            redo B;
4715          } elsif ($token->{tag_name} eq 'input') {
4716            $reconstruct_active_formatting_elements->($insert_to_current);
4717            
4718            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4719            ## TODO: associate with $self->{form_element} if defined
4720            pop @{$self->{open_elements}};
4721            
4722            !!!next-token;
4723            redo B;
4724          } elsif ($token->{tag_name} eq 'isindex') {
4725            !!!parse-error (type => 'isindex');
4726            
4727            if (defined $self->{form_element}) {
4728              ## Ignore the token
4729              !!!next-token;
4730              redo B;
4731            } else {
4732              my $at = $token->{attributes};
4733              my $form_attrs;
4734              $form_attrs->{action} = $at->{action} if $at->{action};
4735              my $prompt_attr = $at->{prompt};
4736              $at->{name} = {name => 'name', value => 'isindex'};
4737              delete $at->{action};
4738              delete $at->{prompt};
4739              my @tokens = (
4740                            {type => 'start tag', tag_name => 'form',
4741                             attributes => $form_attrs},
4742                            {type => 'start tag', tag_name => 'hr'},
4743                            {type => 'start tag', tag_name => 'p'},
4744                            {type => 'start tag', tag_name => 'label'},
4745                           );
4746              if ($prompt_attr) {
4747                push @tokens, {type => 'character', data => $prompt_attr->{value}};
4748              } else {
4749                push @tokens, {type => 'character',
4750                               data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
4751                ## TODO: make this configurable
4752              }
4753              push @tokens,
4754                            {type => 'start tag', tag_name => 'input', attributes => $at},
4755                            #{type => 'character', data => ''}, # SHOULD
4756                            {type => 'end tag', tag_name => 'label'},
4757                            {type => 'end tag', tag_name => 'p'},
4758                            {type => 'start tag', tag_name => 'hr'},
4759                            {type => 'end tag', tag_name => 'form'};
4760              $token = shift @tokens;
4761              !!!back-token (@tokens);
4762              redo B;
4763            }
4764          } elsif ($token->{tag_name} eq 'textarea') {
4765            my $tag_name = $token->{tag_name};
4766            my $el;
4767            !!!create-element ($el, $token->{tag_name}, $token->{attributes});
4768            
4769            ## TODO: $self->{form_element} if defined
4770            $self->{content_model} = RCDATA_CONTENT_MODEL;
4771            delete $self->{escape}; # MUST
4772            
4773            $insert->($el);
4774            
4775            my $text = '';
4776            !!!next-token;
4777            if ($token->{type} eq 'character') {
4778              $token->{data} =~ s/^\x0A//;
4779            unless (length $token->{data}) {            unless (length $token->{data}) {
4780              !!!next-token;              !!!next-token;
             redo B;  
4781            }            }
4782          }          }
4783            while ($token->{type} eq 'character') {
4784              $text .= $token->{data};
4785              !!!next-token;
4786            }
4787            if (length $text) {
4788              $el->manakai_append_text ($text);
4789            }
4790            
4791            $self->{content_model} = PCDATA_CONTENT_MODEL;
4792            
4793            if ($token->{type} eq 'end tag' and
4794                $token->{tag_name} eq $tag_name) {
4795              ## Ignore the token
4796            } else {
4797              !!!parse-error (type => 'in RCDATA:#'.$token->{type});
4798            }
4799            !!!next-token;
4800            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') {
4811            $reconstruct_active_formatting_elements->($insert_to_current);
4812            
4813            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4814            
4815            $self->{insertion_mode} = 'in select';
4816            !!!next-token;
4817            redo B;
4818          } elsif ({
4819                    caption => 1, col => 1, colgroup => 1, frame => 1,
4820                    frameset => 1, head => 1, option => 1, optgroup => 1,
4821                    tbody => 1, td => 1, tfoot => 1, th => 1,
4822                    thead => 1, tr => 1,
4823                   }->{$token->{tag_name}}) {
4824            !!!parse-error (type => 'in body:'.$token->{tag_name});
4825            ## Ignore the token
4826            !!!next-token;
4827            redo B;
4828            
4829            ## ISSUE: An issue on HTML5 new elements in the spec.
4830          } else {
4831            $reconstruct_active_formatting_elements->($insert_to_current);
4832            
4833            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4834            
4835            !!!next-token;
4836            redo B;
4837          }
4838        } elsif ($token->{type} eq 'end tag') {
4839          if ($token->{tag_name} eq 'body') {
4840            if (@{$self->{open_elements}} > 1 and
4841                $self->{open_elements}->[1]->[1] eq 'body') {
4842              for (@{$self->{open_elements}}) {
4843                unless ({
4844                           dd => 1, dt => 1, li => 1, p => 1, td => 1,
4845                           th => 1, tr => 1, body => 1, html => 1,
4846                         tbody => 1, tfoot => 1, thead => 1,
4847                        }->{$_->[1]}) {
4848                  !!!parse-error (type => 'not closed:'.$_->[1]);
4849                }
4850              }
4851    
4852          !!!parse-error (type => 'after html:#character');            $self->{insertion_mode} = 'after body';
4853          $phase = 'main';            !!!next-token;
4854          ## reprocess            redo B;
4855            } else {
4856              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4857              ## Ignore the token
4858              !!!next-token;
4859              redo B;
4860            }
4861          } elsif ($token->{tag_name} eq 'html') {
4862            if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
4863              ## ISSUE: There is an issue in the spec.
4864              if ($self->{open_elements}->[-1]->[1] ne 'body') {
4865                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
4866              }
4867              $self->{insertion_mode} = 'after body';
4868              ## reprocess
4869              redo B;
4870            } else {
4871              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4872              ## Ignore the token
4873              !!!next-token;
4874              redo B;
4875            }
4876          } elsif ({
4877                    address => 1, blockquote => 1, center => 1, dir => 1,
4878                    div => 1, dl => 1, fieldset => 1, listing => 1,
4879                    menu => 1, ol => 1, pre => 1, ul => 1,
4880                    p => 1,
4881                    dd => 1, dt => 1, li => 1,
4882                    button => 1, marquee => 1, object => 1,
4883                   }->{$token->{tag_name}}) {
4884            ## has an element in scope
4885            my $i;
4886            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4887              my $node = $self->{open_elements}->[$_];
4888              if ($node->[1] eq $token->{tag_name}) {
4889                ## generate implied end tags
4890                if ({
4891                     dd => ($token->{tag_name} ne 'dd'),
4892                     dt => ($token->{tag_name} ne 'dt'),
4893                     li => ($token->{tag_name} ne 'li'),
4894                     p => ($token->{tag_name} ne 'p'),
4895                     td => 1, th => 1, tr => 1,
4896                     tbody => 1, tfoot=> 1, thead => 1,
4897                    }->{$self->{open_elements}->[-1]->[1]}) {
4898                  !!!back-token;
4899                  $token = {type => 'end tag',
4900                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4901                  redo B;
4902                }
4903                $i = $_;
4904                last INSCOPE unless $token->{tag_name} eq 'p';
4905              } elsif ({
4906                        table => 1, caption => 1, td => 1, th => 1,
4907                        button => 1, marquee => 1, object => 1, html => 1,
4908                       }->{$node->[1]}) {
4909                last INSCOPE;
4910              }
4911            } # INSCOPE
4912            
4913            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
4914              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            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->()
4930              if {
4931                button => 1, marquee => 1, object => 1,
4932              }->{$token->{tag_name}};
4933            !!!next-token;
4934          redo B;          redo B;
4935        } elsif ($token->{type} eq 'start tag' or        } elsif ($token->{tag_name} eq 'form') {
4936                 $token->{type} eq 'end tag') {          ## has an element in scope
4937          !!!parse-error (type => 'after html:'.$token->{tag_name});          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4938          $phase = 'main';            my $node = $self->{open_elements}->[$_];
4939          ## reprocess            if ($node->[1] eq $token->{tag_name}) {
4940                ## generate implied end tags
4941                if ({
4942                     dd => 1, dt => 1, li => 1, p => 1,
4943                     td => 1, th => 1, tr => 1,
4944                     tbody => 1, tfoot=> 1, thead => 1,
4945                    }->{$self->{open_elements}->[-1]->[1]}) {
4946                  !!!back-token;
4947                  $token = {type => 'end tag',
4948                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4949                  redo B;
4950                }
4951                last INSCOPE;
4952              } elsif ({
4953                        table => 1, caption => 1, td => 1, th => 1,
4954                        button => 1, marquee => 1, object => 1, html => 1,
4955                       }->{$node->[1]}) {
4956                last INSCOPE;
4957              }
4958            } # INSCOPE
4959            
4960            if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
4961              pop @{$self->{open_elements}};
4962            } else {
4963              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4964            }
4965    
4966            undef $self->{form_element};
4967            !!!next-token;
4968            redo B;
4969          } elsif ({
4970                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4971                   }->{$token->{tag_name}}) {
4972            ## has an element in scope
4973            my $i;
4974            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4975              my $node = $self->{open_elements}->[$_];
4976              if ({
4977                   h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4978                  }->{$node->[1]}) {
4979                ## generate implied end tags
4980                if ({
4981                     dd => 1, dt => 1, li => 1, p => 1,
4982                     td => 1, th => 1, tr => 1,
4983                     tbody => 1, tfoot=> 1, thead => 1,
4984                    }->{$self->{open_elements}->[-1]->[1]}) {
4985                  !!!back-token;
4986                  $token = {type => 'end tag',
4987                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4988                  redo B;
4989                }
4990                $i = $_;
4991                last INSCOPE;
4992              } elsif ({
4993                        table => 1, caption => 1, td => 1, th => 1,
4994                        button => 1, marquee => 1, object => 1, html => 1,
4995                       }->{$node->[1]}) {
4996                last INSCOPE;
4997              }
4998            } # INSCOPE
4999            
5000            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5001              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5002            }
5003            
5004            splice @{$self->{open_elements}}, $i if defined $i;
5005            !!!next-token;
5006            redo B;
5007          } elsif ({
5008                    a => 1,
5009                    b => 1, big => 1, em => 1, font => 1, i => 1,
5010                    nobr => 1, s => 1, small => 1, strile => 1,
5011                    strong => 1, tt => 1, u => 1,
5012                   }->{$token->{tag_name}}) {
5013            $formatting_end_tag->($token->{tag_name});
5014          redo B;          redo B;
5015        } elsif ($token->{type} eq 'end-of-file') {        } elsif ($token->{tag_name} eq 'br') {
5016          ## Stop parsing          !!!parse-error (type => 'unmatched end tag:br');
5017          last B;  
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 ({
5029                    caption => 1, col => 1, colgroup => 1, frame => 1,
5030                    frameset => 1, head => 1, option => 1, optgroup => 1,
5031                    tbody => 1, td => 1, tfoot => 1, th => 1,
5032                    thead => 1, tr => 1,
5033                    area => 1, basefont => 1, bgsound => 1,
5034                    embed => 1, hr => 1, iframe => 1, image => 1,
5035                    img => 1, input => 1, isindex => 1, noembed => 1,
5036                    noframes => 1, param => 1, select => 1, spacer => 1,
5037                    table => 1, textarea => 1, wbr => 1,
5038                    noscript => 0, ## TODO: if scripting is enabled
5039                   }->{$token->{tag_name}}) {
5040            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5041            ## Ignore the token
5042            !!!next-token;
5043            redo B;
5044            
5045            ## ISSUE: Issue on HTML5 new elements in spec
5046            
5047        } else {        } else {
5048          die "$0: $token->{type}: Unknown token";          ## Step 1
5049            my $node_i = -1;
5050            my $node = $self->{open_elements}->[$node_i];
5051    
5052            ## Step 2
5053            S2: {
5054              if ($node->[1] eq $token->{tag_name}) {
5055                ## Step 1
5056                ## generate implied end tags
5057                if ({
5058                     dd => 1, dt => 1, li => 1, p => 1,
5059                     td => 1, th => 1, tr => 1,
5060                     tbody => 1, tfoot=> 1, thead => 1,
5061                    }->{$self->{open_elements}->[-1]->[1]}) {
5062                  !!!back-token;
5063                  $token = {type => 'end tag',
5064                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5065                  redo B;
5066                }
5067            
5068                ## Step 2
5069                if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
5070                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5071                }
5072                
5073                ## Step 3
5074                splice @{$self->{open_elements}}, $node_i;
5075    
5076                !!!next-token;
5077                last S2;
5078              } else {
5079                ## Step 3
5080                if (not $formatting_category->{$node->[1]} and
5081                    #not $phrasing_category->{$node->[1]} and
5082                    ($special_category->{$node->[1]} or
5083                     $scoping_category->{$node->[1]})) {
5084                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5085                  ## Ignore the token
5086                  !!!next-token;
5087                  last S2;
5088                }
5089              }
5090              
5091              ## Step 4
5092              $node_i--;
5093              $node = $self->{open_elements}->[$node_i];
5094              
5095              ## Step 5;
5096              redo S2;
5097            } # S2
5098            redo B;
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 5167  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 5279  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 5305  sub get_inner_html ($$$) { Line 5330  sub get_inner_html ($$$) {
5330          spacer => 1, wbr => 1,          spacer => 1, wbr => 1,
5331        }->{$tag_name};        }->{$tag_name};
5332    
5333          $s .= "\x0A" if $tag_name eq 'pre' or $tag_name eq 'textarea';
5334    
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.20  
changed lines
  Added in v.1.53

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24