/[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.158 by wakaba, Sun Aug 31 12:11:42 2008 UTC revision 1.192 by wakaba, Thu Oct 2 10:59:04 2008 UTC
# Line 3  use strict; Line 3  use strict;
3  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4  use Error qw(:try);  use Error qw(:try);
5    
6    ## NOTE: This module don't check all HTML5 parse errors; character
7    ## encoding related parse errors are expected to be handled by relevant
8    ## modules.
9    ## Parse errors for control characters that are not allowed in HTML5
10    ## documents, for surrogate code points, and for noncharacter code
11    ## points, as well as U+FFFD substitions for characters whose code points
12    ## is higher than U+10FFFF may be detected by combining the parser with
13    ## the checker implemented by Whatpm::Charset::UnicodeChecker (for its
14    ## usage example, see |t/HTML-tree.t| in the Whatpm package or the
15    ## WebHACC::Language::HTML module in the WebHACC package).
16    
17  ## ISSUE:  ## ISSUE:
18  ## var doc = implementation.createDocument (null, null, null);  ## var doc = implementation.createDocument (null, null, null);
19  ## doc.write ('');  ## doc.write ('');
# Line 312  my $foreign_attr_xname = { Line 323  my $foreign_attr_xname = {
323    
324  ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.  ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
325    
326  my $c1_entity_char = {  my $charref_map = {
327      0x0D => 0x000A,
328    0x80 => 0x20AC,    0x80 => 0x20AC,
329    0x81 => 0xFFFD,    0x81 => 0xFFFD,
330    0x82 => 0x201A,    0x82 => 0x201A,
# Line 345  my $c1_entity_char = { Line 357  my $c1_entity_char = {
357    0x9D => 0xFFFD,    0x9D => 0xFFFD,
358    0x9E => 0x017E,    0x9E => 0x017E,
359    0x9F => 0x0178,    0x9F => 0x0178,
360  }; # $c1_entity_char  }; # $charref_map
361    $charref_map->{$_} = 0xFFFD
362        for 0x0000..0x0008, 0x000B, 0x000E..0x001F, 0x007F,
363            0xD800..0xDFFF, 0xFDD0..0xFDDF, ## ISSUE: 0xFDEF
364            0xFFFE, 0xFFFF, 0x1FFFE, 0x1FFFF, 0x2FFFE, 0x2FFFF, 0x3FFFE, 0x3FFFF,
365            0x4FFFE, 0x4FFFF, 0x5FFFE, 0x5FFFF, 0x6FFFE, 0x6FFFF, 0x7FFFE,
366            0x7FFFF, 0x8FFFE, 0x8FFFF, 0x9FFFE, 0x9FFFF, 0xAFFFE, 0xAFFFF,
367            0xBFFFE, 0xBFFFF, 0xCFFFE, 0xCFFFF, 0xDFFFE, 0xDFFFF, 0xEFFFE,
368            0xEFFFF, 0xFFFFE, 0xFFFFF, 0x10FFFE, 0x10FFFF;
369    
370    ## TODO: Invoke the reset algorithm when a resettable element is
371    ## created (cf. HTML5 revision 2259).
372    
373  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
374    my $self = shift;    my $self = shift;
# Line 354  sub parse_byte_string ($$$$;$) { Line 377  sub parse_byte_string ($$$$;$) {
377    return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);    return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
378  } # parse_byte_string  } # parse_byte_string
379    
380  sub parse_byte_stream ($$$$;$) {  sub parse_byte_stream ($$$$;$$) {
381      # my ($self, $charset_name, $byte_stream, $doc, $onerror, $get_wrapper) = @_;
382    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
383    my $charset_name = shift;    my $charset_name = shift;
384    my $byte_stream = $_[0];    my $byte_stream = $_[0];
# Line 365  sub parse_byte_stream ($$$$;$) { Line 389  sub parse_byte_stream ($$$$;$) {
389    };    };
390    $self->{parse_error} = $onerror; # updated later by parse_char_string    $self->{parse_error} = $onerror; # updated later by parse_char_string
391    
392      my $get_wrapper = $_[3] || sub ($) {
393        return $_[0]; # $_[0] = byte stream handle, returned = arg to char handle
394      };
395    
396    ## HTML5 encoding sniffing algorithm    ## HTML5 encoding sniffing algorithm
397    require Message::Charset::Info;    require Message::Charset::Info;
398    my $charset;    my $charset;
# Line 372  sub parse_byte_stream ($$$$;$) { Line 400  sub parse_byte_stream ($$$$;$) {
400    my ($char_stream, $e_status);    my ($char_stream, $e_status);
401    
402    SNIFFING: {    SNIFFING: {
403        ## NOTE: By setting |allow_fallback| option true when the
404        ## |get_decode_handle| method is invoked, we ignore what the HTML5
405        ## spec requires, i.e. unsupported encoding should be ignored.
406          ## TODO: We should not do this unless the parser is invoked
407          ## in the conformance checking mode, in which this behavior
408          ## would be useful.
409    
410      ## Step 1      ## Step 1
411      if (defined $charset_name) {      if (defined $charset_name) {
412        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);        $charset = Message::Charset::Info->get_by_html_name ($charset_name);
413              ## TODO: Is this ok?  Transfer protocol's parameter should be
414              ## interpreted in its semantics?
415    
       ## ISSUE: Unsupported encoding is not ignored according to the spec.  
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);             allow_fallback => 1);
# Line 385  sub parse_byte_stream ($$$$;$) { Line 420  sub parse_byte_stream ($$$$;$) {
420          $self->{confident} = 1;          $self->{confident} = 1;
421          last SNIFFING;          last SNIFFING;
422        } else {        } else {
423          ## TODO: unsupported error          !!!parse-error (type => 'charset:not supported',
424                            layer => 'encode',
425                            line => 1, column => 1,
426                            value => $charset_name,
427                            level => $self->{level}->{uncertain});
428        }        }
429      }      }
430    
# Line 399  sub parse_byte_stream ($$$$;$) { Line 438  sub parse_byte_stream ($$$$;$) {
438    
439      ## Step 3      ## Step 3
440      if ($byte_buffer =~ /^\xFE\xFF/) {      if ($byte_buffer =~ /^\xFE\xFF/) {
441        $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');        $charset = Message::Charset::Info->get_by_html_name ('utf-16be');
442        ($char_stream, $e_status) = $charset->get_decode_handle        ($char_stream, $e_status) = $charset->get_decode_handle
443            ($byte_stream, allow_error_reporting => 1,            ($byte_stream, allow_error_reporting => 1,
444             allow_fallback => 1, byte_buffer => \$byte_buffer);             allow_fallback => 1, byte_buffer => \$byte_buffer);
445        $self->{confident} = 1;        $self->{confident} = 1;
446        last SNIFFING;        last SNIFFING;
447      } elsif ($byte_buffer =~ /^\xFF\xFE/) {      } elsif ($byte_buffer =~ /^\xFF\xFE/) {
448        $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');        $charset = Message::Charset::Info->get_by_html_name ('utf-16le');
449        ($char_stream, $e_status) = $charset->get_decode_handle        ($char_stream, $e_status) = $charset->get_decode_handle
450            ($byte_stream, allow_error_reporting => 1,            ($byte_stream, allow_error_reporting => 1,
451             allow_fallback => 1, byte_buffer => \$byte_buffer);             allow_fallback => 1, byte_buffer => \$byte_buffer);
452        $self->{confident} = 1;        $self->{confident} = 1;
453        last SNIFFING;        last SNIFFING;
454      } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {      } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
455        $charset = Message::Charset::Info->get_by_iana_name ('utf-8');        $charset = Message::Charset::Info->get_by_html_name ('utf-8');
456        ($char_stream, $e_status) = $charset->get_decode_handle        ($char_stream, $e_status) = $charset->get_decode_handle
457            ($byte_stream, allow_error_reporting => 1,            ($byte_stream, allow_error_reporting => 1,
458             allow_fallback => 1, byte_buffer => \$byte_buffer);             allow_fallback => 1, byte_buffer => \$byte_buffer);
# Line 432  sub parse_byte_stream ($$$$;$) { Line 471  sub parse_byte_stream ($$$$;$) {
471      $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string      $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
472          ($byte_buffer);          ($byte_buffer);
473      if (defined $charset_name) {      if (defined $charset_name) {
474        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);        $charset = Message::Charset::Info->get_by_html_name ($charset_name);
475    
476        ## ISSUE: Unsupported encoding is not ignored according to the spec.        ## ISSUE: Unsupported encoding is not ignored according to the spec.
477        require Whatpm::Charset::DecodeHandle;        require Whatpm::Charset::DecodeHandle;
# Line 455  sub parse_byte_stream ($$$$;$) { Line 494  sub parse_byte_stream ($$$$;$) {
494    
495      ## Step 7: default      ## Step 7: default
496      ## TODO: Make this configurable.      ## TODO: Make this configurable.
497      $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');      $charset = Message::Charset::Info->get_by_html_name ('windows-1252');
498          ## NOTE: We choose |windows-1252| here, since |utf-8| should be          ## NOTE: We choose |windows-1252| here, since |utf-8| should be
499          ## detectable in the step 6.          ## detectable in the step 6.
500      require Whatpm::Charset::DecodeHandle;      require Whatpm::Charset::DecodeHandle;
# Line 475  sub parse_byte_stream ($$$$;$) { Line 514  sub parse_byte_stream ($$$$;$) {
514      $self->{confident} = 0;      $self->{confident} = 0;
515    } # SNIFFING    } # SNIFFING
516    
   $self->{input_encoding} = $charset->get_iana_name;  
517    if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {    if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
518        $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
519      !!!parse-error (type => 'chardecode:fallback',      !!!parse-error (type => 'chardecode:fallback',
520                      text => $self->{input_encoding},                      #text => $self->{input_encoding},
521                      level => $self->{level}->{uncertain},                      level => $self->{level}->{uncertain},
522                      line => 1, column => 1,                      line => 1, column => 1,
523                      layer => 'encode');                      layer => 'encode');
524    } elsif (not ($e_status &    } elsif (not ($e_status &
525                  Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {                  Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL ())) {
526        $self->{input_encoding} = $charset->get_iana_name;
527      !!!parse-error (type => 'chardecode:no error',      !!!parse-error (type => 'chardecode:no error',
528                      text => $self->{input_encoding},                      text => $self->{input_encoding},
529                      level => $self->{level}->{uncertain},                      level => $self->{level}->{uncertain},
530                      line => 1, column => 1,                      line => 1, column => 1,
531                      layer => 'encode');                      layer => 'encode');
532      } else {
533        $self->{input_encoding} = $charset->get_iana_name;
534    }    }
535    
536    $self->{change_encoding} = sub {    $self->{change_encoding} = sub {
# Line 496  sub parse_byte_stream ($$$$;$) { Line 538  sub parse_byte_stream ($$$$;$) {
538      $charset_name = shift;      $charset_name = shift;
539      my $token = shift;      my $token = shift;
540    
541      $charset = Message::Charset::Info->get_by_iana_name ($charset_name);      $charset = Message::Charset::Info->get_by_html_name ($charset_name);
542      ($char_stream, $e_status) = $charset->get_decode_handle      ($char_stream, $e_status) = $charset->get_decode_handle
543          ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,          ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
544           byte_buffer => \ $buffer->{buffer});           byte_buffer => \ $buffer->{buffer});
# Line 507  sub parse_byte_stream ($$$$;$) { Line 549  sub parse_byte_stream ($$$$;$) {
549        ## Step 1            ## Step 1    
550        if ($charset->{category} &        if ($charset->{category} &
551            Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {            Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
552          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');          $charset = Message::Charset::Info->get_by_html_name ('utf-8');
553          ($char_stream, $e_status) = $charset->get_decode_handle          ($char_stream, $e_status) = $charset->get_decode_handle
554              ($byte_stream,              ($byte_stream,
555               byte_buffer => \ $buffer->{buffer});               byte_buffer => \ $buffer->{buffer});
# Line 545  sub parse_byte_stream ($$$$;$) { Line 587  sub parse_byte_stream ($$$$;$) {
587    my $char_onerror = sub {    my $char_onerror = sub {
588      my (undef, $type, %opt) = @_;      my (undef, $type, %opt) = @_;
589      !!!parse-error (layer => 'encode',      !!!parse-error (layer => 'encode',
590                      %opt, type => $type,                      line => $self->{line}, column => $self->{column} + 1,
591                      line => $self->{line}, column => $self->{column} + 1);                      %opt, type => $type);
592      if ($opt{octets}) {      if ($opt{octets}) {
593        ${$opt{octets}} = "\x{FFFD}"; # relacement character        ${$opt{octets}} = "\x{FFFD}"; # relacement character
594      }      }
595    };    };
   $char_stream->onerror ($char_onerror);  
596    
597    my @args = @_; shift @args; # $s    my $wrapped_char_stream = $get_wrapper->($char_stream);
598      $wrapped_char_stream->onerror ($char_onerror);
599    
600      my @args = ($_[1], $_[2]); # $doc, $onerror - $get_wrapper = undef;
601    my $return;    my $return;
602    try {    try {
603      $return = $self->parse_char_stream ($char_stream, @args);        $return = $self->parse_char_stream ($wrapped_char_stream, @args);  
604    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
605      ## NOTE: Invoked after {change_encoding}.      ## NOTE: Invoked after {change_encoding}.
606    
     $self->{input_encoding} = $charset->get_iana_name;  
607      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
608          $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
609        !!!parse-error (type => 'chardecode:fallback',        !!!parse-error (type => 'chardecode:fallback',
                       text => $self->{input_encoding},  
610                        level => $self->{level}->{uncertain},                        level => $self->{level}->{uncertain},
611                          #text => $self->{input_encoding},
612                        line => 1, column => 1,                        line => 1, column => 1,
613                        layer => 'encode');                        layer => 'encode');
614      } elsif (not ($e_status &      } elsif (not ($e_status &
615                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL ())) {
616          $self->{input_encoding} = $charset->get_iana_name;
617        !!!parse-error (type => 'chardecode:no error',        !!!parse-error (type => 'chardecode:no error',
618                        text => $self->{input_encoding},                        text => $self->{input_encoding},
619                        level => $self->{level}->{uncertain},                        level => $self->{level}->{uncertain},
620                        line => 1, column => 1,                        line => 1, column => 1,
621                        layer => 'encode');                        layer => 'encode');
622        } else {
623          $self->{input_encoding} = $charset->get_iana_name;
624      }      }
625      $self->{confident} = 1;      $self->{confident} = 1;
626      $char_stream->onerror ($char_onerror);  
627      $return = $self->parse_char_stream ($char_stream, @args);      $wrapped_char_stream = $get_wrapper->($char_stream);
628        $wrapped_char_stream->onerror ($char_onerror);
629    
630        $return = $self->parse_char_stream ($wrapped_char_stream, @args);
631    };    };
632    return $return;    return $return;
633  } # parse_byte_stream  } # parse_byte_stream
# Line 591  sub parse_byte_stream ($$$$;$) { Line 641  sub parse_byte_stream ($$$$;$) {
641  ## 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
642  ## strip the BOM and never strip any ZWNBSP.  ## strip the BOM and never strip any ZWNBSP.
643    
644  sub parse_char_string ($$$;$) {  sub parse_char_string ($$$;$$) {
645      #my ($self, $s, $doc, $onerror, $get_wrapper) = @_;
646    my $self = shift;    my $self = shift;
   require utf8;  
647    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $s = ref $_[0] ? $_[0] : \($_[0]);
648    open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;    require Whatpm::Charset::DecodeHandle;
649      my $input = Whatpm::Charset::DecodeHandle::CharString->new ($s);
650    return $self->parse_char_stream ($input, @_[1..$#_]);    return $self->parse_char_stream ($input, @_[1..$#_]);
651  } # parse_char_string  } # parse_char_string
652  *parse_string = \&parse_char_string;  *parse_string = \&parse_char_string; ## NOTE: Alias for backward compatibility.
653    
654  sub parse_char_stream ($$$;$) {  sub parse_char_stream ($$$;$$) {
655    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
656    my $input = $_[0];    my $input = $_[0];
657    $self->{document} = $_[1];    $self->{document} = $_[1];
# Line 611  sub parse_char_stream ($$$;$) { Line 662  sub parse_char_stream ($$$;$) {
662    $self->{confident} = 1 unless exists $self->{confident};    $self->{confident} = 1 unless exists $self->{confident};
663    $self->{document}->input_encoding ($self->{input_encoding})    $self->{document}->input_encoding ($self->{input_encoding})
664        if defined $self->{input_encoding};        if defined $self->{input_encoding};
665    ## TODO: |{input_encoding}| is needless?
666    
   my $i = 0;  
667    $self->{line_prev} = $self->{line} = 1;    $self->{line_prev} = $self->{line} = 1;
668    $self->{column_prev} = $self->{column} = 0;    $self->{column_prev} = -1;
669    $self->{set_next_char} = sub {    $self->{column} = 0;
670      $self->{set_nc} = sub {
671      my $self = shift;      my $self = shift;
672    
673      pop @{$self->{prev_char}};      my $char = '';
674      unshift @{$self->{prev_char}}, $self->{next_char};      if (defined $self->{next_nc}) {
675          $char = $self->{next_nc};
676      my $char;        delete $self->{next_nc};
677      if (defined $self->{next_next_char}) {        $self->{nc} = ord $char;
       $char = $self->{next_next_char};  
       delete $self->{next_next_char};  
678      } else {      } else {
679        $char = $input->getc;        $self->{char_buffer} = '';
680          $self->{char_buffer_pos} = 0;
681    
682          my $count = $input->manakai_read_until
683             ($self->{char_buffer}, qr/[^\x00\x0A\x0D]/, $self->{char_buffer_pos});
684          if ($count) {
685            $self->{line_prev} = $self->{line};
686            $self->{column_prev} = $self->{column};
687            $self->{column}++;
688            $self->{nc}
689                = ord substr ($self->{char_buffer}, $self->{char_buffer_pos}++, 1);
690            return;
691          }
692    
693          if ($input->read ($char, 1)) {
694            $self->{nc} = ord $char;
695          } else {
696            $self->{nc} = -1;
697            return;
698          }
699      }      }
     $self->{next_char} = -1 and return unless defined $char;  
     $self->{next_char} = ord $char;  
700    
701      ($self->{line_prev}, $self->{column_prev})      ($self->{line_prev}, $self->{column_prev})
702          = ($self->{line}, $self->{column});          = ($self->{line}, $self->{column});
703      $self->{column}++;      $self->{column}++;
704            
705      if ($self->{next_char} == 0x000A) { # LF      if ($self->{nc} == 0x000A) { # LF
706        !!!cp ('j1');        !!!cp ('j1');
707        $self->{line}++;        $self->{line}++;
708        $self->{column} = 0;        $self->{column} = 0;
709      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{nc} == 0x000D) { # CR
710        !!!cp ('j2');        !!!cp ('j2');
711        my $next = $input->getc;  ## TODO: support for abort/streaming
712        if (defined $next and $next ne "\x0A") {        my $next = '';
713          $self->{next_next_char} = $next;        if ($input->read ($next, 1) and $next ne "\x0A") {
714            $self->{next_nc} = $next;
715        }        }
716        $self->{next_char} = 0x000A; # LF # MUST        $self->{nc} = 0x000A; # LF # MUST
717        $self->{line}++;        $self->{line}++;
718        $self->{column} = 0;        $self->{column} = 0;
719      } elsif ($self->{next_char} > 0x10FFFF) {      } elsif ($self->{nc} == 0x0000) { # NULL
       !!!cp ('j3');  
       $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST  
     } elsif ($self->{next_char} == 0x0000) { # NULL  
720        !!!cp ('j4');        !!!cp ('j4');
721        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
722        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{nc} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
723      } elsif ($self->{next_char} <= 0x0008 or      }
724               (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or    };
725               (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or  
726               (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or    $self->{read_until} = sub {
727               (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or      #my ($scalar, $specials_range, $offset) = @_;
728               {      return 0 if defined $self->{next_nc};
729                0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,  
730                0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,      my $pattern = qr/[^$_[1]\x00\x0A\x0D]/;
731                0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,      my $offset = $_[2] || 0;
732                0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,  
733                0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,      if ($self->{char_buffer_pos} < length $self->{char_buffer}) {
734                0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,        pos ($self->{char_buffer}) = $self->{char_buffer_pos};
735                0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,        if ($self->{char_buffer} =~ /\G(?>$pattern)+/) {
736                0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,          substr ($_[0], $offset)
737                0x10FFFE => 1, 0x10FFFF => 1,              = substr ($self->{char_buffer}, $-[0], $+[0] - $-[0]);
738               }->{$self->{next_char}}) {          my $count = $+[0] - $-[0];
739        !!!cp ('j5');          if ($count) {
740        if ($self->{next_char} < 0x10000) {            $self->{column} += $count;
741          !!!parse-error (type => 'control char',            $self->{char_buffer_pos} += $count;
742                          text => (sprintf 'U+%04X', $self->{next_char}));            $self->{line_prev} = $self->{line};
743              $self->{column_prev} = $self->{column} - 1;
744              $self->{nc} = -1;
745            }
746            return $count;
747        } else {        } else {
748          !!!parse-error (type => 'control char',          return 0;
                         text => (sprintf 'U-%08X', $self->{next_char}));  
749        }        }
750        } else {
751          my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
752          if ($count) {
753            $self->{column} += $count;
754            $self->{line_prev} = $self->{line};
755            $self->{column_prev} = $self->{column} - 1;
756            $self->{nc} = -1;
757          }
758          return $count;
759      }      }
760    };    }; # $self->{read_until}
   $self->{prev_char} = [-1, -1, -1];  
   $self->{next_char} = -1;  
761    
762    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
763      my (%opt) = @_;      my (%opt) = @_;
# Line 694  sub parse_char_stream ($$$;$) { Line 769  sub parse_char_stream ($$$;$) {
769      $onerror->(line => $self->{line}, column => $self->{column}, @_);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
770    };    };
771    
772      my $char_onerror = sub {
773        my (undef, $type, %opt) = @_;
774        !!!parse-error (layer => 'encode',
775                        line => $self->{line}, column => $self->{column} + 1,
776                        %opt, type => $type);
777      }; # $char_onerror
778    
779      if ($_[3]) {
780        $input = $_[3]->($input);
781        $input->onerror ($char_onerror);
782      } else {
783        $input->onerror ($char_onerror) unless defined $input->onerror;
784      }
785    
786    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
787    $self->_initialize_tree_constructor;    $self->_initialize_tree_constructor;
788    $self->_construct_tree;    $self->_construct_tree;
# Line 708  sub new ($) { Line 797  sub new ($) {
797    my $class = shift;    my $class = shift;
798    my $self = bless {    my $self = bless {
799      level => {must => 'm',      level => {must => 'm',
800                  should => 's',
801                warn => 'w',                warn => 'w',
802                info => 'i',                info => 'i',
803                uncertain => 'u'},                uncertain => 'u'},
804    }, $class;    }, $class;
805    $self->{set_next_char} = sub {    $self->{set_nc} = sub {
806      $self->{next_char} = -1;      $self->{nc} = -1;
807    };    };
808    $self->{parse_error} = sub {    $self->{parse_error} = sub {
809      #      #
# Line 740  sub RCDATA_CONTENT_MODEL () { CM_ENTITY Line 830  sub RCDATA_CONTENT_MODEL () { CM_ENTITY
830  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
831    
832  sub DATA_STATE () { 0 }  sub DATA_STATE () { 0 }
833  sub ENTITY_DATA_STATE () { 1 }  #sub ENTITY_DATA_STATE () { 1 }
834  sub TAG_OPEN_STATE () { 2 }  sub TAG_OPEN_STATE () { 2 }
835  sub CLOSE_TAG_OPEN_STATE () { 3 }  sub CLOSE_TAG_OPEN_STATE () { 3 }
836  sub TAG_NAME_STATE () { 4 }  sub TAG_NAME_STATE () { 4 }
# Line 751  sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 Line 841  sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8
841  sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }  sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
842  sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }  sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
843  sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }  sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
844  sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }  #sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
845  sub MARKUP_DECLARATION_OPEN_STATE () { 13 }  sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
846  sub COMMENT_START_STATE () { 14 }  sub COMMENT_START_STATE () { 14 }
847  sub COMMENT_START_DASH_STATE () { 15 }  sub COMMENT_START_DASH_STATE () { 15 }
# Line 774  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STAT Line 864  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STAT
864  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
865  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
866  sub SELF_CLOSING_START_TAG_STATE () { 34 }  sub SELF_CLOSING_START_TAG_STATE () { 34 }
867  sub CDATA_BLOCK_STATE () { 35 }  sub CDATA_SECTION_STATE () { 35 }
868    sub MD_HYPHEN_STATE () { 36 } # "markup declaration open state" in the spec
869    sub MD_DOCTYPE_STATE () { 37 } # "markup declaration open state" in the spec
870    sub MD_CDATA_STATE () { 38 } # "markup declaration open state" in the spec
871    sub CDATA_RCDATA_CLOSE_TAG_STATE () { 39 } # "close tag open state" in the spec
872    sub CDATA_SECTION_MSE1_STATE () { 40 } # "CDATA section state" in the spec
873    sub CDATA_SECTION_MSE2_STATE () { 41 } # "CDATA section state" in the spec
874    sub PUBLIC_STATE () { 42 } # "after DOCTYPE name state" in the spec
875    sub SYSTEM_STATE () { 43 } # "after DOCTYPE name state" in the spec
876    ## NOTE: "Entity data state", "entity in attribute value state", and
877    ## "consume a character reference" algorithm are jointly implemented
878    ## using the following six states:
879    sub ENTITY_STATE () { 44 }
880    sub ENTITY_HASH_STATE () { 45 }
881    sub NCR_NUM_STATE () { 46 }
882    sub HEXREF_X_STATE () { 47 }
883    sub HEXREF_HEX_STATE () { 48 }
884    sub ENTITY_NAME_STATE () { 49 }
885    sub PCDATA_STATE () { 50 } # "data state" in the spec
886    
887  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
888  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 827  sub IN_COLUMN_GROUP_IM () { 0b10 } Line 935  sub IN_COLUMN_GROUP_IM () { 0b10 }
935  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
936    my $self = shift;    my $self = shift;
937    $self->{state} = DATA_STATE; # MUST    $self->{state} = DATA_STATE; # MUST
938      #$self->{s_kwd}; # state keyword - initialized when used
939      #$self->{entity__value}; # initialized when used
940      #$self->{entity__match}; # initialized when used
941    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
942    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{ct}; # current token
943    undef $self->{current_attribute};    undef $self->{ca}; # current attribute
944    undef $self->{last_emitted_start_tag_name};    undef $self->{last_stag_name}; # last emitted start tag name
945    undef $self->{last_attribute_value_state};    #$self->{prev_state}; # initialized when used
946    delete $self->{self_closing};    delete $self->{self_closing};
947    $self->{char} = [];    $self->{char_buffer} = '';
948    # $self->{next_char}    $self->{char_buffer_pos} = 0;
949      $self->{nc} = -1; # next input character
950      #$self->{next_nc}
951    !!!next-input-character;    !!!next-input-character;
952    $self->{token} = [];    $self->{token} = [];
953    # $self->{escape}    # $self->{escape}
# Line 845  sub _initialize_tokenizer ($) { Line 958  sub _initialize_tokenizer ($) {
958  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
959  ##   ->{name} (DOCTYPE_TOKEN)  ##   ->{name} (DOCTYPE_TOKEN)
960  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
961  ##   ->{public_identifier} (DOCTYPE_TOKEN)  ##   ->{pubid} (DOCTYPE_TOKEN)
962  ##   ->{system_identifier} (DOCTYPE_TOKEN)  ##   ->{sysid} (DOCTYPE_TOKEN)
963  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
964  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
965  ##        ->{name}  ##        ->{name}
# Line 865  sub _initialize_tokenizer ($) { Line 978  sub _initialize_tokenizer ($) {
978  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
979  ## and removed from the list.  ## and removed from the list.
980    
981  ## NOTE: HTML5 "Writing HTML documents" section, applied to  ## TODO: Polytheistic slash SHOULD NOT be used. (Applied only to atheists.)
982  ## documents and not to user agents and conformance checkers,  ## (This requirement was dropped from HTML5 spec, unfortunately.)
983  ## contains some requirements that are not detected by the  
984  ## parsing algorithm:  my $is_space = {
985  ## - Some requirements on character encoding declarations. ## TODO    0x0009 => 1, # CHARACTER TABULATION (HT)
986  ## - "Elements MUST NOT contain content that their content model disallows."    0x000A => 1, # LINE FEED (LF)
987  ##   ... Some are parse error, some are not (will be reported by c.c.).    #0x000B => 0, # LINE TABULATION (VT)
988  ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO    0x000C => 1, # FORM FEED (FF)
989  ## - Text (in elements, attributes, and comments) SHOULD NOT contain    #0x000D => 1, # CARRIAGE RETURN (CR)
990  ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)    0x0020 => 1, # SPACE (SP)
991    };
 ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot  
 ## be detected by the HTML5 parsing algorithm:  
 ## - Text,  
992    
993  sub _get_next_token ($) {  sub _get_next_token ($) {
994    my $self = shift;    my $self = shift;
995    
996    if ($self->{self_closing}) {    if ($self->{self_closing}) {
997      !!!parse-error (type => 'nestc', token => $self->{current_token});      !!!parse-error (type => 'nestc', token => $self->{ct});
998      ## NOTE: The |self_closing| flag is only set by start tag token.      ## NOTE: The |self_closing| flag is only set by start tag token.
999      ## In addition, when a start tag token is emitted, it is always set to      ## In addition, when a start tag token is emitted, it is always set to
1000      ## |current_token|.      ## |ct|.
1001      delete $self->{self_closing};      delete $self->{self_closing};
1002    }    }
1003    
# Line 897  sub _get_next_token ($) { Line 1007  sub _get_next_token ($) {
1007    }    }
1008    
1009    A: {    A: {
1010      if ($self->{state} == DATA_STATE) {      if ($self->{state} == PCDATA_STATE) {
1011        if ($self->{next_char} == 0x0026) { # &        ## NOTE: Same as |DATA_STATE|, but only for |PCDATA| content model.
1012    
1013          if ($self->{nc} == 0x0026) { # &
1014            !!!cp (0.1);
1015            ## NOTE: In the spec, the tokenizer is switched to the
1016            ## "entity data state".  In this implementation, the tokenizer
1017            ## is switched to the |ENTITY_STATE|, which is an implementation
1018            ## of the "consume a character reference" algorithm.
1019            $self->{entity_add} = -1;
1020            $self->{prev_state} = DATA_STATE;
1021            $self->{state} = ENTITY_STATE;
1022            !!!next-input-character;
1023            redo A;
1024          } elsif ($self->{nc} == 0x003C) { # <
1025            !!!cp (0.2);
1026            $self->{state} = TAG_OPEN_STATE;
1027            !!!next-input-character;
1028            redo A;
1029          } elsif ($self->{nc} == -1) {
1030            !!!cp (0.3);
1031            !!!emit ({type => END_OF_FILE_TOKEN,
1032                      line => $self->{line}, column => $self->{column}});
1033            last A; ## TODO: ok?
1034          } else {
1035            !!!cp (0.4);
1036            #
1037          }
1038    
1039          # Anything else
1040          my $token = {type => CHARACTER_TOKEN,
1041                       data => chr $self->{nc},
1042                       line => $self->{line}, column => $self->{column},
1043                      };
1044          $self->{read_until}->($token->{data}, q[<&], length $token->{data});
1045    
1046          ## Stay in the state.
1047          !!!next-input-character;
1048          !!!emit ($token);
1049          redo A;
1050        } elsif ($self->{state} == DATA_STATE) {
1051          $self->{s_kwd} = '' unless defined $self->{s_kwd};
1052          if ($self->{nc} == 0x0026) { # &
1053            $self->{s_kwd} = '';
1054          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
1055              not $self->{escape}) {              not $self->{escape}) {
1056            !!!cp (1);            !!!cp (1);
1057            $self->{state} = ENTITY_DATA_STATE;            ## NOTE: In the spec, the tokenizer is switched to the
1058              ## "entity data state".  In this implementation, the tokenizer
1059              ## is switched to the |ENTITY_STATE|, which is an implementation
1060              ## of the "consume a character reference" algorithm.
1061              $self->{entity_add} = -1;
1062              $self->{prev_state} = DATA_STATE;
1063              $self->{state} = ENTITY_STATE;
1064            !!!next-input-character;            !!!next-input-character;
1065            redo A;            redo A;
1066          } else {          } else {
1067            !!!cp (2);            !!!cp (2);
1068            #            #
1069          }          }
1070        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{nc} == 0x002D) { # -
1071          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1072            unless ($self->{escape}) {            $self->{s_kwd} .= '-';
1073              if ($self->{prev_char}->[0] == 0x002D and # -            
1074                  $self->{prev_char}->[1] == 0x0021 and # !            if ($self->{s_kwd} eq '<!--') {
1075                  $self->{prev_char}->[2] == 0x003C) { # <              !!!cp (3);
1076                !!!cp (3);              $self->{escape} = 1; # unless $self->{escape};
1077                $self->{escape} = 1;              $self->{s_kwd} = '--';
1078              } else {              #
1079                !!!cp (4);            } elsif ($self->{s_kwd} eq '---') {
1080              }              !!!cp (4);
1081                $self->{s_kwd} = '--';
1082                #
1083            } else {            } else {
1084              !!!cp (5);              !!!cp (5);
1085                #
1086            }            }
1087          }          }
1088                    
1089          #          #
1090        } elsif ($self->{next_char} == 0x003C) { # <        } elsif ($self->{nc} == 0x0021) { # !
1091            if (length $self->{s_kwd}) {
1092              !!!cp (5.1);
1093              $self->{s_kwd} .= '!';
1094              #
1095            } else {
1096              !!!cp (5.2);
1097              #$self->{s_kwd} = '';
1098              #
1099            }
1100            #
1101          } elsif ($self->{nc} == 0x003C) { # <
1102          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
1103              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
1104               not $self->{escape})) {               not $self->{escape})) {
# Line 936  sub _get_next_token ($) { Line 1108  sub _get_next_token ($) {
1108            redo A;            redo A;
1109          } else {          } else {
1110            !!!cp (7);            !!!cp (7);
1111              $self->{s_kwd} = '';
1112            #            #
1113          }          }
1114        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1115          if ($self->{escape} and          if ($self->{escape} and
1116              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
1117            if ($self->{prev_char}->[0] == 0x002D and # -            if ($self->{s_kwd} eq '--') {
               $self->{prev_char}->[1] == 0x002D) { # -  
1118              !!!cp (8);              !!!cp (8);
1119              delete $self->{escape};              delete $self->{escape};
1120            } else {            } else {
# Line 952  sub _get_next_token ($) { Line 1124  sub _get_next_token ($) {
1124            !!!cp (10);            !!!cp (10);
1125          }          }
1126                    
1127            $self->{s_kwd} = '';
1128          #          #
1129        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1130          !!!cp (11);          !!!cp (11);
1131            $self->{s_kwd} = '';
1132          !!!emit ({type => END_OF_FILE_TOKEN,          !!!emit ({type => END_OF_FILE_TOKEN,
1133                    line => $self->{line}, column => $self->{column}});                    line => $self->{line}, column => $self->{column}});
1134          last A; ## TODO: ok?          last A; ## TODO: ok?
1135        } else {        } else {
1136          !!!cp (12);          !!!cp (12);
1137            $self->{s_kwd} = '';
1138            #
1139        }        }
1140    
1141        # Anything else        # Anything else
1142        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
1143                     data => chr $self->{next_char},                     data => chr $self->{nc},
1144                     line => $self->{line}, column => $self->{column},                     line => $self->{line}, column => $self->{column},
1145                    };                    };
1146        ## Stay in the data state        if ($self->{read_until}->($token->{data}, q[-!<>&],
1147        !!!next-input-character;                                  length $token->{data})) {
1148            $self->{s_kwd} = '';
1149        !!!emit ($token);        }
   
       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  
1150    
1151        unless (defined $token) {        ## Stay in the data state.
1152          if ($self->{content_model} == PCDATA_CONTENT_MODEL) {
1153          !!!cp (13);          !!!cp (13);
1154          !!!emit ({type => CHARACTER_TOKEN, data => '&',          $self->{state} = PCDATA_STATE;
                   line => $l, column => $c,  
                  });  
1155        } else {        } else {
1156          !!!cp (14);          !!!cp (14);
1157          !!!emit ($token);          ## Stay in the state.
1158        }        }
1159          !!!next-input-character;
1160          !!!emit ($token);
1161        redo A;        redo A;
1162      } elsif ($self->{state} == TAG_OPEN_STATE) {      } elsif ($self->{state} == TAG_OPEN_STATE) {
1163        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1164          if ($self->{next_char} == 0x002F) { # /          if ($self->{nc} == 0x002F) { # /
1165            !!!cp (15);            !!!cp (15);
1166            !!!next-input-character;            !!!next-input-character;
1167            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
1168            redo A;            redo A;
1169            } elsif ($self->{nc} == 0x0021) { # !
1170              !!!cp (15.1);
1171              $self->{s_kwd} = '<' unless $self->{escape};
1172              #
1173          } else {          } else {
1174            !!!cp (16);            !!!cp (16);
1175            ## reconsume            #
           $self->{state} = DATA_STATE;  
   
           !!!emit ({type => CHARACTER_TOKEN, data => '<',  
                     line => $self->{line_prev},  
                     column => $self->{column_prev},  
                    });  
   
           redo A;  
1176          }          }
1177    
1178            ## reconsume
1179            $self->{state} = DATA_STATE;
1180            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1181                      line => $self->{line_prev},
1182                      column => $self->{column_prev},
1183                     });
1184            redo A;
1185        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
1186          if ($self->{next_char} == 0x0021) { # !          if ($self->{nc} == 0x0021) { # !
1187            !!!cp (17);            !!!cp (17);
1188            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
1189            !!!next-input-character;            !!!next-input-character;
1190            redo A;            redo A;
1191          } elsif ($self->{next_char} == 0x002F) { # /          } elsif ($self->{nc} == 0x002F) { # /
1192            !!!cp (18);            !!!cp (18);
1193            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
1194            !!!next-input-character;            !!!next-input-character;
1195            redo A;            redo A;
1196          } elsif (0x0041 <= $self->{next_char} and          } elsif (0x0041 <= $self->{nc} and
1197                   $self->{next_char} <= 0x005A) { # A..Z                   $self->{nc} <= 0x005A) { # A..Z
1198            !!!cp (19);            !!!cp (19);
1199            $self->{current_token}            $self->{ct}
1200              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
1201                 tag_name => chr ($self->{next_char} + 0x0020),                 tag_name => chr ($self->{nc} + 0x0020),
1202                 line => $self->{line_prev},                 line => $self->{line_prev},
1203                 column => $self->{column_prev}};                 column => $self->{column_prev}};
1204            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1205            !!!next-input-character;            !!!next-input-character;
1206            redo A;            redo A;
1207          } elsif (0x0061 <= $self->{next_char} and          } elsif (0x0061 <= $self->{nc} and
1208                   $self->{next_char} <= 0x007A) { # a..z                   $self->{nc} <= 0x007A) { # a..z
1209            !!!cp (20);            !!!cp (20);
1210            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{ct} = {type => START_TAG_TOKEN,
1211                                      tag_name => chr ($self->{next_char}),                                      tag_name => chr ($self->{nc}),
1212                                      line => $self->{line_prev},                                      line => $self->{line_prev},
1213                                      column => $self->{column_prev}};                                      column => $self->{column_prev}};
1214            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1215            !!!next-input-character;            !!!next-input-character;
1216            redo A;            redo A;
1217          } elsif ($self->{next_char} == 0x003E) { # >          } elsif ($self->{nc} == 0x003E) { # >
1218            !!!cp (21);            !!!cp (21);
1219            !!!parse-error (type => 'empty start tag',            !!!parse-error (type => 'empty start tag',
1220                            line => $self->{line_prev},                            line => $self->{line_prev},
# Line 1058  sub _get_next_token ($) { Line 1228  sub _get_next_token ($) {
1228                     });                     });
1229    
1230            redo A;            redo A;
1231          } elsif ($self->{next_char} == 0x003F) { # ?          } elsif ($self->{nc} == 0x003F) { # ?
1232            !!!cp (22);            !!!cp (22);
1233            !!!parse-error (type => 'pio',            !!!parse-error (type => 'pio',
1234                            line => $self->{line_prev},                            line => $self->{line_prev},
1235                            column => $self->{column_prev});                            column => $self->{column_prev});
1236            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1237            $self->{current_token} = {type => COMMENT_TOKEN, data => '',            $self->{ct} = {type => COMMENT_TOKEN, data => '',
1238                                      line => $self->{line_prev},                                      line => $self->{line_prev},
1239                                      column => $self->{column_prev},                                      column => $self->{column_prev},
1240                                     };                                     };
1241            ## $self->{next_char} is intentionally left as is            ## $self->{nc} is intentionally left as is
1242            redo A;            redo A;
1243          } else {          } else {
1244            !!!cp (23);            !!!cp (23);
# Line 1089  sub _get_next_token ($) { Line 1259  sub _get_next_token ($) {
1259          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1260        }        }
1261      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1262          ## NOTE: The "close tag open state" in the spec is implemented as
1263          ## |CLOSE_TAG_OPEN_STATE| and |CDATA_RCDATA_CLOSE_TAG_STATE|.
1264    
1265        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1266        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1267          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_stag_name}) {
1268              $self->{state} = CDATA_RCDATA_CLOSE_TAG_STATE;
1269            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            $self->{s_kwd} = '';
1270            my @next_char;            ## Reconsume.
1271            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...  
           }  
1272          } else {          } else {
1273            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1274              ## NOTE: See <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>.
1275            !!!cp (28);            !!!cp (28);
           # next-input-character is already done  
1276            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1277              ## Reconsume.
1278            !!!emit ({type => CHARACTER_TOKEN, data => '</',            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1279                      line => $l, column => $c,                      line => $l, column => $c,
1280                     });                     });
1281            redo A;            redo A;
1282          }          }
1283        }        }
1284          
1285        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{nc} and
1286            $self->{next_char} <= 0x005A) { # A..Z            $self->{nc} <= 0x005A) { # A..Z
1287          !!!cp (29);          !!!cp (29);
1288          $self->{current_token}          $self->{ct}
1289              = {type => END_TAG_TOKEN,              = {type => END_TAG_TOKEN,
1290                 tag_name => chr ($self->{next_char} + 0x0020),                 tag_name => chr ($self->{nc} + 0x0020),
1291                 line => $l, column => $c};                 line => $l, column => $c};
1292          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1293          !!!next-input-character;          !!!next-input-character;
1294          redo A;          redo A;
1295        } elsif (0x0061 <= $self->{next_char} and        } elsif (0x0061 <= $self->{nc} and
1296                 $self->{next_char} <= 0x007A) { # a..z                 $self->{nc} <= 0x007A) { # a..z
1297          !!!cp (30);          !!!cp (30);
1298          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{ct} = {type => END_TAG_TOKEN,
1299                                    tag_name => chr ($self->{next_char}),                                    tag_name => chr ($self->{nc}),
1300                                    line => $l, column => $c};                                    line => $l, column => $c};
1301          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1302          !!!next-input-character;          !!!next-input-character;
1303          redo A;          redo A;
1304        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1305          !!!cp (31);          !!!cp (31);
1306          !!!parse-error (type => 'empty end tag',          !!!parse-error (type => 'empty end tag',
1307                          line => $self->{line_prev}, ## "<" in "</>"                          line => $self->{line_prev}, ## "<" in "</>"
# Line 1179  sub _get_next_token ($) { Line 1309  sub _get_next_token ($) {
1309          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1310          !!!next-input-character;          !!!next-input-character;
1311          redo A;          redo A;
1312        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1313          !!!cp (32);          !!!cp (32);
1314          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1315          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
# Line 1194  sub _get_next_token ($) { Line 1324  sub _get_next_token ($) {
1324          !!!cp (33);          !!!cp (33);
1325          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1326          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1327          $self->{current_token} = {type => COMMENT_TOKEN, data => '',          $self->{ct} = {type => COMMENT_TOKEN, data => '',
1328                                    line => $self->{line_prev}, # "<" of "</"                                    line => $self->{line_prev}, # "<" of "</"
1329                                    column => $self->{column_prev} - 1,                                    column => $self->{column_prev} - 1,
1330                                   };                                   };
1331          ## $self->{next_char} is intentionally left as is          ## NOTE: $self->{nc} is intentionally left as is.
1332          redo A;          ## Although the "anything else" case of the spec not explicitly
1333            ## states that the next input character is to be reconsumed,
1334            ## it will be included to the |data| of the comment token
1335            ## generated from the bogus end tag, as defined in the
1336            ## "bogus comment state" entry.
1337            redo A;
1338          }
1339        } elsif ($self->{state} == CDATA_RCDATA_CLOSE_TAG_STATE) {
1340          my $ch = substr $self->{last_stag_name}, length $self->{s_kwd}, 1;
1341          if (length $ch) {
1342            my $CH = $ch;
1343            $ch =~ tr/a-z/A-Z/;
1344            my $nch = chr $self->{nc};
1345            if ($nch eq $ch or $nch eq $CH) {
1346              !!!cp (24);
1347              ## Stay in the state.
1348              $self->{s_kwd} .= $nch;
1349              !!!next-input-character;
1350              redo A;
1351            } else {
1352              !!!cp (25);
1353              $self->{state} = DATA_STATE;
1354              ## Reconsume.
1355              !!!emit ({type => CHARACTER_TOKEN,
1356                        data => '</' . $self->{s_kwd},
1357                        line => $self->{line_prev},
1358                        column => $self->{column_prev} - 1 - length $self->{s_kwd},
1359                       });
1360              redo A;
1361            }
1362          } else { # after "<{tag-name}"
1363            unless ($is_space->{$self->{nc}} or
1364                    {
1365                     0x003E => 1, # >
1366                     0x002F => 1, # /
1367                     -1 => 1, # EOF
1368                    }->{$self->{nc}}) {
1369              !!!cp (26);
1370              ## Reconsume.
1371              $self->{state} = DATA_STATE;
1372              !!!emit ({type => CHARACTER_TOKEN,
1373                        data => '</' . $self->{s_kwd},
1374                        line => $self->{line_prev},
1375                        column => $self->{column_prev} - 1 - length $self->{s_kwd},
1376                       });
1377              redo A;
1378            } else {
1379              !!!cp (27);
1380              $self->{ct}
1381                  = {type => END_TAG_TOKEN,
1382                     tag_name => $self->{last_stag_name},
1383                     line => $self->{line_prev},
1384                     column => $self->{column_prev} - 1 - length $self->{s_kwd}};
1385              $self->{state} = TAG_NAME_STATE;
1386              ## Reconsume.
1387              redo A;
1388            }
1389        }        }
1390      } elsif ($self->{state} == TAG_NAME_STATE) {      } elsif ($self->{state} == TAG_NAME_STATE) {
1391        if ($self->{next_char} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
1392          !!!cp (34);          !!!cp (34);
1393          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1394          !!!next-input-character;          !!!next-input-character;
1395          redo A;          redo A;
1396        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1397          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1398            !!!cp (35);            !!!cp (35);
1399            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1400          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1401            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1402            #if ($self->{current_token}->{attributes}) {            #if ($self->{ct}->{attributes}) {
1403            #  ## NOTE: This should never be reached.            #  ## NOTE: This should never be reached.
1404            #  !!! cp (36);            #  !!! cp (36);
1405            #  !!! parse-error (type => 'end tag attribute');            #  !!! parse-error (type => 'end tag attribute');
# Line 1225  sub _get_next_token ($) { Line 1407  sub _get_next_token ($) {
1407              !!!cp (37);              !!!cp (37);
1408            #}            #}
1409          } else {          } else {
1410            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1411          }          }
1412          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1413          !!!next-input-character;          !!!next-input-character;
1414    
1415          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1416    
1417          redo A;          redo A;
1418        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{nc} and
1419                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1420          !!!cp (38);          !!!cp (38);
1421          $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);          $self->{ct}->{tag_name} .= chr ($self->{nc} + 0x0020);
1422            # start tag or end tag            # start tag or end tag
1423          ## Stay in this state          ## Stay in this state
1424          !!!next-input-character;          !!!next-input-character;
1425          redo A;          redo A;
1426        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1427          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1428          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1429            !!!cp (39);            !!!cp (39);
1430            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1431          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1432            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1433            #if ($self->{current_token}->{attributes}) {            #if ($self->{ct}->{attributes}) {
1434            #  ## NOTE: This state should never be reached.            #  ## NOTE: This state should never be reached.
1435            #  !!! cp (40);            #  !!! cp (40);
1436            #  !!! parse-error (type => 'end tag attribute');            #  !!! parse-error (type => 'end tag attribute');
# Line 1256  sub _get_next_token ($) { Line 1438  sub _get_next_token ($) {
1438              !!!cp (41);              !!!cp (41);
1439            #}            #}
1440          } else {          } else {
1441            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1442          }          }
1443          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1444          # reconsume          # reconsume
1445    
1446          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1447    
1448          redo A;          redo A;
1449        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1450          !!!cp (42);          !!!cp (42);
1451          $self->{state} = SELF_CLOSING_START_TAG_STATE;          $self->{state} = SELF_CLOSING_START_TAG_STATE;
1452          !!!next-input-character;          !!!next-input-character;
1453          redo A;          redo A;
1454        } else {        } else {
1455          !!!cp (44);          !!!cp (44);
1456          $self->{current_token}->{tag_name} .= chr $self->{next_char};          $self->{ct}->{tag_name} .= chr $self->{nc};
1457            # start tag or end tag            # start tag or end tag
1458          ## Stay in the state          ## Stay in the state
1459          !!!next-input-character;          !!!next-input-character;
1460          redo A;          redo A;
1461        }        }
1462      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1463        if ($self->{next_char} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
1464          !!!cp (45);          !!!cp (45);
1465          ## Stay in the state          ## Stay in the state
1466          !!!next-input-character;          !!!next-input-character;
1467          redo A;          redo A;
1468        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1469          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1470            !!!cp (46);            !!!cp (46);
1471            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1472          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1473            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1474            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1475              !!!cp (47);              !!!cp (47);
1476              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1477            } else {            } else {
1478              !!!cp (48);              !!!cp (48);
1479            }            }
1480          } else {          } else {
1481            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1482          }          }
1483          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1484          !!!next-input-character;          !!!next-input-character;
1485    
1486          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1487    
1488          redo A;          redo A;
1489        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{nc} and
1490                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1491          !!!cp (49);          !!!cp (49);
1492          $self->{current_attribute}          $self->{ca}
1493              = {name => chr ($self->{next_char} + 0x0020),              = {name => chr ($self->{nc} + 0x0020),
1494                 value => '',                 value => '',
1495                 line => $self->{line}, column => $self->{column}};                 line => $self->{line}, column => $self->{column}};
1496          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1497          !!!next-input-character;          !!!next-input-character;
1498          redo A;          redo A;
1499        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1500          !!!cp (50);          !!!cp (50);
1501          $self->{state} = SELF_CLOSING_START_TAG_STATE;          $self->{state} = SELF_CLOSING_START_TAG_STATE;
1502          !!!next-input-character;          !!!next-input-character;
1503          redo A;          redo A;
1504        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1505          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1506          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1507            !!!cp (52);            !!!cp (52);
1508            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1509          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1510            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1511            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1512              !!!cp (53);              !!!cp (53);
1513              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1514            } else {            } else {
1515              !!!cp (54);              !!!cp (54);
1516            }            }
1517          } else {          } else {
1518            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1519          }          }
1520          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1521          # reconsume          # reconsume
1522    
1523          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1524    
1525          redo A;          redo A;
1526        } else {        } else {
# Line 1350  sub _get_next_token ($) { Line 1528  sub _get_next_token ($) {
1528               0x0022 => 1, # "               0x0022 => 1, # "
1529               0x0027 => 1, # '               0x0027 => 1, # '
1530               0x003D => 1, # =               0x003D => 1, # =
1531              }->{$self->{next_char}}) {              }->{$self->{nc}}) {
1532            !!!cp (55);            !!!cp (55);
1533            !!!parse-error (type => 'bad attribute name');            !!!parse-error (type => 'bad attribute name');
1534          } else {          } else {
1535            !!!cp (56);            !!!cp (56);
1536          }          }
1537          $self->{current_attribute}          $self->{ca}
1538              = {name => chr ($self->{next_char}),              = {name => chr ($self->{nc}),
1539                 value => '',                 value => '',
1540                 line => $self->{line}, column => $self->{column}};                 line => $self->{line}, column => $self->{column}};
1541          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
# Line 1366  sub _get_next_token ($) { Line 1544  sub _get_next_token ($) {
1544        }        }
1545      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
1546        my $before_leave = sub {        my $before_leave = sub {
1547          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{ct}->{attributes} # start tag or end tag
1548              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{ca}->{name}}) { # MUST
1549            !!!cp (57);            !!!cp (57);
1550            !!!parse-error (type => 'duplicate attribute', text => $self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});            !!!parse-error (type => 'duplicate attribute', text => $self->{ca}->{name}, line => $self->{ca}->{line}, column => $self->{ca}->{column});
1551            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{ca} # MUST
1552          } else {          } else {
1553            !!!cp (58);            !!!cp (58);
1554            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{ct}->{attributes}->{$self->{ca}->{name}}
1555              = $self->{current_attribute};              = $self->{ca};
1556          }          }
1557        }; # $before_leave        }; # $before_leave
1558    
1559        if ($self->{next_char} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
1560          !!!cp (59);          !!!cp (59);
1561          $before_leave->();          $before_leave->();
1562          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1563          !!!next-input-character;          !!!next-input-character;
1564          redo A;          redo A;
1565        } elsif ($self->{next_char} == 0x003D) { # =        } elsif ($self->{nc} == 0x003D) { # =
1566          !!!cp (60);          !!!cp (60);
1567          $before_leave->();          $before_leave->();
1568          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1569          !!!next-input-character;          !!!next-input-character;
1570          redo A;          redo A;
1571        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1572          $before_leave->();          $before_leave->();
1573          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1574            !!!cp (61);            !!!cp (61);
1575            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1576          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1577            !!!cp (62);            !!!cp (62);
1578            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1579            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1580              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1581            }            }
1582          } else {          } else {
1583            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1584          }          }
1585          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1586          !!!next-input-character;          !!!next-input-character;
1587    
1588          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1589    
1590          redo A;          redo A;
1591        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{nc} and
1592                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1593          !!!cp (63);          !!!cp (63);
1594          $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);          $self->{ca}->{name} .= chr ($self->{nc} + 0x0020);
1595          ## Stay in the state          ## Stay in the state
1596          !!!next-input-character;          !!!next-input-character;
1597          redo A;          redo A;
1598        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1599          !!!cp (64);          !!!cp (64);
1600          $before_leave->();          $before_leave->();
1601          $self->{state} = SELF_CLOSING_START_TAG_STATE;          $self->{state} = SELF_CLOSING_START_TAG_STATE;
1602          !!!next-input-character;          !!!next-input-character;
1603          redo A;          redo A;
1604        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1605          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1606          $before_leave->();          $before_leave->();
1607          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1608            !!!cp (66);            !!!cp (66);
1609            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1610          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1611            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1612            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1613              !!!cp (67);              !!!cp (67);
1614              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1615            } else {            } else {
# Line 1443  sub _get_next_token ($) { Line 1617  sub _get_next_token ($) {
1617              !!!cp (68);              !!!cp (68);
1618            }            }
1619          } else {          } else {
1620            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1621          }          }
1622          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1623          # reconsume          # reconsume
1624    
1625          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1626    
1627          redo A;          redo A;
1628        } else {        } else {
1629          if ($self->{next_char} == 0x0022 or # "          if ($self->{nc} == 0x0022 or # "
1630              $self->{next_char} == 0x0027) { # '              $self->{nc} == 0x0027) { # '
1631            !!!cp (69);            !!!cp (69);
1632            !!!parse-error (type => 'bad attribute name');            !!!parse-error (type => 'bad attribute name');
1633          } else {          } else {
1634            !!!cp (70);            !!!cp (70);
1635          }          }
1636          $self->{current_attribute}->{name} .= chr ($self->{next_char});          $self->{ca}->{name} .= chr ($self->{nc});
1637          ## Stay in the state          ## Stay in the state
1638          !!!next-input-character;          !!!next-input-character;
1639          redo A;          redo A;
1640        }        }
1641      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1642        if ($self->{next_char} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
1643          !!!cp (71);          !!!cp (71);
1644          ## Stay in the state          ## Stay in the state
1645          !!!next-input-character;          !!!next-input-character;
1646          redo A;          redo A;
1647        } elsif ($self->{next_char} == 0x003D) { # =        } elsif ($self->{nc} == 0x003D) { # =
1648          !!!cp (72);          !!!cp (72);
1649          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1650          !!!next-input-character;          !!!next-input-character;
1651          redo A;          redo A;
1652        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1653          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1654            !!!cp (73);            !!!cp (73);
1655            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1656          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1657            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1658            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1659              !!!cp (74);              !!!cp (74);
1660              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1661            } else {            } else {
# Line 1493  sub _get_next_token ($) { Line 1663  sub _get_next_token ($) {
1663              !!!cp (75);              !!!cp (75);
1664            }            }
1665          } else {          } else {
1666            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1667          }          }
1668          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1669          !!!next-input-character;          !!!next-input-character;
1670    
1671          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1672    
1673          redo A;          redo A;
1674        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{nc} and
1675                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1676          !!!cp (76);          !!!cp (76);
1677          $self->{current_attribute}          $self->{ca}
1678              = {name => chr ($self->{next_char} + 0x0020),              = {name => chr ($self->{nc} + 0x0020),
1679                 value => '',                 value => '',
1680                 line => $self->{line}, column => $self->{column}};                 line => $self->{line}, column => $self->{column}};
1681          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1682          !!!next-input-character;          !!!next-input-character;
1683          redo A;          redo A;
1684        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1685          !!!cp (77);          !!!cp (77);
1686          $self->{state} = SELF_CLOSING_START_TAG_STATE;          $self->{state} = SELF_CLOSING_START_TAG_STATE;
1687          !!!next-input-character;          !!!next-input-character;
1688          redo A;          redo A;
1689        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1690          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1691          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1692            !!!cp (79);            !!!cp (79);
1693            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1694          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1695            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1696            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1697              !!!cp (80);              !!!cp (80);
1698              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1699            } else {            } else {
# Line 1531  sub _get_next_token ($) { Line 1701  sub _get_next_token ($) {
1701              !!!cp (81);              !!!cp (81);
1702            }            }
1703          } else {          } else {
1704            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1705          }          }
1706          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1707          # reconsume          # reconsume
1708    
1709          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1710    
1711          redo A;          redo A;
1712        } else {        } else {
1713          if ($self->{next_char} == 0x0022 or # "          if ($self->{nc} == 0x0022 or # "
1714              $self->{next_char} == 0x0027) { # '              $self->{nc} == 0x0027) { # '
1715            !!!cp (78);            !!!cp (78);
1716            !!!parse-error (type => 'bad attribute name');            !!!parse-error (type => 'bad attribute name');
1717          } else {          } else {
1718            !!!cp (82);            !!!cp (82);
1719          }          }
1720          $self->{current_attribute}          $self->{ca}
1721              = {name => chr ($self->{next_char}),              = {name => chr ($self->{nc}),
1722                 value => '',                 value => '',
1723                 line => $self->{line}, column => $self->{column}};                 line => $self->{line}, column => $self->{column}};
1724          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
# Line 1556  sub _get_next_token ($) { Line 1726  sub _get_next_token ($) {
1726          redo A;                  redo A;        
1727        }        }
1728      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1729        if ($self->{next_char} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP        
1730          !!!cp (83);          !!!cp (83);
1731          ## Stay in the state          ## Stay in the state
1732          !!!next-input-character;          !!!next-input-character;
1733          redo A;          redo A;
1734        } elsif ($self->{next_char} == 0x0022) { # "        } elsif ($self->{nc} == 0x0022) { # "
1735          !!!cp (84);          !!!cp (84);
1736          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1737          !!!next-input-character;          !!!next-input-character;
1738          redo A;          redo A;
1739        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{nc} == 0x0026) { # &
1740          !!!cp (85);          !!!cp (85);
1741          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1742          ## reconsume          ## reconsume
1743          redo A;          redo A;
1744        } elsif ($self->{next_char} == 0x0027) { # '        } elsif ($self->{nc} == 0x0027) { # '
1745          !!!cp (86);          !!!cp (86);
1746          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1747          !!!next-input-character;          !!!next-input-character;
1748          redo A;          redo A;
1749        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1750          !!!parse-error (type => 'empty unquoted attribute value');          !!!parse-error (type => 'empty unquoted attribute value');
1751          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1752            !!!cp (87);            !!!cp (87);
1753            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1754          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1755            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1756            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1757              !!!cp (88);              !!!cp (88);
1758              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1759            } else {            } else {
# Line 1595  sub _get_next_token ($) { Line 1761  sub _get_next_token ($) {
1761              !!!cp (89);              !!!cp (89);
1762            }            }
1763          } else {          } else {
1764            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1765          }          }
1766          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1767          !!!next-input-character;          !!!next-input-character;
1768    
1769          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1770    
1771          redo A;          redo A;
1772        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1773          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1774          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1775            !!!cp (90);            !!!cp (90);
1776            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1777          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1778            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1779            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1780              !!!cp (91);              !!!cp (91);
1781              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1782            } else {            } else {
# Line 1618  sub _get_next_token ($) { Line 1784  sub _get_next_token ($) {
1784              !!!cp (92);              !!!cp (92);
1785            }            }
1786          } else {          } else {
1787            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1788          }          }
1789          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1790          ## reconsume          ## reconsume
1791    
1792          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1793    
1794          redo A;          redo A;
1795        } else {        } else {
1796          if ($self->{next_char} == 0x003D) { # =          if ($self->{nc} == 0x003D) { # =
1797            !!!cp (93);            !!!cp (93);
1798            !!!parse-error (type => 'bad attribute value');            !!!parse-error (type => 'bad attribute value');
1799          } else {          } else {
1800            !!!cp (94);            !!!cp (94);
1801          }          }
1802          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{ca}->{value} .= chr ($self->{nc});
1803          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1804          !!!next-input-character;          !!!next-input-character;
1805          redo A;          redo A;
1806        }        }
1807      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1808        if ($self->{next_char} == 0x0022) { # "        if ($self->{nc} == 0x0022) { # "
1809          !!!cp (95);          !!!cp (95);
1810          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1811          !!!next-input-character;          !!!next-input-character;
1812          redo A;          redo A;
1813        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{nc} == 0x0026) { # &
1814          !!!cp (96);          !!!cp (96);
1815          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1816          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1817            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1818            ## implementation of the "consume a character reference" algorithm.
1819            $self->{prev_state} = $self->{state};
1820            $self->{entity_add} = 0x0022; # "
1821            $self->{state} = ENTITY_STATE;
1822          !!!next-input-character;          !!!next-input-character;
1823          redo A;          redo A;
1824        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1825          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1826          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1827            !!!cp (97);            !!!cp (97);
1828            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1829          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1830            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1831            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1832              !!!cp (98);              !!!cp (98);
1833              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1834            } else {            } else {
# Line 1665  sub _get_next_token ($) { Line 1836  sub _get_next_token ($) {
1836              !!!cp (99);              !!!cp (99);
1837            }            }
1838          } else {          } else {
1839            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1840          }          }
1841          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1842          ## reconsume          ## reconsume
1843    
1844          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1845    
1846          redo A;          redo A;
1847        } else {        } else {
1848          !!!cp (100);          !!!cp (100);
1849          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{ca}->{value} .= chr ($self->{nc});
1850            $self->{read_until}->($self->{ca}->{value},
1851                                  q["&],
1852                                  length $self->{ca}->{value});
1853    
1854          ## Stay in the state          ## Stay in the state
1855          !!!next-input-character;          !!!next-input-character;
1856          redo A;          redo A;
1857        }        }
1858      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1859        if ($self->{next_char} == 0x0027) { # '        if ($self->{nc} == 0x0027) { # '
1860          !!!cp (101);          !!!cp (101);
1861          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1862          !!!next-input-character;          !!!next-input-character;
1863          redo A;          redo A;
1864        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{nc} == 0x0026) { # &
1865          !!!cp (102);          !!!cp (102);
1866          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1867          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1868            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1869            ## implementation of the "consume a character reference" algorithm.
1870            $self->{entity_add} = 0x0027; # '
1871            $self->{prev_state} = $self->{state};
1872            $self->{state} = ENTITY_STATE;
1873          !!!next-input-character;          !!!next-input-character;
1874          redo A;          redo A;
1875        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1876          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1877          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1878            !!!cp (103);            !!!cp (103);
1879            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1880          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1881            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1882            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1883              !!!cp (104);              !!!cp (104);
1884              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1885            } else {            } else {
# Line 1707  sub _get_next_token ($) { Line 1887  sub _get_next_token ($) {
1887              !!!cp (105);              !!!cp (105);
1888            }            }
1889          } else {          } else {
1890            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1891          }          }
1892          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1893          ## reconsume          ## reconsume
1894    
1895          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1896    
1897          redo A;          redo A;
1898        } else {        } else {
1899          !!!cp (106);          !!!cp (106);
1900          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{ca}->{value} .= chr ($self->{nc});
1901            $self->{read_until}->($self->{ca}->{value},
1902                                  q['&],
1903                                  length $self->{ca}->{value});
1904    
1905          ## Stay in the state          ## Stay in the state
1906          !!!next-input-character;          !!!next-input-character;
1907          redo A;          redo A;
1908        }        }
1909      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1910        if ($self->{next_char} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # HT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
1911          !!!cp (107);          !!!cp (107);
1912          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1913          !!!next-input-character;          !!!next-input-character;
1914          redo A;          redo A;
1915        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{nc} == 0x0026) { # &
1916          !!!cp (108);          !!!cp (108);
1917          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1918          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1919            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1920            ## implementation of the "consume a character reference" algorithm.
1921            $self->{entity_add} = -1;
1922            $self->{prev_state} = $self->{state};
1923            $self->{state} = ENTITY_STATE;
1924          !!!next-input-character;          !!!next-input-character;
1925          redo A;          redo A;
1926        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1927          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1928            !!!cp (109);            !!!cp (109);
1929            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1930          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1931            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1932            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1933              !!!cp (110);              !!!cp (110);
1934              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1935            } else {            } else {
# Line 1752  sub _get_next_token ($) { Line 1937  sub _get_next_token ($) {
1937              !!!cp (111);              !!!cp (111);
1938            }            }
1939          } else {          } else {
1940            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1941          }          }
1942          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1943          !!!next-input-character;          !!!next-input-character;
1944    
1945          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1946    
1947          redo A;          redo A;
1948        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1949          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1950          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1951            !!!cp (112);            !!!cp (112);
1952            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1953          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1954            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1955            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1956              !!!cp (113);              !!!cp (113);
1957              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1958            } else {            } else {
# Line 1775  sub _get_next_token ($) { Line 1960  sub _get_next_token ($) {
1960              !!!cp (114);              !!!cp (114);
1961            }            }
1962          } else {          } else {
1963            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1964          }          }
1965          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1966          ## reconsume          ## reconsume
1967    
1968          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1969    
1970          redo A;          redo A;
1971        } else {        } else {
# Line 1788  sub _get_next_token ($) { Line 1973  sub _get_next_token ($) {
1973               0x0022 => 1, # "               0x0022 => 1, # "
1974               0x0027 => 1, # '               0x0027 => 1, # '
1975               0x003D => 1, # =               0x003D => 1, # =
1976              }->{$self->{next_char}}) {              }->{$self->{nc}}) {
1977            !!!cp (115);            !!!cp (115);
1978            !!!parse-error (type => 'bad attribute value');            !!!parse-error (type => 'bad attribute value');
1979          } else {          } else {
1980            !!!cp (116);            !!!cp (116);
1981          }          }
1982          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{ca}->{value} .= chr ($self->{nc});
1983            $self->{read_until}->($self->{ca}->{value},
1984                                  q["'=& >],
1985                                  length $self->{ca}->{value});
1986    
1987          ## Stay in the state          ## Stay in the state
1988          !!!next-input-character;          !!!next-input-character;
1989          redo A;          redo A;
1990        }        }
     } 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;  
1991      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1992        if ($self->{next_char} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
1993          !!!cp (118);          !!!cp (118);
1994          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1995          !!!next-input-character;          !!!next-input-character;
1996          redo A;          redo A;
1997        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1998          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1999            !!!cp (119);            !!!cp (119);
2000            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
2001          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
2002            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
2003            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
2004              !!!cp (120);              !!!cp (120);
2005              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
2006            } else {            } else {
# Line 1845  sub _get_next_token ($) { Line 2008  sub _get_next_token ($) {
2008              !!!cp (121);              !!!cp (121);
2009            }            }
2010          } else {          } else {
2011            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
2012          }          }
2013          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2014          !!!next-input-character;          !!!next-input-character;
2015    
2016          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
2017    
2018          redo A;          redo A;
2019        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
2020          !!!cp (122);          !!!cp (122);
2021          $self->{state} = SELF_CLOSING_START_TAG_STATE;          $self->{state} = SELF_CLOSING_START_TAG_STATE;
2022          !!!next-input-character;          !!!next-input-character;
2023          redo A;          redo A;
2024        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2025          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
2026          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
2027            !!!cp (122.3);            !!!cp (122.3);
2028            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
2029          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
2030            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
2031              !!!cp (122.1);              !!!cp (122.1);
2032              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
2033            } else {            } else {
# Line 1872  sub _get_next_token ($) { Line 2035  sub _get_next_token ($) {
2035              !!!cp (122.2);              !!!cp (122.2);
2036            }            }
2037          } else {          } else {
2038            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
2039          }          }
2040          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2041          ## Reconsume.          ## Reconsume.
2042          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
2043          redo A;          redo A;
2044        } else {        } else {
2045          !!!cp ('124.1');          !!!cp ('124.1');
# Line 1886  sub _get_next_token ($) { Line 2049  sub _get_next_token ($) {
2049          redo A;          redo A;
2050        }        }
2051      } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {      } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
2052        if ($self->{next_char} == 0x003E) { # >        if ($self->{nc} == 0x003E) { # >
2053          if ($self->{current_token}->{type} == END_TAG_TOKEN) {          if ($self->{ct}->{type} == END_TAG_TOKEN) {
2054            !!!cp ('124.2');            !!!cp ('124.2');
2055            !!!parse-error (type => 'nestc', token => $self->{current_token});            !!!parse-error (type => 'nestc', token => $self->{ct});
2056            ## TODO: Different type than slash in start tag            ## TODO: Different type than slash in start tag
2057            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
2058            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
2059              !!!cp ('124.4');              !!!cp ('124.4');
2060              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
2061            } else {            } else {
# Line 1907  sub _get_next_token ($) { Line 2070  sub _get_next_token ($) {
2070          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2071          !!!next-input-character;          !!!next-input-character;
2072    
2073          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
2074    
2075          redo A;          redo A;
2076        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2077          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
2078          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
2079            !!!cp (124.7);            !!!cp (124.7);
2080            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
2081          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
2082            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
2083              !!!cp (124.5);              !!!cp (124.5);
2084              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
2085            } else {            } else {
# Line 1924  sub _get_next_token ($) { Line 2087  sub _get_next_token ($) {
2087              !!!cp (124.6);              !!!cp (124.6);
2088            }            }
2089          } else {          } else {
2090            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
2091          }          }
2092          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2093          ## Reconsume.          ## Reconsume.
2094          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
2095          redo A;          redo A;
2096        } else {        } else {
2097          !!!cp ('124.4');          !!!cp ('124.4');
# Line 1940  sub _get_next_token ($) { Line 2103  sub _get_next_token ($) {
2103        }        }
2104      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
2105        ## (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  
2106    
2107            redo A;        ## NOTE: Unlike spec's "bogus comment state", this implementation
2108          } elsif ($self->{next_char} == -1) {        ## consumes characters one-by-one basis.
2109            !!!cp (125);        
2110            $self->{state} = DATA_STATE;        if ($self->{nc} == 0x003E) { # >
2111            ## reconsume          !!!cp (124);
2112            $self->{state} = DATA_STATE;
2113            !!!next-input-character;
2114    
2115            !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2116            redo A;
2117          } elsif ($self->{nc} == -1) {
2118            !!!cp (125);
2119            $self->{state} = DATA_STATE;
2120            ## reconsume
2121    
2122            redo A;          !!!emit ($self->{ct}); # comment
2123          } else {          redo A;
2124            !!!cp (126);        } else {
2125            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment          !!!cp (126);
2126            !!!next-input-character;          $self->{ct}->{data} .= chr ($self->{nc}); # comment
2127            redo BC;          $self->{read_until}->($self->{ct}->{data},
2128          }                                q[>],
2129        } # BC                                length $self->{ct}->{data});
2130    
2131        die "$0: _get_next_token: unexpected case [BC]";          ## Stay in the state.
2132            !!!next-input-character;
2133            redo A;
2134          }
2135      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2136        ## (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};  
2137                
2138        if ($self->{next_char} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2139            !!!cp (133);
2140            $self->{state} = MD_HYPHEN_STATE;
2141          !!!next-input-character;          !!!next-input-character;
2142          push @next_char, $self->{next_char};          redo A;
2143          if ($self->{next_char} == 0x002D) { # -        } elsif ($self->{nc} == 0x0044 or # D
2144            !!!cp (127);                 $self->{nc} == 0x0064) { # d
2145            $self->{current_token} = {type => COMMENT_TOKEN, data => '',          ## ASCII case-insensitive.
2146                                      line => $l, column => $c,          !!!cp (130);
2147                                     };          $self->{state} = MD_DOCTYPE_STATE;
2148            $self->{state} = COMMENT_START_STATE;          $self->{s_kwd} = chr $self->{nc};
           !!!next-input-character;  
           redo A;  
         } else {  
           !!!cp (128);  
         }  
       } elsif ($self->{next_char} == 0x0044 or # D  
                $self->{next_char} == 0x0064) { # d  
2149          !!!next-input-character;          !!!next-input-character;
2150          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);  
         }  
2151        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2152                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2153                 $self->{next_char} == 0x005B) { # [                 $self->{nc} == 0x005B) { # [
2154            !!!cp (135.4);                
2155            $self->{state} = MD_CDATA_STATE;
2156            $self->{s_kwd} = '[';
2157          !!!next-input-character;          !!!next-input-character;
2158          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);  
         }  
2159        } else {        } else {
2160          !!!cp (136);          !!!cp (136);
2161        }        }
2162    
2163        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment',
2164        $self->{next_char} = shift @next_char;                        line => $self->{line_prev},
2165        !!!back-next-input-character (@next_char);                        column => $self->{column_prev} - 1);
2166          ## Reconsume.
2167        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2168        $self->{current_token} = {type => COMMENT_TOKEN, data => '',        $self->{ct} = {type => COMMENT_TOKEN, data => '',
2169                                  line => $l, column => $c,                                  line => $self->{line_prev},
2170                                    column => $self->{column_prev} - 1,
2171                                 };                                 };
2172        redo A;        redo A;
2173              } elsif ($self->{state} == MD_HYPHEN_STATE) {
2174        ## ISSUE: typos in spec: chacacters, is is a parse error        if ($self->{nc} == 0x002D) { # -
2175        ## 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);
2176            $self->{ct} = {type => COMMENT_TOKEN, data => '',
2177                                      line => $self->{line_prev},
2178                                      column => $self->{column_prev} - 2,
2179                                     };
2180            $self->{state} = COMMENT_START_STATE;
2181            !!!next-input-character;
2182            redo A;
2183          } else {
2184            !!!cp (128);
2185            !!!parse-error (type => 'bogus comment',
2186                            line => $self->{line_prev},
2187                            column => $self->{column_prev} - 2);
2188            $self->{state} = BOGUS_COMMENT_STATE;
2189            ## Reconsume.
2190            $self->{ct} = {type => COMMENT_TOKEN,
2191                                      data => '-',
2192                                      line => $self->{line_prev},
2193                                      column => $self->{column_prev} - 2,
2194                                     };
2195            redo A;
2196          }
2197        } elsif ($self->{state} == MD_DOCTYPE_STATE) {
2198          ## ASCII case-insensitive.
2199          if ($self->{nc} == [
2200                undef,
2201                0x004F, # O
2202                0x0043, # C
2203                0x0054, # T
2204                0x0059, # Y
2205                0x0050, # P
2206              ]->[length $self->{s_kwd}] or
2207              $self->{nc} == [
2208                undef,
2209                0x006F, # o
2210                0x0063, # c
2211                0x0074, # t
2212                0x0079, # y
2213                0x0070, # p
2214              ]->[length $self->{s_kwd}]) {
2215            !!!cp (131);
2216            ## Stay in the state.
2217            $self->{s_kwd} .= chr $self->{nc};
2218            !!!next-input-character;
2219            redo A;
2220          } elsif ((length $self->{s_kwd}) == 6 and
2221                   ($self->{nc} == 0x0045 or # E
2222                    $self->{nc} == 0x0065)) { # e
2223            !!!cp (129);
2224            $self->{state} = DOCTYPE_STATE;
2225            $self->{ct} = {type => DOCTYPE_TOKEN,
2226                                      quirks => 1,
2227                                      line => $self->{line_prev},
2228                                      column => $self->{column_prev} - 7,
2229                                     };
2230            !!!next-input-character;
2231            redo A;
2232          } else {
2233            !!!cp (132);        
2234            !!!parse-error (type => 'bogus comment',
2235                            line => $self->{line_prev},
2236                            column => $self->{column_prev} - 1 - length $self->{s_kwd});
2237            $self->{state} = BOGUS_COMMENT_STATE;
2238            ## Reconsume.
2239            $self->{ct} = {type => COMMENT_TOKEN,
2240                                      data => $self->{s_kwd},
2241                                      line => $self->{line_prev},
2242                                      column => $self->{column_prev} - 1 - length $self->{s_kwd},
2243                                     };
2244            redo A;
2245          }
2246        } elsif ($self->{state} == MD_CDATA_STATE) {
2247          if ($self->{nc} == {
2248                '[' => 0x0043, # C
2249                '[C' => 0x0044, # D
2250                '[CD' => 0x0041, # A
2251                '[CDA' => 0x0054, # T
2252                '[CDAT' => 0x0041, # A
2253              }->{$self->{s_kwd}}) {
2254            !!!cp (135.1);
2255            ## Stay in the state.
2256            $self->{s_kwd} .= chr $self->{nc};
2257            !!!next-input-character;
2258            redo A;
2259          } elsif ($self->{s_kwd} eq '[CDATA' and
2260                   $self->{nc} == 0x005B) { # [
2261            !!!cp (135.2);
2262            $self->{ct} = {type => CHARACTER_TOKEN,
2263                                      data => '',
2264                                      line => $self->{line_prev},
2265                                      column => $self->{column_prev} - 7};
2266            $self->{state} = CDATA_SECTION_STATE;
2267            !!!next-input-character;
2268            redo A;
2269          } else {
2270            !!!cp (135.3);
2271            !!!parse-error (type => 'bogus comment',
2272                            line => $self->{line_prev},
2273                            column => $self->{column_prev} - 1 - length $self->{s_kwd});
2274            $self->{state} = BOGUS_COMMENT_STATE;
2275            ## Reconsume.
2276            $self->{ct} = {type => COMMENT_TOKEN,
2277                                      data => $self->{s_kwd},
2278                                      line => $self->{line_prev},
2279                                      column => $self->{column_prev} - 1 - length $self->{s_kwd},
2280                                     };
2281            redo A;
2282          }
2283      } elsif ($self->{state} == COMMENT_START_STATE) {      } elsif ($self->{state} == COMMENT_START_STATE) {
2284        if ($self->{next_char} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2285          !!!cp (137);          !!!cp (137);
2286          $self->{state} = COMMENT_START_DASH_STATE;          $self->{state} = COMMENT_START_DASH_STATE;
2287          !!!next-input-character;          !!!next-input-character;
2288          redo A;          redo A;
2289        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2290          !!!cp (138);          !!!cp (138);
2291          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2292          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2293          !!!next-input-character;          !!!next-input-character;
2294    
2295          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2296    
2297          redo A;          redo A;
2298        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2299          !!!cp (139);          !!!cp (139);
2300          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2301          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2302          ## reconsume          ## reconsume
2303    
2304          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2305    
2306          redo A;          redo A;
2307        } else {        } else {
2308          !!!cp (140);          !!!cp (140);
2309          $self->{current_token}->{data} # comment          $self->{ct}->{data} # comment
2310              .= chr ($self->{next_char});              .= chr ($self->{nc});
2311          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2312          !!!next-input-character;          !!!next-input-character;
2313          redo A;          redo A;
2314        }        }
2315      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2316        if ($self->{next_char} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2317          !!!cp (141);          !!!cp (141);
2318          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
2319          !!!next-input-character;          !!!next-input-character;
2320          redo A;          redo A;
2321        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2322          !!!cp (142);          !!!cp (142);
2323          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2324          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2325          !!!next-input-character;          !!!next-input-character;
2326    
2327          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2328    
2329          redo A;          redo A;
2330        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2331          !!!cp (143);          !!!cp (143);
2332          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2333          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2334          ## reconsume          ## reconsume
2335    
2336          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2337    
2338          redo A;          redo A;
2339        } else {        } else {
2340          !!!cp (144);          !!!cp (144);
2341          $self->{current_token}->{data} # comment          $self->{ct}->{data} # comment
2342              .= '-' . chr ($self->{next_char});              .= '-' . chr ($self->{nc});
2343          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2344          !!!next-input-character;          !!!next-input-character;
2345          redo A;          redo A;
2346        }        }
2347      } elsif ($self->{state} == COMMENT_STATE) {      } elsif ($self->{state} == COMMENT_STATE) {
2348        if ($self->{next_char} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2349          !!!cp (145);          !!!cp (145);
2350          $self->{state} = COMMENT_END_DASH_STATE;          $self->{state} = COMMENT_END_DASH_STATE;
2351          !!!next-input-character;          !!!next-input-character;
2352          redo A;          redo A;
2353        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2354          !!!cp (146);          !!!cp (146);
2355          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2356          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2357          ## reconsume          ## reconsume
2358    
2359          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2360    
2361          redo A;          redo A;
2362        } else {        } else {
2363          !!!cp (147);          !!!cp (147);
2364          $self->{current_token}->{data} .= chr ($self->{next_char}); # comment          $self->{ct}->{data} .= chr ($self->{nc}); # comment
2365            $self->{read_until}->($self->{ct}->{data},
2366                                  q[-],
2367                                  length $self->{ct}->{data});
2368    
2369          ## Stay in the state          ## Stay in the state
2370          !!!next-input-character;          !!!next-input-character;
2371          redo A;          redo A;
2372        }        }
2373      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2374        if ($self->{next_char} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2375          !!!cp (148);          !!!cp (148);
2376          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
2377          !!!next-input-character;          !!!next-input-character;
2378          redo A;          redo A;
2379        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2380          !!!cp (149);          !!!cp (149);
2381          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2382          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2383          ## reconsume          ## reconsume
2384    
2385          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2386    
2387          redo A;          redo A;
2388        } else {        } else {
2389          !!!cp (150);          !!!cp (150);
2390          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment          $self->{ct}->{data} .= '-' . chr ($self->{nc}); # comment
2391          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2392          !!!next-input-character;          !!!next-input-character;
2393          redo A;          redo A;
2394        }        }
2395      } elsif ($self->{state} == COMMENT_END_STATE) {      } elsif ($self->{state} == COMMENT_END_STATE) {
2396        if ($self->{next_char} == 0x003E) { # >        if ($self->{nc} == 0x003E) { # >
2397          !!!cp (151);          !!!cp (151);
2398          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2399          !!!next-input-character;          !!!next-input-character;
2400    
2401          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2402    
2403          redo A;          redo A;
2404        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{nc} == 0x002D) { # -
2405          !!!cp (152);          !!!cp (152);
2406          !!!parse-error (type => 'dash in comment',          !!!parse-error (type => 'dash in comment',
2407                          line => $self->{line_prev},                          line => $self->{line_prev},
2408                          column => $self->{column_prev});                          column => $self->{column_prev});
2409          $self->{current_token}->{data} .= '-'; # comment          $self->{ct}->{data} .= '-'; # comment
2410          ## Stay in the state          ## Stay in the state
2411          !!!next-input-character;          !!!next-input-character;
2412          redo A;          redo A;
2413        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2414          !!!cp (153);          !!!cp (153);
2415          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2416          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2417          ## reconsume          ## reconsume
2418    
2419          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2420    
2421          redo A;          redo A;
2422        } else {        } else {
# Line 2243  sub _get_next_token ($) { Line 2424  sub _get_next_token ($) {
2424          !!!parse-error (type => 'dash in comment',          !!!parse-error (type => 'dash in comment',
2425                          line => $self->{line_prev},                          line => $self->{line_prev},
2426                          column => $self->{column_prev});                          column => $self->{column_prev});
2427          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment          $self->{ct}->{data} .= '--' . chr ($self->{nc}); # comment
2428          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2429          !!!next-input-character;          !!!next-input-character;
2430          redo A;          redo A;
2431        }        }
2432      } elsif ($self->{state} == DOCTYPE_STATE) {      } elsif ($self->{state} == DOCTYPE_STATE) {
2433        if ($self->{next_char} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
2434          !!!cp (155);          !!!cp (155);
2435          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2436          !!!next-input-character;          !!!next-input-character;
# Line 2266  sub _get_next_token ($) { Line 2443  sub _get_next_token ($) {
2443          redo A;          redo A;
2444        }        }
2445      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2446        if ($self->{next_char} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
2447          !!!cp (157);          !!!cp (157);
2448          ## Stay in the state          ## Stay in the state
2449          !!!next-input-character;          !!!next-input-character;
2450          redo A;          redo A;
2451        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2452          !!!cp (158);          !!!cp (158);
2453          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2454          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2455          !!!next-input-character;          !!!next-input-character;
2456    
2457          !!!emit ($self->{current_token}); # DOCTYPE (quirks)          !!!emit ($self->{ct}); # DOCTYPE (quirks)
2458    
2459          redo A;          redo A;
2460        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2461          !!!cp (159);          !!!cp (159);
2462          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2463          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2464          ## reconsume          ## reconsume
2465    
2466          !!!emit ($self->{current_token}); # DOCTYPE (quirks)          !!!emit ($self->{ct}); # DOCTYPE (quirks)
2467    
2468          redo A;          redo A;
2469        } else {        } else {
2470          !!!cp (160);          !!!cp (160);
2471          $self->{current_token}->{name} = chr $self->{next_char};          $self->{ct}->{name} = chr $self->{nc};
2472          delete $self->{current_token}->{quirks};          delete $self->{ct}->{quirks};
2473  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2474          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2475          !!!next-input-character;          !!!next-input-character;
# Line 2304  sub _get_next_token ($) { Line 2477  sub _get_next_token ($) {
2477        }        }
2478      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2479  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
2480        if ($self->{next_char} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
2481          !!!cp (161);          !!!cp (161);
2482          $self->{state} = AFTER_DOCTYPE_NAME_STATE;          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2483          !!!next-input-character;          !!!next-input-character;
2484          redo A;          redo A;
2485        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2486          !!!cp (162);          !!!cp (162);
2487          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2488          !!!next-input-character;          !!!next-input-character;
2489    
2490          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2491    
2492          redo A;          redo A;
2493        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2494          !!!cp (163);          !!!cp (163);
2495          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2496          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2497          ## reconsume          ## reconsume
2498    
2499          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2500          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2501    
2502          redo A;          redo A;
2503        } else {        } else {
2504          !!!cp (164);          !!!cp (164);
2505          $self->{current_token}->{name}          $self->{ct}->{name}
2506            .= chr ($self->{next_char}); # DOCTYPE            .= chr ($self->{nc}); # DOCTYPE
2507          ## Stay in the state          ## Stay in the state
2508          !!!next-input-character;          !!!next-input-character;
2509          redo A;          redo A;
2510        }        }
2511      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2512        if ($self->{next_char} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
2513          !!!cp (165);          !!!cp (165);
2514          ## Stay in the state          ## Stay in the state
2515          !!!next-input-character;          !!!next-input-character;
2516          redo A;          redo A;
2517        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2518          !!!cp (166);          !!!cp (166);
2519          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2520          !!!next-input-character;          !!!next-input-character;
2521    
2522          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2523    
2524          redo A;          redo A;
2525        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2526          !!!cp (167);          !!!cp (167);
2527          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2528          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2529          ## reconsume          ## reconsume
2530    
2531          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2532          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2533    
2534          redo A;          redo A;
2535        } elsif ($self->{next_char} == 0x0050 or # P        } elsif ($self->{nc} == 0x0050 or # P
2536                 $self->{next_char} == 0x0070) { # p                 $self->{nc} == 0x0070) { # p
2537            $self->{state} = PUBLIC_STATE;
2538            $self->{s_kwd} = chr $self->{nc};
2539          !!!next-input-character;          !!!next-input-character;
2540          if ($self->{next_char} == 0x0055 or # U          redo A;
2541              $self->{next_char} == 0x0075) { # u        } elsif ($self->{nc} == 0x0053 or # S
2542            !!!next-input-character;                 $self->{nc} == 0x0073) { # s
2543            if ($self->{next_char} == 0x0042 or # B          $self->{state} = SYSTEM_STATE;
2544                $self->{next_char} == 0x0062) { # b          $self->{s_kwd} = chr $self->{nc};
             !!!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);  
         }  
   
         #  
       } elsif ($self->{next_char} == 0x0053 or # S  
                $self->{next_char} == 0x0073) { # s  
2545          !!!next-input-character;          !!!next-input-character;
2546          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);  
         }  
   
         #  
2547        } else {        } else {
2548          !!!cp (180);          !!!cp (180);
2549            !!!parse-error (type => 'string after DOCTYPE name');
2550            $self->{ct}->{quirks} = 1;
2551    
2552            $self->{state} = BOGUS_DOCTYPE_STATE;
2553          !!!next-input-character;          !!!next-input-character;
2554          #          redo A;
2555        }        }
2556        } elsif ($self->{state} == PUBLIC_STATE) {
2557          ## ASCII case-insensitive
2558          if ($self->{nc} == [
2559                undef,
2560                0x0055, # U
2561                0x0042, # B
2562                0x004C, # L
2563                0x0049, # I
2564              ]->[length $self->{s_kwd}] or
2565              $self->{nc} == [
2566                undef,
2567                0x0075, # u
2568                0x0062, # b
2569                0x006C, # l
2570                0x0069, # i
2571              ]->[length $self->{s_kwd}]) {
2572            !!!cp (175);
2573            ## Stay in the state.
2574            $self->{s_kwd} .= chr $self->{nc};
2575            !!!next-input-character;
2576            redo A;
2577          } elsif ((length $self->{s_kwd}) == 5 and
2578                   ($self->{nc} == 0x0043 or # C
2579                    $self->{nc} == 0x0063)) { # c
2580            !!!cp (168);
2581            $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2582            !!!next-input-character;
2583            redo A;
2584          } else {
2585            !!!cp (169);
2586            !!!parse-error (type => 'string after DOCTYPE name',
2587                            line => $self->{line_prev},
2588                            column => $self->{column_prev} + 1 - length $self->{s_kwd});
2589            $self->{ct}->{quirks} = 1;
2590    
2591        !!!parse-error (type => 'string after DOCTYPE name');          $self->{state} = BOGUS_DOCTYPE_STATE;
2592        $self->{current_token}->{quirks} = 1;          ## Reconsume.
2593            redo A;
2594          }
2595        } elsif ($self->{state} == SYSTEM_STATE) {
2596          ## ASCII case-insensitive
2597          if ($self->{nc} == [
2598                undef,
2599                0x0059, # Y
2600                0x0053, # S
2601                0x0054, # T
2602                0x0045, # E
2603              ]->[length $self->{s_kwd}] or
2604              $self->{nc} == [
2605                undef,
2606                0x0079, # y
2607                0x0073, # s
2608                0x0074, # t
2609                0x0065, # e
2610              ]->[length $self->{s_kwd}]) {
2611            !!!cp (170);
2612            ## Stay in the state.
2613            $self->{s_kwd} .= chr $self->{nc};
2614            !!!next-input-character;
2615            redo A;
2616          } elsif ((length $self->{s_kwd}) == 5 and
2617                   ($self->{nc} == 0x004D or # M
2618                    $self->{nc} == 0x006D)) { # m
2619            !!!cp (171);
2620            $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2621            !!!next-input-character;
2622            redo A;
2623          } else {
2624            !!!cp (172);
2625            !!!parse-error (type => 'string after DOCTYPE name',
2626                            line => $self->{line_prev},
2627                            column => $self->{column_prev} + 1 - length $self->{s_kwd});
2628            $self->{ct}->{quirks} = 1;
2629    
2630        $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2631        # next-input-character is already done          ## Reconsume.
2632        redo A;          redo A;
2633          }
2634      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2635        if ({        if ($is_space->{$self->{nc}}) {
             0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,  
             #0x000D => 1, # HT, LF, VT, FF, SP, CR  
           }->{$self->{next_char}}) {  
2636          !!!cp (181);          !!!cp (181);
2637          ## Stay in the state          ## Stay in the state
2638          !!!next-input-character;          !!!next-input-character;
2639          redo A;          redo A;
2640        } elsif ($self->{next_char} eq 0x0022) { # "        } elsif ($self->{nc} eq 0x0022) { # "
2641          !!!cp (182);          !!!cp (182);
2642          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{ct}->{pubid} = ''; # DOCTYPE
2643          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2644          !!!next-input-character;          !!!next-input-character;
2645          redo A;          redo A;
2646        } elsif ($self->{next_char} eq 0x0027) { # '        } elsif ($self->{nc} eq 0x0027) { # '
2647          !!!cp (183);          !!!cp (183);
2648          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{ct}->{pubid} = ''; # DOCTYPE
2649          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2650          !!!next-input-character;          !!!next-input-character;
2651          redo A;          redo A;
2652        } elsif ($self->{next_char} eq 0x003E) { # >        } elsif ($self->{nc} eq 0x003E) { # >
2653          !!!cp (184);          !!!cp (184);
2654          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
2655    
2656          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2657          !!!next-input-character;          !!!next-input-character;
2658    
2659          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2660          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2661    
2662          redo A;          redo A;
2663        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2664          !!!cp (185);          !!!cp (185);
2665          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2666    
2667          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2668          ## reconsume          ## reconsume
2669    
2670          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2671          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2672    
2673          redo A;          redo A;
2674        } else {        } else {
2675          !!!cp (186);          !!!cp (186);
2676          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
2677          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2678    
2679          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2680          !!!next-input-character;          !!!next-input-character;
2681          redo A;          redo A;
2682        }        }
2683      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2684        if ($self->{next_char} == 0x0022) { # "        if ($self->{nc} == 0x0022) { # "
2685          !!!cp (187);          !!!cp (187);
2686          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2687          !!!next-input-character;          !!!next-input-character;
2688          redo A;          redo A;
2689        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2690          !!!cp (188);          !!!cp (188);
2691          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2692    
2693          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2694          !!!next-input-character;          !!!next-input-character;
2695    
2696          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2697          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2698    
2699          redo A;          redo A;
2700        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2701          !!!cp (189);          !!!cp (189);
2702          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2703    
2704          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2705          ## reconsume          ## reconsume
2706    
2707          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2708          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2709    
2710          redo A;          redo A;
2711        } else {        } else {
2712          !!!cp (190);          !!!cp (190);
2713          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{ct}->{pubid} # DOCTYPE
2714              .= chr $self->{next_char};              .= chr $self->{nc};
2715            $self->{read_until}->($self->{ct}->{pubid}, q[">],
2716                                  length $self->{ct}->{pubid});
2717    
2718          ## Stay in the state          ## Stay in the state
2719          !!!next-input-character;          !!!next-input-character;
2720          redo A;          redo A;
2721        }        }
2722      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2723        if ($self->{next_char} == 0x0027) { # '        if ($self->{nc} == 0x0027) { # '
2724          !!!cp (191);          !!!cp (191);
2725          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2726          !!!next-input-character;          !!!next-input-character;
2727          redo A;          redo A;
2728        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2729          !!!cp (192);          !!!cp (192);
2730          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2731    
2732          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2733          !!!next-input-character;          !!!next-input-character;
2734    
2735          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2736          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2737    
2738          redo A;          redo A;
2739        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2740          !!!cp (193);          !!!cp (193);
2741          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2742    
2743          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2744          ## reconsume          ## reconsume
2745    
2746          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2747          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2748    
2749          redo A;          redo A;
2750        } else {        } else {
2751          !!!cp (194);          !!!cp (194);
2752          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{ct}->{pubid} # DOCTYPE
2753              .= chr $self->{next_char};              .= chr $self->{nc};
2754            $self->{read_until}->($self->{ct}->{pubid}, q['>],
2755                                  length $self->{ct}->{pubid});
2756    
2757          ## Stay in the state          ## Stay in the state
2758          !!!next-input-character;          !!!next-input-character;
2759          redo A;          redo A;
2760        }        }
2761      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2762        if ({        if ($is_space->{$self->{nc}}) {
             0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,  
             #0x000D => 1, # HT, LF, VT, FF, SP, CR  
           }->{$self->{next_char}}) {  
2763          !!!cp (195);          !!!cp (195);
2764          ## Stay in the state          ## Stay in the state
2765          !!!next-input-character;          !!!next-input-character;
2766          redo A;          redo A;
2767        } elsif ($self->{next_char} == 0x0022) { # "        } elsif ($self->{nc} == 0x0022) { # "
2768          !!!cp (196);          !!!cp (196);
2769          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{ct}->{sysid} = ''; # DOCTYPE
2770          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2771          !!!next-input-character;          !!!next-input-character;
2772          redo A;          redo A;
2773        } elsif ($self->{next_char} == 0x0027) { # '        } elsif ($self->{nc} == 0x0027) { # '
2774          !!!cp (197);          !!!cp (197);
2775          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{ct}->{sysid} = ''; # DOCTYPE
2776          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2777          !!!next-input-character;          !!!next-input-character;
2778          redo A;          redo A;
2779        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2780          !!!cp (198);          !!!cp (198);
2781          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2782          !!!next-input-character;          !!!next-input-character;
2783    
2784          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2785    
2786          redo A;          redo A;
2787        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2788          !!!cp (199);          !!!cp (199);
2789          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2790    
2791          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2792          ## reconsume          ## reconsume
2793    
2794          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2795          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2796    
2797          redo A;          redo A;
2798        } else {        } else {
2799          !!!cp (200);          !!!cp (200);
2800          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2801          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2802    
2803          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2804          !!!next-input-character;          !!!next-input-character;
2805          redo A;          redo A;
2806        }        }
2807      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2808        if ({        if ($is_space->{$self->{nc}}) {
             0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,  
             #0x000D => 1, # HT, LF, VT, FF, SP, CR  
           }->{$self->{next_char}}) {  
2809          !!!cp (201);          !!!cp (201);
2810          ## Stay in the state          ## Stay in the state
2811          !!!next-input-character;          !!!next-input-character;
2812          redo A;          redo A;
2813        } elsif ($self->{next_char} == 0x0022) { # "        } elsif ($self->{nc} == 0x0022) { # "
2814          !!!cp (202);          !!!cp (202);
2815          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{ct}->{sysid} = ''; # DOCTYPE
2816          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2817          !!!next-input-character;          !!!next-input-character;
2818          redo A;          redo A;
2819        } elsif ($self->{next_char} == 0x0027) { # '        } elsif ($self->{nc} == 0x0027) { # '
2820          !!!cp (203);          !!!cp (203);
2821          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{ct}->{sysid} = ''; # DOCTYPE
2822          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2823          !!!next-input-character;          !!!next-input-character;
2824          redo A;          redo A;
2825        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2826          !!!cp (204);          !!!cp (204);
2827          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2828          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2829          !!!next-input-character;          !!!next-input-character;
2830    
2831          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2832          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2833    
2834          redo A;          redo A;
2835        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2836          !!!cp (205);          !!!cp (205);
2837          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2838    
2839          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2840          ## reconsume          ## reconsume
2841    
2842          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2843          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2844    
2845          redo A;          redo A;
2846        } else {        } else {
2847          !!!cp (206);          !!!cp (206);
2848          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2849          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2850    
2851          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2852          !!!next-input-character;          !!!next-input-character;
2853          redo A;          redo A;
2854        }        }
2855      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2856        if ($self->{next_char} == 0x0022) { # "        if ($self->{nc} == 0x0022) { # "
2857          !!!cp (207);          !!!cp (207);
2858          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2859          !!!next-input-character;          !!!next-input-character;
2860          redo A;          redo A;
2861        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2862          !!!cp (208);          !!!cp (208);
2863          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2864    
2865          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2866          !!!next-input-character;          !!!next-input-character;
2867    
2868          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2869          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2870    
2871          redo A;          redo A;
2872        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2873          !!!cp (209);          !!!cp (209);
2874          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2875    
2876          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2877          ## reconsume          ## reconsume
2878    
2879          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2880          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2881    
2882          redo A;          redo A;
2883        } else {        } else {
2884          !!!cp (210);          !!!cp (210);
2885          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{ct}->{sysid} # DOCTYPE
2886              .= chr $self->{next_char};              .= chr $self->{nc};
2887            $self->{read_until}->($self->{ct}->{sysid}, q[">],
2888                                  length $self->{ct}->{sysid});
2889    
2890          ## Stay in the state          ## Stay in the state
2891          !!!next-input-character;          !!!next-input-character;
2892          redo A;          redo A;
2893        }        }
2894      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2895        if ($self->{next_char} == 0x0027) { # '        if ($self->{nc} == 0x0027) { # '
2896          !!!cp (211);          !!!cp (211);
2897          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2898          !!!next-input-character;          !!!next-input-character;
2899          redo A;          redo A;
2900        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2901          !!!cp (212);          !!!cp (212);
2902          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2903    
2904          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2905          !!!next-input-character;          !!!next-input-character;
2906    
2907          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2908          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2909    
2910          redo A;          redo A;
2911        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2912          !!!cp (213);          !!!cp (213);
2913          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2914    
2915          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2916          ## reconsume          ## reconsume
2917    
2918          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2919          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2920    
2921          redo A;          redo A;
2922        } else {        } else {
2923          !!!cp (214);          !!!cp (214);
2924          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{ct}->{sysid} # DOCTYPE
2925              .= chr $self->{next_char};              .= chr $self->{nc};
2926            $self->{read_until}->($self->{ct}->{sysid}, q['>],
2927                                  length $self->{ct}->{sysid});
2928    
2929          ## Stay in the state          ## Stay in the state
2930          !!!next-input-character;          !!!next-input-character;
2931          redo A;          redo A;
2932        }        }
2933      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2934        if ({        if ($is_space->{$self->{nc}}) {
             0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,  
             #0x000D => 1, # HT, LF, VT, FF, SP, CR  
           }->{$self->{next_char}}) {  
2935          !!!cp (215);          !!!cp (215);
2936          ## Stay in the state          ## Stay in the state
2937          !!!next-input-character;          !!!next-input-character;
2938          redo A;          redo A;
2939        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2940          !!!cp (216);          !!!cp (216);
2941          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2942          !!!next-input-character;          !!!next-input-character;
2943    
2944          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2945    
2946          redo A;          redo A;
2947        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2948          !!!cp (217);          !!!cp (217);
2949          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2950          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2951          ## reconsume          ## reconsume
2952    
2953          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2954          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2955    
2956          redo A;          redo A;
2957        } else {        } else {
2958          !!!cp (218);          !!!cp (218);
2959          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2960          #$self->{current_token}->{quirks} = 1;          #$self->{ct}->{quirks} = 1;
2961    
2962          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2963          !!!next-input-character;          !!!next-input-character;
2964          redo A;          redo A;
2965        }        }
2966      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2967        if ($self->{next_char} == 0x003E) { # >        if ($self->{nc} == 0x003E) { # >
2968          !!!cp (219);          !!!cp (219);
2969          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2970          !!!next-input-character;          !!!next-input-character;
2971    
2972          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2973    
2974          redo A;          redo A;
2975        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2976          !!!cp (220);          !!!cp (220);
2977          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2978          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2979          ## reconsume          ## reconsume
2980    
2981          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2982    
2983          redo A;          redo A;
2984        } else {        } else {
2985          !!!cp (221);          !!!cp (221);
2986            my $s = '';
2987            $self->{read_until}->($s, q[>], 0);
2988    
2989          ## Stay in the state          ## Stay in the state
2990          !!!next-input-character;          !!!next-input-character;
2991          redo A;          redo A;
2992        }        }
2993      } elsif ($self->{state} == CDATA_BLOCK_STATE) {      } elsif ($self->{state} == CDATA_SECTION_STATE) {
2994        my $s = '';        ## NOTE: "CDATA section state" in the state is jointly implemented
2995          ## by three states, |CDATA_SECTION_STATE|, |CDATA_SECTION_MSE1_STATE|,
2996          ## and |CDATA_SECTION_MSE2_STATE|.
2997                
2998        my ($l, $c) = ($self->{line}, $self->{column});        if ($self->{nc} == 0x005D) { # ]
2999            !!!cp (221.1);
3000        CS: while ($self->{next_char} != -1) {          $self->{state} = CDATA_SECTION_MSE1_STATE;
         if ($self->{next_char} == 0x005D) { # ]  
           !!!next-input-character;  
           if ($self->{next_char} == 0x005D) { # ]  
             !!!next-input-character;  
             MDC: {  
               if ($self->{next_char} == 0x003E) { # >  
                 !!!cp (221.1);  
                 !!!next-input-character;  
                 last CS;  
               } elsif ($self->{next_char} == 0x005D) { # ]  
                 !!!cp (221.2);  
                 $s .= ']';  
                 !!!next-input-character;  
                 redo MDC;  
               } else {  
                 !!!cp (221.3);  
                 $s .= ']]';  
                 #  
               }  
             } # MDC  
           } else {  
             !!!cp (221.4);  
             $s .= ']';  
             #  
           }  
         } else {  
           !!!cp (221.5);  
           #  
         }  
         $s .= chr $self->{next_char};  
3001          !!!next-input-character;          !!!next-input-character;
3002        } # CS          redo A;
3003          } elsif ($self->{nc} == -1) {
3004            $self->{state} = DATA_STATE;
3005            !!!next-input-character;
3006            if (length $self->{ct}->{data}) { # character
3007              !!!cp (221.2);
3008              !!!emit ($self->{ct}); # character
3009            } else {
3010              !!!cp (221.3);
3011              ## No token to emit. $self->{ct} is discarded.
3012            }        
3013            redo A;
3014          } else {
3015            !!!cp (221.4);
3016            $self->{ct}->{data} .= chr $self->{nc};
3017            $self->{read_until}->($self->{ct}->{data},
3018                                  q<]>,
3019                                  length $self->{ct}->{data});
3020    
3021        $self->{state} = DATA_STATE;          ## Stay in the state.
3022        ## next-input-character done or EOF, which is reconsumed.          !!!next-input-character;
3023            redo A;
3024          }
3025    
3026        if (length $s) {        ## ISSUE: "text tokens" in spec.
3027        } elsif ($self->{state} == CDATA_SECTION_MSE1_STATE) {
3028          if ($self->{nc} == 0x005D) { # ]
3029            !!!cp (221.5);
3030            $self->{state} = CDATA_SECTION_MSE2_STATE;
3031            !!!next-input-character;
3032            redo A;
3033          } else {
3034          !!!cp (221.6);          !!!cp (221.6);
3035          !!!emit ({type => CHARACTER_TOKEN, data => $s,          $self->{ct}->{data} .= ']';
3036                    line => $l, column => $c});          $self->{state} = CDATA_SECTION_STATE;
3037            ## Reconsume.
3038            redo A;
3039          }
3040        } elsif ($self->{state} == CDATA_SECTION_MSE2_STATE) {
3041          if ($self->{nc} == 0x003E) { # >
3042            $self->{state} = DATA_STATE;
3043            !!!next-input-character;
3044            if (length $self->{ct}->{data}) { # character
3045              !!!cp (221.7);
3046              !!!emit ($self->{ct}); # character
3047            } else {
3048              !!!cp (221.8);
3049              ## No token to emit. $self->{ct} is discarded.
3050            }
3051            redo A;
3052          } elsif ($self->{nc} == 0x005D) { # ]
3053            !!!cp (221.9); # character
3054            $self->{ct}->{data} .= ']'; ## Add first "]" of "]]]".
3055            ## Stay in the state.
3056            !!!next-input-character;
3057            redo A;
3058        } else {        } else {
3059          !!!cp (221.7);          !!!cp (221.11);
3060            $self->{ct}->{data} .= ']]'; # character
3061            $self->{state} = CDATA_SECTION_STATE;
3062            ## Reconsume.
3063            redo A;
3064          }
3065        } elsif ($self->{state} == ENTITY_STATE) {
3066          if ($is_space->{$self->{nc}} or
3067              {
3068                0x003C => 1, 0x0026 => 1, -1 => 1, # <, &
3069                $self->{entity_add} => 1,
3070              }->{$self->{nc}}) {
3071            !!!cp (1001);
3072            ## Don't consume
3073            ## No error
3074            ## Return nothing.
3075            #
3076          } elsif ($self->{nc} == 0x0023) { # #
3077            !!!cp (999);
3078            $self->{state} = ENTITY_HASH_STATE;
3079            $self->{s_kwd} = '#';
3080            !!!next-input-character;
3081            redo A;
3082          } elsif ((0x0041 <= $self->{nc} and
3083                    $self->{nc} <= 0x005A) or # A..Z
3084                   (0x0061 <= $self->{nc} and
3085                    $self->{nc} <= 0x007A)) { # a..z
3086            !!!cp (998);
3087            require Whatpm::_NamedEntityList;
3088            $self->{state} = ENTITY_NAME_STATE;
3089            $self->{s_kwd} = chr $self->{nc};
3090            $self->{entity__value} = $self->{s_kwd};
3091            $self->{entity__match} = 0;
3092            !!!next-input-character;
3093            redo A;
3094          } else {
3095            !!!cp (1027);
3096            !!!parse-error (type => 'bare ero');
3097            ## Return nothing.
3098            #
3099        }        }
3100    
3101        redo A;        ## NOTE: No character is consumed by the "consume a character
3102          ## reference" algorithm.  In other word, there is an "&" character
3103        ## ISSUE: "text tokens" in spec.        ## that does not introduce a character reference, which would be
3104        ## TODO: Streaming support        ## appended to the parent element or the attribute value in later
3105      } else {        ## process of the tokenizer.
3106        die "$0: $self->{state}: Unknown state";  
3107      }        if ($self->{prev_state} == DATA_STATE) {
3108    } # A            !!!cp (997);
3109            $self->{state} = $self->{prev_state};
3110    die "$0: _get_next_token: unexpected case";          ## Reconsume.
3111  } # _get_next_token          !!!emit ({type => CHARACTER_TOKEN, data => '&',
3112                      line => $self->{line_prev},
3113  sub _tokenize_attempt_to_consume_an_entity ($$$) {                    column => $self->{column_prev},
3114    my ($self, $in_attr, $additional) = @_;                   });
3115            redo A;
3116    my ($l, $c) = ($self->{line_prev}, $self->{column_prev});        } else {
3117            !!!cp (996);
3118            $self->{ca}->{value} .= '&';
3119            $self->{state} = $self->{prev_state};
3120            ## Reconsume.
3121            redo A;
3122          }
3123        } elsif ($self->{state} == ENTITY_HASH_STATE) {
3124          if ($self->{nc} == 0x0078 or # x
3125              $self->{nc} == 0x0058) { # X
3126            !!!cp (995);
3127            $self->{state} = HEXREF_X_STATE;
3128            $self->{s_kwd} .= chr $self->{nc};
3129            !!!next-input-character;
3130            redo A;
3131          } elsif (0x0030 <= $self->{nc} and
3132                   $self->{nc} <= 0x0039) { # 0..9
3133            !!!cp (994);
3134            $self->{state} = NCR_NUM_STATE;
3135            $self->{s_kwd} = $self->{nc} - 0x0030;
3136            !!!next-input-character;
3137            redo A;
3138          } else {
3139            !!!parse-error (type => 'bare nero',
3140                            line => $self->{line_prev},
3141                            column => $self->{column_prev} - 1);
3142    
3143    if ({          ## NOTE: According to the spec algorithm, nothing is returned,
3144         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,          ## and then "&#" is appended to the parent element or the attribute
3145         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR          ## value in the later processing.
3146         $additional => 1,  
3147        }->{$self->{next_char}}) {          if ($self->{prev_state} == DATA_STATE) {
3148      !!!cp (1001);            !!!cp (1019);
3149      ## Don't consume            $self->{state} = $self->{prev_state};
3150      ## No error            ## Reconsume.
3151      return undef;            !!!emit ({type => CHARACTER_TOKEN,
3152    } elsif ($self->{next_char} == 0x0023) { # #                      data => '&#',
3153      !!!next-input-character;                      line => $self->{line_prev},
3154      if ($self->{next_char} == 0x0078 or # x                      column => $self->{column_prev} - 1,
3155          $self->{next_char} == 0x0058) { # X                     });
3156        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;  
3157          } else {          } else {
3158            !!!cp (1007);            !!!cp (993);
3159            !!!parse-error (type => 'no refc', line => $l, column => $c);            $self->{ca}->{value} .= '&#';
3160              $self->{state} = $self->{prev_state};
3161              ## Reconsume.
3162              redo A;
3163          }          }
3164          }
3165          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {      } elsif ($self->{state} == NCR_NUM_STATE) {
3166            !!!cp (1008);        if (0x0030 <= $self->{nc} and
3167            !!!parse-error (type => 'invalid character reference',            $self->{nc} <= 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  
3168          !!!cp (1012);          !!!cp (1012);
3169          $code *= 10;          $self->{s_kwd} *= 10;
3170          $code += $self->{next_char} - 0x0030;          $self->{s_kwd} += $self->{nc} - 0x0030;
3171                    
3172            ## Stay in the state.
3173          !!!next-input-character;          !!!next-input-character;
3174        }          redo A;
3175          } elsif ($self->{nc} == 0x003B) { # ;
       if ($self->{next_char} == 0x003B) { # ;  
3176          !!!cp (1013);          !!!cp (1013);
3177          !!!next-input-character;          !!!next-input-character;
3178            #
3179        } else {        } else {
3180          !!!cp (1014);          !!!cp (1014);
3181          !!!parse-error (type => 'no refc', line => $l, column => $c);          !!!parse-error (type => 'no refc');
3182            ## Reconsume.
3183            #
3184        }        }
3185    
3186        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        my $code = $self->{s_kwd};
3187          my $l = $self->{line_prev};
3188          my $c = $self->{column_prev};
3189          if ($charref_map->{$code}) {
3190          !!!cp (1015);          !!!cp (1015);
3191          !!!parse-error (type => 'invalid character reference',          !!!parse-error (type => 'invalid character reference',
3192                          text => (sprintf 'U+%04X', $code),                          text => (sprintf 'U+%04X', $code),
3193                          line => $l, column => $c);                          line => $l, column => $c);
3194          $code = 0xFFFD;          $code = $charref_map->{$code};
3195        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
3196          !!!cp (1016);          !!!cp (1016);
3197          !!!parse-error (type => 'invalid character reference',          !!!parse-error (type => 'invalid character reference',
3198                          text => (sprintf 'U-%08X', $code),                          text => (sprintf 'U-%08X', $code),
3199                          line => $l, column => $c);                          line => $l, column => $c);
3200          $code = 0xFFFD;          $code = 0xFFFD;
3201        } elsif ($code == 0x000D) {        }
3202          !!!cp (1017);  
3203          !!!parse-error (type => 'CR character reference',        if ($self->{prev_state} == DATA_STATE) {
3204                          line => $l, column => $c);          !!!cp (992);
3205          $code = 0x000A;          $self->{state} = $self->{prev_state};
3206        } elsif (0x80 <= $code and $code <= 0x9F) {          ## Reconsume.
3207          !!!cp (1018);          !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3208          !!!parse-error (type => 'C1 character reference',                    line => $l, column => $c,
3209                     });
3210            redo A;
3211          } else {
3212            !!!cp (991);
3213            $self->{ca}->{value} .= chr $code;
3214            $self->{ca}->{has_reference} = 1;
3215            $self->{state} = $self->{prev_state};
3216            ## Reconsume.
3217            redo A;
3218          }
3219        } elsif ($self->{state} == HEXREF_X_STATE) {
3220          if ((0x0030 <= $self->{nc} and $self->{nc} <= 0x0039) or
3221              (0x0041 <= $self->{nc} and $self->{nc} <= 0x0046) or
3222              (0x0061 <= $self->{nc} and $self->{nc} <= 0x0066)) {
3223            # 0..9, A..F, a..f
3224            !!!cp (990);
3225            $self->{state} = HEXREF_HEX_STATE;
3226            $self->{s_kwd} = 0;
3227            ## Reconsume.
3228            redo A;
3229          } else {
3230            !!!parse-error (type => 'bare hcro',
3231                            line => $self->{line_prev},
3232                            column => $self->{column_prev} - 2);
3233    
3234            ## NOTE: According to the spec algorithm, nothing is returned,
3235            ## and then "&#" followed by "X" or "x" is appended to the parent
3236            ## element or the attribute value in the later processing.
3237    
3238            if ($self->{prev_state} == DATA_STATE) {
3239              !!!cp (1005);
3240              $self->{state} = $self->{prev_state};
3241              ## Reconsume.
3242              !!!emit ({type => CHARACTER_TOKEN,
3243                        data => '&' . $self->{s_kwd},
3244                        line => $self->{line_prev},
3245                        column => $self->{column_prev} - length $self->{s_kwd},
3246                       });
3247              redo A;
3248            } else {
3249              !!!cp (989);
3250              $self->{ca}->{value} .= '&' . $self->{s_kwd};
3251              $self->{state} = $self->{prev_state};
3252              ## Reconsume.
3253              redo A;
3254            }
3255          }
3256        } elsif ($self->{state} == HEXREF_HEX_STATE) {
3257          if (0x0030 <= $self->{nc} and $self->{nc} <= 0x0039) {
3258            # 0..9
3259            !!!cp (1002);
3260            $self->{s_kwd} *= 0x10;
3261            $self->{s_kwd} += $self->{nc} - 0x0030;
3262            ## Stay in the state.
3263            !!!next-input-character;
3264            redo A;
3265          } elsif (0x0061 <= $self->{nc} and
3266                   $self->{nc} <= 0x0066) { # a..f
3267            !!!cp (1003);
3268            $self->{s_kwd} *= 0x10;
3269            $self->{s_kwd} += $self->{nc} - 0x0060 + 9;
3270            ## Stay in the state.
3271            !!!next-input-character;
3272            redo A;
3273          } elsif (0x0041 <= $self->{nc} and
3274                   $self->{nc} <= 0x0046) { # A..F
3275            !!!cp (1004);
3276            $self->{s_kwd} *= 0x10;
3277            $self->{s_kwd} += $self->{nc} - 0x0040 + 9;
3278            ## Stay in the state.
3279            !!!next-input-character;
3280            redo A;
3281          } elsif ($self->{nc} == 0x003B) { # ;
3282            !!!cp (1006);
3283            !!!next-input-character;
3284            #
3285          } else {
3286            !!!cp (1007);
3287            !!!parse-error (type => 'no refc',
3288                            line => $self->{line},
3289                            column => $self->{column});
3290            ## Reconsume.
3291            #
3292          }
3293    
3294          my $code = $self->{s_kwd};
3295          my $l = $self->{line_prev};
3296          my $c = $self->{column_prev};
3297          if ($charref_map->{$code}) {
3298            !!!cp (1008);
3299            !!!parse-error (type => 'invalid character reference',
3300                          text => (sprintf 'U+%04X', $code),                          text => (sprintf 'U+%04X', $code),
3301                          line => $l, column => $c);                          line => $l, column => $c);
3302          $code = $c1_entity_char->{$code};          $code = $charref_map->{$code};
3303          } elsif ($code > 0x10FFFF) {
3304            !!!cp (1009);
3305            !!!parse-error (type => 'invalid character reference',
3306                            text => (sprintf 'U-%08X', $code),
3307                            line => $l, column => $c);
3308            $code = 0xFFFD;
3309        }        }
3310          
3311        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,        if ($self->{prev_state} == DATA_STATE) {
3312                line => $l, column => $c,          !!!cp (988);
3313               };          $self->{state} = $self->{prev_state};
3314      } else {          ## Reconsume.
3315        !!!cp (1019);          !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3316        !!!parse-error (type => 'bare nero', line => $l, column => $c);                    line => $l, column => $c,
3317        !!!back-next-input-character ($self->{next_char});                   });
3318        $self->{next_char} = 0x0023; # #          redo A;
3319        return undef;        } else {
3320      }          !!!cp (987);
3321    } elsif ((0x0041 <= $self->{next_char} and          $self->{ca}->{value} .= chr $code;
3322              $self->{next_char} <= 0x005A) or          $self->{ca}->{has_reference} = 1;
3323             (0x0061 <= $self->{next_char} and          $self->{state} = $self->{prev_state};
3324              $self->{next_char} <= 0x007A)) {          ## Reconsume.
3325      my $entity_name = chr $self->{next_char};          redo A;
3326      !!!next-input-character;        }
3327        } elsif ($self->{state} == ENTITY_NAME_STATE) {
3328      my $value = $entity_name;        if (length $self->{s_kwd} < 30 and
3329      my $match = 0;            ## NOTE: Some number greater than the maximum length of entity name
3330      require Whatpm::_NamedEntityList;            ((0x0041 <= $self->{nc} and # a
3331      our $EntityChar;              $self->{nc} <= 0x005A) or # x
3332               (0x0061 <= $self->{nc} and # a
3333      while (length $entity_name < 30 and              $self->{nc} <= 0x007A) or # z
3334             ## NOTE: Some number greater than the maximum length of entity name             (0x0030 <= $self->{nc} and # 0
3335             ((0x0041 <= $self->{next_char} and # a              $self->{nc} <= 0x0039) or # 9
3336               $self->{next_char} <= 0x005A) or # x             $self->{nc} == 0x003B)) { # ;
3337              (0x0061 <= $self->{next_char} and # a          our $EntityChar;
3338               $self->{next_char} <= 0x007A) or # z          $self->{s_kwd} .= chr $self->{nc};
3339              (0x0030 <= $self->{next_char} and # 0          if (defined $EntityChar->{$self->{s_kwd}}) {
3340               $self->{next_char} <= 0x0039) or # 9            if ($self->{nc} == 0x003B) { # ;
3341              $self->{next_char} == 0x003B)) { # ;              !!!cp (1020);
3342        $entity_name .= chr $self->{next_char};              $self->{entity__value} = $EntityChar->{$self->{s_kwd}};
3343        if (defined $EntityChar->{$entity_name}) {              $self->{entity__match} = 1;
3344          if ($self->{next_char} == 0x003B) { # ;              !!!next-input-character;
3345            !!!cp (1020);              #
3346            $value = $EntityChar->{$entity_name};            } else {
3347            $match = 1;              !!!cp (1021);
3348            !!!next-input-character;              $self->{entity__value} = $EntityChar->{$self->{s_kwd}};
3349            last;              $self->{entity__match} = -1;
3350                ## Stay in the state.
3351                !!!next-input-character;
3352                redo A;
3353              }
3354          } else {          } else {
3355            !!!cp (1021);            !!!cp (1022);
3356            $value = $EntityChar->{$entity_name};            $self->{entity__value} .= chr $self->{nc};
3357            $match = -1;            $self->{entity__match} *= 2;
3358              ## Stay in the state.
3359            !!!next-input-character;            !!!next-input-character;
3360              redo A;
3361            }
3362          }
3363    
3364          my $data;
3365          my $has_ref;
3366          if ($self->{entity__match} > 0) {
3367            !!!cp (1023);
3368            $data = $self->{entity__value};
3369            $has_ref = 1;
3370            #
3371          } elsif ($self->{entity__match} < 0) {
3372            !!!parse-error (type => 'no refc');
3373            if ($self->{prev_state} != DATA_STATE and # in attribute
3374                $self->{entity__match} < -1) {
3375              !!!cp (1024);
3376              $data = '&' . $self->{s_kwd};
3377              #
3378            } else {
3379              !!!cp (1025);
3380              $data = $self->{entity__value};
3381              $has_ref = 1;
3382              #
3383          }          }
3384        } else {        } else {
3385          !!!cp (1022);          !!!cp (1026);
3386          $value .= chr $self->{next_char};          !!!parse-error (type => 'bare ero',
3387          $match *= 2;                          line => $self->{line_prev},
3388          !!!next-input-character;                          column => $self->{column_prev} - length $self->{s_kwd});
3389            $data = '&' . $self->{s_kwd};
3390            #
3391        }        }
3392      }    
3393              ## NOTE: In these cases, when a character reference is found,
3394      if ($match > 0) {        ## it is consumed and a character token is returned, or, otherwise,
3395        !!!cp (1023);        ## nothing is consumed and returned, according to the spec algorithm.
3396        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,        ## In this implementation, anything that has been examined by the
3397                line => $l, column => $c,        ## tokenizer is appended to the parent element or the attribute value
3398               };        ## as string, either literal string when no character reference or
3399      } elsif ($match < 0) {        ## entity-replaced string otherwise, in this stage, since any characters
3400        !!!parse-error (type => 'no refc', line => $l, column => $c);        ## that would not be consumed are appended in the data state or in an
3401        if ($in_attr and $match < -1) {        ## appropriate attribute value state anyway.
3402          !!!cp (1024);  
3403          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,        if ($self->{prev_state} == DATA_STATE) {
3404                  line => $l, column => $c,          !!!cp (986);
3405                 };          $self->{state} = $self->{prev_state};
3406        } else {          ## Reconsume.
3407          !!!cp (1025);          !!!emit ({type => CHARACTER_TOKEN,
3408          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,                    data => $data,
3409                  line => $l, column => $c,                    line => $self->{line_prev},
3410                 };                    column => $self->{column_prev} + 1 - length $self->{s_kwd},
3411                     });
3412            redo A;
3413          } else {
3414            !!!cp (985);
3415            $self->{ca}->{value} .= $data;
3416            $self->{ca}->{has_reference} = 1 if $has_ref;
3417            $self->{state} = $self->{prev_state};
3418            ## Reconsume.
3419            redo A;
3420        }        }
3421      } else {      } else {
3422        !!!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,  
              };  
3423      }      }
3424    } else {    } # A  
3425      !!!cp (1027);  
3426      ## no characters are consumed    die "$0: _get_next_token: unexpected case";
3427      !!!parse-error (type => 'bare ero', line => $l, column => $c);  } # _get_next_token
     return undef;  
   }  
 } # _tokenize_attempt_to_consume_an_entity  
3428    
3429  sub _initialize_tree_constructor ($) {  sub _initialize_tree_constructor ($) {
3430    my $self = shift;    my $self = shift;
# Line 3154  sub _tree_construction_initial ($) { Line 3489  sub _tree_construction_initial ($) {
3489        ## language.        ## language.
3490        my $doctype_name = $token->{name};        my $doctype_name = $token->{name};
3491        $doctype_name = '' unless defined $doctype_name;        $doctype_name = '' unless defined $doctype_name;
3492        $doctype_name =~ tr/a-z/A-Z/;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3493        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
3494            defined $token->{public_identifier} or            defined $token->{sysid}) {
           defined $token->{system_identifier}) {  
3495          !!!cp ('t1');          !!!cp ('t1');
3496          !!!parse-error (type => 'not HTML5', token => $token);          !!!parse-error (type => 'not HTML5', token => $token);
3497        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3498          !!!cp ('t2');          !!!cp ('t2');
         ## ISSUE: ASCII case-insensitive? (in fact it does not matter)  
3499          !!!parse-error (type => 'not HTML5', token => $token);          !!!parse-error (type => 'not HTML5', token => $token);
3500          } elsif (defined $token->{pubid}) {
3501            if ($token->{pubid} eq 'XSLT-compat') {
3502              !!!cp ('t1.2');
3503              !!!parse-error (type => 'XSLT-compat', token => $token,
3504                              level => $self->{level}->{should});
3505            } else {
3506              !!!parse-error (type => 'not HTML5', token => $token);
3507            }
3508        } else {        } else {
3509          !!!cp ('t3');          !!!cp ('t3');
3510            #
3511        }        }
3512                
3513        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3514          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3515        ## NOTE: Default value for both |public_id| and |system_id| attributes        ## NOTE: Default value for both |public_id| and |system_id| attributes
3516        ## are empty strings, so that we don't set any value in missing cases.        ## are empty strings, so that we don't set any value in missing cases.
3517        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{pubid}) if defined $token->{pubid};
3518            if defined $token->{public_identifier};        $doctype->system_id ($token->{sysid}) if defined $token->{sysid};
       $doctype->system_id ($token->{system_identifier})  
           if defined $token->{system_identifier};  
3519        ## NOTE: Other DocumentType attributes are null or empty lists.        ## NOTE: Other DocumentType attributes are null or empty lists.
3520        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
3521        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
# Line 3183  sub _tree_construction_initial ($) { Line 3523  sub _tree_construction_initial ($) {
3523        if ($token->{quirks} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
3524          !!!cp ('t4');          !!!cp ('t4');
3525          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
3526        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{pubid}) {
3527          my $pubid = $token->{public_identifier};          my $pubid = $token->{pubid};
3528          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3529          my $prefix = [          my $prefix = [
3530            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
# Line 3258  sub _tree_construction_initial ($) { Line 3598  sub _tree_construction_initial ($) {
3598            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3599          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3600                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3601            if (defined $token->{system_identifier}) {            if (defined $token->{sysid}) {
3602              !!!cp ('t6');              !!!cp ('t6');
3603              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
3604            } else {            } else {
# Line 3275  sub _tree_construction_initial ($) { Line 3615  sub _tree_construction_initial ($) {
3615        } else {        } else {
3616          !!!cp ('t10');          !!!cp ('t10');
3617        }        }
3618        if (defined $token->{system_identifier}) {        if (defined $token->{sysid}) {
3619          my $sysid = $token->{system_identifier};          my $sysid = $token->{sysid};
3620          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3621          if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {          if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
3622            ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is            ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
# Line 3306  sub _tree_construction_initial ($) { Line 3646  sub _tree_construction_initial ($) {
3646        !!!ack-later;        !!!ack-later;
3647        return;        return;
3648      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3649        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
3650          ## Ignore the token          ## Ignore the token
3651    
3652          unless (length $token->{data}) {          unless (length $token->{data}) {
# Line 3363  sub _tree_construction_root_element ($) Line 3703  sub _tree_construction_root_element ($)
3703          !!!next-token;          !!!next-token;
3704          redo B;          redo B;
3705        } elsif ($token->{type} == CHARACTER_TOKEN) {        } elsif ($token->{type} == CHARACTER_TOKEN) {
3706          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D          if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
3707            ## Ignore the token.            ## Ignore the token.
3708    
3709            unless (length $token->{data}) {            unless (length $token->{data}) {
# Line 4177  sub _tree_construction_main ($) { Line 4517  sub _tree_construction_main ($) {
4517    
4518      if ($self->{insertion_mode} & HEAD_IMS) {      if ($self->{insertion_mode} & HEAD_IMS) {
4519        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4520          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
4521            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4522              !!!cp ('t88.2');              !!!cp ('t88.2');
4523              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4524                #
4525            } else {            } else {
4526              !!!cp ('t88.1');              !!!cp ('t88.1');
4527              ## Ignore the token.              ## Ignore the token.
4528              !!!next-token;              #
             next B;  
4529            }            }
4530            unless (length $token->{data}) {            unless (length $token->{data}) {
4531              !!!cp ('t88');              !!!cp ('t88');
4532              !!!next-token;              !!!next-token;
4533              next B;              next B;
4534            }            }
4535    ## TODO: set $token->{column} appropriately
4536          }          }
4537    
4538          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
# Line 4355  sub _tree_construction_main ($) { Line 4696  sub _tree_construction_main ($) {
4696                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
4697                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4698                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4699                            [\x09-\x0D\x20]*=                            [\x09\x0A\x0C\x0D\x20]*=
4700                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09\x0A\x0C\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4701                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {                            ([^"'\x09\x0A\x0C\x0D\x20]
4702                               [^\x09\x0A\x0C\x0D\x20\x3B]*))/x) {
4703                      !!!cp ('t107');                      !!!cp ('t107');
4704                      ## NOTE: Whether the encoding is supported or not is handled                      ## NOTE: Whether the encoding is supported or not is handled
4705                      ## in the {change_encoding} callback.                      ## in the {change_encoding} callback.
# Line 5166  sub _tree_construction_main ($) { Line 5508  sub _tree_construction_main ($) {
5508      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
5509        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5510          if (not $open_tables->[-1]->[1] and # tainted          if (not $open_tables->[-1]->[1] and # tainted
5511              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              $token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
5512            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5513                                
5514            unless (length $token->{data}) {            unless (length $token->{data}) {
# Line 5850  sub _tree_construction_main ($) { Line 6192  sub _tree_construction_main ($) {
6192        }        }
6193      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6194            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6195              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
6196                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6197                unless (length $token->{data}) {                unless (length $token->{data}) {
6198                  !!!cp ('t260');                  !!!cp ('t260');
# Line 6191  sub _tree_construction_main ($) { Line 6533  sub _tree_construction_main ($) {
6533        }        }
6534      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
6535        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
6536          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
6537            my $data = $1;            my $data = $1;
6538            ## As if in body            ## As if in body
6539            $reconstruct_active_formatting_elements->($insert_to_current);            $reconstruct_active_formatting_elements->($insert_to_current);
# Line 6208  sub _tree_construction_main ($) { Line 6550  sub _tree_construction_main ($) {
6550          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6551            !!!cp ('t301');            !!!cp ('t301');
6552            !!!parse-error (type => 'after html:#text', token => $token);            !!!parse-error (type => 'after html:#text', token => $token);
6553              #
           ## Reprocess in the "after body" insertion mode.  
6554          } else {          } else {
6555            !!!cp ('t302');            !!!cp ('t302');
6556              ## "after body" insertion mode
6557              !!!parse-error (type => 'after body:#text', token => $token);
6558              #
6559          }          }
           
         ## "after body" insertion mode  
         !!!parse-error (type => 'after body:#text', token => $token);  
6560    
6561          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6562          ## reprocess          ## reprocess
# Line 6225  sub _tree_construction_main ($) { Line 6566  sub _tree_construction_main ($) {
6566            !!!cp ('t303');            !!!cp ('t303');
6567            !!!parse-error (type => 'after html',            !!!parse-error (type => 'after html',
6568                            text => $token->{tag_name}, token => $token);                            text => $token->{tag_name}, token => $token);
6569                        #
           ## Reprocess in the "after body" insertion mode.  
6570          } else {          } else {
6571            !!!cp ('t304');            !!!cp ('t304');
6572              ## "after body" insertion mode
6573              !!!parse-error (type => 'after body',
6574                              text => $token->{tag_name}, token => $token);
6575              #
6576          }          }
6577    
         ## "after body" insertion mode  
         !!!parse-error (type => 'after body',  
                         text => $token->{tag_name}, token => $token);  
   
6578          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6579          !!!ack-later;          !!!ack-later;
6580          ## reprocess          ## reprocess
# Line 6245  sub _tree_construction_main ($) { Line 6585  sub _tree_construction_main ($) {
6585            !!!parse-error (type => 'after html:/',            !!!parse-error (type => 'after html:/',
6586                            text => $token->{tag_name}, token => $token);                            text => $token->{tag_name}, token => $token);
6587                        
6588            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6589            ## Reprocess in the "after body" insertion mode.            ## Reprocess.
6590              next B;
6591          } else {          } else {
6592            !!!cp ('t306');            !!!cp ('t306');
6593          }          }
# Line 6284  sub _tree_construction_main ($) { Line 6625  sub _tree_construction_main ($) {
6625        }        }
6626      } elsif ($self->{insertion_mode} & FRAME_IMS) {      } elsif ($self->{insertion_mode} & FRAME_IMS) {
6627        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
6628          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
6629            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6630                        
6631            unless (length $token->{data}) {            unless (length $token->{data}) {
# Line 6294  sub _tree_construction_main ($) { Line 6635  sub _tree_construction_main ($) {
6635            }            }
6636          }          }
6637                    
6638          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0C\x20]+//) {
6639            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6640              !!!cp ('t311');              !!!cp ('t311');
6641              !!!parse-error (type => 'in frameset:#text', token => $token);              !!!parse-error (type => 'in frameset:#text', token => $token);
# Line 6471  sub _tree_construction_main ($) { Line 6812  sub _tree_construction_main ($) {
6812            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
6813              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6814                  =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6815                      [\x09-\x0D\x20]*=                      [\x09\x0A\x0C\x0D\x20]*=
6816                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09\x0A\x0C\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6817                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {                      ([^"'\x09\x0A\x0C\x0D\x20][^\x09\x0A\x0C\x0D\x20\x3B]*))
6818                       /x) {
6819                !!!cp ('t336');                !!!cp ('t336');
6820                ## NOTE: Whether the encoding is supported or not is handled                ## NOTE: Whether the encoding is supported or not is handled
6821                ## in the {change_encoding} callback.                ## in the {change_encoding} callback.
# Line 7436  sub _tree_construction_main ($) { Line 7778  sub _tree_construction_main ($) {
7778    ## TODO: script stuffs    ## TODO: script stuffs
7779  } # _tree_construct_main  } # _tree_construct_main
7780    
7781  sub set_inner_html ($$$) {  sub set_inner_html ($$$$;$) {
7782    my $class = shift;    my $class = shift;
7783    my $node = shift;    my $node = shift;
7784    my $s = \$_[0];    #my $s = \$_[0];
7785    my $onerror = $_[1];    my $onerror = $_[1];
7786      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7787    
7788    ## ISSUE: Should {confident} be true?    ## ISSUE: Should {confident} be true?
7789    
# Line 7459  sub set_inner_html ($$$) { Line 7802  sub set_inner_html ($$$) {
7802      }      }
7803    
7804      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7805      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($_[0] => $node, $onerror, $get_wrapper);
7806    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7807      ## TODO: If non-html element      ## TODO: If non-html element
7808    
7809      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7810    
7811    ## TODO: Support for $get_wrapper
7812    
7813      ## Step 1 # MUST      ## Step 1 # MUST
7814      my $this_doc = $node->owner_document;      my $this_doc = $node->owner_document;
7815      my $doc = $this_doc->implementation->create_document;      my $doc = $this_doc->implementation->create_document;
# Line 7476  sub set_inner_html ($$$) { Line 7821  sub set_inner_html ($$$) {
7821      my $i = 0;      my $i = 0;
7822      $p->{line_prev} = $p->{line} = 1;      $p->{line_prev} = $p->{line} = 1;
7823      $p->{column_prev} = $p->{column} = 0;      $p->{column_prev} = $p->{column} = 0;
7824      $p->{set_next_char} = sub {      require Whatpm::Charset::DecodeHandle;
7825        my $input = Whatpm::Charset::DecodeHandle::CharString->new (\($_[0]));
7826        $input = $get_wrapper->($input);
7827        $p->{set_nc} = sub {
7828        my $self = shift;        my $self = shift;
7829    
7830        pop @{$self->{prev_char}};        my $char = '';
7831        unshift @{$self->{prev_char}}, $self->{next_char};        if (defined $self->{next_nc}) {
7832            $char = $self->{next_nc};
7833        $self->{next_char} = -1 and return if $i >= length $$s;          delete $self->{next_nc};
7834        $self->{next_char} = ord substr $$s, $i++, 1;          $self->{nc} = ord $char;
7835          } else {
7836            $self->{char_buffer} = '';
7837            $self->{char_buffer_pos} = 0;
7838            
7839            my $count = $input->manakai_read_until
7840                ($self->{char_buffer}, qr/[^\x00\x0A\x0D]/,
7841                 $self->{char_buffer_pos});
7842            if ($count) {
7843              $self->{line_prev} = $self->{line};
7844              $self->{column_prev} = $self->{column};
7845              $self->{column}++;
7846              $self->{nc}
7847                  = ord substr ($self->{char_buffer},
7848                                $self->{char_buffer_pos}++, 1);
7849              return;
7850            }
7851            
7852            if ($input->read ($char, 1)) {
7853              $self->{nc} = ord $char;
7854            } else {
7855              $self->{nc} = -1;
7856              return;
7857            }
7858          }
7859    
7860        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7861        $p->{column}++;        $p->{column}++;
7862    
7863        if ($self->{next_char} == 0x000A) { # LF        if ($self->{nc} == 0x000A) { # LF
7864          $p->{line}++;          $p->{line}++;
7865          $p->{column} = 0;          $p->{column} = 0;
7866          !!!cp ('i1');          !!!cp ('i1');
7867        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{nc} == 0x000D) { # CR
7868          $i++ if substr ($$s, $i, 1) eq "\x0A";  ## TODO: support for abort/streaming
7869          $self->{next_char} = 0x000A; # LF # MUST          my $next = '';
7870            if ($input->read ($next, 1) and $next ne "\x0A") {
7871              $self->{next_nc} = $next;
7872            }
7873            $self->{nc} = 0x000A; # LF # MUST
7874          $p->{line}++;          $p->{line}++;
7875          $p->{column} = 0;          $p->{column} = 0;
7876          !!!cp ('i2');          !!!cp ('i2');
7877        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{nc} == 0x0000) { # NULL
         $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST  
         !!!cp ('i3');  
       } elsif ($self->{next_char} == 0x0000) { # NULL  
7878          !!!cp ('i4');          !!!cp ('i4');
7879          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7880          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{nc} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
       } elsif ($self->{next_char} <= 0x0008 or  
                (0x000E <= $self->{next_char} and  
                 $self->{next_char} <= 0x001F) or  
                (0x007F <= $self->{next_char} and  
                 $self->{next_char} <= 0x009F) or  
                (0xD800 <= $self->{next_char} and  
                 $self->{next_char} <= 0xDFFF) or  
                (0xFDD0 <= $self->{next_char} and  
                 $self->{next_char} <= 0xFDDF) or  
                {  
                 0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,  
                 0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,  
                 0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,  
                 0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,  
                 0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,  
                 0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,  
                 0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,  
                 0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,  
                 0x10FFFE => 1, 0x10FFFF => 1,  
                }->{$self->{next_char}}) {  
         !!!cp ('i4.1');  
         if ($self->{next_char} < 0x10000) {  
           !!!parse-error (type => 'control char',  
                           text => (sprintf 'U+%04X', $self->{next_char}));  
         } else {  
           !!!parse-error (type => 'control char',  
                           text => (sprintf 'U-%08X', $self->{next_char}));  
         }  
7881        }        }
7882      };      };
7883      $p->{prev_char} = [-1, -1, -1];  
7884      $p->{next_char} = -1;      $p->{read_until} = sub {
7885              #my ($scalar, $specials_range, $offset) = @_;
7886          return 0 if defined $p->{next_nc};
7887    
7888          my $pattern = qr/[^$_[1]\x00\x0A\x0D]/;
7889          my $offset = $_[2] || 0;
7890          
7891          if ($p->{char_buffer_pos} < length $p->{char_buffer}) {
7892            pos ($p->{char_buffer}) = $p->{char_buffer_pos};
7893            if ($p->{char_buffer} =~ /\G(?>$pattern)+/) {
7894              substr ($_[0], $offset)
7895                  = substr ($p->{char_buffer}, $-[0], $+[0] - $-[0]);
7896              my $count = $+[0] - $-[0];
7897              if ($count) {
7898                $p->{column} += $count;
7899                $p->{char_buffer_pos} += $count;
7900                $p->{line_prev} = $p->{line};
7901                $p->{column_prev} = $p->{column} - 1;
7902                $p->{nc} = -1;
7903              }
7904              return $count;
7905            } else {
7906              return 0;
7907            }
7908          } else {
7909            my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
7910            if ($count) {
7911              $p->{column} += $count;
7912              $p->{column_prev} += $count;
7913              $p->{nc} = -1;
7914            }
7915            return $count;
7916          }
7917        }; # $p->{read_until}
7918    
7919      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7920        my (%opt) = @_;        my (%opt) = @_;
7921        my $line = $opt{line};        my $line = $opt{line};
# Line 7552  sub set_inner_html ($$$) { Line 7930  sub set_inner_html ($$$) {
7930        $ponerror->(line => $p->{line}, column => $p->{column}, @_);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7931      };      };
7932            
7933        my $char_onerror = sub {
7934          my (undef, $type, %opt) = @_;
7935          $ponerror->(layer => 'encode',
7936                      line => $p->{line}, column => $p->{column} + 1,
7937                      %opt, type => $type);
7938        }; # $char_onerror
7939        $input->onerror ($char_onerror);
7940    
7941      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7942      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7943    

Legend:
Removed from v.1.158  
changed lines
  Added in v.1.192

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24