/[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.72 by wakaba, Sun Mar 2 14:32:26 2008 UTC revision 1.94 by wakaba, Sat Mar 8 04:13:10 2008 UTC
# Line 179  sub parse_string ($$$;$) { Line 179  sub parse_string ($$$;$) {
179    my $i = 0;    my $i = 0;
180    my $line = 1;    my $line = 1;
181    my $column = 0;    my $column = 0;
182    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
183      my $self = shift;      my $self = shift;
184    
185      pop @{$self->{prev_input_character}};      pop @{$self->{prev_char}};
186      unshift @{$self->{prev_input_character}}, $self->{next_input_character};      unshift @{$self->{prev_char}}, $self->{next_char};
187    
188      $self->{next_input_character} = -1 and return if $i >= length $$s;      $self->{next_char} = -1 and return if $i >= length $$s;
189      $self->{next_input_character} = ord substr $$s, $i++, 1;      $self->{next_char} = ord substr $$s, $i++, 1;
190      $column++;      $column++;
191            
192      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
193        $line++;        $line++;
194        $column = 0;        $column = 0;
195      } elsif ($self->{next_input_character} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
196        $i++ if substr ($$s, $i, 1) eq "\x0A";        $i++ if substr ($$s, $i, 1) eq "\x0A";
197        $self->{next_input_character} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
198        $line++;        $line++;
199        $column = 0;        $column = 0;
200      } elsif ($self->{next_input_character} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
201        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
202      } elsif ($self->{next_input_character} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
203        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
204        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
205      }      }
206    };    };
207    $self->{prev_input_character} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
208    $self->{next_input_character} = -1;    $self->{next_char} = -1;
209    
210    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
211      my (%opt) = @_;      my (%opt) = @_;
# Line 226  sub parse_string ($$$;$) { Line 226  sub parse_string ($$$;$) {
226  sub new ($) {  sub new ($) {
227    my $class = shift;    my $class = shift;
228    my $self = bless {}, $class;    my $self = bless {}, $class;
229    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
230      $self->{next_input_character} = -1;      $self->{next_char} = -1;
231    };    };
232    $self->{parse_error} = sub {    $self->{parse_error} = sub {
233      #      #
# Line 304  sub ROW_IMS ()        { 0b10000000 } Line 304  sub ROW_IMS ()        { 0b10000000 }
304  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
305  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
306    
307    ## NOTE: "initial" and "before html" insertion modes have no constants.
308    
309    ## NOTE: "after after body" insertion mode.
310  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
311    
312    ## NOTE: "after after frameset" insertion mode.
313  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
314    
315  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
316  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
317  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
# Line 333  sub _initialize_tokenizer ($) { Line 339  sub _initialize_tokenizer ($) {
339    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
340    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
341    $self->{char} = [];    $self->{char} = [];
342    # $self->{next_input_character}    # $self->{next_char}
343    !!!next-input-character;    !!!next-input-character;
344    $self->{token} = [];    $self->{token} = [];
345    # $self->{escape}    # $self->{escape}
# Line 346  sub _initialize_tokenizer ($) { Line 352  sub _initialize_tokenizer ($) {
352  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
353  ##   ->{public_identifier} (DOCTYPE_TOKEN)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
354  ##   ->{system_identifier} (DOCTYPE_TOKEN)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
355  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
356  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
357  ##        ->{name}  ##        ->{name}
358  ##        ->{value}  ##        ->{value}
# Line 384  sub _get_next_token ($) { Line 390  sub _get_next_token ($) {
390    
391    A: {    A: {
392      if ($self->{state} == DATA_STATE) {      if ($self->{state} == DATA_STATE) {
393        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
394          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
395              not $self->{escape}) {              not $self->{escape}) {
396              !!!cp (1);
397            $self->{state} = ENTITY_DATA_STATE;            $self->{state} = ENTITY_DATA_STATE;
398            !!!next-input-character;            !!!next-input-character;
399            redo A;            redo A;
400          } else {          } else {
401              !!!cp (2);
402            #            #
403          }          }
404        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
405          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
406            unless ($self->{escape}) {            unless ($self->{escape}) {
407              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_char}->[0] == 0x002D and # -
408                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_char}->[1] == 0x0021 and # !
409                  $self->{prev_input_character}->[2] == 0x003C) { # <                  $self->{prev_char}->[2] == 0x003C) { # <
410                  !!!cp (3);
411                $self->{escape} = 1;                $self->{escape} = 1;
412                } else {
413                  !!!cp (4);
414              }              }
415              } else {
416                !!!cp (5);
417            }            }
418          }          }
419                    
420          #          #
421        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x003C) { # <
422          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
423              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
424               not $self->{escape})) {               not $self->{escape})) {
425              !!!cp (6);
426            $self->{state} = TAG_OPEN_STATE;            $self->{state} = TAG_OPEN_STATE;
427            !!!next-input-character;            !!!next-input-character;
428            redo A;            redo A;
429          } else {          } else {
430              !!!cp (7);
431            #            #
432          }          }
433        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
434          if ($self->{escape} and          if ($self->{escape} and
435              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
436            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_char}->[0] == 0x002D and # -
437                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_char}->[1] == 0x002D) { # -
438                !!!cp (8);
439              delete $self->{escape};              delete $self->{escape};
440              } else {
441                !!!cp (9);
442            }            }
443            } else {
444              !!!cp (10);
445          }          }
446                    
447          #          #
448        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
449            !!!cp (11);
450          !!!emit ({type => END_OF_FILE_TOKEN});          !!!emit ({type => END_OF_FILE_TOKEN});
451          last A; ## TODO: ok?          last A; ## TODO: ok?
452          } else {
453            !!!cp (12);
454        }        }
455        # Anything else        # Anything else
456        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
457                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char}};
458        ## Stay in the data state        ## Stay in the data state
459        !!!next-input-character;        !!!next-input-character;
460    
# Line 447  sub _get_next_token ($) { Line 470  sub _get_next_token ($) {
470        # next-input-character is already done        # next-input-character is already done
471    
472        unless (defined $token) {        unless (defined $token) {
473            !!!cp (13);
474          !!!emit ({type => CHARACTER_TOKEN, data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&'});
475        } else {        } else {
476            !!!cp (14);
477          !!!emit ($token);          !!!emit ($token);
478        }        }
479    
480        redo A;        redo A;
481      } elsif ($self->{state} == TAG_OPEN_STATE) {      } elsif ($self->{state} == TAG_OPEN_STATE) {
482        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
483          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_char} == 0x002F) { # /
484              !!!cp (15);
485            !!!next-input-character;            !!!next-input-character;
486            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
487            redo A;            redo A;
488          } else {          } else {
489              !!!cp (16);
490            ## reconsume            ## reconsume
491            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
492    
# Line 468  sub _get_next_token ($) { Line 495  sub _get_next_token ($) {
495            redo A;            redo A;
496          }          }
497        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
498          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
499              !!!cp (17);
500            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
501            !!!next-input-character;            !!!next-input-character;
502            redo A;            redo A;
503          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
504              !!!cp (18);
505            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
506            !!!next-input-character;            !!!next-input-character;
507            redo A;            redo A;
508          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
509                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
510              !!!cp (19);
511            $self->{current_token}            $self->{current_token}
512              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
513                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020)};
514            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
515            !!!next-input-character;            !!!next-input-character;
516            redo A;            redo A;
517          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
518                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
519              !!!cp (20);
520            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
521                              tag_name => chr ($self->{next_input_character})};                              tag_name => chr ($self->{next_char})};
522            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
523            !!!next-input-character;            !!!next-input-character;
524            redo A;            redo A;
525          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
526              !!!cp (21);
527            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag');
528            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
529            !!!next-input-character;            !!!next-input-character;
# Line 499  sub _get_next_token ($) { Line 531  sub _get_next_token ($) {
531            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});
532    
533            redo A;            redo A;
534          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
535              !!!cp (22);
536            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio');
537            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
538            ## $self->{next_input_character} is intentionally left as is            ## $self->{next_char} is intentionally left as is
539            redo A;            redo A;
540          } else {          } else {
541              !!!cp (23);
542            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
543            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
544            ## reconsume            ## reconsume
# Line 522  sub _get_next_token ($) { Line 556  sub _get_next_token ($) {
556            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
557            my @next_char;            my @next_char;
558            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++) {
559              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
560              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
561              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
562              if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {              if ($self->{next_char} == $c or $self->{next_char} == $C) {
563                  !!!cp (24);
564                !!!next-input-character;                !!!next-input-character;
565                next TAGNAME;                next TAGNAME;
566              } else {              } else {
567                $self->{next_input_character} = shift @next_char; # reconsume                !!!cp (25);
568                  $self->{next_char} = shift @next_char; # reconsume
569                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
570                $self->{state} = DATA_STATE;                $self->{state} = DATA_STATE;
571    
# Line 538  sub _get_next_token ($) { Line 574  sub _get_next_token ($) {
574                redo A;                redo A;
575              }              }
576            }            }
577            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
578                
579            unless ($self->{next_input_character} == 0x0009 or # HT            unless ($self->{next_char} == 0x0009 or # HT
580                    $self->{next_input_character} == 0x000A or # LF                    $self->{next_char} == 0x000A or # LF
581                    $self->{next_input_character} == 0x000B or # VT                    $self->{next_char} == 0x000B or # VT
582                    $self->{next_input_character} == 0x000C or # FF                    $self->{next_char} == 0x000C or # FF
583                    $self->{next_input_character} == 0x0020 or # SP                    $self->{next_char} == 0x0020 or # SP
584                    $self->{next_input_character} == 0x003E or # >                    $self->{next_char} == 0x003E or # >
585                    $self->{next_input_character} == 0x002F or # /                    $self->{next_char} == 0x002F or # /
586                    $self->{next_input_character} == -1) {                    $self->{next_char} == -1) {
587              $self->{next_input_character} = shift @next_char; # reconsume              !!!cp (26);
588                $self->{next_char} = shift @next_char; # reconsume
589              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
590              $self->{state} = DATA_STATE;              $self->{state} = DATA_STATE;
591              !!!emit ({type => CHARACTER_TOKEN, data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</'});
592              redo A;              redo A;
593            } else {            } else {
594              $self->{next_input_character} = shift @next_char;              !!!cp (27);
595                $self->{next_char} = shift @next_char;
596              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
597              # and consume...              # and consume...
598            }            }
599          } else {          } else {
600            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
601              !!!cp (28);
602            # next-input-character is already done            # next-input-character is already done
603            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
604            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</'});
# Line 567  sub _get_next_token ($) { Line 606  sub _get_next_token ($) {
606          }          }
607        }        }
608                
609        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
610            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
611            !!!cp (29);
612          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
613                            tag_name => chr ($self->{next_input_character} + 0x0020)};                            tag_name => chr ($self->{next_char} + 0x0020)};
614          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
615          !!!next-input-character;          !!!next-input-character;
616          redo A;          redo A;
617        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_char} and
618                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
619            !!!cp (30);
620          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
621                            tag_name => chr ($self->{next_input_character})};                            tag_name => chr ($self->{next_char})};
622          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
623          !!!next-input-character;          !!!next-input-character;
624          redo A;          redo A;
625        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
626            !!!cp (31);
627          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag');
628          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
629          !!!next-input-character;          !!!next-input-character;
630          redo A;          redo A;
631        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
632            !!!cp (32);
633          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
634          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
635          # reconsume          # reconsume
# Line 595  sub _get_next_token ($) { Line 638  sub _get_next_token ($) {
638    
639          redo A;          redo A;
640        } else {        } else {
641            !!!cp (33);
642          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
643          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
644          ## $self->{next_input_character} is intentionally left as is          ## $self->{next_char} is intentionally left as is
645          redo A;          redo A;
646        }        }
647      } elsif ($self->{state} == TAG_NAME_STATE) {      } elsif ($self->{state} == TAG_NAME_STATE) {
648        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
649            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
650            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
651            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
652            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
653            !!!cp (34);
654          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
655          !!!next-input-character;          !!!next-input-character;
656          redo A;          redo A;
657        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
658          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
659              !!!cp (35);
660            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
661                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
662            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
663          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
664            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
665            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
666              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
667            }            #  !!! cp (36);
668              #  !!! parse-error (type => 'end tag attribute');
669              #} else {
670                !!!cp (37);
671              #}
672          } else {          } else {
673            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
674          }          }
# Line 628  sub _get_next_token ($) { Line 678  sub _get_next_token ($) {
678          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
679    
680          redo A;          redo A;
681        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
682                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
683          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
684            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
685            # start tag or end tag            # start tag or end tag
686          ## Stay in this state          ## Stay in this state
687          !!!next-input-character;          !!!next-input-character;
688          redo A;          redo A;
689        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
690          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
691          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
692              !!!cp (39);
693            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
694                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
695            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
696          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
697            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
698            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
699              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
700            }            #  !!! cp (40);
701              #  !!! parse-error (type => 'end tag attribute');
702              #} else {
703                !!!cp (41);
704              #}
705          } else {          } else {
706            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
707          }          }
# Line 655  sub _get_next_token ($) { Line 711  sub _get_next_token ($) {
711          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
712    
713          redo A;          redo A;
714        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
715          !!!next-input-character;          !!!next-input-character;
716          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
717              $self->{current_token}->{type} == START_TAG_TOKEN and              $self->{current_token}->{type} == START_TAG_TOKEN and
718              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
719            # permitted slash            # permitted slash
720              !!!cp (42);
721            #            #
722          } else {          } else {
723              !!!cp (43);
724            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
725          }          }
726          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
727          # next-input-character is already done          # next-input-character is already done
728          redo A;          redo A;
729        } else {        } else {
730          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
731            $self->{current_token}->{tag_name} .= chr $self->{next_char};
732            # start tag or end tag            # start tag or end tag
733          ## Stay in the state          ## Stay in the state
734          !!!next-input-character;          !!!next-input-character;
735          redo A;          redo A;
736        }        }
737      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
738        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
739            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
740            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
741            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
742            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
743            !!!cp (45);
744          ## Stay in the state          ## Stay in the state
745          !!!next-input-character;          !!!next-input-character;
746          redo A;          redo A;
747        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
748          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
749              !!!cp (46);
750            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
751                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
752            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
753          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
754            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
755            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
756                !!!cp (47);
757              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
758              } else {
759                !!!cp (48);
760            }            }
761          } else {          } else {
762            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 703  sub _get_next_token ($) { Line 767  sub _get_next_token ($) {
767          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
768    
769          redo A;          redo A;
770        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
771                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
772          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
773            $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),
774                                value => ''};                                value => ''};
775          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
776          !!!next-input-character;          !!!next-input-character;
777          redo A;          redo A;
778        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
779          !!!next-input-character;          !!!next-input-character;
780          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
781              $self->{current_token}->{type} == START_TAG_TOKEN and              $self->{current_token}->{type} == START_TAG_TOKEN and
782              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
783            # permitted slash            # permitted slash
784              !!!cp (50);
785            #            #
786          } else {          } else {
787              !!!cp (51);
788            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
789          }          }
790          ## Stay in the state          ## Stay in the state
791          # next-input-character is already done          # next-input-character is already done
792          redo A;          redo A;
793        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
794          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
795          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
796              !!!cp (52);
797            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
798                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
799            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
800          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
801            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
802            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
803                !!!cp (53);
804              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
805              } else {
806                !!!cp (54);
807            }            }
808          } else {          } else {
809            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 748  sub _get_next_token ($) { Line 819  sub _get_next_token ($) {
819               0x0022 => 1, # "               0x0022 => 1, # "
820               0x0027 => 1, # '               0x0027 => 1, # '
821               0x003D => 1, # =               0x003D => 1, # =
822              }->{$self->{next_input_character}}) {              }->{$self->{next_char}}) {
823              !!!cp (55);
824            !!!parse-error (type => 'bad attribute name');            !!!parse-error (type => 'bad attribute name');
825            } else {
826              !!!cp (56);
827          }          }
828          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_char}),
829                                value => ''};                                value => ''};
830          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
831          !!!next-input-character;          !!!next-input-character;
# Line 761  sub _get_next_token ($) { Line 835  sub _get_next_token ($) {
835        my $before_leave = sub {        my $before_leave = sub {
836          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
837              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
838              !!!cp (57);
839            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});
840            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
841          } else {          } else {
842              !!!cp (58);
843            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
844              = $self->{current_attribute};              = $self->{current_attribute};
845          }          }
846        }; # $before_leave        }; # $before_leave
847    
848        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
849            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
850            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
851            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
852            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
853            !!!cp (59);
854          $before_leave->();          $before_leave->();
855          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
856          !!!next-input-character;          !!!next-input-character;
857          redo A;          redo A;
858        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
859            !!!cp (60);
860          $before_leave->();          $before_leave->();
861          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
862          !!!next-input-character;          !!!next-input-character;
863          redo A;          redo A;
864        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
865          $before_leave->();          $before_leave->();
866          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
867              !!!cp (61);
868            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
869                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
870            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
871          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
872              !!!cp (62);
873            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
874            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
875              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 803  sub _get_next_token ($) { Line 883  sub _get_next_token ($) {
883          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
884    
885          redo A;          redo A;
886        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
887                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
888          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
889            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
890          ## Stay in the state          ## Stay in the state
891          !!!next-input-character;          !!!next-input-character;
892          redo A;          redo A;
893        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
894          $before_leave->();          $before_leave->();
895          !!!next-input-character;          !!!next-input-character;
896          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
897              $self->{current_token}->{type} == START_TAG_TOKEN and              $self->{current_token}->{type} == START_TAG_TOKEN and
898              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
899            # permitted slash            # permitted slash
900              !!!cp (64);
901            #            #
902          } else {          } else {
903              !!!cp (65);
904            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
905          }          }
906          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
907          # next-input-character is already done          # next-input-character is already done
908          redo A;          redo A;
909        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
910          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
911          $before_leave->();          $before_leave->();
912          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
913              !!!cp (66);
914            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
915                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
916            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
917          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
918            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
919            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
920                !!!cp (67);
921              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
922              } else {
923                ## NOTE: This state should never be reached.
924                !!!cp (68);
925            }            }
926          } else {          } else {
927            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 845  sub _get_next_token ($) { Line 933  sub _get_next_token ($) {
933    
934          redo A;          redo A;
935        } else {        } else {
936          if ($self->{next_input_character} == 0x0022 or # "          if ($self->{next_char} == 0x0022 or # "
937              $self->{next_input_character} == 0x0027) { # '              $self->{next_char} == 0x0027) { # '
938              !!!cp (69);
939            !!!parse-error (type => 'bad attribute name');            !!!parse-error (type => 'bad attribute name');
940            } else {
941              !!!cp (70);
942          }          }
943          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          $self->{current_attribute}->{name} .= chr ($self->{next_char});
944          ## Stay in the state          ## Stay in the state
945          !!!next-input-character;          !!!next-input-character;
946          redo A;          redo A;
947        }        }
948      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
949        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
950            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
951            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
952            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
953            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
954            !!!cp (71);
955          ## Stay in the state          ## Stay in the state
956          !!!next-input-character;          !!!next-input-character;
957          redo A;          redo A;
958        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
959            !!!cp (72);
960          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
961          !!!next-input-character;          !!!next-input-character;
962          redo A;          redo A;
963        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
964          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
965              !!!cp (73);
966            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
967                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
968            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
969          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
970            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
971            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
972                !!!cp (74);
973              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
974              } else {
975                ## NOTE: This state should never be reached.
976                !!!cp (75);
977            }            }
978          } else {          } else {
979            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 886  sub _get_next_token ($) { Line 984  sub _get_next_token ($) {
984          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
985    
986          redo A;          redo A;
987        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
988                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
989          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
990            $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),
991                                value => ''};                                value => ''};
992          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
993          !!!next-input-character;          !!!next-input-character;
994          redo A;          redo A;
995        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
996          !!!next-input-character;          !!!next-input-character;
997          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
998              $self->{current_token}->{type} == START_TAG_TOKEN and              $self->{current_token}->{type} == START_TAG_TOKEN and
999              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
1000            # permitted slash            # permitted slash
1001              !!!cp (77);
1002            #            #
1003          } else {          } else {
1004              !!!cp (78);
1005            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
1006            ## TODO: Different error type for <aa / bb> than <aa/>            ## TODO: Different error type for <aa / bb> than <aa/>
1007          }          }
1008          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1009          # next-input-character is already done          # next-input-character is already done
1010          redo A;          redo A;
1011        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1012          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1013          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1014              !!!cp (79);
1015            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1016                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1017            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1018          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1019            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1020            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1021                !!!cp (80);
1022              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1023              } else {
1024                ## NOTE: This state should never be reached.
1025                !!!cp (81);
1026            }            }
1027          } else {          } else {
1028            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 928  sub _get_next_token ($) { Line 1034  sub _get_next_token ($) {
1034    
1035          redo A;          redo A;
1036        } else {        } else {
1037          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          !!!cp (82);
1038            $self->{current_attribute} = {name => chr ($self->{next_char}),
1039                                value => ''};                                value => ''};
1040          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1041          !!!next-input-character;          !!!next-input-character;
1042          redo A;                  redo A;        
1043        }        }
1044      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1045        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1046            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1047            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1048            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1049            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1050            !!!cp (83);
1051          ## Stay in the state          ## Stay in the state
1052          !!!next-input-character;          !!!next-input-character;
1053          redo A;          redo A;
1054        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1055            !!!cp (84);
1056          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1057          !!!next-input-character;          !!!next-input-character;
1058          redo A;          redo A;
1059        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1060            !!!cp (85);
1061          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1062          ## reconsume          ## reconsume
1063          redo A;          redo A;
1064        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1065            !!!cp (86);
1066          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1067          !!!next-input-character;          !!!next-input-character;
1068          redo A;          redo A;
1069        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1070          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1071              !!!cp (87);
1072            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1073                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1074            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1075          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1076            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1077            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1078                !!!cp (88);
1079              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1080              } else {
1081                ## NOTE: This state should never be reached.
1082                !!!cp (89);
1083            }            }
1084          } else {          } else {
1085            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 974  sub _get_next_token ($) { Line 1090  sub _get_next_token ($) {
1090          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1091    
1092          redo A;          redo A;
1093        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1094          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1095          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1096              !!!cp (90);
1097            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1098                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1099            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1100          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1101            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1102            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1103                !!!cp (91);
1104              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1105              } else {
1106                ## NOTE: This state should never be reached.
1107                !!!cp (92);
1108            }            }
1109          } else {          } else {
1110            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 995  sub _get_next_token ($) { Line 1116  sub _get_next_token ($) {
1116    
1117          redo A;          redo A;
1118        } else {        } else {
1119          if ($self->{next_input_character} == 0x003D) { # =          if ($self->{next_char} == 0x003D) { # =
1120              !!!cp (93);
1121            !!!parse-error (type => 'bad attribute value');            !!!parse-error (type => 'bad attribute value');
1122            } else {
1123              !!!cp (94);
1124          }          }
1125          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          $self->{current_attribute}->{value} .= chr ($self->{next_char});
1126          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1127          !!!next-input-character;          !!!next-input-character;
1128          redo A;          redo A;
1129        }        }
1130      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1131        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1132            !!!cp (95);
1133          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1134          !!!next-input-character;          !!!next-input-character;
1135          redo A;          redo A;
1136        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1137            !!!cp (96);
1138          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1139          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1140          !!!next-input-character;          !!!next-input-character;
1141          redo A;          redo A;
1142        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1143          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1144          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1145              !!!cp (97);
1146            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1147                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1148            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1149          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1150            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1151            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1152                !!!cp (98);
1153              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1154              } else {
1155                ## NOTE: This state should never be reached.
1156                !!!cp (99);
1157            }            }
1158          } else {          } else {
1159            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1034  sub _get_next_token ($) { Line 1165  sub _get_next_token ($) {
1165    
1166          redo A;          redo A;
1167        } else {        } else {
1168          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1169            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1170          ## Stay in the state          ## Stay in the state
1171          !!!next-input-character;          !!!next-input-character;
1172          redo A;          redo A;
1173        }        }
1174      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1175        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1176            !!!cp (101);
1177          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1178          !!!next-input-character;          !!!next-input-character;
1179          redo A;          redo A;
1180        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1181            !!!cp (102);
1182          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1183          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1184          !!!next-input-character;          !!!next-input-character;
1185          redo A;          redo A;
1186        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1187          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1188          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1189              !!!cp (103);
1190            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1191                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1192            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1193          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1194            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1195            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1196                !!!cp (104);
1197              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1198              } else {
1199                ## NOTE: This state should never be reached.
1200                !!!cp (105);
1201            }            }
1202          } else {          } else {
1203            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1070  sub _get_next_token ($) { Line 1209  sub _get_next_token ($) {
1209    
1210          redo A;          redo A;
1211        } else {        } else {
1212          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1213            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1214          ## Stay in the state          ## Stay in the state
1215          !!!next-input-character;          !!!next-input-character;
1216          redo A;          redo A;
1217        }        }
1218      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1219        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1220            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1221            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1222            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1223            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1224            !!!cp (107);
1225          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1226          !!!next-input-character;          !!!next-input-character;
1227          redo A;          redo A;
1228        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1229            !!!cp (108);
1230          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1231          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1232          !!!next-input-character;          !!!next-input-character;
1233          redo A;          redo A;
1234        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1235          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1236              !!!cp (109);
1237            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1238                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1239            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1240          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1241            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1242            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1243                !!!cp (110);
1244              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1245              } else {
1246                ## NOTE: This state should never be reached.
1247                !!!cp (111);
1248            }            }
1249          } else {          } else {
1250            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1108  sub _get_next_token ($) { Line 1255  sub _get_next_token ($) {
1255          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1256    
1257          redo A;          redo A;
1258        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1259          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1260          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1261              !!!cp (112);
1262            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1263                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1264            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1265          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1266            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1267            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1268                !!!cp (113);
1269              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1270              } else {
1271                ## NOTE: This state should never be reached.
1272                !!!cp (114);
1273            }            }
1274          } else {          } else {
1275            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1133  sub _get_next_token ($) { Line 1285  sub _get_next_token ($) {
1285               0x0022 => 1, # "               0x0022 => 1, # "
1286               0x0027 => 1, # '               0x0027 => 1, # '
1287               0x003D => 1, # =               0x003D => 1, # =
1288              }->{$self->{next_input_character}}) {              }->{$self->{next_char}}) {
1289              !!!cp (115);
1290            !!!parse-error (type => 'bad attribute value');            !!!parse-error (type => 'bad attribute value');
1291            } else {
1292              !!!cp (116);
1293          }          }
1294          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          $self->{current_attribute}->{value} .= chr ($self->{next_char});
1295          ## Stay in the state          ## Stay in the state
1296          !!!next-input-character;          !!!next-input-character;
1297          redo A;          redo A;
# Line 1151  sub _get_next_token ($) { Line 1306  sub _get_next_token ($) {
1306             -1);             -1);
1307    
1308        unless (defined $token) {        unless (defined $token) {
1309            !!!cp (117);
1310          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1311        } else {        } else {
1312            !!!cp (118);
1313          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1314          $self->{current_attribute}->{has_reference} = $token->{has_reference};          $self->{current_attribute}->{has_reference} = $token->{has_reference};
1315          ## ISSUE: spec says "append the returned character token to the current attribute's value"          ## ISSUE: spec says "append the returned character token to the current attribute's value"
# Line 1162  sub _get_next_token ($) { Line 1319  sub _get_next_token ($) {
1319        # next-input-character is already done        # next-input-character is already done
1320        redo A;        redo A;
1321      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1322        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1323            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1324            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1325            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1326            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1327            !!!cp (118);
1328          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1329          !!!next-input-character;          !!!next-input-character;
1330          redo A;          redo A;
1331        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1332          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1333              !!!cp (119);
1334            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1335                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1336            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1337          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1338            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1339            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1340                !!!cp (120);
1341              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1342              } else {
1343                ## NOTE: This state should never be reached.
1344                !!!cp (121);
1345            }            }
1346          } else {          } else {
1347            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1189  sub _get_next_token ($) { Line 1352  sub _get_next_token ($) {
1352          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1353    
1354          redo A;          redo A;
1355        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1356          !!!next-input-character;          !!!next-input-character;
1357          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_char} == 0x003E and # >
1358              $self->{current_token}->{type} == START_TAG_TOKEN and              $self->{current_token}->{type} == START_TAG_TOKEN and
1359              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
1360            # permitted slash            # permitted slash
1361              !!!cp (122);
1362            #            #
1363          } else {          } else {
1364              !!!cp (123);
1365            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
1366          }          }
1367          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1368          # next-input-character is already done          # next-input-character is already done
1369          redo A;          redo A;
1370        } else {        } else {
1371            !!!cp (124);
1372          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1373          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1374          ## reconsume          ## reconsume
# Line 1214  sub _get_next_token ($) { Line 1380  sub _get_next_token ($) {
1380        my $token = {type => COMMENT_TOKEN, data => ''};        my $token = {type => COMMENT_TOKEN, data => ''};
1381    
1382        BC: {        BC: {
1383          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1384              !!!cp (124);
1385            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1386            !!!next-input-character;            !!!next-input-character;
1387    
1388            !!!emit ($token);            !!!emit ($token);
1389    
1390            redo A;            redo A;
1391          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1392              !!!cp (125);
1393            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1394            ## reconsume            ## reconsume
1395    
# Line 1229  sub _get_next_token ($) { Line 1397  sub _get_next_token ($) {
1397    
1398            redo A;            redo A;
1399          } else {          } else {
1400            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1401              $token->{data} .= chr ($self->{next_char});
1402            !!!next-input-character;            !!!next-input-character;
1403            redo BC;            redo BC;
1404          }          }
1405        } # BC        } # BC
1406    
1407          die "$0: _get_next_token: unexpected case [BC]";
1408      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1409        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1410    
1411        my @next_char;        my @next_char;
1412        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
1413                
1414        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1415          !!!next-input-character;          !!!next-input-character;
1416          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1417          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1418              !!!cp (127);
1419            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};
1420            $self->{state} = COMMENT_START_STATE;            $self->{state} = COMMENT_START_STATE;
1421            !!!next-input-character;            !!!next-input-character;
1422            redo A;            redo A;
1423            } else {
1424              !!!cp (128);
1425          }          }
1426        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
1427                 $self->{next_input_character} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
1428          !!!next-input-character;          !!!next-input-character;
1429          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1430          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x004F or # O
1431              $self->{next_input_character} == 0x006F) { # o              $self->{next_char} == 0x006F) { # o
1432            !!!next-input-character;            !!!next-input-character;
1433            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1434            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0043 or # C
1435                $self->{next_input_character} == 0x0063) { # c                $self->{next_char} == 0x0063) { # c
1436              !!!next-input-character;              !!!next-input-character;
1437              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1438              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
1439                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
1440                !!!next-input-character;                !!!next-input-character;
1441                push @next_char, $self->{next_input_character};                push @next_char, $self->{next_char};
1442                if ($self->{next_input_character} == 0x0059 or # Y                if ($self->{next_char} == 0x0059 or # Y
1443                    $self->{next_input_character} == 0x0079) { # y                    $self->{next_char} == 0x0079) { # y
1444                  !!!next-input-character;                  !!!next-input-character;
1445                  push @next_char, $self->{next_input_character};                  push @next_char, $self->{next_char};
1446                  if ($self->{next_input_character} == 0x0050 or # P                  if ($self->{next_char} == 0x0050 or # P
1447                      $self->{next_input_character} == 0x0070) { # p                      $self->{next_char} == 0x0070) { # p
1448                    !!!next-input-character;                    !!!next-input-character;
1449                    push @next_char, $self->{next_input_character};                    push @next_char, $self->{next_char};
1450                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_char} == 0x0045 or # E
1451                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_char} == 0x0065) { # e
1452                      ## ISSUE: What a stupid code this is!                      !!!cp (129);
1453                        ## TODO: What a stupid code this is!
1454                      $self->{state} = DOCTYPE_STATE;                      $self->{state} = DOCTYPE_STATE;
1455                      !!!next-input-character;                      !!!next-input-character;
1456                      redo A;                      redo A;
1457                      } else {
1458                        !!!cp (130);
1459                    }                    }
1460                    } else {
1461                      !!!cp (131);
1462                  }                  }
1463                  } else {
1464                    !!!cp (132);
1465                }                }
1466                } else {
1467                  !!!cp (133);
1468              }              }
1469              } else {
1470                !!!cp (134);
1471            }            }
1472            } else {
1473              !!!cp (135);
1474          }          }
1475          } else {
1476            !!!cp (136);
1477        }        }
1478    
1479        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
1480        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
1481        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1482        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
1483        redo A;        redo A;
# Line 1296  sub _get_next_token ($) { Line 1485  sub _get_next_token ($) {
1485        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1486        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?
1487      } elsif ($self->{state} == COMMENT_START_STATE) {      } elsif ($self->{state} == COMMENT_START_STATE) {
1488        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1489            !!!cp (137);
1490          $self->{state} = COMMENT_START_DASH_STATE;          $self->{state} = COMMENT_START_DASH_STATE;
1491          !!!next-input-character;          !!!next-input-character;
1492          redo A;          redo A;
1493        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1494            !!!cp (138);
1495          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1496          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1497          !!!next-input-character;          !!!next-input-character;
# Line 1308  sub _get_next_token ($) { Line 1499  sub _get_next_token ($) {
1499          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1500    
1501          redo A;          redo A;
1502        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1503            !!!cp (139);
1504          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1505          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1506          ## reconsume          ## reconsume
# Line 1317  sub _get_next_token ($) { Line 1509  sub _get_next_token ($) {
1509    
1510          redo A;          redo A;
1511        } else {        } else {
1512            !!!cp (140);
1513          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1514              .= chr ($self->{next_input_character});              .= chr ($self->{next_char});
1515          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
1516          !!!next-input-character;          !!!next-input-character;
1517          redo A;          redo A;
1518        }        }
1519      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1520        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1521            !!!cp (141);
1522          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
1523          !!!next-input-character;          !!!next-input-character;
1524          redo A;          redo A;
1525        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1526            !!!cp (142);
1527          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1528          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1529          !!!next-input-character;          !!!next-input-character;
# Line 1336  sub _get_next_token ($) { Line 1531  sub _get_next_token ($) {
1531          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1532    
1533          redo A;          redo A;
1534        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1535            !!!cp (143);
1536          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1537          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1538          ## reconsume          ## reconsume
# Line 1345  sub _get_next_token ($) { Line 1541  sub _get_next_token ($) {
1541    
1542          redo A;          redo A;
1543        } else {        } else {
1544            !!!cp (144);
1545          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1546              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_char});
1547          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
1548          !!!next-input-character;          !!!next-input-character;
1549          redo A;          redo A;
1550        }        }
1551      } elsif ($self->{state} == COMMENT_STATE) {      } elsif ($self->{state} == COMMENT_STATE) {
1552        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1553            !!!cp (145);
1554          $self->{state} = COMMENT_END_DASH_STATE;          $self->{state} = COMMENT_END_DASH_STATE;
1555          !!!next-input-character;          !!!next-input-character;
1556          redo A;          redo A;
1557        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1558            !!!cp (146);
1559          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1560          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1561          ## reconsume          ## reconsume
# Line 1365  sub _get_next_token ($) { Line 1564  sub _get_next_token ($) {
1564    
1565          redo A;          redo A;
1566        } else {        } else {
1567          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
1568            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1569          ## Stay in the state          ## Stay in the state
1570          !!!next-input-character;          !!!next-input-character;
1571          redo A;          redo A;
1572        }        }
1573      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1574        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1575            !!!cp (148);
1576          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
1577          !!!next-input-character;          !!!next-input-character;
1578          redo A;          redo A;
1579        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1580            !!!cp (149);
1581          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1582          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1583          ## reconsume          ## reconsume
# Line 1384  sub _get_next_token ($) { Line 1586  sub _get_next_token ($) {
1586    
1587          redo A;          redo A;
1588        } else {        } else {
1589          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
1590            $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
1591          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
1592          !!!next-input-character;          !!!next-input-character;
1593          redo A;          redo A;
1594        }        }
1595      } elsif ($self->{state} == COMMENT_END_STATE) {      } elsif ($self->{state} == COMMENT_END_STATE) {
1596        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
1597            !!!cp (151);
1598          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1599          !!!next-input-character;          !!!next-input-character;
1600    
1601          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
1602    
1603          redo A;          redo A;
1604        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
1605            !!!cp (152);
1606          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment');
1607          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
1608          ## Stay in the state          ## Stay in the state
1609          !!!next-input-character;          !!!next-input-character;
1610          redo A;          redo A;
1611        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1612            !!!cp (153);
1613          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1614          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1615          ## reconsume          ## reconsume
# Line 1412  sub _get_next_token ($) { Line 1618  sub _get_next_token ($) {
1618    
1619          redo A;          redo A;
1620        } else {        } else {
1621            !!!cp (154);
1622          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment');
1623          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
1624          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
1625          !!!next-input-character;          !!!next-input-character;
1626          redo A;          redo A;
1627        }        }
1628      } elsif ($self->{state} == DOCTYPE_STATE) {      } elsif ($self->{state} == DOCTYPE_STATE) {
1629        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1630            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1631            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1632            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1633            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1634            !!!cp (155);
1635          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1636          !!!next-input-character;          !!!next-input-character;
1637          redo A;          redo A;
1638        } else {        } else {
1639            !!!cp (156);
1640          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
1641          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1642          ## reconsume          ## reconsume
1643          redo A;          redo A;
1644        }        }
1645      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
1646        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1647            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1648            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1649            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1650            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1651            !!!cp (157);
1652          ## Stay in the state          ## Stay in the state
1653          !!!next-input-character;          !!!next-input-character;
1654          redo A;          redo A;
1655        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1656            !!!cp (158);
1657          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1658          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1659          !!!next-input-character;          !!!next-input-character;
1660    
1661          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});
1662    
1663          redo A;          redo A;
1664        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1665            !!!cp (159);
1666          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1667          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1668          ## reconsume          ## reconsume
1669    
1670          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});
1671    
1672          redo A;          redo A;
1673        } else {        } else {
1674            !!!cp (160);
1675          $self->{current_token}          $self->{current_token}
1676              = {type => DOCTYPE_TOKEN,              = {type => DOCTYPE_TOKEN,
1677                 name => chr ($self->{next_input_character}),                 name => chr ($self->{next_char}),
1678                 correct => 1};                 #quirks => 0,
1679                  };
1680  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1681          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
1682          !!!next-input-character;          !!!next-input-character;
# Line 1470  sub _get_next_token ($) { Line 1684  sub _get_next_token ($) {
1684        }        }
1685      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1686  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
1687        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1688            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1689            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1690            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1691            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1692            !!!cp (161);
1693          $self->{state} = AFTER_DOCTYPE_NAME_STATE;          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
1694          !!!next-input-character;          !!!next-input-character;
1695          redo A;          redo A;
1696        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1697            !!!cp (162);
1698          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1699          !!!next-input-character;          !!!next-input-character;
1700    
1701          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1702    
1703          redo A;          redo A;
1704        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1705            !!!cp (163);
1706          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1707          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1708          ## reconsume          ## reconsume
1709    
1710          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1711          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1712    
1713          redo A;          redo A;
1714        } else {        } else {
1715            !!!cp (164);
1716          $self->{current_token}->{name}          $self->{current_token}->{name}
1717            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_char}); # DOCTYPE
1718          ## Stay in the state          ## Stay in the state
1719          !!!next-input-character;          !!!next-input-character;
1720          redo A;          redo A;
1721        }        }
1722      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
1723        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1724            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1725            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1726            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1727            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1728            !!!cp (165);
1729          ## Stay in the state          ## Stay in the state
1730          !!!next-input-character;          !!!next-input-character;
1731          redo A;          redo A;
1732        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1733            !!!cp (166);
1734          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1735          !!!next-input-character;          !!!next-input-character;
1736    
1737          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1738    
1739          redo A;          redo A;
1740        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1741            !!!cp (167);
1742          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1743          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1744          ## reconsume          ## reconsume
1745    
1746          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1747          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1748    
1749          redo A;          redo A;
1750        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
1751                 $self->{next_input_character} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
1752          !!!next-input-character;          !!!next-input-character;
1753          if ($self->{next_input_character} == 0x0055 or # U          if ($self->{next_char} == 0x0055 or # U
1754              $self->{next_input_character} == 0x0075) { # u              $self->{next_char} == 0x0075) { # u
1755            !!!next-input-character;            !!!next-input-character;
1756            if ($self->{next_input_character} == 0x0042 or # B            if ($self->{next_char} == 0x0042 or # B
1757                $self->{next_input_character} == 0x0062) { # b                $self->{next_char} == 0x0062) { # b
1758              !!!next-input-character;              !!!next-input-character;
1759              if ($self->{next_input_character} == 0x004C or # L              if ($self->{next_char} == 0x004C or # L
1760                  $self->{next_input_character} == 0x006C) { # l                  $self->{next_char} == 0x006C) { # l
1761                !!!next-input-character;                !!!next-input-character;
1762                if ($self->{next_input_character} == 0x0049 or # I                if ($self->{next_char} == 0x0049 or # I
1763                    $self->{next_input_character} == 0x0069) { # i                    $self->{next_char} == 0x0069) { # i
1764                  !!!next-input-character;                  !!!next-input-character;
1765                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_char} == 0x0043 or # C
1766                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_char} == 0x0063) { # c
1767                      !!!cp (168);
1768                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1769                    !!!next-input-character;                    !!!next-input-character;
1770                    redo A;                    redo A;
1771                    } else {
1772                      !!!cp (169);
1773                  }                  }
1774                  } else {
1775                    !!!cp (170);
1776                }                }
1777                } else {
1778                  !!!cp (171);
1779              }              }
1780              } else {
1781                !!!cp (172);
1782            }            }
1783            } else {
1784              !!!cp (173);
1785          }          }
1786    
1787          #          #
1788        } elsif ($self->{next_input_character} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
1789                 $self->{next_input_character} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
1790          !!!next-input-character;          !!!next-input-character;
1791          if ($self->{next_input_character} == 0x0059 or # Y          if ($self->{next_char} == 0x0059 or # Y
1792              $self->{next_input_character} == 0x0079) { # y              $self->{next_char} == 0x0079) { # y
1793            !!!next-input-character;            !!!next-input-character;
1794            if ($self->{next_input_character} == 0x0053 or # S            if ($self->{next_char} == 0x0053 or # S
1795                $self->{next_input_character} == 0x0073) { # s                $self->{next_char} == 0x0073) { # s
1796              !!!next-input-character;              !!!next-input-character;
1797              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
1798                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
1799                !!!next-input-character;                !!!next-input-character;
1800                if ($self->{next_input_character} == 0x0045 or # E                if ($self->{next_char} == 0x0045 or # E
1801                    $self->{next_input_character} == 0x0065) { # e                    $self->{next_char} == 0x0065) { # e
1802                  !!!next-input-character;                  !!!next-input-character;
1803                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_char} == 0x004D or # M
1804                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_char} == 0x006D) { # m
1805                      !!!cp (174);
1806                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1807                    !!!next-input-character;                    !!!next-input-character;
1808                    redo A;                    redo A;
1809                    } else {
1810                      !!!cp (175);
1811                  }                  }
1812                  } else {
1813                    !!!cp (176);
1814                }                }
1815                } else {
1816                  !!!cp (177);
1817              }              }
1818              } else {
1819                !!!cp (178);
1820            }            }
1821            } else {
1822              !!!cp (179);
1823          }          }
1824    
1825          #          #
1826        } else {        } else {
1827            !!!cp (180);
1828          !!!next-input-character;          !!!next-input-character;
1829          #          #
1830        }        }
1831    
1832        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
1833          $self->{current_token}->{quirks} = 1;
1834    
1835        $self->{state} = BOGUS_DOCTYPE_STATE;        $self->{state} = BOGUS_DOCTYPE_STATE;
1836        # next-input-character is already done        # next-input-character is already done
1837        redo A;        redo A;
# Line 1593  sub _get_next_token ($) { Line 1839  sub _get_next_token ($) {
1839        if ({        if ({
1840              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1841              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
1842            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
1843            !!!cp (181);
1844          ## Stay in the state          ## Stay in the state
1845          !!!next-input-character;          !!!next-input-character;
1846          redo A;          redo A;
1847        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_char} eq 0x0022) { # "
1848            !!!cp (182);
1849          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1850          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
1851          !!!next-input-character;          !!!next-input-character;
1852          redo A;          redo A;
1853        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_char} eq 0x0027) { # '
1854            !!!cp (183);
1855          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1856          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
1857          !!!next-input-character;          !!!next-input-character;
1858          redo A;          redo A;
1859        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_char} eq 0x003E) { # >
1860            !!!cp (184);
1861          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
1862    
1863          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1864          !!!next-input-character;          !!!next-input-character;
1865    
1866          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1867          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1868    
1869          redo A;          redo A;
1870        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1871            !!!cp (185);
1872          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1873    
1874          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1875          ## reconsume          ## reconsume
1876    
1877          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1878          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1879    
1880          redo A;          redo A;
1881        } else {        } else {
1882            !!!cp (186);
1883          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
1884            $self->{current_token}->{quirks} = 1;
1885    
1886          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
1887          !!!next-input-character;          !!!next-input-character;
1888          redo A;          redo A;
1889        }        }
1890      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1891        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1892            !!!cp (187);
1893          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1894          !!!next-input-character;          !!!next-input-character;
1895          redo A;          redo A;
1896        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1897            !!!cp (188);
1898          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1899    
1900          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1901          !!!next-input-character;          !!!next-input-character;
1902    
1903          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1904          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1905    
1906          redo A;          redo A;
1907        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1908            !!!cp (189);
1909          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1910    
1911          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1912          ## reconsume          ## reconsume
1913    
1914          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1915          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1916    
1917          redo A;          redo A;
1918        } else {        } else {
1919            !!!cp (190);
1920          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
1921              .= chr $self->{next_input_character};              .= chr $self->{next_char};
1922          ## Stay in the state          ## Stay in the state
1923          !!!next-input-character;          !!!next-input-character;
1924          redo A;          redo A;
1925        }        }
1926      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
1927        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1928            !!!cp (191);
1929          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1930          !!!next-input-character;          !!!next-input-character;
1931          redo A;          redo A;
1932        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1933            !!!cp (192);
1934          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1935    
1936          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1937          !!!next-input-character;          !!!next-input-character;
1938    
1939          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1940          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1941    
1942          redo A;          redo A;
1943        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1944            !!!cp (193);
1945          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1946    
1947          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1948          ## reconsume          ## reconsume
1949    
1950          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1951          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1952    
1953          redo A;          redo A;
1954        } else {        } else {
1955            !!!cp (194);
1956          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
1957              .= chr $self->{next_input_character};              .= chr $self->{next_char};
1958          ## Stay in the state          ## Stay in the state
1959          !!!next-input-character;          !!!next-input-character;
1960          redo A;          redo A;
# Line 1701  sub _get_next_token ($) { Line 1963  sub _get_next_token ($) {
1963        if ({        if ({
1964              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1965              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
1966            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
1967            !!!cp (195);
1968          ## Stay in the state          ## Stay in the state
1969          !!!next-input-character;          !!!next-input-character;
1970          redo A;          redo A;
1971        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1972            !!!cp (196);
1973          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1974          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1975          !!!next-input-character;          !!!next-input-character;
1976          redo A;          redo A;
1977        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1978            !!!cp (197);
1979          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1980          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1981          !!!next-input-character;          !!!next-input-character;
1982          redo A;          redo A;
1983        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1984            !!!cp (198);
1985          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1986          !!!next-input-character;          !!!next-input-character;
1987    
1988          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
1989    
1990          redo A;          redo A;
1991        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1992            !!!cp (199);
1993          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1994    
1995          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1996          ## reconsume          ## reconsume
1997    
1998          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
1999          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2000    
2001          redo A;          redo A;
2002        } else {        } else {
2003            !!!cp (200);
2004          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2005            $self->{current_token}->{quirks} = 1;
2006    
2007          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2008          !!!next-input-character;          !!!next-input-character;
2009          redo A;          redo A;
# Line 1742  sub _get_next_token ($) { Line 2012  sub _get_next_token ($) {
2012        if ({        if ({
2013              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2014              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2015            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2016            !!!cp (201);
2017          ## Stay in the state          ## Stay in the state
2018          !!!next-input-character;          !!!next-input-character;
2019          redo A;          redo A;
2020        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2021            !!!cp (202);
2022          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2023          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2024          !!!next-input-character;          !!!next-input-character;
2025          redo A;          redo A;
2026        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2027            !!!cp (203);
2028          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2029          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2030          !!!next-input-character;          !!!next-input-character;
2031          redo A;          redo A;
2032        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2033            !!!cp (204);
2034          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2035          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2036          !!!next-input-character;          !!!next-input-character;
2037    
2038          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2039          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2040    
2041          redo A;          redo A;
2042        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2043            !!!cp (205);
2044          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2045    
2046          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2047          ## reconsume          ## reconsume
2048    
2049          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2050          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2051    
2052          redo A;          redo A;
2053        } else {        } else {
2054            !!!cp (206);
2055          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2056            $self->{current_token}->{quirks} = 1;
2057    
2058          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2059          !!!next-input-character;          !!!next-input-character;
2060          redo A;          redo A;
2061        }        }
2062      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2063        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2064            !!!cp (207);
2065          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2066          !!!next-input-character;          !!!next-input-character;
2067          redo A;          redo A;
2068        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2069            !!!cp (208);
2070          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2071    
2072          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2073          !!!next-input-character;          !!!next-input-character;
2074    
2075          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2076          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2077    
2078          redo A;          redo A;
2079        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2080            !!!cp (209);
2081          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2082    
2083          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2084          ## reconsume          ## reconsume
2085    
2086          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2087          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2088    
2089          redo A;          redo A;
2090        } else {        } else {
2091            !!!cp (210);
2092          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2093              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2094          ## Stay in the state          ## Stay in the state
2095          !!!next-input-character;          !!!next-input-character;
2096          redo A;          redo A;
2097        }        }
2098      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2099        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2100            !!!cp (211);
2101          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2102          !!!next-input-character;          !!!next-input-character;
2103          redo A;          redo A;
2104        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2105            !!!cp (212);
2106          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2107    
2108          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2109          !!!next-input-character;          !!!next-input-character;
2110    
2111          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2112          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2113    
2114          redo A;          redo A;
2115        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2116            !!!cp (213);
2117          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2118    
2119          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2120          ## reconsume          ## reconsume
2121    
2122          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2123          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2124    
2125          redo A;          redo A;
2126        } else {        } else {
2127            !!!cp (214);
2128          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2129              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2130          ## Stay in the state          ## Stay in the state
2131          !!!next-input-character;          !!!next-input-character;
2132          redo A;          redo A;
# Line 1849  sub _get_next_token ($) { Line 2135  sub _get_next_token ($) {
2135        if ({        if ({
2136              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2137              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2138            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2139            !!!cp (215);
2140          ## Stay in the state          ## Stay in the state
2141          !!!next-input-character;          !!!next-input-character;
2142          redo A;          redo A;
2143        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2144            !!!cp (216);
2145          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2146          !!!next-input-character;          !!!next-input-character;
2147    
2148          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2149    
2150          redo A;          redo A;
2151        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2152            !!!cp (217);
2153          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2154    
2155          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2156          ## reconsume          ## reconsume
2157    
2158          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2159          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2160    
2161          redo A;          redo A;
2162        } else {        } else {
2163            !!!cp (218);
2164          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2165            #$self->{current_token}->{quirks} = 1;
2166    
2167          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2168          !!!next-input-character;          !!!next-input-character;
2169          redo A;          redo A;
2170        }        }
2171      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2172        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2173            !!!cp (219);
2174          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2175          !!!next-input-character;          !!!next-input-character;
2176    
         delete $self->{current_token}->{correct};  
2177          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2178    
2179          redo A;          redo A;
2180        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2181            !!!cp (220);
2182          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2183          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2184          ## reconsume          ## reconsume
2185    
         delete $self->{current_token}->{correct};  
2186          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2187    
2188          redo A;          redo A;
2189        } else {        } else {
2190            !!!cp (221);
2191          ## Stay in the state          ## Stay in the state
2192          !!!next-input-character;          !!!next-input-character;
2193          redo A;          redo A;
# Line 1914  sub _tokenize_attempt_to_consume_an_enti Line 2207  sub _tokenize_attempt_to_consume_an_enti
2207         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2208         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2209         $additional => 1,         $additional => 1,
2210        }->{$self->{next_input_character}}) {        }->{$self->{next_char}}) {
2211        !!!cp (1001);
2212      ## Don't consume      ## Don't consume
2213      ## No error      ## No error
2214      return undef;      return undef;
2215    } elsif ($self->{next_input_character} == 0x0023) { # #    } elsif ($self->{next_char} == 0x0023) { # #
2216      !!!next-input-character;      !!!next-input-character;
2217      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_char} == 0x0078 or # x
2218          $self->{next_input_character} == 0x0058) { # X          $self->{next_char} == 0x0058) { # X
2219        my $code;        my $code;
2220        X: {        X: {
2221          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2222          !!!next-input-character;          !!!next-input-character;
2223          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2224              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2225              !!!cp (1002);
2226            $code ||= 0;            $code ||= 0;
2227            $code *= 0x10;            $code *= 0x10;
2228            $code += $self->{next_input_character} - 0x0030;            $code += $self->{next_char} - 0x0030;
2229            redo X;            redo X;
2230          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2231                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2232              !!!cp (1003);
2233            $code ||= 0;            $code ||= 0;
2234            $code *= 0x10;            $code *= 0x10;
2235            $code += $self->{next_input_character} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2236            redo X;            redo X;
2237          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2238                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2239              !!!cp (1004);
2240            $code ||= 0;            $code ||= 0;
2241            $code *= 0x10;            $code *= 0x10;
2242            $code += $self->{next_input_character} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2243            redo X;            redo X;
2244          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2245              !!!cp (1005);
2246            !!!parse-error (type => 'bare hcro');            !!!parse-error (type => 'bare hcro');
2247            !!!back-next-input-character ($x_char, $self->{next_input_character});            !!!back-next-input-character ($x_char, $self->{next_char});
2248            $self->{next_input_character} = 0x0023; # #            $self->{next_char} = 0x0023; # #
2249            return undef;            return undef;
2250          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2251              !!!cp (1006);
2252            !!!next-input-character;            !!!next-input-character;
2253          } else {          } else {
2254              !!!cp (1007);
2255            !!!parse-error (type => 'no refc');            !!!parse-error (type => 'no refc');
2256          }          }
2257    
2258          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2259              !!!cp (1008);
2260            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
2261            $code = 0xFFFD;            $code = 0xFFFD;
2262          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2263              !!!cp (1009);
2264            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
2265            $code = 0xFFFD;            $code = 0xFFFD;
2266          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2267              !!!cp (1010);
2268            !!!parse-error (type => 'CR character reference');            !!!parse-error (type => 'CR character reference');
2269            $code = 0x000A;            $code = 0x000A;
2270          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2271              !!!cp (1011);
2272            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
2273            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2274          }          }
# Line 1972  sub _tokenize_attempt_to_consume_an_enti Line 2276  sub _tokenize_attempt_to_consume_an_enti
2276          return {type => CHARACTER_TOKEN, data => chr $code,          return {type => CHARACTER_TOKEN, data => chr $code,
2277                  has_reference => 1};                  has_reference => 1};
2278        } # X        } # X
2279      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
2280               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
2281        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
2282        !!!next-input-character;        !!!next-input-character;
2283                
2284        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
2285                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2286            !!!cp (1012);
2287          $code *= 10;          $code *= 10;
2288          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
2289                    
2290          !!!next-input-character;          !!!next-input-character;
2291        }        }
2292    
2293        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
2294            !!!cp (1013);
2295          !!!next-input-character;          !!!next-input-character;
2296        } else {        } else {
2297            !!!cp (1014);
2298          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc');
2299        }        }
2300    
2301        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2302            !!!cp (1015);
2303          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);
2304          $code = 0xFFFD;          $code = 0xFFFD;
2305        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2306            !!!cp (1016);
2307          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);
2308          $code = 0xFFFD;          $code = 0xFFFD;
2309        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2310            !!!cp (1017);
2311          !!!parse-error (type => 'CR character reference');          !!!parse-error (type => 'CR character reference');
2312          $code = 0x000A;          $code = 0x000A;
2313        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2314            !!!cp (1018);
2315          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);
2316          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2317        }        }
2318                
2319        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};
2320      } else {      } else {
2321          !!!cp (1019);
2322        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero');
2323        !!!back-next-input-character ($self->{next_input_character});        !!!back-next-input-character ($self->{next_char});
2324        $self->{next_input_character} = 0x0023; # #        $self->{next_char} = 0x0023; # #
2325        return undef;        return undef;
2326      }      }
2327    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
2328              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
2329             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
2330              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
2331      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
2332      !!!next-input-character;      !!!next-input-character;
2333    
2334      my $value = $entity_name;      my $value = $entity_name;
# Line 2026  sub _tokenize_attempt_to_consume_an_enti Line 2338  sub _tokenize_attempt_to_consume_an_enti
2338    
2339      while (length $entity_name < 10 and      while (length $entity_name < 10 and
2340             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2341             ((0x0041 <= $self->{next_input_character} and # a             ((0x0041 <= $self->{next_char} and # a
2342               $self->{next_input_character} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
2343              (0x0061 <= $self->{next_input_character} and # a              (0x0061 <= $self->{next_char} and # a
2344               $self->{next_input_character} <= 0x007A) or # z               $self->{next_char} <= 0x007A) or # z
2345              (0x0030 <= $self->{next_input_character} and # 0              (0x0030 <= $self->{next_char} and # 0
2346               $self->{next_input_character} <= 0x0039) or # 9               $self->{next_char} <= 0x0039) or # 9
2347              $self->{next_input_character} == 0x003B)) { # ;              $self->{next_char} == 0x003B)) { # ;
2348        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_char};
2349        if (defined $EntityChar->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
2350          if ($self->{next_input_character} == 0x003B) { # ;          if ($self->{next_char} == 0x003B) { # ;
2351              !!!cp (1020);
2352            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2353            $match = 1;            $match = 1;
2354            !!!next-input-character;            !!!next-input-character;
2355            last;            last;
2356          } else {          } else {
2357              !!!cp (1021);
2358            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2359            $match = -1;            $match = -1;
2360            !!!next-input-character;            !!!next-input-character;
2361          }          }
2362        } else {        } else {
2363          $value .= chr $self->{next_input_character};          !!!cp (1022);
2364            $value .= chr $self->{next_char};
2365          $match *= 2;          $match *= 2;
2366          !!!next-input-character;          !!!next-input-character;
2367        }        }
2368      }      }
2369            
2370      if ($match > 0) {      if ($match > 0) {
2371          !!!cp (1023);
2372        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
2373      } elsif ($match < 0) {      } elsif ($match < 0) {
2374        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc');
2375        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
2376            !!!cp (1024);
2377          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
2378        } else {        } else {
2379            !!!cp (1025);
2380          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
2381        }        }
2382      } else {      } else {
2383          !!!cp (1026);
2384        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
2385        ## NOTE: "No characters are consumed" in the spec.        ## NOTE: "No characters are consumed" in the spec.
2386        return {type => CHARACTER_TOKEN, data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value};
2387      }      }
2388    } else {    } else {
2389        !!!cp (1027);
2390      ## no characters are consumed      ## no characters are consumed
2391      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero');
2392      return undef;      return undef;
# Line 2106  sub _construct_tree ($) { Line 2426  sub _construct_tree ($) {
2426        
2427    !!!next-token;    !!!next-token;
2428    
   $self->{insertion_mode} = BEFORE_HEAD_IM;  
2429    undef $self->{form_element};    undef $self->{form_element};
2430    undef $self->{head_element};    undef $self->{head_element};
2431    $self->{open_elements} = [];    $self->{open_elements} = [];
2432    undef $self->{inner_html_node};    undef $self->{inner_html_node};
2433    
2434      ## NOTE: The "initial" insertion mode.
2435    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
2436    
2437      ## NOTE: The "before html" insertion mode.
2438    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
2439      $self->{insertion_mode} = BEFORE_HEAD_IM;
2440    
2441      ## NOTE: The "before head" insertion mode and so on.
2442    $self->_tree_construction_main;    $self->_tree_construction_main;
2443  } # _construct_tree  } # _construct_tree
2444    
2445  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
2446    my $self = shift;    my $self = shift;
2447    
2448      ## NOTE: "initial" insertion mode
2449    
2450    INITIAL: {    INITIAL: {
2451      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
2452        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
# Line 2130  sub _tree_construction_initial ($) { Line 2458  sub _tree_construction_initial ($) {
2458        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
2459            defined $token->{public_identifier} or            defined $token->{public_identifier} or
2460            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
2461            !!!cp ('t1');
2462          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5');
2463        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
2464            !!!cp ('t2');
2465          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
2466          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5');
2467          } else {
2468            !!!cp ('t3');
2469        }        }
2470                
2471        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
# Line 2146  sub _tree_construction_initial ($) { Line 2478  sub _tree_construction_initial ($) {
2478        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
2479        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
2480                
2481        if (not $token->{correct} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
2482            !!!cp ('t4');
2483          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
2484        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
2485          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
# Line 2225  sub _tree_construction_initial ($) { Line 2558  sub _tree_construction_initial ($) {
2558            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
2559            "HTML" => 1,            "HTML" => 1,
2560          }->{$pubid}) {          }->{$pubid}) {
2561              !!!cp ('t5');
2562            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
2563          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
2564                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
2565            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
2566                !!!cp ('t6');
2567              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
2568            } else {            } else {
2569                !!!cp ('t7');
2570              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
2571            }            }
2572          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or
2573                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {
2574              !!!cp ('t8');
2575            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
2576            } else {
2577              !!!cp ('t9');
2578          }          }
2579          } else {
2580            !!!cp ('t10');
2581        }        }
2582        if (defined $token->{system_identifier}) {        if (defined $token->{system_identifier}) {
2583          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
2584          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
2585          if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {          if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
2586              ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"
2587            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
2588              !!!cp ('t11');
2589            } else {
2590              !!!cp ('t12');
2591          }          }
2592          } else {
2593            !!!cp ('t13');
2594        }        }
2595                
2596        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
2597        !!!next-token;        !!!next-token;
2598        return;        return;
2599      } elsif ({      } elsif ({
# Line 2254  sub _tree_construction_initial ($) { Line 2601  sub _tree_construction_initial ($) {
2601                END_TAG_TOKEN, 1,                END_TAG_TOKEN, 1,
2602                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
2603               }->{$token->{type}}) {               }->{$token->{type}}) {
2604          !!!cp ('t14');
2605        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE');
2606        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2607        ## Go to the root element phase        ## Go to the "before html" insertion mode.
2608        ## reprocess        ## reprocess
2609        return;        return;
2610      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
# Line 2264  sub _tree_construction_initial ($) { Line 2612  sub _tree_construction_initial ($) {
2612          ## Ignore the token          ## Ignore the token
2613    
2614          unless (length $token->{data}) {          unless (length $token->{data}) {
2615            ## Stay in the phase            !!!cp ('t15');
2616              ## Stay in the insertion mode.
2617            !!!next-token;            !!!next-token;
2618            redo INITIAL;            redo INITIAL;
2619            } else {
2620              !!!cp ('t16');
2621          }          }
2622          } else {
2623            !!!cp ('t17');
2624        }        }
2625    
2626        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE');
2627        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2628        ## Go to the root element phase        ## Go to the "before html" insertion mode.
2629        ## reprocess        ## reprocess
2630        return;        return;
2631      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
2632          !!!cp ('t18');
2633        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2634        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
2635                
2636        ## Stay in the phase.        ## Stay in the insertion mode.
2637        !!!next-token;        !!!next-token;
2638        redo INITIAL;        redo INITIAL;
2639      } else {      } else {
2640        die "$0: $token->{type}: Unknown token type";        die "$0: $token->{type}: Unknown token type";
2641      }      }
2642    } # INITIAL    } # INITIAL
2643    
2644      die "$0: _tree_construction_initial: This should be never reached";
2645  } # _tree_construction_initial  } # _tree_construction_initial
2646    
2647  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
2648    my $self = shift;    my $self = shift;
2649    
2650      ## NOTE: "before html" insertion mode.
2651        
2652    B: {    B: {
2653        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
2654            !!!cp ('t19');
2655          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE');
2656          ## Ignore the token          ## Ignore the token
2657          ## Stay in the phase          ## Stay in the insertion mode.
2658          !!!next-token;          !!!next-token;
2659          redo B;          redo B;
2660        } elsif ($token->{type} == COMMENT_TOKEN) {        } elsif ($token->{type} == COMMENT_TOKEN) {
2661            !!!cp ('t20');
2662          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2663          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2664          ## Stay in the phase          ## Stay in the insertion mode.
2665          !!!next-token;          !!!next-token;
2666          redo B;          redo B;
2667        } elsif ($token->{type} == CHARACTER_TOKEN) {        } elsif ($token->{type} == CHARACTER_TOKEN) {
# Line 2309  sub _tree_construction_root_element ($) Line 2669  sub _tree_construction_root_element ($)
2669            ## Ignore the token.            ## Ignore the token.
2670    
2671            unless (length $token->{data}) {            unless (length $token->{data}) {
2672              ## Stay in the phase              !!!cp ('t21');
2673                ## Stay in the insertion mode.
2674              !!!next-token;              !!!next-token;
2675              redo B;              redo B;
2676              } else {
2677                !!!cp ('t22');
2678            }            }
2679            } else {
2680              !!!cp ('t23');
2681          }          }
2682    
2683          $self->{application_cache_selection}->(undef);          $self->{application_cache_selection}->(undef);
2684    
2685          #          #
2686        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
2687          if ($token->{tag_name} eq 'html' and          if ($token->{tag_name} eq 'html') {
2688              $token->{attributes}->{manifest}) {            my $root_element;
2689            $self->{application_cache_selection}            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes});
2690                 ->($token->{attributes}->{manifest}->{value});            $self->{document}->append_child ($root_element);
2691            ## ISSUE: No relative reference resolution?            push @{$self->{open_elements}}, [$root_element, 'html'];
2692    
2693              if ($token->{attributes}->{manifest}) {
2694                !!!cp ('t24');
2695                $self->{application_cache_selection}
2696                    ->($token->{attributes}->{manifest}->{value});
2697                ## ISSUE: No relative reference resolution?
2698              } else {
2699                !!!cp ('t25');
2700                $self->{application_cache_selection}->(undef);
2701              }
2702    
2703              !!!next-token;
2704              return; ## Go to the "before head" insertion mode.
2705          } else {          } else {
2706            $self->{application_cache_selection}->(undef);            !!!cp ('t25.1');
2707              #
2708          }          }
   
         ## ISSUE: There is an issue in the spec  
         #  
2709        } elsif ({        } elsif ({
2710                  END_TAG_TOKEN, 1,                  END_TAG_TOKEN, 1,
2711                  END_OF_FILE_TOKEN, 1,                  END_OF_FILE_TOKEN, 1,
2712                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2713          $self->{application_cache_selection}->(undef);          !!!cp ('t26');
   
         ## ISSUE: There is an issue in the spec  
2714          #          #
2715        } else {        } else {
2716          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
2717        }        }
2718    
2719        my $root_element; !!!create-element ($root_element, 'html');      my $root_element; !!!create-element ($root_element, 'html');
2720        $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
2721        push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, 'html'];
2722        ## reprocess  
2723        #redo B;      $self->{application_cache_selection}->(undef);
2724        return; ## Go to the main phase.  
2725        ## NOTE: Reprocess the token.
2726        return; ## Go to the "before head" insertion mode.
2727    
2728        ## ISSUE: There is an issue in the spec
2729    } # B    } # B
2730    
2731      die "$0: _tree_construction_root_element: This should never be reached";
2732  } # _tree_construction_root_element  } # _tree_construction_root_element
2733    
2734  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2373  sub _reset_insertion_mode ($) { Line 2753  sub _reset_insertion_mode ($) {
2753          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
2754            if ($self->{inner_html_node}->[1] eq 'td' or            if ($self->{inner_html_node}->[1] eq 'td' or
2755                $self->{inner_html_node}->[1] eq 'th') {                $self->{inner_html_node}->[1] eq 'th') {
2756                !!!cp ('t27');
2757              #              #
2758            } else {            } else {
2759                !!!cp ('t28');
2760              $node = $self->{inner_html_node};              $node = $self->{inner_html_node};
2761            }            }
2762          }          }
# Line 2383  sub _reset_insertion_mode ($) { Line 2765  sub _reset_insertion_mode ($) {
2765        ## Step 4..13        ## Step 4..13
2766        my $new_mode = {        my $new_mode = {
2767                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
2768                          ## NOTE: |option| and |optgroup| do not set
2769                          ## insertion mode to "in select" by themselves.
2770                        td => IN_CELL_IM,                        td => IN_CELL_IM,
2771                        th => IN_CELL_IM,                        th => IN_CELL_IM,
2772                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
# Line 2401  sub _reset_insertion_mode ($) { Line 2785  sub _reset_insertion_mode ($) {
2785        ## Step 14        ## Step 14
2786        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2787          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2788              !!!cp ('t29');
2789            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
2790          } else {          } else {
2791              ## ISSUE: Can this state be reached?
2792              !!!cp ('t30');
2793            $self->{insertion_mode} = AFTER_HEAD_IM;            $self->{insertion_mode} = AFTER_HEAD_IM;
2794          }          }
2795          return;          return;
2796          } else {
2797            !!!cp ('t31');
2798        }        }
2799                
2800        ## Step 15        ## Step 15
# Line 2418  sub _reset_insertion_mode ($) { Line 2807  sub _reset_insertion_mode ($) {
2807        ## Step 17        ## Step 17
2808        redo S3;        redo S3;
2809      } # S3      } # S3
2810    
2811      die "$0: _reset_insertion_mode: This line should never be reached";
2812  } # _reset_insertion_mode  } # _reset_insertion_mode
2813    
2814  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
# Line 2439  sub _tree_construction_main ($) { Line 2830  sub _tree_construction_main ($) {
2830      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
2831      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
2832        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
2833            !!!cp ('t32');
2834          return;          return;
2835        }        }
2836      }      }
# Line 2453  sub _tree_construction_main ($) { Line 2845  sub _tree_construction_main ($) {
2845    
2846        ## Step 6        ## Step 6
2847        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
2848            !!!cp ('t33_1');
2849          #          #
2850        } else {        } else {
2851          my $in_open_elements;          my $in_open_elements;
2852          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
2853            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
2854                !!!cp ('t33');
2855              $in_open_elements = 1;              $in_open_elements = 1;
2856              last OE;              last OE;
2857            }            }
2858          }          }
2859          if ($in_open_elements) {          if ($in_open_elements) {
2860              !!!cp ('t34');
2861            #            #
2862          } else {          } else {
2863              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
2864              !!!cp ('t35');
2865            redo S4;            redo S4;
2866          }          }
2867        }        }
# Line 2487  sub _tree_construction_main ($) { Line 2884  sub _tree_construction_main ($) {
2884    
2885        ## Step 11        ## Step 11
2886        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
2887            !!!cp ('t36');
2888          ## Step 7'          ## Step 7'
2889          $i++;          $i++;
2890          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
2891                    
2892          redo S7;          redo S7;
2893        }        }
2894    
2895          !!!cp ('t37');
2896      } # S7      } # S7
2897    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
2898    
2899    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
2900      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
2901        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
2902            !!!cp ('t38');
2903          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
2904          return;          return;
2905        }        }
2906      }      }
2907    
2908        !!!cp ('t39');
2909    }; # $clear_up_to_marker    }; # $clear_up_to_marker
2910    
2911    my $parse_rcdata = sub ($$) {    my $parse_rcdata = sub ($$) {
# Line 2524  sub _tree_construction_main ($) { Line 2927  sub _tree_construction_main ($) {
2927      my $text = '';      my $text = '';
2928      !!!next-token;      !!!next-token;
2929      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2930          !!!cp ('t40');
2931        $text .= $token->{data};        $text .= $token->{data};
2932        !!!next-token;        !!!next-token;
2933      }      }
2934    
2935      ## Step 5      ## Step 5
2936      if (length $text) {      if (length $text) {
2937          !!!cp ('t41');
2938        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
2939        $el->append_child ($text);        $el->append_child ($text);
2940      }      }
# Line 2538  sub _tree_construction_main ($) { Line 2943  sub _tree_construction_main ($) {
2943      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
2944    
2945      ## Step 7      ## Step 7
2946      if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
2947            $token->{tag_name} eq $start_tag_name) {
2948          !!!cp ('t42');
2949        ## Ignore the token        ## Ignore the token
2950      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2951          !!!cp ('t43');
2952        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type});
2953      } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {      } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
2954          !!!cp ('t44');
2955        !!!parse-error (type => 'in RCDATA:#'.$token->{type});        !!!parse-error (type => 'in RCDATA:#'.$token->{type});
2956      } else {      } else {
2957        die "$0: $content_model_flag in parse_rcdata";        die "$0: $content_model_flag in parse_rcdata";
# Line 2562  sub _tree_construction_main ($) { Line 2971  sub _tree_construction_main ($) {
2971      my $text = '';      my $text = '';
2972      !!!next-token;      !!!next-token;
2973      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
2974          !!!cp ('t45');
2975        $text .= $token->{data};        $text .= $token->{data};
2976        !!!next-token;        !!!next-token;
2977      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
2978      if (length $text) {      if (length $text) {
2979          !!!cp ('t46');
2980        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
2981      }      }
2982                                
# Line 2573  sub _tree_construction_main ($) { Line 2984  sub _tree_construction_main ($) {
2984    
2985      if ($token->{type} == END_TAG_TOKEN and      if ($token->{type} == END_TAG_TOKEN and
2986          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
2987          !!!cp ('t47');
2988        ## Ignore the token        ## Ignore the token
2989      } else {      } else {
2990          !!!cp ('t48');
2991        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type});
2992        ## ISSUE: And ignore?        ## ISSUE: And ignore?
2993        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
2994      }      }
2995            
2996      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
2997          !!!cp ('t49');
2998        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
2999      } else {      } else {
3000          !!!cp ('t50');
3001        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3002        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3003    
# Line 2605  sub _tree_construction_main ($) { Line 3020  sub _tree_construction_main ($) {
3020        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3021        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3022          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {
3023              !!!cp ('t51');
3024            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3025            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3026            last AFE;            last AFE;
3027          } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {          } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {
3028              !!!cp ('t52');
3029            last AFE;            last AFE;
3030          }          }
3031        } # AFE        } # AFE
3032        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3033            !!!cp ('t53');
3034          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!parse-error (type => 'unmatched end tag:'.$tag_name);
3035          ## Ignore the token          ## Ignore the token
3036          !!!next-token;          !!!next-token;
# Line 2625  sub _tree_construction_main ($) { Line 3043  sub _tree_construction_main ($) {
3043          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3044          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3045            if ($in_scope) {            if ($in_scope) {
3046                !!!cp ('t54');
3047              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3048              last INSCOPE;              last INSCOPE;
3049            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3050                !!!cp ('t55');
3051              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3052              ## Ignore the token              ## Ignore the token
3053              !!!next-token;              !!!next-token;
# Line 2637  sub _tree_construction_main ($) { Line 3057  sub _tree_construction_main ($) {
3057                    table => 1, caption => 1, td => 1, th => 1,                    table => 1, caption => 1, td => 1, th => 1,
3058                    button => 1, marquee => 1, object => 1, html => 1,                    button => 1, marquee => 1, object => 1, html => 1,
3059                   }->{$node->[1]}) {                   }->{$node->[1]}) {
3060              !!!cp ('t56');
3061            $in_scope = 0;            $in_scope = 0;
3062          }          }
3063        } # INSCOPE        } # INSCOPE
3064        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3065            !!!cp ('t57');
3066          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3067          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3068          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3069          return;          return;
3070        }        }
3071        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3072            !!!cp ('t58');
3073          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3074        }        }
3075                
# Line 2659  sub _tree_construction_main ($) { Line 3082  sub _tree_construction_main ($) {
3082              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3083              ($special_category->{$node->[1]} or              ($special_category->{$node->[1]} or
3084               $scoping_category->{$node->[1]})) {               $scoping_category->{$node->[1]})) {
3085              !!!cp ('t59');
3086            $furthest_block = $node;            $furthest_block = $node;
3087            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3088          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3089              !!!cp ('t60');
3090            last OE;            last OE;
3091          }          }
3092        } # OE        } # OE
3093                
3094        ## Step 3        ## Step 3
3095        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3096            !!!cp ('t61');
3097          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3098          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3099          !!!next-token;          !!!next-token;
# Line 2680  sub _tree_construction_main ($) { Line 3106  sub _tree_construction_main ($) {
3106        ## Step 5        ## Step 5
3107        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3108        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3109            !!!cp ('t62');
3110          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3111        }        }
3112                
# Line 2702  sub _tree_construction_main ($) { Line 3129  sub _tree_construction_main ($) {
3129          S7S2: {          S7S2: {
3130            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3131              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3132                  !!!cp ('t63');
3133                $node_i_in_active = $_;                $node_i_in_active = $_;
3134                last S7S2;                last S7S2;
3135              }              }
# Line 2715  sub _tree_construction_main ($) { Line 3143  sub _tree_construction_main ($) {
3143                    
3144          ## Step 4          ## Step 4
3145          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3146              !!!cp ('t64');
3147            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3148          }          }
3149                    
3150          ## Step 5          ## Step 5
3151          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3152              !!!cp ('t65');
3153            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3154            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3155            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2754  sub _tree_construction_main ($) { Line 3184  sub _tree_construction_main ($) {
3184        my $i;        my $i;
3185        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3186          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3187              !!!cp ('t66');
3188            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3189            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3190          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3191              !!!cp ('t67');
3192            $i = $_;            $i = $_;
3193          }          }
3194        } # AFE        } # AFE
# Line 2766  sub _tree_construction_main ($) { Line 3198  sub _tree_construction_main ($) {
3198        undef $i;        undef $i;
3199        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3200          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3201              !!!cp ('t68');
3202            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3203            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3204          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3205              !!!cp ('t69');
3206            $i = $_;            $i = $_;
3207          }          }
3208        } # OE        } # OE
# Line 2796  sub _tree_construction_main ($) { Line 3230  sub _tree_construction_main ($) {
3230                             if ($self->{open_elements}->[$_]->[1] eq 'table') {                             if ($self->{open_elements}->[$_]->[1] eq 'table') {
3231                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3232                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3233                                   !!!cp ('t70');
3234                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
3235                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
3236                               } else {                               } else {
3237                                   !!!cp ('t71');
3238                                 $foster_parent_element                                 $foster_parent_element
3239                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
3240                               }                               }
# Line 2810  sub _tree_construction_main ($) { Line 3246  sub _tree_construction_main ($) {
3246                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3247                             ($child, $next_sibling);                             ($child, $next_sibling);
3248                         } else {                         } else {
3249                             !!!cp ('t72');
3250                           $self->{open_elements}->[-1]->[0]->append_child ($child);                           $self->{open_elements}->[-1]->[0]->append_child ($child);
3251                         }                         }
3252    }; # $insert_to_foster    }; # $insert_to_foster
# Line 2818  sub _tree_construction_main ($) { Line 3255  sub _tree_construction_main ($) {
3255    
3256    B: {    B: {
3257      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3258          !!!cp ('t73');
3259        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'DOCTYPE in the middle');
3260        ## Ignore the token        ## Ignore the token
3261        ## Stay in the phase        ## Stay in the phase
# Line 2825  sub _tree_construction_main ($) { Line 3263  sub _tree_construction_main ($) {
3263        redo B;        redo B;
3264      } elsif ($token->{type} == END_OF_FILE_TOKEN) {      } elsif ($token->{type} == END_OF_FILE_TOKEN) {
3265        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3266            !!!cp ('t74');
3267          #          #
3268        } else {        } else {
3269          ## Generate implied end tags          ## Generate implied end tags
3270          if ({          while ({
3271               dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,                  dd => 1, dt => 1, li => 1, p => 1,
3272               tbody => 1, tfoot=> 1, thead => 1,                 }->{$self->{open_elements}->[-1]->[1]}) {
3273              }->{$self->{open_elements}->[-1]->[1]}) {            !!!cp ('t75');
3274            !!!back-token;            pop @{$self->{open_elements}};
           $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};  
           redo B;  
3275          }          }
3276                    
3277          if (@{$self->{open_elements}} > 2 or          if (@{$self->{open_elements}} > 2 or
3278              (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {              (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {
3279              !!!cp ('t76');
3280            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3281          } elsif (defined $self->{inner_html_node} and          } elsif (defined $self->{inner_html_node} and
3282                   @{$self->{open_elements}} > 1 and                   @{$self->{open_elements}} > 1 and
3283                   $self->{open_elements}->[1]->[1] ne 'body') {                   $self->{open_elements}->[1]->[1] ne 'body') {
3284    ## ISSUE: This case is never reached.
3285              !!!cp ('t77');
3286            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3287            } else {
3288              !!!cp ('t78');
3289          }          }
3290    
3291          ## ISSUE: There is an issue in the spec.          ## ISSUE: There is an issue in the spec.
# Line 2854  sub _tree_construction_main ($) { Line 3296  sub _tree_construction_main ($) {
3296      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3297               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3298        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3299          ## Turn into the main phase          !!!cp ('t79');
3300          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html');
3301          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
3302        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3303          ## Turn into the main phase          !!!cp ('t80');
3304          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html');
3305          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3306          } else {
3307            !!!cp ('t81');
3308        }        }
3309    
3310  ## ISSUE: "aa<html>" is not a parse error.        !!!cp ('t82');
3311  ## ISSUE: "<html>" in fragment is not a parse error.        !!!parse-error (type => 'not first start tag');
       unless ($token->{first_start_tag}) {  
         !!!parse-error (type => 'not first start tag');  
       }  
3312        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
3313        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
3314          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3315              !!!cp ('t84');
3316            $top_el->set_attribute_ns            $top_el->set_attribute_ns
3317              (undef, [undef, $attr_name],              (undef, [undef, $attr_name],
3318               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
# Line 2881  sub _tree_construction_main ($) { Line 3323  sub _tree_construction_main ($) {
3323      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3324        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3325        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3326            !!!cp ('t85');
3327          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3328        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
3329            !!!cp ('t86');
3330          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
3331        } else {        } else {
3332            !!!cp ('t87');
3333          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
3334        }        }
3335        !!!next-token;        !!!next-token;
# Line 2894  sub _tree_construction_main ($) { Line 3339  sub _tree_construction_main ($) {
3339          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3340            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3341            unless (length $token->{data}) {            unless (length $token->{data}) {
3342                !!!cp ('t88');
3343              !!!next-token;              !!!next-token;
3344              redo B;              redo B;
3345            }            }
3346          }          }
3347    
3348          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3349              !!!cp ('t89');
3350            ## As if <head>            ## As if <head>
3351            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, 'head');
3352            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
# Line 2910  sub _tree_construction_main ($) { Line 3357  sub _tree_construction_main ($) {
3357    
3358            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
3359          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3360              !!!cp ('t90');
3361            ## As if </noscript>            ## As if </noscript>
3362            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
3363            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character');
# Line 2920  sub _tree_construction_main ($) { Line 3368  sub _tree_construction_main ($) {
3368    
3369            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
3370          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3371              !!!cp ('t91');
3372            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
3373    
3374            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
3375            } else {
3376              !!!cp ('t92');
3377          }          }
3378    
3379              ## "after head" insertion mode              ## "after head" insertion mode
# Line 2934  sub _tree_construction_main ($) { Line 3385  sub _tree_construction_main ($) {
3385            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
3386              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
3387                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3388                    !!!cp ('t93');
3389                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
3390                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3391                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
# Line 2941  sub _tree_construction_main ($) { Line 3393  sub _tree_construction_main ($) {
3393                  !!!next-token;                  !!!next-token;
3394                  redo B;                  redo B;
3395                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3396                    !!!cp ('t94');
3397                  #                  #
3398                } else {                } else {
3399                    !!!cp ('t95');
3400                  !!!parse-error (type => 'in head:head'); # or in head noscript                  !!!parse-error (type => 'in head:head'); # or in head noscript
3401                  ## Ignore the token                  ## Ignore the token
3402                  !!!next-token;                  !!!next-token;
3403                  redo B;                  redo B;
3404                }                }
3405              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3406                  !!!cp ('t96');
3407                ## As if <head>                ## As if <head>
3408                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
3409                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
# Line 2956  sub _tree_construction_main ($) { Line 3411  sub _tree_construction_main ($) {
3411    
3412                $self->{insertion_mode} = IN_HEAD_IM;                $self->{insertion_mode} = IN_HEAD_IM;
3413                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
3414                } else {
3415                  !!!cp ('t97');
3416              }              }
3417    
3418              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
3419                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3420                    !!!cp ('t98');
3421                  ## As if </noscript>                  ## As if </noscript>
3422                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3423                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base');
3424                                
3425                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3426                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3427                  } else {
3428                    !!!cp ('t99');
3429                }                }
3430    
3431                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3432                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3433                    !!!cp ('t100');
3434                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3435                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3436                  } else {
3437                    !!!cp ('t101');
3438                }                }
3439                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3440                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
# Line 2982  sub _tree_construction_main ($) { Line 3445  sub _tree_construction_main ($) {
3445              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
3446                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3447                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3448                    !!!cp ('t102');
3449                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3450                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3451                  } else {
3452                    !!!cp ('t103');
3453                }                }
3454                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3455                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
# Line 2994  sub _tree_construction_main ($) { Line 3460  sub _tree_construction_main ($) {
3460              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
3461                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3462                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3463                    !!!cp ('t104');
3464                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3465                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3466                  } else {
3467                    !!!cp ('t105');
3468                }                }
3469                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3470                my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
3471    
3472                unless ($self->{confident}) {                unless ($self->{confident}) {
3473                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
3474                      !!!cp ('t106');
3475                    $self->{change_encoding}                    $self->{change_encoding}
3476                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value});
3477                                        
# Line 3016  sub _tree_construction_main ($) { Line 3486  sub _tree_construction_main ($) {
3486                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
3487                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
3488                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
3489                        !!!cp ('t107');
3490                      $self->{change_encoding}                      $self->{change_encoding}
3491                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
3492                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3493                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
3494                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
3495                                                     ->{has_reference});                                                     ->{has_reference});
3496                      } else {
3497                        !!!cp ('t108');
3498                    }                    }
3499                  }                  }
3500                } else {                } else {
3501                  if ($token->{attributes}->{charset}) {                  if ($token->{attributes}->{charset}) {
3502                      !!!cp ('t109');
3503                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
3504                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
3505                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
3506                                                 ->{has_reference});                                                 ->{has_reference});
3507                  }                  }
3508                  if ($token->{attributes}->{content}) {                  if ($token->{attributes}->{content}) {
3509                      !!!cp ('t110');
3510                    $meta_el->[0]->get_attribute_node_ns (undef, 'content')                    $meta_el->[0]->get_attribute_node_ns (undef, 'content')
3511                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
3512                                             $token->{attributes}->{content}                                             $token->{attributes}->{content}
# Line 3045  sub _tree_construction_main ($) { Line 3520  sub _tree_construction_main ($) {
3520                redo B;                redo B;
3521              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
3522                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3523                    !!!cp ('t111');
3524                  ## As if </noscript>                  ## As if </noscript>
3525                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3526                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title');
# Line 3052  sub _tree_construction_main ($) { Line 3528  sub _tree_construction_main ($) {
3528                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3529                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3530                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3531                    !!!cp ('t112');
3532                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3533                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3534                  } else {
3535                    !!!cp ('t113');
3536                }                }
3537    
3538                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
# Line 3069  sub _tree_construction_main ($) { Line 3548  sub _tree_construction_main ($) {
3548                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
3549                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3550                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3551                    !!!cp ('t114');
3552                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3553                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3554                  } else {
3555                    !!!cp ('t115');
3556                }                }
3557                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
3558                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
# Line 3078  sub _tree_construction_main ($) { Line 3560  sub _tree_construction_main ($) {
3560                redo B;                redo B;
3561              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
3562                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
3563                    !!!cp ('t116');
3564                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
3565                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3566                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
3567                  !!!next-token;                  !!!next-token;
3568                  redo B;                  redo B;
3569                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3570                    !!!cp ('t117');
3571                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript:noscript');
3572                  ## Ignore the token                  ## Ignore the token
3573                  !!!next-token;                  !!!next-token;
3574                  redo B;                  redo B;
3575                } else {                } else {
3576                    !!!cp ('t118');
3577                  #                  #
3578                }                }
3579              } elsif ($token->{tag_name} eq 'script') {              } elsif ($token->{tag_name} eq 'script') {
3580                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3581                    !!!cp ('t119');
3582                  ## As if </noscript>                  ## As if </noscript>
3583                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3584                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script');
# Line 3100  sub _tree_construction_main ($) { Line 3586  sub _tree_construction_main ($) {
3586                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3587                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3588                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3589                    !!!cp ('t120');
3590                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3591                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3592                  } else {
3593                    !!!cp ('t121');
3594                }                }
3595    
3596                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
# Line 3112  sub _tree_construction_main ($) { Line 3601  sub _tree_construction_main ($) {
3601              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
3602                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
3603                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3604                    !!!cp ('t122');
3605                  ## As if </noscript>                  ## As if </noscript>
3606                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3607                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});
# Line 3122  sub _tree_construction_main ($) { Line 3612  sub _tree_construction_main ($) {
3612                                    
3613                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
3614                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3615                    !!!cp ('t124');
3616                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3617                                    
3618                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
3619                  } else {
3620                    !!!cp ('t125');
3621                }                }
3622    
3623                ## "after head" insertion mode                ## "after head" insertion mode
3624                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3625                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
3626                    !!!cp ('t126');
3627                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
3628                } elsif ($token->{tag_name} eq 'frameset') {                } elsif ($token->{tag_name} eq 'frameset') {
3629                    !!!cp ('t127');
3630                  $self->{insertion_mode} = IN_FRAMESET_IM;                  $self->{insertion_mode} = IN_FRAMESET_IM;
3631                } else {                } else {
3632                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
# Line 3139  sub _tree_construction_main ($) { Line 3634  sub _tree_construction_main ($) {
3634                !!!next-token;                !!!next-token;
3635                redo B;                redo B;
3636              } else {              } else {
3637                  !!!cp ('t128');
3638                #                #
3639              }              }
3640    
3641              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3642                  !!!cp ('t129');
3643                ## As if </noscript>                ## As if </noscript>
3644                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3645                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
# Line 3153  sub _tree_construction_main ($) { Line 3650  sub _tree_construction_main ($) {
3650    
3651                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3652              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3653                  !!!cp ('t130');
3654                ## As if </head>                ## As if </head>
3655                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3656    
3657                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3658                } else {
3659                  !!!cp ('t131');
3660              }              }
3661    
3662              ## "after head" insertion mode              ## "after head" insertion mode
# Line 3168  sub _tree_construction_main ($) { Line 3668  sub _tree_construction_main ($) {
3668            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
3669              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
3670                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3671                    !!!cp ('t132');
3672                  ## As if <head>                  ## As if <head>
3673                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head');
3674                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
# Line 3179  sub _tree_construction_main ($) { Line 3680  sub _tree_construction_main ($) {
3680                  !!!next-token;                  !!!next-token;
3681                  redo B;                  redo B;
3682                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3683                    !!!cp ('t133');
3684                  ## As if </noscript>                  ## As if </noscript>
3685                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3686                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:/head');
3687                                    
3688                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3689                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 3189  sub _tree_construction_main ($) { Line 3691  sub _tree_construction_main ($) {
3691                  !!!next-token;                  !!!next-token;
3692                  redo B;                  redo B;
3693                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3694                    !!!cp ('t134');
3695                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3696                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
3697                  !!!next-token;                  !!!next-token;
3698                  redo B;                  redo B;
3699                } else {                } else {
3700                    !!!cp ('t135');
3701                  #                  #
3702                }                }
3703              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
3704                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3705                    !!!cp ('t136');
3706                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3707                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3708                  !!!next-token;                  !!!next-token;
3709                  redo B;                  redo B;
3710                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3711                    !!!cp ('t137');
3712                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!parse-error (type => 'unmatched end tag:noscript');
3713                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
3714                  !!!next-token;                  !!!next-token;
3715                  redo B;                  redo B;
3716                } else {                } else {
3717                    !!!cp ('t138');
3718                  #                  #
3719                }                }
3720              } elsif ({              } elsif ({
3721                        body => 1, html => 1,                        body => 1, html => 1,
3722                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3723                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3724                    !!!cp ('t139');
3725                  ## As if <head>                  ## As if <head>
3726                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head');
3727                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
# Line 3222  sub _tree_construction_main ($) { Line 3730  sub _tree_construction_main ($) {
3730                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3731                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3732                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3733                    !!!cp ('t140');
3734                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3735                  ## Ignore the token                  ## Ignore the token
3736                  !!!next-token;                  !!!next-token;
3737                  redo B;                  redo B;
3738                  } else {
3739                    !!!cp ('t141');
3740                }                }
3741                                
3742                #                #
# Line 3233  sub _tree_construction_main ($) { Line 3744  sub _tree_construction_main ($) {
3744                        p => 1, br => 1,                        p => 1, br => 1,
3745                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3746                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3747                    !!!cp ('t142');
3748                  ## As if <head>                  ## As if <head>
3749                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head');
3750                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
# Line 3240  sub _tree_construction_main ($) { Line 3752  sub _tree_construction_main ($) {
3752    
3753                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
3754                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3755                  } else {
3756                    !!!cp ('t143');
3757                }                }
3758    
3759                #                #
3760              } else {              } else {
3761                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3762                    !!!cp ('t144');
3763                  #                  #
3764                } else {                } else {
3765                    !!!cp ('t145');
3766                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3767                  ## Ignore the token                  ## Ignore the token
3768                  !!!next-token;                  !!!next-token;
# Line 3255  sub _tree_construction_main ($) { Line 3771  sub _tree_construction_main ($) {
3771              }              }
3772    
3773              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3774                  !!!cp ('t146');
3775                ## As if </noscript>                ## As if </noscript>
3776                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3777                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
# Line 3265  sub _tree_construction_main ($) { Line 3782  sub _tree_construction_main ($) {
3782    
3783                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3784              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3785                  !!!cp ('t147');
3786                ## As if </head>                ## As if </head>
3787                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3788    
3789                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3790              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3791    ## ISSUE: This case cannot be reached?
3792                  !!!cp ('t148');
3793                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3794                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
3795                !!!next-token;                !!!next-token;
3796                redo B;                redo B;
3797                } else {
3798                  !!!cp ('t149');
3799              }              }
3800    
3801              ## "after head" insertion mode              ## "after head" insertion mode
# Line 3289  sub _tree_construction_main ($) { Line 3811  sub _tree_construction_main ($) {
3811            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
3812      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
3813            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
3814                !!!cp ('t150');
3815              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
3816              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
3817                            
# Line 3307  sub _tree_construction_main ($) { Line 3830  sub _tree_construction_main ($) {
3830                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3831                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
3832                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {
3833                        !!!cp ('t151');
3834                      $tn = $node->[1];                      $tn = $node->[1];
3835                      last INSCOPE;                      last INSCOPE;
3836                    } elsif ({                    } elsif ({
3837                              table => 1, html => 1,                              table => 1, html => 1,
3838                             }->{$node->[1]}) {                             }->{$node->[1]}) {
3839                        !!!cp ('t152');
3840                      last INSCOPE;                      last INSCOPE;
3841                    }                    }
3842                  } # INSCOPE                  } # INSCOPE
3843                    unless (defined $tn) {                    unless (defined $tn) {
3844                        !!!cp ('t153');
3845    ## TODO: This error type is wrong.
3846                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3847                      ## Ignore the token                      ## Ignore the token
3848                      !!!next-token;                      !!!next-token;
3849                      redo B;                      redo B;
3850                    }                    }
3851                                    
3852                    !!!cp ('t154');
3853                  ## Close the cell                  ## Close the cell
3854                  !!!back-token; # <?>                  !!!back-token; # <?>
3855                  $token = {type => END_TAG_TOKEN, tag_name => $tn};                  $token = {type => END_TAG_TOKEN, tag_name => $tn};
# Line 3335  sub _tree_construction_main ($) { Line 3863  sub _tree_construction_main ($) {
3863                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3864                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
3865                    if ($node->[1] eq 'caption') {                    if ($node->[1] eq 'caption') {
3866                        !!!cp ('t155');
3867                      $i = $_;                      $i = $_;
3868                      last INSCOPE;                      last INSCOPE;
3869                    } elsif ({                    } elsif ({
3870                              table => 1, html => 1,                              table => 1, html => 1,
3871                             }->{$node->[1]}) {                             }->{$node->[1]}) {
3872                        !!!cp ('t156');
3873                      last INSCOPE;                      last INSCOPE;
3874                    }                    }
3875                  } # INSCOPE                  } # INSCOPE
3876                    unless (defined $i) {                    unless (defined $i) {
3877                        !!!cp ('t157');
3878    ## TODO: this type is wrong.
3879                      !!!parse-error (type => 'unmatched end tag:caption');                      !!!parse-error (type => 'unmatched end tag:caption');
3880                      ## Ignore the token                      ## Ignore the token
3881                      !!!next-token;                      !!!next-token;
# Line 3351  sub _tree_construction_main ($) { Line 3883  sub _tree_construction_main ($) {
3883                    }                    }
3884                                    
3885                  ## generate implied end tags                  ## generate implied end tags
3886                  if ({                  while ({
3887                       dd => 1, dt => 1, li => 1, p => 1,                          dd => 1, dt => 1, li => 1, p => 1,
3888                       td => 1, th => 1, tr => 1,                         }->{$self->{open_elements}->[-1]->[1]}) {
3889                       tbody => 1, tfoot=> 1, thead => 1,                    !!!cp ('t158');
3890                      }->{$self->{open_elements}->[-1]->[1]}) {                    pop @{$self->{open_elements}};
                   !!!back-token; # <?>  
                   $token = {type => END_TAG_TOKEN, tag_name => 'caption'};  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
3891                  }                  }
3892    
3893                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
3894                      !!!cp ('t159');
3895                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3896                    } else {
3897                      !!!cp ('t160');
3898                  }                  }
3899                                    
3900                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3377  sub _tree_construction_main ($) { Line 3906  sub _tree_construction_main ($) {
3906                  ## reprocess                  ## reprocess
3907                  redo B;                  redo B;
3908                } else {                } else {
3909                    !!!cp ('t161');
3910                  #                  #
3911                }                }
3912              } else {              } else {
3913                  !!!cp ('t162');
3914                #                #
3915              }              }
3916            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
# Line 3390  sub _tree_construction_main ($) { Line 3921  sub _tree_construction_main ($) {
3921                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3922                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
3923                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[1] eq $token->{tag_name}) {
3924                        !!!cp ('t163');
3925                      $i = $_;                      $i = $_;
3926                      last INSCOPE;                      last INSCOPE;
3927                    } elsif ({                    } elsif ({
3928                              table => 1, html => 1,                              table => 1, html => 1,
3929                             }->{$node->[1]}) {                             }->{$node->[1]}) {
3930                        !!!cp ('t164');
3931                      last INSCOPE;                      last INSCOPE;
3932                    }                    }
3933                  } # INSCOPE                  } # INSCOPE
3934                    unless (defined $i) {                    unless (defined $i) {
3935                        !!!cp ('t165');
3936                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3937                      ## Ignore the token                      ## Ignore the token
3938                      !!!next-token;                      !!!next-token;
# Line 3406  sub _tree_construction_main ($) { Line 3940  sub _tree_construction_main ($) {
3940                    }                    }
3941                                    
3942                  ## generate implied end tags                  ## generate implied end tags
3943                  if ({                  while ({
3944                       dd => 1, dt => 1, li => 1, p => 1,                          dd => 1, dt => 1, li => 1, p => 1,
3945                       td => ($token->{tag_name} eq 'th'),                         }->{$self->{open_elements}->[-1]->[1]}) {
3946                       th => ($token->{tag_name} eq 'td'),                    !!!cp ('t166');
3947                       tr => 1,                    pop @{$self->{open_elements}};
                      tbody => 1, tfoot=> 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
3948                  }                  }
3949                    
3950                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
3951                      !!!cp ('t167');
3952                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3953                    } else {
3954                      !!!cp ('t168');
3955                  }                  }
3956                                    
3957                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3432  sub _tree_construction_main ($) { Line 3963  sub _tree_construction_main ($) {
3963                  !!!next-token;                  !!!next-token;
3964                  redo B;                  redo B;
3965                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3966                    !!!cp ('t169');
3967                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3968                  ## Ignore the token                  ## Ignore the token
3969                  !!!next-token;                  !!!next-token;
3970                  redo B;                  redo B;
3971                } else {                } else {
3972                    !!!cp ('t170');
3973                  #                  #
3974                }                }
3975              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
# Line 3446  sub _tree_construction_main ($) { Line 3979  sub _tree_construction_main ($) {
3979                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
3980                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
3981                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[1] eq $token->{tag_name}) {
3982                        !!!cp ('t171');
3983                      $i = $_;                      $i = $_;
3984                      last INSCOPE;                      last INSCOPE;
3985                    } elsif ({                    } elsif ({
3986                              table => 1, html => 1,                              table => 1, html => 1,
3987                             }->{$node->[1]}) {                             }->{$node->[1]}) {
3988                        !!!cp ('t172');
3989                      last INSCOPE;                      last INSCOPE;
3990                    }                    }
3991                  } # INSCOPE                  } # INSCOPE
3992                    unless (defined $i) {                    unless (defined $i) {
3993                        !!!cp ('t173');
3994                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3995                      ## Ignore the token                      ## Ignore the token
3996                      !!!next-token;                      !!!next-token;
# Line 3462  sub _tree_construction_main ($) { Line 3998  sub _tree_construction_main ($) {
3998                    }                    }
3999                                    
4000                  ## generate implied end tags                  ## generate implied end tags
4001                  if ({                  while ({
4002                       dd => 1, dt => 1, li => 1, p => 1,                          dd => 1, dt => 1, li => 1, p => 1,
4003                       td => 1, th => 1, tr => 1,                         }->{$self->{open_elements}->[-1]->[1]}) {
4004                       tbody => 1, tfoot=> 1, thead => 1,                    !!!cp ('t174');
4005                      }->{$self->{open_elements}->[-1]->[1]}) {                    pop @{$self->{open_elements}};
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
4006                  }                  }
4007                                    
4008                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4009                      !!!cp ('t175');
4010                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4011                    } else {
4012                      !!!cp ('t176');
4013                  }                  }
4014                                    
4015                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3486  sub _tree_construction_main ($) { Line 4021  sub _tree_construction_main ($) {
4021                  !!!next-token;                  !!!next-token;
4022                  redo B;                  redo B;
4023                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4024                    !!!cp ('t177');
4025                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4026                  ## Ignore the token                  ## Ignore the token
4027                  !!!next-token;                  !!!next-token;
4028                  redo B;                  redo B;
4029                } else {                } else {
4030                    !!!cp ('t178');
4031                  #                  #
4032                }                }
4033              } elsif ({              } elsif ({
# Line 3504  sub _tree_construction_main ($) { Line 4041  sub _tree_construction_main ($) {
4041                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4042                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4043                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4044                      !!!cp ('t179');
4045                    $i = $_;                    $i = $_;
4046                    last INSCOPE;                    last INSCOPE;
4047                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {
4048                      !!!cp ('t180');
4049                    $tn = $node->[1];                    $tn = $node->[1];
4050                    ## NOTE: There is exactly one |td| or |th| element                    ## NOTE: There is exactly one |td| or |th| element
4051                    ## in scope in the stack of open elements by definition.                    ## in scope in the stack of open elements by definition.
4052                  } elsif ({                  } elsif ({
4053                            table => 1, html => 1,                            table => 1, html => 1,
4054                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4055                      !!!cp ('t181');
4056                    last INSCOPE;                    last INSCOPE;
4057                  }                  }
4058                } # INSCOPE                } # INSCOPE
4059                unless (defined $i) {                unless (defined $i) {
4060                    !!!cp ('t182');
4061                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4062                  ## Ignore the token                  ## Ignore the token
4063                  !!!next-token;                  !!!next-token;
4064                  redo B;                  redo B;
4065                  } else {
4066                    !!!cp ('t183');
4067                }                }
4068    
4069                ## Close the cell                ## Close the cell
# Line 3537  sub _tree_construction_main ($) { Line 4080  sub _tree_construction_main ($) {
4080                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4081                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4082                  if ($node->[1] eq 'caption') {                  if ($node->[1] eq 'caption') {
4083                      !!!cp ('t184');
4084                    $i = $_;                    $i = $_;
4085                    last INSCOPE;                    last INSCOPE;
4086                  } elsif ({                  } elsif ({
4087                            table => 1, html => 1,                            table => 1, html => 1,
4088                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4089                      !!!cp ('t185');
4090                    last INSCOPE;                    last INSCOPE;
4091                  }                  }
4092                } # INSCOPE                } # INSCOPE
4093                unless (defined $i) {                unless (defined $i) {
4094                    !!!cp ('t186');
4095                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'unmatched end tag:caption');
4096                  ## Ignore the token                  ## Ignore the token
4097                  !!!next-token;                  !!!next-token;
# Line 3553  sub _tree_construction_main ($) { Line 4099  sub _tree_construction_main ($) {
4099                }                }
4100                                
4101                ## generate implied end tags                ## generate implied end tags
4102                if ({                while ({
4103                     dd => 1, dt => 1, li => 1, p => 1,                        dd => 1, dt => 1, li => 1, p => 1,
4104                     td => 1, th => 1, tr => 1,                       }->{$self->{open_elements}->[-1]->[1]}) {
4105                     tbody => 1, tfoot=> 1, thead => 1,                  !!!cp ('t187');
4106                    }->{$self->{open_elements}->[-1]->[1]}) {                  pop @{$self->{open_elements}};
                 !!!back-token; # </table>  
                 $token = {type => END_TAG_TOKEN, tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
4107                }                }
4108    
4109                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                if ($self->{open_elements}->[-1]->[1] ne 'caption') {
4110                    !!!cp ('t188');
4111                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4112                  } else {
4113                    !!!cp ('t189');
4114                }                }
4115    
4116                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 3582  sub _tree_construction_main ($) { Line 4125  sub _tree_construction_main ($) {
4125                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
4126                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4127                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4128                    !!!cp ('t190');
4129                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4130                  ## Ignore the token                  ## Ignore the token
4131                  !!!next-token;                  !!!next-token;
4132                  redo B;                  redo B;
4133                } else {                } else {
4134                    !!!cp ('t191');
4135                  #                  #
4136                }                }
4137              } elsif ({              } elsif ({
# Line 3594  sub _tree_construction_main ($) { Line 4139  sub _tree_construction_main ($) {
4139                        thead => 1, tr => 1,                        thead => 1, tr => 1,
4140                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
4141                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4142                  !!!cp ('t192');
4143                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4144                ## Ignore the token                ## Ignore the token
4145                !!!next-token;                !!!next-token;
4146                redo B;                redo B;
4147              } else {              } else {
4148                  !!!cp ('t193');
4149                #                #
4150              }              }
4151        } else {        } else {
# Line 3613  sub _tree_construction_main ($) { Line 4160  sub _tree_construction_main ($) {
4160                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4161                                
4162                unless (length $token->{data}) {                unless (length $token->{data}) {
4163                    !!!cp ('t194');
4164                  !!!next-token;                  !!!next-token;
4165                  redo B;                  redo B;
4166                  } else {
4167                    !!!cp ('t195');
4168                }                }
4169              }              }
4170    
# Line 3638  sub _tree_construction_main ($) { Line 4188  sub _tree_construction_main ($) {
4188                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] eq 'table') {
4189                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4190                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
4191                        !!!cp ('t196');
4192                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
4193                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
4194                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
4195                    } else {                    } else {
4196                        !!!cp ('t197');
4197                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
4198                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
4199                    }                    }
# Line 3653  sub _tree_construction_main ($) { Line 4205  sub _tree_construction_main ($) {
4205                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
4206                if (defined $prev_sibling and                if (defined $prev_sibling and
4207                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
4208                    !!!cp ('t198');
4209                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
4210                } else {                } else {
4211                    !!!cp ('t199');
4212                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
4213                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
4214                     $next_sibling);                     $next_sibling);
4215                }                }
4216              } else {              } else {
4217                  !!!cp ('t200');
4218                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4219              }              }
4220                            
# Line 3674  sub _tree_construction_main ($) { Line 4229  sub _tree_construction_main ($) {
4229                  ## Clear back to table context                  ## Clear back to table context
4230                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
4231                         $self->{open_elements}->[-1]->[1] ne 'html') {                         $self->{open_elements}->[-1]->[1] ne 'html') {
4232                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t201');
4233                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4234                  }                  }
4235                                    
# Line 3685  sub _tree_construction_main ($) { Line 4240  sub _tree_construction_main ($) {
4240    
4241                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
4242                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
4243                      !!!cp ('t202');
4244                    !!!parse-error (type => 'missing start tag:tr');                    !!!parse-error (type => 'missing start tag:tr');
4245                  }                  }
4246                                    
# Line 3692  sub _tree_construction_main ($) { Line 4248  sub _tree_construction_main ($) {
4248                  while (not {                  while (not {
4249                    tbody => 1, tfoot => 1, thead => 1, html => 1,                    tbody => 1, tfoot => 1, thead => 1, html => 1,
4250                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4251                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t203');
4252                      ## ISSUE: Can this case be reached?
4253                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4254                  }                  }
4255                                    
4256                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4257                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
4258                      !!!cp ('t204');
4259                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes});
4260                    !!!next-token;                    !!!next-token;
4261                    redo B;                    redo B;
4262                  } else {                  } else {
4263                      !!!cp ('t205');
4264                    !!!insert-element ('tr');                    !!!insert-element ('tr');
4265                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
4266                  }                  }
4267                  } else {
4268                    !!!cp ('t206');
4269                }                }
4270    
4271                ## Clear back to table row context                ## Clear back to table row context
4272                while (not {                while (not {
4273                  tr => 1, html => 1,                  tr => 1, html => 1,
4274                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4275                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t207');
4276                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4277                }                }
4278                                
# Line 3734  sub _tree_construction_main ($) { Line 4295  sub _tree_construction_main ($) {
4295                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4296                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4297                    if ($node->[1] eq 'tr') {                    if ($node->[1] eq 'tr') {
4298                        !!!cp ('t208');
4299                      $i = $_;                      $i = $_;
4300                      last INSCOPE;                      last INSCOPE;
4301                    } elsif ({                    } elsif ({
4302                              table => 1, html => 1,                              html => 1,
4303    
4304                                ## NOTE: This element does not appear here, maybe.
4305                                table => 1,
4306                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4307                        !!!cp ('t209');
4308                      last INSCOPE;                      last INSCOPE;
4309                    }                    }
4310                  } # INSCOPE                  } # INSCOPE
4311                  unless (defined $i) {                  unless (defined $i) {
4312                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                   !!!cp ('t210');
4313    ## TODO: This type is wrong.
4314                     !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});
4315                    ## Ignore the token                    ## Ignore the token
4316                    !!!next-token;                    !!!next-token;
4317                    redo B;                    redo B;
# Line 3753  sub _tree_construction_main ($) { Line 4321  sub _tree_construction_main ($) {
4321                  while (not {                  while (not {
4322                    tr => 1, html => 1,                    tr => 1, html => 1,
4323                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4324                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t211');
4325                      ## ISSUE: Can this case be reached?
4326                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4327                  }                  }
4328                                    
4329                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
4330                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4331                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
4332                      !!!cp ('t212');
4333                    ## reprocess                    ## reprocess
4334                    redo B;                    redo B;
4335                  } else {                  } else {
4336                      !!!cp ('t213');
4337                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
4338                  }                  }
4339                }                }
# Line 3775  sub _tree_construction_main ($) { Line 4346  sub _tree_construction_main ($) {
4346                    if ({                    if ({
4347                         tbody => 1, thead => 1, tfoot => 1,                         tbody => 1, thead => 1, tfoot => 1,
4348                        }->{$node->[1]}) {                        }->{$node->[1]}) {
4349                        !!!cp ('t214');
4350                      $i = $_;                      $i = $_;
4351                      last INSCOPE;                      last INSCOPE;
4352                    } elsif ({                    } elsif ({
4353                              table => 1, html => 1,                              table => 1, html => 1,
4354                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4355                        !!!cp ('t215');
4356                      last INSCOPE;                      last INSCOPE;
4357                    }                    }
4358                  } # INSCOPE                  } # INSCOPE
4359                  unless (defined $i) {                  unless (defined $i) {
4360                      !!!cp ('t216');
4361    ## TODO: This erorr type ios wrong.
4362                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4363                    ## Ignore the token                    ## Ignore the token
4364                    !!!next-token;                    !!!next-token;
# Line 3794  sub _tree_construction_main ($) { Line 4369  sub _tree_construction_main ($) {
4369                  while (not {                  while (not {
4370                    tbody => 1, tfoot => 1, thead => 1, html => 1,                    tbody => 1, tfoot => 1, thead => 1, html => 1,
4371                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4372                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t217');
4373                      ## ISSUE: Can this state be reached?
4374                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4375                  }                  }
4376                                    
# Line 3808  sub _tree_construction_main ($) { Line 4384  sub _tree_construction_main ($) {
4384                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4385                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4386                  ## reprocess in "in table" insertion mode...                  ## reprocess in "in table" insertion mode...
4387                  } else {
4388                    !!!cp ('t218');
4389                }                }
4390    
4391                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
4392                  ## Clear back to table context                  ## Clear back to table context
4393                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
4394                         $self->{open_elements}->[-1]->[1] ne 'html') {                         $self->{open_elements}->[-1]->[1] ne 'html') {
4395                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t219');
4396                      ## ISSUE: Can this state be reached?
4397                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4398                  }                  }
4399                                    
# Line 3830  sub _tree_construction_main ($) { Line 4409  sub _tree_construction_main ($) {
4409                  ## Clear back to table context                  ## Clear back to table context
4410                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
4411                         $self->{open_elements}->[-1]->[1] ne 'html') {                         $self->{open_elements}->[-1]->[1] ne 'html') {
4412                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t220');
4413                      ## ISSUE: Can this state be reached?
4414                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4415                  }                  }
4416                                    
# Line 3859  sub _tree_construction_main ($) { Line 4439  sub _tree_construction_main ($) {
4439                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4440                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4441                  if ($node->[1] eq 'table') {                  if ($node->[1] eq 'table') {
4442                      !!!cp ('t221');
4443                    $i = $_;                    $i = $_;
4444                    last INSCOPE;                    last INSCOPE;
4445                  } elsif ({                  } elsif ({
4446                            table => 1, html => 1,                            #table => 1,
4447                              html => 1,
4448                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4449                      !!!cp ('t222');
4450                    last INSCOPE;                    last INSCOPE;
4451                  }                  }
4452                } # INSCOPE                } # INSCOPE
4453                unless (defined $i) {                unless (defined $i) {
4454                    !!!cp ('t223');
4455    ## TODO: The following is wrong, maybe.
4456                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag:table');
4457                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
4458                  !!!next-token;                  !!!next-token;
# Line 3875  sub _tree_construction_main ($) { Line 4460  sub _tree_construction_main ($) {
4460                }                }
4461                                
4462                ## generate implied end tags                ## generate implied end tags
4463                if ({                while ({
4464                     dd => 1, dt => 1, li => 1, p => 1,                        dd => 1, dt => 1, li => 1, p => 1,
4465                     td => 1, th => 1, tr => 1,                       }->{$self->{open_elements}->[-1]->[1]}) {
4466                     tbody => 1, tfoot=> 1, thead => 1,                  !!!cp ('t224');
4467                    }->{$self->{open_elements}->[-1]->[1]}) {                  pop @{$self->{open_elements}};
                 !!!back-token; # <table>  
                 $token = {type => END_TAG_TOKEN, tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
4468                }                }
4469    
4470                if ($self->{open_elements}->[-1]->[1] ne 'table') {                if ($self->{open_elements}->[-1]->[1] ne 'table') {
4471                    !!!cp ('t225');
4472    ## ISSUE: Can this case be reached?
4473                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
4474                  } else {
4475                    !!!cp ('t226');
4476                }                }
4477    
4478                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 3899  sub _tree_construction_main ($) { Line 4482  sub _tree_construction_main ($) {
4482                ## reprocess                ## reprocess
4483                redo B;                redo B;
4484          } else {          } else {
4485              !!!cp ('t227');
4486            !!!parse-error (type => 'in table:'.$token->{tag_name});            !!!parse-error (type => 'in table:'.$token->{tag_name});
4487    
4488            $insert = $insert_to_foster;            $insert = $insert_to_foster;
# Line 3912  sub _tree_construction_main ($) { Line 4496  sub _tree_construction_main ($) {
4496                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4497                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4498                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4499                      !!!cp ('t228');
4500                    $i = $_;                    $i = $_;
4501                    last INSCOPE;                    last INSCOPE;
4502                  } elsif ({                  } elsif ({
4503                            table => 1, html => 1,                            table => 1, html => 1,
4504                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4505                      !!!cp ('t229');
4506                    last INSCOPE;                    last INSCOPE;
4507                  }                  }
4508                } # INSCOPE                } # INSCOPE
4509                unless (defined $i) {                unless (defined $i) {
4510                    !!!cp ('t230');
4511                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4512                  ## Ignore the token                  ## Ignore the token
4513                  !!!next-token;                  !!!next-token;
4514                  redo B;                  redo B;
4515                  } else {
4516                    !!!cp ('t232');
4517                }                }
4518    
4519                ## Clear back to table row context                ## Clear back to table row context
4520                while (not {                while (not {
4521                  tr => 1, html => 1,                  tr => 1, html => 1,
4522                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4523                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t231');
4524    ## ISSUE: Can this state be reached?
4525                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4526                }                }
4527    
# Line 3947  sub _tree_construction_main ($) { Line 4537  sub _tree_construction_main ($) {
4537                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4538                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4539                    if ($node->[1] eq 'tr') {                    if ($node->[1] eq 'tr') {
4540                        !!!cp ('t233');
4541                      $i = $_;                      $i = $_;
4542                      last INSCOPE;                      last INSCOPE;
4543                    } elsif ({                    } elsif ({
4544                              table => 1, html => 1,                              table => 1, html => 1,
4545                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4546                        !!!cp ('t234');
4547                      last INSCOPE;                      last INSCOPE;
4548                    }                    }
4549                  } # INSCOPE                  } # INSCOPE
4550                  unless (defined $i) {                  unless (defined $i) {
4551                      !!!cp ('t235');
4552    ## TODO: The following is wrong.
4553                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});
4554                    ## Ignore the token                    ## Ignore the token
4555                    !!!next-token;                    !!!next-token;
# Line 3966  sub _tree_construction_main ($) { Line 4560  sub _tree_construction_main ($) {
4560                  while (not {                  while (not {
4561                    tr => 1, html => 1,                    tr => 1, html => 1,
4562                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4563                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t236');
4564    ## ISSUE: Can this state be reached?
4565                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4566                  }                  }
4567                                    
# Line 3983  sub _tree_construction_main ($) { Line 4578  sub _tree_construction_main ($) {
4578                    if ({                    if ({
4579                         tbody => 1, thead => 1, tfoot => 1,                         tbody => 1, thead => 1, tfoot => 1,
4580                        }->{$node->[1]}) {                        }->{$node->[1]}) {
4581                        !!!cp ('t237');
4582                      $i = $_;                      $i = $_;
4583                      last INSCOPE;                      last INSCOPE;
4584                    } elsif ({                    } elsif ({
4585                              table => 1, html => 1,                              table => 1, html => 1,
4586                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4587                        !!!cp ('t238');
4588                      last INSCOPE;                      last INSCOPE;
4589                    }                    }
4590                  } # INSCOPE                  } # INSCOPE
4591                  unless (defined $i) {                  unless (defined $i) {
4592                      !!!cp ('t239');
4593                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4594                    ## Ignore the token                    ## Ignore the token
4595                    !!!next-token;                    !!!next-token;
# Line 4002  sub _tree_construction_main ($) { Line 4600  sub _tree_construction_main ($) {
4600                  while (not {                  while (not {
4601                    tbody => 1, tfoot => 1, thead => 1, html => 1,                    tbody => 1, tfoot => 1, thead => 1, html => 1,
4602                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4603                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t240');
4604                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4605                  }                  }
4606                                    
# Line 4018  sub _tree_construction_main ($) { Line 4616  sub _tree_construction_main ($) {
4616                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
4617                }                }
4618    
4619                  ## NOTE: </table> in the "in table" insertion mode.
4620                  ## When you edit the code fragment below, please ensure that
4621                  ## the code for <table> in the "in table" insertion mode
4622                  ## is synced with it.
4623    
4624                ## have a table element in table scope                ## have a table element in table scope
4625                my $i;                my $i;
4626                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4627                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4628                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4629                      !!!cp ('t241');
4630                    $i = $_;                    $i = $_;
4631                    last INSCOPE;                    last INSCOPE;
4632                  } elsif ({                  } elsif ({
4633                            table => 1, html => 1,                            table => 1, html => 1,
4634                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4635                      !!!cp ('t242');
4636                    last INSCOPE;                    last INSCOPE;
4637                  }                  }
4638                } # INSCOPE                } # INSCOPE
4639                unless (defined $i) {                unless (defined $i) {
4640                    !!!cp ('t243');
4641                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4642                  ## Ignore the token                  ## Ignore the token
4643                  !!!next-token;                  !!!next-token;
4644                  redo B;                  redo B;
4645                }                }
   
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           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]);  
               }  
4646                                    
4647                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4648                                
# Line 4070  sub _tree_construction_main ($) { Line 4660  sub _tree_construction_main ($) {
4660                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4661                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4662                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[1] eq $token->{tag_name}) {
4663                        !!!cp ('t247');
4664                      $i = $_;                      $i = $_;
4665                      last INSCOPE;                      last INSCOPE;
4666                    } elsif ({                    } elsif ({
4667                              table => 1, html => 1,                              table => 1, html => 1,
4668                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4669                        !!!cp ('t248');
4670                      last INSCOPE;                      last INSCOPE;
4671                    }                    }
4672                  } # INSCOPE                  } # INSCOPE
4673                    unless (defined $i) {                    unless (defined $i) {
4674                        !!!cp ('t249');
4675                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4676                      ## Ignore the token                      ## Ignore the token
4677                      !!!next-token;                      !!!next-token;
# Line 4091  sub _tree_construction_main ($) { Line 4684  sub _tree_construction_main ($) {
4684                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4685                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4686                    if ($node->[1] eq 'tr') {                    if ($node->[1] eq 'tr') {
4687                        !!!cp ('t250');
4688                      $i = $_;                      $i = $_;
4689                      last INSCOPE;                      last INSCOPE;
4690                    } elsif ({                    } elsif ({
4691                              table => 1, html => 1,                              table => 1, html => 1,
4692                             }->{$node->[1]}) {                             }->{$node->[1]}) {
4693                        !!!cp ('t251');
4694                      last INSCOPE;                      last INSCOPE;
4695                    }                    }
4696                  } # INSCOPE                  } # INSCOPE
4697                    unless (defined $i) {                    unless (defined $i) {
4698                        !!!cp ('t252');
4699                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!parse-error (type => 'unmatched end tag:tr');
4700                      ## Ignore the token                      ## Ignore the token
4701                      !!!next-token;                      !!!next-token;
# Line 4110  sub _tree_construction_main ($) { Line 4706  sub _tree_construction_main ($) {
4706                  while (not {                  while (not {
4707                    tr => 1, html => 1,                    tr => 1, html => 1,
4708                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
4709                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t253');
4710    ## ISSUE: Can this case be reached?
4711                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
4712                  }                  }
4713                                    
# Line 4124  sub _tree_construction_main ($) { Line 4721  sub _tree_construction_main ($) {
4721                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4722                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4723                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4724                      !!!cp ('t254');
4725                    $i = $_;                    $i = $_;
4726                    last INSCOPE;                    last INSCOPE;
4727                  } elsif ({                  } elsif ({
4728                            table => 1, html => 1,                            table => 1, html => 1,
4729                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4730                      !!!cp ('t255');
4731                    last INSCOPE;                    last INSCOPE;
4732                  }                  }
4733                } # INSCOPE                } # INSCOPE
4734                unless (defined $i) {                unless (defined $i) {
4735                    !!!cp ('t256');
4736                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4737                  ## Ignore the token                  ## Ignore the token
4738                  !!!next-token;                  !!!next-token;
# Line 4143  sub _tree_construction_main ($) { Line 4743  sub _tree_construction_main ($) {
4743                while (not {                while (not {
4744                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  tbody => 1, tfoot => 1, thead => 1, html => 1,
4745                }->{$self->{open_elements}->[-1]->[1]}) {                }->{$self->{open_elements}->[-1]->[1]}) {
4746                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t257');
4747    ## ISSUE: Can this case be reached?
4748                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4749                }                }
4750    
# Line 4157  sub _tree_construction_main ($) { Line 4758  sub _tree_construction_main ($) {
4758                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4759                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
4760                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4761                  !!!cp ('t258');
4762                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4763                ## Ignore the token                ## Ignore the token
4764                !!!next-token;                !!!next-token;
4765                redo B;                redo B;
4766          } else {          } else {
4767              !!!cp ('t259');
4768            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!parse-error (type => 'in table:/'.$token->{tag_name});
4769    
4770            $insert = $insert_to_foster;            $insert = $insert_to_foster;
# Line 4175  sub _tree_construction_main ($) { Line 4778  sub _tree_construction_main ($) {
4778              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4779                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4780                unless (length $token->{data}) {                unless (length $token->{data}) {
4781                    !!!cp ('t260');
4782                  !!!next-token;                  !!!next-token;
4783                  redo B;                  redo B;
4784                }                }
4785              }              }
4786                            
4787                !!!cp ('t261');
4788              #              #
4789            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4790              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
4791                  !!!cp ('t262');
4792                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
4793                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4794                !!!next-token;                !!!next-token;
4795                redo B;                redo B;
4796              } else {              } else {
4797                  !!!cp ('t263');
4798                #                #
4799              }              }
4800            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4801              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
4802                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] eq 'html') {
4803                    !!!cp ('t264');
4804                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag:colgroup');
4805                  ## Ignore the token                  ## Ignore the token
4806                  !!!next-token;                  !!!next-token;
4807                  redo B;                  redo B;
4808                } else {                } else {
4809                    !!!cp ('t265');
4810                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
4811                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4812                  !!!next-token;                  !!!next-token;
4813                  redo B;                              redo B;            
4814                }                }
4815              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
4816                  !!!cp ('t266');
4817                !!!parse-error (type => 'unmatched end tag:col');                !!!parse-error (type => 'unmatched end tag:col');
4818                ## Ignore the token                ## Ignore the token
4819                !!!next-token;                !!!next-token;
4820                redo B;                redo B;
4821              } else {              } else {
4822                  !!!cp ('t267');
4823                #                #
4824              }              }
4825            } else {            } else {
4826              #              die "$0: $token->{type}: Unknown token type";
4827            }            }
4828    
4829            ## As if </colgroup>            ## As if </colgroup>
4830            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] eq 'html') {
4831                !!!cp ('t269');
4832              !!!parse-error (type => 'unmatched end tag:colgroup');              !!!parse-error (type => 'unmatched end tag:colgroup');
4833              ## Ignore the token              ## Ignore the token
4834              !!!next-token;              !!!next-token;
4835              redo B;              redo B;
4836            } else {            } else {
4837                !!!cp ('t270');
4838              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
4839              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
4840              ## reprocess              ## reprocess
# Line 4229  sub _tree_construction_main ($) { Line 4842  sub _tree_construction_main ($) {
4842            }            }
4843      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
4844        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4845            !!!cp ('t271');
4846          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4847          !!!next-token;          !!!next-token;
4848          redo B;          redo B;
4849        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4850              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
4851                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4852                    !!!cp ('t272');
4853                  ## As if </option>                  ## As if </option>
4854                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4855                  } else {
4856                    !!!cp ('t273');
4857                }                }
4858    
4859                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
# Line 4244  sub _tree_construction_main ($) { Line 4861  sub _tree_construction_main ($) {
4861                redo B;                redo B;
4862              } elsif ($token->{tag_name} eq 'optgroup') {              } elsif ($token->{tag_name} eq 'optgroup') {
4863                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4864                    !!!cp ('t274');
4865                  ## As if </option>                  ## As if </option>
4866                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4867                  } else {
4868                    !!!cp ('t275');
4869                }                }
4870    
4871                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
4872                    !!!cp ('t276');
4873                  ## As if </optgroup>                  ## As if </optgroup>
4874                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4875                  } else {
4876                    !!!cp ('t277');
4877                }                }
4878    
4879                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
4880                !!!next-token;                !!!next-token;
4881                redo B;                redo B;
4882              } elsif ($token->{tag_name} eq 'select') {              } elsif ($token->{tag_name} eq 'select') {
4883    ## TODO: The type below is not good - <select> is replaced by </select>
4884                !!!parse-error (type => 'not closed:select');                !!!parse-error (type => 'not closed:select');
4885                ## As if </select> instead                ## As if </select> instead
4886                ## have an element in table scope                ## have an element in table scope
# Line 4264  sub _tree_construction_main ($) { Line 4888  sub _tree_construction_main ($) {
4888                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4889                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4890                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4891                      !!!cp ('t278');
4892                    $i = $_;                    $i = $_;
4893                    last INSCOPE;                    last INSCOPE;
4894                  } elsif ({                  } elsif ({
4895                            table => 1, html => 1,                            table => 1, html => 1,
4896                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4897                      !!!cp ('t279');
4898                    last INSCOPE;                    last INSCOPE;
4899                  }                  }
4900                } # INSCOPE                } # INSCOPE
4901                unless (defined $i) {                unless (defined $i) {
4902                    !!!cp ('t280');
4903                  !!!parse-error (type => 'unmatched end tag:select');                  !!!parse-error (type => 'unmatched end tag:select');
4904                  ## Ignore the token                  ## Ignore the token
4905                  !!!next-token;                  !!!next-token;
4906                  redo B;                  redo B;
4907                }                }
4908                                
4909                  !!!cp ('t281');
4910                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4911    
4912                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
# Line 4286  sub _tree_construction_main ($) { Line 4914  sub _tree_construction_main ($) {
4914                !!!next-token;                !!!next-token;
4915                redo B;                redo B;
4916          } else {          } else {
4917              !!!cp ('t282');
4918            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!parse-error (type => 'in select:'.$token->{tag_name});
4919            ## Ignore the token            ## Ignore the token
4920            !!!next-token;            !!!next-token;
# Line 4295  sub _tree_construction_main ($) { Line 4924  sub _tree_construction_main ($) {
4924              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
4925                if ($self->{open_elements}->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
4926                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
4927                    !!!cp ('t283');
4928                  ## As if </option>                  ## As if </option>
4929                  splice @{$self->{open_elements}}, -2;                  splice @{$self->{open_elements}}, -2;
4930                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {
4931                    !!!cp ('t284');
4932                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4933                } else {                } else {
4934                    !!!cp ('t285');
4935                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4936                  ## Ignore the token                  ## Ignore the token
4937                }                }
# Line 4307  sub _tree_construction_main ($) { Line 4939  sub _tree_construction_main ($) {
4939                redo B;                redo B;
4940              } elsif ($token->{tag_name} eq 'option') {              } elsif ($token->{tag_name} eq 'option') {
4941                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4942                    !!!cp ('t286');
4943                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4944                } else {                } else {
4945                    !!!cp ('t287');
4946                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4947                  ## Ignore the token                  ## Ignore the token
4948                }                }
# Line 4320  sub _tree_construction_main ($) { Line 4954  sub _tree_construction_main ($) {
4954                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4955                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4956                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4957                      !!!cp ('t288');
4958                    $i = $_;                    $i = $_;
4959                    last INSCOPE;                    last INSCOPE;
4960                  } elsif ({                  } elsif ({
4961                            table => 1, html => 1,                            table => 1, html => 1,
4962                           }->{$node->[1]}) {                           }->{$node->[1]}) {
4963                      !!!cp ('t289');
4964                    last INSCOPE;                    last INSCOPE;
4965                  }                  }
4966                } # INSCOPE                } # INSCOPE
4967                unless (defined $i) {                unless (defined $i) {
4968                    !!!cp ('t290');
4969                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4970                  ## Ignore the token                  ## Ignore the token
4971                  !!!next-token;                  !!!next-token;
4972                  redo B;                  redo B;
4973                }                }
4974                                
4975                  !!!cp ('t291');
4976                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
4977    
4978                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
# Line 4345  sub _tree_construction_main ($) { Line 4983  sub _tree_construction_main ($) {
4983                        caption => 1, table => 1, tbody => 1,                        caption => 1, table => 1, tbody => 1,
4984                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
4985                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4986    ## TODO: The following is wrong?
4987                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4988                                
4989                ## have an element in table scope                ## have an element in table scope
# Line 4352  sub _tree_construction_main ($) { Line 4991  sub _tree_construction_main ($) {
4991                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4992                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4993                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] eq $token->{tag_name}) {
4994                      !!!cp ('t292');
4995                    $i = $_;                    $i = $_;
4996                    last INSCOPE;                    last INSCOPE;
4997                  } elsif ({                  } elsif ({
4998                            table => 1, html => 1,                            table => 1, html => 1,
4999                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5000                      !!!cp ('t293');
5001                    last INSCOPE;                    last INSCOPE;
5002                  }                  }
5003                } # INSCOPE                } # INSCOPE
5004                unless (defined $i) {                unless (defined $i) {
5005                    !!!cp ('t294');
5006                  ## Ignore the token                  ## Ignore the token
5007                  !!!next-token;                  !!!next-token;
5008                  redo B;                  redo B;
# Line 4372  sub _tree_construction_main ($) { Line 5014  sub _tree_construction_main ($) {
5014                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5015                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5016                  if ($node->[1] eq 'select') {                  if ($node->[1] eq 'select') {
5017                      !!!cp ('t295');
5018                    $i = $_;                    $i = $_;
5019                    last INSCOPE;                    last INSCOPE;
5020                  } elsif ({                  } elsif ({
5021                            table => 1, html => 1,                            table => 1, html => 1,
5022                           }->{$node->[1]}) {                           }->{$node->[1]}) {
5023    ## ISSUE: Can this state be reached?
5024                      !!!cp ('t296');
5025                    last INSCOPE;                    last INSCOPE;
5026                  }                  }
5027                } # INSCOPE                } # INSCOPE
5028                unless (defined $i) {                unless (defined $i) {
5029                    !!!cp ('t297');
5030    ## TODO: The following error type is correct?
5031                  !!!parse-error (type => 'unmatched end tag:select');                  !!!parse-error (type => 'unmatched end tag:select');
5032                  ## Ignore the </select> token                  ## Ignore the </select> token
5033                  !!!next-token; ## TODO: ok?                  !!!next-token; ## TODO: ok?
5034                  redo B;                  redo B;
5035                }                }
5036                                
5037                  !!!cp ('t298');
5038                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5039    
5040                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
# Line 4394  sub _tree_construction_main ($) { Line 5042  sub _tree_construction_main ($) {
5042                ## reprocess                ## reprocess
5043                redo B;                redo B;
5044          } else {          } else {
5045              !!!cp ('t299');
5046            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!parse-error (type => 'in select:/'.$token->{tag_name});
5047            ## Ignore the token            ## Ignore the token
5048            !!!next-token;            !!!next-token;
# Line 4412  sub _tree_construction_main ($) { Line 5061  sub _tree_construction_main ($) {
5061            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5062                        
5063            unless (length $token->{data}) {            unless (length $token->{data}) {
5064                !!!cp ('t300');
5065              !!!next-token;              !!!next-token;
5066              redo B;              redo B;
5067            }            }
5068          }          }
5069                    
5070          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5071              !!!cp ('t301');
5072            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#character');
5073    
5074            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5075            } else {
5076              !!!cp ('t302');
5077          }          }
5078                    
5079          ## "after body" insertion mode          ## "after body" insertion mode
# Line 4431  sub _tree_construction_main ($) { Line 5084  sub _tree_construction_main ($) {
5084          redo B;          redo B;
5085        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5086          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5087              !!!cp ('t303');
5088            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name});
5089                        
5090            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5091            } else {
5092              !!!cp ('t304');
5093          }          }
5094    
5095          ## "after body" insertion mode          ## "after body" insertion mode
# Line 4444  sub _tree_construction_main ($) { Line 5100  sub _tree_construction_main ($) {
5100          redo B;          redo B;
5101        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5102          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
5103              !!!cp ('t305');
5104            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name});
5105                        
5106            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
5107            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
5108            } else {
5109              !!!cp ('t306');
5110          }          }
5111    
5112          ## "after body" insertion mode          ## "after body" insertion mode
5113          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
5114            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
5115                !!!cp ('t307');
5116              !!!parse-error (type => 'unmatched end tag:html');              !!!parse-error (type => 'unmatched end tag:html');
5117              ## Ignore the token              ## Ignore the token
5118              !!!next-token;              !!!next-token;
5119              redo B;              redo B;
5120            } else {            } else {
5121                !!!cp ('t308');
5122              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
5123              !!!next-token;              !!!next-token;
5124              redo B;              redo B;
5125            }            }
5126          } else {          } else {
5127              !!!cp ('t309');
5128            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!parse-error (type => 'after body:/'.$token->{tag_name});
5129    
5130            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
# Line 4478  sub _tree_construction_main ($) { Line 5140  sub _tree_construction_main ($) {
5140            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5141                        
5142            unless (length $token->{data}) {            unless (length $token->{data}) {
5143                !!!cp ('t310');
5144              !!!next-token;              !!!next-token;
5145              redo B;              redo B;
5146            }            }
# Line 4485  sub _tree_construction_main ($) { Line 5148  sub _tree_construction_main ($) {
5148                    
5149          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
5150            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5151                !!!cp ('t311');
5152              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#character');
5153            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
5154                !!!cp ('t312');
5155              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character');
5156            } else { # "after html frameset"            } else { # "after html frameset"
5157                !!!cp ('t313');
5158              !!!parse-error (type => 'after html:#character');              !!!parse-error (type => 'after html:#character');
5159    
5160              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5161              ## Reprocess in the "main" phase, "after frameset"...              ## Reprocess in the "after frameset" insertion mode.
5162              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character');
5163            }            }
5164                        
5165            ## Ignore the token.            ## Ignore the token.
5166            if (length $token->{data}) {            if (length $token->{data}) {
5167                !!!cp ('t314');
5168              ## reprocess the rest of characters              ## reprocess the rest of characters
5169            } else {            } else {
5170                !!!cp ('t315');
5171              !!!next-token;              !!!next-token;
5172            }            }
5173            redo B;            redo B;
# Line 4508  sub _tree_construction_main ($) { Line 5176  sub _tree_construction_main ($) {
5176          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
5177        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5178          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5179              !!!cp ('t316');
5180            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name});
5181    
5182            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
5183            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
5184          }          } else {
5185              !!!cp ('t317');
5186            }
5187    
5188          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
5189              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
5190              !!!cp ('t318');
5191            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
5192            !!!next-token;            !!!next-token;
5193            redo B;            redo B;
5194          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
5195                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
5196              !!!cp ('t319');
5197            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
5198            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
5199            !!!next-token;            !!!next-token;
5200            redo B;            redo B;
5201          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
5202              !!!cp ('t320');
5203            ## NOTE: As if in body.            ## NOTE: As if in body.
5204            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
5205            redo B;            redo B;
5206          } else {          } else {
5207            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5208                !!!cp ('t321');
5209              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.$token->{tag_name});
5210            } else {            } else {
5211                !!!cp ('t322');
5212              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset:'.$token->{tag_name});
5213            }            }
5214            ## Ignore the token            ## Ignore the token
# Line 4541  sub _tree_construction_main ($) { Line 5217  sub _tree_construction_main ($) {
5217          }          }
5218        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5219          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
5220              !!!cp ('t323');
5221            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name});
5222    
5223            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
5224            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
5225            } else {
5226              !!!cp ('t324');
5227          }          }
5228    
5229          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
5230              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
5231            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] eq 'html' and
5232                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
5233                !!!cp ('t325');
5234              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5235              ## Ignore the token              ## Ignore the token
5236              !!!next-token;              !!!next-token;
5237            } else {            } else {
5238                !!!cp ('t326');
5239              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
5240              !!!next-token;              !!!next-token;
5241            }            }
5242    
5243            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
5244                $self->{open_elements}->[-1]->[1] ne 'frameset') {                $self->{open_elements}->[-1]->[1] ne 'frameset') {
5245                !!!cp ('t327');
5246              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
5247              } else {
5248                !!!cp ('t328');
5249            }            }
5250            redo B;            redo B;
5251          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
5252                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
5253              !!!cp ('t329');
5254            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
5255            !!!next-token;            !!!next-token;
5256            redo B;            redo B;
5257          } else {          } else {
5258            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
5259                !!!cp ('t330');
5260              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
5261            } else {            } else {
5262                !!!cp ('t331');
5263              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
5264            }            }
5265            ## Ignore the token            ## Ignore the token
# Line 4591  sub _tree_construction_main ($) { Line 5278  sub _tree_construction_main ($) {
5278      ## "in body" insertion mode      ## "in body" insertion mode
5279      if ($token->{type} == START_TAG_TOKEN) {      if ($token->{type} == START_TAG_TOKEN) {
5280        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
5281            !!!cp ('t332');
5282          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5283          $script_start_tag->($insert);          $script_start_tag->($insert);
5284          redo B;          redo B;
5285        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
5286            !!!cp ('t333');
5287          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5288          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
5289          redo B;          redo B;
5290        } elsif ({        } elsif ({
5291                  base => 1, link => 1,                  base => 1, link => 1,
5292                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5293            !!!cp ('t334');
5294          ## NOTE: This is an "as if in head" code clone, only "-t" differs          ## NOTE: This is an "as if in head" code clone, only "-t" differs
5295          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5296          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
# Line 4613  sub _tree_construction_main ($) { Line 5303  sub _tree_construction_main ($) {
5303    
5304          unless ($self->{confident}) {          unless ($self->{confident}) {
5305            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) { ## TODO: And if supported
5306                !!!cp ('t335');
5307              $self->{change_encoding}              $self->{change_encoding}
5308                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value});
5309                            
# Line 4627  sub _tree_construction_main ($) { Line 5318  sub _tree_construction_main ($) {
5318                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
5319                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
5320                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
5321                  !!!cp ('t336');
5322                $self->{change_encoding}                $self->{change_encoding}
5323                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
5324                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
# Line 4637  sub _tree_construction_main ($) { Line 5329  sub _tree_construction_main ($) {
5329            }            }
5330          } else {          } else {
5331            if ($token->{attributes}->{charset}) {            if ($token->{attributes}->{charset}) {
5332                !!!cp ('t337');
5333              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
5334                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
5335                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
5336                                           ->{has_reference});                                           ->{has_reference});
5337            }            }
5338            if ($token->{attributes}->{content}) {            if ($token->{attributes}->{content}) {
5339                !!!cp ('t338');
5340              $meta_el->[0]->get_attribute_node_ns (undef, 'content')              $meta_el->[0]->get_attribute_node_ns (undef, 'content')
5341                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
5342                                       $token->{attributes}->{content}                                       $token->{attributes}->{content}
# Line 4653  sub _tree_construction_main ($) { Line 5347  sub _tree_construction_main ($) {
5347          !!!next-token;          !!!next-token;
5348          redo B;          redo B;
5349        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
5350            !!!cp ('t341');
5351          !!!parse-error (type => 'in body:title');          !!!parse-error (type => 'in body:title');
5352          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
5353          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {
5354            if (defined $self->{head_element}) {            if (defined $self->{head_element}) {
5355                !!!cp ('t339');
5356              $self->{head_element}->append_child ($_[0]);              $self->{head_element}->append_child ($_[0]);
5357            } else {            } else {
5358                !!!cp ('t340');
5359              $insert->($_[0]);              $insert->($_[0]);
5360            }            }
5361          });          });
# Line 4668  sub _tree_construction_main ($) { Line 5365  sub _tree_construction_main ($) {
5365                                
5366          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
5367              $self->{open_elements}->[1]->[1] ne 'body') {              $self->{open_elements}->[1]->[1] ne 'body') {
5368              !!!cp ('t342');
5369            ## Ignore the token            ## Ignore the token
5370          } else {          } else {
5371            my $body_el = $self->{open_elements}->[1]->[0];            my $body_el = $self->{open_elements}->[1]->[0];
5372            for my $attr_name (keys %{$token->{attributes}}) {            for my $attr_name (keys %{$token->{attributes}}) {
5373              unless ($body_el->has_attribute_ns (undef, $attr_name)) {              unless ($body_el->has_attribute_ns (undef, $attr_name)) {
5374                  !!!cp ('t343');
5375                $body_el->set_attribute_ns                $body_el->set_attribute_ns
5376                  (undef, [undef, $attr_name],                  (undef, [undef, $attr_name],
5377                   $token->{attributes}->{$attr_name}->{value});                   $token->{attributes}->{$attr_name}->{value});
# Line 4683  sub _tree_construction_main ($) { Line 5382  sub _tree_construction_main ($) {
5382          redo B;          redo B;
5383        } elsif ({        } elsif ({
5384                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
5385                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1,
5386                  menu => 1, ol => 1, p => 1, ul => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
5387                    listing => 1, menu => 1, ol => 1, p => 1, ul => 1,
5388                  pre => 1,                  pre => 1,
5389                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5390          ## has a p element in scope          ## has a p element in scope
5391          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5392            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5393                !!!cp ('t344');
5394              !!!back-token;              !!!back-token;
5395              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5396              redo B;              redo B;
# Line 4697  sub _tree_construction_main ($) { Line 5398  sub _tree_construction_main ($) {
5398                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5399                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5400                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5401                !!!cp ('t345');
5402              last INSCOPE;              last INSCOPE;
5403            }            }
5404          } # INSCOPE          } # INSCOPE
# Line 4707  sub _tree_construction_main ($) { Line 5409  sub _tree_construction_main ($) {
5409            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
5410              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
5411              unless (length $token->{data}) {              unless (length $token->{data}) {
5412                  !!!cp ('t346');
5413                !!!next-token;                !!!next-token;
5414                } else {
5415                  !!!cp ('t349');
5416              }              }
5417              } else {
5418                !!!cp ('t348');
5419            }            }
5420          } else {          } else {
5421              !!!cp ('t347');
5422            !!!next-token;            !!!next-token;
5423          }          }
5424          redo B;          redo B;
5425        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
5426          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
5427              !!!cp ('t350');
5428            !!!parse-error (type => 'in form:form');            !!!parse-error (type => 'in form:form');
5429            ## Ignore the token            ## Ignore the token
5430            !!!next-token;            !!!next-token;
# Line 4724  sub _tree_construction_main ($) { Line 5433  sub _tree_construction_main ($) {
5433            ## has a p element in scope            ## has a p element in scope
5434            INSCOPE: for (reverse @{$self->{open_elements}}) {            INSCOPE: for (reverse @{$self->{open_elements}}) {
5435              if ($_->[1] eq 'p') {              if ($_->[1] eq 'p') {
5436                  !!!cp ('t351');
5437                !!!back-token;                !!!back-token;
5438                $token = {type => END_TAG_TOKEN, tag_name => 'p'};                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5439                redo B;                redo B;
# Line 4731  sub _tree_construction_main ($) { Line 5441  sub _tree_construction_main ($) {
5441                        table => 1, caption => 1, td => 1, th => 1,                        table => 1, caption => 1, td => 1, th => 1,
5442                        button => 1, marquee => 1, object => 1, html => 1,                        button => 1, marquee => 1, object => 1, html => 1,
5443                       }->{$_->[1]}) {                       }->{$_->[1]}) {
5444                  !!!cp ('t352');
5445                last INSCOPE;                last INSCOPE;
5446              }              }
5447            } # INSCOPE            } # INSCOPE
# Line 4744  sub _tree_construction_main ($) { Line 5455  sub _tree_construction_main ($) {
5455          ## has a p element in scope          ## has a p element in scope
5456          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5457            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5458                !!!cp ('t353');
5459              !!!back-token;              !!!back-token;
5460              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5461              redo B;              redo B;
# Line 4751  sub _tree_construction_main ($) { Line 5463  sub _tree_construction_main ($) {
5463                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5464                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5465                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5466                !!!cp ('t354');
5467              last INSCOPE;              last INSCOPE;
5468            }            }
5469          } # INSCOPE          } # INSCOPE
# Line 4762  sub _tree_construction_main ($) { Line 5475  sub _tree_construction_main ($) {
5475            ## Step 2            ## Step 2
5476            if ($node->[1] eq 'li') {            if ($node->[1] eq 'li') {
5477              if ($i != -1) {              if ($i != -1) {
5478                  !!!cp ('t355');
5479                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'end tag missing:'.
5480                                $self->{open_elements}->[-1]->[1]);                                $self->{open_elements}->[-1]->[1]);
5481                } else {
5482                  !!!cp ('t356');
5483              }              }
5484              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
5485              last LI;              last LI;
5486              } else {
5487                !!!cp ('t357');
5488            }            }
5489                        
5490            ## Step 3            ## Step 3
# Line 4775  sub _tree_construction_main ($) { Line 5493  sub _tree_construction_main ($) {
5493                ($special_category->{$node->[1]} or                ($special_category->{$node->[1]} or
5494                 $scoping_category->{$node->[1]}) and                 $scoping_category->{$node->[1]}) and
5495                $node->[1] ne 'address' and $node->[1] ne 'div') {                $node->[1] ne 'address' and $node->[1] ne 'div') {
5496                !!!cp ('t358');
5497              last LI;              last LI;
5498            }            }
5499                        
5500              !!!cp ('t359');
5501            ## Step 4            ## Step 4
5502            $i--;            $i--;
5503            $node = $self->{open_elements}->[$i];            $node = $self->{open_elements}->[$i];
# Line 4791  sub _tree_construction_main ($) { Line 5511  sub _tree_construction_main ($) {
5511          ## has a p element in scope          ## has a p element in scope
5512          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5513            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5514                !!!cp ('t360');
5515              !!!back-token;              !!!back-token;
5516              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5517              redo B;              redo B;
# Line 4798  sub _tree_construction_main ($) { Line 5519  sub _tree_construction_main ($) {
5519                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5520                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5521                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5522                !!!cp ('t361');
5523              last INSCOPE;              last INSCOPE;
5524            }            }
5525          } # INSCOPE          } # INSCOPE
# Line 4809  sub _tree_construction_main ($) { Line 5531  sub _tree_construction_main ($) {
5531            ## Step 2            ## Step 2
5532            if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {            if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {
5533              if ($i != -1) {              if ($i != -1) {
5534                  !!!cp ('t362');
5535                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'end tag missing:'.
5536                                $self->{open_elements}->[-1]->[1]);                                $self->{open_elements}->[-1]->[1]);
5537                } else {
5538                  !!!cp ('t363');
5539              }              }
5540              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
5541              last LI;              last LI;
5542              } else {
5543                !!!cp ('t364');
5544            }            }
5545                        
5546            ## Step 3            ## Step 3
# Line 4822  sub _tree_construction_main ($) { Line 5549  sub _tree_construction_main ($) {
5549                ($special_category->{$node->[1]} or                ($special_category->{$node->[1]} or
5550                 $scoping_category->{$node->[1]}) and                 $scoping_category->{$node->[1]}) and
5551                $node->[1] ne 'address' and $node->[1] ne 'div') {                $node->[1] ne 'address' and $node->[1] ne 'div') {
5552                !!!cp ('t365');
5553              last LI;              last LI;
5554            }            }
5555                        
5556              !!!cp ('t366');
5557            ## Step 4            ## Step 4
5558            $i--;            $i--;
5559            $node = $self->{open_elements}->[$i];            $node = $self->{open_elements}->[$i];
# Line 4838  sub _tree_construction_main ($) { Line 5567  sub _tree_construction_main ($) {
5567          ## has a p element in scope          ## has a p element in scope
5568          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5569            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5570                !!!cp ('t367');
5571              !!!back-token;              !!!back-token;
5572              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5573              redo B;              redo B;
# Line 4845  sub _tree_construction_main ($) { Line 5575  sub _tree_construction_main ($) {
5575                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5576                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5577                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5578                !!!cp ('t368');
5579              last INSCOPE;              last INSCOPE;
5580            }            }
5581          } # INSCOPE          } # INSCOPE
# Line 4855  sub _tree_construction_main ($) { Line 5586  sub _tree_construction_main ($) {
5586                        
5587          !!!next-token;          !!!next-token;
5588          redo B;          redo B;
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>  
         ## has an element in scope  
         #my $i;  
         #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
         #  my $node = $self->{open_elements}->[$_];  
         #  if ({  
         #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
         #      }->{$node->[1]}) {  
         #    $i = $_;  
         #    last INSCOPE;  
         #  } elsif ({  
         #            table => 1, caption => 1, td => 1, th => 1,  
         #            button => 1, marquee => 1, object => 1, html => 1,  
         #           }->{$node->[1]}) {  
         #    last INSCOPE;  
         #  }  
         #} # INSCOPE  
         #    
         #if (defined $i) {  
         #  !!! parse-error (type => 'in hn:hn');  
         #  splice @{$self->{open_elements}}, $i;  
         #}  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         !!!next-token;  
         redo B;  
5589        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
5590          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
5591            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
5592            if ($node->[1] eq 'a') {            if ($node->[1] eq 'a') {
5593                !!!cp ('t371');
5594              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a');
5595                            
5596              !!!back-token;              !!!back-token;
# Line 4912  sub _tree_construction_main ($) { Line 5599  sub _tree_construction_main ($) {
5599                            
5600              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
5601                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
5602                    !!!cp ('t372');
5603                  splice @$active_formatting_elements, $_, 1;                  splice @$active_formatting_elements, $_, 1;
5604                  last AFE2;                  last AFE2;
5605                }                }
5606              } # AFE2              } # AFE2
5607              OE: for (reverse 0..$#{$self->{open_elements}}) {              OE: for (reverse 0..$#{$self->{open_elements}}) {
5608                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
5609                    !!!cp ('t373');
5610                  splice @{$self->{open_elements}}, $_, 1;                  splice @{$self->{open_elements}}, $_, 1;
5611                  last OE;                  last OE;
5612                }                }
5613              } # OE              } # OE
5614              last AFE;              last AFE;
5615            } elsif ($node->[0] eq '#marker') {            } elsif ($node->[0] eq '#marker') {
5616                !!!cp ('t374');
5617              last AFE;              last AFE;
5618            }            }
5619          } # AFE          } # AFE
# Line 4940  sub _tree_construction_main ($) { Line 5630  sub _tree_construction_main ($) {
5630                  s => 1, small => 1, strile => 1,                  s => 1, small => 1, strile => 1,
5631                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
5632                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5633            !!!cp ('t375');
5634          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5635                    
5636          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
# Line 4954  sub _tree_construction_main ($) { Line 5645  sub _tree_construction_main ($) {
5645          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5646            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
5647            if ($node->[1] eq 'nobr') {            if ($node->[1] eq 'nobr') {
5648                !!!cp ('t376');
5649              !!!parse-error (type => 'in nobr:nobr');              !!!parse-error (type => 'in nobr:nobr');
5650              !!!back-token;              !!!back-token;
5651              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
# Line 4962  sub _tree_construction_main ($) { Line 5654  sub _tree_construction_main ($) {
5654                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5655                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5656                     }->{$node->[1]}) {                     }->{$node->[1]}) {
5657                !!!cp ('t377');
5658              last INSCOPE;              last INSCOPE;
5659            }            }
5660          } # INSCOPE          } # INSCOPE
# Line 4976  sub _tree_construction_main ($) { Line 5669  sub _tree_construction_main ($) {
5669          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5670            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
5671            if ($node->[1] eq 'button') {            if ($node->[1] eq 'button') {
5672                !!!cp ('t378');
5673              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button');
5674              !!!back-token;              !!!back-token;
5675              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button'};
# Line 4984  sub _tree_construction_main ($) { Line 5678  sub _tree_construction_main ($) {
5678                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5679                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5680                     }->{$node->[1]}) {                     }->{$node->[1]}) {
5681                !!!cp ('t379');
5682              last INSCOPE;              last INSCOPE;
5683            }            }
5684          } # INSCOPE          } # INSCOPE
# Line 4991  sub _tree_construction_main ($) { Line 5686  sub _tree_construction_main ($) {
5686          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5687                        
5688          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5689    
5690            ## TODO: associate with $self->{form_element} if defined
5691    
5692          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
5693    
5694          !!!next-token;          !!!next-token;
5695          redo B;          redo B;
5696        } elsif ($token->{tag_name} eq 'marquee' or        } elsif ($token->{tag_name} eq 'marquee' or
5697                 $token->{tag_name} eq 'object') {                 $token->{tag_name} eq 'object') {
5698            !!!cp ('t380');
5699          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5700                    
5701          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
# Line 5005  sub _tree_construction_main ($) { Line 5704  sub _tree_construction_main ($) {
5704          !!!next-token;          !!!next-token;
5705          redo B;          redo B;
5706        } elsif ($token->{tag_name} eq 'xmp') {        } elsif ($token->{tag_name} eq 'xmp') {
5707            !!!cp ('t381');
5708          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5709          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
5710          redo B;          redo B;
# Line 5012  sub _tree_construction_main ($) { Line 5712  sub _tree_construction_main ($) {
5712          ## has a p element in scope          ## has a p element in scope
5713          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5714            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5715                !!!cp ('t382');
5716              !!!back-token;              !!!back-token;
5717              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5718              redo B;              redo B;
# Line 5019  sub _tree_construction_main ($) { Line 5720  sub _tree_construction_main ($) {
5720                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5721                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5722                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5723                !!!cp ('t383');
5724              last INSCOPE;              last INSCOPE;
5725            }            }
5726          } # INSCOPE          } # INSCOPE
# Line 5035  sub _tree_construction_main ($) { Line 5737  sub _tree_construction_main ($) {
5737                  image => 1,                  image => 1,
5738                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5739          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'image') {
5740              !!!cp ('t384');
5741            !!!parse-error (type => 'image');            !!!parse-error (type => 'image');
5742            $token->{tag_name} = 'img';            $token->{tag_name} = 'img';
5743            } else {
5744              !!!cp ('t385');
5745          }          }
5746    
5747          ## NOTE: There is an "as if <br>" code clone.          ## NOTE: There is an "as if <br>" code clone.
# Line 5051  sub _tree_construction_main ($) { Line 5756  sub _tree_construction_main ($) {
5756          ## has a p element in scope          ## has a p element in scope
5757          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
5758            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
5759                !!!cp ('t386');
5760              !!!back-token;              !!!back-token;
5761              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
5762              redo B;              redo B;
# Line 5058  sub _tree_construction_main ($) { Line 5764  sub _tree_construction_main ($) {
5764                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5765                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5766                     }->{$_->[1]}) {                     }->{$_->[1]}) {
5767                !!!cp ('t387');
5768              last INSCOPE;              last INSCOPE;
5769            }            }
5770          } # INSCOPE          } # INSCOPE
# Line 5068  sub _tree_construction_main ($) { Line 5775  sub _tree_construction_main ($) {
5775          !!!next-token;          !!!next-token;
5776          redo B;          redo B;
5777        } elsif ($token->{tag_name} eq 'input') {        } elsif ($token->{tag_name} eq 'input') {
5778            !!!cp ('t388');
5779          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5780                    
5781          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
# Line 5080  sub _tree_construction_main ($) { Line 5788  sub _tree_construction_main ($) {
5788          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex');
5789                    
5790          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
5791              !!!cp ('t389');
5792            ## Ignore the token            ## Ignore the token
5793            !!!next-token;            !!!next-token;
5794            redo B;            redo B;
# Line 5099  sub _tree_construction_main ($) { Line 5808  sub _tree_construction_main ($) {
5808                          {type => START_TAG_TOKEN, tag_name => 'label'},                          {type => START_TAG_TOKEN, tag_name => 'label'},
5809                         );                         );
5810            if ($prompt_attr) {            if ($prompt_attr) {
5811                !!!cp ('t390');
5812              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
5813            } else {            } else {
5814                !!!cp ('t391');
5815              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
5816                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
5817              ## TODO: make this configurable              ## TODO: make this configurable
# Line 5132  sub _tree_construction_main ($) { Line 5843  sub _tree_construction_main ($) {
5843          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
5844            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
5845            unless (length $token->{data}) {            unless (length $token->{data}) {
5846                !!!cp ('t392');
5847              !!!next-token;              !!!next-token;
5848              } else {
5849                !!!cp ('t393');
5850            }            }
5851            } else {
5852              !!!cp ('t394');
5853          }          }
5854          while ($token->{type} == CHARACTER_TOKEN) {          while ($token->{type} == CHARACTER_TOKEN) {
5855              !!!cp ('t395');
5856            $text .= $token->{data};            $text .= $token->{data};
5857            !!!next-token;            !!!next-token;
5858          }          }
5859          if (length $text) {          if (length $text) {
5860              !!!cp ('t396');
5861            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
5862          }          }
5863                    
# Line 5147  sub _tree_construction_main ($) { Line 5865  sub _tree_construction_main ($) {
5865                    
5866          if ($token->{type} == END_TAG_TOKEN and          if ($token->{type} == END_TAG_TOKEN and
5867              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
5868              !!!cp ('t397');
5869            ## Ignore the token            ## Ignore the token
5870          } else {          } else {
5871              !!!cp ('t398');
5872            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#'.$token->{type});
5873          }          }
5874          !!!next-token;          !!!next-token;
# Line 5159  sub _tree_construction_main ($) { Line 5879  sub _tree_construction_main ($) {
5879                  noframes => 1,                  noframes => 1,
5880                  noscript => 0, ## TODO: 1 if scripting is enabled                  noscript => 0, ## TODO: 1 if scripting is enabled
5881                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5882            !!!cp ('t399');
5883          ## NOTE: There is an "as if in body" code clone.          ## NOTE: There is an "as if in body" code clone.
5884          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
5885          redo B;          redo B;
5886        } elsif ($token->{tag_name} eq 'select') {        } elsif ($token->{tag_name} eq 'select') {
5887            !!!cp ('t400');
5888          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5889                    
5890          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5891    
5892            ## TODO: associate with $self->{form_element} if defined
5893                    
5894          $self->{insertion_mode} = IN_SELECT_IM;          $self->{insertion_mode} = IN_SELECT_IM;
5895          !!!next-token;          !!!next-token;
# Line 5176  sub _tree_construction_main ($) { Line 5900  sub _tree_construction_main ($) {
5900                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
5901                  thead => 1, tr => 1,                  thead => 1, tr => 1,
5902                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5903            !!!cp ('t401');
5904          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body:'.$token->{tag_name});
5905          ## Ignore the token          ## Ignore the token
5906          !!!next-token;          !!!next-token;
# Line 5183  sub _tree_construction_main ($) { Line 5908  sub _tree_construction_main ($) {
5908                    
5909          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
5910        } else {        } else {
5911            !!!cp ('t402');
5912          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
5913                    
5914          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
# Line 5200  sub _tree_construction_main ($) { Line 5926  sub _tree_construction_main ($) {
5926                         th => 1, tr => 1, body => 1, html => 1,                         th => 1, tr => 1, body => 1, html => 1,
5927                       tbody => 1, tfoot => 1, thead => 1,                       tbody => 1, tfoot => 1, thead => 1,
5928                      }->{$_->[1]}) {                      }->{$_->[1]}) {
5929                  !!!cp ('t403');
5930                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!parse-error (type => 'not closed:'.$_->[1]);
5931                } else {
5932                  !!!cp ('t404');
5933              }              }
5934            }            }
5935    
# Line 5208  sub _tree_construction_main ($) { Line 5937  sub _tree_construction_main ($) {
5937            !!!next-token;            !!!next-token;
5938            redo B;            redo B;
5939          } else {          } else {
5940              !!!cp ('t405');
5941            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5942            ## Ignore the token            ## Ignore the token
5943            !!!next-token;            !!!next-token;
# Line 5217  sub _tree_construction_main ($) { Line 5947  sub _tree_construction_main ($) {
5947          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') {
5948            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
5949            if ($self->{open_elements}->[-1]->[1] ne 'body') {            if ($self->{open_elements}->[-1]->[1] ne 'body') {
5950                !!!cp ('t406');
5951              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
5952              } else {
5953                !!!cp ('t407');
5954            }            }
5955            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
5956            ## reprocess            ## reprocess
5957            redo B;            redo B;
5958          } else {          } else {
5959              !!!cp ('t408');
5960            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5961            ## Ignore the token            ## Ignore the token
5962            !!!next-token;            !!!next-token;
# Line 5232  sub _tree_construction_main ($) { Line 5966  sub _tree_construction_main ($) {
5966                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
5967                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
5968                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
5969                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
5970                  button => 1, marquee => 1, object => 1,                  button => 1, marquee => 1, object => 1,
5971                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 5241  sub _tree_construction_main ($) { Line 5974  sub _tree_construction_main ($) {
5974          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5975            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
5976            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] eq $token->{tag_name}) {
5977              ## generate implied end tags              !!!cp ('t410');
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
5978              $i = $_;              $i = $_;
5979              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
5980            } elsif ({            } elsif ({
5981                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
5982                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
5983                     }->{$node->[1]}) {                     }->{$node->[1]}) {
5984                !!!cp ('t411');
5985              last INSCOPE;              last INSCOPE;
5986            }            }
5987          } # INSCOPE          } # INSCOPE
5988            
5989          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
5990            if (defined $i) {            !!!cp ('t413');
5991              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5992            } else {
5993              ## Step 1. generate implied end tags
5994              while ({
5995                      dd => ($token->{tag_name} ne 'dd'),
5996                      dt => ($token->{tag_name} ne 'dt'),
5997                      li => ($token->{tag_name} ne 'li'),
5998                      p => 1,
5999                     }->{$self->{open_elements}->[-1]->[1]}) {
6000                !!!cp ('t409');
6001                pop @{$self->{open_elements}};
6002              }
6003    
6004              ## Step 2.
6005              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6006                !!!cp ('t412');
6007              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
6008            } else {            } else {
6009              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t414');
6010            }            }
6011          }  
6012                      ## Step 3.
         if (defined $i) {  
6013            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6014          } elsif ($token->{tag_name} eq 'p') {  
6015            ## As if <p>, then reprocess the current token            ## Step 4.
6016            my $el;            $clear_up_to_marker->()
6017            !!!create-element ($el, 'p');                if {
6018            $insert->($el);                  button => 1, marquee => 1, object => 1,
6019                  }->{$token->{tag_name}};
6020          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
6021          !!!next-token;          !!!next-token;
6022          redo B;          redo B;
6023        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
6024            undef $self->{form_element};
6025    
6026          ## has an element in scope          ## has an element in scope
6027            my $i;
6028          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6029            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6030            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] eq $token->{tag_name}) {
6031              ## generate implied end tags              !!!cp ('t418');
6032              if ({              $i = $_;
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
6033              last INSCOPE;              last INSCOPE;
6034            } elsif ({            } elsif ({
6035                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
6036                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
6037                     }->{$node->[1]}) {                     }->{$node->[1]}) {
6038                !!!cp ('t419');
6039              last INSCOPE;              last INSCOPE;
6040            }            }
6041          } # INSCOPE          } # INSCOPE
6042            
6043          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6044            pop @{$self->{open_elements}};            !!!cp ('t421');
         } else {  
6045            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6046            } else {
6047              ## Step 1. generate implied end tags
6048              while ({
6049                      dd => 1, dt => 1, li => 1, p => 1,
6050                     }->{$self->{open_elements}->[-1]->[1]}) {
6051                !!!cp ('t417');
6052                pop @{$self->{open_elements}};
6053              }
6054              
6055              ## Step 2.
6056              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6057                !!!cp ('t417.1');
6058                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
6059              } else {
6060                !!!cp ('t420');
6061              }  
6062              
6063              ## Step 3.
6064              splice @{$self->{open_elements}}, $i;
6065          }          }
6066    
         undef $self->{form_element};  
6067          !!!next-token;          !!!next-token;
6068          redo B;          redo B;
6069        } elsif ({        } elsif ({
# Line 5331  sub _tree_construction_main ($) { Line 6076  sub _tree_construction_main ($) {
6076            if ({            if ({
6077                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6078                }->{$node->[1]}) {                }->{$node->[1]}) {
6079              ## generate implied end tags              !!!cp ('t423');
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
6080              $i = $_;              $i = $_;
6081              last INSCOPE;              last INSCOPE;
6082            } elsif ({            } elsif ({
6083                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
6084                      button => 1, marquee => 1, object => 1, html => 1,                      button => 1, marquee => 1, object => 1, html => 1,
6085                     }->{$node->[1]}) {                     }->{$node->[1]}) {
6086                !!!cp ('t424');
6087              last INSCOPE;              last INSCOPE;
6088            }            }
6089          } # INSCOPE          } # INSCOPE
6090            
6091          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6092              !!!cp ('t425.1');
6093            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6094            } else {
6095              ## Step 1. generate implied end tags
6096              while ({
6097                      dd => 1, dt => 1, li => 1, p => 1,
6098                     }->{$self->{open_elements}->[-1]->[1]}) {
6099                !!!cp ('t422');
6100                pop @{$self->{open_elements}};
6101              }
6102              
6103              ## Step 2.
6104              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6105                !!!cp ('t425');
6106                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6107              } else {
6108                !!!cp ('t426');
6109              }
6110    
6111              ## Step 3.
6112              splice @{$self->{open_elements}}, $i;
6113          }          }
6114                    
6115          splice @{$self->{open_elements}}, $i if defined $i;          !!!next-token;
6116            redo B;
6117          } elsif ($token->{tag_name} eq 'p') {
6118            ## has an element in scope
6119            my $i;
6120            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6121              my $node = $self->{open_elements}->[$_];
6122              if ($node->[1] eq $token->{tag_name}) {
6123                !!!cp ('t410.1');
6124                $i = $_;
6125                last INSCOPE;
6126              } elsif ({
6127                        table => 1, caption => 1, td => 1, th => 1,
6128                        button => 1, marquee => 1, object => 1, html => 1,
6129                       }->{$node->[1]}) {
6130                !!!cp ('t411.1');
6131                last INSCOPE;
6132              }
6133            } # INSCOPE
6134    
6135            if (defined $i) {
6136              if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
6137                !!!cp ('t412.1');
6138                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
6139              } else {
6140                !!!cp ('t414.1');
6141              }
6142    
6143              splice @{$self->{open_elements}}, $i;
6144            } else {
6145              !!!cp ('t413.1');
6146              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6147    
6148              !!!cp ('t415.1');
6149              ## As if <p>, then reprocess the current token
6150              my $el;
6151              !!!create-element ($el, 'p');
6152              $insert->($el);
6153              ## NOTE: Not inserted into |$self->{open_elements}|.
6154            }
6155    
6156          !!!next-token;          !!!next-token;
6157          redo B;          redo B;
6158        } elsif ({        } elsif ({
# Line 5365  sub _tree_construction_main ($) { Line 6161  sub _tree_construction_main ($) {
6161                  nobr => 1, s => 1, small => 1, strile => 1,                  nobr => 1, s => 1, small => 1, strile => 1,
6162                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
6163                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6164            !!!cp ('t427');
6165          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token->{tag_name});
6166          redo B;          redo B;
6167        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
6168            !!!cp ('t428');
6169          !!!parse-error (type => 'unmatched end tag:br');          !!!parse-error (type => 'unmatched end tag:br');
6170    
6171          ## As if <br>          ## As if <br>
# Line 5392  sub _tree_construction_main ($) { Line 6190  sub _tree_construction_main ($) {
6190                  table => 1, textarea => 1, wbr => 1,                  table => 1, textarea => 1, wbr => 1,
6191                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
6192                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6193            !!!cp ('t429');
6194          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6195          ## Ignore the token          ## Ignore the token
6196          !!!next-token;          !!!next-token;
# Line 5409  sub _tree_construction_main ($) { Line 6208  sub _tree_construction_main ($) {
6208            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] eq $token->{tag_name}) {
6209              ## Step 1              ## Step 1
6210              ## generate implied end tags              ## generate implied end tags
6211              if ({              while ({
6212                   dd => 1, dt => 1, li => 1, p => 1,                      dd => 1, dt => 1, li => 1, p => 1,
6213                   td => 1, th => 1, tr => 1,                     }->{$self->{open_elements}->[-1]->[1]}) {
6214                   tbody => 1, tfoot => 1, thead => 1,                !!!cp ('t430');
6215                  }->{$self->{open_elements}->[-1]->[1]}) {                ## ISSUE: Can this case be reached?
6216                !!!back-token;                pop @{$self->{open_elements}};
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
6217              }              }
6218                    
6219              ## Step 2              ## Step 2
6220              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
6221                  !!!cp ('t431');
6222                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
6223                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
6224                } else {
6225                  !!!cp ('t432');
6226              }              }
6227                            
6228              ## Step 3              ## Step 3
# Line 5437  sub _tree_construction_main ($) { Line 6236  sub _tree_construction_main ($) {
6236                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
6237                  ($special_category->{$node->[1]} or                  ($special_category->{$node->[1]} or
6238                   $scoping_category->{$node->[1]})) {                   $scoping_category->{$node->[1]})) {
6239                  !!!cp ('t433');
6240                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
6241                ## Ignore the token                ## Ignore the token
6242                !!!next-token;                !!!next-token;
6243                last S2;                last S2;
6244              }              }
6245    
6246                !!!cp ('t434');
6247            }            }
6248                        
6249            ## Step 4            ## Step 4
# Line 5457  sub _tree_construction_main ($) { Line 6259  sub _tree_construction_main ($) {
6259      redo B;      redo B;
6260    } # B    } # B
6261    
   ## NOTE: The "trailing end" phase in HTML5 is split into  
   ## two insertion modes: "after html body" and "after html frameset".  
   ## NOTE: States in the main stage is preserved while  
   ## the parser stays in the trailing end phase. # MUST  
   
6262    ## Stop parsing # MUST    ## Stop parsing # MUST
6263        
6264    ## TODO: script stuffs    ## TODO: script stuffs
# Line 5503  sub set_inner_html ($$$) { Line 6300  sub set_inner_html ($$$) {
6300      my $p = $class->new;      my $p = $class->new;
6301      $p->{document} = $doc;      $p->{document} = $doc;
6302    
6303      ## Step 9 # MUST      ## Step 8 # MUST
6304      my $i = 0;      my $i = 0;
6305      my $line = 1;      my $line = 1;
6306      my $column = 0;      my $column = 0;
6307      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
6308        my $self = shift;        my $self = shift;
6309    
6310        pop @{$self->{prev_input_character}};        pop @{$self->{prev_char}};
6311        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        unshift @{$self->{prev_char}}, $self->{next_char};
6312    
6313        $self->{next_input_character} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
6314        $self->{next_input_character} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
6315        $column++;        $column++;
6316    
6317        if ($self->{next_input_character} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
6318          $line++;          $line++;
6319          $column = 0;          $column = 0;
6320        } elsif ($self->{next_input_character} == 0x000D) { # CR          !!!cp ('i1');
6321          } elsif ($self->{next_char} == 0x000D) { # CR
6322          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
6323          $self->{next_input_character} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
6324          $line++;          $line++;
6325          $column = 0;          $column = 0;
6326        } elsif ($self->{next_input_character} > 0x10FFFF) {          !!!cp ('i2');
6327          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        } elsif ($self->{next_char} > 0x10FFFF) {
6328        } elsif ($self->{next_input_character} == 0x0000) { # NULL          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
6329            !!!cp ('i3');
6330          } elsif ($self->{next_char} == 0x0000) { # NULL
6331            !!!cp ('i4');
6332          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
6333          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
6334        }        }
6335      };      };
6336      $p->{prev_input_character} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
6337      $p->{next_input_character} = -1;      $p->{next_char} = -1;
6338            
6339      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
6340        my (%opt) = @_;        my (%opt) = @_;
# Line 5566  sub set_inner_html ($$$) { Line 6367  sub set_inner_html ($$$) {
6367    
6368      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $node_ln];
6369    
6370      ## Step 4      ## Step 3
6371      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
6372        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
6373    
6374      ## Step 5 # MUST      ## Step 4 # MUST
6375      $doc->append_child ($root);      $doc->append_child ($root);
6376    
6377      ## Step 6 # MUST      ## Step 5 # MUST
6378      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, 'html'];
6379    
6380      undef $p->{head_element};      undef $p->{head_element};
6381    
6382      ## Step 7 # MUST      ## Step 6 # MUST
6383      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
6384    
6385      ## Step 8 # MUST      ## Step 7 # MUST
6386      my $anode = $node;      my $anode = $node;
6387      AN: while (defined $anode) {      AN: while (defined $anode) {
6388        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
6389          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
6390          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
6391            if ($anode->manakai_local_name eq 'form') {            if ($anode->manakai_local_name eq 'form') {
6392                !!!cp ('i5');
6393              $p->{form_element} = $anode;              $p->{form_element} = $anode;
6394              last AN;              last AN;
6395            }            }
# Line 5596  sub set_inner_html ($$$) { Line 6398  sub set_inner_html ($$$) {
6398        $anode = $anode->parent_node;        $anode = $anode->parent_node;
6399      } # AN      } # AN
6400            
6401      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
6402      {      {
6403        my $self = $p;        my $self = $p;
6404        !!!next-token;        !!!next-token;
6405      }      }
6406      $p->_tree_construction_main;      $p->_tree_construction_main;
6407    
6408      ## Step 11 # MUST      ## Step 10 # MUST
6409      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
6410      for (@cn) {      for (@cn) {
6411        $node->remove_child ($_);        $node->remove_child ($_);
6412      }      }
6413      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
6414    
6415      ## Step 12 # MUST      ## Step 11 # MUST
6416      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
6417      for (@cn) {      for (@cn) {
6418        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);

Legend:
Removed from v.1.72  
changed lines
  Added in v.1.94

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24