/[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.153 by wakaba, Fri Aug 15 08:32:41 2008 UTC revision 1.178 by wakaba, Sun Sep 14 11:57:41 2008 UTC
# Line 354  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 365  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 372  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 399  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 432  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 455  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 475  sub parse_byte_stream ($$$$;$) { Line 488  sub parse_byte_stream ($$$$;$) {
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        $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
493      !!!parse-error (type => 'chardecode:fallback',      !!!parse-error (type => 'chardecode:fallback',
494                      text => $self->{input_encoding},                      #text => $self->{input_encoding},
495                      level => $self->{level}->{uncertain},                      level => $self->{level}->{uncertain},
496                      line => 1, column => 1,                      line => 1, column => 1,
497                      layer => 'encode');                      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        $self->{input_encoding} = $charset->get_iana_name;
501      !!!parse-error (type => 'chardecode:no error',      !!!parse-error (type => 'chardecode:no error',
502                      text => $self->{input_encoding},                      text => $self->{input_encoding},
503                      level => $self->{level}->{uncertain},                      level => $self->{level}->{uncertain},
504                      line => 1, column => 1,                      line => 1, column => 1,
505                      layer => 'encode');                      layer => 'encode');
506      } else {
507        $self->{input_encoding} = $charset->get_iana_name;
508    }    }
509    
510    $self->{change_encoding} = sub {    $self->{change_encoding} = sub {
# Line 496  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 507  sub parse_byte_stream ($$$$;$) { Line 523  sub parse_byte_stream ($$$$;$) {
523        ## Step 1            ## Step 1    
524        if ($charset->{category} &        if ($charset->{category} &
525            Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {            Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
526          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');          $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 545  sub parse_byte_stream ($$$$;$) { Line 561  sub parse_byte_stream ($$$$;$) {
561    my $char_onerror = sub {    my $char_onerror = sub {
562      my (undef, $type, %opt) = @_;      my (undef, $type, %opt) = @_;
563      !!!parse-error (layer => 'encode',      !!!parse-error (layer => 'encode',
564                      %opt, type => $type,                      line => $self->{line}, column => $self->{column} + 1,
565                      line => $self->{line}, column => $self->{column} + 1);                      %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          $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
583        !!!parse-error (type => 'chardecode:fallback',        !!!parse-error (type => 'chardecode:fallback',
                       text => $self->{input_encoding},  
584                        level => $self->{level}->{uncertain},                        level => $self->{level}->{uncertain},
585                          #text => $self->{input_encoding},
586                        line => 1, column => 1,                        line => 1, column => 1,
587                        layer => 'encode');                        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          $self->{input_encoding} = $charset->get_iana_name;
591        !!!parse-error (type => 'chardecode:no error',        !!!parse-error (type => 'chardecode:no error',
592                        text => $self->{input_encoding},                        text => $self->{input_encoding},
593                        level => $self->{level}->{uncertain},                        level => $self->{level}->{uncertain},
594                        line => 1, column => 1,                        line => 1, column => 1,
595                        layer => 'encode');                        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 591  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 611  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 621  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 641  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 660  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 684  sub parse_char_stream ($$$;$) { Line 719  sub parse_char_stream ($$$;$) {
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 708  sub new ($) { Line 760  sub new ($) {
760    my $class = shift;    my $class = shift;
761    my $self = bless {    my $self = bless {
762      level => {must => 'm',      level => {must => 'm',
763                  should => 's',
764                warn => 'w',                warn => 'w',
765                info => 'i',                info => 'i',
766                uncertain => 'u'},                uncertain => 'u'},
# Line 740  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 751  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 774  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 827  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 865  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 902  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 966  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 1089  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 1198  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 1540  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 1575  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 1639  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 1669  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 1681  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 1711  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 1727  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 1788  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 1933  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 => ''};  
   
       BC: {  
         if ($self->{next_char} == 0x003E) { # >  
           !!!cp (124);  
           $self->{state} = DATA_STATE;  
           !!!next-input-character;  
   
           !!!emit ($self->{current_token}); # comment  
2015    
2016            redo A;        ## NOTE: Unlike spec's "bogus comment state", this implementation
2017          } elsif ($self->{next_char} == -1) {        ## consumes characters one-by-one basis.
2018            !!!cp (125);        
2019            $self->{state} = DATA_STATE;        if ($self->{next_char} == 0x003E) { # >
2020            ## reconsume          !!!cp (124);
2021            $self->{state} = DATA_STATE;
2022            !!!next-input-character;
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 2178  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 2362  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 2532  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 2568  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 2704  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 2740  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 2800  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;
2942          if ($self->{next_char} == 0x005D) { # ]          !!!next-input-character;
2943            !!!next-input-character;          redo A;
2944            if ($self->{next_char} == 0x005D) { # ]        } elsif ($self->{next_char} == -1) {
2945              !!!next-input-character;          $self->{state} = DATA_STATE;
             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};  
2946          !!!next-input-character;          !!!next-input-character;
2947        } # CS          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 => 'invalid character reference',            $self->{next_char} <= 0x0039) { # 0..9
                           text => (sprintf 'U+%04X', $code),  
                           line => $l, column => $c);  
           $code = 0xFFFD;  
         } elsif ($code > 0x10FFFF) {  
           !!!cp (1009);  
           !!!parse-error (type => 'invalid character reference',  
                           text => (sprintf '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 => 'C1 character reference', text => (sprintf '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 => 'invalid character reference',          !!!parse-error (type => 'invalid character reference',
# Line 2996  sub _tokenize_attempt_to_consume_an_enti Line 3151  sub _tokenize_attempt_to_consume_an_enti
3151                          line => $l, column => $c);                          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 {          } else {
3201            !!!cp (1021);            !!!cp (989);
3202            $value = $EntityChar->{$entity_name};            $self->{current_attribute}->{value} .= '&' . $self->{state_keyword};
3203            $match = -1;            $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 {
3315              !!!cp (1022);
3316              $self->{entity__value} .= chr $self->{next_char};
3317              $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 3091  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 3145  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 4172  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 6292  sub _tree_construction_main ($) { Line 6604  sub _tree_construction_main ($) {
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:#text', 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:#text', 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:#text', token => $token);  
6610            }            }
6611                        
6612            ## Ignore the token.            ## Ignore the token.
# Line 6314  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',  
                           text => $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 6345  sub _tree_construction_main ($) { Line 6642  sub _tree_construction_main ($) {
6642            ## NOTE: As if in head.            ## 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',              !!!parse-error (type => 'in frameset',
6652                              text => $token->{tag_name}, token => $token);                              text => $token->{tag_name}, token => $token);
6653            } else {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6654              !!!cp ('t322');              !!!cp ('t322');
6655              !!!parse-error (type => 'after frameset',              !!!parse-error (type => 'after frameset',
6656                              text => $token->{tag_name}, token => $token);                              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 6361  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:/',  
                           text => $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
# Line 6406  sub _tree_construction_main ($) { Line 6699  sub _tree_construction_main ($) {
6699              !!!cp ('t330');              !!!cp ('t330');
6700              !!!parse-error (type => 'in frameset:/',              !!!parse-error (type => 'in frameset:/',
6701                              text => $token->{tag_name}, token => $token);                              text => $token->{tag_name}, token => $token);
6702            } else {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6703              !!!cp ('t331');              !!!cp ('t330.1');
6704              !!!parse-error (type => 'after frameset:/',              !!!parse-error (type => 'after frameset:/',
6705                              text => $token->{tag_name}, token => $token);                              text => $token->{tag_name}, token => $token);
6706              } else { # "after after html"
6707                !!!cp ('t331');
6708                !!!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 6946  sub _tree_construction_main ($) { Line 7243  sub _tree_construction_main ($) {
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 7130  sub _tree_construction_main ($) { Line 7429  sub _tree_construction_main ($) {
7429            !!!cp ('t413');            !!!cp ('t413');
7430            !!!parse-error (type => 'unmatched end tag',            !!!parse-error (type => 'unmatched end tag',
7431                            text => $token->{tag_name}, token => $token);                            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 ({
# Line 7189  sub _tree_construction_main ($) { Line 7489  sub _tree_construction_main ($) {
7489            !!!cp ('t421');            !!!cp ('t421');
7490            !!!parse-error (type => 'unmatched end tag',            !!!parse-error (type => 'unmatched end tag',
7491                            text => $token->{tag_name}, token => $token);                            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 7235  sub _tree_construction_main ($) { Line 7536  sub _tree_construction_main ($) {
7536            !!!cp ('t425.1');            !!!cp ('t425.1');
7537            !!!parse-error (type => 'unmatched end tag',            !!!parse-error (type => 'unmatched end tag',
7538                            text => $token->{tag_name}, token => $token);                            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 7437  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 7460  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 7477  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 7494  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 7538  sub set_inner_html ($$$) { Line 7861  sub set_inner_html ($$$) {
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 7553  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.153  
changed lines
  Added in v.1.178

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24