/[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.23 by wakaba, Sat Jun 23 16:01:36 2007 UTC revision 1.54 by wakaba, Sat Aug 11 06:37:12 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    sub AFTER_HTML_IMS () { 0b100 }
163    sub HEAD_IMS ()       { 0b1000 }
164    sub BODY_IMS ()       { 0b10000 }
165    sub BODY_TABLE_IMS () { 0b100000 | BODY_IMS }
166    sub TABLE_IMS ()      { 0b1000000 }
167    sub ROW_IMS ()        { 0b10000000 | TABLE_IMS }
168    sub BODY_AFTER_IMS () { 0b100000000 }
169    sub FRAME_IMS ()      { 0b1000000000 }
170    
171    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
172    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
173    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
174    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
175    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
176    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
177    sub IN_BODY_IM () { BODY_IMS }
178    sub IN_CELL_IM () { BODY_TABLE_IMS | 0b01 }
179    sub IN_CAPTION_IM () { BODY_TABLE_IMS | 0b10 }
180    sub IN_ROW_IM () { ROW_IMS | 0b01 }
181    sub IN_TABLE_BODY_IM () { ROW_IMS | 0b10 }
182    sub IN_TABLE_IM () { TABLE_IMS }
183    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
184    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
185    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
186    sub IN_SELECT_IM () { 0b01 }
187    sub IN_COLUMN_GROUP_IM () { 0b10 }
188    
189  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
190    
191  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
192    my $self = shift;    my $self = shift;
193    $self->{state} = 'data'; # MUST    $self->{state} = 'data'; # MUST
194    $self->{content_model_flag} = 'PCDATA'; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
195    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
196    undef $self->{current_attribute};    undef $self->{current_attribute};
197    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
# Line 188  sub _get_next_token ($) { Line 230  sub _get_next_token ($) {
230    A: {    A: {
231      if ($self->{state} eq 'data') {      if ($self->{state} eq 'data') {
232        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_input_character} == 0x0026) { # &
233          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA
             $self->{content_model_flag} eq 'RCDATA') {  
234            $self->{state} = 'entity data';            $self->{state} = 'entity data';
235            !!!next-input-character;            !!!next-input-character;
236            redo A;            redo A;
# Line 197  sub _get_next_token ($) { Line 238  sub _get_next_token ($) {
238            #            #
239          }          }
240        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_input_character} == 0x002D) { # -
241          if ($self->{content_model_flag} eq 'RCDATA' or          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
             $self->{content_model_flag} eq 'CDATA') {  
242            unless ($self->{escape}) {            unless ($self->{escape}) {
243              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_input_character}->[0] == 0x002D and # -
244                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_input_character}->[1] == 0x0021 and # !
# Line 210  sub _get_next_token ($) { Line 250  sub _get_next_token ($) {
250                    
251          #          #
252        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_input_character} == 0x003C) { # <
253          if ($self->{content_model_flag} eq 'PCDATA' or          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
254              (($self->{content_model_flag} eq 'CDATA' or              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
               $self->{content_model_flag} eq 'RCDATA') and  
255               not $self->{escape})) {               not $self->{escape})) {
256            $self->{state} = 'tag open';            $self->{state} = 'tag open';
257            !!!next-input-character;            !!!next-input-character;
# Line 222  sub _get_next_token ($) { Line 261  sub _get_next_token ($) {
261          }          }
262        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
263          if ($self->{escape} and          if ($self->{escape} and
264              ($self->{content_model_flag} eq 'RCDATA' or              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
              $self->{content_model_flag} eq 'CDATA')) {  
265            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_input_character}->[0] == 0x002D and # -
266                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_input_character}->[1] == 0x002D) { # -
267              delete $self->{escape};              delete $self->{escape};
# Line 247  sub _get_next_token ($) { Line 285  sub _get_next_token ($) {
285      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} eq 'entity data') {
286        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
287                
288        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);
289    
290        $self->{state} = 'data';        $self->{state} = 'data';
291        # next-input-character is already done        # next-input-character is already done
# Line 260  sub _get_next_token ($) { Line 298  sub _get_next_token ($) {
298    
299        redo A;        redo A;
300      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} eq 'tag open') {
301        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
           $self->{content_model_flag} eq 'CDATA') {  
302          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_input_character} == 0x002F) { # /
303            !!!next-input-character;            !!!next-input-character;
304            $self->{state} = 'close tag open';            $self->{state} = 'close tag open';
# Line 274  sub _get_next_token ($) { Line 311  sub _get_next_token ($) {
311    
312            redo A;            redo A;
313          }          }
314        } elsif ($self->{content_model_flag} eq 'PCDATA') {        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
315          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_input_character} == 0x0021) { # !
316            $self->{state} = 'markup declaration open';            $self->{state} = 'markup declaration open';
317            !!!next-input-character;            !!!next-input-character;
# Line 321  sub _get_next_token ($) { Line 358  sub _get_next_token ($) {
358            redo A;            redo A;
359          }          }
360        } else {        } else {
361          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
362        }        }
363      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} eq 'close tag open') {
364        if ($self->{content_model_flag} eq 'RCDATA' or        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
           $self->{content_model_flag} eq 'CDATA') {  
365          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
366              ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
367            my @next_char;            my @next_char;
368            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
369              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_input_character};
# Line 418  sub _get_next_token ($) { Line 455  sub _get_next_token ($) {
455          redo A;          redo A;
456        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
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 443  sub _get_next_token ($) { Line 482  sub _get_next_token ($) {
482        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
483          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
484          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
485              $self->{current_token}->{first_start_tag}
486                  = not defined $self->{last_emitted_start_tag_name};
487            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
488          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
489            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
490            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
491              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
492            }            }
# Line 489  sub _get_next_token ($) { Line 530  sub _get_next_token ($) {
530          redo A;          redo A;
531        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
532          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
533              $self->{current_token}->{first_start_tag}
534                  = not defined $self->{last_emitted_start_tag_name};
535            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
536          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
537            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
538            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
539              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
540            }            }
# Line 527  sub _get_next_token ($) { Line 570  sub _get_next_token ($) {
570        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
571          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
572          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
573              $self->{current_token}->{first_start_tag}
574                  = not defined $self->{last_emitted_start_tag_name};
575            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
576          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
577            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
578            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
579              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
580            }            }
# Line 553  sub _get_next_token ($) { Line 598  sub _get_next_token ($) {
598        my $before_leave = sub {        my $before_leave = sub {
599          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
600              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
601            !!!parse-error (type => 'dupulicate attribute');            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});
602            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
603          } else {          } else {
604            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
# Line 578  sub _get_next_token ($) { Line 623  sub _get_next_token ($) {
623        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
624          $before_leave->();          $before_leave->();
625          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
626              $self->{current_token}->{first_start_tag}
627                  = not defined $self->{last_emitted_start_tag_name};
628            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
629          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
630            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
631            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
632              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
633            }            }
# Line 617  sub _get_next_token ($) { Line 664  sub _get_next_token ($) {
664          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
665          $before_leave->();          $before_leave->();
666          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
667              $self->{current_token}->{first_start_tag}
668                  = not defined $self->{last_emitted_start_tag_name};
669            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
670          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
671            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
672            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
673              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
674            }            }
# Line 653  sub _get_next_token ($) { Line 702  sub _get_next_token ($) {
702          redo A;          redo A;
703        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
704          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
705              $self->{current_token}->{first_start_tag}
706                  = not defined $self->{last_emitted_start_tag_name};
707            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
708          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
709            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
710            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
711              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
712            }            }
# Line 684  sub _get_next_token ($) { Line 735  sub _get_next_token ($) {
735            #            #
736          } else {          } else {
737            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
738              ## TODO: Different error type for <aa / bb> than <aa/>
739          }          }
740          $self->{state} = 'before attribute name';          $self->{state} = 'before attribute name';
741          # next-input-character is already done          # next-input-character is already done
# Line 691  sub _get_next_token ($) { Line 743  sub _get_next_token ($) {
743        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
744          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
745          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
746              $self->{current_token}->{first_start_tag}
747                  = not defined $self->{last_emitted_start_tag_name};
748            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
749          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
750            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
751            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
752              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
753            }            }
# Line 736  sub _get_next_token ($) { Line 790  sub _get_next_token ($) {
790          redo A;          redo A;
791        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
792          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
793              $self->{current_token}->{first_start_tag}
794                  = not defined $self->{last_emitted_start_tag_name};
795            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
796          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
797            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
798            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
799              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
800            }            }
# Line 754  sub _get_next_token ($) { Line 810  sub _get_next_token ($) {
810        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
811          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
812          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
813              $self->{current_token}->{first_start_tag}
814                  = not defined $self->{last_emitted_start_tag_name};
815            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
816          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
817            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
818            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
819              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
820            }            }
# Line 788  sub _get_next_token ($) { Line 846  sub _get_next_token ($) {
846        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
847          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
848          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
849              $self->{current_token}->{first_start_tag}
850                  = not defined $self->{last_emitted_start_tag_name};
851            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
852          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
853            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
854            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
855              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
856            }            }
# Line 822  sub _get_next_token ($) { Line 882  sub _get_next_token ($) {
882        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
883          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
884          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
885              $self->{current_token}->{first_start_tag}
886                  = not defined $self->{last_emitted_start_tag_name};
887            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
888          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
889            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
890            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
891              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
892            }            }
# Line 859  sub _get_next_token ($) { Line 921  sub _get_next_token ($) {
921          redo A;          redo A;
922        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
923          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
924              $self->{current_token}->{first_start_tag}
925                  = not defined $self->{last_emitted_start_tag_name};
926            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
927          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
928            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
929            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
930              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
931            }            }
# Line 877  sub _get_next_token ($) { Line 941  sub _get_next_token ($) {
941        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
942          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
943          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} eq 'start tag') {
944              $self->{current_token}->{first_start_tag}
945                  = not defined $self->{last_emitted_start_tag_name};
946            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
947          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} eq 'end tag') {
948            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
949            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
950              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
951            }            }
# Line 899  sub _get_next_token ($) { Line 965  sub _get_next_token ($) {
965          redo A;          redo A;
966        }        }
967      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} eq 'entity in attribute value') {
968        my $token = $self->_tokenize_attempt_to_consume_an_entity;        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);
969    
970        unless (defined $token) {        unless (defined $token) {
971          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
# Line 990  sub _get_next_token ($) { Line 1056  sub _get_next_token ($) {
1056          }          }
1057        }        }
1058    
1059        !!!parse-error (type => 'bogus comment open');        !!!parse-error (type => 'bogus comment');
1060        $self->{next_input_character} = shift @next_char;        $self->{next_input_character} = shift @next_char;
1061        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1062        $self->{state} = 'bogus comment';        $self->{state} = 'bogus comment';
# Line 1049  sub _get_next_token ($) { Line 1115  sub _get_next_token ($) {
1115          redo A;          redo A;
1116        } else {        } else {
1117          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1118              .= chr ($self->{next_input_character});              .= '-' . chr ($self->{next_input_character});
1119          $self->{state} = 'comment';          $self->{state} = 'comment';
1120          !!!next-input-character;          !!!next-input-character;
1121          redo A;          redo A;
# Line 1409  sub _get_next_token ($) { Line 1475  sub _get_next_token ($) {
1475          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1476    
1477          $self->{state} = 'data';          $self->{state} = 'data';
1478          ## recomsume          ## reconsume
1479    
1480          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1481          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1452  sub _get_next_token ($) { Line 1518  sub _get_next_token ($) {
1518          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1519    
1520          $self->{state} = 'data';          $self->{state} = 'data';
1521          ## recomsume          ## reconsume
1522    
1523          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1524          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1525    
1526          redo A;          redo A;
1527        } else {        } else {
1528          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after SYSTEM');
1529          $self->{state} = 'bogus DOCTYPE';          $self->{state} = 'bogus DOCTYPE';
1530          !!!next-input-character;          !!!next-input-character;
1531          redo A;          redo A;
# Line 1527  sub _get_next_token ($) { Line 1593  sub _get_next_token ($) {
1593          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1594    
1595          $self->{state} = 'data';          $self->{state} = 'data';
1596          ## recomsume          ## reconsume
1597    
1598          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
1599          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1570  sub _get_next_token ($) { Line 1636  sub _get_next_token ($) {
1636    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
1637  } # _get_next_token  } # _get_next_token
1638    
1639  sub _tokenize_attempt_to_consume_an_entity ($) {  sub _tokenize_attempt_to_consume_an_entity ($$) {
1640    my $self = shift;    my ($self, $in_attr) = @_;
1641    
1642    if ({    if ({
1643         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
# Line 1584  sub _tokenize_attempt_to_consume_an_enti Line 1650  sub _tokenize_attempt_to_consume_an_enti
1650      !!!next-input-character;      !!!next-input-character;
1651      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_input_character} == 0x0078 or # x
1652          $self->{next_input_character} == 0x0058) { # X          $self->{next_input_character} == 0x0058) { # X
1653        my $num;        my $code;
1654        X: {        X: {
1655          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_input_character};
1656          !!!next-input-character;          !!!next-input-character;
1657          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_input_character} and
1658              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_input_character} <= 0x0039) { # 0..9
1659            $num ||= 0;            $code ||= 0;
1660            $num *= 0x10;            $code *= 0x10;
1661            $num += $self->{next_input_character} - 0x0030;            $code += $self->{next_input_character} - 0x0030;
1662            redo X;            redo X;
1663          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
1664                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_input_character} <= 0x0066) { # a..f
1665            ## ISSUE: the spec says U+0078, which is apparently incorrect            $code ||= 0;
1666            $num ||= 0;            $code *= 0x10;
1667            $num *= 0x10;            $code += $self->{next_input_character} - 0x0060 + 9;
           $num += $self->{next_input_character} - 0x0060 + 9;  
1668            redo X;            redo X;
1669          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
1670                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_input_character} <= 0x0046) { # A..F
1671            ## ISSUE: the spec says U+0058, which is apparently incorrect            $code ||= 0;
1672            $num ||= 0;            $code *= 0x10;
1673            $num *= 0x10;            $code += $self->{next_input_character} - 0x0040 + 9;
           $num += $self->{next_input_character} - 0x0040 + 9;  
1674            redo X;            redo X;
1675          } elsif (not defined $num) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
1676            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro');
1677              !!!back-next-input-character ($x_char, $self->{next_input_character});
1678            $self->{next_input_character} = 0x0023; # #            $self->{next_input_character} = 0x0023; # #
           !!!back-next-input-character ($x_char);  
1679            return undef;            return undef;
1680          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_input_character} == 0x003B) { # ;
1681            !!!next-input-character;            !!!next-input-character;
# Line 1619  sub _tokenize_attempt_to_consume_an_enti Line 1683  sub _tokenize_attempt_to_consume_an_enti
1683            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc');
1684          }          }
1685    
1686          ## TODO: check the definition for |a valid Unicode character|.          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1687          ## <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2006-December/thread.html#8189>            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1688          if ($num > 1114111 or $num == 0) {            $code = 0xFFFD;
1689            $num = 0xFFFD; # REPLACEMENT CHARACTER          } elsif ($code > 0x10FFFF) {
1690            ## ISSUE: Why this is not an error?            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1691          } elsif (0x80 <= $num and $num <= 0x9F) {            $code = 0xFFFD;
1692            !!!parse-error (type => sprintf 'c1 entity:U+%04X', $num);          } elsif ($code == 0x000D) {
1693            $num = $c1_entity_char->{$num};            !!!parse-error (type => 'CR character reference');
1694              $code = 0x000A;
1695            } elsif (0x80 <= $code and $code <= 0x9F) {
1696              !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
1697              $code = $c1_entity_char->{$code};
1698          }          }
1699    
1700          return {type => 'character', data => chr $num};          return {type => 'character', data => chr $code};
1701        } # X        } # X
1702      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_input_character} and
1703               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_input_character} <= 0x0039) { # 0..9
# Line 1650  sub _tokenize_attempt_to_consume_an_enti Line 1718  sub _tokenize_attempt_to_consume_an_enti
1718          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc');
1719        }        }
1720    
1721        ## TODO: check the definition for |a valid Unicode character|.        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
1722        if ($code > 1114111 or $code == 0) {          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
1723          $code = 0xFFFD; # REPLACEMENT CHARACTER          $code = 0xFFFD;
1724          ## ISSUE: Why this is not an error?        } elsif ($code > 0x10FFFF) {
1725            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
1726            $code = 0xFFFD;
1727          } elsif ($code == 0x000D) {
1728            !!!parse-error (type => 'CR character reference');
1729            $code = 0x000A;
1730        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
1731          !!!parse-error (type => sprintf 'c1 entity:U+%04X', $code);          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
1732          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
1733        }        }
1734                
# Line 1674  sub _tokenize_attempt_to_consume_an_enti Line 1747  sub _tokenize_attempt_to_consume_an_enti
1747      !!!next-input-character;      !!!next-input-character;
1748    
1749      my $value = $entity_name;      my $value = $entity_name;
1750      my $match;      my $match = 0;
1751      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
1752      our $EntityChar;      our $EntityChar;
1753    
# Line 1689  sub _tokenize_attempt_to_consume_an_enti Line 1762  sub _tokenize_attempt_to_consume_an_enti
1762              $self->{next_input_character} == 0x003B)) { # ;              $self->{next_input_character} == 0x003B)) { # ;
1763        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_input_character};
1764        if (defined $EntityChar->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
         $value = $EntityChar->{$entity_name};  
1765          if ($self->{next_input_character} == 0x003B) { # ;          if ($self->{next_input_character} == 0x003B) { # ;
1766              $value = $EntityChar->{$entity_name};
1767            $match = 1;            $match = 1;
1768            !!!next-input-character;            !!!next-input-character;
1769            last;            last;
1770          } else {          } else {
1771              $value = $EntityChar->{$entity_name};
1772            $match = -1;            $match = -1;
1773              !!!next-input-character;
1774          }          }
1775        } else {        } else {
1776          $value .= chr $self->{next_input_character};          $value .= chr $self->{next_input_character};
1777            $match *= 2;
1778            !!!next-input-character;
1779        }        }
       !!!next-input-character;  
1780      }      }
1781            
1782      if ($match > 0) {      if ($match > 0) {
1783        return {type => 'character', data => $value};        return {type => 'character', data => $value};
1784      } elsif ($match < 0) {      } elsif ($match < 0) {
1785        !!!parse-error (type => 'refc');        !!!parse-error (type => 'no refc');
1786        return {type => 'character', data => $value};        if ($in_attr and $match < -1) {
1787            return {type => 'character', data => '&'.$entity_name};
1788          } else {
1789            return {type => 'character', data => $value};
1790          }
1791      } else {      } else {
1792        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
1793        ## NOTE: No characters are consumed in the spec.        ## NOTE: No characters are consumed in the spec.
1794        !!!back-token ({type => 'character', data => $value});        return {type => 'character', data => '&'.$value};
       return undef;  
1795      }      }
1796    } else {    } else {
1797      ## no characters are consumed      ## no characters are consumed
# Line 1754  sub _construct_tree ($) { Line 1833  sub _construct_tree ($) {
1833        
1834    !!!next-token;    !!!next-token;
1835    
1836    $self->{insertion_mode} = 'before head';    $self->{insertion_mode} = BEFORE_HEAD_IM;
1837    undef $self->{form_element};    undef $self->{form_element};
1838    undef $self->{head_element};    undef $self->{head_element};
1839    $self->{open_elements} = [];    $self->{open_elements} = [];
# Line 1907  sub _tree_construction_initial ($) { Line 1986  sub _tree_construction_initial ($) {
1986      } elsif ($token->{type} eq 'character') {      } elsif ($token->{type} eq 'character') {
1987        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
1988          ## Ignore the token          ## Ignore the token
1989    
1990          unless (length $token->{data}) {          unless (length $token->{data}) {
1991            ## Stay in the phase            ## Stay in the phase
1992            !!!next-token;            !!!next-token;
# Line 1949  sub _tree_construction_root_element ($) Line 2029  sub _tree_construction_root_element ($)
2029          !!!next-token;          !!!next-token;
2030          redo B;          redo B;
2031        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} eq 'character') {
2032          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2033            $self->{document}->manakai_append_text ($1);            ## Ignore the token.
2034            ## ISSUE: DOM3 Core does not allow Document > Text  
2035            unless (length $token->{data}) {            unless (length $token->{data}) {
2036              ## Stay in the phase              ## Stay in the phase
2037              !!!next-token;              !!!next-token;
# Line 1972  sub _tree_construction_root_element ($) Line 2052  sub _tree_construction_root_element ($)
2052        my $root_element; !!!create-element ($root_element, 'html');        my $root_element; !!!create-element ($root_element, 'html');
2053        $self->{document}->append_child ($root_element);        $self->{document}->append_child ($root_element);
2054        push @{$self->{open_elements}}, [$root_element, 'html'];        push @{$self->{open_elements}}, [$root_element, 'html'];
       #$phase = 'main';  
2055        ## reprocess        ## reprocess
2056        #redo B;        #redo B;
2057        return;        return; ## Go to the main phase.
2058    } # B    } # B
2059  } # _tree_construction_root_element  } # _tree_construction_root_element
2060    
# Line 1991  sub _reset_insertion_mode ($) { Line 2070  sub _reset_insertion_mode ($) {
2070            
2071      ## Step 3      ## Step 3
2072      S3: {      S3: {
2073        $last = 1 if $self->{open_elements}->[0]->[0] eq $node->[0];        ## ISSUE: Oops! "If node is the first node in the stack of open
2074        if (defined $self->{inner_html_node}) {        ## elements, then set last to true. If the context element of the
2075          if ($self->{inner_html_node}->[1] eq 'td' or        ## HTML fragment parsing algorithm is neither a td element nor a
2076              $self->{inner_html_node}->[1] eq 'th') {        ## th element, then set node to the context element. (fragment case)":
2077            #        ## The second "if" is in the scope of the first "if"!?
2078          } else {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
2079            $node = $self->{inner_html_node};          $last = 1;
2080            if (defined $self->{inner_html_node}) {
2081              if ($self->{inner_html_node}->[1] eq 'td' or
2082                  $self->{inner_html_node}->[1] eq 'th') {
2083                #
2084              } else {
2085                $node = $self->{inner_html_node};
2086              }
2087          }          }
2088        }        }
2089            
2090        ## Step 4..13        ## Step 4..13
2091        my $new_mode = {        my $new_mode = {
2092                        select => 'in select',                        select => IN_SELECT_IM,
2093                        td => 'in cell',                        td => IN_CELL_IM,
2094                        th => 'in cell',                        th => IN_CELL_IM,
2095                        tr => 'in row',                        tr => IN_ROW_IM,
2096                        tbody => 'in table body',                        tbody => IN_TABLE_BODY_IM,
2097                        thead => 'in table head',                        thead => IN_TABLE_BODY_IM,
2098                        tfoot => 'in table foot',                        tfoot => IN_TABLE_BODY_IM,
2099                        caption => 'in caption',                        caption => IN_CAPTION_IM,
2100                        colgroup => 'in column group',                        colgroup => IN_COLUMN_GROUP_IM,
2101                        table => 'in table',                        table => IN_TABLE_IM,
2102                        head => 'in body', # not in head!                        head => IN_BODY_IM, # not in head!
2103                        body => 'in body',                        body => IN_BODY_IM,
2104                        frameset => 'in frameset',                        frameset => IN_FRAMESET_IM,
2105                       }->{$node->[1]};                       }->{$node->[1]};
2106        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2107                
2108        ## Step 14        ## Step 14
2109        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2110          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2111            $self->{insertion_mode} = 'before head';            $self->{insertion_mode} = BEFORE_HEAD_IM;
2112          } else {          } else {
2113            $self->{insertion_mode} = 'after head';            $self->{insertion_mode} = AFTER_HEAD_IM;
2114          }          }
2115          return;          return;
2116        }        }
2117                
2118        ## Step 15        ## Step 15
2119        $self->{insertion_mode} = 'in body' and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
2120                
2121        ## Step 16        ## Step 16
2122        $i--;        $i--;
# Line 2044  sub _reset_insertion_mode ($) { Line 2130  sub _reset_insertion_mode ($) {
2130  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
2131    my $self = shift;    my $self = shift;
2132    
   my $phase = 'main';  
   
2133    my $active_formatting_elements = [];    my $active_formatting_elements = [];
2134    
2135    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 2128  sub _tree_construction_main ($) { Line 2212  sub _tree_construction_main ($) {
2212      }      }
2213    }; # $clear_up_to_marker    }; # $clear_up_to_marker
2214    
2215    my $style_start_tag = sub {    my $parse_rcdata = sub ($$) {
2216      my $style_el; !!!create-element ($style_el, 'style', $token->{attributes});      my ($content_model_flag, $insert) = @_;
2217      ## $self->{insertion_mode} eq 'in head' and ... (always true)  
2218      (($self->{insertion_mode} eq 'in head' and defined $self->{head_element})      ## Step 1
2219       ? $self->{head_element} : $self->{open_elements}->[-1]->[0])      my $start_tag_name = $token->{tag_name};
2220        ->append_child ($style_el);      my $el;
2221      $self->{content_model_flag} = 'CDATA';      !!!create-element ($el, $start_tag_name, $token->{attributes});
2222    
2223        ## Step 2
2224        $insert->($el); # /context node/->append_child ($el)
2225    
2226        ## Step 3
2227        $self->{content_model} = $content_model_flag; # CDATA or RCDATA
2228      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2229                  
2230        ## Step 4
2231      my $text = '';      my $text = '';
2232      !!!next-token;      !!!next-token;
2233      while ($token->{type} eq 'character') {      while ($token->{type} eq 'character') { # or until stop tokenizing
2234        $text .= $token->{data};        $text .= $token->{data};
2235        !!!next-token;        !!!next-token;
2236      } # stop if non-character token or tokenizer stops tokenising      }
2237    
2238        ## Step 5
2239      if (length $text) {      if (length $text) {
2240        $style_el->manakai_append_text ($text);        my $text = $self->{document}->create_text_node ($text);
2241          $el->append_child ($text);
2242      }      }
2243        
2244      $self->{content_model_flag} = 'PCDATA';      ## Step 6
2245                      $self->{content_model} = PCDATA_CONTENT_MODEL;
2246      if ($token->{type} eq 'end tag' and $token->{tag_name} eq 'style') {  
2247        ## Step 7
2248        if ($token->{type} eq 'end tag' and $token->{tag_name} eq $start_tag_name) {
2249        ## Ignore the token        ## Ignore the token
2250      } else {      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2251        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type});
2252        ## ISSUE: And ignore?      } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
2253          !!!parse-error (type => 'in RCDATA:#'.$token->{type});
2254        } else {
2255          die "$0: $content_model_flag in parse_rcdata";
2256      }      }
2257      !!!next-token;      !!!next-token;
2258    }; # $style_start_tag    }; # $parse_rcdata
2259    
2260    my $script_start_tag = sub {    my $script_start_tag = sub ($) {
2261        my $insert = $_[0];
2262      my $script_el;      my $script_el;
2263      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, 'script', $token->{attributes});
2264      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
2265    
2266      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
2267      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
2268            
2269      my $text = '';      my $text = '';
# Line 2176  sub _tree_construction_main ($) { Line 2276  sub _tree_construction_main ($) {
2276        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
2277      }      }
2278                                
2279      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
2280    
2281      if ($token->{type} eq 'end tag' and      if ($token->{type} eq 'end tag' and
2282          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
# Line 2192  sub _tree_construction_main ($) { Line 2292  sub _tree_construction_main ($) {
2292      } else {      } else {
2293        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
2294        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
2295          
2296        (($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);  
2297                
2298        ## TODO: insertion point = $old_insertion_point (might be "undefined")        ## TODO: insertion point = $old_insertion_point (might be "undefined")
2299                
# Line 2388  sub _tree_construction_main ($) { Line 2487  sub _tree_construction_main ($) {
2487    }; # $formatting_end_tag    }; # $formatting_end_tag
2488    
2489    my $insert_to_current = sub {    my $insert_to_current = sub {
2490      $self->{open_elements}->[-1]->[0]->append_child (shift);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
2491    }; # $insert_to_current    }; # $insert_to_current
2492    
2493    my $insert_to_foster = sub {    my $insert_to_foster = sub {
# Line 2422  sub _tree_construction_main ($) { Line 2521  sub _tree_construction_main ($) {
2521                         }                         }
2522    }; # $insert_to_foster    }; # $insert_to_foster
2523    
2524    my $in_body = sub {    my $insert;
2525      my $insert = shift;  
2526      if ($token->{type} eq 'start tag') {    B: {
2527        if ($token->{tag_name} eq 'script') {      if ($token->{type} eq 'DOCTYPE') {
2528          $script_start_tag->();        !!!parse-error (type => 'DOCTYPE in the middle');
2529          return;        ## Ignore the token
2530        } elsif ($token->{tag_name} eq 'style') {        ## Stay in the phase
2531          $style_start_tag->();        !!!next-token;
2532          return;        redo B;
2533        } elsif ({      } elsif ($token->{type} eq 'end-of-file') {
2534                  base => 1, link => 1, meta => 1,        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM or
2535                 }->{$token->{tag_name}}) {            $self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
2536          !!!parse-error (type => 'in body:'.$token->{tag_name});          #
2537          ## NOTE: This is an "as if in head" code clone        } else {
2538          my $el;          ## Generate implied end tags
2539          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          if ({
2540          if (defined $self->{head_element}) {               dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,
2541            $self->{head_element}->append_child ($el);               tbody => 1, tfoot=> 1, thead => 1,
2542          } else {              }->{$self->{open_elements}->[-1]->[1]}) {
2543            $insert->($el);            !!!back-token;
2544              $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};
2545              redo B;
2546          }          }
2547                    
2548          !!!next-token;          if (@{$self->{open_elements}} > 2 or
2549          return;              (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {
2550        } elsif ($token->{tag_name} eq 'title') {            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
2551          !!!parse-error (type => 'in body:title');          } elsif (defined $self->{inner_html_node} and
2552          ## NOTE: There is an "as if in head" code clone                   @{$self->{open_elements}} > 1 and
2553          my $title_el;                   $self->{open_elements}->[1]->[1] ne 'body') {
2554          !!!create-element ($title_el, 'title', $token->{attributes});            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
2555          (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])          }
2556            ->append_child ($title_el);  
2557          $self->{content_model_flag} = 'RCDATA';          ## ISSUE: There is an issue in the spec.
2558          delete $self->{escape}; # MUST        }
2559            
2560          my $text = '';        ## Stop parsing
2561          !!!next-token;        last B;
2562          while ($token->{type} eq 'character') {      } elsif ($token->{type} eq 'start tag' and
2563            $text .= $token->{data};               $token->{tag_name} eq 'html') {
2564          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
2565            ## Turn into the main phase
2566            !!!parse-error (type => 'after html:html');
2567            $self->{insertion_mode} = AFTER_BODY_IM;
2568          } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
2569            ## Turn into the main phase
2570            !!!parse-error (type => 'after html:html');
2571            $self->{insertion_mode} = AFTER_FRAMESET_IM;
2572          }
2573    
2574    ## ISSUE: "aa<html>" is not a parse error.
2575    ## ISSUE: "<html>" in fragment is not a parse error.
2576          unless ($token->{first_start_tag}) {
2577            !!!parse-error (type => 'not first start tag');
2578          }
2579          my $top_el = $self->{open_elements}->[0]->[0];
2580          for my $attr_name (keys %{$token->{attributes}}) {
2581            unless ($top_el->has_attribute_ns (undef, $attr_name)) {
2582              $top_el->set_attribute_ns
2583                (undef, [undef, $attr_name],
2584                 $token->{attributes}->{$attr_name}->{value});
2585            }
2586          }
2587          !!!next-token;
2588          redo B;
2589        } elsif ($token->{type} eq 'comment') {
2590          my $comment = $self->{document}->create_comment ($token->{data});
2591          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM or
2592              $self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
2593            $self->{document}->append_child ($comment);
2594          } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
2595            $self->{open_elements}->[0]->[0]->append_child ($comment);
2596          } else {
2597            $self->{open_elements}->[-1]->[0]->append_child ($comment);
2598          }
2599          !!!next-token;
2600          redo B;
2601        } elsif ($self->{insertion_mode} == IN_HEAD_IM or
2602                 $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM or
2603                 $self->{insertion_mode} == AFTER_HEAD_IM or
2604                 $self->{insertion_mode} == BEFORE_HEAD_IM) {
2605          if ($token->{type} eq 'character') {
2606            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
2607              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
2608              unless (length $token->{data}) {
2609                !!!next-token;
2610                redo B;
2611              }
2612            }
2613    
2614            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2615              ## As if <head>
2616              !!!create-element ($self->{head_element}, 'head');
2617              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2618              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2619    
2620              ## Reprocess in the "in head" insertion mode...
2621              pop @{$self->{open_elements}};
2622    
2623              ## Reprocess in the "after head" insertion mode...
2624            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2625              ## As if </noscript>
2626              pop @{$self->{open_elements}};
2627              !!!parse-error (type => 'in noscript:#character');
2628              
2629              ## Reprocess in the "in head" insertion mode...
2630              ## As if </head>
2631              pop @{$self->{open_elements}};
2632    
2633              ## Reprocess in the "after head" insertion mode...
2634            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2635              pop @{$self->{open_elements}};
2636    
2637              ## Reprocess in the "after head" insertion mode...
2638            }
2639    
2640                ## "after head" insertion mode
2641                ## As if <body>
2642                !!!insert-element ('body');
2643                $self->{insertion_mode} = IN_BODY_IM;
2644                ## reprocess
2645                redo B;
2646              } elsif ($token->{type} eq 'start tag') {
2647                if ($token->{tag_name} eq 'head') {
2648                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2649                    !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
2650                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2651                    push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
2652                    $self->{insertion_mode} = IN_HEAD_IM;
2653                    !!!next-token;
2654                    redo B;
2655                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2656                    #
2657                  } else {
2658                    !!!parse-error (type => 'in head:head'); # or in head noscript
2659                    ## Ignore the token
2660                    !!!next-token;
2661                    redo B;
2662                  }
2663                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2664                  ## As if <head>
2665                  !!!create-element ($self->{head_element}, 'head');
2666                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2667                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2668    
2669                  $self->{insertion_mode} = IN_HEAD_IM;
2670                  ## Reprocess in the "in head" insertion mode...
2671                }
2672    
2673                if ($token->{tag_name} eq 'base') {
2674                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2675                    ## As if </noscript>
2676                    pop @{$self->{open_elements}};
2677                    !!!parse-error (type => 'in noscript:base');
2678                  
2679                    $self->{insertion_mode} = IN_HEAD_IM;
2680                    ## Reprocess in the "in head" insertion mode...
2681                  }
2682    
2683                  ## NOTE: There is a "as if in head" code clone.
2684                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2685                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2686                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2687                  }
2688                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2689                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2690                  pop @{$self->{open_elements}}
2691                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2692                  !!!next-token;
2693                  redo B;
2694                } elsif ($token->{tag_name} eq 'link') {
2695                  ## NOTE: There is a "as if in head" code clone.
2696                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2697                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2698                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2699                  }
2700                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2701                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2702                  pop @{$self->{open_elements}}
2703                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2704                  !!!next-token;
2705                  redo B;
2706                } elsif ($token->{tag_name} eq 'meta') {
2707                  ## NOTE: There is a "as if in head" code clone.
2708                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2709                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2710                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2711                  }
2712                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2713                  pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2714    
2715                  unless ($self->{confident}) {
2716                    my $charset;
2717                    if ($token->{attributes}->{charset}) { ## TODO: And if supported
2718                      $charset = $token->{attributes}->{charset}->{value};
2719                    }
2720                    if ($token->{attributes}->{'http-equiv'}) {
2721                      ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
2722                      if ($token->{attributes}->{'http-equiv'}->{value}
2723                          =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
2724                              [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2725                              ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
2726                        $charset = defined $1 ? $1 : defined $2 ? $2 : $3;
2727                      } ## TODO: And if supported
2728                    }
2729                    ## TODO: Change the encoding
2730                  }
2731    
2732                  ## TODO: Extracting |charset| from |meta|.
2733                  pop @{$self->{open_elements}}
2734                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2735                  !!!next-token;
2736                  redo B;
2737                } elsif ($token->{tag_name} eq 'title') {
2738                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2739                    ## As if </noscript>
2740                    pop @{$self->{open_elements}};
2741                    !!!parse-error (type => 'in noscript:title');
2742                  
2743                    $self->{insertion_mode} = IN_HEAD_IM;
2744                    ## Reprocess in the "in head" insertion mode...
2745                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2746                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2747                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2748                  }
2749    
2750                  ## NOTE: There is a "as if in head" code clone.
2751                  my $parent = defined $self->{head_element} ? $self->{head_element}
2752                      : $self->{open_elements}->[-1]->[0];
2753                  $parse_rcdata->(RCDATA_CONTENT_MODEL,
2754                                  sub { $parent->append_child ($_[0]) });
2755                  pop @{$self->{open_elements}}
2756                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2757                  redo B;
2758                } elsif ($token->{tag_name} eq 'style') {
2759                  ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
2760                  ## insertion mode IN_HEAD_IM)
2761                  ## NOTE: There is a "as if in head" code clone.
2762                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2763                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2764                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2765                  }
2766                  $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
2767                  pop @{$self->{open_elements}}
2768                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2769                  redo B;
2770                } elsif ($token->{tag_name} eq 'noscript') {
2771                  if ($self->{insertion_mode} == IN_HEAD_IM) {
2772                    ## NOTE: and scripting is disalbed
2773                    !!!insert-element ($token->{tag_name}, $token->{attributes});
2774                    $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
2775                    !!!next-token;
2776                    redo B;
2777                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2778                    !!!parse-error (type => 'in noscript:noscript');
2779                    ## Ignore the token
2780                    !!!next-token;
2781                    redo B;
2782                  } else {
2783                    #
2784                  }
2785                } elsif ($token->{tag_name} eq 'script') {
2786                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2787                    ## As if </noscript>
2788                    pop @{$self->{open_elements}};
2789                    !!!parse-error (type => 'in noscript:script');
2790                  
2791                    $self->{insertion_mode} = IN_HEAD_IM;
2792                    ## Reprocess in the "in head" insertion mode...
2793                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2794                    !!!parse-error (type => 'after head:'.$token->{tag_name});
2795                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2796                  }
2797    
2798                  ## NOTE: There is a "as if in head" code clone.
2799                  $script_start_tag->($insert_to_current);
2800                  pop @{$self->{open_elements}}
2801                      if $self->{insertion_mode} == AFTER_HEAD_IM;
2802                  redo B;
2803                } elsif ($token->{tag_name} eq 'body' or
2804                         $token->{tag_name} eq 'frameset') {
2805                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2806                    ## As if </noscript>
2807                    pop @{$self->{open_elements}};
2808                    !!!parse-error (type => 'in noscript:'.$token->{tag_name});
2809                    
2810                    ## Reprocess in the "in head" insertion mode...
2811                    ## As if </head>
2812                    pop @{$self->{open_elements}};
2813                    
2814                    ## Reprocess in the "after head" insertion mode...
2815                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2816                    pop @{$self->{open_elements}};
2817                    
2818                    ## Reprocess in the "after head" insertion mode...
2819                  }
2820    
2821                  ## "after head" insertion mode
2822                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2823                  if ($token->{tag_name} eq 'body') {
2824                    $self->{insertion_mode} = IN_BODY_IM;
2825                  } elsif ($token->{tag_name} eq 'frameset') {
2826                    $self->{insertion_mode} = IN_FRAMESET_IM;
2827                  } else {
2828                    die "$0: tag name: $self->{tag_name}";
2829                  }
2830                  !!!next-token;
2831                  redo B;
2832                } else {
2833                  #
2834                }
2835    
2836                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2837                  ## As if </noscript>
2838                  pop @{$self->{open_elements}};
2839                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
2840                  
2841                  ## Reprocess in the "in head" insertion mode...
2842                  ## As if </head>
2843                  pop @{$self->{open_elements}};
2844    
2845                  ## Reprocess in the "after head" insertion mode...
2846                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2847                  ## As if </head>
2848                  pop @{$self->{open_elements}};
2849    
2850                  ## Reprocess in the "after head" insertion mode...
2851                }
2852    
2853                ## "after head" insertion mode
2854                ## As if <body>
2855                !!!insert-element ('body');
2856                $self->{insertion_mode} = IN_BODY_IM;
2857                ## reprocess
2858                redo B;
2859              } elsif ($token->{type} eq 'end tag') {
2860                if ($token->{tag_name} eq 'head') {
2861                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2862                    ## As if <head>
2863                    !!!create-element ($self->{head_element}, 'head');
2864                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2865                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2866    
2867                    ## Reprocess in the "in head" insertion mode...
2868                    pop @{$self->{open_elements}};
2869                    $self->{insertion_mode} = AFTER_HEAD_IM;
2870                    !!!next-token;
2871                    redo B;
2872                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2873                    ## As if </noscript>
2874                    pop @{$self->{open_elements}};
2875                    !!!parse-error (type => 'in noscript:script');
2876                    
2877                    ## Reprocess in the "in head" insertion mode...
2878                    pop @{$self->{open_elements}};
2879                    $self->{insertion_mode} = AFTER_HEAD_IM;
2880                    !!!next-token;
2881                    redo B;
2882                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2883                    pop @{$self->{open_elements}};
2884                    $self->{insertion_mode} = AFTER_HEAD_IM;
2885                    !!!next-token;
2886                    redo B;
2887                  } else {
2888                    #
2889                  }
2890                } elsif ($token->{tag_name} eq 'noscript') {
2891                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2892                    pop @{$self->{open_elements}};
2893                    $self->{insertion_mode} = IN_HEAD_IM;
2894                    !!!next-token;
2895                    redo B;
2896                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2897                    !!!parse-error (type => 'unmatched end tag:noscript');
2898                    ## Ignore the token ## ISSUE: An issue in the spec.
2899                    !!!next-token;
2900                    redo B;
2901                  } else {
2902                    #
2903                  }
2904                } elsif ({
2905                          body => 1, html => 1,
2906                         }->{$token->{tag_name}}) {
2907                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2908                    ## As if <head>
2909                    !!!create-element ($self->{head_element}, 'head');
2910                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2911                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2912    
2913                    $self->{insertion_mode} = IN_HEAD_IM;
2914                    ## Reprocess in the "in head" insertion mode...
2915                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2916                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2917                    ## Ignore the token
2918                    !!!next-token;
2919                    redo B;
2920                  }
2921                  
2922                  #
2923                } elsif ({
2924                          p => 1, br => 1,
2925                         }->{$token->{tag_name}}) {
2926                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2927                    ## As if <head>
2928                    !!!create-element ($self->{head_element}, 'head');
2929                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2930                    push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2931    
2932                    $self->{insertion_mode} = IN_HEAD_IM;
2933                    ## Reprocess in the "in head" insertion mode...
2934                  }
2935    
2936                  #
2937                } else {
2938                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2939                    #
2940                  } else {
2941                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2942                    ## Ignore the token
2943                    !!!next-token;
2944                    redo B;
2945                  }
2946                }
2947    
2948                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2949                  ## As if </noscript>
2950                  pop @{$self->{open_elements}};
2951                  !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
2952                  
2953                  ## Reprocess in the "in head" insertion mode...
2954                  ## As if </head>
2955                  pop @{$self->{open_elements}};
2956    
2957                  ## Reprocess in the "after head" insertion mode...
2958                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2959                  ## As if </head>
2960                  pop @{$self->{open_elements}};
2961    
2962                  ## Reprocess in the "after head" insertion mode...
2963                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2964                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
2965                  ## Ignore the token ## ISSUE: An issue in the spec.
2966                  !!!next-token;
2967                  redo B;
2968                }
2969    
2970                ## "after head" insertion mode
2971                ## As if <body>
2972                !!!insert-element ('body');
2973                $self->{insertion_mode} = IN_BODY_IM;
2974                ## reprocess
2975                redo B;
2976              } else {
2977                die "$0: $token->{type}: Unknown token type";
2978              }
2979    
2980              ## ISSUE: An issue in the spec.
2981        } elsif ($self->{insertion_mode} == IN_BODY_IM or
2982                 $self->{insertion_mode} == IN_CELL_IM or
2983                 $self->{insertion_mode} == IN_CAPTION_IM) {
2984              if ($token->{type} eq 'character') {
2985                ## NOTE: There is a code clone of "character in body".
2986                $reconstruct_active_formatting_elements->($insert_to_current);
2987                
2988                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
2989    
2990                !!!next-token;
2991                redo B;
2992              } elsif ($token->{type} eq 'start tag') {
2993                if ({
2994                     caption => 1, col => 1, colgroup => 1, tbody => 1,
2995                     td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
2996                    }->{$token->{tag_name}}) {
2997                  if ($self->{insertion_mode} == IN_CELL_IM) {
2998                    ## have an element in table scope
2999                    my $tn;
3000                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3001                      my $node = $self->{open_elements}->[$_];
3002                      if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3003                        $tn = $node->[1];
3004                        last INSCOPE;
3005                      } elsif ({
3006                                table => 1, html => 1,
3007                               }->{$node->[1]}) {
3008                        last INSCOPE;
3009                      }
3010                    } # INSCOPE
3011                      unless (defined $tn) {
3012                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3013                        ## Ignore the token
3014                        !!!next-token;
3015                        redo B;
3016                      }
3017                    
3018                    ## Close the cell
3019                    !!!back-token; # <?>
3020                    $token = {type => 'end tag', tag_name => $tn};
3021                    redo B;
3022                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3023                    !!!parse-error (type => 'not closed:caption');
3024                    
3025                    ## As if </caption>
3026                    ## have a table element in table scope
3027                    my $i;
3028                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3029                      my $node = $self->{open_elements}->[$_];
3030                      if ($node->[1] eq 'caption') {
3031                        $i = $_;
3032                        last INSCOPE;
3033                      } elsif ({
3034                                table => 1, html => 1,
3035                               }->{$node->[1]}) {
3036                        last INSCOPE;
3037                      }
3038                    } # INSCOPE
3039                      unless (defined $i) {
3040                        !!!parse-error (type => 'unmatched end tag:caption');
3041                        ## Ignore the token
3042                        !!!next-token;
3043                        redo B;
3044                      }
3045                    
3046                    ## generate implied end tags
3047                    if ({
3048                         dd => 1, dt => 1, li => 1, p => 1,
3049                         td => 1, th => 1, tr => 1,
3050                         tbody => 1, tfoot=> 1, thead => 1,
3051                        }->{$self->{open_elements}->[-1]->[1]}) {
3052                      !!!back-token; # <?>
3053                      $token = {type => 'end tag', tag_name => 'caption'};
3054                      !!!back-token;
3055                      $token = {type => 'end tag',
3056                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3057                      redo B;
3058                    }
3059    
3060                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3061                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3062                    }
3063                    
3064                    splice @{$self->{open_elements}}, $i;
3065                    
3066                    $clear_up_to_marker->();
3067                    
3068                    $self->{insertion_mode} = IN_TABLE_IM;
3069                    
3070                    ## reprocess
3071                    redo B;
3072                  } else {
3073                    #
3074                  }
3075                } else {
3076                  #
3077                }
3078              } elsif ($token->{type} eq 'end tag') {
3079                if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3080                  if ($self->{insertion_mode} == IN_CELL_IM) {
3081                    ## have an element in table scope
3082                    my $i;
3083                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3084                      my $node = $self->{open_elements}->[$_];
3085                      if ($node->[1] eq $token->{tag_name}) {
3086                        $i = $_;
3087                        last INSCOPE;
3088                      } elsif ({
3089                                table => 1, html => 1,
3090                               }->{$node->[1]}) {
3091                        last INSCOPE;
3092                      }
3093                    } # INSCOPE
3094                      unless (defined $i) {
3095                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3096                        ## Ignore the token
3097                        !!!next-token;
3098                        redo B;
3099                      }
3100                    
3101                    ## generate implied end tags
3102                    if ({
3103                         dd => 1, dt => 1, li => 1, p => 1,
3104                         td => ($token->{tag_name} eq 'th'),
3105                         th => ($token->{tag_name} eq 'td'),
3106                         tr => 1,
3107                         tbody => 1, tfoot=> 1, thead => 1,
3108                        }->{$self->{open_elements}->[-1]->[1]}) {
3109                      !!!back-token;
3110                      $token = {type => 'end tag',
3111                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3112                      redo B;
3113                    }
3114                    
3115                    if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3116                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3117                    }
3118                    
3119                    splice @{$self->{open_elements}}, $i;
3120                    
3121                    $clear_up_to_marker->();
3122                    
3123                    $self->{insertion_mode} = IN_ROW_IM;
3124                    
3125                    !!!next-token;
3126                    redo B;
3127                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3128                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3129                    ## Ignore the token
3130                    !!!next-token;
3131                    redo B;
3132                  } else {
3133                    #
3134                  }
3135                } elsif ($token->{tag_name} eq 'caption') {
3136                  if ($self->{insertion_mode} == IN_CAPTION_IM) {
3137                    ## have a table element in table scope
3138                    my $i;
3139                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3140                      my $node = $self->{open_elements}->[$_];
3141                      if ($node->[1] eq $token->{tag_name}) {
3142                        $i = $_;
3143                        last INSCOPE;
3144                      } elsif ({
3145                                table => 1, html => 1,
3146                               }->{$node->[1]}) {
3147                        last INSCOPE;
3148                      }
3149                    } # INSCOPE
3150                      unless (defined $i) {
3151                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3152                        ## Ignore the token
3153                        !!!next-token;
3154                        redo B;
3155                      }
3156                    
3157                    ## generate implied end tags
3158                    if ({
3159                         dd => 1, dt => 1, li => 1, p => 1,
3160                         td => 1, th => 1, tr => 1,
3161                         tbody => 1, tfoot=> 1, thead => 1,
3162                        }->{$self->{open_elements}->[-1]->[1]}) {
3163                      !!!back-token;
3164                      $token = {type => 'end tag',
3165                                tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3166                      redo B;
3167                    }
3168                    
3169                    if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3170                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3171                    }
3172                    
3173                    splice @{$self->{open_elements}}, $i;
3174                    
3175                    $clear_up_to_marker->();
3176                    
3177                    $self->{insertion_mode} = IN_TABLE_IM;
3178                    
3179                    !!!next-token;
3180                    redo B;
3181                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
3182                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3183                    ## Ignore the token
3184                    !!!next-token;
3185                    redo B;
3186                  } else {
3187                    #
3188                  }
3189                } elsif ({
3190                          table => 1, tbody => 1, tfoot => 1,
3191                          thead => 1, tr => 1,
3192                         }->{$token->{tag_name}} and
3193                         $self->{insertion_mode} == IN_CELL_IM) {
3194                  ## have an element in table scope
3195                  my $i;
3196                  my $tn;
3197                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3198                    my $node = $self->{open_elements}->[$_];
3199                    if ($node->[1] eq $token->{tag_name}) {
3200                      $i = $_;
3201                      last INSCOPE;
3202                    } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
3203                      $tn = $node->[1];
3204                      ## NOTE: There is exactly one |td| or |th| element
3205                      ## in scope in the stack of open elements by definition.
3206                    } elsif ({
3207                              table => 1, html => 1,
3208                             }->{$node->[1]}) {
3209                      last INSCOPE;
3210                    }
3211                  } # INSCOPE
3212                  unless (defined $i) {
3213                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3214                    ## Ignore the token
3215                    !!!next-token;
3216                    redo B;
3217                  }
3218    
3219                  ## Close the cell
3220                  !!!back-token; # </?>
3221                  $token = {type => 'end tag', tag_name => $tn};
3222                  redo B;
3223                } elsif ($token->{tag_name} eq 'table' and
3224                         $self->{insertion_mode} == IN_CAPTION_IM) {
3225                  !!!parse-error (type => 'not closed:caption');
3226    
3227                  ## As if </caption>
3228                  ## have a table element in table scope
3229                  my $i;
3230                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3231                    my $node = $self->{open_elements}->[$_];
3232                    if ($node->[1] eq 'caption') {
3233                      $i = $_;
3234                      last INSCOPE;
3235                    } elsif ({
3236                              table => 1, html => 1,
3237                             }->{$node->[1]}) {
3238                      last INSCOPE;
3239                    }
3240                  } # INSCOPE
3241                  unless (defined $i) {
3242                    !!!parse-error (type => 'unmatched end tag:caption');
3243                    ## Ignore the token
3244                    !!!next-token;
3245                    redo B;
3246                  }
3247                  
3248                  ## generate implied end tags
3249                  if ({
3250                       dd => 1, dt => 1, li => 1, p => 1,
3251                       td => 1, th => 1, tr => 1,
3252                       tbody => 1, tfoot=> 1, thead => 1,
3253                      }->{$self->{open_elements}->[-1]->[1]}) {
3254                    !!!back-token; # </table>
3255                    $token = {type => 'end tag', tag_name => 'caption'};
3256                    !!!back-token;
3257                    $token = {type => 'end tag',
3258                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3259                    redo B;
3260                  }
3261    
3262                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3263                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3264                  }
3265    
3266                  splice @{$self->{open_elements}}, $i;
3267    
3268                  $clear_up_to_marker->();
3269    
3270                  $self->{insertion_mode} = IN_TABLE_IM;
3271    
3272                  ## reprocess
3273                  redo B;
3274                } elsif ({
3275                          body => 1, col => 1, colgroup => 1, html => 1,
3276                         }->{$token->{tag_name}}) {
3277                  if ($self->{insertion_mode} == IN_CELL_IM or
3278                      $self->{insertion_mode} == IN_CAPTION_IM) {
3279                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3280                    ## Ignore the token
3281                    !!!next-token;
3282                    redo B;
3283                  } else {
3284                    #
3285                  }
3286                } elsif ({
3287                          tbody => 1, tfoot => 1,
3288                          thead => 1, tr => 1,
3289                         }->{$token->{tag_name}} and
3290                         $self->{insertion_mode} == IN_CAPTION_IM) {
3291                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3292                  ## Ignore the token
3293                  !!!next-token;
3294                  redo B;
3295                } else {
3296                  #
3297                }
3298          } else {
3299            die "$0: $token->{type}: Unknown token type";
3300          }
3301    
3302          $insert = $insert_to_current;
3303          #
3304        } elsif ($self->{insertion_mode} == IN_ROW_IM or
3305                 $self->{insertion_mode} == IN_TABLE_BODY_IM or
3306                 $self->{insertion_mode} == IN_TABLE_IM) {
3307              if ($token->{type} eq 'character') {
3308                ## NOTE: There are "character in table" code clones.
3309                if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3310                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3311                  
3312                  unless (length $token->{data}) {
3313                    !!!next-token;
3314                    redo B;
3315                  }
3316                }
3317    
3318                !!!parse-error (type => 'in table:#character');
3319    
3320                ## As if in body, but insert into foster parent element
3321                ## ISSUE: Spec says that "whenever a node would be inserted
3322                ## into the current node" while characters might not be
3323                ## result in a new Text node.
3324                $reconstruct_active_formatting_elements->($insert_to_foster);
3325                
3326                if ({
3327                     table => 1, tbody => 1, tfoot => 1,
3328                     thead => 1, tr => 1,
3329                    }->{$self->{open_elements}->[-1]->[1]}) {
3330                  # MUST
3331                  my $foster_parent_element;
3332                  my $next_sibling;
3333                  my $prev_sibling;
3334                  OE: for (reverse 0..$#{$self->{open_elements}}) {
3335                    if ($self->{open_elements}->[$_]->[1] eq 'table') {
3336                      my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3337                      if (defined $parent and $parent->node_type == 1) {
3338                        $foster_parent_element = $parent;
3339                        $next_sibling = $self->{open_elements}->[$_]->[0];
3340                        $prev_sibling = $next_sibling->previous_sibling;
3341                      } else {
3342                        $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
3343                        $prev_sibling = $foster_parent_element->last_child;
3344                      }
3345                      last OE;
3346                    }
3347                  } # OE
3348                  $foster_parent_element = $self->{open_elements}->[0]->[0] and
3349                  $prev_sibling = $foster_parent_element->last_child
3350                    unless defined $foster_parent_element;
3351                  if (defined $prev_sibling and
3352                      $prev_sibling->node_type == 3) {
3353                    $prev_sibling->manakai_append_text ($token->{data});
3354                  } else {
3355                    $foster_parent_element->insert_before
3356                      ($self->{document}->create_text_node ($token->{data}),
3357                       $next_sibling);
3358                  }
3359                } else {
3360                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3361                }
3362                
3363                !!!next-token;
3364                redo B;
3365              } elsif ($token->{type} eq 'start tag') {
3366                if ({
3367                     tr => ($self->{insertion_mode} != IN_ROW_IM),
3368                     th => 1, td => 1,
3369                    }->{$token->{tag_name}}) {
3370                  if ($self->{insertion_mode} == IN_TABLE_IM) {
3371                    ## Clear back to table context
3372                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3373                           $self->{open_elements}->[-1]->[1] ne 'html') {
3374                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3375                      pop @{$self->{open_elements}};
3376                    }
3377                    
3378                    !!!insert-element ('tbody');
3379                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3380                    ## reprocess in the "in table body" insertion mode...
3381                  }
3382    
3383                  if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3384                    unless ($token->{tag_name} eq 'tr') {
3385                      !!!parse-error (type => 'missing start tag:tr');
3386                    }
3387                    
3388                    ## Clear back to table body context
3389                    while (not {
3390                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3391                    }->{$self->{open_elements}->[-1]->[1]}) {
3392                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3393                      pop @{$self->{open_elements}};
3394                    }
3395                    
3396                    $self->{insertion_mode} = IN_ROW_IM;
3397                    if ($token->{tag_name} eq 'tr') {
3398                      !!!insert-element ($token->{tag_name}, $token->{attributes});
3399                      !!!next-token;
3400                      redo B;
3401                    } else {
3402                      !!!insert-element ('tr');
3403                      ## reprocess in the "in row" insertion mode
3404                    }
3405                  }
3406    
3407                  ## Clear back to table row context
3408                  while (not {
3409                    tr => 1, html => 1,
3410                  }->{$self->{open_elements}->[-1]->[1]}) {
3411                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3412                    pop @{$self->{open_elements}};
3413                  }
3414                  
3415                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3416                  $self->{insertion_mode} = IN_CELL_IM;
3417    
3418                  push @$active_formatting_elements, ['#marker', ''];
3419                  
3420                  !!!next-token;
3421                  redo B;
3422                } elsif ({
3423                          caption => 1, col => 1, colgroup => 1,
3424                          tbody => 1, tfoot => 1, thead => 1,
3425                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3426                         }->{$token->{tag_name}}) {
3427                  if ($self->{insertion_mode} == IN_ROW_IM) {
3428                    ## As if </tr>
3429                    ## have an element in table scope
3430                    my $i;
3431                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3432                      my $node = $self->{open_elements}->[$_];
3433                      if ($node->[1] eq 'tr') {
3434                        $i = $_;
3435                        last INSCOPE;
3436                      } elsif ({
3437                                table => 1, html => 1,
3438                               }->{$node->[1]}) {
3439                        last INSCOPE;
3440                      }
3441                    } # INSCOPE
3442                    unless (defined $i) {
3443                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
3444                      ## Ignore the token
3445                      !!!next-token;
3446                      redo B;
3447                    }
3448                    
3449                    ## Clear back to table row context
3450                    while (not {
3451                      tr => 1, html => 1,
3452                    }->{$self->{open_elements}->[-1]->[1]}) {
3453                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3454                      pop @{$self->{open_elements}};
3455                    }
3456                    
3457                    pop @{$self->{open_elements}}; # tr
3458                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3459                    if ($token->{tag_name} eq 'tr') {
3460                      ## reprocess
3461                      redo B;
3462                    } else {
3463                      ## reprocess in the "in table body" insertion mode...
3464                    }
3465                  }
3466    
3467                  if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3468                    ## have an element in table scope
3469                    my $i;
3470                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3471                      my $node = $self->{open_elements}->[$_];
3472                      if ({
3473                           tbody => 1, thead => 1, tfoot => 1,
3474                          }->{$node->[1]}) {
3475                        $i = $_;
3476                        last INSCOPE;
3477                      } elsif ({
3478                                table => 1, html => 1,
3479                               }->{$node->[1]}) {
3480                        last INSCOPE;
3481                      }
3482                    } # INSCOPE
3483                    unless (defined $i) {
3484                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3485                      ## Ignore the token
3486                      !!!next-token;
3487                      redo B;
3488                    }
3489    
3490                    ## Clear back to table body context
3491                    while (not {
3492                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3493                    }->{$self->{open_elements}->[-1]->[1]}) {
3494                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3495                      pop @{$self->{open_elements}};
3496                    }
3497                    
3498                    ## As if <{current node}>
3499                    ## have an element in table scope
3500                    ## true by definition
3501                    
3502                    ## Clear back to table body context
3503                    ## nop by definition
3504                    
3505                    pop @{$self->{open_elements}};
3506                    $self->{insertion_mode} = IN_TABLE_IM;
3507                    ## reprocess in "in table" insertion mode...
3508                  }
3509    
3510                  if ($token->{tag_name} eq 'col') {
3511                    ## Clear back to table context
3512                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3513                           $self->{open_elements}->[-1]->[1] ne 'html') {
3514                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3515                      pop @{$self->{open_elements}};
3516                    }
3517                    
3518                    !!!insert-element ('colgroup');
3519                    $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
3520                    ## reprocess
3521                    redo B;
3522                  } elsif ({
3523                            caption => 1,
3524                            colgroup => 1,
3525                            tbody => 1, tfoot => 1, thead => 1,
3526                           }->{$token->{tag_name}}) {
3527                    ## Clear back to table context
3528                    while ($self->{open_elements}->[-1]->[1] ne 'table' and
3529                           $self->{open_elements}->[-1]->[1] ne 'html') {
3530                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3531                      pop @{$self->{open_elements}};
3532                    }
3533                    
3534                    push @$active_formatting_elements, ['#marker', '']
3535                        if $token->{tag_name} eq 'caption';
3536                    
3537                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3538                    $self->{insertion_mode} = {
3539                                               caption => IN_CAPTION_IM,
3540                                               colgroup => IN_COLUMN_GROUP_IM,
3541                                               tbody => IN_TABLE_BODY_IM,
3542                                               tfoot => IN_TABLE_BODY_IM,
3543                                               thead => IN_TABLE_BODY_IM,
3544                                              }->{$token->{tag_name}};
3545                    !!!next-token;
3546                    redo B;
3547                  } else {
3548                    die "$0: in table: <>: $token->{tag_name}";
3549                  }
3550                } elsif ($token->{tag_name} eq 'table') {
3551                  ## NOTE: There are code clones for this "table in table"
3552                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3553    
3554                  ## As if </table>
3555                  ## have a table element in table scope
3556                  my $i;
3557                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3558                    my $node = $self->{open_elements}->[$_];
3559                    if ($node->[1] eq 'table') {
3560                      $i = $_;
3561                      last INSCOPE;
3562                    } elsif ({
3563                              table => 1, html => 1,
3564                             }->{$node->[1]}) {
3565                      last INSCOPE;
3566                    }
3567                  } # INSCOPE
3568                  unless (defined $i) {
3569                    !!!parse-error (type => 'unmatched end tag:table');
3570                    ## Ignore tokens </table><table>
3571                    !!!next-token;
3572                    redo B;
3573                  }
3574                  
3575                  ## generate implied end tags
3576                  if ({
3577                       dd => 1, dt => 1, li => 1, p => 1,
3578                       td => 1, th => 1, tr => 1,
3579                       tbody => 1, tfoot=> 1, thead => 1,
3580                      }->{$self->{open_elements}->[-1]->[1]}) {
3581                    !!!back-token; # <table>
3582                    $token = {type => 'end tag', tag_name => 'table'};
3583                    !!!back-token;
3584                    $token = {type => 'end tag',
3585                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3586                    redo B;
3587                  }
3588    
3589                  if ($self->{open_elements}->[-1]->[1] ne 'table') {
3590                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3591                  }
3592    
3593                  splice @{$self->{open_elements}}, $i;
3594    
3595                  $self->_reset_insertion_mode;
3596    
3597                  ## reprocess
3598                  redo B;
3599                } else {
3600                  #
3601                }
3602              } elsif ($token->{type} eq 'end tag') {
3603                if ($token->{tag_name} eq 'tr' and
3604                    $self->{insertion_mode} == IN_ROW_IM) {
3605                  ## have an element in table scope
3606                  my $i;
3607                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3608                    my $node = $self->{open_elements}->[$_];
3609                    if ($node->[1] eq $token->{tag_name}) {
3610                      $i = $_;
3611                      last INSCOPE;
3612                    } elsif ({
3613                              table => 1, html => 1,
3614                             }->{$node->[1]}) {
3615                      last INSCOPE;
3616                    }
3617                  } # INSCOPE
3618                  unless (defined $i) {
3619                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3620                    ## Ignore the token
3621                    !!!next-token;
3622                    redo B;
3623                  }
3624    
3625                  ## Clear back to table row context
3626                  while (not {
3627                    tr => 1, html => 1,
3628                  }->{$self->{open_elements}->[-1]->[1]}) {
3629                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3630                    pop @{$self->{open_elements}};
3631                  }
3632    
3633                  pop @{$self->{open_elements}}; # tr
3634                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3635                  !!!next-token;
3636                  redo B;
3637                } elsif ($token->{tag_name} eq 'table') {
3638                  if ($self->{insertion_mode} == IN_ROW_IM) {
3639                    ## As if </tr>
3640                    ## have an element in table scope
3641                    my $i;
3642                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3643                      my $node = $self->{open_elements}->[$_];
3644                      if ($node->[1] eq 'tr') {
3645                        $i = $_;
3646                        last INSCOPE;
3647                      } elsif ({
3648                                table => 1, html => 1,
3649                               }->{$node->[1]}) {
3650                        last INSCOPE;
3651                      }
3652                    } # INSCOPE
3653                    unless (defined $i) {
3654                      !!!parse-error (type => 'unmatched end tag:'.$token->{type});
3655                      ## Ignore the token
3656                      !!!next-token;
3657                      redo B;
3658                    }
3659                    
3660                    ## Clear back to table row context
3661                    while (not {
3662                      tr => 1, html => 1,
3663                    }->{$self->{open_elements}->[-1]->[1]}) {
3664                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3665                      pop @{$self->{open_elements}};
3666                    }
3667                    
3668                    pop @{$self->{open_elements}}; # tr
3669                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3670                    ## reprocess in the "in table body" insertion mode...
3671                  }
3672    
3673                  if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3674                    ## have an element in table scope
3675                    my $i;
3676                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3677                      my $node = $self->{open_elements}->[$_];
3678                      if ({
3679                           tbody => 1, thead => 1, tfoot => 1,
3680                          }->{$node->[1]}) {
3681                        $i = $_;
3682                        last INSCOPE;
3683                      } elsif ({
3684                                table => 1, html => 1,
3685                               }->{$node->[1]}) {
3686                        last INSCOPE;
3687                      }
3688                    } # INSCOPE
3689                    unless (defined $i) {
3690                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3691                      ## Ignore the token
3692                      !!!next-token;
3693                      redo B;
3694                    }
3695                    
3696                    ## Clear back to table body context
3697                    while (not {
3698                      tbody => 1, tfoot => 1, thead => 1, html => 1,
3699                    }->{$self->{open_elements}->[-1]->[1]}) {
3700                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3701                      pop @{$self->{open_elements}};
3702                    }
3703                    
3704                    ## As if <{current node}>
3705                    ## have an element in table scope
3706                    ## true by definition
3707                    
3708                    ## Clear back to table body context
3709                    ## nop by definition
3710                    
3711                    pop @{$self->{open_elements}};
3712                    $self->{insertion_mode} = IN_TABLE_IM;
3713                    ## reprocess in the "in table" insertion mode...
3714                  }
3715    
3716                  ## have a table element in table scope
3717                  my $i;
3718                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3719                    my $node = $self->{open_elements}->[$_];
3720                    if ($node->[1] eq $token->{tag_name}) {
3721                      $i = $_;
3722                      last INSCOPE;
3723                    } elsif ({
3724                              table => 1, html => 1,
3725                             }->{$node->[1]}) {
3726                      last INSCOPE;
3727                    }
3728                  } # INSCOPE
3729                  unless (defined $i) {
3730                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3731                    ## Ignore the token
3732                    !!!next-token;
3733                    redo B;
3734                  }
3735    
3736                  ## generate implied end tags
3737                  if ({
3738                       dd => 1, dt => 1, li => 1, p => 1,
3739                       td => 1, th => 1, tr => 1,
3740                       tbody => 1, tfoot=> 1, thead => 1,
3741                      }->{$self->{open_elements}->[-1]->[1]}) {
3742                    !!!back-token;
3743                    $token = {type => 'end tag',
3744                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3745                    redo B;
3746                  }
3747                  
3748                  if ($self->{open_elements}->[-1]->[1] ne 'table') {
3749                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3750                  }
3751                    
3752                  splice @{$self->{open_elements}}, $i;
3753                  
3754                  $self->_reset_insertion_mode;
3755                  
3756                  !!!next-token;
3757                  redo B;
3758                } elsif ({
3759                          tbody => 1, tfoot => 1, thead => 1,
3760                         }->{$token->{tag_name}} and
3761                         ($self->{insertion_mode} == IN_ROW_IM or
3762                          $self->{insertion_mode} == IN_TABLE_BODY_IM)) {
3763                  if ($self->{insertion_mode} == IN_ROW_IM) {
3764                    ## have an element in table scope
3765                    my $i;
3766                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3767                      my $node = $self->{open_elements}->[$_];
3768                      if ($node->[1] eq $token->{tag_name}) {
3769                        $i = $_;
3770                        last INSCOPE;
3771                      } elsif ({
3772                                table => 1, html => 1,
3773                               }->{$node->[1]}) {
3774                        last INSCOPE;
3775                      }
3776                    } # INSCOPE
3777                      unless (defined $i) {
3778                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3779                        ## Ignore the token
3780                        !!!next-token;
3781                        redo B;
3782                      }
3783                    
3784                    ## As if </tr>
3785                    ## have an element in table scope
3786                    my $i;
3787                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3788                      my $node = $self->{open_elements}->[$_];
3789                      if ($node->[1] eq 'tr') {
3790                        $i = $_;
3791                        last INSCOPE;
3792                      } elsif ({
3793                                table => 1, html => 1,
3794                               }->{$node->[1]}) {
3795                        last INSCOPE;
3796                      }
3797                    } # INSCOPE
3798                      unless (defined $i) {
3799                        !!!parse-error (type => 'unmatched end tag:tr');
3800                        ## Ignore the token
3801                        !!!next-token;
3802                        redo B;
3803                      }
3804                    
3805                    ## Clear back to table row context
3806                    while (not {
3807                      tr => 1, html => 1,
3808                    }->{$self->{open_elements}->[-1]->[1]}) {
3809                      !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3810                      pop @{$self->{open_elements}};
3811                    }
3812                    
3813                    pop @{$self->{open_elements}}; # tr
3814                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
3815                    ## reprocess in the "in table body" insertion mode...
3816                  }
3817    
3818                  ## have an element in table scope
3819                  my $i;
3820                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3821                    my $node = $self->{open_elements}->[$_];
3822                    if ($node->[1] eq $token->{tag_name}) {
3823                      $i = $_;
3824                      last INSCOPE;
3825                    } elsif ({
3826                              table => 1, html => 1,
3827                             }->{$node->[1]}) {
3828                      last INSCOPE;
3829                    }
3830                  } # INSCOPE
3831                  unless (defined $i) {
3832                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3833                    ## Ignore the token
3834                    !!!next-token;
3835                    redo B;
3836                  }
3837    
3838                  ## Clear back to table body context
3839                  while (not {
3840                    tbody => 1, tfoot => 1, thead => 1, html => 1,
3841                  }->{$self->{open_elements}->[-1]->[1]}) {
3842                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3843                    pop @{$self->{open_elements}};
3844                  }
3845    
3846                  pop @{$self->{open_elements}};
3847                  $self->{insertion_mode} = IN_TABLE_IM;
3848                  !!!next-token;
3849                  redo B;
3850                } elsif ({
3851                          body => 1, caption => 1, col => 1, colgroup => 1,
3852                          html => 1, td => 1, th => 1,
3853                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3854                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
3855                         }->{$token->{tag_name}}) {
3856                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3857                  ## Ignore the token
3858                  !!!next-token;
3859                  redo B;
3860                } else {
3861                  #
3862                }
3863              } else {
3864                die "$0: $token->{type}: Unknown token type";
3865              }
3866    
3867          !!!parse-error (type => 'in table:'.$token->{tag_name});
3868    
3869          $insert = $insert_to_foster;
3870          #
3871        } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
3872              if ($token->{type} eq 'character') {
3873                if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3874                  $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3875                  unless (length $token->{data}) {
3876                    !!!next-token;
3877                    redo B;
3878                  }
3879                }
3880                
3881                #
3882              } elsif ($token->{type} eq 'start tag') {
3883                if ($token->{tag_name} eq 'col') {
3884                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3885                  pop @{$self->{open_elements}};
3886                  !!!next-token;
3887                  redo B;
3888                } else {
3889                  #
3890                }
3891              } elsif ($token->{type} eq 'end tag') {
3892                if ($token->{tag_name} eq 'colgroup') {
3893                  if ($self->{open_elements}->[-1]->[1] eq 'html') {
3894                    !!!parse-error (type => 'unmatched end tag:colgroup');
3895                    ## Ignore the token
3896                    !!!next-token;
3897                    redo B;
3898                  } else {
3899                    pop @{$self->{open_elements}}; # colgroup
3900                    $self->{insertion_mode} = IN_TABLE_IM;
3901                    !!!next-token;
3902                    redo B;            
3903                  }
3904                } elsif ($token->{tag_name} eq 'col') {
3905                  !!!parse-error (type => 'unmatched end tag:col');
3906                  ## Ignore the token
3907                  !!!next-token;
3908                  redo B;
3909                } else {
3910                  #
3911                }
3912              } else {
3913                #
3914              }
3915    
3916              ## As if </colgroup>
3917              if ($self->{open_elements}->[-1]->[1] eq 'html') {
3918                !!!parse-error (type => 'unmatched end tag:colgroup');
3919                ## Ignore the token
3920                !!!next-token;
3921                redo B;
3922              } else {
3923                pop @{$self->{open_elements}}; # colgroup
3924                $self->{insertion_mode} = IN_TABLE_IM;
3925                ## reprocess
3926                redo B;
3927              }
3928        } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
3929              if ($token->{type} eq 'character') {
3930                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3931                !!!next-token;
3932                redo B;
3933              } elsif ($token->{type} eq 'start tag') {
3934                if ($token->{tag_name} eq 'option') {
3935                  if ($self->{open_elements}->[-1]->[1] eq 'option') {
3936                    ## As if </option>
3937                    pop @{$self->{open_elements}};
3938                  }
3939    
3940                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3941                  !!!next-token;
3942                  redo B;
3943                } elsif ($token->{tag_name} eq 'optgroup') {
3944                  if ($self->{open_elements}->[-1]->[1] eq 'option') {
3945                    ## As if </option>
3946                    pop @{$self->{open_elements}};
3947                  }
3948    
3949                  if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
3950                    ## As if </optgroup>
3951                    pop @{$self->{open_elements}};
3952                  }
3953    
3954                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3955                  !!!next-token;
3956                  redo B;
3957                } elsif ($token->{tag_name} eq 'select') {
3958                  !!!parse-error (type => 'not closed:select');
3959                  ## As if </select> instead
3960                  ## have an element in table scope
3961                  my $i;
3962                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3963                    my $node = $self->{open_elements}->[$_];
3964                    if ($node->[1] eq $token->{tag_name}) {
3965                      $i = $_;
3966                      last INSCOPE;
3967                    } elsif ({
3968                              table => 1, html => 1,
3969                             }->{$node->[1]}) {
3970                      last INSCOPE;
3971                    }
3972                  } # INSCOPE
3973                  unless (defined $i) {
3974                    !!!parse-error (type => 'unmatched end tag:select');
3975                    ## Ignore the token
3976                    !!!next-token;
3977                    redo B;
3978                  }
3979                  
3980                  splice @{$self->{open_elements}}, $i;
3981    
3982                  $self->_reset_insertion_mode;
3983    
3984                  !!!next-token;
3985                  redo B;
3986                } else {
3987                  #
3988                }
3989              } elsif ($token->{type} eq 'end tag') {
3990                if ($token->{tag_name} eq 'optgroup') {
3991                  if ($self->{open_elements}->[-1]->[1] eq 'option' and
3992                      $self->{open_elements}->[-2]->[1] eq 'optgroup') {
3993                    ## As if </option>
3994                    splice @{$self->{open_elements}}, -2;
3995                  } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
3996                    pop @{$self->{open_elements}};
3997                  } else {
3998                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3999                    ## Ignore the token
4000                  }
4001                  !!!next-token;
4002                  redo B;
4003                } elsif ($token->{tag_name} eq 'option') {
4004                  if ($self->{open_elements}->[-1]->[1] eq 'option') {
4005                    pop @{$self->{open_elements}};
4006                  } else {
4007                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4008                    ## Ignore the token
4009                  }
4010                  !!!next-token;
4011                  redo B;
4012                } elsif ($token->{tag_name} eq 'select') {
4013                  ## have an element in table scope
4014                  my $i;
4015                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4016                    my $node = $self->{open_elements}->[$_];
4017                    if ($node->[1] eq $token->{tag_name}) {
4018                      $i = $_;
4019                      last INSCOPE;
4020                    } elsif ({
4021                              table => 1, html => 1,
4022                             }->{$node->[1]}) {
4023                      last INSCOPE;
4024                    }
4025                  } # INSCOPE
4026                  unless (defined $i) {
4027                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4028                    ## Ignore the token
4029                    !!!next-token;
4030                    redo B;
4031                  }
4032                  
4033                  splice @{$self->{open_elements}}, $i;
4034    
4035                  $self->_reset_insertion_mode;
4036    
4037                  !!!next-token;
4038                  redo B;
4039                } elsif ({
4040                          caption => 1, table => 1, tbody => 1,
4041                          tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
4042                         }->{$token->{tag_name}}) {
4043                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4044                  
4045                  ## have an element in table scope
4046                  my $i;
4047                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4048                    my $node = $self->{open_elements}->[$_];
4049                    if ($node->[1] eq $token->{tag_name}) {
4050                      $i = $_;
4051                      last INSCOPE;
4052                    } elsif ({
4053                              table => 1, html => 1,
4054                             }->{$node->[1]}) {
4055                      last INSCOPE;
4056                    }
4057                  } # INSCOPE
4058                  unless (defined $i) {
4059                    ## Ignore the token
4060                    !!!next-token;
4061                    redo B;
4062                  }
4063                  
4064                  ## As if </select>
4065                  ## have an element in table scope
4066                  undef $i;
4067                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4068                    my $node = $self->{open_elements}->[$_];
4069                    if ($node->[1] eq 'select') {
4070                      $i = $_;
4071                      last INSCOPE;
4072                    } elsif ({
4073                              table => 1, html => 1,
4074                             }->{$node->[1]}) {
4075                      last INSCOPE;
4076                    }
4077                  } # INSCOPE
4078                  unless (defined $i) {
4079                    !!!parse-error (type => 'unmatched end tag:select');
4080                    ## Ignore the </select> token
4081                    !!!next-token; ## TODO: ok?
4082                    redo B;
4083                  }
4084                  
4085                  splice @{$self->{open_elements}}, $i;
4086    
4087                  $self->_reset_insertion_mode;
4088    
4089                  ## reprocess
4090                  redo B;
4091                } else {
4092                  #
4093                }
4094              } else {
4095                #
4096              }
4097    
4098              !!!parse-error (type => 'in select:'.$token->{tag_name});
4099              ## Ignore the token
4100            !!!next-token;            !!!next-token;
4101              redo B;
4102        } elsif ($self->{insertion_mode} == AFTER_BODY_IM or
4103                 $self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4104          if ($token->{type} eq 'character') {
4105            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4106              my $data = $1;
4107              ## As if in body
4108              $reconstruct_active_formatting_elements->($insert_to_current);
4109                  
4110              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4111              
4112              unless (length $token->{data}) {
4113                !!!next-token;
4114                redo B;
4115              }
4116          }          }
4117          if (length $text) {          
4118            $title_el->manakai_append_text ($text);          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4119              !!!parse-error (type => 'after html:#character');
4120    
4121              ## Reprocess in the "main" phase, "after body" insertion mode...
4122          }          }
4123                    
4124          $self->{content_model_flag} = 'PCDATA';          ## "after body" insertion mode
4125            !!!parse-error (type => 'after body:#character');
4126    
4127            $self->{insertion_mode} = IN_BODY_IM;
4128            ## reprocess
4129            redo B;
4130          } elsif ($token->{type} eq 'start tag') {
4131            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4132              !!!parse-error (type => 'after html:'.$token->{tag_name});
4133              
4134              ## Reprocess in the "main" phase, "after body" insertion mode...
4135            }
4136    
4137            ## "after body" insertion mode
4138            !!!parse-error (type => 'after body:'.$token->{tag_name});
4139    
4140            $self->{insertion_mode} = IN_BODY_IM;
4141            ## reprocess
4142            redo B;
4143          } elsif ($token->{type} eq 'end tag') {
4144            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4145              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4146              
4147              $self->{insertion_mode} = AFTER_BODY_IM;
4148              ## Reprocess in the "main" phase, "after body" insertion mode...
4149            }
4150    
4151            ## "after body" insertion mode
4152            if ($token->{tag_name} eq 'html') {
4153              if (defined $self->{inner_html_node}) {
4154                !!!parse-error (type => 'unmatched end tag:html');
4155                ## Ignore the token
4156                !!!next-token;
4157                redo B;
4158              } else {
4159                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
4160                !!!next-token;
4161                redo B;
4162              }
4163            } else {
4164              !!!parse-error (type => 'after body:/'.$token->{tag_name});
4165    
4166              $self->{insertion_mode} = IN_BODY_IM;
4167              ## reprocess
4168              redo B;
4169            }
4170          } else {
4171            die "$0: $token->{type}: Unknown token type";
4172          }
4173        } elsif ($self->{insertion_mode} == IN_FRAMESET_IM or
4174                 $self->{insertion_mode} == AFTER_FRAMESET_IM or
4175                 $self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4176          if ($token->{type} eq 'character') {
4177            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4178              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4179              
4180              unless (length $token->{data}) {
4181                !!!next-token;
4182                redo B;
4183              }
4184            }
4185                    
4186          if ($token->{type} eq 'end tag' and          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
4187              $token->{tag_name} eq 'title') {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4188                !!!parse-error (type => 'in frameset:#character');
4189              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4190                !!!parse-error (type => 'after frameset:#character');
4191              } else { # "after html frameset"
4192                !!!parse-error (type => 'after html:#character');
4193    
4194                $self->{insertion_mode} = AFTER_FRAMESET_IM;
4195                ## Reprocess in the "main" phase, "after frameset"...
4196                !!!parse-error (type => 'after frameset:#character');
4197              }
4198              
4199              ## Ignore the token.
4200              if (length $token->{data}) {
4201                ## reprocess the rest of characters
4202              } else {
4203                !!!next-token;
4204              }
4205              redo B;
4206            }
4207            
4208            die qq[$0: Character "$token->{data}"];
4209          } elsif ($token->{type} eq 'start tag') {
4210            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4211              !!!parse-error (type => 'after html:'.$token->{tag_name});
4212    
4213              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4214              ## Process in the "main" phase, "after frameset" insertion mode...
4215            }
4216    
4217            if ($token->{tag_name} eq 'frameset' and
4218                $self->{insertion_mode} == IN_FRAMESET_IM) {
4219              !!!insert-element ($token->{tag_name}, $token->{attributes});
4220              !!!next-token;
4221              redo B;
4222            } elsif ($token->{tag_name} eq 'frame' and
4223                     $self->{insertion_mode} == IN_FRAMESET_IM) {
4224              !!!insert-element ($token->{tag_name}, $token->{attributes});
4225              pop @{$self->{open_elements}};
4226              !!!next-token;
4227              redo B;
4228            } elsif ($token->{tag_name} eq 'noframes') {
4229              ## NOTE: As if in body.
4230              $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4231              redo B;
4232            } else {
4233              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4234                !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4235              } else {
4236                !!!parse-error (type => 'after frameset:'.$token->{tag_name});
4237              }
4238            ## Ignore the token            ## Ignore the token
4239              !!!next-token;
4240              redo B;
4241            }
4242          } elsif ($token->{type} eq 'end tag') {
4243            if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4244              !!!parse-error (type => 'after html:/'.$token->{tag_name});
4245    
4246              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4247              ## Process in the "main" phase, "after frameset" insertion mode...
4248            }
4249    
4250            if ($token->{tag_name} eq 'frameset' and
4251                $self->{insertion_mode} == IN_FRAMESET_IM) {
4252              if ($self->{open_elements}->[-1]->[1] eq 'html' and
4253                  @{$self->{open_elements}} == 1) {
4254                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4255                ## Ignore the token
4256                !!!next-token;
4257              } else {
4258                pop @{$self->{open_elements}};
4259                !!!next-token;
4260              }
4261    
4262              if (not defined $self->{inner_html_node} and
4263                  $self->{open_elements}->[-1]->[1] ne 'frameset') {
4264                $self->{insertion_mode} = AFTER_FRAMESET_IM;
4265              }
4266              redo B;
4267            } elsif ($token->{tag_name} eq 'html' and
4268                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
4269              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
4270              !!!next-token;
4271              redo B;
4272          } else {          } else {
4273            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4274            ## ISSUE: And ignore?              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
4275              } else {
4276                !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
4277              }
4278              ## Ignore the token
4279              !!!next-token;
4280              redo B;
4281          }          }
4282          } else {
4283            die "$0: $token->{type}: Unknown token type";
4284          }
4285    
4286          ## ISSUE: An issue in spec here
4287        } else {
4288          die "$0: $self->{insertion_mode}: Unknown insertion mode";
4289        }
4290    
4291        ## "in body" insertion mode
4292        if ($token->{type} eq 'start tag') {
4293          if ($token->{tag_name} eq 'script') {
4294            ## NOTE: This is an "as if in head" code clone
4295            $script_start_tag->($insert);
4296            redo B;
4297          } elsif ($token->{tag_name} eq 'style') {
4298            ## NOTE: This is an "as if in head" code clone
4299            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4300            redo B;
4301          } elsif ({
4302                    base => 1, link => 1,
4303                   }->{$token->{tag_name}}) {
4304            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4305            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4306            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4307          !!!next-token;          !!!next-token;
4308          return;          redo B;
4309          } elsif ($token->{tag_name} eq 'meta') {
4310            ## NOTE: This is an "as if in head" code clone, only "-t" differs
4311            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4312            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4313    
4314            unless ($self->{confident}) {
4315              my $charset;
4316              if ($token->{attributes}->{charset}) { ## TODO: And if supported
4317                $charset = $token->{attributes}->{charset}->{value};
4318              }
4319              if ($token->{attributes}->{'http-equiv'}) {
4320                ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
4321                if ($token->{attributes}->{'http-equiv'}->{value}
4322                    =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
4323                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4324                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4325                  $charset = defined $1 ? $1 : defined $2 ? $2 : $3;
4326                } ## TODO: And if supported
4327              }
4328              ## TODO: Change the encoding
4329            }
4330    
4331            !!!next-token;
4332            redo B;
4333          } elsif ($token->{tag_name} eq 'title') {
4334            !!!parse-error (type => 'in body:title');
4335            ## NOTE: This is an "as if in head" code clone
4336            $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
4337              if (defined $self->{head_element}) {
4338                $self->{head_element}->append_child ($_[0]);
4339              } else {
4340                $insert->($_[0]);
4341              }
4342            });
4343            redo B;
4344        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
4345          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body');
4346                                
# Line 2494  sub _tree_construction_main ($) { Line 4358  sub _tree_construction_main ($) {
4358            }            }
4359          }          }
4360          !!!next-token;          !!!next-token;
4361          return;          redo B;
4362        } elsif ({        } elsif ({
4363                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
4364                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
# Line 2506  sub _tree_construction_main ($) { Line 4370  sub _tree_construction_main ($) {
4370            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4371              !!!back-token;              !!!back-token;
4372              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => 'end tag', tag_name => 'p'};
4373              return;              redo B;
4374            } elsif ({            } elsif ({
4375                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4376                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2527  sub _tree_construction_main ($) { Line 4391  sub _tree_construction_main ($) {
4391          } else {          } else {
4392            !!!next-token;            !!!next-token;
4393          }          }
4394          return;          redo B;
4395        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
4396          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
4397            !!!parse-error (type => 'in form:form');            !!!parse-error (type => 'in form:form');
4398            ## Ignore the token            ## Ignore the token
4399            !!!next-token;            !!!next-token;
4400            return;            redo B;
4401          } else {          } else {
4402            ## has a p element in scope            ## has a p element in scope
4403            INSCOPE: for (reverse @{$self->{open_elements}}) {            INSCOPE: for (reverse @{$self->{open_elements}}) {
4404              if ($_->[1] eq 'p') {              if ($_->[1] eq 'p') {
4405                !!!back-token;                !!!back-token;
4406                $token = {type => 'end tag', tag_name => 'p'};                $token = {type => 'end tag', tag_name => 'p'};
4407                return;                redo B;
4408              } elsif ({              } elsif ({
4409                        table => 1, caption => 1, td => 1, th => 1,                        table => 1, caption => 1, td => 1, th => 1,
4410                        button => 1, marquee => 1, object => 1, html => 1,                        button => 1, marquee => 1, object => 1, html => 1,
# Line 2552  sub _tree_construction_main ($) { Line 4416  sub _tree_construction_main ($) {
4416            !!!insert-element-t ($token->{tag_name}, $token->{attributes});            !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4417            $self->{form_element} = $self->{open_elements}->[-1]->[0];            $self->{form_element} = $self->{open_elements}->[-1]->[0];
4418            !!!next-token;            !!!next-token;
4419            return;            redo B;
4420          }          }
4421        } elsif ($token->{tag_name} eq 'li') {        } elsif ($token->{tag_name} eq 'li') {
4422          ## has a p element in scope          ## has a p element in scope
# Line 2560  sub _tree_construction_main ($) { Line 4424  sub _tree_construction_main ($) {
4424            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4425              !!!back-token;              !!!back-token;
4426              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => 'end tag', tag_name => 'p'};
4427              return;              redo B;
4428            } elsif ({            } elsif ({
4429                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4430                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2578  sub _tree_construction_main ($) { Line 4442  sub _tree_construction_main ($) {
4442              if ($i != -1) {              if ($i != -1) {
4443                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'end tag missing:'.
4444                                $self->{open_elements}->[-1]->[1]);                                $self->{open_elements}->[-1]->[1]);
               ## TODO: test  
4445              }              }
4446              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
4447              last LI;              last LI;
# Line 2601  sub _tree_construction_main ($) { Line 4464  sub _tree_construction_main ($) {
4464                        
4465          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4466          !!!next-token;          !!!next-token;
4467          return;          redo B;
4468        } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {        } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {
4469          ## has a p element in scope          ## has a p element in scope
4470          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4471            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4472              !!!back-token;              !!!back-token;
4473              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => 'end tag', tag_name => 'p'};
4474              return;              redo B;
4475            } elsif ({            } elsif ({
4476                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4477                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2626  sub _tree_construction_main ($) { Line 4489  sub _tree_construction_main ($) {
4489              if ($i != -1) {              if ($i != -1) {
4490                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'end tag missing:'.
4491                                $self->{open_elements}->[-1]->[1]);                                $self->{open_elements}->[-1]->[1]);
               ## TODO: test  
4492              }              }
4493              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
4494              last LI;              last LI;
# Line 2649  sub _tree_construction_main ($) { Line 4511  sub _tree_construction_main ($) {
4511                        
4512          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4513          !!!next-token;          !!!next-token;
4514          return;          redo B;
4515        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
4516          ## has a p element in scope          ## has a p element in scope
4517          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4518            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4519              !!!back-token;              !!!back-token;
4520              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => 'end tag', tag_name => 'p'};
4521              return;              redo B;
4522            } elsif ({            } elsif ({
4523                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4524                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2667  sub _tree_construction_main ($) { Line 4529  sub _tree_construction_main ($) {
4529                        
4530          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4531                        
4532          $self->{content_model_flag} = 'PLAINTEXT';          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
4533                        
4534          !!!next-token;          !!!next-token;
4535          return;          redo B;
4536        } elsif ({        } elsif ({
4537                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
4538                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 2680  sub _tree_construction_main ($) { Line 4542  sub _tree_construction_main ($) {
4542            if ($node->[1] eq 'p') {            if ($node->[1] eq 'p') {
4543              !!!back-token;              !!!back-token;
4544              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => 'end tag', tag_name => 'p'};
4545              return;              redo B;
4546            } elsif ({            } elsif ({
4547                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4548                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2715  sub _tree_construction_main ($) { Line 4577  sub _tree_construction_main ($) {
4577          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4578                        
4579          !!!next-token;          !!!next-token;
4580          return;          redo B;
4581        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
4582          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
4583            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
# Line 2750  sub _tree_construction_main ($) { Line 4612  sub _tree_construction_main ($) {
4612          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
4613    
4614          !!!next-token;          !!!next-token;
4615          return;          redo B;
4616        } elsif ({        } elsif ({
4617                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
4618                  s => 1, small => 1, strile => 1,                  s => 1, small => 1, strile => 1,
# Line 2762  sub _tree_construction_main ($) { Line 4624  sub _tree_construction_main ($) {
4624          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
4625                    
4626          !!!next-token;          !!!next-token;
4627          return;          redo B;
4628        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
4629          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4630    
# Line 2770  sub _tree_construction_main ($) { Line 4632  sub _tree_construction_main ($) {
4632          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4633            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
4634            if ($node->[1] eq 'nobr') {            if ($node->[1] eq 'nobr') {
4635                !!!parse-error (type => 'not closed:nobr');
4636              !!!back-token;              !!!back-token;
4637              $token = {type => 'end tag', tag_name => 'nobr'};              $token = {type => 'end tag', tag_name => 'nobr'};
4638              return;              redo B;
4639            } elsif ({            } elsif ({
4640                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4641                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2785  sub _tree_construction_main ($) { Line 4648  sub _tree_construction_main ($) {
4648          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
4649                    
4650          !!!next-token;          !!!next-token;
4651          return;          redo B;
4652        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
4653          ## has a button element in scope          ## has a button element in scope
4654          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 2794  sub _tree_construction_main ($) { Line 4657  sub _tree_construction_main ($) {
4657              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button');
4658              !!!back-token;              !!!back-token;
4659              $token = {type => 'end tag', tag_name => 'button'};              $token = {type => 'end tag', tag_name => 'button'};
4660              return;              redo B;
4661            } elsif ({            } elsif ({
4662                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4663                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2809  sub _tree_construction_main ($) { Line 4672  sub _tree_construction_main ($) {
4672          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
4673    
4674          !!!next-token;          !!!next-token;
4675          return;          redo B;
4676        } elsif ($token->{tag_name} eq 'marquee' or        } elsif ($token->{tag_name} eq 'marquee' or
4677                 $token->{tag_name} eq 'object') {                 $token->{tag_name} eq 'object') {
4678          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
# Line 2818  sub _tree_construction_main ($) { Line 4681  sub _tree_construction_main ($) {
4681          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
4682                    
4683          !!!next-token;          !!!next-token;
4684          return;          redo B;
4685        } elsif ($token->{tag_name} eq 'xmp') {        } elsif ($token->{tag_name} eq 'xmp') {
4686          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4687                    $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4688          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          redo B;
           
         $self->{content_model_flag} = 'CDATA';  
         delete $self->{escape}; # MUST  
           
         !!!next-token;  
         return;  
4689        } elsif ($token->{tag_name} eq 'table') {        } elsif ($token->{tag_name} eq 'table') {
4690          ## has a p element in scope          ## has a p element in scope
4691          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4692            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4693              !!!back-token;              !!!back-token;
4694              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => 'end tag', tag_name => 'p'};
4695              return;              redo B;
4696            } elsif ({            } elsif ({
4697                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4698                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2846  sub _tree_construction_main ($) { Line 4703  sub _tree_construction_main ($) {
4703                        
4704          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4705                        
4706          $self->{insertion_mode} = 'in table';          $self->{insertion_mode} = IN_TABLE_IM;
4707                        
4708          !!!next-token;          !!!next-token;
4709          return;          redo B;
4710        } elsif ({        } elsif ({
4711                  area => 1, basefont => 1, bgsound => 1, br => 1,                  area => 1, basefont => 1, bgsound => 1, br => 1,
4712                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
# Line 2859  sub _tree_construction_main ($) { Line 4716  sub _tree_construction_main ($) {
4716            !!!parse-error (type => 'image');            !!!parse-error (type => 'image');
4717            $token->{tag_name} = 'img';            $token->{tag_name} = 'img';
4718          }          }
4719            
4720            ## NOTE: There is an "as if <br>" code clone.
4721          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4722                    
4723          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4724          pop @{$self->{open_elements}};          pop @{$self->{open_elements}};
4725                    
4726          !!!next-token;          !!!next-token;
4727          return;          redo B;
4728        } elsif ($token->{tag_name} eq 'hr') {        } elsif ($token->{tag_name} eq 'hr') {
4729          ## has a p element in scope          ## has a p element in scope
4730          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4731            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4732              !!!back-token;              !!!back-token;
4733              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => 'end tag', tag_name => 'p'};
4734              return;              redo B;
4735            } elsif ({            } elsif ({
4736                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
4737                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
# Line 2886  sub _tree_construction_main ($) { Line 4744  sub _tree_construction_main ($) {
4744          pop @{$self->{open_elements}};          pop @{$self->{open_elements}};
4745                        
4746          !!!next-token;          !!!next-token;
4747          return;          redo B;
4748        } elsif ($token->{tag_name} eq 'input') {        } elsif ($token->{tag_name} eq 'input') {
4749          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4750                    
# Line 2895  sub _tree_construction_main ($) { Line 4753  sub _tree_construction_main ($) {
4753          pop @{$self->{open_elements}};          pop @{$self->{open_elements}};
4754                    
4755          !!!next-token;          !!!next-token;
4756          return;          redo B;
4757        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
4758          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex');
4759                    
4760          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
4761            ## Ignore the token            ## Ignore the token
4762            !!!next-token;            !!!next-token;
4763            return;            redo B;
4764          } else {          } else {
4765            my $at = $token->{attributes};            my $at = $token->{attributes};
4766            my $form_attrs;            my $form_attrs;
# Line 2934  sub _tree_construction_main ($) { Line 4792  sub _tree_construction_main ($) {
4792                          {type => 'end tag', tag_name => 'form'};                          {type => 'end tag', tag_name => 'form'};
4793            $token = shift @tokens;            $token = shift @tokens;
4794            !!!back-token (@tokens);            !!!back-token (@tokens);
4795            return;            redo B;
4796          }          }
4797        } elsif ({        } elsif ($token->{tag_name} eq 'textarea') {
                 textarea => 1,  
                 iframe => 1,  
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
4798          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
4799          my $el;          my $el;
4800          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $token->{tag_name}, $token->{attributes});
4801                    
4802          if ($token->{tag_name} eq 'textarea') {          ## TODO: $self->{form_element} if defined
4803            ## TODO: $self->{form_element} if defined          $self->{content_model} = RCDATA_CONTENT_MODEL;
           $self->{content_model_flag} = 'RCDATA';  
         } else {  
           $self->{content_model_flag} = 'CDATA';  
         }  
4804          delete $self->{escape}; # MUST          delete $self->{escape}; # MUST
4805                    
4806          $insert->($el);          $insert->($el);
4807                    
4808          my $text = '';          my $text = '';
4809          if ($token->{tag_name} eq 'textarea') {          !!!next-token;
4810            !!!next-token;          if ($token->{type} eq 'character') {
4811            if ($token->{type} eq 'character') {            $token->{data} =~ s/^\x0A//;
4812              $token->{data} =~ s/^\x0A//;            unless (length $token->{data}) {
4813              unless (length $token->{data}) {              !!!next-token;
               !!!next-token;  
             }  
4814            }            }
         } else {  
           !!!next-token;  
4815          }          }
4816          while ($token->{type} eq 'character') {          while ($token->{type} eq 'character') {
4817            $text .= $token->{data};            $text .= $token->{data};
# Line 2977  sub _tree_construction_main ($) { Line 4821  sub _tree_construction_main ($) {
4821            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
4822          }          }
4823                    
4824          $self->{content_model_flag} = 'PCDATA';          $self->{content_model} = PCDATA_CONTENT_MODEL;
4825                    
4826          if ($token->{type} eq 'end tag' and          if ($token->{type} eq 'end tag' and
4827              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
4828            ## Ignore the token            ## Ignore the token
4829          } else {          } else {
4830            if ($token->{tag_name} eq 'textarea') {            !!!parse-error (type => 'in RCDATA:#'.$token->{type});
             !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
           } else {  
             !!!parse-error (type => 'in CDATA:#'.$token->{type});  
           }  
           ## ISSUE: And ignore?  
4831          }          }
4832          !!!next-token;          !!!next-token;
4833          return;          redo B;
4834          } elsif ({
4835                    iframe => 1,
4836                    noembed => 1,
4837                    noframes => 1,
4838                    noscript => 0, ## TODO: 1 if scripting is enabled
4839                   }->{$token->{tag_name}}) {
4840            ## NOTE: There are two "as if in body" code clones.
4841            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
4842            redo B;
4843        } elsif ($token->{tag_name} eq 'select') {        } elsif ($token->{tag_name} eq 'select') {
4844          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
4845                    
4846          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4847                    
4848          $self->{insertion_mode} = 'in select';          $self->{insertion_mode} = IN_SELECT_IM;
4849          !!!next-token;          !!!next-token;
4850          return;          redo B;
4851        } elsif ({        } elsif ({
4852                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
4853                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 3009  sub _tree_construction_main ($) { Line 4857  sub _tree_construction_main ($) {
4857          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body:'.$token->{tag_name});
4858          ## Ignore the token          ## Ignore the token
4859          !!!next-token;          !!!next-token;
4860          return;          redo B;
4861                    
4862          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
4863        } else {        } else {
# Line 3018  sub _tree_construction_main ($) { Line 4866  sub _tree_construction_main ($) {
4866          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4867                    
4868          !!!next-token;          !!!next-token;
4869          return;          redo B;
4870        }        }
4871      } elsif ($token->{type} eq 'end tag') {      } elsif ($token->{type} eq 'end tag') {
4872        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
# Line 3028  sub _tree_construction_main ($) { Line 4876  sub _tree_construction_main ($) {
4876              unless ({              unless ({
4877                         dd => 1, dt => 1, li => 1, p => 1, td => 1,                         dd => 1, dt => 1, li => 1, p => 1, td => 1,
4878                         th => 1, tr => 1, body => 1, html => 1,                         th => 1, tr => 1, body => 1, html => 1,
4879                         tbody => 1, tfoot => 1, thead => 1,
4880                      }->{$_->[1]}) {                      }->{$_->[1]}) {
4881                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!parse-error (type => 'not closed:'.$_->[1]);
4882              }              }
4883            }            }
4884    
4885            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = AFTER_BODY_IM;
4886            !!!next-token;            !!!next-token;
4887            return;            redo B;
4888          } else {          } else {
4889            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4890            ## Ignore the token            ## Ignore the token
4891            !!!next-token;            !!!next-token;
4892            return;            redo B;
4893          }          }
4894        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
4895          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {
# Line 3048  sub _tree_construction_main ($) { Line 4897  sub _tree_construction_main ($) {
4897            if ($self->{open_elements}->[-1]->[1] ne 'body') {            if ($self->{open_elements}->[-1]->[1] ne 'body') {
4898              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
4899            }            }
4900            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = AFTER_BODY_IM;
4901            ## reprocess            ## reprocess
4902            return;            redo B;
4903          } else {          } else {
4904            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4905            ## Ignore the token            ## Ignore the token
4906            !!!next-token;            !!!next-token;
4907            return;            redo B;
4908          }          }
4909        } elsif ({        } elsif ({
4910                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
# Line 3077  sub _tree_construction_main ($) { Line 4926  sub _tree_construction_main ($) {
4926                   li => ($token->{tag_name} ne 'li'),                   li => ($token->{tag_name} ne 'li'),
4927                   p => ($token->{tag_name} ne 'p'),                   p => ($token->{tag_name} ne 'p'),
4928                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
4929                     tbody => 1, tfoot=> 1, thead => 1,
4930                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4931                !!!back-token;                !!!back-token;
4932                $token = {type => 'end tag',                $token = {type => 'end tag',
4933                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4934                return;                redo B;
4935              }              }
4936              $i = $_;              $i = $_;
4937              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE unless $token->{tag_name} eq 'p';
# Line 3094  sub _tree_construction_main ($) { Line 4944  sub _tree_construction_main ($) {
4944          } # INSCOPE          } # INSCOPE
4945                    
4946          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
4947            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            if (defined $i) {
4948                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4949              } else {
4950                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4951              }
4952          }          }
4953                    
4954          splice @{$self->{open_elements}}, $i if defined $i;          if (defined $i) {
4955              splice @{$self->{open_elements}}, $i;
4956            } elsif ($token->{tag_name} eq 'p') {
4957              ## As if <p>, then reprocess the current token
4958              my $el;
4959              !!!create-element ($el, 'p');
4960              $insert->($el);
4961            }
4962          $clear_up_to_marker->()          $clear_up_to_marker->()
4963            if {            if {
4964              button => 1, marquee => 1, object => 1,              button => 1, marquee => 1, object => 1,
4965            }->{$token->{tag_name}};            }->{$token->{tag_name}};
4966          !!!next-token;          !!!next-token;
4967          return;          redo B;
4968        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
4969          ## has an element in scope          ## has an element in scope
4970          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3113  sub _tree_construction_main ($) { Line 4974  sub _tree_construction_main ($) {
4974              if ({              if ({
4975                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
4976                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
4977                     tbody => 1, tfoot=> 1, thead => 1,
4978                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4979                !!!back-token;                !!!back-token;
4980                $token = {type => 'end tag',                $token = {type => 'end tag',
4981                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
4982                return;                redo B;
4983              }              }
4984              last INSCOPE;              last INSCOPE;
4985            } elsif ({            } elsif ({
# Line 3136  sub _tree_construction_main ($) { Line 4998  sub _tree_construction_main ($) {
4998    
4999          undef $self->{form_element};          undef $self->{form_element};
5000          !!!next-token;          !!!next-token;
5001          return;          redo B;
5002        } elsif ({        } elsif ({
5003                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5004                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 3151  sub _tree_construction_main ($) { Line 5013  sub _tree_construction_main ($) {
5013              if ({              if ({
5014                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
5015                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
5016                     tbody => 1, tfoot=> 1, thead => 1,
5017                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5018                !!!back-token;                !!!back-token;
5019                $token = {type => 'end tag',                $token = {type => 'end tag',
5020                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5021                return;                redo B;
5022              }              }
5023              $i = $_;              $i = $_;
5024              last INSCOPE;              last INSCOPE;
# Line 3173  sub _tree_construction_main ($) { Line 5036  sub _tree_construction_main ($) {
5036                    
5037          splice @{$self->{open_elements}}, $i if defined $i;          splice @{$self->{open_elements}}, $i if defined $i;
5038          !!!next-token;          !!!next-token;
5039          return;          redo B;
5040        } elsif ({        } elsif ({
5041                  a => 1,                  a => 1,
5042                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 3181  sub _tree_construction_main ($) { Line 5044  sub _tree_construction_main ($) {
5044                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
5045                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5046          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token->{tag_name});
5047  ## TODO: <http://html5.org/tools/web-apps-tracker?from=883&to=884>          redo B;
5048          return;        } elsif ($token->{tag_name} eq 'br') {
5049            !!!parse-error (type => 'unmatched end tag:br');
5050    
5051            ## As if <br>
5052            $reconstruct_active_formatting_elements->($insert_to_current);
5053            
5054            my $el;
5055            !!!create-element ($el, 'br');
5056            $insert->($el);
5057            
5058            ## Ignore the token.
5059            !!!next-token;
5060            redo B;
5061        } elsif ({        } elsif ({
5062                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
5063                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
5064                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
5065                  thead => 1, tr => 1,                  thead => 1, tr => 1,
5066                  area => 1, basefont => 1, bgsound => 1, br => 1,                  area => 1, basefont => 1, bgsound => 1,
5067                  embed => 1, hr => 1, iframe => 1, image => 1,                  embed => 1, hr => 1, iframe => 1, image => 1,
5068                  img => 1, input => 1, isindex => 1, noembed => 1,                  img => 1, input => 1, isindex => 1, noembed => 1,
5069                  noframes => 1, param => 1, select => 1, spacer => 1,                  noframes => 1, param => 1, select => 1, spacer => 1,
# Line 3198  sub _tree_construction_main ($) { Line 5073  sub _tree_construction_main ($) {
5073          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5074          ## Ignore the token          ## Ignore the token
5075          !!!next-token;          !!!next-token;
5076          return;          redo B;
5077                    
5078          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
5079                    
# Line 3215  sub _tree_construction_main ($) { Line 5090  sub _tree_construction_main ($) {
5090              if ({              if ({
5091                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
5092                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
5093                     tbody => 1, tfoot=> 1, thead => 1,
5094                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5095                !!!back-token;                !!!back-token;
5096                $token = {type => 'end tag',                $token = {type => 'end tag',
5097                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5098                return;                redo B;
5099              }              }
5100                    
5101              ## Step 2              ## Step 2
# Line 3238  sub _tree_construction_main ($) { Line 5114  sub _tree_construction_main ($) {
5114                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
5115                  ($special_category->{$node->[1]} or                  ($special_category->{$node->[1]} or
5116                   $scoping_category->{$node->[1]})) {                   $scoping_category->{$node->[1]})) {
5117                !!!parse-error (type => 'not closed:'.$node->[1]);                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5118                ## Ignore the token                ## Ignore the token
5119                !!!next-token;                !!!next-token;
5120                last S2;                last S2;
# Line 3252  sub _tree_construction_main ($) { Line 5128  sub _tree_construction_main ($) {
5128            ## Step 5;            ## Step 5;
5129            redo S2;            redo S2;
5130          } # S2          } # S2
5131          return;          redo B;
       }  
     }  
   }; # $in_body  
   
   B: {  
     if ($phase eq 'main') {  
       if ($token->{type} eq 'DOCTYPE') {  
         !!!parse-error (type => 'in html:#DOCTYPE');  
         ## Ignore the token  
         ## Stay in the phase  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'start tag' and  
                $token->{tag_name} eq 'html') {  
         ## TODO: unless it is the first start tag token, parse-error  
         my $top_el = $self->{open_elements}->[0]->[0];  
         for my $attr_name (keys %{$token->{attributes}}) {  
           unless ($top_el->has_attribute_ns (undef, $attr_name)) {  
             $top_el->set_attribute_ns  
               (undef, [undef, $attr_name],  
                $token->{attributes}->{$attr_name}->{value});  
           }  
         }  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'end-of-file') {  
         ## Generate implied end tags  
         if ({  
              dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,  
             }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!back-token;  
           $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};  
           redo B;  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
   
         ## Stop parsing  
         last B;  
   
         ## ISSUE: There is an issue in the spec.  
       } else {  
         if ($self->{insertion_mode} eq 'before head') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
             ## As if <head>  
             !!!create-element ($self->{head_element}, 'head');  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
             ## reprocess  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};  
             !!!create-element ($self->{head_element}, 'head', $attr);  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
             if ($token->{tag_name} eq 'head') {  
               !!!next-token;  
             #} elsif ({  
             #          base => 1, link => 1, meta => 1,  
             #          script => 1, style => 1, title => 1,  
             #         }->{$token->{tag_name}}) {  
             #  ## reprocess  
             } else {  
               ## reprocess  
             }  
             redo B;  
           } elsif ($token->{type} eq 'end tag') {  
             if ({head => 1, body => 1, html => 1}->{$token->{tag_name}}) {  
               ## As if <head>  
               !!!create-element ($self->{head_element}, 'head');  
               $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
               push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               $self->{insertion_mode} = 'in head';  
               ## reprocess  
               redo B;  
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token ## ISSUE: An issue in the spec.  
               !!!next-token;  
               redo B;  
             }  
           } else {  
             die "$0: $token->{type}: Unknown type";  
           }  
         } elsif ($self->{insertion_mode} eq 'in head') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'title') {  
               ## NOTE: There is an "as if in head" code clone  
               my $title_el;  
               !!!create-element ($title_el, 'title', $token->{attributes});  
               (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
                 ->append_child ($title_el);  
               $self->{content_model_flag} = 'RCDATA';  
               delete $self->{escape}; # MUST  
   
               my $text = '';  
               !!!next-token;  
               while ($token->{type} eq 'character') {  
                 $text .= $token->{data};  
                 !!!next-token;  
               }  
               if (length $text) {  
                 $title_el->manakai_append_text ($text);  
               }  
                 
               $self->{content_model_flag} = 'PCDATA';  
                 
               if ($token->{type} eq 'end tag' and  
                   $token->{tag_name} eq 'title') {  
                 ## Ignore the token  
               } else {  
                 !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
                 ## ISSUE: And ignore?  
               }  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'style') {  
               $style_start_tag->();  
               redo B;  
             } elsif ($token->{tag_name} eq 'script') {  
               $script_start_tag->();  
               redo B;  
             } elsif ({base => 1, link => 1, meta => 1}->{$token->{tag_name}}) {  
               ## NOTE: There are "as if in head" code clones  
               my $el;  
               !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
               (defined $self->{head_element} ? $self->{head_element} : $self->{open_elements}->[-1]->[0])  
                 ->append_child ($el);  
   
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'head') {  
               !!!parse-error (type => 'in head:head');  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'head') {  
               if ($self->{open_elements}->[-1]->[1] eq 'head') {  
                 pop @{$self->{open_elements}};  
               } else {  
                 !!!parse-error (type => 'unmatched end tag:head');  
               }  
               $self->{insertion_mode} = 'after head';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'body' or  
                      $token->{tag_name} eq 'html') {  
               #  
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             }  
           } else {  
             #  
           }  
   
           if ($self->{open_elements}->[-1]->[1] eq 'head') {  
             ## As if </head>  
             pop @{$self->{open_elements}};  
           }  
           $self->{insertion_mode} = 'after head';  
           ## reprocess  
           redo B;  
   
           ## ISSUE: An issue in the spec.  
         } elsif ($self->{insertion_mode} eq 'after head') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'body') {  
               !!!insert-element ('body', $token->{attributes});  
               $self->{insertion_mode} = 'in body';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'frameset') {  
               !!!insert-element ('frameset', $token->{attributes});  
               $self->{insertion_mode} = 'in frameset';  
               !!!next-token;  
               redo B;  
             } elsif ({  
                       base => 1, link => 1, meta => 1,  
                       script => 1, style => 1, title => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'after head:'.$token->{tag_name});  
               $self->{insertion_mode} = 'in head';  
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           ## As if <body>  
           !!!insert-element ('body');  
           $self->{insertion_mode} = 'in body';  
           ## reprocess  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in body') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: There is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } else {  
             $in_body->($insert_to_current);  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There are "character in table" code clones.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1,  
                  colgroup => 1,  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$token->{tag_name}}) {  
               ## Clear back to table context  
               while ($self->{open_elements}->[-1]->[1] ne 'table' and  
                      $self->{open_elements}->[-1]->[1] ne 'html') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               push @$active_formatting_elements, ['#marker', '']  
                 if $token->{tag_name} eq 'caption';  
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = {  
                                  caption => 'in caption',  
                                  colgroup => 'in column group',  
                                  tbody => 'in table body',  
                                  tfoot => 'in table body',  
                                  thead => 'in table body',  
                                 }->{$token->{tag_name}};  
               !!!next-token;  
               redo B;  
             } elsif ({  
                       col => 1,  
                       td => 1, th => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## Clear back to table context  
               while ($self->{open_elements}->[-1]->[1] ne 'table' and  
                      $self->{open_elements}->[-1]->[1] ne 'html') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');  
               $self->{insertion_mode} = $token->{tag_name} eq 'col'  
                 ? 'in column group' : 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: There are code clones for this "table in table"  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
   
               ## As if </table>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'table') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:table');  
                 ## Ignore tokens </table><table>  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'table') {  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in caption') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: This is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1, col => 1, colgroup => 1, tbody => 1,  
                  td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,  
                 }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'not closed:caption');  
   
               ## As if </caption>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'caption') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:caption');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <?>  
                 $token = {type => 'end tag', tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'caption') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'caption') {  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'caption') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               !!!parse-error (type => 'not closed:caption');  
   
               ## As if </caption>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'caption') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:caption');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # </table>  
                 $token = {type => 'end tag', tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'caption') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in table';  
   
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, col => 1, colgroup => 1,  
                       html => 1, tbody => 1, td => 1, tfoot => 1,  
                       th => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
                 
           $in_body->($insert_to_current);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in column group') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'col') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'colgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html') {  
                 !!!parse-error (type => 'unmatched end tag:colgroup');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               } else {  
                 pop @{$self->{open_elements}}; # colgroup  
                 $self->{insertion_mode} = 'in table';  
                 !!!next-token;  
                 redo B;              
               }  
             } elsif ($token->{tag_name} eq 'col') {  
               !!!parse-error (type => 'unmatched end tag:col');  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           ## As if </colgroup>  
           if ($self->{open_elements}->[-1]->[1] eq 'html') {  
             !!!parse-error (type => 'unmatched end tag:colgroup');  
             ## Ignore the token  
             !!!next-token;  
             redo B;  
           } else {  
             pop @{$self->{open_elements}}; # colgroup  
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table body') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
   
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  tr => 1,  
                  th => 1, td => 1,  
                 }->{$token->{tag_name}}) {  
               unless ($token->{tag_name} eq 'tr') {  
                 !!!parse-error (type => 'missing start tag:tr');  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
               $self->{insertion_mode} = 'in row';  
               if ($token->{tag_name} eq 'tr') {  
                 !!!insert-element ($token->{tag_name}, $token->{attributes});  
                 !!!next-token;  
               } else {  
                 !!!insert-element ('tr');  
                 ## reprocess  
               }  
               redo B;  
             } elsif ({  
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ({  
                      tbody => 1, thead => 1, tfoot => 1,  
                     }->{$node->[1]}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               ## reprocess  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: This is a code clone of "table in table"  
               !!!parse-error (type => 'not closed:table');  
   
               ## As if </table>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'table') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:table');  
                 ## Ignore tokens </table><table>  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ({  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ({  
                      tbody => 1, thead => 1, tfoot => 1,  
                     }->{$node->[1]}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table body context  
               while (not {  
                 tbody => 1, tfoot => 1, thead => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               ## As if <{current node}>  
               ## have an element in table scope  
               ## true by definition  
   
               ## Clear back to table body context  
               ## nop by definition  
   
               pop @{$self->{open_elements}};  
               $self->{insertion_mode} = 'in table';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1, colgroup => 1,  
                       html => 1, td => 1, th => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           ## As if in table  
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in row') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a "character in table" code clone.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             !!!parse-error (type => 'in table:#character');  
   
             ## As if in body, but insert into foster parent element  
             ## ISSUE: Spec says that "whenever a node would be inserted  
             ## into the current node" while characters might not be  
             ## result in a new Text node.  
             $reconstruct_active_formatting_elements->($insert_to_foster);  
               
             if ({  
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               # MUST  
               my $foster_parent_element;  
               my $next_sibling;  
               my $prev_sibling;  
               OE: for (reverse 0..$#{$self->{open_elements}}) {  
                 if ($self->{open_elements}->[$_]->[1] eq 'table') {  
                   my $parent = $self->{open_elements}->[$_]->[0]->parent_node;  
                   if (defined $parent and $parent->node_type == 1) {  
                     $foster_parent_element = $parent;  
                     $next_sibling = $self->{open_elements}->[$_]->[0];  
                     $prev_sibling = $next_sibling->previous_sibling;  
                   } else {  
                     $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];  
                     $prev_sibling = $foster_parent_element->last_child;  
                   }  
                   last OE;  
                 }  
               } # OE  
               $foster_parent_element = $self->{open_elements}->[0]->[0] and  
               $prev_sibling = $foster_parent_element->last_child  
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
               } else {  
                 $foster_parent_element->insert_before  
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
               }  
             } else {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             }  
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## Copied from 'in table'  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'th' or  
                 $token->{tag_name} eq 'td') {  
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
                 
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = 'in cell';  
   
               push @$active_formatting_elements, ['#marker', ''];  
                 
               !!!next-token;  
               redo B;  
             } elsif ({  
                       caption => 1, col => 1, colgroup => 1,  
                       tbody => 1, tfoot => 1, thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: This is a code clone of "table in table"  
               !!!parse-error (type => 'not closed:table');  
   
               ## As if </table>  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'table') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:table');  
                 ## Ignore tokens </table><table>  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'tr') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{type});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       tbody => 1, tfoot => 1, thead => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## As if </tr>  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'tr') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:tr');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Clear back to table row context  
               while (not {  
                 tr => 1, html => 1,  
               }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                 pop @{$self->{open_elements}};  
               }  
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1,  
                       colgroup => 1, html => 1, td => 1, th => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           ## As if in table  
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
           $in_body->($insert_to_foster);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in cell') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: This is a code clone of "character in body".  
             $reconstruct_active_formatting_elements->($insert_to_current);  
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             ## NOTE: This is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1, col => 1, colgroup => 1,  
                  tbody => 1, td => 1, tfoot => 1, th => 1,  
                  thead => 1, tr => 1,  
                 }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $tn) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Close the cell  
               !!!back-token; # <?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => ($token->{tag_name} eq 'th'),  
                    th => ($token->{tag_name} eq 'td'),  
                    tr => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
   
               $self->{insertion_mode} = 'in row';  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       body => 1, caption => 1, col => 1,  
                       colgroup => 1, html => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } elsif ({  
                       table => 1, tbody => 1, tfoot => 1,  
                       thead => 1, tr => 1,  
                      }->{$token->{tag_name}}) {  
               ## have an element in table scope  
               my $i;  
               my $tn;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
                   $tn = $node->[1];  
                   ## NOTE: There is exactly one |td| or |th| element  
                   ## in scope in the stack of open elements by definition.  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           $in_body->($insert_to_current);  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in select') {  
           if ($token->{type} eq 'character') {  
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'option') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 ## As if </option>  
                 pop @{$self->{open_elements}};  
               }  
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'optgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 ## As if </option>  
                 pop @{$self->{open_elements}};  
               }  
   
               if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {  
                 ## As if </optgroup>  
                 pop @{$self->{open_elements}};  
               }  
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'select') {  
               !!!parse-error (type => 'not closed:select');  
               ## As if </select> instead  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:select');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'optgroup') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option' and  
                   $self->{open_elements}->[-2]->[1] eq 'optgroup') {  
                 ## As if </option>  
                 splice @{$self->{open_elements}}, -2;  
               } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {  
                 pop @{$self->{open_elements}};  
               } else {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
               }  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'option') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 pop @{$self->{open_elements}};  
               } else {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
               }  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'select') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               !!!next-token;  
               redo B;  
             } elsif ({  
                       caption => 1, table => 1, tbody => 1,  
                       tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,  
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## As if </select>  
               ## have an element in table scope  
               undef $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq 'select') {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:select');  
                 ## Ignore the </select> token  
                 !!!next-token; ## TODO: ok?  
                 redo B;  
               }  
                 
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in select:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'after body') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               ## As if in body  
               $reconstruct_active_formatting_elements->($insert_to_current);  
                 
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
             !!!parse-error (type => 'after body:#'.$token->{type});  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[0]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             !!!parse-error (type => 'after body:'.$token->{tag_name});  
             #  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'html') {  
               if (defined $self->{inner_html_node}) {  
                 !!!parse-error (type => 'unmatched end tag:html');  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               } else {  
                 $phase = 'trailing end';  
                 !!!next-token;  
                 redo B;  
               }  
             } else {  
               !!!parse-error (type => 'after body:/'.$token->{tag_name});  
             }  
           } else {  
             !!!parse-error (type => 'after body:#'.$token->{type});  
           }  
   
           $self->{insertion_mode} = 'in body';  
           ## reprocess  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'in frameset') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'frameset') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'frame') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'noframes') {  
               $in_body->($insert_to_current);  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'frameset') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html' and  
                   @{$self->{open_elements}} == 1) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
               } else {  
                 pop @{$self->{open_elements}};  
                 !!!next-token;  
               }  
                 
               ## if not inner_html and  
               if ($self->{open_elements}->[-1]->[1] ne 'frameset') {  
                 $self->{insertion_mode} = 'after frameset';  
               }  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           if (defined $token->{tag_name}) {  
             !!!parse-error (type => 'in frameset:'.$token->{tag_name});  
           } else {  
             !!!parse-error (type => 'in frameset:#'.$token->{type});  
           }  
           ## Ignore the token  
           !!!next-token;  
           redo B;  
         } elsif ($self->{insertion_mode} eq 'after frameset') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
   
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
   
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'noframes') {  
               $in_body->($insert_to_current);  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'html') {  
               $phase = 'trailing end';  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
             
           if (defined $token->{tag_name}) {  
             !!!parse-error (type => 'after frameset:'.$token->{tag_name});  
           } else {  
             !!!parse-error (type => 'after frameset:#'.$token->{type});  
           }  
           ## Ignore the token  
           !!!next-token;  
           redo B;  
   
           ## ISSUE: An issue in spec there  
         } else {  
           die "$0: $self->{insertion_mode}: Unknown insertion mode";  
         }  
       }  
     } elsif ($phase eq 'trailing end') {  
       ## states in the main stage is preserved yet # MUST  
         
       if ($token->{type} eq 'DOCTYPE') {  
         !!!parse-error (type => 'after html:#DOCTYPE');  
         ## Ignore the token  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'comment') {  
         my $comment = $self->{document}->create_comment ($token->{data});  
         $self->{document}->append_child ($comment);  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'character') {  
         if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
           my $data = $1;  
           ## As if in the main phase.  
           ## NOTE: The insertion mode in the main phase  
           ## just before the phase has been changed to the trailing  
           ## end phase is either "after body" or "after frameset".  
           $reconstruct_active_formatting_elements->($insert_to_current)  
             if $phase eq 'main';  
             
           $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);  
             
           unless (length $token->{data}) {  
             !!!next-token;  
             redo B;  
           }  
         }  
   
         !!!parse-error (type => 'after html:#character');  
         $phase = 'main';  
         ## reprocess  
         redo B;  
       } elsif ($token->{type} eq 'start tag' or  
                $token->{type} eq 'end tag') {  
         !!!parse-error (type => 'after html:'.$token->{tag_name});  
         $phase = 'main';  
         ## reprocess  
         redo B;  
       } elsif ($token->{type} eq 'end-of-file') {  
         ## Stop parsing  
         last B;  
       } else {  
         die "$0: $token->{type}: Unknown token";  
5132        }        }
5133      }      }
5134        redo B;
5135    } # B    } # B
5136    
5137      ## NOTE: The "trailing end" phase in HTML5 is split into
5138      ## two insertion modes: "after html body" and "after html frameset".
5139      ## NOTE: States in the main stage is preserved while
5140      ## the parser stays in the trailing end phase. # MUST
5141    
5142    ## Stop parsing # MUST    ## Stop parsing # MUST
5143        
5144    ## TODO: script stuffs    ## TODO: script stuffs
# Line 5206  sub set_inner_html ($$$) { Line 5223  sub set_inner_html ($$$) {
5223    
5224      ## Step 2      ## Step 2
5225      my $node_ln = $node->local_name;      my $node_ln = $node->local_name;
5226      $p->{content_model_flag} = {      $p->{content_model} = {
5227        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
5228        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
5229        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
5230        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
5231        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
5232        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
5233        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
5234        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
5235        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
5236        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
5237      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
5238         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
5239            unless defined $p->{content_model};
5240            ## ISSUE: What is "the name of the element"? local name?
5241    
5242      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
5243    
# Line 5318  sub get_inner_html ($$$) { Line 5337  sub get_inner_html ($$$) {
5337            
5338      my $nt = $child->node_type;      my $nt = $child->node_type;
5339      if ($nt == 1) { # Element      if ($nt == 1) { # Element
5340        my $tag_name = lc $child->tag_name; ## ISSUE: Definition of "lowercase"        my $tag_name = $child->tag_name; ## TODO: manakai_tag_name
5341        $s .= '<' . $tag_name;        $s .= '<' . $tag_name;
5342          ## NOTE: Non-HTML case:
5343        ## ISSUE: Non-html elements        ## <http://permalink.gmane.org/gmane.org.w3c.whatwg.discuss/11191>
5344    
5345        my @attrs = @{$child->attributes}; # sort order MUST be stable        my @attrs = @{$child->attributes}; # sort order MUST be stable
5346        for my $attr (@attrs) { # order is implementation dependent        for my $attr (@attrs) { # order is implementation dependent
5347          my $attr_name = lc $attr->name; ## ISSUE: Definition of "lowercase"          my $attr_name = $attr->name; ## TODO: manakai_name
5348          $s .= ' ' . $attr_name . '="';          $s .= ' ' . $attr_name . '="';
5349          my $attr_value = $attr->value;          my $attr_value = $attr->value;
5350          ## escape          ## escape
# Line 5349  sub get_inner_html ($$$) { Line 5368  sub get_inner_html ($$$) {
5368        if (not $in_cdata and {        if (not $in_cdata and {
5369          style => 1, script => 1, xmp => 1, iframe => 1,          style => 1, script => 1, xmp => 1, iframe => 1,
5370          noembed => 1, noframes => 1, noscript => 1,          noembed => 1, noframes => 1, noscript => 1,
5371            plaintext => 1,
5372        }->{$tag_name}) {        }->{$tag_name}) {
5373          unshift @node, 'cdata-out';          unshift @node, 'cdata-out';
5374          $in_cdata = 1;          $in_cdata = 1;

Legend:
Removed from v.1.23  
changed lines
  Added in v.1.54

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24