/[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.146 by wakaba, Sat May 24 14:19:55 2008 UTC revision 1.178 by wakaba, Sun Sep 14 11:57:41 2008 UTC
# Line 45  sub MISC_SPECIAL_EL () { 0b1000000000000 Line 45  sub MISC_SPECIAL_EL () { 0b1000000000000
45  sub FOREIGN_EL () { 0b10000000000000000000000000 }  sub FOREIGN_EL () { 0b10000000000000000000000000 }
46  sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }  sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
47  sub MML_AXML_EL () { 0b1000000000000000000000000000 }  sub MML_AXML_EL () { 0b1000000000000000000000000000 }
48    sub RUBY_EL () { 0b10000000000000000000000000000 }
49    sub RUBY_COMPONENT_EL () { 0b100000000000000000000000000000 }
50    
51  sub TABLE_ROWS_EL () {  sub TABLE_ROWS_EL () {
52    TABLE_EL |    TABLE_EL |
# Line 52  sub TABLE_ROWS_EL () { Line 54  sub TABLE_ROWS_EL () {
54    TABLE_ROW_GROUP_EL    TABLE_ROW_GROUP_EL
55  }  }
56    
57    ## NOTE: Used in "generate implied end tags" algorithm.
58    ## NOTE: There is a code where a modified version of END_TAG_OPTIONAL_EL
59    ## is used in "generate implied end tags" implementation (search for the
60    ## function mae).
61  sub END_TAG_OPTIONAL_EL () {  sub END_TAG_OPTIONAL_EL () {
62    DD_EL |    DD_EL |
63    DT_EL |    DT_EL |
64    LI_EL |    LI_EL |
65    P_EL    P_EL |
66      RUBY_COMPONENT_EL
67  }  }
68    
69    ## NOTE: Used in </body> and EOF algorithms.
70  sub ALL_END_TAG_OPTIONAL_EL () {  sub ALL_END_TAG_OPTIONAL_EL () {
71    END_TAG_OPTIONAL_EL |    DD_EL |
72      DT_EL |
73      LI_EL |
74      P_EL |
75    
76    BODY_EL |    BODY_EL |
77    HTML_EL |    HTML_EL |
78    TABLE_CELL_EL |    TABLE_CELL_EL |
# Line 96  sub SPECIAL_EL () { Line 108  sub SPECIAL_EL () {
108    ADDRESS_EL |    ADDRESS_EL |
109    BODY_EL |    BODY_EL |
110    DIV_EL |    DIV_EL |
111    END_TAG_OPTIONAL_EL |  
112      DD_EL |
113      DT_EL |
114      LI_EL |
115      P_EL |
116    
117    FORM_EL |    FORM_EL |
118    FRAMESET_EL |    FRAMESET_EL |
119    HEADING_EL |    HEADING_EL |
# Line 170  my $el_category = { Line 187  my $el_category = {
187    param => MISC_SPECIAL_EL,    param => MISC_SPECIAL_EL,
188    plaintext => MISC_SPECIAL_EL,    plaintext => MISC_SPECIAL_EL,
189    pre => MISC_SPECIAL_EL,    pre => MISC_SPECIAL_EL,
190      rp => RUBY_COMPONENT_EL,
191      rt => RUBY_COMPONENT_EL,
192      ruby => RUBY_EL,
193    s => FORMATTING_EL,    s => FORMATTING_EL,
194    script => MISC_SPECIAL_EL,    script => MISC_SPECIAL_EL,
195    select => SELECT_EL,    select => SELECT_EL,
# Line 334  sub parse_byte_string ($$$$;$) { Line 354  sub parse_byte_string ($$$$;$) {
354    return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);    return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
355  } # parse_byte_string  } # parse_byte_string
356    
357  sub parse_byte_stream ($$$$;$) {  sub parse_byte_stream ($$$$;$$) {
358      # my ($self, $charset_name, $byte_stream, $doc, $onerror, $get_wrapper) = @_;
359    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
360    my $charset_name = shift;    my $charset_name = shift;
361    my $byte_stream = $_[0];    my $byte_stream = $_[0];
# Line 345  sub parse_byte_stream ($$$$;$) { Line 366  sub parse_byte_stream ($$$$;$) {
366    };    };
367    $self->{parse_error} = $onerror; # updated later by parse_char_string    $self->{parse_error} = $onerror; # updated later by parse_char_string
368    
369      my $get_wrapper = $_[3] || sub ($) {
370        return $_[0]; # $_[0] = byte stream handle, returned = arg to char handle
371      };
372    
373    ## HTML5 encoding sniffing algorithm    ## HTML5 encoding sniffing algorithm
374    require Message::Charset::Info;    require Message::Charset::Info;
375    my $charset;    my $charset;
# Line 352  sub parse_byte_stream ($$$$;$) { Line 377  sub parse_byte_stream ($$$$;$) {
377    my ($char_stream, $e_status);    my ($char_stream, $e_status);
378    
379    SNIFFING: {    SNIFFING: {
380        ## NOTE: By setting |allow_fallback| option true when the
381        ## |get_decode_handle| method is invoked, we ignore what the HTML5
382        ## spec requires, i.e. unsupported encoding should be ignored.
383          ## TODO: We should not do this unless the parser is invoked
384          ## in the conformance checking mode, in which this behavior
385          ## would be useful.
386    
387      ## Step 1      ## Step 1
388      if (defined $charset_name) {      if (defined $charset_name) {
389        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);        $charset = Message::Charset::Info->get_by_html_name ($charset_name);
390              ## TODO: Is this ok?  Transfer protocol's parameter should be
391              ## interpreted in its semantics?
392    
393        ## ISSUE: Unsupported encoding is not ignored according to the spec.        ## ISSUE: Unsupported encoding is not ignored according to the spec.
394        ($char_stream, $e_status) = $charset->get_decode_handle        ($char_stream, $e_status) = $charset->get_decode_handle
# Line 379  sub parse_byte_stream ($$$$;$) { Line 412  sub parse_byte_stream ($$$$;$) {
412    
413      ## Step 3      ## Step 3
414      if ($byte_buffer =~ /^\xFE\xFF/) {      if ($byte_buffer =~ /^\xFE\xFF/) {
415        $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');        $charset = Message::Charset::Info->get_by_html_name ('utf-16be');
416        ($char_stream, $e_status) = $charset->get_decode_handle        ($char_stream, $e_status) = $charset->get_decode_handle
417            ($byte_stream, allow_error_reporting => 1,            ($byte_stream, allow_error_reporting => 1,
418             allow_fallback => 1, byte_buffer => \$byte_buffer);             allow_fallback => 1, byte_buffer => \$byte_buffer);
419        $self->{confident} = 1;        $self->{confident} = 1;
420        last SNIFFING;        last SNIFFING;
421      } elsif ($byte_buffer =~ /^\xFF\xFE/) {      } elsif ($byte_buffer =~ /^\xFF\xFE/) {
422        $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');        $charset = Message::Charset::Info->get_by_html_name ('utf-16le');
423        ($char_stream, $e_status) = $charset->get_decode_handle        ($char_stream, $e_status) = $charset->get_decode_handle
424            ($byte_stream, allow_error_reporting => 1,            ($byte_stream, allow_error_reporting => 1,
425             allow_fallback => 1, byte_buffer => \$byte_buffer);             allow_fallback => 1, byte_buffer => \$byte_buffer);
426        $self->{confident} = 1;        $self->{confident} = 1;
427        last SNIFFING;        last SNIFFING;
428      } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {      } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
429        $charset = Message::Charset::Info->get_by_iana_name ('utf-8');        $charset = Message::Charset::Info->get_by_html_name ('utf-8');
430        ($char_stream, $e_status) = $charset->get_decode_handle        ($char_stream, $e_status) = $charset->get_decode_handle
431            ($byte_stream, allow_error_reporting => 1,            ($byte_stream, allow_error_reporting => 1,
432             allow_fallback => 1, byte_buffer => \$byte_buffer);             allow_fallback => 1, byte_buffer => \$byte_buffer);
# Line 412  sub parse_byte_stream ($$$$;$) { Line 445  sub parse_byte_stream ($$$$;$) {
445      $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string      $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
446          ($byte_buffer);          ($byte_buffer);
447      if (defined $charset_name) {      if (defined $charset_name) {
448        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);        $charset = Message::Charset::Info->get_by_html_name ($charset_name);
449    
450        ## ISSUE: Unsupported encoding is not ignored according to the spec.        ## ISSUE: Unsupported encoding is not ignored according to the spec.
451        require Whatpm::Charset::DecodeHandle;        require Whatpm::Charset::DecodeHandle;
# Line 423  sub parse_byte_stream ($$$$;$) { Line 456  sub parse_byte_stream ($$$$;$) {
456             allow_fallback => 1, byte_buffer => \$byte_buffer);             allow_fallback => 1, byte_buffer => \$byte_buffer);
457        if ($char_stream) {        if ($char_stream) {
458          $buffer->{buffer} = $byte_buffer;          $buffer->{buffer} = $byte_buffer;
459          !!!parse-error (type => 'sniffing:chardet', ## TODO: type name          !!!parse-error (type => 'sniffing:chardet',
460                          value => $charset_name,                          text => $charset_name,
461                          level => $self->{info_level},                          level => $self->{level}->{info},
462                            layer => 'encode',
463                          line => 1, column => 1);                          line => 1, column => 1);
464          $self->{confident} = 0;          $self->{confident} = 0;
465          last SNIFFING;          last SNIFFING;
# Line 434  sub parse_byte_stream ($$$$;$) { Line 468  sub parse_byte_stream ($$$$;$) {
468    
469      ## Step 7: default      ## Step 7: default
470      ## TODO: Make this configurable.      ## TODO: Make this configurable.
471      $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');      $charset = Message::Charset::Info->get_by_html_name ('windows-1252');
472          ## NOTE: We choose |windows-1252| here, since |utf-8| should be          ## NOTE: We choose |windows-1252| here, since |utf-8| should be
473          ## detectable in the step 6.          ## detectable in the step 6.
474      require Whatpm::Charset::DecodeHandle;      require Whatpm::Charset::DecodeHandle;
# Line 446  sub parse_byte_stream ($$$$;$) { Line 480  sub parse_byte_stream ($$$$;$) {
480                                         allow_fallback => 1,                                         allow_fallback => 1,
481                                         byte_buffer => \$byte_buffer);                                         byte_buffer => \$byte_buffer);
482      $buffer->{buffer} = $byte_buffer;      $buffer->{buffer} = $byte_buffer;
483      !!!parse-error (type => 'sniffing:default', ## TODO: type name      !!!parse-error (type => 'sniffing:default',
484                      value => 'windows-1252',                      text => 'windows-1252',
485                      level => $self->{info_level},                      level => $self->{level}->{info},
486                      line => 1, column => 1);                      line => 1, column => 1,
487                        layer => 'encode');
488      $self->{confident} = 0;      $self->{confident} = 0;
489    } # SNIFFING    } # SNIFFING
490    
   $self->{input_encoding} = $charset->get_iana_name;  
491    if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {    if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
492      !!!parse-error (type => 'chardecode:fallback', ## TODO: type name      $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
493                      value => $self->{input_encoding},      !!!parse-error (type => 'chardecode:fallback',
494                      level => $self->{unsupported_level},                      #text => $self->{input_encoding},
495                      line => 1, column => 1);                      level => $self->{level}->{uncertain},
496                        line => 1, column => 1,
497                        layer => 'encode');
498    } elsif (not ($e_status &    } elsif (not ($e_status &
499                  Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {                  Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL ())) {
500      !!!parse-error (type => 'chardecode:no error', ## TODO: type name      $self->{input_encoding} = $charset->get_iana_name;
501                      value => $self->{input_encoding},      !!!parse-error (type => 'chardecode:no error',
502                      level => $self->{unsupported_level},                      text => $self->{input_encoding},
503                      line => 1, column => 1);                      level => $self->{level}->{uncertain},
504                        line => 1, column => 1,
505                        layer => 'encode');
506      } else {
507        $self->{input_encoding} = $charset->get_iana_name;
508    }    }
509    
510    $self->{change_encoding} = sub {    $self->{change_encoding} = sub {
# Line 472  sub parse_byte_stream ($$$$;$) { Line 512  sub parse_byte_stream ($$$$;$) {
512      $charset_name = shift;      $charset_name = shift;
513      my $token = shift;      my $token = shift;
514    
515      $charset = Message::Charset::Info->get_by_iana_name ($charset_name);      $charset = Message::Charset::Info->get_by_html_name ($charset_name);
516      ($char_stream, $e_status) = $charset->get_decode_handle      ($char_stream, $e_status) = $charset->get_decode_handle
517          ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,          ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
518           byte_buffer => \ $buffer->{buffer});           byte_buffer => \ $buffer->{buffer});
# Line 481  sub parse_byte_stream ($$$$;$) { Line 521  sub parse_byte_stream ($$$$;$) {
521        ## "Change the encoding" algorithm:        ## "Change the encoding" algorithm:
522    
523        ## Step 1            ## Step 1    
524        if ($charset->{iana_names}->{'utf-16'}) { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?        if ($charset->{category} &
525          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');            Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
526            $charset = Message::Charset::Info->get_by_html_name ('utf-8');
527          ($char_stream, $e_status) = $charset->get_decode_handle          ($char_stream, $e_status) = $charset->get_decode_handle
528              ($byte_stream,              ($byte_stream,
529               byte_buffer => \ $buffer->{buffer});               byte_buffer => \ $buffer->{buffer});
# Line 492  sub parse_byte_stream ($$$$;$) { Line 533  sub parse_byte_stream ($$$$;$) {
533        ## Step 2        ## Step 2
534        if (defined $self->{input_encoding} and        if (defined $self->{input_encoding} and
535            $self->{input_encoding} eq $charset_name) {            $self->{input_encoding} eq $charset_name) {
536          !!!parse-error (type => 'charset label:matching', ## TODO: type          !!!parse-error (type => 'charset label:matching',
537                          value => $charset_name,                          text => $charset_name,
538                          level => $self->{info_level});                          level => $self->{level}->{info});
539          $self->{confident} = 1;          $self->{confident} = 1;
540          return;          return;
541        }        }
542    
543        !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.        !!!parse-error (type => 'charset label detected',
544            ':'.$charset_name, level => 'w', token => $token);                        text => $self->{input_encoding},
545                          value => $charset_name,
546                          level => $self->{level}->{warn},
547                          token => $token);
548                
549        ## Step 3        ## Step 3
550        # if (can) {        # if (can) {
# Line 516  sub parse_byte_stream ($$$$;$) { Line 560  sub parse_byte_stream ($$$$;$) {
560    
561    my $char_onerror = sub {    my $char_onerror = sub {
562      my (undef, $type, %opt) = @_;      my (undef, $type, %opt) = @_;
563      !!!parse-error (%opt, type => $type,      !!!parse-error (layer => 'encode',
564                      line => $self->{line}, column => $self->{column} + 1);                      line => $self->{line}, column => $self->{column} + 1,
565                        %opt, type => $type);
566      if ($opt{octets}) {      if ($opt{octets}) {
567        ${$opt{octets}} = "\x{FFFD}"; # relacement character        ${$opt{octets}} = "\x{FFFD}"; # relacement character
568      }      }
569    };    };
570    $char_stream->onerror ($char_onerror);  
571      my $wrapped_char_stream = $get_wrapper->($char_stream);
572      $wrapped_char_stream->onerror ($char_onerror);
573    
574    my @args = @_; shift @args; # $s    my @args = @_; shift @args; # $s
575    my $return;    my $return;
576    try {    try {
577      $return = $self->parse_char_stream ($char_stream, @args);        $return = $self->parse_char_stream ($wrapped_char_stream, @args);  
578    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
579      ## NOTE: Invoked after {change_encoding}.      ## NOTE: Invoked after {change_encoding}.
580    
     $self->{input_encoding} = $charset->get_iana_name;  
581      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
582        !!!parse-error (type => 'chardecode:fallback', ## TODO: type name        $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
583                        value => $self->{input_encoding},        !!!parse-error (type => 'chardecode:fallback',
584                        level => $self->{unsupported_level},                        level => $self->{level}->{uncertain},
585                        line => 1, column => 1);                        #text => $self->{input_encoding},
586                          line => 1, column => 1,
587                          layer => 'encode');
588      } elsif (not ($e_status &      } elsif (not ($e_status &
589                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL ())) {
590        !!!parse-error (type => 'chardecode:no error', ## TODO: type name        $self->{input_encoding} = $charset->get_iana_name;
591                        value => $self->{input_encoding},        !!!parse-error (type => 'chardecode:no error',
592                        level => $self->{unsupported_level},                        text => $self->{input_encoding},
593                        line => 1, column => 1);                        level => $self->{level}->{uncertain},
594                          line => 1, column => 1,
595                          layer => 'encode');
596        } else {
597          $self->{input_encoding} = $charset->get_iana_name;
598      }      }
599      $self->{confident} = 1;      $self->{confident} = 1;
600      $char_stream->onerror ($char_onerror);  
601      $return = $self->parse_char_stream ($char_stream, @args);      $wrapped_char_stream = $get_wrapper->($char_stream);
602        $wrapped_char_stream->onerror ($char_onerror);
603    
604        $return = $self->parse_char_stream ($wrapped_char_stream, @args);
605    };    };
606    return $return;    return $return;
607  } # parse_byte_stream  } # parse_byte_stream
# Line 560  sub parse_byte_stream ($$$$;$) { Line 615  sub parse_byte_stream ($$$$;$) {
615  ## such as |parse_byte_string| in this module, must ensure that it does  ## such as |parse_byte_string| in this module, must ensure that it does
616  ## strip the BOM and never strip any ZWNBSP.  ## strip the BOM and never strip any ZWNBSP.
617    
618  sub parse_char_string ($$$;$) {  sub parse_char_string ($$$;$$) {
619      #my ($self, $s, $doc, $onerror, $get_wrapper) = @_;
620    my $self = shift;    my $self = shift;
   require utf8;  
621    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $s = ref $_[0] ? $_[0] : \($_[0]);
622    open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;    require Whatpm::Charset::DecodeHandle;
623      my $input = Whatpm::Charset::DecodeHandle::CharString->new ($s);
624      if ($_[3]) {
625        $input = $_[3]->($input);
626      }
627    return $self->parse_char_stream ($input, @_[1..$#_]);    return $self->parse_char_stream ($input, @_[1..$#_]);
628  } # parse_char_string  } # parse_char_string
629  *parse_string = \&parse_char_string;  *parse_string = \&parse_char_string; ## NOTE: Alias for backward compatibility.
630    
631  sub parse_char_stream ($$$;$) {  sub parse_char_stream ($$$;$) {
632    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
# Line 580  sub parse_char_stream ($$$;$) { Line 639  sub parse_char_stream ($$$;$) {
639    $self->{confident} = 1 unless exists $self->{confident};    $self->{confident} = 1 unless exists $self->{confident};
640    $self->{document}->input_encoding ($self->{input_encoding})    $self->{document}->input_encoding ($self->{input_encoding})
641        if defined $self->{input_encoding};        if defined $self->{input_encoding};
642    ## TODO: |{input_encoding}| is needless?
643    
644    my $i = 0;    my $i = 0;
645    $self->{line_prev} = $self->{line} = 1;    $self->{line_prev} = $self->{line} = 1;
# Line 590  sub parse_char_stream ($$$;$) { Line 650  sub parse_char_stream ($$$;$) {
650      pop @{$self->{prev_char}};      pop @{$self->{prev_char}};
651      unshift @{$self->{prev_char}}, $self->{next_char};      unshift @{$self->{prev_char}}, $self->{next_char};
652    
653      my $char;      my $char = '';
654      if (defined $self->{next_next_char}) {      if (defined $self->{next_next_char}) {
655        $char = $self->{next_next_char};        $char = $self->{next_next_char};
656        delete $self->{next_next_char};        delete $self->{next_next_char};
657          $self->{next_char} = ord $char;
658      } else {      } else {
659        $char = $input->getc;        if ($input->read ($char, 1)) {
660            $self->{next_char} = ord $char;
661          } else {
662            $self->{next_char} = -1;
663            return;
664          }
665      }      }
     $self->{next_char} = -1 and return unless defined $char;  
     $self->{next_char} = ord $char;  
666    
667      ($self->{line_prev}, $self->{column_prev})      ($self->{line_prev}, $self->{column_prev})
668          = ($self->{line}, $self->{column});          = ($self->{line}, $self->{column});
# Line 610  sub parse_char_stream ($$$;$) { Line 674  sub parse_char_stream ($$$;$) {
674        $self->{column} = 0;        $self->{column} = 0;
675      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
676        !!!cp ('j2');        !!!cp ('j2');
677        my $next = $input->getc;  ## TODO: support for abort/streaming
678        if (defined $next and $next ne "\x0A") {        my $next = '';
679          if ($input->read ($next, 1) and $next ne "\x0A") {
680          $self->{next_next_char} = $next;          $self->{next_next_char} = $next;
681        }        }
682        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
# Line 629  sub parse_char_stream ($$$;$) { Line 694  sub parse_char_stream ($$$;$) {
694               (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or               (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
695               (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or               (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
696               (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or               (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
697    ## ISSUE: U+FDE0-U+FDEF are not excluded
698               {               {
699                0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,                0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
700                0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,                0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
# Line 641  sub parse_char_stream ($$$;$) { Line 707  sub parse_char_stream ($$$;$) {
707                0x10FFFE => 1, 0x10FFFF => 1,                0x10FFFE => 1, 0x10FFFF => 1,
708               }->{$self->{next_char}}) {               }->{$self->{next_char}}) {
709        !!!cp ('j5');        !!!cp ('j5');
710        !!!parse-error (type => 'control char', level => $self->{must_level});        if ($self->{next_char} < 0x10000) {
711  ## TODO: error type documentation          !!!parse-error (type => 'control char',
712                            text => (sprintf 'U+%04X', $self->{next_char}));
713          } else {
714            !!!parse-error (type => 'control char',
715                            text => (sprintf 'U-%08X', $self->{next_char}));
716          }
717      }      }
718    };    };
719    $self->{prev_char} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
720    $self->{next_char} = -1;    $self->{next_char} = -1;
721    
722      $self->{read_until} = sub {
723        #my ($scalar, $specials_range, $offset) = @_;
724        my $specials_range = $_[1];
725        return 0 if defined $self->{next_next_char};
726        my $count = $input->manakai_read_until
727           ($_[0],
728            qr/(?![$specials_range\x{FDD0}-\x{FDDF}\x{FFFE}\x{FFFF}\x{1FFFE}\x{1FFFF}\x{2FFFE}\x{2FFFF}\x{3FFFE}\x{3FFFF}\x{4FFFE}\x{4FFFF}\x{5FFFE}\x{5FFFF}\x{6FFFE}\x{6FFFF}\x{7FFFE}\x{7FFFF}\x{8FFFE}\x{8FFFF}\x{9FFFE}\x{9FFFF}\x{AFFFE}\x{AFFFF}\x{BFFFE}\x{BFFFF}\x{CFFFE}\x{CFFFF}\x{DFFFE}\x{DFFFF}\x{EFFFE}\x{EFFFF}\x{FFFFE}\x{FFFFF}])[\x20-\x7E\xA0-\x{D7FF}\x{E000}-\x{10FFFD}]/,
729            $_[2]);
730        if ($count) {
731          $self->{column} += $count;
732          $self->{column_prev} += $count;
733          $self->{prev_char} = [-1, -1, -1];
734          $self->{next_char} = -1;
735        }
736        return $count;
737      }; # $self->{read_until}
738    
739    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
740      my (%opt) = @_;      my (%opt) = @_;
741      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
# Line 671  sub parse_char_stream ($$$;$) { Line 759  sub parse_char_stream ($$$;$) {
759  sub new ($) {  sub new ($) {
760    my $class = shift;    my $class = shift;
761    my $self = bless {    my $self = bless {
762      must_level => 'm',      level => {must => 'm',
763      should_level => 's',                should => 's',
764      good_level => 'w',                warn => 'w',
765      warn_level => 'w',                info => 'i',
766      info_level => 'i',                uncertain => 'u'},
     unsupported_level => 'u',  
767    }, $class;    }, $class;
768    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
769      $self->{next_char} = -1;      $self->{next_char} = -1;
# Line 706  sub RCDATA_CONTENT_MODEL () { CM_ENTITY Line 793  sub RCDATA_CONTENT_MODEL () { CM_ENTITY
793  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
794    
795  sub DATA_STATE () { 0 }  sub DATA_STATE () { 0 }
796  sub ENTITY_DATA_STATE () { 1 }  #sub ENTITY_DATA_STATE () { 1 }
797  sub TAG_OPEN_STATE () { 2 }  sub TAG_OPEN_STATE () { 2 }
798  sub CLOSE_TAG_OPEN_STATE () { 3 }  sub CLOSE_TAG_OPEN_STATE () { 3 }
799  sub TAG_NAME_STATE () { 4 }  sub TAG_NAME_STATE () { 4 }
# Line 717  sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 Line 804  sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8
804  sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }  sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
805  sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }  sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
806  sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }  sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
807  sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }  #sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
808  sub MARKUP_DECLARATION_OPEN_STATE () { 13 }  sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
809  sub COMMENT_START_STATE () { 14 }  sub COMMENT_START_STATE () { 14 }
810  sub COMMENT_START_DASH_STATE () { 15 }  sub COMMENT_START_DASH_STATE () { 15 }
# Line 740  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STAT Line 827  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STAT
827  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
828  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
829  sub SELF_CLOSING_START_TAG_STATE () { 34 }  sub SELF_CLOSING_START_TAG_STATE () { 34 }
830  sub CDATA_BLOCK_STATE () { 35 }  sub CDATA_SECTION_STATE () { 35 }
831    sub MD_HYPHEN_STATE () { 36 } # "markup declaration open state" in the spec
832    sub MD_DOCTYPE_STATE () { 37 } # "markup declaration open state" in the spec
833    sub MD_CDATA_STATE () { 38 } # "markup declaration open state" in the spec
834    sub CDATA_PCDATA_CLOSE_TAG_STATE () { 39 } # "close tag open state" in the spec
835    sub CDATA_SECTION_MSE1_STATE () { 40 } # "CDATA section state" in the spec
836    sub CDATA_SECTION_MSE2_STATE () { 41 } # "CDATA section state" in the spec
837    sub PUBLIC_STATE () { 42 } # "after DOCTYPE name state" in the spec
838    sub SYSTEM_STATE () { 43 } # "after DOCTYPE name state" in the spec
839    ## NOTE: "Entity data state", "entity in attribute value state", and
840    ## "consume a character reference" algorithm are jointly implemented
841    ## using the following six states:
842    sub ENTITY_STATE () { 44 }
843    sub ENTITY_HASH_STATE () { 45 }
844    sub NCR_NUM_STATE () { 46 }
845    sub HEXREF_X_STATE () { 47 }
846    sub HEXREF_HEX_STATE () { 48 }
847    sub ENTITY_NAME_STATE () { 49 }
848    
849  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
850  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 793  sub IN_COLUMN_GROUP_IM () { 0b10 } Line 897  sub IN_COLUMN_GROUP_IM () { 0b10 }
897  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
898    my $self = shift;    my $self = shift;
899    $self->{state} = DATA_STATE; # MUST    $self->{state} = DATA_STATE; # MUST
900      #$self->{state_keyword}; # initialized when used
901      #$self->{entity__value}; # initialized when used
902      #$self->{entity__match}; # initialized when used
903    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
904    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token};
905    undef $self->{current_attribute};    undef $self->{current_attribute};
906    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
907    undef $self->{last_attribute_value_state};    #$self->{prev_state}; # initialized when used
908    delete $self->{self_closing};    delete $self->{self_closing};
   $self->{char} = [];  
909    # $self->{next_char}    # $self->{next_char}
910    !!!next-input-character;    !!!next-input-character;
911    $self->{token} = [];    $self->{token} = [];
# Line 823  sub _initialize_tokenizer ($) { Line 929  sub _initialize_tokenizer ($) {
929  ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|  ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
930  ##     while the token is pushed back to the stack.  ##     while the token is pushed back to the stack.
931    
 ## ISSUE: "When a DOCTYPE token is created, its  
 ## <i>self-closing flag</i> must be unset (its other state is that it  
 ## be set), and its attributes list must be empty.": Wrong subject?  
   
932  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
933    
934  ## Before each step, UA MAY check to see if either one of the scripts in  ## Before each step, UA MAY check to see if either one of the scripts in
# Line 835  sub _initialize_tokenizer ($) { Line 937  sub _initialize_tokenizer ($) {
937  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
938  ## and removed from the list.  ## and removed from the list.
939    
940  ## NOTE: HTML5 "Writing HTML documents" section, applied to  ## TODO: Polytheistic slash SHOULD NOT be used. (Applied only to atheists.)
941  ## documents and not to user agents and conformance checkers,  ## (This requirement was dropped from HTML5 spec, unfortunately.)
 ## contains some requirements that are not detected by the  
 ## parsing algorithm:  
 ## - Some requirements on character encoding declarations. ## TODO  
 ## - "Elements MUST NOT contain content that their content model disallows."  
 ##   ... Some are parse error, some are not (will be reported by c.c.).  
 ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO  
 ## - Text (in elements, attributes, and comments) SHOULD NOT contain  
 ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)  
   
 ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot  
 ## be detected by the HTML5 parsing algorithm:  
 ## - Text,  
942    
943  sub _get_next_token ($) {  sub _get_next_token ($) {
944    my $self = shift;    my $self = shift;
# Line 872  sub _get_next_token ($) { Line 962  sub _get_next_token ($) {
962          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
963              not $self->{escape}) {              not $self->{escape}) {
964            !!!cp (1);            !!!cp (1);
965            $self->{state} = ENTITY_DATA_STATE;            ## NOTE: In the spec, the tokenizer is switched to the
966              ## "entity data state".  In this implementation, the tokenizer
967              ## is switched to the |ENTITY_STATE|, which is an implementation
968              ## of the "consume a character reference" algorithm.
969              $self->{entity_additional} = -1;
970              $self->{prev_state} = DATA_STATE;
971              $self->{state} = ENTITY_STATE;
972            !!!next-input-character;            !!!next-input-character;
973            redo A;            redo A;
974          } else {          } else {
# Line 936  sub _get_next_token ($) { Line 1032  sub _get_next_token ($) {
1032                     data => chr $self->{next_char},                     data => chr $self->{next_char},
1033                     line => $self->{line}, column => $self->{column},                     line => $self->{line}, column => $self->{column},
1034                    };                    };
1035          $self->{read_until}->($token->{data}, q[-!<>&], length $token->{data});
1036    
1037        ## Stay in the data state        ## Stay in the data state
1038        !!!next-input-character;        !!!next-input-character;
1039    
1040        !!!emit ($token);        !!!emit ($token);
1041    
1042        redo A;        redo A;
     } elsif ($self->{state} == ENTITY_DATA_STATE) {  
       ## (cannot happen in CDATA state)  
   
       my ($l, $c) = ($self->{line_prev}, $self->{column_prev});  
         
       my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);  
   
       $self->{state} = DATA_STATE;  
       # next-input-character is already done  
   
       unless (defined $token) {  
         !!!cp (13);  
         !!!emit ({type => CHARACTER_TOKEN, data => '&',  
                   line => $l, column => $c,  
                  });  
       } else {  
         !!!cp (14);  
         !!!emit ($token);  
       }  
   
       redo A;  
1043      } elsif ($self->{state} == TAG_OPEN_STATE) {      } elsif ($self->{state} == TAG_OPEN_STATE) {
1044        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1045          if ($self->{next_char} == 0x002F) { # /          if ($self->{next_char} == 0x002F) { # /
# Line 1059  sub _get_next_token ($) { Line 1136  sub _get_next_token ($) {
1136          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1137        }        }
1138      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1139          ## NOTE: The "close tag open state" in the spec is implemented as
1140          ## |CLOSE_TAG_OPEN_STATE| and |CDATA_PCDATA_CLOSE_TAG_STATE|.
1141    
1142        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1143        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1144          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1145              $self->{state} = CDATA_PCDATA_CLOSE_TAG_STATE;
1146            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            $self->{state_keyword} = '';
1147            my @next_char;            ## Reconsume.
1148            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            redo A;
             push @next_char, $self->{next_char};  
             my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);  
             my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;  
             if ($self->{next_char} == $c or $self->{next_char} == $C) {  
               !!!cp (24);  
               !!!next-input-character;  
               next TAGNAME;  
             } else {  
               !!!cp (25);  
               $self->{next_char} = shift @next_char; # reconsume  
               !!!back-next-input-character (@next_char);  
               $self->{state} = DATA_STATE;  
   
               !!!emit ({type => CHARACTER_TOKEN, data => '</',  
                         line => $l, column => $c,  
                        });  
     
               redo A;  
             }  
           }  
           push @next_char, $self->{next_char};  
         
           unless ($self->{next_char} == 0x0009 or # HT  
                   $self->{next_char} == 0x000A or # LF  
                   $self->{next_char} == 0x000B or # VT  
                   $self->{next_char} == 0x000C or # FF  
                   $self->{next_char} == 0x0020 or # SP  
                   $self->{next_char} == 0x003E or # >  
                   $self->{next_char} == 0x002F or # /  
                   $self->{next_char} == -1) {  
             !!!cp (26);  
             $self->{next_char} = shift @next_char; # reconsume  
             !!!back-next-input-character (@next_char);  
             $self->{state} = DATA_STATE;  
             !!!emit ({type => CHARACTER_TOKEN, data => '</',  
                       line => $l, column => $c,  
                      });  
             redo A;  
           } else {  
             !!!cp (27);  
             $self->{next_char} = shift @next_char;  
             !!!back-next-input-character (@next_char);  
             # and consume...  
           }  
1149          } else {          } else {
1150            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1151              ## NOTE: See <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>.
1152            !!!cp (28);            !!!cp (28);
           # next-input-character is already done  
1153            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1154              ## Reconsume.
1155            !!!emit ({type => CHARACTER_TOKEN, data => '</',            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1156                      line => $l, column => $c,                      line => $l, column => $c,
1157                     });                     });
1158            redo A;            redo A;
1159          }          }
1160        }        }
1161          
1162        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{next_char} and
1163            $self->{next_char} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1164          !!!cp (29);          !!!cp (29);
# Line 1168  sub _get_next_token ($) { Line 1205  sub _get_next_token ($) {
1205                                    line => $self->{line_prev}, # "<" of "</"                                    line => $self->{line_prev}, # "<" of "</"
1206                                    column => $self->{column_prev} - 1,                                    column => $self->{column_prev} - 1,
1207                                   };                                   };
1208          ## $self->{next_char} is intentionally left as is          ## NOTE: $self->{next_char} is intentionally left as is.
1209          redo A;          ## Although the "anything else" case of the spec not explicitly
1210            ## states that the next input character is to be reconsumed,
1211            ## it will be included to the |data| of the comment token
1212            ## generated from the bogus end tag, as defined in the
1213            ## "bogus comment state" entry.
1214            redo A;
1215          }
1216        } elsif ($self->{state} == CDATA_PCDATA_CLOSE_TAG_STATE) {
1217          my $ch = substr $self->{last_emitted_start_tag_name}, length $self->{state_keyword}, 1;
1218          if (length $ch) {
1219            my $CH = $ch;
1220            $ch =~ tr/a-z/A-Z/;
1221            my $nch = chr $self->{next_char};
1222            if ($nch eq $ch or $nch eq $CH) {
1223              !!!cp (24);
1224              ## Stay in the state.
1225              $self->{state_keyword} .= $nch;
1226              !!!next-input-character;
1227              redo A;
1228            } else {
1229              !!!cp (25);
1230              $self->{state} = DATA_STATE;
1231              ## Reconsume.
1232              !!!emit ({type => CHARACTER_TOKEN,
1233                        data => '</' . $self->{state_keyword},
1234                        line => $self->{line_prev},
1235                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1236                       });
1237              redo A;
1238            }
1239          } else { # after "<{tag-name}"
1240            unless ({
1241                     0x0009 => 1, # HT
1242                     0x000A => 1, # LF
1243                     0x000B => 1, # VT
1244                     0x000C => 1, # FF
1245                     0x0020 => 1, # SP
1246                     0x003E => 1, # >
1247                     0x002F => 1, # /
1248                     -1 => 1, # EOF
1249                    }->{$self->{next_char}}) {
1250              !!!cp (26);
1251              ## Reconsume.
1252              $self->{state} = DATA_STATE;
1253              !!!emit ({type => CHARACTER_TOKEN,
1254                        data => '</' . $self->{state_keyword},
1255                        line => $self->{line_prev},
1256                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1257                       });
1258              redo A;
1259            } else {
1260              !!!cp (27);
1261              $self->{current_token}
1262                  = {type => END_TAG_TOKEN,
1263                     tag_name => $self->{last_emitted_start_tag_name},
1264                     line => $self->{line_prev},
1265                     column => $self->{column_prev} - 1 - length $self->{state_keyword}};
1266              $self->{state} = TAG_NAME_STATE;
1267              ## Reconsume.
1268              redo A;
1269            }
1270        }        }
1271      } elsif ($self->{state} == TAG_NAME_STATE) {      } elsif ($self->{state} == TAG_NAME_STATE) {
1272        if ($self->{next_char} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
# Line 1339  sub _get_next_token ($) { Line 1436  sub _get_next_token ($) {
1436          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1437              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1438            !!!cp (57);            !!!cp (57);
1439            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});            !!!parse-error (type => 'duplicate attribute', text => $self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1440            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1441          } else {          } else {
1442            !!!cp (58);            !!!cp (58);
# Line 1510  sub _get_next_token ($) { Line 1607  sub _get_next_token ($) {
1607    
1608          redo A;          redo A;
1609        } else {        } else {
1610          !!!cp (82);          if ($self->{next_char} == 0x0022 or # "
1611                $self->{next_char} == 0x0027) { # '
1612              !!!cp (78);
1613              !!!parse-error (type => 'bad attribute name');
1614            } else {
1615              !!!cp (82);
1616            }
1617          $self->{current_attribute}          $self->{current_attribute}
1618              = {name => chr ($self->{next_char}),              = {name => chr ($self->{next_char}),
1619                 value => '',                 value => '',
# Line 1545  sub _get_next_token ($) { Line 1648  sub _get_next_token ($) {
1648          !!!next-input-character;          !!!next-input-character;
1649          redo A;          redo A;
1650        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1651            !!!parse-error (type => 'empty unquoted attribute value');
1652          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1653            !!!cp (87);            !!!cp (87);
1654            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
# Line 1609  sub _get_next_token ($) { Line 1713  sub _get_next_token ($) {
1713          redo A;          redo A;
1714        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1715          !!!cp (96);          !!!cp (96);
1716          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1717          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1718            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1719            ## implementation of the "consume a character reference" algorithm.
1720            $self->{prev_state} = $self->{state};
1721            $self->{entity_additional} = 0x0022; # "
1722            $self->{state} = ENTITY_STATE;
1723          !!!next-input-character;          !!!next-input-character;
1724          redo A;          redo A;
1725        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
# Line 1639  sub _get_next_token ($) { Line 1748  sub _get_next_token ($) {
1748        } else {        } else {
1749          !!!cp (100);          !!!cp (100);
1750          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{current_attribute}->{value} .= chr ($self->{next_char});
1751            $self->{read_until}->($self->{current_attribute}->{value},
1752                                  q["&],
1753                                  length $self->{current_attribute}->{value});
1754    
1755          ## Stay in the state          ## Stay in the state
1756          !!!next-input-character;          !!!next-input-character;
1757          redo A;          redo A;
# Line 1651  sub _get_next_token ($) { Line 1764  sub _get_next_token ($) {
1764          redo A;          redo A;
1765        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1766          !!!cp (102);          !!!cp (102);
1767          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1768          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1769            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1770            ## implementation of the "consume a character reference" algorithm.
1771            $self->{entity_additional} = 0x0027; # '
1772            $self->{prev_state} = $self->{state};
1773            $self->{state} = ENTITY_STATE;
1774          !!!next-input-character;          !!!next-input-character;
1775          redo A;          redo A;
1776        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
# Line 1681  sub _get_next_token ($) { Line 1799  sub _get_next_token ($) {
1799        } else {        } else {
1800          !!!cp (106);          !!!cp (106);
1801          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{current_attribute}->{value} .= chr ($self->{next_char});
1802            $self->{read_until}->($self->{current_attribute}->{value},
1803                                  q['&],
1804                                  length $self->{current_attribute}->{value});
1805    
1806          ## Stay in the state          ## Stay in the state
1807          !!!next-input-character;          !!!next-input-character;
1808          redo A;          redo A;
# Line 1697  sub _get_next_token ($) { Line 1819  sub _get_next_token ($) {
1819          redo A;          redo A;
1820        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1821          !!!cp (108);          !!!cp (108);
1822          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1823          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1824            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1825            ## implementation of the "consume a character reference" algorithm.
1826            $self->{entity_additional} = -1;
1827            $self->{prev_state} = $self->{state};
1828            $self->{state} = ENTITY_STATE;
1829          !!!next-input-character;          !!!next-input-character;
1830          redo A;          redo A;
1831        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
# Line 1758  sub _get_next_token ($) { Line 1885  sub _get_next_token ($) {
1885            !!!cp (116);            !!!cp (116);
1886          }          }
1887          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{current_attribute}->{value} .= chr ($self->{next_char});
1888            $self->{read_until}->($self->{current_attribute}->{value},
1889                                  q["'=& >],
1890                                  length $self->{current_attribute}->{value});
1891    
1892          ## Stay in the state          ## Stay in the state
1893          !!!next-input-character;          !!!next-input-character;
1894          redo A;          redo A;
1895        }        }
     } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {  
       my $token = $self->_tokenize_attempt_to_consume_an_entity  
           (1,  
            $self->{last_attribute_value_state}  
              == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "  
            $self->{last_attribute_value_state}  
              == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '  
            -1);  
   
       unless (defined $token) {  
         !!!cp (117);  
         $self->{current_attribute}->{value} .= '&';  
       } else {  
         !!!cp (118);  
         $self->{current_attribute}->{value} .= $token->{data};  
         $self->{current_attribute}->{has_reference} = $token->{has_reference};  
         ## ISSUE: spec says "append the returned character token to the current attribute's value"  
       }  
   
       $self->{state} = $self->{last_attribute_value_state};  
       # next-input-character is already done  
       redo A;  
1896      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1897        if ($self->{next_char} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1898            $self->{next_char} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
# Line 1903  sub _get_next_token ($) { Line 2012  sub _get_next_token ($) {
2012        }        }
2013      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
2014        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
         
       ## NOTE: Set by the previous state  
       #my $token = {type => COMMENT_TOKEN, data => ''};  
2015    
2016        BC: {        ## NOTE: Unlike spec's "bogus comment state", this implementation
2017          if ($self->{next_char} == 0x003E) { # >        ## consumes characters one-by-one basis.
2018            !!!cp (124);        
2019            $self->{state} = DATA_STATE;        if ($self->{next_char} == 0x003E) { # >
2020            !!!next-input-character;          !!!cp (124);
2021            $self->{state} = DATA_STATE;
2022            !!!emit ($self->{current_token}); # comment          !!!next-input-character;
   
           redo A;  
         } elsif ($self->{next_char} == -1) {  
           !!!cp (125);  
           $self->{state} = DATA_STATE;  
           ## reconsume  
2023    
2024            !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2025            redo A;
2026          } elsif ($self->{next_char} == -1) {
2027            !!!cp (125);
2028            $self->{state} = DATA_STATE;
2029            ## reconsume
2030    
2031            redo A;          !!!emit ($self->{current_token}); # comment
2032          } else {          redo A;
2033            !!!cp (126);        } else {
2034            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment          !!!cp (126);
2035            !!!next-input-character;          $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2036            redo BC;          $self->{read_until}->($self->{current_token}->{data},
2037          }                                q[>],
2038        } # BC                                length $self->{current_token}->{data});
2039    
2040        die "$0: _get_next_token: unexpected case [BC]";          ## Stay in the state.
2041            !!!next-input-character;
2042            redo A;
2043          }
2044      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2045        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
   
       my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);  
   
       my @next_char;  
       push @next_char, $self->{next_char};  
2046                
2047        if ($self->{next_char} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2048            !!!cp (133);
2049            $self->{state} = MD_HYPHEN_STATE;
2050          !!!next-input-character;          !!!next-input-character;
2051          push @next_char, $self->{next_char};          redo A;
         if ($self->{next_char} == 0x002D) { # -  
           !!!cp (127);  
           $self->{current_token} = {type => COMMENT_TOKEN, data => '',  
                                     line => $l, column => $c,  
                                    };  
           $self->{state} = COMMENT_START_STATE;  
           !!!next-input-character;  
           redo A;  
         } else {  
           !!!cp (128);  
         }  
2052        } elsif ($self->{next_char} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
2053                 $self->{next_char} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
2054            ## ASCII case-insensitive.
2055            !!!cp (130);
2056            $self->{state} = MD_DOCTYPE_STATE;
2057            $self->{state_keyword} = chr $self->{next_char};
2058          !!!next-input-character;          !!!next-input-character;
2059          push @next_char, $self->{next_char};          redo A;
         if ($self->{next_char} == 0x004F or # O  
             $self->{next_char} == 0x006F) { # o  
           !!!next-input-character;  
           push @next_char, $self->{next_char};  
           if ($self->{next_char} == 0x0043 or # C  
               $self->{next_char} == 0x0063) { # c  
             !!!next-input-character;  
             push @next_char, $self->{next_char};  
             if ($self->{next_char} == 0x0054 or # T  
                 $self->{next_char} == 0x0074) { # t  
               !!!next-input-character;  
               push @next_char, $self->{next_char};  
               if ($self->{next_char} == 0x0059 or # Y  
                   $self->{next_char} == 0x0079) { # y  
                 !!!next-input-character;  
                 push @next_char, $self->{next_char};  
                 if ($self->{next_char} == 0x0050 or # P  
                     $self->{next_char} == 0x0070) { # p  
                   !!!next-input-character;  
                   push @next_char, $self->{next_char};  
                   if ($self->{next_char} == 0x0045 or # E  
                       $self->{next_char} == 0x0065) { # e  
                     !!!cp (129);  
                     ## TODO: What a stupid code this is!  
                     $self->{state} = DOCTYPE_STATE;  
                     $self->{current_token} = {type => DOCTYPE_TOKEN,  
                                               quirks => 1,  
                                               line => $l, column => $c,  
                                              };  
                     !!!next-input-character;  
                     redo A;  
                   } else {  
                     !!!cp (130);  
                   }  
                 } else {  
                   !!!cp (131);  
                 }  
               } else {  
                 !!!cp (132);  
               }  
             } else {  
               !!!cp (133);  
             }  
           } else {  
             !!!cp (134);  
           }  
         } else {  
           !!!cp (135);  
         }  
2060        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2061                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2062                 $self->{next_char} == 0x005B) { # [                 $self->{next_char} == 0x005B) { # [
2063            !!!cp (135.4);                
2064            $self->{state} = MD_CDATA_STATE;
2065            $self->{state_keyword} = '[';
2066          !!!next-input-character;          !!!next-input-character;
2067          push @next_char, $self->{next_char};          redo A;
         if ($self->{next_char} == 0x0043) { # C  
           !!!next-input-character;  
           push @next_char, $self->{next_char};  
           if ($self->{next_char} == 0x0044) { # D  
             !!!next-input-character;  
             push @next_char, $self->{next_char};  
             if ($self->{next_char} == 0x0041) { # A  
               !!!next-input-character;  
               push @next_char, $self->{next_char};  
               if ($self->{next_char} == 0x0054) { # T  
                 !!!next-input-character;  
                 push @next_char, $self->{next_char};  
                 if ($self->{next_char} == 0x0041) { # A  
                   !!!next-input-character;  
                   push @next_char, $self->{next_char};  
                   if ($self->{next_char} == 0x005B) { # [  
                     !!!cp (135.1);  
                     $self->{state} = CDATA_BLOCK_STATE;  
                     !!!next-input-character;  
                     redo A;  
                   } else {  
                     !!!cp (135.2);  
                   }  
                 } else {  
                   !!!cp (135.3);  
                 }  
               } else {  
                 !!!cp (135.4);                  
               }  
             } else {  
               !!!cp (135.5);  
             }  
           } else {  
             !!!cp (135.6);  
           }  
         } else {  
           !!!cp (135.7);  
         }  
2068        } else {        } else {
2069          !!!cp (136);          !!!cp (136);
2070        }        }
2071    
2072        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment',
2073        $self->{next_char} = shift @next_char;                        line => $self->{line_prev},
2074        !!!back-next-input-character (@next_char);                        column => $self->{column_prev} - 1);
2075          ## Reconsume.
2076        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2077        $self->{current_token} = {type => COMMENT_TOKEN, data => '',        $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2078                                  line => $l, column => $c,                                  line => $self->{line_prev},
2079                                    column => $self->{column_prev} - 1,
2080                                 };                                 };
2081        redo A;        redo A;
2082              } elsif ($self->{state} == MD_HYPHEN_STATE) {
2083        ## ISSUE: typos in spec: chacacters, is is a parse error        if ($self->{next_char} == 0x002D) { # -
2084        ## 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?          !!!cp (127);
2085            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2086                                      line => $self->{line_prev},
2087                                      column => $self->{column_prev} - 2,
2088                                     };
2089            $self->{state} = COMMENT_START_STATE;
2090            !!!next-input-character;
2091            redo A;
2092          } else {
2093            !!!cp (128);
2094            !!!parse-error (type => 'bogus comment',
2095                            line => $self->{line_prev},
2096                            column => $self->{column_prev} - 2);
2097            $self->{state} = BOGUS_COMMENT_STATE;
2098            ## Reconsume.
2099            $self->{current_token} = {type => COMMENT_TOKEN,
2100                                      data => '-',
2101                                      line => $self->{line_prev},
2102                                      column => $self->{column_prev} - 2,
2103                                     };
2104            redo A;
2105          }
2106        } elsif ($self->{state} == MD_DOCTYPE_STATE) {
2107          ## ASCII case-insensitive.
2108          if ($self->{next_char} == [
2109                undef,
2110                0x004F, # O
2111                0x0043, # C
2112                0x0054, # T
2113                0x0059, # Y
2114                0x0050, # P
2115              ]->[length $self->{state_keyword}] or
2116              $self->{next_char} == [
2117                undef,
2118                0x006F, # o
2119                0x0063, # c
2120                0x0074, # t
2121                0x0079, # y
2122                0x0070, # p
2123              ]->[length $self->{state_keyword}]) {
2124            !!!cp (131);
2125            ## Stay in the state.
2126            $self->{state_keyword} .= chr $self->{next_char};
2127            !!!next-input-character;
2128            redo A;
2129          } elsif ((length $self->{state_keyword}) == 6 and
2130                   ($self->{next_char} == 0x0045 or # E
2131                    $self->{next_char} == 0x0065)) { # e
2132            !!!cp (129);
2133            $self->{state} = DOCTYPE_STATE;
2134            $self->{current_token} = {type => DOCTYPE_TOKEN,
2135                                      quirks => 1,
2136                                      line => $self->{line_prev},
2137                                      column => $self->{column_prev} - 7,
2138                                     };
2139            !!!next-input-character;
2140            redo A;
2141          } else {
2142            !!!cp (132);        
2143            !!!parse-error (type => 'bogus comment',
2144                            line => $self->{line_prev},
2145                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2146            $self->{state} = BOGUS_COMMENT_STATE;
2147            ## Reconsume.
2148            $self->{current_token} = {type => COMMENT_TOKEN,
2149                                      data => $self->{state_keyword},
2150                                      line => $self->{line_prev},
2151                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2152                                     };
2153            redo A;
2154          }
2155        } elsif ($self->{state} == MD_CDATA_STATE) {
2156          if ($self->{next_char} == {
2157                '[' => 0x0043, # C
2158                '[C' => 0x0044, # D
2159                '[CD' => 0x0041, # A
2160                '[CDA' => 0x0054, # T
2161                '[CDAT' => 0x0041, # A
2162              }->{$self->{state_keyword}}) {
2163            !!!cp (135.1);
2164            ## Stay in the state.
2165            $self->{state_keyword} .= chr $self->{next_char};
2166            !!!next-input-character;
2167            redo A;
2168          } elsif ($self->{state_keyword} eq '[CDATA' and
2169                   $self->{next_char} == 0x005B) { # [
2170            !!!cp (135.2);
2171            $self->{current_token} = {type => CHARACTER_TOKEN,
2172                                      data => '',
2173                                      line => $self->{line_prev},
2174                                      column => $self->{column_prev} - 7};
2175            $self->{state} = CDATA_SECTION_STATE;
2176            !!!next-input-character;
2177            redo A;
2178          } else {
2179            !!!cp (135.3);
2180            !!!parse-error (type => 'bogus comment',
2181                            line => $self->{line_prev},
2182                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2183            $self->{state} = BOGUS_COMMENT_STATE;
2184            ## Reconsume.
2185            $self->{current_token} = {type => COMMENT_TOKEN,
2186                                      data => $self->{state_keyword},
2187                                      line => $self->{line_prev},
2188                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2189                                     };
2190            redo A;
2191          }
2192      } elsif ($self->{state} == COMMENT_START_STATE) {      } elsif ($self->{state} == COMMENT_START_STATE) {
2193        if ($self->{next_char} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2194          !!!cp (137);          !!!cp (137);
# Line 2148  sub _get_next_token ($) { Line 2271  sub _get_next_token ($) {
2271        } else {        } else {
2272          !!!cp (147);          !!!cp (147);
2273          $self->{current_token}->{data} .= chr ($self->{next_char}); # comment          $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2274            $self->{read_until}->($self->{current_token}->{data},
2275                                  q[-],
2276                                  length $self->{current_token}->{data});
2277    
2278          ## Stay in the state          ## Stay in the state
2279          !!!next-input-character;          !!!next-input-character;
2280          redo A;          redo A;
# Line 2332  sub _get_next_token ($) { Line 2459  sub _get_next_token ($) {
2459          redo A;          redo A;
2460        } elsif ($self->{next_char} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
2461                 $self->{next_char} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
2462            $self->{state} = PUBLIC_STATE;
2463            $self->{state_keyword} = chr $self->{next_char};
2464          !!!next-input-character;          !!!next-input-character;
2465          if ($self->{next_char} == 0x0055 or # U          redo A;
             $self->{next_char} == 0x0075) { # u  
           !!!next-input-character;  
           if ($self->{next_char} == 0x0042 or # B  
               $self->{next_char} == 0x0062) { # b  
             !!!next-input-character;  
             if ($self->{next_char} == 0x004C or # L  
                 $self->{next_char} == 0x006C) { # l  
               !!!next-input-character;  
               if ($self->{next_char} == 0x0049 or # I  
                   $self->{next_char} == 0x0069) { # i  
                 !!!next-input-character;  
                 if ($self->{next_char} == 0x0043 or # C  
                     $self->{next_char} == 0x0063) { # c  
                   !!!cp (168);  
                   $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;  
                   !!!next-input-character;  
                   redo A;  
                 } else {  
                   !!!cp (169);  
                 }  
               } else {  
                 !!!cp (170);  
               }  
             } else {  
               !!!cp (171);  
             }  
           } else {  
             !!!cp (172);  
           }  
         } else {  
           !!!cp (173);  
         }  
   
         #  
2466        } elsif ($self->{next_char} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
2467                 $self->{next_char} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
2468            $self->{state} = SYSTEM_STATE;
2469            $self->{state_keyword} = chr $self->{next_char};
2470          !!!next-input-character;          !!!next-input-character;
2471          if ($self->{next_char} == 0x0059 or # Y          redo A;
             $self->{next_char} == 0x0079) { # y  
           !!!next-input-character;  
           if ($self->{next_char} == 0x0053 or # S  
               $self->{next_char} == 0x0073) { # s  
             !!!next-input-character;  
             if ($self->{next_char} == 0x0054 or # T  
                 $self->{next_char} == 0x0074) { # t  
               !!!next-input-character;  
               if ($self->{next_char} == 0x0045 or # E  
                   $self->{next_char} == 0x0065) { # e  
                 !!!next-input-character;  
                 if ($self->{next_char} == 0x004D or # M  
                     $self->{next_char} == 0x006D) { # m  
                   !!!cp (174);  
                   $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;  
                   !!!next-input-character;  
                   redo A;  
                 } else {  
                   !!!cp (175);  
                 }  
               } else {  
                 !!!cp (176);  
               }  
             } else {  
               !!!cp (177);  
             }  
           } else {  
             !!!cp (178);  
           }  
         } else {  
           !!!cp (179);  
         }  
   
         #  
2472        } else {        } else {
2473          !!!cp (180);          !!!cp (180);
2474            !!!parse-error (type => 'string after DOCTYPE name');
2475            $self->{current_token}->{quirks} = 1;
2476    
2477            $self->{state} = BOGUS_DOCTYPE_STATE;
2478          !!!next-input-character;          !!!next-input-character;
2479          #          redo A;
2480        }        }
2481        } elsif ($self->{state} == PUBLIC_STATE) {
2482          ## ASCII case-insensitive
2483          if ($self->{next_char} == [
2484                undef,
2485                0x0055, # U
2486                0x0042, # B
2487                0x004C, # L
2488                0x0049, # I
2489              ]->[length $self->{state_keyword}] or
2490              $self->{next_char} == [
2491                undef,
2492                0x0075, # u
2493                0x0062, # b
2494                0x006C, # l
2495                0x0069, # i
2496              ]->[length $self->{state_keyword}]) {
2497            !!!cp (175);
2498            ## Stay in the state.
2499            $self->{state_keyword} .= chr $self->{next_char};
2500            !!!next-input-character;
2501            redo A;
2502          } elsif ((length $self->{state_keyword}) == 5 and
2503                   ($self->{next_char} == 0x0043 or # C
2504                    $self->{next_char} == 0x0063)) { # c
2505            !!!cp (168);
2506            $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2507            !!!next-input-character;
2508            redo A;
2509          } else {
2510            !!!cp (169);
2511            !!!parse-error (type => 'string after DOCTYPE name',
2512                            line => $self->{line_prev},
2513                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2514            $self->{current_token}->{quirks} = 1;
2515    
2516        !!!parse-error (type => 'string after DOCTYPE name');          $self->{state} = BOGUS_DOCTYPE_STATE;
2517        $self->{current_token}->{quirks} = 1;          ## Reconsume.
2518            redo A;
2519          }
2520        } elsif ($self->{state} == SYSTEM_STATE) {
2521          ## ASCII case-insensitive
2522          if ($self->{next_char} == [
2523                undef,
2524                0x0059, # Y
2525                0x0053, # S
2526                0x0054, # T
2527                0x0045, # E
2528              ]->[length $self->{state_keyword}] or
2529              $self->{next_char} == [
2530                undef,
2531                0x0079, # y
2532                0x0073, # s
2533                0x0074, # t
2534                0x0065, # e
2535              ]->[length $self->{state_keyword}]) {
2536            !!!cp (170);
2537            ## Stay in the state.
2538            $self->{state_keyword} .= chr $self->{next_char};
2539            !!!next-input-character;
2540            redo A;
2541          } elsif ((length $self->{state_keyword}) == 5 and
2542                   ($self->{next_char} == 0x004D or # M
2543                    $self->{next_char} == 0x006D)) { # m
2544            !!!cp (171);
2545            $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2546            !!!next-input-character;
2547            redo A;
2548          } else {
2549            !!!cp (172);
2550            !!!parse-error (type => 'string after DOCTYPE name',
2551                            line => $self->{line_prev},
2552                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2553            $self->{current_token}->{quirks} = 1;
2554    
2555        $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2556        # next-input-character is already done          ## Reconsume.
2557        redo A;          redo A;
2558          }
2559      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2560        if ({        if ({
2561              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
# Line 2502  sub _get_next_token ($) { Line 2640  sub _get_next_token ($) {
2640          !!!cp (190);          !!!cp (190);
2641          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2642              .= chr $self->{next_char};              .= chr $self->{next_char};
2643            $self->{read_until}->($self->{current_token}->{public_identifier},
2644                                  q[">],
2645                                  length $self->{current_token}->{public_identifier});
2646    
2647          ## Stay in the state          ## Stay in the state
2648          !!!next-input-character;          !!!next-input-character;
2649          redo A;          redo A;
# Line 2538  sub _get_next_token ($) { Line 2680  sub _get_next_token ($) {
2680          !!!cp (194);          !!!cp (194);
2681          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2682              .= chr $self->{next_char};              .= chr $self->{next_char};
2683            $self->{read_until}->($self->{current_token}->{public_identifier},
2684                                  q['>],
2685                                  length $self->{current_token}->{public_identifier});
2686    
2687          ## Stay in the state          ## Stay in the state
2688          !!!next-input-character;          !!!next-input-character;
2689          redo A;          redo A;
# Line 2650  sub _get_next_token ($) { Line 2796  sub _get_next_token ($) {
2796          redo A;          redo A;
2797        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2798          !!!cp (208);          !!!cp (208);
2799          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2800    
2801          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2802          !!!next-input-character;          !!!next-input-character;
# Line 2674  sub _get_next_token ($) { Line 2820  sub _get_next_token ($) {
2820          !!!cp (210);          !!!cp (210);
2821          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2822              .= chr $self->{next_char};              .= chr $self->{next_char};
2823            $self->{read_until}->($self->{current_token}->{system_identifier},
2824                                  q[">],
2825                                  length $self->{current_token}->{system_identifier});
2826    
2827          ## Stay in the state          ## Stay in the state
2828          !!!next-input-character;          !!!next-input-character;
2829          redo A;          redo A;
# Line 2686  sub _get_next_token ($) { Line 2836  sub _get_next_token ($) {
2836          redo A;          redo A;
2837        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2838          !!!cp (212);          !!!cp (212);
2839          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2840    
2841          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2842          !!!next-input-character;          !!!next-input-character;
# Line 2710  sub _get_next_token ($) { Line 2860  sub _get_next_token ($) {
2860          !!!cp (214);          !!!cp (214);
2861          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2862              .= chr $self->{next_char};              .= chr $self->{next_char};
2863            $self->{read_until}->($self->{current_token}->{system_identifier},
2864                                  q['>],
2865                                  length $self->{current_token}->{system_identifier});
2866    
2867          ## Stay in the state          ## Stay in the state
2868          !!!next-input-character;          !!!next-input-character;
2869          redo A;          redo A;
# Line 2733  sub _get_next_token ($) { Line 2887  sub _get_next_token ($) {
2887          redo A;          redo A;
2888        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
2889          !!!cp (217);          !!!cp (217);
2890            !!!parse-error (type => 'unclosed DOCTYPE');
2891          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2892          ## reconsume          ## reconsume
2893    
# Line 2770  sub _get_next_token ($) { Line 2924  sub _get_next_token ($) {
2924          redo A;          redo A;
2925        } else {        } else {
2926          !!!cp (221);          !!!cp (221);
2927            my $s = '';
2928            $self->{read_until}->($s, q[>], 0);
2929    
2930          ## Stay in the state          ## Stay in the state
2931          !!!next-input-character;          !!!next-input-character;
2932          redo A;          redo A;
2933        }        }
2934      } elsif ($self->{state} == CDATA_BLOCK_STATE) {      } elsif ($self->{state} == CDATA_SECTION_STATE) {
2935        my $s = '';        ## NOTE: "CDATA section state" in the state is jointly implemented
2936          ## by three states, |CDATA_SECTION_STATE|, |CDATA_SECTION_MSE1_STATE|,
2937          ## and |CDATA_SECTION_MSE2_STATE|.
2938                
2939        my ($l, $c) = ($self->{line}, $self->{column});        if ($self->{next_char} == 0x005D) { # ]
2940            !!!cp (221.1);
2941        CS: while ($self->{next_char} != -1) {          $self->{state} = CDATA_SECTION_MSE1_STATE;
         if ($self->{next_char} == 0x005D) { # ]  
           !!!next-input-character;  
           if ($self->{next_char} == 0x005D) { # ]  
             !!!next-input-character;  
             MDC: {  
               if ($self->{next_char} == 0x003E) { # >  
                 !!!cp (221.1);  
                 !!!next-input-character;  
                 last CS;  
               } elsif ($self->{next_char} == 0x005D) { # ]  
                 !!!cp (221.2);  
                 $s .= ']';  
                 !!!next-input-character;  
                 redo MDC;  
               } else {  
                 !!!cp (221.3);  
                 $s .= ']]';  
                 #  
               }  
             } # MDC  
           } else {  
             !!!cp (221.4);  
             $s .= ']';  
             #  
           }  
         } else {  
           !!!cp (221.5);  
           #  
         }  
         $s .= chr $self->{next_char};  
2942          !!!next-input-character;          !!!next-input-character;
2943        } # CS          redo A;
2944          } elsif ($self->{next_char} == -1) {
2945            $self->{state} = DATA_STATE;
2946            !!!next-input-character;
2947            if (length $self->{current_token}->{data}) { # character
2948              !!!cp (221.2);
2949              !!!emit ($self->{current_token}); # character
2950            } else {
2951              !!!cp (221.3);
2952              ## No token to emit. $self->{current_token} is discarded.
2953            }        
2954            redo A;
2955          } else {
2956            !!!cp (221.4);
2957            $self->{current_token}->{data} .= chr $self->{next_char};
2958            $self->{read_until}->($self->{current_token}->{data},
2959                                  q<]>,
2960                                  length $self->{current_token}->{data});
2961    
2962        $self->{state} = DATA_STATE;          ## Stay in the state.
2963        ## next-input-character done or EOF, which is reconsumed.          !!!next-input-character;
2964            redo A;
2965          }
2966    
2967        if (length $s) {        ## ISSUE: "text tokens" in spec.
2968        } elsif ($self->{state} == CDATA_SECTION_MSE1_STATE) {
2969          if ($self->{next_char} == 0x005D) { # ]
2970            !!!cp (221.5);
2971            $self->{state} = CDATA_SECTION_MSE2_STATE;
2972            !!!next-input-character;
2973            redo A;
2974          } else {
2975          !!!cp (221.6);          !!!cp (221.6);
2976          !!!emit ({type => CHARACTER_TOKEN, data => $s,          $self->{current_token}->{data} .= ']';
2977                    line => $l, column => $c});          $self->{state} = CDATA_SECTION_STATE;
2978            ## Reconsume.
2979            redo A;
2980          }
2981        } elsif ($self->{state} == CDATA_SECTION_MSE2_STATE) {
2982          if ($self->{next_char} == 0x003E) { # >
2983            $self->{state} = DATA_STATE;
2984            !!!next-input-character;
2985            if (length $self->{current_token}->{data}) { # character
2986              !!!cp (221.7);
2987              !!!emit ($self->{current_token}); # character
2988            } else {
2989              !!!cp (221.8);
2990              ## No token to emit. $self->{current_token} is discarded.
2991            }
2992            redo A;
2993          } elsif ($self->{next_char} == 0x005D) { # ]
2994            !!!cp (221.9); # character
2995            $self->{current_token}->{data} .= ']'; ## Add first "]" of "]]]".
2996            ## Stay in the state.
2997            !!!next-input-character;
2998            redo A;
2999        } else {        } else {
3000          !!!cp (221.7);          !!!cp (221.11);
3001            $self->{current_token}->{data} .= ']]'; # character
3002            $self->{state} = CDATA_SECTION_STATE;
3003            ## Reconsume.
3004            redo A;
3005          }
3006        } elsif ($self->{state} == ENTITY_STATE) {
3007          if ({
3008            0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
3009            0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, &
3010            $self->{entity_additional} => 1,
3011          }->{$self->{next_char}}) {
3012            !!!cp (1001);
3013            ## Don't consume
3014            ## No error
3015            ## Return nothing.
3016            #
3017          } elsif ($self->{next_char} == 0x0023) { # #
3018            !!!cp (999);
3019            $self->{state} = ENTITY_HASH_STATE;
3020            $self->{state_keyword} = '#';
3021            !!!next-input-character;
3022            redo A;
3023          } elsif ((0x0041 <= $self->{next_char} and
3024                    $self->{next_char} <= 0x005A) or # A..Z
3025                   (0x0061 <= $self->{next_char} and
3026                    $self->{next_char} <= 0x007A)) { # a..z
3027            !!!cp (998);
3028            require Whatpm::_NamedEntityList;
3029            $self->{state} = ENTITY_NAME_STATE;
3030            $self->{state_keyword} = chr $self->{next_char};
3031            $self->{entity__value} = $self->{state_keyword};
3032            $self->{entity__match} = 0;
3033            !!!next-input-character;
3034            redo A;
3035          } else {
3036            !!!cp (1027);
3037            !!!parse-error (type => 'bare ero');
3038            ## Return nothing.
3039            #
3040        }        }
3041    
3042        redo A;        ## NOTE: No character is consumed by the "consume a character
3043          ## reference" algorithm.  In other word, there is an "&" character
3044        ## ISSUE: "text tokens" in spec.        ## that does not introduce a character reference, which would be
3045        ## TODO: Streaming support        ## appended to the parent element or the attribute value in later
3046      } else {        ## process of the tokenizer.
3047        die "$0: $self->{state}: Unknown state";  
3048      }        if ($self->{prev_state} == DATA_STATE) {
3049    } # A            !!!cp (997);
3050            $self->{state} = $self->{prev_state};
3051    die "$0: _get_next_token: unexpected case";          ## Reconsume.
3052  } # _get_next_token          !!!emit ({type => CHARACTER_TOKEN, data => '&',
3053                      line => $self->{line_prev},
3054  sub _tokenize_attempt_to_consume_an_entity ($$$) {                    column => $self->{column_prev},
3055    my ($self, $in_attr, $additional) = @_;                   });
3056            redo A;
3057    my ($l, $c) = ($self->{line_prev}, $self->{column_prev});        } else {
3058            !!!cp (996);
3059            $self->{current_attribute}->{value} .= '&';
3060            $self->{state} = $self->{prev_state};
3061            ## Reconsume.
3062            redo A;
3063          }
3064        } elsif ($self->{state} == ENTITY_HASH_STATE) {
3065          if ($self->{next_char} == 0x0078 or # x
3066              $self->{next_char} == 0x0058) { # X
3067            !!!cp (995);
3068            $self->{state} = HEXREF_X_STATE;
3069            $self->{state_keyword} .= chr $self->{next_char};
3070            !!!next-input-character;
3071            redo A;
3072          } elsif (0x0030 <= $self->{next_char} and
3073                   $self->{next_char} <= 0x0039) { # 0..9
3074            !!!cp (994);
3075            $self->{state} = NCR_NUM_STATE;
3076            $self->{state_keyword} = $self->{next_char} - 0x0030;
3077            !!!next-input-character;
3078            redo A;
3079          } else {
3080            !!!parse-error (type => 'bare nero',
3081                            line => $self->{line_prev},
3082                            column => $self->{column_prev} - 1);
3083    
3084    if ({          ## NOTE: According to the spec algorithm, nothing is returned,
3085         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,          ## and then "&#" is appended to the parent element or the attribute
3086         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR          ## value in the later processing.
3087         $additional => 1,  
3088        }->{$self->{next_char}}) {          if ($self->{prev_state} == DATA_STATE) {
3089      !!!cp (1001);            !!!cp (1019);
3090      ## Don't consume            $self->{state} = $self->{prev_state};
3091      ## No error            ## Reconsume.
3092      return undef;            !!!emit ({type => CHARACTER_TOKEN,
3093    } elsif ($self->{next_char} == 0x0023) { # #                      data => '&#',
3094      !!!next-input-character;                      line => $self->{line_prev},
3095      if ($self->{next_char} == 0x0078 or # x                      column => $self->{column_prev} - 1,
3096          $self->{next_char} == 0x0058) { # X                     });
3097        my $code;            redo A;
       X: {  
         my $x_char = $self->{next_char};  
         !!!next-input-character;  
         if (0x0030 <= $self->{next_char} and  
             $self->{next_char} <= 0x0039) { # 0..9  
           !!!cp (1002);  
           $code ||= 0;  
           $code *= 0x10;  
           $code += $self->{next_char} - 0x0030;  
           redo X;  
         } elsif (0x0061 <= $self->{next_char} and  
                  $self->{next_char} <= 0x0066) { # a..f  
           !!!cp (1003);  
           $code ||= 0;  
           $code *= 0x10;  
           $code += $self->{next_char} - 0x0060 + 9;  
           redo X;  
         } elsif (0x0041 <= $self->{next_char} and  
                  $self->{next_char} <= 0x0046) { # A..F  
           !!!cp (1004);  
           $code ||= 0;  
           $code *= 0x10;  
           $code += $self->{next_char} - 0x0040 + 9;  
           redo X;  
         } elsif (not defined $code) { # no hexadecimal digit  
           !!!cp (1005);  
           !!!parse-error (type => 'bare hcro', line => $l, column => $c);  
           !!!back-next-input-character ($x_char, $self->{next_char});  
           $self->{next_char} = 0x0023; # #  
           return undef;  
         } elsif ($self->{next_char} == 0x003B) { # ;  
           !!!cp (1006);  
           !!!next-input-character;  
3098          } else {          } else {
3099            !!!cp (1007);            !!!cp (993);
3100            !!!parse-error (type => 'no refc', line => $l, column => $c);            $self->{current_attribute}->{value} .= '&#';
3101              $self->{state} = $self->{prev_state};
3102              ## Reconsume.
3103              redo A;
3104          }          }
3105          }
3106          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {      } elsif ($self->{state} == NCR_NUM_STATE) {
3107            !!!cp (1008);        if (0x0030 <= $self->{next_char} and
3108            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);            $self->{next_char} <= 0x0039) { # 0..9
           $code = 0xFFFD;  
         } elsif ($code > 0x10FFFF) {  
           !!!cp (1009);  
           !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);  
           $code = 0xFFFD;  
         } elsif ($code == 0x000D) {  
           !!!cp (1010);  
           !!!parse-error (type => 'CR character reference', line => $l, column => $c);  
           $code = 0x000A;  
         } elsif (0x80 <= $code and $code <= 0x9F) {  
           !!!cp (1011);  
           !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);  
           $code = $c1_entity_char->{$code};  
         }  
   
         return {type => CHARACTER_TOKEN, data => chr $code,  
                 has_reference => 1,  
                 line => $l, column => $c,  
                };  
       } # X  
     } elsif (0x0030 <= $self->{next_char} and  
              $self->{next_char} <= 0x0039) { # 0..9  
       my $code = $self->{next_char} - 0x0030;  
       !!!next-input-character;  
         
       while (0x0030 <= $self->{next_char} and  
                 $self->{next_char} <= 0x0039) { # 0..9  
3109          !!!cp (1012);          !!!cp (1012);
3110          $code *= 10;          $self->{state_keyword} *= 10;
3111          $code += $self->{next_char} - 0x0030;          $self->{state_keyword} += $self->{next_char} - 0x0030;
3112                    
3113            ## Stay in the state.
3114          !!!next-input-character;          !!!next-input-character;
3115        }          redo A;
3116          } elsif ($self->{next_char} == 0x003B) { # ;
       if ($self->{next_char} == 0x003B) { # ;  
3117          !!!cp (1013);          !!!cp (1013);
3118          !!!next-input-character;          !!!next-input-character;
3119            #
3120        } else {        } else {
3121          !!!cp (1014);          !!!cp (1014);
3122          !!!parse-error (type => 'no refc', line => $l, column => $c);          !!!parse-error (type => 'no refc');
3123            ## Reconsume.
3124            #
3125        }        }
3126    
3127          my $code = $self->{state_keyword};
3128          my $l = $self->{line_prev};
3129          my $c = $self->{column_prev};
3130        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3131          !!!cp (1015);          !!!cp (1015);
3132          !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);          !!!parse-error (type => 'invalid character reference',
3133                            text => (sprintf 'U+%04X', $code),
3134                            line => $l, column => $c);
3135          $code = 0xFFFD;          $code = 0xFFFD;
3136        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
3137          !!!cp (1016);          !!!cp (1016);
3138          !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);          !!!parse-error (type => 'invalid character reference',
3139                            text => (sprintf 'U-%08X', $code),
3140                            line => $l, column => $c);
3141          $code = 0xFFFD;          $code = 0xFFFD;
3142        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
3143          !!!cp (1017);          !!!cp (1017);
3144          !!!parse-error (type => 'CR character reference', line => $l, column => $c);          !!!parse-error (type => 'CR character reference',
3145                            line => $l, column => $c);
3146          $code = 0x000A;          $code = 0x000A;
3147        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3148          !!!cp (1018);          !!!cp (1018);
3149          !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);          !!!parse-error (type => 'C1 character reference',
3150                            text => (sprintf 'U+%04X', $code),
3151                            line => $l, column => $c);
3152          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3153        }        }
3154          
3155        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,        if ($self->{prev_state} == DATA_STATE) {
3156                line => $l, column => $c,          !!!cp (992);
3157               };          $self->{state} = $self->{prev_state};
3158      } else {          ## Reconsume.
3159        !!!cp (1019);          !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3160        !!!parse-error (type => 'bare nero', line => $l, column => $c);                    line => $l, column => $c,
3161        !!!back-next-input-character ($self->{next_char});                   });
3162        $self->{next_char} = 0x0023; # #          redo A;
3163        return undef;        } else {
3164      }          !!!cp (991);
3165    } elsif ((0x0041 <= $self->{next_char} and          $self->{current_attribute}->{value} .= chr $code;
3166              $self->{next_char} <= 0x005A) or          $self->{current_attribute}->{has_reference} = 1;
3167             (0x0061 <= $self->{next_char} and          $self->{state} = $self->{prev_state};
3168              $self->{next_char} <= 0x007A)) {          ## Reconsume.
3169      my $entity_name = chr $self->{next_char};          redo A;
3170      !!!next-input-character;        }
3171        } elsif ($self->{state} == HEXREF_X_STATE) {
3172      my $value = $entity_name;        if ((0x0030 <= $self->{next_char} and $self->{next_char} <= 0x0039) or
3173      my $match = 0;            (0x0041 <= $self->{next_char} and $self->{next_char} <= 0x0046) or
3174      require Whatpm::_NamedEntityList;            (0x0061 <= $self->{next_char} and $self->{next_char} <= 0x0066)) {
3175      our $EntityChar;          # 0..9, A..F, a..f
3176            !!!cp (990);
3177      while (length $entity_name < 30 and          $self->{state} = HEXREF_HEX_STATE;
3178             ## NOTE: Some number greater than the maximum length of entity name          $self->{state_keyword} = 0;
3179             ((0x0041 <= $self->{next_char} and # a          ## Reconsume.
3180               $self->{next_char} <= 0x005A) or # x          redo A;
3181              (0x0061 <= $self->{next_char} and # a        } else {
3182               $self->{next_char} <= 0x007A) or # z          !!!parse-error (type => 'bare hcro',
3183              (0x0030 <= $self->{next_char} and # 0                          line => $self->{line_prev},
3184               $self->{next_char} <= 0x0039) or # 9                          column => $self->{column_prev} - 2);
3185              $self->{next_char} == 0x003B)) { # ;  
3186        $entity_name .= chr $self->{next_char};          ## NOTE: According to the spec algorithm, nothing is returned,
3187        if (defined $EntityChar->{$entity_name}) {          ## and then "&#" followed by "X" or "x" is appended to the parent
3188          if ($self->{next_char} == 0x003B) { # ;          ## element or the attribute value in the later processing.
3189            !!!cp (1020);  
3190            $value = $EntityChar->{$entity_name};          if ($self->{prev_state} == DATA_STATE) {
3191            $match = 1;            !!!cp (1005);
3192            !!!next-input-character;            $self->{state} = $self->{prev_state};
3193            last;            ## Reconsume.
3194              !!!emit ({type => CHARACTER_TOKEN,
3195                        data => '&' . $self->{state_keyword},
3196                        line => $self->{line_prev},
3197                        column => $self->{column_prev} - length $self->{state_keyword},
3198                       });
3199              redo A;
3200            } else {
3201              !!!cp (989);
3202              $self->{current_attribute}->{value} .= '&' . $self->{state_keyword};
3203              $self->{state} = $self->{prev_state};
3204              ## Reconsume.
3205              redo A;
3206            }
3207          }
3208        } elsif ($self->{state} == HEXREF_HEX_STATE) {
3209          if (0x0030 <= $self->{next_char} and $self->{next_char} <= 0x0039) {
3210            # 0..9
3211            !!!cp (1002);
3212            $self->{state_keyword} *= 0x10;
3213            $self->{state_keyword} += $self->{next_char} - 0x0030;
3214            ## Stay in the state.
3215            !!!next-input-character;
3216            redo A;
3217          } elsif (0x0061 <= $self->{next_char} and
3218                   $self->{next_char} <= 0x0066) { # a..f
3219            !!!cp (1003);
3220            $self->{state_keyword} *= 0x10;
3221            $self->{state_keyword} += $self->{next_char} - 0x0060 + 9;
3222            ## Stay in the state.
3223            !!!next-input-character;
3224            redo A;
3225          } elsif (0x0041 <= $self->{next_char} and
3226                   $self->{next_char} <= 0x0046) { # A..F
3227            !!!cp (1004);
3228            $self->{state_keyword} *= 0x10;
3229            $self->{state_keyword} += $self->{next_char} - 0x0040 + 9;
3230            ## Stay in the state.
3231            !!!next-input-character;
3232            redo A;
3233          } elsif ($self->{next_char} == 0x003B) { # ;
3234            !!!cp (1006);
3235            !!!next-input-character;
3236            #
3237          } else {
3238            !!!cp (1007);
3239            !!!parse-error (type => 'no refc',
3240                            line => $self->{line},
3241                            column => $self->{column});
3242            ## Reconsume.
3243            #
3244          }
3245    
3246          my $code = $self->{state_keyword};
3247          my $l = $self->{line_prev};
3248          my $c = $self->{column_prev};
3249          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3250            !!!cp (1008);
3251            !!!parse-error (type => 'invalid character reference',
3252                            text => (sprintf 'U+%04X', $code),
3253                            line => $l, column => $c);
3254            $code = 0xFFFD;
3255          } elsif ($code > 0x10FFFF) {
3256            !!!cp (1009);
3257            !!!parse-error (type => 'invalid character reference',
3258                            text => (sprintf 'U-%08X', $code),
3259                            line => $l, column => $c);
3260            $code = 0xFFFD;
3261          } elsif ($code == 0x000D) {
3262            !!!cp (1010);
3263            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
3264            $code = 0x000A;
3265          } elsif (0x80 <= $code and $code <= 0x9F) {
3266            !!!cp (1011);
3267            !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
3268            $code = $c1_entity_char->{$code};
3269          }
3270    
3271          if ($self->{prev_state} == DATA_STATE) {
3272            !!!cp (988);
3273            $self->{state} = $self->{prev_state};
3274            ## Reconsume.
3275            !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3276                      line => $l, column => $c,
3277                     });
3278            redo A;
3279          } else {
3280            !!!cp (987);
3281            $self->{current_attribute}->{value} .= chr $code;
3282            $self->{current_attribute}->{has_reference} = 1;
3283            $self->{state} = $self->{prev_state};
3284            ## Reconsume.
3285            redo A;
3286          }
3287        } elsif ($self->{state} == ENTITY_NAME_STATE) {
3288          if (length $self->{state_keyword} < 30 and
3289              ## NOTE: Some number greater than the maximum length of entity name
3290              ((0x0041 <= $self->{next_char} and # a
3291                $self->{next_char} <= 0x005A) or # x
3292               (0x0061 <= $self->{next_char} and # a
3293                $self->{next_char} <= 0x007A) or # z
3294               (0x0030 <= $self->{next_char} and # 0
3295                $self->{next_char} <= 0x0039) or # 9
3296               $self->{next_char} == 0x003B)) { # ;
3297            our $EntityChar;
3298            $self->{state_keyword} .= chr $self->{next_char};
3299            if (defined $EntityChar->{$self->{state_keyword}}) {
3300              if ($self->{next_char} == 0x003B) { # ;
3301                !!!cp (1020);
3302                $self->{entity__value} = $EntityChar->{$self->{state_keyword}};
3303                $self->{entity__match} = 1;
3304                !!!next-input-character;
3305                #
3306              } else {
3307                !!!cp (1021);
3308                $self->{entity__value} = $EntityChar->{$self->{state_keyword}};
3309                $self->{entity__match} = -1;
3310                ## Stay in the state.
3311                !!!next-input-character;
3312                redo A;
3313              }
3314          } else {          } else {
3315            !!!cp (1021);            !!!cp (1022);
3316            $value = $EntityChar->{$entity_name};            $self->{entity__value} .= chr $self->{next_char};
3317            $match = -1;            $self->{entity__match} *= 2;
3318              ## Stay in the state.
3319            !!!next-input-character;            !!!next-input-character;
3320              redo A;
3321            }
3322          }
3323    
3324          my $data;
3325          my $has_ref;
3326          if ($self->{entity__match} > 0) {
3327            !!!cp (1023);
3328            $data = $self->{entity__value};
3329            $has_ref = 1;
3330            #
3331          } elsif ($self->{entity__match} < 0) {
3332            !!!parse-error (type => 'no refc');
3333            if ($self->{prev_state} != DATA_STATE and # in attribute
3334                $self->{entity__match} < -1) {
3335              !!!cp (1024);
3336              $data = '&' . $self->{state_keyword};
3337              #
3338            } else {
3339              !!!cp (1025);
3340              $data = $self->{entity__value};
3341              $has_ref = 1;
3342              #
3343          }          }
3344        } else {        } else {
3345          !!!cp (1022);          !!!cp (1026);
3346          $value .= chr $self->{next_char};          !!!parse-error (type => 'bare ero',
3347          $match *= 2;                          line => $self->{line_prev},
3348          !!!next-input-character;                          column => $self->{column_prev} - length $self->{state_keyword});
3349            $data = '&' . $self->{state_keyword};
3350            #
3351        }        }
3352      }    
3353              ## NOTE: In these cases, when a character reference is found,
3354      if ($match > 0) {        ## it is consumed and a character token is returned, or, otherwise,
3355        !!!cp (1023);        ## nothing is consumed and returned, according to the spec algorithm.
3356        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,        ## In this implementation, anything that has been examined by the
3357                line => $l, column => $c,        ## tokenizer is appended to the parent element or the attribute value
3358               };        ## as string, either literal string when no character reference or
3359      } elsif ($match < 0) {        ## entity-replaced string otherwise, in this stage, since any characters
3360        !!!parse-error (type => 'no refc', line => $l, column => $c);        ## that would not be consumed are appended in the data state or in an
3361        if ($in_attr and $match < -1) {        ## appropriate attribute value state anyway.
3362          !!!cp (1024);  
3363          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,        if ($self->{prev_state} == DATA_STATE) {
3364                  line => $l, column => $c,          !!!cp (986);
3365                 };          $self->{state} = $self->{prev_state};
3366        } else {          ## Reconsume.
3367          !!!cp (1025);          !!!emit ({type => CHARACTER_TOKEN,
3368          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,                    data => $data,
3369                  line => $l, column => $c,                    line => $self->{line_prev},
3370                 };                    column => $self->{column_prev} + 1 - length $self->{state_keyword},
3371                     });
3372            redo A;
3373          } else {
3374            !!!cp (985);
3375            $self->{current_attribute}->{value} .= $data;
3376            $self->{current_attribute}->{has_reference} = 1 if $has_ref;
3377            $self->{state} = $self->{prev_state};
3378            ## Reconsume.
3379            redo A;
3380        }        }
3381      } else {      } else {
3382        !!!cp (1026);        die "$0: $self->{state}: Unknown state";
       !!!parse-error (type => 'bare ero', line => $l, column => $c);  
       ## NOTE: "No characters are consumed" in the spec.  
       return {type => CHARACTER_TOKEN, data => '&'.$value,  
               line => $l, column => $c,  
              };  
3383      }      }
3384    } else {    } # A  
3385      !!!cp (1027);  
3386      ## no characters are consumed    die "$0: _get_next_token: unexpected case";
3387      !!!parse-error (type => 'bare ero', line => $l, column => $c);  } # _get_next_token
     return undef;  
   }  
 } # _tokenize_attempt_to_consume_an_entity  
3388    
3389  sub _initialize_tree_constructor ($) {  sub _initialize_tree_constructor ($) {
3390    my $self = shift;    my $self = shift;
# Line 3050  sub _initialize_tree_constructor ($) { Line 3393  sub _initialize_tree_constructor ($) {
3393    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3394    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3395    $self->{document}->manakai_is_html (1); # MUST    $self->{document}->manakai_is_html (1); # MUST
3396      $self->{document}->set_user_data (manakai_source_line => 1);
3397      $self->{document}->set_user_data (manakai_source_column => 1);
3398  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3399    
3400  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 3104  sub _tree_construction_initial ($) { Line 3449  sub _tree_construction_initial ($) {
3449        ## language.        ## language.
3450        my $doctype_name = $token->{name};        my $doctype_name = $token->{name};
3451        $doctype_name = '' unless defined $doctype_name;        $doctype_name = '' unless defined $doctype_name;
3452        $doctype_name =~ tr/a-z/A-Z/;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3453        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
           defined $token->{public_identifier} or  
3454            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3455          !!!cp ('t1');          !!!cp ('t1');
3456          !!!parse-error (type => 'not HTML5', token => $token);          !!!parse-error (type => 'not HTML5', token => $token);
3457        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3458          !!!cp ('t2');          !!!cp ('t2');
         ## ISSUE: ASCII case-insensitive? (in fact it does not matter)  
3459          !!!parse-error (type => 'not HTML5', token => $token);          !!!parse-error (type => 'not HTML5', token => $token);
3460          } elsif (defined $token->{public_identifier}) {
3461            if ($token->{public_identifier} eq 'XSLT-compat') {
3462              !!!cp ('t1.2');
3463              !!!parse-error (type => 'XSLT-compat', token => $token,
3464                              level => $self->{level}->{should});
3465            } else {
3466              !!!parse-error (type => 'not HTML5', token => $token);
3467            }
3468        } else {        } else {
3469          !!!cp ('t3');          !!!cp ('t3');
3470            #
3471        }        }
3472                
3473        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
# Line 3416  sub _reset_insertion_mode ($) { Line 3768  sub _reset_insertion_mode ($) {
3768          ## NOTE: Strictly spaking, the line below only applies to MathML and          ## NOTE: Strictly spaking, the line below only applies to MathML and
3769          ## SVG elements.  Currently the HTML syntax supports only MathML and          ## SVG elements.  Currently the HTML syntax supports only MathML and
3770          ## SVG elements as foreigners.          ## SVG elements as foreigners.
3771          $new_mode = $self->{insertion_mode} | IN_FOREIGN_CONTENT_IM;          $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
         ## ISSUE: What is set as the secondary insertion mode?  
3772        } elsif ($node->[1] & TABLE_CELL_EL) {        } elsif ($node->[1] & TABLE_CELL_EL) {
3773          if ($last) {          if ($last) {
3774            !!!cp ('t28.2');            !!!cp ('t28.2');
# Line 3618  sub _tree_construction_main ($) { Line 3969  sub _tree_construction_main ($) {
3969        ## NOTE: An end-of-file token.        ## NOTE: An end-of-file token.
3970        if ($content_model_flag == CDATA_CONTENT_MODEL) {        if ($content_model_flag == CDATA_CONTENT_MODEL) {
3971          !!!cp ('t43');          !!!cp ('t43');
3972          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);          !!!parse-error (type => 'in CDATA:#eof', token => $token);
3973        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3974          !!!cp ('t44');          !!!cp ('t44');
3975          !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);          !!!parse-error (type => 'in RCDATA:#eof', token => $token);
3976        } else {        } else {
3977          die "$0: $content_model_flag in parse_rcdata";          die "$0: $content_model_flag in parse_rcdata";
3978        }        }
# Line 3658  sub _tree_construction_main ($) { Line 4009  sub _tree_construction_main ($) {
4009        ## Ignore the token        ## Ignore the token
4010      } else {      } else {
4011        !!!cp ('t48');        !!!cp ('t48');
4012        !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);        !!!parse-error (type => 'in CDATA:#eof', token => $token);
4013        ## ISSUE: And ignore?        ## ISSUE: And ignore?
4014        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
4015      }      }
# Line 3709  sub _tree_construction_main ($) { Line 4060  sub _tree_construction_main ($) {
4060        } # AFE        } # AFE
4061        unless (defined $formatting_element) {        unless (defined $formatting_element) {
4062          !!!cp ('t53');          !!!cp ('t53');
4063          !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);          !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
4064          ## Ignore the token          ## Ignore the token
4065          !!!next-token;          !!!next-token;
4066          return;          return;
# Line 3726  sub _tree_construction_main ($) { Line 4077  sub _tree_construction_main ($) {
4077              last INSCOPE;              last INSCOPE;
4078            } else { # in open elements but not in scope            } else { # in open elements but not in scope
4079              !!!cp ('t55');              !!!cp ('t55');
4080              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},              !!!parse-error (type => 'unmatched end tag',
4081                                text => $token->{tag_name},
4082                              token => $end_tag_token);                              token => $end_tag_token);
4083              ## Ignore the token              ## Ignore the token
4084              !!!next-token;              !!!next-token;
# Line 3739  sub _tree_construction_main ($) { Line 4091  sub _tree_construction_main ($) {
4091        } # INSCOPE        } # INSCOPE
4092        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
4093          !!!cp ('t57');          !!!cp ('t57');
4094          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},          !!!parse-error (type => 'unmatched end tag',
4095                            text => $token->{tag_name},
4096                          token => $end_tag_token);                          token => $end_tag_token);
4097          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
4098          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
# Line 3748  sub _tree_construction_main ($) { Line 4101  sub _tree_construction_main ($) {
4101        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
4102          !!!cp ('t58');          !!!cp ('t58');
4103          !!!parse-error (type => 'not closed',          !!!parse-error (type => 'not closed',
4104                          value => $self->{open_elements}->[-1]->[0]                          text => $self->{open_elements}->[-1]->[0]
4105                              ->manakai_local_name,                              ->manakai_local_name,
4106                          token => $end_tag_token);                          token => $end_tag_token);
4107        }        }
# Line 3957  sub _tree_construction_main ($) { Line 4310  sub _tree_construction_main ($) {
4310    B: while (1) {    B: while (1) {
4311      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
4312        !!!cp ('t73');        !!!cp ('t73');
4313        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4314        ## Ignore the token        ## Ignore the token
4315        ## Stay in the phase        ## Stay in the phase
4316        !!!next-token;        !!!next-token;
# Line 3966  sub _tree_construction_main ($) { Line 4319  sub _tree_construction_main ($) {
4319               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
4320        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4321          !!!cp ('t79');          !!!cp ('t79');
4322          !!!parse-error (type => 'after html:html', token => $token);          !!!parse-error (type => 'after html', text => 'html', token => $token);
4323          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
4324        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4325          !!!cp ('t80');          !!!cp ('t80');
4326          !!!parse-error (type => 'after html:html', token => $token);          !!!parse-error (type => 'after html', text => 'html', token => $token);
4327          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4328        } else {        } else {
4329          !!!cp ('t81');          !!!cp ('t81');
# Line 4031  sub _tree_construction_main ($) { Line 4384  sub _tree_construction_main ($) {
4384                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
4385            !!!cp ('t87.2');            !!!cp ('t87.2');
4386            !!!parse-error (type => 'not closed',            !!!parse-error (type => 'not closed',
4387                            value => $self->{open_elements}->[-1]->[0]                            text => $self->{open_elements}->[-1]->[0]
4388                                ->manakai_local_name,                                ->manakai_local_name,
4389                            token => $token);                            token => $token);
4390    
# Line 4109  sub _tree_construction_main ($) { Line 4462  sub _tree_construction_main ($) {
4462        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4463          !!!cp ('t87.6');          !!!cp ('t87.6');
4464          !!!parse-error (type => 'not closed',          !!!parse-error (type => 'not closed',
4465                          value => $self->{open_elements}->[-1]->[0]                          text => $self->{open_elements}->[-1]->[0]
4466                              ->manakai_local_name,                              ->manakai_local_name,
4467                          token => $token);                          token => $token);
4468    
# Line 4130  sub _tree_construction_main ($) { Line 4483  sub _tree_construction_main ($) {
4483            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4484              !!!cp ('t88.2');              !!!cp ('t88.2');
4485              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4486                #
4487            } else {            } else {
4488              !!!cp ('t88.1');              !!!cp ('t88.1');
4489              ## Ignore the token.              ## Ignore the token.
4490              !!!next-token;              #
             next B;  
4491            }            }
4492            unless (length $token->{data}) {            unless (length $token->{data}) {
4493              !!!cp ('t88');              !!!cp ('t88');
4494              !!!next-token;              !!!next-token;
4495              next B;              next B;
4496            }            }
4497    ## TODO: set $token->{column} appropriately
4498          }          }
4499    
4500          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
# Line 4159  sub _tree_construction_main ($) { Line 4513  sub _tree_construction_main ($) {
4513            !!!cp ('t90');            !!!cp ('t90');
4514            ## As if </noscript>            ## As if </noscript>
4515            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4516            !!!parse-error (type => 'in noscript:#character', token => $token);            !!!parse-error (type => 'in noscript:#text', token => $token);
4517                        
4518            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4519            ## As if </head>            ## As if </head>
# Line 4196  sub _tree_construction_main ($) { Line 4550  sub _tree_construction_main ($) {
4550              next B;              next B;
4551            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4552              !!!cp ('t93.2');              !!!cp ('t93.2');
4553              !!!parse-error (type => 'after head:head', token => $token); ## TODO: error type              !!!parse-error (type => 'after head', text => 'head',
4554                                token => $token);
4555              ## Ignore the token              ## Ignore the token
4556              !!!nack ('t93.3');              !!!nack ('t93.3');
4557              !!!next-token;              !!!next-token;
4558              next B;              next B;
4559            } else {            } else {
4560              !!!cp ('t95');              !!!cp ('t95');
4561              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript              !!!parse-error (type => 'in head:head',
4562                                token => $token); # or in head noscript
4563              ## Ignore the token              ## Ignore the token
4564              !!!nack ('t95.1');              !!!nack ('t95.1');
4565              !!!next-token;              !!!next-token;
# Line 4228  sub _tree_construction_main ($) { Line 4584  sub _tree_construction_main ($) {
4584                  !!!cp ('t98');                  !!!cp ('t98');
4585                  ## As if </noscript>                  ## As if </noscript>
4586                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4587                  !!!parse-error (type => 'in noscript:base', token => $token);                  !!!parse-error (type => 'in noscript', text => 'base',
4588                                    token => $token);
4589                                
4590                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4591                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 4239  sub _tree_construction_main ($) { Line 4596  sub _tree_construction_main ($) {
4596                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4597                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4598                  !!!cp ('t100');                  !!!cp ('t100');
4599                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4600                                    text => $token->{tag_name}, token => $token);
4601                  push @{$self->{open_elements}},                  push @{$self->{open_elements}},
4602                      [$self->{head_element}, $el_category->{head}];                      [$self->{head_element}, $el_category->{head}];
4603                } else {                } else {
# Line 4256  sub _tree_construction_main ($) { Line 4614  sub _tree_construction_main ($) {
4614                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4615                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4616                  !!!cp ('t102');                  !!!cp ('t102');
4617                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4618                                    text => $token->{tag_name}, token => $token);
4619                  push @{$self->{open_elements}},                  push @{$self->{open_elements}},
4620                      [$self->{head_element}, $el_category->{head}];                      [$self->{head_element}, $el_category->{head}];
4621                } else {                } else {
# Line 4273  sub _tree_construction_main ($) { Line 4632  sub _tree_construction_main ($) {
4632                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4633                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4634                  !!!cp ('t104');                  !!!cp ('t104');
4635                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4636                                    text => $token->{tag_name}, token => $token);
4637                  push @{$self->{open_elements}},                  push @{$self->{open_elements}},
4638                      [$self->{head_element}, $el_category->{head}];                      [$self->{head_element}, $el_category->{head}];
4639                } else {                } else {
# Line 4342  sub _tree_construction_main ($) { Line 4702  sub _tree_construction_main ($) {
4702                  !!!cp ('t111');                  !!!cp ('t111');
4703                  ## As if </noscript>                  ## As if </noscript>
4704                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4705                  !!!parse-error (type => 'in noscript:title', token => $token);                  !!!parse-error (type => 'in noscript', text => 'title',
4706                                    token => $token);
4707                                
4708                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4709                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4710                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4711                  !!!cp ('t112');                  !!!cp ('t112');
4712                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4713                                    text => $token->{tag_name}, token => $token);
4714                  push @{$self->{open_elements}},                  push @{$self->{open_elements}},
4715                      [$self->{head_element}, $el_category->{head}];                      [$self->{head_element}, $el_category->{head}];
4716                } else {                } else {
# Line 4362  sub _tree_construction_main ($) { Line 4724  sub _tree_construction_main ($) {
4724                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4725                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4726                next B;                next B;
4727              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style' or
4728                         $token->{tag_name} eq 'noframes') {
4729                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4730                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4731                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4732                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4733                  !!!cp ('t114');                  !!!cp ('t114');
4734                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4735                                    text => $token->{tag_name}, token => $token);
4736                  push @{$self->{open_elements}},                  push @{$self->{open_elements}},
4737                      [$self->{head_element}, $el_category->{head}];                      [$self->{head_element}, $el_category->{head}];
4738                } else {                } else {
# Line 4389  sub _tree_construction_main ($) { Line 4753  sub _tree_construction_main ($) {
4753                  next B;                  next B;
4754                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4755                  !!!cp ('t117');                  !!!cp ('t117');
4756                  !!!parse-error (type => 'in noscript:noscript', token => $token);                  !!!parse-error (type => 'in noscript', text => 'noscript',
4757                                    token => $token);
4758                  ## Ignore the token                  ## Ignore the token
4759                  !!!nack ('t117.1');                  !!!nack ('t117.1');
4760                  !!!next-token;                  !!!next-token;
# Line 4403  sub _tree_construction_main ($) { Line 4768  sub _tree_construction_main ($) {
4768                  !!!cp ('t119');                  !!!cp ('t119');
4769                  ## As if </noscript>                  ## As if </noscript>
4770                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4771                  !!!parse-error (type => 'in noscript:script', token => $token);                  !!!parse-error (type => 'in noscript', text => 'script',
4772                                    token => $token);
4773                                
4774                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4775                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4776                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4777                  !!!cp ('t120');                  !!!cp ('t120');
4778                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4779                                    text => $token->{tag_name}, token => $token);
4780                  push @{$self->{open_elements}},                  push @{$self->{open_elements}},
4781                      [$self->{head_element}, $el_category->{head}];                      [$self->{head_element}, $el_category->{head}];
4782                } else {                } else {
# Line 4427  sub _tree_construction_main ($) { Line 4794  sub _tree_construction_main ($) {
4794                  !!!cp ('t122');                  !!!cp ('t122');
4795                  ## As if </noscript>                  ## As if </noscript>
4796                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4797                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'in noscript',
4798                                    text => $token->{tag_name}, token => $token);
4799                                    
4800                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4801                  ## As if </head>                  ## As if </head>
# Line 4466  sub _tree_construction_main ($) { Line 4834  sub _tree_construction_main ($) {
4834                !!!cp ('t129');                !!!cp ('t129');
4835                ## As if </noscript>                ## As if </noscript>
4836                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4837                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'in noscript:/',
4838                                  text => $token->{tag_name}, token => $token);
4839                                
4840                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4841                ## As if </head>                ## As if </head>
# Line 4509  sub _tree_construction_main ($) { Line 4878  sub _tree_construction_main ($) {
4878                  !!!cp ('t133');                  !!!cp ('t133');
4879                  ## As if </noscript>                  ## As if </noscript>
4880                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4881                  !!!parse-error (type => 'in noscript:/head', token => $token);                  !!!parse-error (type => 'in noscript:/',
4882                                    text => 'head', token => $token);
4883                                    
4884                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4885                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4524  sub _tree_construction_main ($) { Line 4894  sub _tree_construction_main ($) {
4894                  next B;                  next B;
4895                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4896                  !!!cp ('t134.1');                  !!!cp ('t134.1');
4897                  !!!parse-error (type => 'unmatched end tag:head', token => $token);                  !!!parse-error (type => 'unmatched end tag', text => 'head',
4898                                    token => $token);
4899                  ## Ignore the token                  ## Ignore the token
4900                  !!!next-token;                  !!!next-token;
4901                  next B;                  next B;
# Line 4541  sub _tree_construction_main ($) { Line 4912  sub _tree_construction_main ($) {
4912                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4913                         $self->{insertion_mode} == AFTER_HEAD_IM) {                         $self->{insertion_mode} == AFTER_HEAD_IM) {
4914                  !!!cp ('t137');                  !!!cp ('t137');
4915                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);                  !!!parse-error (type => 'unmatched end tag',
4916                                    text => 'noscript', token => $token);
4917                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4918                  !!!next-token;                  !!!next-token;
4919                  next B;                  next B;
# Line 4556  sub _tree_construction_main ($) { Line 4928  sub _tree_construction_main ($) {
4928                    $self->{insertion_mode} == IN_HEAD_IM or                    $self->{insertion_mode} == IN_HEAD_IM or
4929                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4930                  !!!cp ('t140');                  !!!cp ('t140');
4931                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
4932                                    text => $token->{tag_name}, token => $token);
4933                  ## Ignore the token                  ## Ignore the token
4934                  !!!next-token;                  !!!next-token;
4935                  next B;                  next B;
4936                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4937                  !!!cp ('t140.1');                  !!!cp ('t140.1');
4938                  !!!parse-error (type => 'unmatched end tag:' . $token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
4939                                    text => $token->{tag_name}, token => $token);
4940                  ## Ignore the token                  ## Ignore the token
4941                  !!!next-token;                  !!!next-token;
4942                  next B;                  next B;
# Line 4571  sub _tree_construction_main ($) { Line 4945  sub _tree_construction_main ($) {
4945                }                }
4946              } elsif ($token->{tag_name} eq 'p') {              } elsif ($token->{tag_name} eq 'p') {
4947                !!!cp ('t142');                !!!cp ('t142');
4948                !!!parse-error (type => 'unmatched end tag:p', token => $token);                !!!parse-error (type => 'unmatched end tag',
4949                                  text => $token->{tag_name}, token => $token);
4950                ## Ignore the token                ## Ignore the token
4951                !!!next-token;                !!!next-token;
4952                next B;                next B;
# Line 4594  sub _tree_construction_main ($) { Line 4969  sub _tree_construction_main ($) {
4969                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4970                  !!!cp ('t143.3');                  !!!cp ('t143.3');
4971                  ## ISSUE: Two parse errors for <head><noscript></br>                  ## ISSUE: Two parse errors for <head><noscript></br>
4972                  !!!parse-error (type => 'unmatched end tag:br', token => $token);                  !!!parse-error (type => 'unmatched end tag',
4973                                    text => 'br', token => $token);
4974                  ## As if </noscript>                  ## As if </noscript>
4975                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4976                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
# Line 4613  sub _tree_construction_main ($) { Line 4989  sub _tree_construction_main ($) {
4989                }                }
4990    
4991                ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.                ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4992                !!!parse-error (type => 'unmatched end tag:br', token => $token);                !!!parse-error (type => 'unmatched end tag',
4993                                  text => 'br', token => $token);
4994                ## Ignore the token                ## Ignore the token
4995                !!!next-token;                !!!next-token;
4996                next B;                next B;
4997              } else {              } else {
4998                !!!cp ('t145');                !!!cp ('t145');
4999                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
5000                                  text => $token->{tag_name}, token => $token);
5001                ## Ignore the token                ## Ignore the token
5002                !!!next-token;                !!!next-token;
5003                next B;                next B;
# Line 4629  sub _tree_construction_main ($) { Line 5007  sub _tree_construction_main ($) {
5007                !!!cp ('t146');                !!!cp ('t146');
5008                ## As if </noscript>                ## As if </noscript>
5009                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5010                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'in noscript:/',
5011                                  text => $token->{tag_name}, token => $token);
5012                                
5013                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
5014                ## As if </head>                ## As if </head>
# Line 4645  sub _tree_construction_main ($) { Line 5024  sub _tree_construction_main ($) {
5024              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5025  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
5026                !!!cp ('t148');                !!!cp ('t148');
5027                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
5028                                  text => $token->{tag_name}, token => $token);
5029                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
5030                !!!next-token;                !!!next-token;
5031                next B;                next B;
# Line 4756  sub _tree_construction_main ($) { Line 5136  sub _tree_construction_main ($) {
5136    
5137                  !!!cp ('t153');                  !!!cp ('t153');
5138                  !!!parse-error (type => 'start tag not allowed',                  !!!parse-error (type => 'start tag not allowed',
5139                      value => $token->{tag_name}, token => $token);                      text => $token->{tag_name}, token => $token);
5140                  ## Ignore the token                  ## Ignore the token
5141                  !!!nack ('t153.1');                  !!!nack ('t153.1');
5142                  !!!next-token;                  !!!next-token;
5143                  next B;                  next B;
5144                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5145                  !!!parse-error (type => 'not closed:caption', token => $token);                  !!!parse-error (type => 'not closed', text => 'caption',
5146                                    token => $token);
5147                                    
5148                  ## NOTE: As if </caption>.                  ## NOTE: As if </caption>.
5149                  ## have a table element in table scope                  ## have a table element in table scope
# Line 4782  sub _tree_construction_main ($) { Line 5163  sub _tree_construction_main ($) {
5163    
5164                    !!!cp ('t157');                    !!!cp ('t157');
5165                    !!!parse-error (type => 'start tag not allowed',                    !!!parse-error (type => 'start tag not allowed',
5166                                    value => $token->{tag_name}, token => $token);                                    text => $token->{tag_name}, token => $token);
5167                    ## Ignore the token                    ## Ignore the token
5168                    !!!nack ('t157.1');                    !!!nack ('t157.1');
5169                    !!!next-token;                    !!!next-token;
# Line 4799  sub _tree_construction_main ($) { Line 5180  sub _tree_construction_main ($) {
5180                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5181                    !!!cp ('t159');                    !!!cp ('t159');
5182                    !!!parse-error (type => 'not closed',                    !!!parse-error (type => 'not closed',
5183                                    value => $self->{open_elements}->[-1]->[0]                                    text => $self->{open_elements}->[-1]->[0]
5184                                        ->manakai_local_name,                                        ->manakai_local_name,
5185                                    token => $token);                                    token => $token);
5186                  } else {                  } else {
# Line 4841  sub _tree_construction_main ($) { Line 5222  sub _tree_construction_main ($) {
5222                  } # INSCOPE                  } # INSCOPE
5223                    unless (defined $i) {                    unless (defined $i) {
5224                      !!!cp ('t165');                      !!!cp ('t165');
5225                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag',
5226                                        text => $token->{tag_name},
5227                                        token => $token);
5228                      ## Ignore the token                      ## Ignore the token
5229                      !!!next-token;                      !!!next-token;
5230                      next B;                      next B;
# Line 4858  sub _tree_construction_main ($) { Line 5241  sub _tree_construction_main ($) {
5241                          ne $token->{tag_name}) {                          ne $token->{tag_name}) {
5242                    !!!cp ('t167');                    !!!cp ('t167');
5243                    !!!parse-error (type => 'not closed',                    !!!parse-error (type => 'not closed',
5244                                    value => $self->{open_elements}->[-1]->[0]                                    text => $self->{open_elements}->[-1]->[0]
5245                                        ->manakai_local_name,                                        ->manakai_local_name,
5246                                    token => $token);                                    token => $token);
5247                  } else {                  } else {
# Line 4875  sub _tree_construction_main ($) { Line 5258  sub _tree_construction_main ($) {
5258                  next B;                  next B;
5259                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5260                  !!!cp ('t169');                  !!!cp ('t169');
5261                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5262                                    text => $token->{tag_name}, token => $token);
5263                  ## Ignore the token                  ## Ignore the token
5264                  !!!next-token;                  !!!next-token;
5265                  next B;                  next B;
# Line 4902  sub _tree_construction_main ($) { Line 5286  sub _tree_construction_main ($) {
5286    
5287                    !!!cp ('t173');                    !!!cp ('t173');
5288                    !!!parse-error (type => 'unmatched end tag',                    !!!parse-error (type => 'unmatched end tag',
5289                                    value => $token->{tag_name}, token => $token);                                    text => $token->{tag_name}, token => $token);
5290                    ## Ignore the token                    ## Ignore the token
5291                    !!!next-token;                    !!!next-token;
5292                    next B;                    next B;
# Line 4918  sub _tree_construction_main ($) { Line 5302  sub _tree_construction_main ($) {
5302                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5303                    !!!cp ('t175');                    !!!cp ('t175');
5304                    !!!parse-error (type => 'not closed',                    !!!parse-error (type => 'not closed',
5305                                    value => $self->{open_elements}->[-1]->[0]                                    text => $self->{open_elements}->[-1]->[0]
5306                                        ->manakai_local_name,                                        ->manakai_local_name,
5307                                    token => $token);                                    token => $token);
5308                  } else {                  } else {
# Line 4935  sub _tree_construction_main ($) { Line 5319  sub _tree_construction_main ($) {
5319                  next B;                  next B;
5320                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5321                  !!!cp ('t177');                  !!!cp ('t177');
5322                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5323                                    text => $token->{tag_name}, token => $token);
5324                  ## Ignore the token                  ## Ignore the token
5325                  !!!next-token;                  !!!next-token;
5326                  next B;                  next B;
# Line 4978  sub _tree_construction_main ($) { Line 5363  sub _tree_construction_main ($) {
5363    
5364                  !!!cp ('t182');                  !!!cp ('t182');
5365                  !!!parse-error (type => 'unmatched end tag',                  !!!parse-error (type => 'unmatched end tag',
5366                      value => $token->{tag_name}, token => $token);                      text => $token->{tag_name}, token => $token);
5367                  ## Ignore the token                  ## Ignore the token
5368                  !!!next-token;                  !!!next-token;
5369                  next B;                  next B;
5370                } # INSCOPE                } # INSCOPE
5371              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5372                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5373                !!!parse-error (type => 'not closed:caption', token => $token);                !!!parse-error (type => 'not closed', text => 'caption',
5374                                  token => $token);
5375    
5376                ## As if </caption>                ## As if </caption>
5377                ## have a table element in table scope                ## have a table element in table scope
# Line 5003  sub _tree_construction_main ($) { Line 5389  sub _tree_construction_main ($) {
5389                } # INSCOPE                } # INSCOPE
5390                unless (defined $i) {                unless (defined $i) {
5391                  !!!cp ('t186');                  !!!cp ('t186');
5392                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);                  !!!parse-error (type => 'unmatched end tag',
5393                                    text => 'caption', token => $token);
5394                  ## Ignore the token                  ## Ignore the token
5395                  !!!next-token;                  !!!next-token;
5396                  next B;                  next B;
# Line 5018  sub _tree_construction_main ($) { Line 5405  sub _tree_construction_main ($) {
5405                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5406                  !!!cp ('t188');                  !!!cp ('t188');
5407                  !!!parse-error (type => 'not closed',                  !!!parse-error (type => 'not closed',
5408                                  value => $self->{open_elements}->[-1]->[0]                                  text => $self->{open_elements}->[-1]->[0]
5409                                      ->manakai_local_name,                                      ->manakai_local_name,
5410                                  token => $token);                                  token => $token);
5411                } else {                } else {
# Line 5038  sub _tree_construction_main ($) { Line 5425  sub _tree_construction_main ($) {
5425                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5426                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5427                  !!!cp ('t190');                  !!!cp ('t190');
5428                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5429                                    text => $token->{tag_name}, token => $token);
5430                  ## Ignore the token                  ## Ignore the token
5431                  !!!next-token;                  !!!next-token;
5432                  next B;                  next B;
# Line 5052  sub _tree_construction_main ($) { Line 5440  sub _tree_construction_main ($) {
5440                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5441                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5442                !!!cp ('t192');                !!!cp ('t192');
5443                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
5444                                  text => $token->{tag_name}, token => $token);
5445                ## Ignore the token                ## Ignore the token
5446                !!!next-token;                !!!next-token;
5447                next B;                next B;
# Line 5092  sub _tree_construction_main ($) { Line 5481  sub _tree_construction_main ($) {
5481            }            }
5482          }          }
5483    
5484              !!!parse-error (type => 'in table:#character', token => $token);          !!!parse-error (type => 'in table:#text', token => $token);
5485    
5486              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5487              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 5143  sub _tree_construction_main ($) { Line 5532  sub _tree_construction_main ($) {
5532          !!!next-token;          !!!next-token;
5533          next B;          next B;
5534        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5535              if ({          if ({
5536                   tr => ($self->{insertion_mode} != IN_ROW_IM),               tr => ($self->{insertion_mode} != IN_ROW_IM),
5537                   th => 1, td => 1,               th => 1, td => 1,
5538                  }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
5539                if ($self->{insertion_mode} == IN_TABLE_IM) {            if ($self->{insertion_mode} == IN_TABLE_IM) {
5540                  ## Clear back to table context              ## Clear back to table context
5541                  while (not ($self->{open_elements}->[-1]->[1]              while (not ($self->{open_elements}->[-1]->[1]
5542                                  & TABLE_SCOPING_EL)) {                              & TABLE_SCOPING_EL)) {
5543                    !!!cp ('t201');                !!!cp ('t201');
5544                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5545                  }              }
5546                                
5547                  !!!insert-element ('tbody',, $token);              !!!insert-element ('tbody',, $token);
5548                  $self->{insertion_mode} = IN_TABLE_BODY_IM;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5549                  ## reprocess in the "in table body" insertion mode...              ## reprocess in the "in table body" insertion mode...
5550                }            }
5551              
5552                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5553                  unless ($token->{tag_name} eq 'tr') {              unless ($token->{tag_name} eq 'tr') {
5554                    !!!cp ('t202');                !!!cp ('t202');
5555                    !!!parse-error (type => 'missing start tag:tr', token => $token);                !!!parse-error (type => 'missing start tag:tr', token => $token);
5556                  }              }
5557                                    
5558                  ## Clear back to table body context              ## Clear back to table body context
5559                  while (not ($self->{open_elements}->[-1]->[1]              while (not ($self->{open_elements}->[-1]->[1]
5560                                  & TABLE_ROWS_SCOPING_EL)) {                              & TABLE_ROWS_SCOPING_EL)) {
5561                    !!!cp ('t203');                !!!cp ('t203');
5562                    ## ISSUE: Can this case be reached?                ## ISSUE: Can this case be reached?
5563                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5564                  }              }
5565                                    
5566                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5567                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
# Line 5228  sub _tree_construction_main ($) { Line 5617  sub _tree_construction_main ($) {
5617                  unless (defined $i) {                  unless (defined $i) {
5618                    !!!cp ('t210');                    !!!cp ('t210');
5619  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5620                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmacthed end tag',
5621                                      text => $token->{tag_name}, token => $token);
5622                    ## Ignore the token                    ## Ignore the token
5623                    !!!nack ('t210.1');                    !!!nack ('t210.1');
5624                    !!!next-token;                    !!!next-token;
# Line 5272  sub _tree_construction_main ($) { Line 5662  sub _tree_construction_main ($) {
5662                  } # INSCOPE                  } # INSCOPE
5663                  unless (defined $i) {                  unless (defined $i) {
5664                    !!!cp ('t216');                    !!!cp ('t216');
5665  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type is wrong.
5666                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5667                                      text => $token->{tag_name}, token => $token);
5668                    ## Ignore the token                    ## Ignore the token
5669                    !!!nack ('t216.1');                    !!!nack ('t216.1');
5670                    !!!next-token;                    !!!next-token;
# Line 5348  sub _tree_construction_main ($) { Line 5739  sub _tree_construction_main ($) {
5739                }                }
5740              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5741                !!!parse-error (type => 'not closed',                !!!parse-error (type => 'not closed',
5742                                value => $self->{open_elements}->[-1]->[0]                                text => $self->{open_elements}->[-1]->[0]
5743                                    ->manakai_local_name,                                    ->manakai_local_name,
5744                                token => $token);                                token => $token);
5745    
# Line 5369  sub _tree_construction_main ($) { Line 5760  sub _tree_construction_main ($) {
5760                unless (defined $i) {                unless (defined $i) {
5761                  !!!cp ('t223');                  !!!cp ('t223');
5762  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5763                  !!!parse-error (type => 'unmatched end tag:table', token => $token);                  !!!parse-error (type => 'unmatched end tag', text => 'table',
5764                                    token => $token);
5765                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5766                  !!!nack ('t223.1');                  !!!nack ('t223.1');
5767                  !!!next-token;                  !!!next-token;
5768                  next B;                  next B;
5769                }                }
5770                                
5771  ## TODO: Followings are removed from the latest spec.  ## TODO: Followings are removed from the latest spec.
5772                ## generate implied end tags                ## generate implied end tags
5773                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5774                  !!!cp ('t224');                  !!!cp ('t224');
# Line 5387  sub _tree_construction_main ($) { Line 5779  sub _tree_construction_main ($) {
5779                  !!!cp ('t225');                  !!!cp ('t225');
5780                  ## NOTE: |<table><tr><table>|                  ## NOTE: |<table><tr><table>|
5781                  !!!parse-error (type => 'not closed',                  !!!parse-error (type => 'not closed',
5782                                  value => $self->{open_elements}->[-1]->[0]                                  text => $self->{open_elements}->[-1]->[0]
5783                                      ->manakai_local_name,                                      ->manakai_local_name,
5784                                  token => $token);                                  token => $token);
5785                } else {                } else {
# Line 5428  sub _tree_construction_main ($) { Line 5820  sub _tree_construction_main ($) {
5820                my $type = lc $token->{attributes}->{type}->{value};                my $type = lc $token->{attributes}->{type}->{value};
5821                if ($type eq 'hidden') {                if ($type eq 'hidden') {
5822                  !!!cp ('t227.3');                  !!!cp ('t227.3');
5823                  !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'in table',
5824                                    text => $token->{tag_name}, token => $token);
5825    
5826                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5827    
# Line 5456  sub _tree_construction_main ($) { Line 5849  sub _tree_construction_main ($) {
5849            #            #
5850          }          }
5851    
5852          !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'in table', text => $token->{tag_name},
5853                            token => $token);
5854    
5855          $insert = $insert_to_foster;          $insert = $insert_to_foster;
5856          #          #
# Line 5478  sub _tree_construction_main ($) { Line 5872  sub _tree_construction_main ($) {
5872                } # INSCOPE                } # INSCOPE
5873                unless (defined $i) {                unless (defined $i) {
5874                  !!!cp ('t230');                  !!!cp ('t230');
5875                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5876                                    text => $token->{tag_name}, token => $token);
5877                  ## Ignore the token                  ## Ignore the token
5878                  !!!nack ('t230.1');                  !!!nack ('t230.1');
5879                  !!!next-token;                  !!!next-token;
# Line 5519  sub _tree_construction_main ($) { Line 5914  sub _tree_construction_main ($) {
5914                  unless (defined $i) {                  unless (defined $i) {
5915                    !!!cp ('t235');                    !!!cp ('t235');
5916  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5917                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5918                                      text => $token->{type}, token => $token);
5919                    ## Ignore the token                    ## Ignore the token
5920                    !!!nack ('t236.1');                    !!!nack ('t236.1');
5921                    !!!next-token;                    !!!next-token;
# Line 5555  sub _tree_construction_main ($) { Line 5951  sub _tree_construction_main ($) {
5951                  } # INSCOPE                  } # INSCOPE
5952                  unless (defined $i) {                  unless (defined $i) {
5953                    !!!cp ('t239');                    !!!cp ('t239');
5954                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5955                                      text => $token->{tag_name}, token => $token);
5956                    ## Ignore the token                    ## Ignore the token
5957                    !!!nack ('t239.1');                    !!!nack ('t239.1');
5958                    !!!next-token;                    !!!next-token;
# Line 5601  sub _tree_construction_main ($) { Line 5998  sub _tree_construction_main ($) {
5998                } # INSCOPE                } # INSCOPE
5999                unless (defined $i) {                unless (defined $i) {
6000                  !!!cp ('t243');                  !!!cp ('t243');
6001                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
6002                                    text => $token->{tag_name}, token => $token);
6003                  ## Ignore the token                  ## Ignore the token
6004                  !!!nack ('t243.1');                  !!!nack ('t243.1');
6005                  !!!next-token;                  !!!next-token;
# Line 5635  sub _tree_construction_main ($) { Line 6033  sub _tree_construction_main ($) {
6033                  } # INSCOPE                  } # INSCOPE
6034                    unless (defined $i) {                    unless (defined $i) {
6035                      !!!cp ('t249');                      !!!cp ('t249');
6036                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag',
6037                                        text => $token->{tag_name}, token => $token);
6038                      ## Ignore the token                      ## Ignore the token
6039                      !!!nack ('t249.1');                      !!!nack ('t249.1');
6040                      !!!next-token;                      !!!next-token;
# Line 5658  sub _tree_construction_main ($) { Line 6057  sub _tree_construction_main ($) {
6057                  } # INSCOPE                  } # INSCOPE
6058                    unless (defined $i) {                    unless (defined $i) {
6059                      !!!cp ('t252');                      !!!cp ('t252');
6060                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);                      !!!parse-error (type => 'unmatched end tag',
6061                                        text => 'tr', token => $token);
6062                      ## Ignore the token                      ## Ignore the token
6063                      !!!nack ('t252.1');                      !!!nack ('t252.1');
6064                      !!!next-token;                      !!!next-token;
# Line 5693  sub _tree_construction_main ($) { Line 6093  sub _tree_construction_main ($) {
6093                } # INSCOPE                } # INSCOPE
6094                unless (defined $i) {                unless (defined $i) {
6095                  !!!cp ('t256');                  !!!cp ('t256');
6096                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
6097                                    text => $token->{tag_name}, token => $token);
6098                  ## Ignore the token                  ## Ignore the token
6099                  !!!nack ('t256.1');                  !!!nack ('t256.1');
6100                  !!!next-token;                  !!!next-token;
# Line 5720  sub _tree_construction_main ($) { Line 6121  sub _tree_construction_main ($) {
6121                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
6122                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
6123            !!!cp ('t258');            !!!cp ('t258');
6124            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
6125                              text => $token->{tag_name}, token => $token);
6126            ## Ignore the token            ## Ignore the token
6127            !!!nack ('t258.1');            !!!nack ('t258.1');
6128             !!!next-token;             !!!next-token;
6129            next B;            next B;
6130          } else {          } else {
6131            !!!cp ('t259');            !!!cp ('t259');
6132            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in table:/',
6133                              text => $token->{tag_name}, token => $token);
6134    
6135            $insert = $insert_to_foster;            $insert = $insert_to_foster;
6136            #            #
# Line 5777  sub _tree_construction_main ($) { Line 6180  sub _tree_construction_main ($) {
6180              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
6181                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6182                  !!!cp ('t264');                  !!!cp ('t264');
6183                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);                  !!!parse-error (type => 'unmatched end tag',
6184                                    text => 'colgroup', token => $token);
6185                  ## Ignore the token                  ## Ignore the token
6186                  !!!next-token;                  !!!next-token;
6187                  next B;                  next B;
# Line 5790  sub _tree_construction_main ($) { Line 6194  sub _tree_construction_main ($) {
6194                }                }
6195              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
6196                !!!cp ('t266');                !!!cp ('t266');
6197                !!!parse-error (type => 'unmatched end tag:col', token => $token);                !!!parse-error (type => 'unmatched end tag',
6198                                  text => 'col', token => $token);
6199                ## Ignore the token                ## Ignore the token
6200                !!!next-token;                !!!next-token;
6201                next B;                next B;
# Line 5820  sub _tree_construction_main ($) { Line 6225  sub _tree_construction_main ($) {
6225            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6226              !!!cp ('t269');              !!!cp ('t269');
6227  ## TODO: Wrong error type?  ## TODO: Wrong error type?
6228              !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);              !!!parse-error (type => 'unmatched end tag',
6229                                text => 'colgroup', token => $token);
6230              ## Ignore the token              ## Ignore the token
6231              !!!nack ('t269.1');              !!!nack ('t269.1');
6232              !!!next-token;              !!!next-token;
# Line 5884  sub _tree_construction_main ($) { Line 6290  sub _tree_construction_main ($) {
6290                     tr => 1, td => 1, th => 1,                     tr => 1, td => 1, th => 1,
6291                    }->{$token->{tag_name}})) {                    }->{$token->{tag_name}})) {
6292            ## TODO: The type below is not good - <select> is replaced by </select>            ## TODO: The type below is not good - <select> is replaced by </select>
6293            !!!parse-error (type => 'not closed:select', token => $token);            !!!parse-error (type => 'not closed', text => 'select',
6294                              token => $token);
6295            ## NOTE: As if the token were </select> (<select> case) or            ## NOTE: As if the token were </select> (<select> case) or
6296            ## as if there were </select> (otherwise).            ## as if there were </select> (otherwise).
6297            ## have an element in table scope            ## have an element in table scope
# Line 5902  sub _tree_construction_main ($) { Line 6309  sub _tree_construction_main ($) {
6309            } # INSCOPE            } # INSCOPE
6310            unless (defined $i) {            unless (defined $i) {
6311              !!!cp ('t280');              !!!cp ('t280');
6312              !!!parse-error (type => 'unmatched end tag:select', token => $token);              !!!parse-error (type => 'unmatched end tag',
6313                                text => 'select', token => $token);
6314              ## Ignore the token              ## Ignore the token
6315              !!!nack ('t280.1');              !!!nack ('t280.1');
6316              !!!next-token;              !!!next-token;
# Line 5926  sub _tree_construction_main ($) { Line 6334  sub _tree_construction_main ($) {
6334            }            }
6335          } else {          } else {
6336            !!!cp ('t282');            !!!cp ('t282');
6337            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select',
6338                              text => $token->{tag_name}, token => $token);
6339            ## Ignore the token            ## Ignore the token
6340            !!!nack ('t282.1');            !!!nack ('t282.1');
6341            !!!next-token;            !!!next-token;
# Line 5944  sub _tree_construction_main ($) { Line 6353  sub _tree_construction_main ($) {
6353              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6354            } else {            } else {
6355              !!!cp ('t285');              !!!cp ('t285');
6356              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6357                                text => $token->{tag_name}, token => $token);
6358              ## Ignore the token              ## Ignore the token
6359            }            }
6360            !!!nack ('t285.1');            !!!nack ('t285.1');
# Line 5956  sub _tree_construction_main ($) { Line 6366  sub _tree_construction_main ($) {
6366              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6367            } else {            } else {
6368              !!!cp ('t287');              !!!cp ('t287');
6369              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6370                                text => $token->{tag_name}, token => $token);
6371              ## Ignore the token              ## Ignore the token
6372            }            }
6373            !!!nack ('t287.1');            !!!nack ('t287.1');
# Line 5978  sub _tree_construction_main ($) { Line 6389  sub _tree_construction_main ($) {
6389            } # INSCOPE            } # INSCOPE
6390            unless (defined $i) {            unless (defined $i) {
6391              !!!cp ('t290');              !!!cp ('t290');
6392              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6393                                text => $token->{tag_name}, token => $token);
6394              ## Ignore the token              ## Ignore the token
6395              !!!nack ('t290.1');              !!!nack ('t290.1');
6396              !!!next-token;              !!!next-token;
# Line 5999  sub _tree_construction_main ($) { Line 6411  sub _tree_construction_main ($) {
6411                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6412                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
6413  ## TODO: The following is wrong?  ## TODO: The following is wrong?
6414            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
6415                              text => $token->{tag_name}, token => $token);
6416                                
6417            ## have an element in table scope            ## have an element in table scope
6418            my $i;            my $i;
# Line 6040  sub _tree_construction_main ($) { Line 6453  sub _tree_construction_main ($) {
6453            unless (defined $i) {            unless (defined $i) {
6454              !!!cp ('t297');              !!!cp ('t297');
6455  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
6456              !!!parse-error (type => 'unmatched end tag:select', token => $token);              !!!parse-error (type => 'unmatched end tag',
6457                                text => 'select', token => $token);
6458              ## Ignore the </select> token              ## Ignore the </select> token
6459              !!!nack ('t297.1');              !!!nack ('t297.1');
6460              !!!next-token; ## TODO: ok?              !!!next-token; ## TODO: ok?
# Line 6057  sub _tree_construction_main ($) { Line 6471  sub _tree_construction_main ($) {
6471            next B;            next B;
6472          } else {          } else {
6473            !!!cp ('t299');            !!!cp ('t299');
6474            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select:/',
6475                              text => $token->{tag_name}, token => $token);
6476            ## Ignore the token            ## Ignore the token
6477            !!!nack ('t299.3');            !!!nack ('t299.3');
6478            !!!next-token;            !!!next-token;
# Line 6095  sub _tree_construction_main ($) { Line 6510  sub _tree_construction_main ($) {
6510                    
6511          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6512            !!!cp ('t301');            !!!cp ('t301');
6513            !!!parse-error (type => 'after html:#character', token => $token);            !!!parse-error (type => 'after html:#text', token => $token);
6514    
6515            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6516          } else {          } else {
# Line 6103  sub _tree_construction_main ($) { Line 6518  sub _tree_construction_main ($) {
6518          }          }
6519                    
6520          ## "after body" insertion mode          ## "after body" insertion mode
6521          !!!parse-error (type => 'after body:#character', token => $token);          !!!parse-error (type => 'after body:#text', token => $token);
6522    
6523          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6524          ## reprocess          ## reprocess
# Line 6111  sub _tree_construction_main ($) { Line 6526  sub _tree_construction_main ($) {
6526        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6527          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6528            !!!cp ('t303');            !!!cp ('t303');
6529            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after html',
6530                              text => $token->{tag_name}, token => $token);
6531                        
6532            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6533          } else {          } else {
# Line 6119  sub _tree_construction_main ($) { Line 6535  sub _tree_construction_main ($) {
6535          }          }
6536    
6537          ## "after body" insertion mode          ## "after body" insertion mode
6538          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'after body',
6539                            text => $token->{tag_name}, token => $token);
6540    
6541          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6542          !!!ack-later;          !!!ack-later;
# Line 6128  sub _tree_construction_main ($) { Line 6545  sub _tree_construction_main ($) {
6545        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6546          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6547            !!!cp ('t305');            !!!cp ('t305');
6548            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after html:/',
6549                              text => $token->{tag_name}, token => $token);
6550                        
6551            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6552            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
# Line 6140  sub _tree_construction_main ($) { Line 6558  sub _tree_construction_main ($) {
6558          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6559            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6560              !!!cp ('t307');              !!!cp ('t307');
6561              !!!parse-error (type => 'unmatched end tag:html', token => $token);              !!!parse-error (type => 'unmatched end tag',
6562                                text => 'html', token => $token);
6563              ## Ignore the token              ## Ignore the token
6564              !!!next-token;              !!!next-token;
6565              next B;              next B;
# Line 6152  sub _tree_construction_main ($) { Line 6571  sub _tree_construction_main ($) {
6571            }            }
6572          } else {          } else {
6573            !!!cp ('t309');            !!!cp ('t309');
6574            !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after body:/',
6575                              text => $token->{tag_name}, token => $token);
6576    
6577            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6578            ## reprocess            ## reprocess
# Line 6180  sub _tree_construction_main ($) { Line 6600  sub _tree_construction_main ($) {
6600          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6601            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6602              !!!cp ('t311');              !!!cp ('t311');
6603              !!!parse-error (type => 'in frameset:#character', token => $token);              !!!parse-error (type => 'in frameset:#text', token => $token);
6604            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6605              !!!cp ('t312');              !!!cp ('t312');
6606              !!!parse-error (type => 'after frameset:#character', token => $token);              !!!parse-error (type => 'after frameset:#text', token => $token);
6607            } else { # "after html frameset"            } else { # "after after frameset"
6608              !!!cp ('t313');              !!!cp ('t313');
6609              !!!parse-error (type => 'after html:#character', token => $token);              !!!parse-error (type => 'after html:#text', token => $token);
   
             $self->{insertion_mode} = AFTER_FRAMESET_IM;  
             ## Reprocess in the "after frameset" insertion mode.  
             !!!parse-error (type => 'after frameset:#character', token => $token);  
6610            }            }
6611                        
6612            ## Ignore the token.            ## Ignore the token.
# Line 6206  sub _tree_construction_main ($) { Line 6622  sub _tree_construction_main ($) {
6622                    
6623          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6624        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
         if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {  
           !!!cp ('t316');  
           !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);  
   
           $self->{insertion_mode} = AFTER_FRAMESET_IM;  
           ## Process in the "after frameset" insertion mode.  
         } else {  
           !!!cp ('t317');  
         }  
   
6625          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6626              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6627            !!!cp ('t318');            !!!cp ('t318');
# Line 6233  sub _tree_construction_main ($) { Line 6639  sub _tree_construction_main ($) {
6639            next B;            next B;
6640          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6641            !!!cp ('t320');            !!!cp ('t320');
6642            ## NOTE: As if in body.            ## NOTE: As if in head.
6643            $parse_rcdata->(CDATA_CONTENT_MODEL);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6644            next B;            next B;
6645    
6646              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6647              ## has no parse error.
6648          } else {          } else {
6649            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6650              !!!cp ('t321');              !!!cp ('t321');
6651              !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'in frameset',
6652            } else {                              text => $token->{tag_name}, token => $token);
6653              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6654              !!!cp ('t322');              !!!cp ('t322');
6655              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'after frameset',
6656                                text => $token->{tag_name}, token => $token);
6657              } else { # "after after frameset"
6658                !!!cp ('t322.2');
6659                !!!parse-error (type => 'after after frameset',
6660                                text => $token->{tag_name}, token => $token);
6661            }            }
6662            ## Ignore the token            ## Ignore the token
6663            !!!nack ('t322.1');            !!!nack ('t322.1');
# Line 6250  sub _tree_construction_main ($) { Line 6665  sub _tree_construction_main ($) {
6665            next B;            next B;
6666          }          }
6667        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
         if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {  
           !!!cp ('t323');  
           !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);  
   
           $self->{insertion_mode} = AFTER_FRAMESET_IM;  
           ## Process in the "after frameset" insertion mode.  
         } else {  
           !!!cp ('t324');  
         }  
   
6668          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6669              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6670            if ($self->{open_elements}->[-1]->[1] & HTML_EL and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6671                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6672              !!!cp ('t325');              !!!cp ('t325');
6673              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6674                                text => $token->{tag_name}, token => $token);
6675              ## Ignore the token              ## Ignore the token
6676              !!!next-token;              !!!next-token;
6677            } else {            } else {
# Line 6291  sub _tree_construction_main ($) { Line 6697  sub _tree_construction_main ($) {
6697          } else {          } else {
6698            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6699              !!!cp ('t330');              !!!cp ('t330');
6700              !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'in frameset:/',
6701            } else {                              text => $token->{tag_name}, token => $token);
6702              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6703                !!!cp ('t330.1');
6704                !!!parse-error (type => 'after frameset:/',
6705                                text => $token->{tag_name}, token => $token);
6706              } else { # "after after html"
6707              !!!cp ('t331');              !!!cp ('t331');
6708              !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'after after frameset:/',
6709                                text => $token->{tag_name}, token => $token);
6710            }            }
6711            ## Ignore the token            ## Ignore the token
6712            !!!next-token;            !!!next-token;
# Line 6402  sub _tree_construction_main ($) { Line 6814  sub _tree_construction_main ($) {
6814          $parse_rcdata->(RCDATA_CONTENT_MODEL);          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6815          next B;          next B;
6816        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6817          !!!parse-error (type => 'in body:body', token => $token);          !!!parse-error (type => 'in body', text => 'body', token => $token);
6818                                
6819          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6820              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
# Line 6522  sub _tree_construction_main ($) { Line 6934  sub _tree_construction_main ($) {
6934              if ($i != -1) {              if ($i != -1) {
6935                !!!cp ('t355');                !!!cp ('t355');
6936                !!!parse-error (type => 'not closed',                !!!parse-error (type => 'not closed',
6937                                value => $self->{open_elements}->[-1]->[0]                                text => $self->{open_elements}->[-1]->[0]
6938                                    ->manakai_local_name,                                    ->manakai_local_name,
6939                                token => $token);                                token => $token);
6940              } else {              } else {
# Line 6676  sub _tree_construction_main ($) { Line 7088  sub _tree_construction_main ($) {
7088                  xmp => 1,                  xmp => 1,
7089                  iframe => 1,                  iframe => 1,
7090                  noembed => 1,                  noembed => 1,
7091                  noframes => 1,                  noframes => 1, ## NOTE: This is an "as if in head" code clone.
7092                  noscript => 0, ## TODO: 1 if scripting is enabled                  noscript => 0, ## TODO: 1 if scripting is enabled
7093                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7094          if ($token->{tag_name} eq 'xmp') {          if ($token->{tag_name} eq 'xmp') {
# Line 6698  sub _tree_construction_main ($) { Line 7110  sub _tree_construction_main ($) {
7110            !!!next-token;            !!!next-token;
7111            next B;            next B;
7112          } else {          } else {
7113              !!!ack ('t391.1');
7114    
7115            my $at = $token->{attributes};            my $at = $token->{attributes};
7116            my $form_attrs;            my $form_attrs;
7117            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 6741  sub _tree_construction_main ($) { Line 7155  sub _tree_construction_main ($) {
7155                           line => $token->{line}, column => $token->{column}},                           line => $token->{line}, column => $token->{column}},
7156                          {type => END_TAG_TOKEN, tag_name => 'form',                          {type => END_TAG_TOKEN, tag_name => 'form',
7157                           line => $token->{line}, column => $token->{column}};                           line => $token->{line}, column => $token->{column}};
           !!!nack ('t391.1'); ## NOTE: Not acknowledged.  
7158            !!!back-token (@tokens);            !!!back-token (@tokens);
7159            !!!next-token;            !!!next-token;
7160            next B;            next B;
# Line 6789  sub _tree_construction_main ($) { Line 7202  sub _tree_construction_main ($) {
7202            ## Ignore the token            ## Ignore the token
7203          } else {          } else {
7204            !!!cp ('t398');            !!!cp ('t398');
7205            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
7206          }          }
7207          !!!next-token;          !!!next-token;
7208          next B;          next B;
7209          } elsif ($token->{tag_name} eq 'rt' or
7210                   $token->{tag_name} eq 'rp') {
7211            ## has a |ruby| element in scope
7212            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7213              my $node = $self->{open_elements}->[$_];
7214              if ($node->[1] & RUBY_EL) {
7215                !!!cp ('t398.1');
7216                ## generate implied end tags
7217                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7218                  !!!cp ('t398.2');
7219                  pop @{$self->{open_elements}};
7220                }
7221                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
7222                  !!!cp ('t398.3');
7223                  !!!parse-error (type => 'not closed',
7224                                  text => $self->{open_elements}->[-1]->[0]
7225                                      ->manakai_local_name,
7226                                  token => $token);
7227                  pop @{$self->{open_elements}}
7228                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
7229                }
7230                last INSCOPE;
7231              } elsif ($node->[1] & SCOPING_EL) {
7232                !!!cp ('t398.4');
7233                last INSCOPE;
7234              }
7235            } # INSCOPE
7236    
7237            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7238    
7239            !!!nack ('t398.5');
7240            !!!next-token;
7241            redo B;
7242        } elsif ($token->{tag_name} eq 'math' or        } elsif ($token->{tag_name} eq 'math' or
7243                 $token->{tag_name} eq 'svg') {                 $token->{tag_name} eq 'svg') {
7244          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7245    
7246            ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
7247    
7248          ## "adjust SVG attributes" ('svg' only) - done in insert-element-f          ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
7249    
7250          ## "adjust foreign attributes" - done in insert-element-f          ## "adjust foreign attributes" - done in insert-element-f
# Line 6823  sub _tree_construction_main ($) { Line 7271  sub _tree_construction_main ($) {
7271                  thead => 1, tr => 1,                  thead => 1, tr => 1,
7272                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7273          !!!cp ('t401');          !!!cp ('t401');
7274          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'in body',
7275                            text => $token->{tag_name}, token => $token);
7276          ## Ignore the token          ## Ignore the token
7277          !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.          !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7278          !!!next-token;          !!!next-token;
# Line 6908  sub _tree_construction_main ($) { Line 7357  sub _tree_construction_main ($) {
7357            }            }
7358    
7359            !!!parse-error (type => 'start tag not allowed',            !!!parse-error (type => 'start tag not allowed',
7360                            value => $token->{tag_name}, token => $token);                            text => $token->{tag_name}, token => $token);
7361            ## NOTE: Ignore the token.            ## NOTE: Ignore the token.
7362            !!!next-token;            !!!next-token;
7363            next B;            next B;
# Line 6918  sub _tree_construction_main ($) { Line 7367  sub _tree_construction_main ($) {
7367            unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {            unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7368              !!!cp ('t403');              !!!cp ('t403');
7369              !!!parse-error (type => 'not closed',              !!!parse-error (type => 'not closed',
7370                              value => $_->[0]->manakai_local_name,                              text => $_->[0]->manakai_local_name,
7371                              token => $token);                              token => $token);
7372              last;              last;
7373            } else {            } else {
# Line 6938  sub _tree_construction_main ($) { Line 7387  sub _tree_construction_main ($) {
7387            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7388              !!!cp ('t406');              !!!cp ('t406');
7389              !!!parse-error (type => 'not closed',              !!!parse-error (type => 'not closed',
7390                              value => $self->{open_elements}->[1]->[0]                              text => $self->{open_elements}->[1]->[0]
7391                                  ->manakai_local_name,                                  ->manakai_local_name,
7392                              token => $token);                              token => $token);
7393            } else {            } else {
# Line 6949  sub _tree_construction_main ($) { Line 7398  sub _tree_construction_main ($) {
7398            next B;            next B;
7399          } else {          } else {
7400            !!!cp ('t408');            !!!cp ('t408');
7401            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7402                              text => $token->{tag_name}, token => $token);
7403            ## Ignore the token            ## Ignore the token
7404            !!!next-token;            !!!next-token;
7405            next B;            next B;
# Line 6977  sub _tree_construction_main ($) { Line 7427  sub _tree_construction_main ($) {
7427    
7428          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7429            !!!cp ('t413');            !!!cp ('t413');
7430            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7431                              text => $token->{tag_name}, token => $token);
7432              ## NOTE: Ignore the token.
7433          } else {          } else {
7434            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7435            while ({            while ({
7436                      ## END_TAG_OPTIONAL_EL
7437                    dd => ($token->{tag_name} ne 'dd'),                    dd => ($token->{tag_name} ne 'dd'),
7438                    dt => ($token->{tag_name} ne 'dt'),                    dt => ($token->{tag_name} ne 'dt'),
7439                    li => ($token->{tag_name} ne 'li'),                    li => ($token->{tag_name} ne 'li'),
7440                    p => 1,                    p => 1,
7441                      rt => 1,
7442                      rp => 1,
7443                   }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {                   }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7444              !!!cp ('t409');              !!!cp ('t409');
7445              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
# Line 6995  sub _tree_construction_main ($) { Line 7450  sub _tree_construction_main ($) {
7450                    ne $token->{tag_name}) {                    ne $token->{tag_name}) {
7451              !!!cp ('t412');              !!!cp ('t412');
7452              !!!parse-error (type => 'not closed',              !!!parse-error (type => 'not closed',
7453                              value => $self->{open_elements}->[-1]->[0]                              text => $self->{open_elements}->[-1]->[0]
7454                                  ->manakai_local_name,                                  ->manakai_local_name,
7455                              token => $token);                              token => $token);
7456            } else {            } else {
# Line 7032  sub _tree_construction_main ($) { Line 7487  sub _tree_construction_main ($) {
7487    
7488          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7489            !!!cp ('t421');            !!!cp ('t421');
7490            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7491                              text => $token->{tag_name}, token => $token);
7492              ## NOTE: Ignore the token.
7493          } else {          } else {
7494            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7495            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
# Line 7045  sub _tree_construction_main ($) { Line 7502  sub _tree_construction_main ($) {
7502                    ne $token->{tag_name}) {                    ne $token->{tag_name}) {
7503              !!!cp ('t417.1');              !!!cp ('t417.1');
7504              !!!parse-error (type => 'not closed',              !!!parse-error (type => 'not closed',
7505                              value => $self->{open_elements}->[-1]->[0]                              text => $self->{open_elements}->[-1]->[0]
7506                                  ->manakai_local_name,                                  ->manakai_local_name,
7507                              token => $token);                              token => $token);
7508            } else {            } else {
# Line 7077  sub _tree_construction_main ($) { Line 7534  sub _tree_construction_main ($) {
7534    
7535          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7536            !!!cp ('t425.1');            !!!cp ('t425.1');
7537            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7538                              text => $token->{tag_name}, token => $token);
7539              ## NOTE: Ignore the token.
7540          } else {          } else {
7541            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7542            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
# Line 7089  sub _tree_construction_main ($) { Line 7548  sub _tree_construction_main ($) {
7548            if ($self->{open_elements}->[-1]->[0]->manakai_local_name            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7549                    ne $token->{tag_name}) {                    ne $token->{tag_name}) {
7550              !!!cp ('t425');              !!!cp ('t425');
7551              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
7552                                text => $token->{tag_name}, token => $token);
7553            } else {            } else {
7554              !!!cp ('t426');              !!!cp ('t426');
7555            }            }
# Line 7120  sub _tree_construction_main ($) { Line 7580  sub _tree_construction_main ($) {
7580                    ne $token->{tag_name}) {                    ne $token->{tag_name}) {
7581              !!!cp ('t412.1');              !!!cp ('t412.1');
7582              !!!parse-error (type => 'not closed',              !!!parse-error (type => 'not closed',
7583                              value => $self->{open_elements}->[-1]->[0]                              text => $self->{open_elements}->[-1]->[0]
7584                                  ->manakai_local_name,                                  ->manakai_local_name,
7585                              token => $token);                              token => $token);
7586            } else {            } else {
# Line 7130  sub _tree_construction_main ($) { Line 7590  sub _tree_construction_main ($) {
7590            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7591          } else {          } else {
7592            !!!cp ('t413.1');            !!!cp ('t413.1');
7593            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7594                              text => $token->{tag_name}, token => $token);
7595    
7596            !!!cp ('t415.1');            !!!cp ('t415.1');
7597            ## As if <p>, then reprocess the current token            ## As if <p>, then reprocess the current token
# Line 7153  sub _tree_construction_main ($) { Line 7614  sub _tree_construction_main ($) {
7614          next B;          next B;
7615        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7616          !!!cp ('t428');          !!!cp ('t428');
7617          !!!parse-error (type => 'unmatched end tag:br', token => $token);          !!!parse-error (type => 'unmatched end tag',
7618                            text => 'br', token => $token);
7619    
7620          ## As if <br>          ## As if <br>
7621          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
# Line 7178  sub _tree_construction_main ($) { Line 7640  sub _tree_construction_main ($) {
7640                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7641                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7642          !!!cp ('t429');          !!!cp ('t429');
7643          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'unmatched end tag',
7644                            text => $token->{tag_name}, token => $token);
7645          ## Ignore the token          ## Ignore the token
7646          !!!next-token;          !!!next-token;
7647          next B;          next B;
# Line 7197  sub _tree_construction_main ($) { Line 7660  sub _tree_construction_main ($) {
7660              ## generate implied end tags              ## generate implied end tags
7661              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7662                !!!cp ('t430');                !!!cp ('t430');
7663                ## ISSUE: Can this case be reached?                ## NOTE: |<ruby><rt></ruby>|.
7664                  ## ISSUE: <ruby><rt></rt> will also take this code path,
7665                  ## which seems wrong.
7666                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
7667                  $node_i++;
7668              }              }
7669                    
7670              ## Step 2              ## Step 2
# Line 7207  sub _tree_construction_main ($) { Line 7673  sub _tree_construction_main ($) {
7673                !!!cp ('t431');                !!!cp ('t431');
7674                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7675                !!!parse-error (type => 'not closed',                !!!parse-error (type => 'not closed',
7676                                value => $self->{open_elements}->[-1]->[0]                                text => $self->{open_elements}->[-1]->[0]
7677                                    ->manakai_local_name,                                    ->manakai_local_name,
7678                                token => $token);                                token => $token);
7679              } else {              } else {
# Line 7215  sub _tree_construction_main ($) { Line 7681  sub _tree_construction_main ($) {
7681              }              }
7682                            
7683              ## Step 3              ## Step 3
7684              splice @{$self->{open_elements}}, $node_i;              splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7685    
7686              !!!next-token;              !!!next-token;
7687              last S2;              last S2;
# Line 7226  sub _tree_construction_main ($) { Line 7692  sub _tree_construction_main ($) {
7692                  ($node->[1] & SPECIAL_EL or                  ($node->[1] & SPECIAL_EL or
7693                   $node->[1] & SCOPING_EL)) {                   $node->[1] & SCOPING_EL)) {
7694                !!!cp ('t433');                !!!cp ('t433');
7695                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
7696                                  text => $token->{tag_name}, token => $token);
7697                ## Ignore the token                ## Ignore the token
7698                !!!next-token;                !!!next-token;
7699                last S2;                last S2;
# Line 7272  sub _tree_construction_main ($) { Line 7739  sub _tree_construction_main ($) {
7739    ## TODO: script stuffs    ## TODO: script stuffs
7740  } # _tree_construct_main  } # _tree_construct_main
7741    
7742  sub set_inner_html ($$$) {  sub set_inner_html ($$$$;$) {
7743    my $class = shift;    my $class = shift;
7744    my $node = shift;    my $node = shift;
7745    my $s = \$_[0];    #my $s = \$_[0];
7746    my $onerror = $_[1];    my $onerror = $_[1];
7747      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7748    
7749    ## ISSUE: Should {confident} be true?    ## ISSUE: Should {confident} be true?
7750    
# Line 7295  sub set_inner_html ($$$) { Line 7763  sub set_inner_html ($$$) {
7763      }      }
7764    
7765      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7766      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($_[0] => $node, $onerror, $get_wrapper);
7767    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7768      ## TODO: If non-html element      ## TODO: If non-html element
7769    
7770      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7771    
7772    ## TODO: Support for $get_wrapper
7773    
7774      ## Step 1 # MUST      ## Step 1 # MUST
7775      my $this_doc = $node->owner_document;      my $this_doc = $node->owner_document;
7776      my $doc = $this_doc->implementation->create_document;      my $doc = $this_doc->implementation->create_document;
# Line 7312  sub set_inner_html ($$$) { Line 7782  sub set_inner_html ($$$) {
7782      my $i = 0;      my $i = 0;
7783      $p->{line_prev} = $p->{line} = 1;      $p->{line_prev} = $p->{line} = 1;
7784      $p->{column_prev} = $p->{column} = 0;      $p->{column_prev} = $p->{column} = 0;
7785        require Whatpm::Charset::DecodeHandle;
7786        my $input = Whatpm::Charset::DecodeHandle::CharString->new (\($_[0]));
7787        $input = $get_wrapper->($input);
7788      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
7789        my $self = shift;        my $self = shift;
7790    
7791        pop @{$self->{prev_char}};        pop @{$self->{prev_char}};
7792        unshift @{$self->{prev_char}}, $self->{next_char};        unshift @{$self->{prev_char}}, $self->{next_char};
7793    
7794        $self->{next_char} = -1 and return if $i >= length $$s;        my $char = '';
7795        $self->{next_char} = ord substr $$s, $i++, 1;        if (defined $self->{next_next_char}) {
7796            $char = $self->{next_next_char};
7797            delete $self->{next_next_char};
7798            $self->{next_char} = ord $char;
7799          } else {
7800            if ($input->read ($char, 1)) {
7801              $self->{next_char} = ord $char;
7802            } else {
7803              $self->{next_char} = -1;
7804              return;
7805            }
7806          }
7807    
7808        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7809        $p->{column}++;        $p->{column}++;
# Line 7329  sub set_inner_html ($$$) { Line 7813  sub set_inner_html ($$$) {
7813          $p->{column} = 0;          $p->{column} = 0;
7814          !!!cp ('i1');          !!!cp ('i1');
7815        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7816          $i++ if substr ($$s, $i, 1) eq "\x0A";  ## TODO: support for abort/streaming
7817            my $next = '';
7818            if ($input->read ($next, 1) and $next ne "\x0A") {
7819              $self->{next_next_char} = $next;
7820            }
7821          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7822          $p->{line}++;          $p->{line}++;
7823          $p->{column} = 0;          $p->{column} = 0;
# Line 7362  sub set_inner_html ($$$) { Line 7850  sub set_inner_html ($$$) {
7850                  0x10FFFE => 1, 0x10FFFF => 1,                  0x10FFFE => 1, 0x10FFFF => 1,
7851                 }->{$self->{next_char}}) {                 }->{$self->{next_char}}) {
7852          !!!cp ('i4.1');          !!!cp ('i4.1');
7853          !!!parse-error (type => 'control char', level => $self->{must_level});          if ($self->{next_char} < 0x10000) {
7854  ## TODO: error type documentation            !!!parse-error (type => 'control char',
7855                              text => (sprintf 'U+%04X', $self->{next_char}));
7856            } else {
7857              !!!parse-error (type => 'control char',
7858                              text => (sprintf 'U-%08X', $self->{next_char}));
7859            }
7860        }        }
7861      };      };
7862      $p->{prev_char} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
7863      $p->{next_char} = -1;      $p->{next_char} = -1;
7864        
7865        $p->{read_until} = sub {
7866          #my ($scalar, $specials_range, $offset) = @_;
7867          my $specials_range = $_[1];
7868          return 0 if defined $p->{next_next_char};
7869          my $count = $input->manakai_read_until
7870            ($_[0],
7871             qr/(?![$specials_range\x{FDD0}-\x{FDDF}\x{FFFE}\x{FFFF}\x{1FFFE}\x{1FFFF}\x{2FFFE}\x{2FFFF}\x{3FFFE}\x{3FFFF}\x{4FFFE}\x{4FFFF}\x{5FFFE}\x{5FFFF}\x{6FFFE}\x{6FFFF}\x{7FFFE}\x{7FFFF}\x{8FFFE}\x{8FFFF}\x{9FFFE}\x{9FFFF}\x{AFFFE}\x{AFFFF}\x{BFFFE}\x{BFFFF}\x{CFFFE}\x{CFFFF}\x{DFFFE}\x{DFFFF}\x{EFFFE}\x{EFFFF}\x{FFFFE}\x{FFFFF}])[\x20-\x7E\xA0-\x{D7FF}\x{E000}-\x{10FFFD}]/,
7872             $_[2]);
7873          if ($count) {
7874            $p->{column} += $count;
7875            $p->{column_prev} += $count;
7876            $p->{prev_char} = [-1, -1, -1];
7877            $p->{next_char} = -1;
7878          }
7879          return $count;
7880        }; # $p->{read_until}
7881    
7882      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7883        my (%opt) = @_;        my (%opt) = @_;
7884        my $line = $opt{line};        my $line = $opt{line};
# Line 7383  sub set_inner_html ($$$) { Line 7893  sub set_inner_html ($$$) {
7893        $ponerror->(line => $p->{line}, column => $p->{column}, @_);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7894      };      };
7895            
7896        my $char_onerror = sub {
7897          my (undef, $type, %opt) = @_;
7898          $ponerror->(layer => 'encode',
7899                      line => $p->{line}, column => $p->{column} + 1,
7900                      %opt, type => $type);
7901        }; # $char_onerror
7902        $input->onerror ($char_onerror);
7903    
7904      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7905      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7906    

Legend:
Removed from v.1.146  
changed lines
  Added in v.1.178

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24