/[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.150 by wakaba, Sun Jun 1 06:47:08 2008 UTC revision 1.185 by wakaba, Mon Sep 15 09:27:53 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 45  sub MISC_SPECIAL_EL () { 0b1000000000000 Line 56  sub MISC_SPECIAL_EL () { 0b1000000000000
56  sub FOREIGN_EL () { 0b10000000000000000000000000 }  sub FOREIGN_EL () { 0b10000000000000000000000000 }
57  sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }  sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
58  sub MML_AXML_EL () { 0b1000000000000000000000000000 }  sub MML_AXML_EL () { 0b1000000000000000000000000000 }
59    sub RUBY_EL () { 0b10000000000000000000000000000 }
60    sub RUBY_COMPONENT_EL () { 0b100000000000000000000000000000 }
61    
62  sub TABLE_ROWS_EL () {  sub TABLE_ROWS_EL () {
63    TABLE_EL |    TABLE_EL |
# Line 52  sub TABLE_ROWS_EL () { Line 65  sub TABLE_ROWS_EL () {
65    TABLE_ROW_GROUP_EL    TABLE_ROW_GROUP_EL
66  }  }
67    
68    ## NOTE: Used in "generate implied end tags" algorithm.
69    ## NOTE: There is a code where a modified version of END_TAG_OPTIONAL_EL
70    ## is used in "generate implied end tags" implementation (search for the
71    ## function mae).
72  sub END_TAG_OPTIONAL_EL () {  sub END_TAG_OPTIONAL_EL () {
73    DD_EL |    DD_EL |
74    DT_EL |    DT_EL |
75    LI_EL |    LI_EL |
76    P_EL    P_EL |
77      RUBY_COMPONENT_EL
78  }  }
79    
80    ## NOTE: Used in </body> and EOF algorithms.
81  sub ALL_END_TAG_OPTIONAL_EL () {  sub ALL_END_TAG_OPTIONAL_EL () {
82    END_TAG_OPTIONAL_EL |    DD_EL |
83      DT_EL |
84      LI_EL |
85      P_EL |
86    
87    BODY_EL |    BODY_EL |
88    HTML_EL |    HTML_EL |
89    TABLE_CELL_EL |    TABLE_CELL_EL |
# Line 96  sub SPECIAL_EL () { Line 119  sub SPECIAL_EL () {
119    ADDRESS_EL |    ADDRESS_EL |
120    BODY_EL |    BODY_EL |
121    DIV_EL |    DIV_EL |
122    END_TAG_OPTIONAL_EL |  
123      DD_EL |
124      DT_EL |
125      LI_EL |
126      P_EL |
127    
128    FORM_EL |    FORM_EL |
129    FRAMESET_EL |    FRAMESET_EL |
130    HEADING_EL |    HEADING_EL |
# Line 170  my $el_category = { Line 198  my $el_category = {
198    param => MISC_SPECIAL_EL,    param => MISC_SPECIAL_EL,
199    plaintext => MISC_SPECIAL_EL,    plaintext => MISC_SPECIAL_EL,
200    pre => MISC_SPECIAL_EL,    pre => MISC_SPECIAL_EL,
201      rp => RUBY_COMPONENT_EL,
202      rt => RUBY_COMPONENT_EL,
203      ruby => RUBY_EL,
204    s => FORMATTING_EL,    s => FORMATTING_EL,
205    script => MISC_SPECIAL_EL,    script => MISC_SPECIAL_EL,
206    select => SELECT_EL,    select => SELECT_EL,
# Line 334  sub parse_byte_string ($$$$;$) { Line 365  sub parse_byte_string ($$$$;$) {
365    return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);    return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
366  } # parse_byte_string  } # parse_byte_string
367    
368  sub parse_byte_stream ($$$$;$) {  sub parse_byte_stream ($$$$;$$) {
369      # my ($self, $charset_name, $byte_stream, $doc, $onerror, $get_wrapper) = @_;
370    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
371    my $charset_name = shift;    my $charset_name = shift;
372    my $byte_stream = $_[0];    my $byte_stream = $_[0];
# Line 345  sub parse_byte_stream ($$$$;$) { Line 377  sub parse_byte_stream ($$$$;$) {
377    };    };
378    $self->{parse_error} = $onerror; # updated later by parse_char_string    $self->{parse_error} = $onerror; # updated later by parse_char_string
379    
380      my $get_wrapper = $_[3] || sub ($) {
381        return $_[0]; # $_[0] = byte stream handle, returned = arg to char handle
382      };
383    
384    ## HTML5 encoding sniffing algorithm    ## HTML5 encoding sniffing algorithm
385    require Message::Charset::Info;    require Message::Charset::Info;
386    my $charset;    my $charset;
# Line 352  sub parse_byte_stream ($$$$;$) { Line 388  sub parse_byte_stream ($$$$;$) {
388    my ($char_stream, $e_status);    my ($char_stream, $e_status);
389    
390    SNIFFING: {    SNIFFING: {
391        ## NOTE: By setting |allow_fallback| option true when the
392        ## |get_decode_handle| method is invoked, we ignore what the HTML5
393        ## spec requires, i.e. unsupported encoding should be ignored.
394          ## TODO: We should not do this unless the parser is invoked
395          ## in the conformance checking mode, in which this behavior
396          ## would be useful.
397    
398      ## Step 1      ## Step 1
399      if (defined $charset_name) {      if (defined $charset_name) {
400        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);        $charset = Message::Charset::Info->get_by_html_name ($charset_name);
401              ## TODO: Is this ok?  Transfer protocol's parameter should be
402              ## interpreted in its semantics?
403    
404        ## ISSUE: Unsupported encoding is not ignored according to the spec.        ## ISSUE: Unsupported encoding is not ignored according to the spec.
405        ($char_stream, $e_status) = $charset->get_decode_handle        ($char_stream, $e_status) = $charset->get_decode_handle
# Line 379  sub parse_byte_stream ($$$$;$) { Line 423  sub parse_byte_stream ($$$$;$) {
423    
424      ## Step 3      ## Step 3
425      if ($byte_buffer =~ /^\xFE\xFF/) {      if ($byte_buffer =~ /^\xFE\xFF/) {
426        $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');        $charset = Message::Charset::Info->get_by_html_name ('utf-16be');
427        ($char_stream, $e_status) = $charset->get_decode_handle        ($char_stream, $e_status) = $charset->get_decode_handle
428            ($byte_stream, allow_error_reporting => 1,            ($byte_stream, allow_error_reporting => 1,
429             allow_fallback => 1, byte_buffer => \$byte_buffer);             allow_fallback => 1, byte_buffer => \$byte_buffer);
430        $self->{confident} = 1;        $self->{confident} = 1;
431        last SNIFFING;        last SNIFFING;
432      } elsif ($byte_buffer =~ /^\xFF\xFE/) {      } elsif ($byte_buffer =~ /^\xFF\xFE/) {
433        $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');        $charset = Message::Charset::Info->get_by_html_name ('utf-16le');
434        ($char_stream, $e_status) = $charset->get_decode_handle        ($char_stream, $e_status) = $charset->get_decode_handle
435            ($byte_stream, allow_error_reporting => 1,            ($byte_stream, allow_error_reporting => 1,
436             allow_fallback => 1, byte_buffer => \$byte_buffer);             allow_fallback => 1, byte_buffer => \$byte_buffer);
437        $self->{confident} = 1;        $self->{confident} = 1;
438        last SNIFFING;        last SNIFFING;
439      } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {      } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
440        $charset = Message::Charset::Info->get_by_iana_name ('utf-8');        $charset = Message::Charset::Info->get_by_html_name ('utf-8');
441        ($char_stream, $e_status) = $charset->get_decode_handle        ($char_stream, $e_status) = $charset->get_decode_handle
442            ($byte_stream, allow_error_reporting => 1,            ($byte_stream, allow_error_reporting => 1,
443             allow_fallback => 1, byte_buffer => \$byte_buffer);             allow_fallback => 1, byte_buffer => \$byte_buffer);
# Line 412  sub parse_byte_stream ($$$$;$) { Line 456  sub parse_byte_stream ($$$$;$) {
456      $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string      $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
457          ($byte_buffer);          ($byte_buffer);
458      if (defined $charset_name) {      if (defined $charset_name) {
459        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);        $charset = Message::Charset::Info->get_by_html_name ($charset_name);
460    
461        ## ISSUE: Unsupported encoding is not ignored according to the spec.        ## ISSUE: Unsupported encoding is not ignored according to the spec.
462        require Whatpm::Charset::DecodeHandle;        require Whatpm::Charset::DecodeHandle;
# Line 423  sub parse_byte_stream ($$$$;$) { Line 467  sub parse_byte_stream ($$$$;$) {
467             allow_fallback => 1, byte_buffer => \$byte_buffer);             allow_fallback => 1, byte_buffer => \$byte_buffer);
468        if ($char_stream) {        if ($char_stream) {
469          $buffer->{buffer} = $byte_buffer;          $buffer->{buffer} = $byte_buffer;
470          !!!parse-error (type => 'sniffing:chardet', ## TODO: type name          !!!parse-error (type => 'sniffing:chardet',
471                          value => $charset_name,                          text => $charset_name,
472                          level => $self->{info_level},                          level => $self->{level}->{info},
473                            layer => 'encode',
474                          line => 1, column => 1);                          line => 1, column => 1);
475          $self->{confident} = 0;          $self->{confident} = 0;
476          last SNIFFING;          last SNIFFING;
# Line 434  sub parse_byte_stream ($$$$;$) { Line 479  sub parse_byte_stream ($$$$;$) {
479    
480      ## Step 7: default      ## Step 7: default
481      ## TODO: Make this configurable.      ## TODO: Make this configurable.
482      $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');      $charset = Message::Charset::Info->get_by_html_name ('windows-1252');
483          ## NOTE: We choose |windows-1252| here, since |utf-8| should be          ## NOTE: We choose |windows-1252| here, since |utf-8| should be
484          ## detectable in the step 6.          ## detectable in the step 6.
485      require Whatpm::Charset::DecodeHandle;      require Whatpm::Charset::DecodeHandle;
# Line 446  sub parse_byte_stream ($$$$;$) { Line 491  sub parse_byte_stream ($$$$;$) {
491                                         allow_fallback => 1,                                         allow_fallback => 1,
492                                         byte_buffer => \$byte_buffer);                                         byte_buffer => \$byte_buffer);
493      $buffer->{buffer} = $byte_buffer;      $buffer->{buffer} = $byte_buffer;
494      !!!parse-error (type => 'sniffing:default', ## TODO: type name      !!!parse-error (type => 'sniffing:default',
495                      value => 'windows-1252',                      text => 'windows-1252',
496                      level => $self->{info_level},                      level => $self->{level}->{info},
497                      line => 1, column => 1);                      line => 1, column => 1,
498                        layer => 'encode');
499      $self->{confident} = 0;      $self->{confident} = 0;
500    } # SNIFFING    } # SNIFFING
501    
   $self->{input_encoding} = $charset->get_iana_name;  
502    if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {    if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
503      !!!parse-error (type => 'chardecode:fallback', ## TODO: type name      $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
504                      value => $self->{input_encoding},      !!!parse-error (type => 'chardecode:fallback',
505                      level => $self->{unsupported_level},                      #text => $self->{input_encoding},
506                      line => 1, column => 1);                      level => $self->{level}->{uncertain},
507                        line => 1, column => 1,
508                        layer => 'encode');
509    } elsif (not ($e_status &    } elsif (not ($e_status &
510                  Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {                  Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL ())) {
511      !!!parse-error (type => 'chardecode:no error', ## TODO: type name      $self->{input_encoding} = $charset->get_iana_name;
512                      value => $self->{input_encoding},      !!!parse-error (type => 'chardecode:no error',
513                      level => $self->{unsupported_level},                      text => $self->{input_encoding},
514                      line => 1, column => 1);                      level => $self->{level}->{uncertain},
515                        line => 1, column => 1,
516                        layer => 'encode');
517      } else {
518        $self->{input_encoding} = $charset->get_iana_name;
519    }    }
520    
521    $self->{change_encoding} = sub {    $self->{change_encoding} = sub {
# Line 472  sub parse_byte_stream ($$$$;$) { Line 523  sub parse_byte_stream ($$$$;$) {
523      $charset_name = shift;      $charset_name = shift;
524      my $token = shift;      my $token = shift;
525    
526      $charset = Message::Charset::Info->get_by_iana_name ($charset_name);      $charset = Message::Charset::Info->get_by_html_name ($charset_name);
527      ($char_stream, $e_status) = $charset->get_decode_handle      ($char_stream, $e_status) = $charset->get_decode_handle
528          ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,          ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
529           byte_buffer => \ $buffer->{buffer});           byte_buffer => \ $buffer->{buffer});
# Line 483  sub parse_byte_stream ($$$$;$) { Line 534  sub parse_byte_stream ($$$$;$) {
534        ## Step 1            ## Step 1    
535        if ($charset->{category} &        if ($charset->{category} &
536            Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {            Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
537          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');          $charset = Message::Charset::Info->get_by_html_name ('utf-8');
538          ($char_stream, $e_status) = $charset->get_decode_handle          ($char_stream, $e_status) = $charset->get_decode_handle
539              ($byte_stream,              ($byte_stream,
540               byte_buffer => \ $buffer->{buffer});               byte_buffer => \ $buffer->{buffer});
# Line 493  sub parse_byte_stream ($$$$;$) { Line 544  sub parse_byte_stream ($$$$;$) {
544        ## Step 2        ## Step 2
545        if (defined $self->{input_encoding} and        if (defined $self->{input_encoding} and
546            $self->{input_encoding} eq $charset_name) {            $self->{input_encoding} eq $charset_name) {
547          !!!parse-error (type => 'charset label:matching', ## TODO: type          !!!parse-error (type => 'charset label:matching',
548                          value => $charset_name,                          text => $charset_name,
549                          level => $self->{info_level});                          level => $self->{level}->{info});
550          $self->{confident} = 1;          $self->{confident} = 1;
551          return;          return;
552        }        }
553    
554        !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.        !!!parse-error (type => 'charset label detected',
555            ':'.$charset_name, level => 'w', token => $token);                        text => $self->{input_encoding},
556                          value => $charset_name,
557                          level => $self->{level}->{warn},
558                          token => $token);
559                
560        ## Step 3        ## Step 3
561        # if (can) {        # if (can) {
# Line 517  sub parse_byte_stream ($$$$;$) { Line 571  sub parse_byte_stream ($$$$;$) {
571    
572    my $char_onerror = sub {    my $char_onerror = sub {
573      my (undef, $type, %opt) = @_;      my (undef, $type, %opt) = @_;
574      !!!parse-error (%opt, type => $type,      !!!parse-error (layer => 'encode',
575                      line => $self->{line}, column => $self->{column} + 1);                      line => $self->{line}, column => $self->{column} + 1,
576                        %opt, type => $type);
577      if ($opt{octets}) {      if ($opt{octets}) {
578        ${$opt{octets}} = "\x{FFFD}"; # relacement character        ${$opt{octets}} = "\x{FFFD}"; # relacement character
579      }      }
580    };    };
   $char_stream->onerror ($char_onerror);  
581    
582    my @args = @_; shift @args; # $s    my $wrapped_char_stream = $get_wrapper->($char_stream);
583      $wrapped_char_stream->onerror ($char_onerror);
584    
585      my @args = ($_[1], $_[2]); # $doc, $onerror - $get_wrapper = undef;
586    my $return;    my $return;
587    try {    try {
588      $return = $self->parse_char_stream ($char_stream, @args);        $return = $self->parse_char_stream ($wrapped_char_stream, @args);  
589    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
590      ## NOTE: Invoked after {change_encoding}.      ## NOTE: Invoked after {change_encoding}.
591    
     $self->{input_encoding} = $charset->get_iana_name;  
592      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
593        !!!parse-error (type => 'chardecode:fallback', ## TODO: type name        $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
594                        value => $self->{input_encoding},        !!!parse-error (type => 'chardecode:fallback',
595                        level => $self->{unsupported_level},                        level => $self->{level}->{uncertain},
596                        line => 1, column => 1);                        #text => $self->{input_encoding},
597                          line => 1, column => 1,
598                          layer => 'encode');
599      } elsif (not ($e_status &      } elsif (not ($e_status &
600                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL ())) {
601        !!!parse-error (type => 'chardecode:no error', ## TODO: type name        $self->{input_encoding} = $charset->get_iana_name;
602                        value => $self->{input_encoding},        !!!parse-error (type => 'chardecode:no error',
603                        level => $self->{unsupported_level},                        text => $self->{input_encoding},
604                        line => 1, column => 1);                        level => $self->{level}->{uncertain},
605                          line => 1, column => 1,
606                          layer => 'encode');
607        } else {
608          $self->{input_encoding} = $charset->get_iana_name;
609      }      }
610      $self->{confident} = 1;      $self->{confident} = 1;
611      $char_stream->onerror ($char_onerror);  
612      $return = $self->parse_char_stream ($char_stream, @args);      $wrapped_char_stream = $get_wrapper->($char_stream);
613        $wrapped_char_stream->onerror ($char_onerror);
614    
615        $return = $self->parse_char_stream ($wrapped_char_stream, @args);
616    };    };
617    return $return;    return $return;
618  } # parse_byte_stream  } # parse_byte_stream
# Line 561  sub parse_byte_stream ($$$$;$) { Line 626  sub parse_byte_stream ($$$$;$) {
626  ## 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
627  ## strip the BOM and never strip any ZWNBSP.  ## strip the BOM and never strip any ZWNBSP.
628    
629  sub parse_char_string ($$$;$) {  sub parse_char_string ($$$;$$) {
630      #my ($self, $s, $doc, $onerror, $get_wrapper) = @_;
631    my $self = shift;    my $self = shift;
   require utf8;  
632    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $s = ref $_[0] ? $_[0] : \($_[0]);
633    open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;    require Whatpm::Charset::DecodeHandle;
634      my $input = Whatpm::Charset::DecodeHandle::CharString->new ($s);
635    return $self->parse_char_stream ($input, @_[1..$#_]);    return $self->parse_char_stream ($input, @_[1..$#_]);
636  } # parse_char_string  } # parse_char_string
637  *parse_string = \&parse_char_string;  *parse_string = \&parse_char_string; ## NOTE: Alias for backward compatibility.
638    
639  sub parse_char_stream ($$$;$) {  sub parse_char_stream ($$$;$$) {
640    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
641    my $input = $_[0];    my $input = $_[0];
642    $self->{document} = $_[1];    $self->{document} = $_[1];
# Line 581  sub parse_char_stream ($$$;$) { Line 647  sub parse_char_stream ($$$;$) {
647    $self->{confident} = 1 unless exists $self->{confident};    $self->{confident} = 1 unless exists $self->{confident};
648    $self->{document}->input_encoding ($self->{input_encoding})    $self->{document}->input_encoding ($self->{input_encoding})
649        if defined $self->{input_encoding};        if defined $self->{input_encoding};
650    ## TODO: |{input_encoding}| is needless?
651    
   my $i = 0;  
652    $self->{line_prev} = $self->{line} = 1;    $self->{line_prev} = $self->{line} = 1;
653    $self->{column_prev} = $self->{column} = 0;    $self->{column_prev} = -1;
654    $self->{set_next_char} = sub {    $self->{column} = 0;
655      $self->{set_nc} = sub {
656      my $self = shift;      my $self = shift;
657    
658      pop @{$self->{prev_char}};      my $char = '';
659      unshift @{$self->{prev_char}}, $self->{next_char};      if (defined $self->{next_nc}) {
660          $char = $self->{next_nc};
661      my $char;        delete $self->{next_nc};
662      if (defined $self->{next_next_char}) {        $self->{nc} = ord $char;
       $char = $self->{next_next_char};  
       delete $self->{next_next_char};  
663      } else {      } else {
664        $char = $input->getc;        $self->{char_buffer} = '';
665          $self->{char_buffer_pos} = 0;
666    
667          my $count = $input->manakai_read_until
668             ($self->{char_buffer}, qr/[^\x00\x0A\x0D]/, $self->{char_buffer_pos});
669          if ($count) {
670            $self->{line_prev} = $self->{line};
671            $self->{column_prev} = $self->{column};
672            $self->{column}++;
673            $self->{nc}
674                = ord substr ($self->{char_buffer}, $self->{char_buffer_pos}++, 1);
675            return;
676          }
677    
678          if ($input->read ($char, 1)) {
679            $self->{nc} = ord $char;
680          } else {
681            $self->{nc} = -1;
682            return;
683          }
684      }      }
     $self->{next_char} = -1 and return unless defined $char;  
     $self->{next_char} = ord $char;  
685    
686      ($self->{line_prev}, $self->{column_prev})      ($self->{line_prev}, $self->{column_prev})
687          = ($self->{line}, $self->{column});          = ($self->{line}, $self->{column});
688      $self->{column}++;      $self->{column}++;
689            
690      if ($self->{next_char} == 0x000A) { # LF      if ($self->{nc} == 0x000A) { # LF
691        !!!cp ('j1');        !!!cp ('j1');
692        $self->{line}++;        $self->{line}++;
693        $self->{column} = 0;        $self->{column} = 0;
694      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{nc} == 0x000D) { # CR
695        !!!cp ('j2');        !!!cp ('j2');
696        my $next = $input->getc;  ## TODO: support for abort/streaming
697        if (defined $next and $next ne "\x0A") {        my $next = '';
698          $self->{next_next_char} = $next;        if ($input->read ($next, 1) and $next ne "\x0A") {
699            $self->{next_nc} = $next;
700        }        }
701        $self->{next_char} = 0x000A; # LF # MUST        $self->{nc} = 0x000A; # LF # MUST
702        $self->{line}++;        $self->{line}++;
703        $self->{column} = 0;        $self->{column} = 0;
704      } 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  
705        !!!cp ('j4');        !!!cp ('j4');
706        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
707        $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 ('j5');  
       !!!parse-error (type => 'control char', level => $self->{must_level});  
 ## TODO: error type documentation  
708      }      }
709    };    };
710    $self->{prev_char} = [-1, -1, -1];  
711    $self->{next_char} = -1;    $self->{read_until} = sub {
712        #my ($scalar, $specials_range, $offset) = @_;
713        return 0 if defined $self->{next_nc};
714    
715        my $pattern = qr/[^$_[1]\x00\x0A\x0D]/;
716        my $offset = $_[2] || 0;
717    
718        if ($self->{char_buffer_pos} < length $self->{char_buffer}) {
719          pos ($self->{char_buffer}) = $self->{char_buffer_pos};
720          if ($self->{char_buffer} =~ /\G(?>$pattern)+/) {
721            substr ($_[0], $offset)
722                = substr ($self->{char_buffer}, $-[0], $+[0] - $-[0]);
723            my $count = $+[0] - $-[0];
724            if ($count) {
725              $self->{column} += $count;
726              $self->{char_buffer_pos} += $count;
727              $self->{line_prev} = $self->{line};
728              $self->{column_prev} = $self->{column} - 1;
729              $self->{nc} = -1;
730            }
731            return $count;
732          } else {
733            return 0;
734          }
735        } else {
736          my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
737          if ($count) {
738            $self->{column} += $count;
739            $self->{line_prev} = $self->{line};
740            $self->{column_prev} = $self->{column} - 1;
741            $self->{nc} = -1;
742          }
743          return $count;
744        }
745      }; # $self->{read_until}
746    
747    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
748      my (%opt) = @_;      my (%opt) = @_;
# Line 659  sub parse_char_stream ($$$;$) { Line 754  sub parse_char_stream ($$$;$) {
754      $onerror->(line => $self->{line}, column => $self->{column}, @_);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
755    };    };
756    
757      my $char_onerror = sub {
758        my (undef, $type, %opt) = @_;
759        !!!parse-error (layer => 'encode',
760                        line => $self->{line}, column => $self->{column} + 1,
761                        %opt, type => $type);
762      }; # $char_onerror
763    
764      if ($_[3]) {
765        $input = $_[3]->($input);
766        $input->onerror ($char_onerror);
767      } else {
768        $input->onerror ($char_onerror) unless defined $input->onerror;
769      }
770    
771    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
772    $self->_initialize_tree_constructor;    $self->_initialize_tree_constructor;
773    $self->_construct_tree;    $self->_construct_tree;
# Line 672  sub parse_char_stream ($$$;$) { Line 781  sub parse_char_stream ($$$;$) {
781  sub new ($) {  sub new ($) {
782    my $class = shift;    my $class = shift;
783    my $self = bless {    my $self = bless {
784      must_level => 'm',      level => {must => 'm',
785      should_level => 's',                should => 's',
786      good_level => 'w',                warn => 'w',
787      warn_level => 'w',                info => 'i',
788      info_level => 'i',                uncertain => 'u'},
     unsupported_level => 'u',  
789    }, $class;    }, $class;
790    $self->{set_next_char} = sub {    $self->{set_nc} = sub {
791      $self->{next_char} = -1;      $self->{nc} = -1;
792    };    };
793    $self->{parse_error} = sub {    $self->{parse_error} = sub {
794      #      #
# Line 707  sub RCDATA_CONTENT_MODEL () { CM_ENTITY Line 815  sub RCDATA_CONTENT_MODEL () { CM_ENTITY
815  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
816    
817  sub DATA_STATE () { 0 }  sub DATA_STATE () { 0 }
818  sub ENTITY_DATA_STATE () { 1 }  #sub ENTITY_DATA_STATE () { 1 }
819  sub TAG_OPEN_STATE () { 2 }  sub TAG_OPEN_STATE () { 2 }
820  sub CLOSE_TAG_OPEN_STATE () { 3 }  sub CLOSE_TAG_OPEN_STATE () { 3 }
821  sub TAG_NAME_STATE () { 4 }  sub TAG_NAME_STATE () { 4 }
# Line 718  sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 Line 826  sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8
826  sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }  sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
827  sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }  sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
828  sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }  sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
829  sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }  #sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
830  sub MARKUP_DECLARATION_OPEN_STATE () { 13 }  sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
831  sub COMMENT_START_STATE () { 14 }  sub COMMENT_START_STATE () { 14 }
832  sub COMMENT_START_DASH_STATE () { 15 }  sub COMMENT_START_DASH_STATE () { 15 }
# Line 741  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STAT Line 849  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STAT
849  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
850  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
851  sub SELF_CLOSING_START_TAG_STATE () { 34 }  sub SELF_CLOSING_START_TAG_STATE () { 34 }
852  sub CDATA_BLOCK_STATE () { 35 }  sub CDATA_SECTION_STATE () { 35 }
853    sub MD_HYPHEN_STATE () { 36 } # "markup declaration open state" in the spec
854    sub MD_DOCTYPE_STATE () { 37 } # "markup declaration open state" in the spec
855    sub MD_CDATA_STATE () { 38 } # "markup declaration open state" in the spec
856    sub CDATA_RCDATA_CLOSE_TAG_STATE () { 39 } # "close tag open state" in the spec
857    sub CDATA_SECTION_MSE1_STATE () { 40 } # "CDATA section state" in the spec
858    sub CDATA_SECTION_MSE2_STATE () { 41 } # "CDATA section state" in the spec
859    sub PUBLIC_STATE () { 42 } # "after DOCTYPE name state" in the spec
860    sub SYSTEM_STATE () { 43 } # "after DOCTYPE name state" in the spec
861    ## NOTE: "Entity data state", "entity in attribute value state", and
862    ## "consume a character reference" algorithm are jointly implemented
863    ## using the following six states:
864    sub ENTITY_STATE () { 44 }
865    sub ENTITY_HASH_STATE () { 45 }
866    sub NCR_NUM_STATE () { 46 }
867    sub HEXREF_X_STATE () { 47 }
868    sub HEXREF_HEX_STATE () { 48 }
869    sub ENTITY_NAME_STATE () { 49 }
870    sub PCDATA_STATE () { 50 } # "data state" in the spec
871    
872  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
873  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 794  sub IN_COLUMN_GROUP_IM () { 0b10 } Line 920  sub IN_COLUMN_GROUP_IM () { 0b10 }
920  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
921    my $self = shift;    my $self = shift;
922    $self->{state} = DATA_STATE; # MUST    $self->{state} = DATA_STATE; # MUST
923      #$self->{s_kwd}; # state keyword - initialized when used
924      #$self->{entity__value}; # initialized when used
925      #$self->{entity__match}; # initialized when used
926    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
927    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{ct}; # current token
928    undef $self->{current_attribute};    undef $self->{ca}; # current attribute
929    undef $self->{last_emitted_start_tag_name};    undef $self->{last_stag_name}; # last emitted start tag name
930    undef $self->{last_attribute_value_state};    #$self->{prev_state}; # initialized when used
931    delete $self->{self_closing};    delete $self->{self_closing};
932    $self->{char} = [];    $self->{char_buffer} = '';
933    # $self->{next_char}    $self->{char_buffer_pos} = 0;
934      $self->{nc} = -1; # next input character
935      #$self->{next_nc}
936    !!!next-input-character;    !!!next-input-character;
937    $self->{token} = [];    $self->{token} = [];
938    # $self->{escape}    # $self->{escape}
# Line 812  sub _initialize_tokenizer ($) { Line 943  sub _initialize_tokenizer ($) {
943  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
944  ##   ->{name} (DOCTYPE_TOKEN)  ##   ->{name} (DOCTYPE_TOKEN)
945  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
946  ##   ->{public_identifier} (DOCTYPE_TOKEN)  ##   ->{pubid} (DOCTYPE_TOKEN)
947  ##   ->{system_identifier} (DOCTYPE_TOKEN)  ##   ->{sysid} (DOCTYPE_TOKEN)
948  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
949  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
950  ##        ->{name}  ##        ->{name}
# Line 832  sub _initialize_tokenizer ($) { Line 963  sub _initialize_tokenizer ($) {
963  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
964  ## and removed from the list.  ## and removed from the list.
965    
966  ## NOTE: HTML5 "Writing HTML documents" section, applied to  ## TODO: Polytheistic slash SHOULD NOT be used. (Applied only to atheists.)
967  ## documents and not to user agents and conformance checkers,  ## (This requirement was dropped from HTML5 spec, unfortunately.)
 ## contains some requirements that are not detected by the  
 ## parsing algorithm:  
 ## - Some requirements on character encoding declarations. ## TODO  
 ## - "Elements MUST NOT contain content that their content model disallows."  
 ##   ... Some are parse error, some are not (will be reported by c.c.).  
 ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO  
 ## - Text (in elements, attributes, and comments) SHOULD NOT contain  
 ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)  
   
 ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot  
 ## be detected by the HTML5 parsing algorithm:  
 ## - Text,  
968    
969  sub _get_next_token ($) {  sub _get_next_token ($) {
970    my $self = shift;    my $self = shift;
971    
972    if ($self->{self_closing}) {    if ($self->{self_closing}) {
973      !!!parse-error (type => 'nestc', token => $self->{current_token});      !!!parse-error (type => 'nestc', token => $self->{ct});
974      ## NOTE: The |self_closing| flag is only set by start tag token.      ## NOTE: The |self_closing| flag is only set by start tag token.
975      ## 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
976      ## |current_token|.      ## |ct|.
977      delete $self->{self_closing};      delete $self->{self_closing};
978    }    }
979    
# Line 864  sub _get_next_token ($) { Line 983  sub _get_next_token ($) {
983    }    }
984    
985    A: {    A: {
986      if ($self->{state} == DATA_STATE) {      if ($self->{state} == PCDATA_STATE) {
987        if ($self->{next_char} == 0x0026) { # &        ## NOTE: Same as |DATA_STATE|, but only for |PCDATA| content model.
988    
989          if ($self->{nc} == 0x0026) { # &
990            !!!cp (0.1);
991            ## NOTE: In the spec, the tokenizer is switched to the
992            ## "entity data state".  In this implementation, the tokenizer
993            ## is switched to the |ENTITY_STATE|, which is an implementation
994            ## of the "consume a character reference" algorithm.
995            $self->{entity_add} = -1;
996            $self->{prev_state} = DATA_STATE;
997            $self->{state} = ENTITY_STATE;
998            !!!next-input-character;
999            redo A;
1000          } elsif ($self->{nc} == 0x003C) { # <
1001            !!!cp (0.2);
1002            $self->{state} = TAG_OPEN_STATE;
1003            !!!next-input-character;
1004            redo A;
1005          } elsif ($self->{nc} == -1) {
1006            !!!cp (0.3);
1007            !!!emit ({type => END_OF_FILE_TOKEN,
1008                      line => $self->{line}, column => $self->{column}});
1009            last A; ## TODO: ok?
1010          } else {
1011            !!!cp (0.4);
1012            #
1013          }
1014    
1015          # Anything else
1016          my $token = {type => CHARACTER_TOKEN,
1017                       data => chr $self->{nc},
1018                       line => $self->{line}, column => $self->{column},
1019                      };
1020          $self->{read_until}->($token->{data}, q[<&], length $token->{data});
1021    
1022          ## Stay in the state.
1023          !!!next-input-character;
1024          !!!emit ($token);
1025          redo A;
1026        } elsif ($self->{state} == DATA_STATE) {
1027          $self->{s_kwd} = '' unless defined $self->{s_kwd};
1028          if ($self->{nc} == 0x0026) { # &
1029            $self->{s_kwd} = '';
1030          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
1031              not $self->{escape}) {              not $self->{escape}) {
1032            !!!cp (1);            !!!cp (1);
1033            $self->{state} = ENTITY_DATA_STATE;            ## NOTE: In the spec, the tokenizer is switched to the
1034              ## "entity data state".  In this implementation, the tokenizer
1035              ## is switched to the |ENTITY_STATE|, which is an implementation
1036              ## of the "consume a character reference" algorithm.
1037              $self->{entity_add} = -1;
1038              $self->{prev_state} = DATA_STATE;
1039              $self->{state} = ENTITY_STATE;
1040            !!!next-input-character;            !!!next-input-character;
1041            redo A;            redo A;
1042          } else {          } else {
1043            !!!cp (2);            !!!cp (2);
1044            #            #
1045          }          }
1046        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{nc} == 0x002D) { # -
1047          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1048            unless ($self->{escape}) {            $self->{s_kwd} .= '-';
1049              if ($self->{prev_char}->[0] == 0x002D and # -            
1050                  $self->{prev_char}->[1] == 0x0021 and # !            if ($self->{s_kwd} eq '<!--') {
1051                  $self->{prev_char}->[2] == 0x003C) { # <              !!!cp (3);
1052                !!!cp (3);              $self->{escape} = 1; # unless $self->{escape};
1053                $self->{escape} = 1;              $self->{s_kwd} = '--';
1054              } else {              #
1055                !!!cp (4);            } elsif ($self->{s_kwd} eq '---') {
1056              }              !!!cp (4);
1057                $self->{s_kwd} = '--';
1058                #
1059            } else {            } else {
1060              !!!cp (5);              !!!cp (5);
1061                #
1062            }            }
1063          }          }
1064                    
1065          #          #
1066        } elsif ($self->{next_char} == 0x003C) { # <        } elsif ($self->{nc} == 0x0021) { # !
1067            if (length $self->{s_kwd}) {
1068              !!!cp (5.1);
1069              $self->{s_kwd} .= '!';
1070              #
1071            } else {
1072              !!!cp (5.2);
1073              #$self->{s_kwd} = '';
1074              #
1075            }
1076            #
1077          } elsif ($self->{nc} == 0x003C) { # <
1078          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
1079              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
1080               not $self->{escape})) {               not $self->{escape})) {
# Line 903  sub _get_next_token ($) { Line 1084  sub _get_next_token ($) {
1084            redo A;            redo A;
1085          } else {          } else {
1086            !!!cp (7);            !!!cp (7);
1087              $self->{s_kwd} = '';
1088            #            #
1089          }          }
1090        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1091          if ($self->{escape} and          if ($self->{escape} and
1092              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
1093            if ($self->{prev_char}->[0] == 0x002D and # -            if ($self->{s_kwd} eq '--') {
               $self->{prev_char}->[1] == 0x002D) { # -  
1094              !!!cp (8);              !!!cp (8);
1095              delete $self->{escape};              delete $self->{escape};
1096            } else {            } else {
# Line 919  sub _get_next_token ($) { Line 1100  sub _get_next_token ($) {
1100            !!!cp (10);            !!!cp (10);
1101          }          }
1102                    
1103            $self->{s_kwd} = '';
1104          #          #
1105        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1106          !!!cp (11);          !!!cp (11);
1107            $self->{s_kwd} = '';
1108          !!!emit ({type => END_OF_FILE_TOKEN,          !!!emit ({type => END_OF_FILE_TOKEN,
1109                    line => $self->{line}, column => $self->{column}});                    line => $self->{line}, column => $self->{column}});
1110          last A; ## TODO: ok?          last A; ## TODO: ok?
1111        } else {        } else {
1112          !!!cp (12);          !!!cp (12);
1113            $self->{s_kwd} = '';
1114            #
1115        }        }
1116    
1117        # Anything else        # Anything else
1118        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
1119                     data => chr $self->{next_char},                     data => chr $self->{nc},
1120                     line => $self->{line}, column => $self->{column},                     line => $self->{line}, column => $self->{column},
1121                    };                    };
1122        ## Stay in the data state        if ($self->{read_until}->($token->{data}, q[-!<>&],
1123        !!!next-input-character;                                  length $token->{data})) {
1124            $self->{s_kwd} = '';
1125        !!!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  
1126    
1127        unless (defined $token) {        ## Stay in the data state.
1128          if ($self->{content_model} == PCDATA_CONTENT_MODEL) {
1129          !!!cp (13);          !!!cp (13);
1130          !!!emit ({type => CHARACTER_TOKEN, data => '&',          $self->{state} = PCDATA_STATE;
                   line => $l, column => $c,  
                  });  
1131        } else {        } else {
1132          !!!cp (14);          !!!cp (14);
1133          !!!emit ($token);          ## Stay in the state.
1134        }        }
1135          !!!next-input-character;
1136          !!!emit ($token);
1137        redo A;        redo A;
1138      } elsif ($self->{state} == TAG_OPEN_STATE) {      } elsif ($self->{state} == TAG_OPEN_STATE) {
1139        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1140          if ($self->{next_char} == 0x002F) { # /          if ($self->{nc} == 0x002F) { # /
1141            !!!cp (15);            !!!cp (15);
1142            !!!next-input-character;            !!!next-input-character;
1143            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
1144            redo A;            redo A;
1145            } elsif ($self->{nc} == 0x0021) { # !
1146              !!!cp (15.1);
1147              $self->{s_kwd} = '<' unless $self->{escape};
1148              #
1149          } else {          } else {
1150            !!!cp (16);            !!!cp (16);
1151            ## reconsume            #
           $self->{state} = DATA_STATE;  
   
           !!!emit ({type => CHARACTER_TOKEN, data => '<',  
                     line => $self->{line_prev},  
                     column => $self->{column_prev},  
                    });  
   
           redo A;  
1152          }          }
1153    
1154            ## reconsume
1155            $self->{state} = DATA_STATE;
1156            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1157                      line => $self->{line_prev},
1158                      column => $self->{column_prev},
1159                     });
1160            redo A;
1161        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
1162          if ($self->{next_char} == 0x0021) { # !          if ($self->{nc} == 0x0021) { # !
1163            !!!cp (17);            !!!cp (17);
1164            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
1165            !!!next-input-character;            !!!next-input-character;
1166            redo A;            redo A;
1167          } elsif ($self->{next_char} == 0x002F) { # /          } elsif ($self->{nc} == 0x002F) { # /
1168            !!!cp (18);            !!!cp (18);
1169            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
1170            !!!next-input-character;            !!!next-input-character;
1171            redo A;            redo A;
1172          } elsif (0x0041 <= $self->{next_char} and          } elsif (0x0041 <= $self->{nc} and
1173                   $self->{next_char} <= 0x005A) { # A..Z                   $self->{nc} <= 0x005A) { # A..Z
1174            !!!cp (19);            !!!cp (19);
1175            $self->{current_token}            $self->{ct}
1176              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
1177                 tag_name => chr ($self->{next_char} + 0x0020),                 tag_name => chr ($self->{nc} + 0x0020),
1178                 line => $self->{line_prev},                 line => $self->{line_prev},
1179                 column => $self->{column_prev}};                 column => $self->{column_prev}};
1180            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1181            !!!next-input-character;            !!!next-input-character;
1182            redo A;            redo A;
1183          } elsif (0x0061 <= $self->{next_char} and          } elsif (0x0061 <= $self->{nc} and
1184                   $self->{next_char} <= 0x007A) { # a..z                   $self->{nc} <= 0x007A) { # a..z
1185            !!!cp (20);            !!!cp (20);
1186            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{ct} = {type => START_TAG_TOKEN,
1187                                      tag_name => chr ($self->{next_char}),                                      tag_name => chr ($self->{nc}),
1188                                      line => $self->{line_prev},                                      line => $self->{line_prev},
1189                                      column => $self->{column_prev}};                                      column => $self->{column_prev}};
1190            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1191            !!!next-input-character;            !!!next-input-character;
1192            redo A;            redo A;
1193          } elsif ($self->{next_char} == 0x003E) { # >          } elsif ($self->{nc} == 0x003E) { # >
1194            !!!cp (21);            !!!cp (21);
1195            !!!parse-error (type => 'empty start tag',            !!!parse-error (type => 'empty start tag',
1196                            line => $self->{line_prev},                            line => $self->{line_prev},
# Line 1025  sub _get_next_token ($) { Line 1204  sub _get_next_token ($) {
1204                     });                     });
1205    
1206            redo A;            redo A;
1207          } elsif ($self->{next_char} == 0x003F) { # ?          } elsif ($self->{nc} == 0x003F) { # ?
1208            !!!cp (22);            !!!cp (22);
1209            !!!parse-error (type => 'pio',            !!!parse-error (type => 'pio',
1210                            line => $self->{line_prev},                            line => $self->{line_prev},
1211                            column => $self->{column_prev});                            column => $self->{column_prev});
1212            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1213            $self->{current_token} = {type => COMMENT_TOKEN, data => '',            $self->{ct} = {type => COMMENT_TOKEN, data => '',
1214                                      line => $self->{line_prev},                                      line => $self->{line_prev},
1215                                      column => $self->{column_prev},                                      column => $self->{column_prev},
1216                                     };                                     };
1217            ## $self->{next_char} is intentionally left as is            ## $self->{nc} is intentionally left as is
1218            redo A;            redo A;
1219          } else {          } else {
1220            !!!cp (23);            !!!cp (23);
# Line 1056  sub _get_next_token ($) { Line 1235  sub _get_next_token ($) {
1235          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1236        }        }
1237      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1238          ## NOTE: The "close tag open state" in the spec is implemented as
1239          ## |CLOSE_TAG_OPEN_STATE| and |CDATA_RCDATA_CLOSE_TAG_STATE|.
1240    
1241        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1242        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1243          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_stag_name}) {
1244              $self->{state} = CDATA_RCDATA_CLOSE_TAG_STATE;
1245            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            $self->{s_kwd} = '';
1246            my @next_char;            ## Reconsume.
1247            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...  
           }  
1248          } else {          } else {
1249            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1250              ## NOTE: See <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>.
1251            !!!cp (28);            !!!cp (28);
           # next-input-character is already done  
1252            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1253              ## Reconsume.
1254            !!!emit ({type => CHARACTER_TOKEN, data => '</',            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1255                      line => $l, column => $c,                      line => $l, column => $c,
1256                     });                     });
1257            redo A;            redo A;
1258          }          }
1259        }        }
1260          
1261        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{nc} and
1262            $self->{next_char} <= 0x005A) { # A..Z            $self->{nc} <= 0x005A) { # A..Z
1263          !!!cp (29);          !!!cp (29);
1264          $self->{current_token}          $self->{ct}
1265              = {type => END_TAG_TOKEN,              = {type => END_TAG_TOKEN,
1266                 tag_name => chr ($self->{next_char} + 0x0020),                 tag_name => chr ($self->{nc} + 0x0020),
1267                 line => $l, column => $c};                 line => $l, column => $c};
1268          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1269          !!!next-input-character;          !!!next-input-character;
1270          redo A;          redo A;
1271        } elsif (0x0061 <= $self->{next_char} and        } elsif (0x0061 <= $self->{nc} and
1272                 $self->{next_char} <= 0x007A) { # a..z                 $self->{nc} <= 0x007A) { # a..z
1273          !!!cp (30);          !!!cp (30);
1274          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{ct} = {type => END_TAG_TOKEN,
1275                                    tag_name => chr ($self->{next_char}),                                    tag_name => chr ($self->{nc}),
1276                                    line => $l, column => $c};                                    line => $l, column => $c};
1277          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1278          !!!next-input-character;          !!!next-input-character;
1279          redo A;          redo A;
1280        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1281          !!!cp (31);          !!!cp (31);
1282          !!!parse-error (type => 'empty end tag',          !!!parse-error (type => 'empty end tag',
1283                          line => $self->{line_prev}, ## "<" in "</>"                          line => $self->{line_prev}, ## "<" in "</>"
# Line 1146  sub _get_next_token ($) { Line 1285  sub _get_next_token ($) {
1285          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1286          !!!next-input-character;          !!!next-input-character;
1287          redo A;          redo A;
1288        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1289          !!!cp (32);          !!!cp (32);
1290          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1291          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
# Line 1161  sub _get_next_token ($) { Line 1300  sub _get_next_token ($) {
1300          !!!cp (33);          !!!cp (33);
1301          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1302          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1303          $self->{current_token} = {type => COMMENT_TOKEN, data => '',          $self->{ct} = {type => COMMENT_TOKEN, data => '',
1304                                    line => $self->{line_prev}, # "<" of "</"                                    line => $self->{line_prev}, # "<" of "</"
1305                                    column => $self->{column_prev} - 1,                                    column => $self->{column_prev} - 1,
1306                                   };                                   };
1307          ## $self->{next_char} is intentionally left as is          ## NOTE: $self->{nc} is intentionally left as is.
1308          redo A;          ## Although the "anything else" case of the spec not explicitly
1309            ## states that the next input character is to be reconsumed,
1310            ## it will be included to the |data| of the comment token
1311            ## generated from the bogus end tag, as defined in the
1312            ## "bogus comment state" entry.
1313            redo A;
1314          }
1315        } elsif ($self->{state} == CDATA_RCDATA_CLOSE_TAG_STATE) {
1316          my $ch = substr $self->{last_stag_name}, length $self->{s_kwd}, 1;
1317          if (length $ch) {
1318            my $CH = $ch;
1319            $ch =~ tr/a-z/A-Z/;
1320            my $nch = chr $self->{nc};
1321            if ($nch eq $ch or $nch eq $CH) {
1322              !!!cp (24);
1323              ## Stay in the state.
1324              $self->{s_kwd} .= $nch;
1325              !!!next-input-character;
1326              redo A;
1327            } else {
1328              !!!cp (25);
1329              $self->{state} = DATA_STATE;
1330              ## Reconsume.
1331              !!!emit ({type => CHARACTER_TOKEN,
1332                        data => '</' . $self->{s_kwd},
1333                        line => $self->{line_prev},
1334                        column => $self->{column_prev} - 1 - length $self->{s_kwd},
1335                       });
1336              redo A;
1337            }
1338          } else { # after "<{tag-name}"
1339            unless ({
1340                     0x0009 => 1, # HT
1341                     0x000A => 1, # LF
1342                     0x000B => 1, # VT
1343                     0x000C => 1, # FF
1344                     0x0020 => 1, # SP
1345                     0x003E => 1, # >
1346                     0x002F => 1, # /
1347                     -1 => 1, # EOF
1348                    }->{$self->{nc}}) {
1349              !!!cp (26);
1350              ## Reconsume.
1351              $self->{state} = DATA_STATE;
1352              !!!emit ({type => CHARACTER_TOKEN,
1353                        data => '</' . $self->{s_kwd},
1354                        line => $self->{line_prev},
1355                        column => $self->{column_prev} - 1 - length $self->{s_kwd},
1356                       });
1357              redo A;
1358            } else {
1359              !!!cp (27);
1360              $self->{ct}
1361                  = {type => END_TAG_TOKEN,
1362                     tag_name => $self->{last_stag_name},
1363                     line => $self->{line_prev},
1364                     column => $self->{column_prev} - 1 - length $self->{s_kwd}};
1365              $self->{state} = TAG_NAME_STATE;
1366              ## Reconsume.
1367              redo A;
1368            }
1369        }        }
1370      } elsif ($self->{state} == TAG_NAME_STATE) {      } elsif ($self->{state} == TAG_NAME_STATE) {
1371        if ($self->{next_char} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1372            $self->{next_char} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1373            $self->{next_char} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
1374            $self->{next_char} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1375            $self->{next_char} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
1376          !!!cp (34);          !!!cp (34);
1377          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1378          !!!next-input-character;          !!!next-input-character;
1379          redo A;          redo A;
1380        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1381          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1382            !!!cp (35);            !!!cp (35);
1383            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1384          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1385            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1386            #if ($self->{current_token}->{attributes}) {            #if ($self->{ct}->{attributes}) {
1387            #  ## NOTE: This should never be reached.            #  ## NOTE: This should never be reached.
1388            #  !!! cp (36);            #  !!! cp (36);
1389            #  !!! parse-error (type => 'end tag attribute');            #  !!! parse-error (type => 'end tag attribute');
# Line 1192  sub _get_next_token ($) { Line 1391  sub _get_next_token ($) {
1391              !!!cp (37);              !!!cp (37);
1392            #}            #}
1393          } else {          } else {
1394            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1395          }          }
1396          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1397          !!!next-input-character;          !!!next-input-character;
1398    
1399          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1400    
1401          redo A;          redo A;
1402        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{nc} and
1403                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1404          !!!cp (38);          !!!cp (38);
1405          $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);          $self->{ct}->{tag_name} .= chr ($self->{nc} + 0x0020);
1406            # start tag or end tag            # start tag or end tag
1407          ## Stay in this state          ## Stay in this state
1408          !!!next-input-character;          !!!next-input-character;
1409          redo A;          redo A;
1410        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1411          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1412          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1413            !!!cp (39);            !!!cp (39);
1414            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1415          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1416            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1417            #if ($self->{current_token}->{attributes}) {            #if ($self->{ct}->{attributes}) {
1418            #  ## NOTE: This state should never be reached.            #  ## NOTE: This state should never be reached.
1419            #  !!! cp (40);            #  !!! cp (40);
1420            #  !!! parse-error (type => 'end tag attribute');            #  !!! parse-error (type => 'end tag attribute');
# Line 1223  sub _get_next_token ($) { Line 1422  sub _get_next_token ($) {
1422              !!!cp (41);              !!!cp (41);
1423            #}            #}
1424          } else {          } else {
1425            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1426          }          }
1427          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1428          # reconsume          # reconsume
1429    
1430          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1431    
1432          redo A;          redo A;
1433        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1434          !!!cp (42);          !!!cp (42);
1435          $self->{state} = SELF_CLOSING_START_TAG_STATE;          $self->{state} = SELF_CLOSING_START_TAG_STATE;
1436          !!!next-input-character;          !!!next-input-character;
1437          redo A;          redo A;
1438        } else {        } else {
1439          !!!cp (44);          !!!cp (44);
1440          $self->{current_token}->{tag_name} .= chr $self->{next_char};          $self->{ct}->{tag_name} .= chr $self->{nc};
1441            # start tag or end tag            # start tag or end tag
1442          ## Stay in the state          ## Stay in the state
1443          !!!next-input-character;          !!!next-input-character;
1444          redo A;          redo A;
1445        }        }
1446      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1447        if ($self->{next_char} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1448            $self->{next_char} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1449            $self->{next_char} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
1450            $self->{next_char} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1451            $self->{next_char} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
1452          !!!cp (45);          !!!cp (45);
1453          ## Stay in the state          ## Stay in the state
1454          !!!next-input-character;          !!!next-input-character;
1455          redo A;          redo A;
1456        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1457          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1458            !!!cp (46);            !!!cp (46);
1459            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1460          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1461            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1462            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1463              !!!cp (47);              !!!cp (47);
1464              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1465            } else {            } else {
1466              !!!cp (48);              !!!cp (48);
1467            }            }
1468          } else {          } else {
1469            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1470          }          }
1471          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1472          !!!next-input-character;          !!!next-input-character;
1473    
1474          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1475    
1476          redo A;          redo A;
1477        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{nc} and
1478                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1479          !!!cp (49);          !!!cp (49);
1480          $self->{current_attribute}          $self->{ca}
1481              = {name => chr ($self->{next_char} + 0x0020),              = {name => chr ($self->{nc} + 0x0020),
1482                 value => '',                 value => '',
1483                 line => $self->{line}, column => $self->{column}};                 line => $self->{line}, column => $self->{column}};
1484          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1485          !!!next-input-character;          !!!next-input-character;
1486          redo A;          redo A;
1487        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1488          !!!cp (50);          !!!cp (50);
1489          $self->{state} = SELF_CLOSING_START_TAG_STATE;          $self->{state} = SELF_CLOSING_START_TAG_STATE;
1490          !!!next-input-character;          !!!next-input-character;
1491          redo A;          redo A;
1492        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1493          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1494          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1495            !!!cp (52);            !!!cp (52);
1496            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1497          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1498            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1499            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1500              !!!cp (53);              !!!cp (53);
1501              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1502            } else {            } else {
1503              !!!cp (54);              !!!cp (54);
1504            }            }
1505          } else {          } else {
1506            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1507          }          }
1508          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1509          # reconsume          # reconsume
1510    
1511          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1512    
1513          redo A;          redo A;
1514        } else {        } else {
# Line 1317  sub _get_next_token ($) { Line 1516  sub _get_next_token ($) {
1516               0x0022 => 1, # "               0x0022 => 1, # "
1517               0x0027 => 1, # '               0x0027 => 1, # '
1518               0x003D => 1, # =               0x003D => 1, # =
1519              }->{$self->{next_char}}) {              }->{$self->{nc}}) {
1520            !!!cp (55);            !!!cp (55);
1521            !!!parse-error (type => 'bad attribute name');            !!!parse-error (type => 'bad attribute name');
1522          } else {          } else {
1523            !!!cp (56);            !!!cp (56);
1524          }          }
1525          $self->{current_attribute}          $self->{ca}
1526              = {name => chr ($self->{next_char}),              = {name => chr ($self->{nc}),
1527                 value => '',                 value => '',
1528                 line => $self->{line}, column => $self->{column}};                 line => $self->{line}, column => $self->{column}};
1529          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
# Line 1333  sub _get_next_token ($) { Line 1532  sub _get_next_token ($) {
1532        }        }
1533      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
1534        my $before_leave = sub {        my $before_leave = sub {
1535          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{ct}->{attributes} # start tag or end tag
1536              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{ca}->{name}}) { # MUST
1537            !!!cp (57);            !!!cp (57);
1538            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});            !!!parse-error (type => 'duplicate attribute', text => $self->{ca}->{name}, line => $self->{ca}->{line}, column => $self->{ca}->{column});
1539            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{ca} # MUST
1540          } else {          } else {
1541            !!!cp (58);            !!!cp (58);
1542            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{ct}->{attributes}->{$self->{ca}->{name}}
1543              = $self->{current_attribute};              = $self->{ca};
1544          }          }
1545        }; # $before_leave        }; # $before_leave
1546    
1547        if ($self->{next_char} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1548            $self->{next_char} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1549            $self->{next_char} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
1550            $self->{next_char} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1551            $self->{next_char} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
1552          !!!cp (59);          !!!cp (59);
1553          $before_leave->();          $before_leave->();
1554          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1555          !!!next-input-character;          !!!next-input-character;
1556          redo A;          redo A;
1557        } elsif ($self->{next_char} == 0x003D) { # =        } elsif ($self->{nc} == 0x003D) { # =
1558          !!!cp (60);          !!!cp (60);
1559          $before_leave->();          $before_leave->();
1560          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1561          !!!next-input-character;          !!!next-input-character;
1562          redo A;          redo A;
1563        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1564          $before_leave->();          $before_leave->();
1565          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1566            !!!cp (61);            !!!cp (61);
1567            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1568          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1569            !!!cp (62);            !!!cp (62);
1570            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1571            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1572              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1573            }            }
1574          } else {          } else {
1575            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1576          }          }
1577          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1578          !!!next-input-character;          !!!next-input-character;
1579    
1580          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1581    
1582          redo A;          redo A;
1583        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{nc} and
1584                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1585          !!!cp (63);          !!!cp (63);
1586          $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);          $self->{ca}->{name} .= chr ($self->{nc} + 0x0020);
1587          ## Stay in the state          ## Stay in the state
1588          !!!next-input-character;          !!!next-input-character;
1589          redo A;          redo A;
1590        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1591          !!!cp (64);          !!!cp (64);
1592          $before_leave->();          $before_leave->();
1593          $self->{state} = SELF_CLOSING_START_TAG_STATE;          $self->{state} = SELF_CLOSING_START_TAG_STATE;
1594          !!!next-input-character;          !!!next-input-character;
1595          redo A;          redo A;
1596        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1597          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1598          $before_leave->();          $before_leave->();
1599          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1600            !!!cp (66);            !!!cp (66);
1601            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1602          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1603            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1604            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1605              !!!cp (67);              !!!cp (67);
1606              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1607            } else {            } else {
# Line 1410  sub _get_next_token ($) { Line 1609  sub _get_next_token ($) {
1609              !!!cp (68);              !!!cp (68);
1610            }            }
1611          } else {          } else {
1612            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1613          }          }
1614          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1615          # reconsume          # reconsume
1616    
1617          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1618    
1619          redo A;          redo A;
1620        } else {        } else {
1621          if ($self->{next_char} == 0x0022 or # "          if ($self->{nc} == 0x0022 or # "
1622              $self->{next_char} == 0x0027) { # '              $self->{nc} == 0x0027) { # '
1623            !!!cp (69);            !!!cp (69);
1624            !!!parse-error (type => 'bad attribute name');            !!!parse-error (type => 'bad attribute name');
1625          } else {          } else {
1626            !!!cp (70);            !!!cp (70);
1627          }          }
1628          $self->{current_attribute}->{name} .= chr ($self->{next_char});          $self->{ca}->{name} .= chr ($self->{nc});
1629          ## Stay in the state          ## Stay in the state
1630          !!!next-input-character;          !!!next-input-character;
1631          redo A;          redo A;
1632        }        }
1633      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1634        if ($self->{next_char} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1635            $self->{next_char} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1636            $self->{next_char} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
1637            $self->{next_char} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1638            $self->{next_char} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
1639          !!!cp (71);          !!!cp (71);
1640          ## Stay in the state          ## Stay in the state
1641          !!!next-input-character;          !!!next-input-character;
1642          redo A;          redo A;
1643        } elsif ($self->{next_char} == 0x003D) { # =        } elsif ($self->{nc} == 0x003D) { # =
1644          !!!cp (72);          !!!cp (72);
1645          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1646          !!!next-input-character;          !!!next-input-character;
1647          redo A;          redo A;
1648        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1649          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1650            !!!cp (73);            !!!cp (73);
1651            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1652          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1653            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1654            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1655              !!!cp (74);              !!!cp (74);
1656              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1657            } else {            } else {
# Line 1460  sub _get_next_token ($) { Line 1659  sub _get_next_token ($) {
1659              !!!cp (75);              !!!cp (75);
1660            }            }
1661          } else {          } else {
1662            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1663          }          }
1664          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1665          !!!next-input-character;          !!!next-input-character;
1666    
1667          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1668    
1669          redo A;          redo A;
1670        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{nc} and
1671                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1672          !!!cp (76);          !!!cp (76);
1673          $self->{current_attribute}          $self->{ca}
1674              = {name => chr ($self->{next_char} + 0x0020),              = {name => chr ($self->{nc} + 0x0020),
1675                 value => '',                 value => '',
1676                 line => $self->{line}, column => $self->{column}};                 line => $self->{line}, column => $self->{column}};
1677          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1678          !!!next-input-character;          !!!next-input-character;
1679          redo A;          redo A;
1680        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1681          !!!cp (77);          !!!cp (77);
1682          $self->{state} = SELF_CLOSING_START_TAG_STATE;          $self->{state} = SELF_CLOSING_START_TAG_STATE;
1683          !!!next-input-character;          !!!next-input-character;
1684          redo A;          redo A;
1685        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1686          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1687          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1688            !!!cp (79);            !!!cp (79);
1689            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1690          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1691            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1692            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1693              !!!cp (80);              !!!cp (80);
1694              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1695            } else {            } else {
# Line 1498  sub _get_next_token ($) { Line 1697  sub _get_next_token ($) {
1697              !!!cp (81);              !!!cp (81);
1698            }            }
1699          } else {          } else {
1700            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1701          }          }
1702          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1703          # reconsume          # reconsume
1704    
1705          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1706    
1707          redo A;          redo A;
1708        } else {        } else {
1709          !!!cp (82);          if ($self->{nc} == 0x0022 or # "
1710          $self->{current_attribute}              $self->{nc} == 0x0027) { # '
1711              = {name => chr ($self->{next_char}),            !!!cp (78);
1712              !!!parse-error (type => 'bad attribute name');
1713            } else {
1714              !!!cp (82);
1715            }
1716            $self->{ca}
1717                = {name => chr ($self->{nc}),
1718                 value => '',                 value => '',
1719                 line => $self->{line}, column => $self->{column}};                 line => $self->{line}, column => $self->{column}};
1720          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
# Line 1517  sub _get_next_token ($) { Line 1722  sub _get_next_token ($) {
1722          redo A;                  redo A;        
1723        }        }
1724      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1725        if ($self->{next_char} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1726            $self->{next_char} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1727            $self->{next_char} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
1728            $self->{next_char} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1729            $self->{next_char} == 0x0020) { # SP                  $self->{nc} == 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          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          !!!parse-error (type => 'empty unquoted attribute value');
1751            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 1555  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 1578  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 1625  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 1667  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 ($self->{nc} == 0x0009 or # HT
1911            $self->{next_char} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1912            $self->{next_char} == 0x000B or # HT            $self->{nc} == 0x000B or # HT
1913            $self->{next_char} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1914            $self->{next_char} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
1915          !!!cp (107);          !!!cp (107);
1916          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1917          !!!next-input-character;          !!!next-input-character;
1918          redo A;          redo A;
1919        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{nc} == 0x0026) { # &
1920          !!!cp (108);          !!!cp (108);
1921          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1922          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1923            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1924            ## implementation of the "consume a character reference" algorithm.
1925            $self->{entity_add} = -1;
1926            $self->{prev_state} = $self->{state};
1927            $self->{state} = ENTITY_STATE;
1928          !!!next-input-character;          !!!next-input-character;
1929          redo A;          redo A;
1930        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1931          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1932            !!!cp (109);            !!!cp (109);
1933            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1934          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1935            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1936            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1937              !!!cp (110);              !!!cp (110);
1938              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1939            } else {            } else {
# Line 1712  sub _get_next_token ($) { Line 1941  sub _get_next_token ($) {
1941              !!!cp (111);              !!!cp (111);
1942            }            }
1943          } else {          } else {
1944            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1945          }          }
1946          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1947          !!!next-input-character;          !!!next-input-character;
1948    
1949          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1950    
1951          redo A;          redo A;
1952        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1953          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1954          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1955            !!!cp (112);            !!!cp (112);
1956            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1957          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1958            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1959            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1960              !!!cp (113);              !!!cp (113);
1961              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1962            } else {            } else {
# Line 1735  sub _get_next_token ($) { Line 1964  sub _get_next_token ($) {
1964              !!!cp (114);              !!!cp (114);
1965            }            }
1966          } else {          } else {
1967            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1968          }          }
1969          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1970          ## reconsume          ## reconsume
1971    
1972          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1973    
1974          redo A;          redo A;
1975        } else {        } else {
# Line 1748  sub _get_next_token ($) { Line 1977  sub _get_next_token ($) {
1977               0x0022 => 1, # "               0x0022 => 1, # "
1978               0x0027 => 1, # '               0x0027 => 1, # '
1979               0x003D => 1, # =               0x003D => 1, # =
1980              }->{$self->{next_char}}) {              }->{$self->{nc}}) {
1981            !!!cp (115);            !!!cp (115);
1982            !!!parse-error (type => 'bad attribute value');            !!!parse-error (type => 'bad attribute value');
1983          } else {          } else {
1984            !!!cp (116);            !!!cp (116);
1985          }          }
1986          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{ca}->{value} .= chr ($self->{nc});
1987            $self->{read_until}->($self->{ca}->{value},
1988                                  q["'=& >],
1989                                  length $self->{ca}->{value});
1990    
1991          ## Stay in the state          ## Stay in the state
1992          !!!next-input-character;          !!!next-input-character;
1993          redo A;          redo A;
1994        }        }
     } 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;  
1995      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1996        if ($self->{next_char} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1997            $self->{next_char} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1998            $self->{next_char} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
1999            $self->{next_char} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
2000            $self->{next_char} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
2001          !!!cp (118);          !!!cp (118);
2002          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
2003          !!!next-input-character;          !!!next-input-character;
2004          redo A;          redo A;
2005        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2006          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
2007            !!!cp (119);            !!!cp (119);
2008            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
2009          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
2010            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
2011            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
2012              !!!cp (120);              !!!cp (120);
2013              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
2014            } else {            } else {
# Line 1805  sub _get_next_token ($) { Line 2016  sub _get_next_token ($) {
2016              !!!cp (121);              !!!cp (121);
2017            }            }
2018          } else {          } else {
2019            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
2020          }          }
2021          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2022          !!!next-input-character;          !!!next-input-character;
2023    
2024          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
2025    
2026          redo A;          redo A;
2027        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
2028          !!!cp (122);          !!!cp (122);
2029          $self->{state} = SELF_CLOSING_START_TAG_STATE;          $self->{state} = SELF_CLOSING_START_TAG_STATE;
2030          !!!next-input-character;          !!!next-input-character;
2031          redo A;          redo A;
2032        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2033          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
2034          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
2035            !!!cp (122.3);            !!!cp (122.3);
2036            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
2037          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
2038            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
2039              !!!cp (122.1);              !!!cp (122.1);
2040              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
2041            } else {            } else {
# Line 1832  sub _get_next_token ($) { Line 2043  sub _get_next_token ($) {
2043              !!!cp (122.2);              !!!cp (122.2);
2044            }            }
2045          } else {          } else {
2046            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
2047          }          }
2048          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2049          ## Reconsume.          ## Reconsume.
2050          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
2051          redo A;          redo A;
2052        } else {        } else {
2053          !!!cp ('124.1');          !!!cp ('124.1');
# Line 1846  sub _get_next_token ($) { Line 2057  sub _get_next_token ($) {
2057          redo A;          redo A;
2058        }        }
2059      } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {      } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
2060        if ($self->{next_char} == 0x003E) { # >        if ($self->{nc} == 0x003E) { # >
2061          if ($self->{current_token}->{type} == END_TAG_TOKEN) {          if ($self->{ct}->{type} == END_TAG_TOKEN) {
2062            !!!cp ('124.2');            !!!cp ('124.2');
2063            !!!parse-error (type => 'nestc', token => $self->{current_token});            !!!parse-error (type => 'nestc', token => $self->{ct});
2064            ## TODO: Different type than slash in start tag            ## TODO: Different type than slash in start tag
2065            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
2066            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
2067              !!!cp ('124.4');              !!!cp ('124.4');
2068              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
2069            } else {            } else {
# Line 1867  sub _get_next_token ($) { Line 2078  sub _get_next_token ($) {
2078          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2079          !!!next-input-character;          !!!next-input-character;
2080    
2081          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
2082    
2083          redo A;          redo A;
2084        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2085          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
2086          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
2087            !!!cp (124.7);            !!!cp (124.7);
2088            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
2089          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
2090            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
2091              !!!cp (124.5);              !!!cp (124.5);
2092              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
2093            } else {            } else {
# Line 1884  sub _get_next_token ($) { Line 2095  sub _get_next_token ($) {
2095              !!!cp (124.6);              !!!cp (124.6);
2096            }            }
2097          } else {          } else {
2098            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
2099          }          }
2100          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2101          ## Reconsume.          ## Reconsume.
2102          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
2103          redo A;          redo A;
2104        } else {        } else {
2105          !!!cp ('124.4');          !!!cp ('124.4');
# Line 1900  sub _get_next_token ($) { Line 2111  sub _get_next_token ($) {
2111        }        }
2112      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
2113        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
         
       ## NOTE: Set by the previous state  
       #my $token = {type => COMMENT_TOKEN, data => ''};  
2114    
2115        BC: {        ## NOTE: Unlike spec's "bogus comment state", this implementation
2116          if ($self->{next_char} == 0x003E) { # >        ## consumes characters one-by-one basis.
2117            !!!cp (124);        
2118            $self->{state} = DATA_STATE;        if ($self->{nc} == 0x003E) { # >
2119            !!!next-input-character;          !!!cp (124);
2120            $self->{state} = DATA_STATE;
2121            !!!emit ($self->{current_token}); # comment          !!!next-input-character;
   
           redo A;  
         } elsif ($self->{next_char} == -1) {  
           !!!cp (125);  
           $self->{state} = DATA_STATE;  
           ## reconsume  
2122    
2123            !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2124            redo A;
2125          } elsif ($self->{nc} == -1) {
2126            !!!cp (125);
2127            $self->{state} = DATA_STATE;
2128            ## reconsume
2129    
2130            redo A;          !!!emit ($self->{ct}); # comment
2131          } else {          redo A;
2132            !!!cp (126);        } else {
2133            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment          !!!cp (126);
2134            !!!next-input-character;          $self->{ct}->{data} .= chr ($self->{nc}); # comment
2135            redo BC;          $self->{read_until}->($self->{ct}->{data},
2136          }                                q[>],
2137        } # BC                                length $self->{ct}->{data});
2138    
2139        die "$0: _get_next_token: unexpected case [BC]";          ## Stay in the state.
2140            !!!next-input-character;
2141            redo A;
2142          }
2143      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2144        ## (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};  
2145                
2146        if ($self->{next_char} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2147            !!!cp (133);
2148            $self->{state} = MD_HYPHEN_STATE;
2149          !!!next-input-character;          !!!next-input-character;
2150          push @next_char, $self->{next_char};          redo A;
2151          if ($self->{next_char} == 0x002D) { # -        } elsif ($self->{nc} == 0x0044 or # D
2152            !!!cp (127);                 $self->{nc} == 0x0064) { # d
2153            $self->{current_token} = {type => COMMENT_TOKEN, data => '',          ## ASCII case-insensitive.
2154                                      line => $l, column => $c,          !!!cp (130);
2155                                     };          $self->{state} = MD_DOCTYPE_STATE;
2156            $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  
2157          !!!next-input-character;          !!!next-input-character;
2158          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);  
         }  
2159        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2160                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2161                 $self->{next_char} == 0x005B) { # [                 $self->{nc} == 0x005B) { # [
2162            !!!cp (135.4);                
2163            $self->{state} = MD_CDATA_STATE;
2164            $self->{s_kwd} = '[';
2165          !!!next-input-character;          !!!next-input-character;
2166          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);  
         }  
2167        } else {        } else {
2168          !!!cp (136);          !!!cp (136);
2169        }        }
2170    
2171        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment',
2172        $self->{next_char} = shift @next_char;                        line => $self->{line_prev},
2173        !!!back-next-input-character (@next_char);                        column => $self->{column_prev} - 1);
2174          ## Reconsume.
2175        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2176        $self->{current_token} = {type => COMMENT_TOKEN, data => '',        $self->{ct} = {type => COMMENT_TOKEN, data => '',
2177                                  line => $l, column => $c,                                  line => $self->{line_prev},
2178                                    column => $self->{column_prev} - 1,
2179                                 };                                 };
2180        redo A;        redo A;
2181              } elsif ($self->{state} == MD_HYPHEN_STATE) {
2182        ## ISSUE: typos in spec: chacacters, is is a parse error        if ($self->{nc} == 0x002D) { # -
2183        ## 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);
2184            $self->{ct} = {type => COMMENT_TOKEN, data => '',
2185                                      line => $self->{line_prev},
2186                                      column => $self->{column_prev} - 2,
2187                                     };
2188            $self->{state} = COMMENT_START_STATE;
2189            !!!next-input-character;
2190            redo A;
2191          } else {
2192            !!!cp (128);
2193            !!!parse-error (type => 'bogus comment',
2194                            line => $self->{line_prev},
2195                            column => $self->{column_prev} - 2);
2196            $self->{state} = BOGUS_COMMENT_STATE;
2197            ## Reconsume.
2198            $self->{ct} = {type => COMMENT_TOKEN,
2199                                      data => '-',
2200                                      line => $self->{line_prev},
2201                                      column => $self->{column_prev} - 2,
2202                                     };
2203            redo A;
2204          }
2205        } elsif ($self->{state} == MD_DOCTYPE_STATE) {
2206          ## ASCII case-insensitive.
2207          if ($self->{nc} == [
2208                undef,
2209                0x004F, # O
2210                0x0043, # C
2211                0x0054, # T
2212                0x0059, # Y
2213                0x0050, # P
2214              ]->[length $self->{s_kwd}] or
2215              $self->{nc} == [
2216                undef,
2217                0x006F, # o
2218                0x0063, # c
2219                0x0074, # t
2220                0x0079, # y
2221                0x0070, # p
2222              ]->[length $self->{s_kwd}]) {
2223            !!!cp (131);
2224            ## Stay in the state.
2225            $self->{s_kwd} .= chr $self->{nc};
2226            !!!next-input-character;
2227            redo A;
2228          } elsif ((length $self->{s_kwd}) == 6 and
2229                   ($self->{nc} == 0x0045 or # E
2230                    $self->{nc} == 0x0065)) { # e
2231            !!!cp (129);
2232            $self->{state} = DOCTYPE_STATE;
2233            $self->{ct} = {type => DOCTYPE_TOKEN,
2234                                      quirks => 1,
2235                                      line => $self->{line_prev},
2236                                      column => $self->{column_prev} - 7,
2237                                     };
2238            !!!next-input-character;
2239            redo A;
2240          } else {
2241            !!!cp (132);        
2242            !!!parse-error (type => 'bogus comment',
2243                            line => $self->{line_prev},
2244                            column => $self->{column_prev} - 1 - length $self->{s_kwd});
2245            $self->{state} = BOGUS_COMMENT_STATE;
2246            ## Reconsume.
2247            $self->{ct} = {type => COMMENT_TOKEN,
2248                                      data => $self->{s_kwd},
2249                                      line => $self->{line_prev},
2250                                      column => $self->{column_prev} - 1 - length $self->{s_kwd},
2251                                     };
2252            redo A;
2253          }
2254        } elsif ($self->{state} == MD_CDATA_STATE) {
2255          if ($self->{nc} == {
2256                '[' => 0x0043, # C
2257                '[C' => 0x0044, # D
2258                '[CD' => 0x0041, # A
2259                '[CDA' => 0x0054, # T
2260                '[CDAT' => 0x0041, # A
2261              }->{$self->{s_kwd}}) {
2262            !!!cp (135.1);
2263            ## Stay in the state.
2264            $self->{s_kwd} .= chr $self->{nc};
2265            !!!next-input-character;
2266            redo A;
2267          } elsif ($self->{s_kwd} eq '[CDATA' and
2268                   $self->{nc} == 0x005B) { # [
2269            !!!cp (135.2);
2270            $self->{ct} = {type => CHARACTER_TOKEN,
2271                                      data => '',
2272                                      line => $self->{line_prev},
2273                                      column => $self->{column_prev} - 7};
2274            $self->{state} = CDATA_SECTION_STATE;
2275            !!!next-input-character;
2276            redo A;
2277          } else {
2278            !!!cp (135.3);
2279            !!!parse-error (type => 'bogus comment',
2280                            line => $self->{line_prev},
2281                            column => $self->{column_prev} - 1 - length $self->{s_kwd});
2282            $self->{state} = BOGUS_COMMENT_STATE;
2283            ## Reconsume.
2284            $self->{ct} = {type => COMMENT_TOKEN,
2285                                      data => $self->{s_kwd},
2286                                      line => $self->{line_prev},
2287                                      column => $self->{column_prev} - 1 - length $self->{s_kwd},
2288                                     };
2289            redo A;
2290          }
2291      } elsif ($self->{state} == COMMENT_START_STATE) {      } elsif ($self->{state} == COMMENT_START_STATE) {
2292        if ($self->{next_char} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2293          !!!cp (137);          !!!cp (137);
2294          $self->{state} = COMMENT_START_DASH_STATE;          $self->{state} = COMMENT_START_DASH_STATE;
2295          !!!next-input-character;          !!!next-input-character;
2296          redo A;          redo A;
2297        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2298          !!!cp (138);          !!!cp (138);
2299          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2300          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2301          !!!next-input-character;          !!!next-input-character;
2302    
2303          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2304    
2305          redo A;          redo A;
2306        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2307          !!!cp (139);          !!!cp (139);
2308          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2309          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2310          ## reconsume          ## reconsume
2311    
2312          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2313    
2314          redo A;          redo A;
2315        } else {        } else {
2316          !!!cp (140);          !!!cp (140);
2317          $self->{current_token}->{data} # comment          $self->{ct}->{data} # comment
2318              .= chr ($self->{next_char});              .= chr ($self->{nc});
2319          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2320          !!!next-input-character;          !!!next-input-character;
2321          redo A;          redo A;
2322        }        }
2323      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2324        if ($self->{next_char} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2325          !!!cp (141);          !!!cp (141);
2326          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
2327          !!!next-input-character;          !!!next-input-character;
2328          redo A;          redo A;
2329        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2330          !!!cp (142);          !!!cp (142);
2331          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2332          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2333          !!!next-input-character;          !!!next-input-character;
2334    
2335          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2336    
2337          redo A;          redo A;
2338        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2339          !!!cp (143);          !!!cp (143);
2340          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2341          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2342          ## reconsume          ## reconsume
2343    
2344          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2345    
2346          redo A;          redo A;
2347        } else {        } else {
2348          !!!cp (144);          !!!cp (144);
2349          $self->{current_token}->{data} # comment          $self->{ct}->{data} # comment
2350              .= '-' . chr ($self->{next_char});              .= '-' . chr ($self->{nc});
2351          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2352          !!!next-input-character;          !!!next-input-character;
2353          redo A;          redo A;
2354        }        }
2355      } elsif ($self->{state} == COMMENT_STATE) {      } elsif ($self->{state} == COMMENT_STATE) {
2356        if ($self->{next_char} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2357          !!!cp (145);          !!!cp (145);
2358          $self->{state} = COMMENT_END_DASH_STATE;          $self->{state} = COMMENT_END_DASH_STATE;
2359          !!!next-input-character;          !!!next-input-character;
2360          redo A;          redo A;
2361        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2362          !!!cp (146);          !!!cp (146);
2363          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2364          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2365          ## reconsume          ## reconsume
2366    
2367          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2368    
2369          redo A;          redo A;
2370        } else {        } else {
2371          !!!cp (147);          !!!cp (147);
2372          $self->{current_token}->{data} .= chr ($self->{next_char}); # comment          $self->{ct}->{data} .= chr ($self->{nc}); # comment
2373            $self->{read_until}->($self->{ct}->{data},
2374                                  q[-],
2375                                  length $self->{ct}->{data});
2376    
2377          ## Stay in the state          ## Stay in the state
2378          !!!next-input-character;          !!!next-input-character;
2379          redo A;          redo A;
2380        }        }
2381      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2382        if ($self->{next_char} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2383          !!!cp (148);          !!!cp (148);
2384          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
2385          !!!next-input-character;          !!!next-input-character;
2386          redo A;          redo A;
2387        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2388          !!!cp (149);          !!!cp (149);
2389          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2390          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2391          ## reconsume          ## reconsume
2392    
2393          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2394    
2395          redo A;          redo A;
2396        } else {        } else {
2397          !!!cp (150);          !!!cp (150);
2398          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment          $self->{ct}->{data} .= '-' . chr ($self->{nc}); # comment
2399          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2400          !!!next-input-character;          !!!next-input-character;
2401          redo A;          redo A;
2402        }        }
2403      } elsif ($self->{state} == COMMENT_END_STATE) {      } elsif ($self->{state} == COMMENT_END_STATE) {
2404        if ($self->{next_char} == 0x003E) { # >        if ($self->{nc} == 0x003E) { # >
2405          !!!cp (151);          !!!cp (151);
2406          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2407          !!!next-input-character;          !!!next-input-character;
2408    
2409          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2410    
2411          redo A;          redo A;
2412        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{nc} == 0x002D) { # -
2413          !!!cp (152);          !!!cp (152);
2414          !!!parse-error (type => 'dash in comment',          !!!parse-error (type => 'dash in comment',
2415                          line => $self->{line_prev},                          line => $self->{line_prev},
2416                          column => $self->{column_prev});                          column => $self->{column_prev});
2417          $self->{current_token}->{data} .= '-'; # comment          $self->{ct}->{data} .= '-'; # comment
2418          ## Stay in the state          ## Stay in the state
2419          !!!next-input-character;          !!!next-input-character;
2420          redo A;          redo A;
2421        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2422          !!!cp (153);          !!!cp (153);
2423          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2424          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2425          ## reconsume          ## reconsume
2426    
2427          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2428    
2429          redo A;          redo A;
2430        } else {        } else {
# Line 2203  sub _get_next_token ($) { Line 2432  sub _get_next_token ($) {
2432          !!!parse-error (type => 'dash in comment',          !!!parse-error (type => 'dash in comment',
2433                          line => $self->{line_prev},                          line => $self->{line_prev},
2434                          column => $self->{column_prev});                          column => $self->{column_prev});
2435          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment          $self->{ct}->{data} .= '--' . chr ($self->{nc}); # comment
2436          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2437          !!!next-input-character;          !!!next-input-character;
2438          redo A;          redo A;
2439        }        }
2440      } elsif ($self->{state} == DOCTYPE_STATE) {      } elsif ($self->{state} == DOCTYPE_STATE) {
2441        if ($self->{next_char} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
2442            $self->{next_char} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
2443            $self->{next_char} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
2444            $self->{next_char} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
2445            $self->{next_char} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
2446          !!!cp (155);          !!!cp (155);
2447          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2448          !!!next-input-character;          !!!next-input-character;
# Line 2226  sub _get_next_token ($) { Line 2455  sub _get_next_token ($) {
2455          redo A;          redo A;
2456        }        }
2457      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2458        if ($self->{next_char} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
2459            $self->{next_char} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
2460            $self->{next_char} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
2461            $self->{next_char} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
2462            $self->{next_char} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
2463          !!!cp (157);          !!!cp (157);
2464          ## Stay in the state          ## Stay in the state
2465          !!!next-input-character;          !!!next-input-character;
2466          redo A;          redo A;
2467        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2468          !!!cp (158);          !!!cp (158);
2469          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2470          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2471          !!!next-input-character;          !!!next-input-character;
2472    
2473          !!!emit ($self->{current_token}); # DOCTYPE (quirks)          !!!emit ($self->{ct}); # DOCTYPE (quirks)
2474    
2475          redo A;          redo A;
2476        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2477          !!!cp (159);          !!!cp (159);
2478          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2479          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2480          ## reconsume          ## reconsume
2481    
2482          !!!emit ($self->{current_token}); # DOCTYPE (quirks)          !!!emit ($self->{ct}); # DOCTYPE (quirks)
2483    
2484          redo A;          redo A;
2485        } else {        } else {
2486          !!!cp (160);          !!!cp (160);
2487          $self->{current_token}->{name} = chr $self->{next_char};          $self->{ct}->{name} = chr $self->{nc};
2488          delete $self->{current_token}->{quirks};          delete $self->{ct}->{quirks};
2489  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2490          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2491          !!!next-input-character;          !!!next-input-character;
# Line 2264  sub _get_next_token ($) { Line 2493  sub _get_next_token ($) {
2493        }        }
2494      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2495  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
2496        if ($self->{next_char} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
2497            $self->{next_char} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
2498            $self->{next_char} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
2499            $self->{next_char} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
2500            $self->{next_char} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
2501          !!!cp (161);          !!!cp (161);
2502          $self->{state} = AFTER_DOCTYPE_NAME_STATE;          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2503          !!!next-input-character;          !!!next-input-character;
2504          redo A;          redo A;
2505        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2506          !!!cp (162);          !!!cp (162);
2507          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2508          !!!next-input-character;          !!!next-input-character;
2509    
2510          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2511    
2512          redo A;          redo A;
2513        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2514          !!!cp (163);          !!!cp (163);
2515          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2516          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2517          ## reconsume          ## reconsume
2518    
2519          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2520          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2521    
2522          redo A;          redo A;
2523        } else {        } else {
2524          !!!cp (164);          !!!cp (164);
2525          $self->{current_token}->{name}          $self->{ct}->{name}
2526            .= chr ($self->{next_char}); # DOCTYPE            .= chr ($self->{nc}); # DOCTYPE
2527          ## Stay in the state          ## Stay in the state
2528          !!!next-input-character;          !!!next-input-character;
2529          redo A;          redo A;
2530        }        }
2531      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2532        if ($self->{next_char} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
2533            $self->{next_char} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
2534            $self->{next_char} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
2535            $self->{next_char} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
2536            $self->{next_char} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
2537          !!!cp (165);          !!!cp (165);
2538          ## Stay in the state          ## Stay in the state
2539          !!!next-input-character;          !!!next-input-character;
2540          redo A;          redo A;
2541        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2542          !!!cp (166);          !!!cp (166);
2543          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2544          !!!next-input-character;          !!!next-input-character;
2545    
2546          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2547    
2548          redo A;          redo A;
2549        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2550          !!!cp (167);          !!!cp (167);
2551          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2552          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2553          ## reconsume          ## reconsume
2554    
2555          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2556          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2557    
2558          redo A;          redo A;
2559        } elsif ($self->{next_char} == 0x0050 or # P        } elsif ($self->{nc} == 0x0050 or # P
2560                 $self->{next_char} == 0x0070) { # p                 $self->{nc} == 0x0070) { # p
2561            $self->{state} = PUBLIC_STATE;
2562            $self->{s_kwd} = chr $self->{nc};
2563          !!!next-input-character;          !!!next-input-character;
2564          if ($self->{next_char} == 0x0055 or # U          redo A;
2565              $self->{next_char} == 0x0075) { # u        } elsif ($self->{nc} == 0x0053 or # S
2566            !!!next-input-character;                 $self->{nc} == 0x0073) { # s
2567            if ($self->{next_char} == 0x0042 or # B          $self->{state} = SYSTEM_STATE;
2568                $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  
2569          !!!next-input-character;          !!!next-input-character;
2570          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);  
         }  
   
         #  
2571        } else {        } else {
2572          !!!cp (180);          !!!cp (180);
2573            !!!parse-error (type => 'string after DOCTYPE name');
2574            $self->{ct}->{quirks} = 1;
2575    
2576            $self->{state} = BOGUS_DOCTYPE_STATE;
2577          !!!next-input-character;          !!!next-input-character;
2578          #          redo A;
2579        }        }
2580        } elsif ($self->{state} == PUBLIC_STATE) {
2581          ## ASCII case-insensitive
2582          if ($self->{nc} == [
2583                undef,
2584                0x0055, # U
2585                0x0042, # B
2586                0x004C, # L
2587                0x0049, # I
2588              ]->[length $self->{s_kwd}] or
2589              $self->{nc} == [
2590                undef,
2591                0x0075, # u
2592                0x0062, # b
2593                0x006C, # l
2594                0x0069, # i
2595              ]->[length $self->{s_kwd}]) {
2596            !!!cp (175);
2597            ## Stay in the state.
2598            $self->{s_kwd} .= chr $self->{nc};
2599            !!!next-input-character;
2600            redo A;
2601          } elsif ((length $self->{s_kwd}) == 5 and
2602                   ($self->{nc} == 0x0043 or # C
2603                    $self->{nc} == 0x0063)) { # c
2604            !!!cp (168);
2605            $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2606            !!!next-input-character;
2607            redo A;
2608          } else {
2609            !!!cp (169);
2610            !!!parse-error (type => 'string after DOCTYPE name',
2611                            line => $self->{line_prev},
2612                            column => $self->{column_prev} + 1 - length $self->{s_kwd});
2613            $self->{ct}->{quirks} = 1;
2614    
2615        !!!parse-error (type => 'string after DOCTYPE name');          $self->{state} = BOGUS_DOCTYPE_STATE;
2616        $self->{current_token}->{quirks} = 1;          ## Reconsume.
2617            redo A;
2618          }
2619        } elsif ($self->{state} == SYSTEM_STATE) {
2620          ## ASCII case-insensitive
2621          if ($self->{nc} == [
2622                undef,
2623                0x0059, # Y
2624                0x0053, # S
2625                0x0054, # T
2626                0x0045, # E
2627              ]->[length $self->{s_kwd}] or
2628              $self->{nc} == [
2629                undef,
2630                0x0079, # y
2631                0x0073, # s
2632                0x0074, # t
2633                0x0065, # e
2634              ]->[length $self->{s_kwd}]) {
2635            !!!cp (170);
2636            ## Stay in the state.
2637            $self->{s_kwd} .= chr $self->{nc};
2638            !!!next-input-character;
2639            redo A;
2640          } elsif ((length $self->{s_kwd}) == 5 and
2641                   ($self->{nc} == 0x004D or # M
2642                    $self->{nc} == 0x006D)) { # m
2643            !!!cp (171);
2644            $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2645            !!!next-input-character;
2646            redo A;
2647          } else {
2648            !!!cp (172);
2649            !!!parse-error (type => 'string after DOCTYPE name',
2650                            line => $self->{line_prev},
2651                            column => $self->{column_prev} + 1 - length $self->{s_kwd});
2652            $self->{ct}->{quirks} = 1;
2653    
2654        $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2655        # next-input-character is already done          ## Reconsume.
2656        redo A;          redo A;
2657          }
2658      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2659        if ({        if ({
2660              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2661              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2662            }->{$self->{next_char}}) {            }->{$self->{nc}}) {
2663          !!!cp (181);          !!!cp (181);
2664          ## Stay in the state          ## Stay in the state
2665          !!!next-input-character;          !!!next-input-character;
2666          redo A;          redo A;
2667        } elsif ($self->{next_char} eq 0x0022) { # "        } elsif ($self->{nc} eq 0x0022) { # "
2668          !!!cp (182);          !!!cp (182);
2669          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{ct}->{pubid} = ''; # DOCTYPE
2670          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2671          !!!next-input-character;          !!!next-input-character;
2672          redo A;          redo A;
2673        } elsif ($self->{next_char} eq 0x0027) { # '        } elsif ($self->{nc} eq 0x0027) { # '
2674          !!!cp (183);          !!!cp (183);
2675          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{ct}->{pubid} = ''; # DOCTYPE
2676          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2677          !!!next-input-character;          !!!next-input-character;
2678          redo A;          redo A;
2679        } elsif ($self->{next_char} eq 0x003E) { # >        } elsif ($self->{nc} eq 0x003E) { # >
2680          !!!cp (184);          !!!cp (184);
2681          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
2682    
2683          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2684          !!!next-input-character;          !!!next-input-character;
2685    
2686          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2687          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2688    
2689          redo A;          redo A;
2690        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2691          !!!cp (185);          !!!cp (185);
2692          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2693    
2694          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2695          ## reconsume          ## reconsume
2696    
2697          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2698          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2699    
2700          redo A;          redo A;
2701        } else {        } else {
2702          !!!cp (186);          !!!cp (186);
2703          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
2704          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2705    
2706          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2707          !!!next-input-character;          !!!next-input-character;
2708          redo A;          redo A;
2709        }        }
2710      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2711        if ($self->{next_char} == 0x0022) { # "        if ($self->{nc} == 0x0022) { # "
2712          !!!cp (187);          !!!cp (187);
2713          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2714          !!!next-input-character;          !!!next-input-character;
2715          redo A;          redo A;
2716        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2717          !!!cp (188);          !!!cp (188);
2718          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2719    
2720          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2721          !!!next-input-character;          !!!next-input-character;
2722    
2723          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2724          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2725    
2726          redo A;          redo A;
2727        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2728          !!!cp (189);          !!!cp (189);
2729          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2730    
2731          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2732          ## reconsume          ## reconsume
2733    
2734          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2735          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2736    
2737          redo A;          redo A;
2738        } else {        } else {
2739          !!!cp (190);          !!!cp (190);
2740          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{ct}->{pubid} # DOCTYPE
2741              .= chr $self->{next_char};              .= chr $self->{nc};
2742            $self->{read_until}->($self->{ct}->{pubid}, q[">],
2743                                  length $self->{ct}->{pubid});
2744    
2745          ## Stay in the state          ## Stay in the state
2746          !!!next-input-character;          !!!next-input-character;
2747          redo A;          redo A;
2748        }        }
2749      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2750        if ($self->{next_char} == 0x0027) { # '        if ($self->{nc} == 0x0027) { # '
2751          !!!cp (191);          !!!cp (191);
2752          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2753          !!!next-input-character;          !!!next-input-character;
2754          redo A;          redo A;
2755        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2756          !!!cp (192);          !!!cp (192);
2757          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2758    
2759          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2760          !!!next-input-character;          !!!next-input-character;
2761    
2762          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2763          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2764    
2765          redo A;          redo A;
2766        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2767          !!!cp (193);          !!!cp (193);
2768          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2769    
2770          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2771          ## reconsume          ## reconsume
2772    
2773          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2774          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2775    
2776          redo A;          redo A;
2777        } else {        } else {
2778          !!!cp (194);          !!!cp (194);
2779          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{ct}->{pubid} # DOCTYPE
2780              .= chr $self->{next_char};              .= chr $self->{nc};
2781            $self->{read_until}->($self->{ct}->{pubid}, q['>],
2782                                  length $self->{ct}->{pubid});
2783    
2784          ## Stay in the state          ## Stay in the state
2785          !!!next-input-character;          !!!next-input-character;
2786          redo A;          redo A;
# Line 2543  sub _get_next_token ($) { Line 2789  sub _get_next_token ($) {
2789        if ({        if ({
2790              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2791              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2792            }->{$self->{next_char}}) {            }->{$self->{nc}}) {
2793          !!!cp (195);          !!!cp (195);
2794          ## Stay in the state          ## Stay in the state
2795          !!!next-input-character;          !!!next-input-character;
2796          redo A;          redo A;
2797        } elsif ($self->{next_char} == 0x0022) { # "        } elsif ($self->{nc} == 0x0022) { # "
2798          !!!cp (196);          !!!cp (196);
2799          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{ct}->{sysid} = ''; # DOCTYPE
2800          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2801          !!!next-input-character;          !!!next-input-character;
2802          redo A;          redo A;
2803        } elsif ($self->{next_char} == 0x0027) { # '        } elsif ($self->{nc} == 0x0027) { # '
2804          !!!cp (197);          !!!cp (197);
2805          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{ct}->{sysid} = ''; # DOCTYPE
2806          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2807          !!!next-input-character;          !!!next-input-character;
2808          redo A;          redo A;
2809        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2810          !!!cp (198);          !!!cp (198);
2811          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2812          !!!next-input-character;          !!!next-input-character;
2813    
2814          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2815    
2816          redo A;          redo A;
2817        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2818          !!!cp (199);          !!!cp (199);
2819          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2820    
2821          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2822          ## reconsume          ## reconsume
2823    
2824          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2825          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2826    
2827          redo A;          redo A;
2828        } else {        } else {
2829          !!!cp (200);          !!!cp (200);
2830          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2831          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2832    
2833          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2834          !!!next-input-character;          !!!next-input-character;
# Line 2592  sub _get_next_token ($) { Line 2838  sub _get_next_token ($) {
2838        if ({        if ({
2839              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2840              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2841            }->{$self->{next_char}}) {            }->{$self->{nc}}) {
2842          !!!cp (201);          !!!cp (201);
2843          ## Stay in the state          ## Stay in the state
2844          !!!next-input-character;          !!!next-input-character;
2845          redo A;          redo A;
2846        } elsif ($self->{next_char} == 0x0022) { # "        } elsif ($self->{nc} == 0x0022) { # "
2847          !!!cp (202);          !!!cp (202);
2848          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{ct}->{sysid} = ''; # DOCTYPE
2849          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2850          !!!next-input-character;          !!!next-input-character;
2851          redo A;          redo A;
2852        } elsif ($self->{next_char} == 0x0027) { # '        } elsif ($self->{nc} == 0x0027) { # '
2853          !!!cp (203);          !!!cp (203);
2854          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{ct}->{sysid} = ''; # DOCTYPE
2855          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2856          !!!next-input-character;          !!!next-input-character;
2857          redo A;          redo A;
2858        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2859          !!!cp (204);          !!!cp (204);
2860          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2861          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2862          !!!next-input-character;          !!!next-input-character;
2863    
2864          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2865          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2866    
2867          redo A;          redo A;
2868        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2869          !!!cp (205);          !!!cp (205);
2870          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2871    
2872          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2873          ## reconsume          ## reconsume
2874    
2875          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2876          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2877    
2878          redo A;          redo A;
2879        } else {        } else {
2880          !!!cp (206);          !!!cp (206);
2881          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2882          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2883    
2884          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2885          !!!next-input-character;          !!!next-input-character;
2886          redo A;          redo A;
2887        }        }
2888      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2889        if ($self->{next_char} == 0x0022) { # "        if ($self->{nc} == 0x0022) { # "
2890          !!!cp (207);          !!!cp (207);
2891          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2892          !!!next-input-character;          !!!next-input-character;
2893          redo A;          redo A;
2894        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2895          !!!cp (208);          !!!cp (208);
2896          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2897    
2898          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2899          !!!next-input-character;          !!!next-input-character;
2900    
2901          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2902          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2903    
2904          redo A;          redo A;
2905        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2906          !!!cp (209);          !!!cp (209);
2907          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2908    
2909          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2910          ## reconsume          ## reconsume
2911    
2912          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2913          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2914    
2915          redo A;          redo A;
2916        } else {        } else {
2917          !!!cp (210);          !!!cp (210);
2918          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{ct}->{sysid} # DOCTYPE
2919              .= chr $self->{next_char};              .= chr $self->{nc};
2920            $self->{read_until}->($self->{ct}->{sysid}, q[">],
2921                                  length $self->{ct}->{sysid});
2922    
2923          ## Stay in the state          ## Stay in the state
2924          !!!next-input-character;          !!!next-input-character;
2925          redo A;          redo A;
2926        }        }
2927      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2928        if ($self->{next_char} == 0x0027) { # '        if ($self->{nc} == 0x0027) { # '
2929          !!!cp (211);          !!!cp (211);
2930          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2931          !!!next-input-character;          !!!next-input-character;
2932          redo A;          redo A;
2933        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2934          !!!cp (212);          !!!cp (212);
2935          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2936    
2937          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2938          !!!next-input-character;          !!!next-input-character;
2939    
2940          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2941          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2942    
2943          redo A;          redo A;
2944        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2945          !!!cp (213);          !!!cp (213);
2946          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2947    
2948          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2949          ## reconsume          ## reconsume
2950    
2951          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2952          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2953    
2954          redo A;          redo A;
2955        } else {        } else {
2956          !!!cp (214);          !!!cp (214);
2957          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{ct}->{sysid} # DOCTYPE
2958              .= chr $self->{next_char};              .= chr $self->{nc};
2959            $self->{read_until}->($self->{ct}->{sysid}, q['>],
2960                                  length $self->{ct}->{sysid});
2961    
2962          ## Stay in the state          ## Stay in the state
2963          !!!next-input-character;          !!!next-input-character;
2964          redo A;          redo A;
# Line 2715  sub _get_next_token ($) { Line 2967  sub _get_next_token ($) {
2967        if ({        if ({
2968              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2969              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2970            }->{$self->{next_char}}) {            }->{$self->{nc}}) {
2971          !!!cp (215);          !!!cp (215);
2972          ## Stay in the state          ## Stay in the state
2973          !!!next-input-character;          !!!next-input-character;
2974          redo A;          redo A;
2975        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2976          !!!cp (216);          !!!cp (216);
2977          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2978          !!!next-input-character;          !!!next-input-character;
2979    
2980          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2981    
2982          redo A;          redo A;
2983        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2984          !!!cp (217);          !!!cp (217);
2985          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2986          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2987          ## reconsume          ## reconsume
2988    
2989          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2990          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2991    
2992          redo A;          redo A;
2993        } else {        } else {
2994          !!!cp (218);          !!!cp (218);
2995          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2996          #$self->{current_token}->{quirks} = 1;          #$self->{ct}->{quirks} = 1;
2997    
2998          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2999          !!!next-input-character;          !!!next-input-character;
3000          redo A;          redo A;
3001        }        }
3002      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
3003        if ($self->{next_char} == 0x003E) { # >        if ($self->{nc} == 0x003E) { # >
3004          !!!cp (219);          !!!cp (219);
3005          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
3006          !!!next-input-character;          !!!next-input-character;
3007    
3008          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
3009    
3010          redo A;          redo A;
3011        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
3012          !!!cp (220);          !!!cp (220);
3013          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
3014          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
3015          ## reconsume          ## reconsume
3016    
3017          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
3018    
3019          redo A;          redo A;
3020        } else {        } else {
3021          !!!cp (221);          !!!cp (221);
3022            my $s = '';
3023            $self->{read_until}->($s, q[>], 0);
3024    
3025          ## Stay in the state          ## Stay in the state
3026          !!!next-input-character;          !!!next-input-character;
3027          redo A;          redo A;
3028        }        }
3029      } elsif ($self->{state} == CDATA_BLOCK_STATE) {      } elsif ($self->{state} == CDATA_SECTION_STATE) {
3030        my $s = '';        ## NOTE: "CDATA section state" in the state is jointly implemented
3031          ## by three states, |CDATA_SECTION_STATE|, |CDATA_SECTION_MSE1_STATE|,
3032          ## and |CDATA_SECTION_MSE2_STATE|.
3033                
3034        my ($l, $c) = ($self->{line}, $self->{column});        if ($self->{nc} == 0x005D) { # ]
3035            !!!cp (221.1);
3036        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};  
3037          !!!next-input-character;          !!!next-input-character;
3038        } # CS          redo A;
3039          } elsif ($self->{nc} == -1) {
3040            $self->{state} = DATA_STATE;
3041            !!!next-input-character;
3042            if (length $self->{ct}->{data}) { # character
3043              !!!cp (221.2);
3044              !!!emit ($self->{ct}); # character
3045            } else {
3046              !!!cp (221.3);
3047              ## No token to emit. $self->{ct} is discarded.
3048            }        
3049            redo A;
3050          } else {
3051            !!!cp (221.4);
3052            $self->{ct}->{data} .= chr $self->{nc};
3053            $self->{read_until}->($self->{ct}->{data},
3054                                  q<]>,
3055                                  length $self->{ct}->{data});
3056    
3057        $self->{state} = DATA_STATE;          ## Stay in the state.
3058        ## next-input-character done or EOF, which is reconsumed.          !!!next-input-character;
3059            redo A;
3060          }
3061    
3062        if (length $s) {        ## ISSUE: "text tokens" in spec.
3063        } elsif ($self->{state} == CDATA_SECTION_MSE1_STATE) {
3064          if ($self->{nc} == 0x005D) { # ]
3065            !!!cp (221.5);
3066            $self->{state} = CDATA_SECTION_MSE2_STATE;
3067            !!!next-input-character;
3068            redo A;
3069          } else {
3070          !!!cp (221.6);          !!!cp (221.6);
3071          !!!emit ({type => CHARACTER_TOKEN, data => $s,          $self->{ct}->{data} .= ']';
3072                    line => $l, column => $c});          $self->{state} = CDATA_SECTION_STATE;
3073            ## Reconsume.
3074            redo A;
3075          }
3076        } elsif ($self->{state} == CDATA_SECTION_MSE2_STATE) {
3077          if ($self->{nc} == 0x003E) { # >
3078            $self->{state} = DATA_STATE;
3079            !!!next-input-character;
3080            if (length $self->{ct}->{data}) { # character
3081              !!!cp (221.7);
3082              !!!emit ($self->{ct}); # character
3083            } else {
3084              !!!cp (221.8);
3085              ## No token to emit. $self->{ct} is discarded.
3086            }
3087            redo A;
3088          } elsif ($self->{nc} == 0x005D) { # ]
3089            !!!cp (221.9); # character
3090            $self->{ct}->{data} .= ']'; ## Add first "]" of "]]]".
3091            ## Stay in the state.
3092            !!!next-input-character;
3093            redo A;
3094        } else {        } else {
3095          !!!cp (221.7);          !!!cp (221.11);
3096            $self->{ct}->{data} .= ']]'; # character
3097            $self->{state} = CDATA_SECTION_STATE;
3098            ## Reconsume.
3099            redo A;
3100          }
3101        } elsif ($self->{state} == ENTITY_STATE) {
3102          if ({
3103            0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
3104            0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, &
3105            $self->{entity_add} => 1,
3106          }->{$self->{nc}}) {
3107            !!!cp (1001);
3108            ## Don't consume
3109            ## No error
3110            ## Return nothing.
3111            #
3112          } elsif ($self->{nc} == 0x0023) { # #
3113            !!!cp (999);
3114            $self->{state} = ENTITY_HASH_STATE;
3115            $self->{s_kwd} = '#';
3116            !!!next-input-character;
3117            redo A;
3118          } elsif ((0x0041 <= $self->{nc} and
3119                    $self->{nc} <= 0x005A) or # A..Z
3120                   (0x0061 <= $self->{nc} and
3121                    $self->{nc} <= 0x007A)) { # a..z
3122            !!!cp (998);
3123            require Whatpm::_NamedEntityList;
3124            $self->{state} = ENTITY_NAME_STATE;
3125            $self->{s_kwd} = chr $self->{nc};
3126            $self->{entity__value} = $self->{s_kwd};
3127            $self->{entity__match} = 0;
3128            !!!next-input-character;
3129            redo A;
3130          } else {
3131            !!!cp (1027);
3132            !!!parse-error (type => 'bare ero');
3133            ## Return nothing.
3134            #
3135        }        }
3136    
3137        redo A;        ## NOTE: No character is consumed by the "consume a character
3138          ## reference" algorithm.  In other word, there is an "&" character
3139        ## ISSUE: "text tokens" in spec.        ## that does not introduce a character reference, which would be
3140        ## TODO: Streaming support        ## appended to the parent element or the attribute value in later
3141      } else {        ## process of the tokenizer.
3142        die "$0: $self->{state}: Unknown state";  
3143      }        if ($self->{prev_state} == DATA_STATE) {
3144    } # A            !!!cp (997);
3145            $self->{state} = $self->{prev_state};
3146    die "$0: _get_next_token: unexpected case";          ## Reconsume.
3147  } # _get_next_token          !!!emit ({type => CHARACTER_TOKEN, data => '&',
3148                      line => $self->{line_prev},
3149  sub _tokenize_attempt_to_consume_an_entity ($$$) {                    column => $self->{column_prev},
3150    my ($self, $in_attr, $additional) = @_;                   });
3151            redo A;
3152    my ($l, $c) = ($self->{line_prev}, $self->{column_prev});        } else {
3153            !!!cp (996);
3154            $self->{ca}->{value} .= '&';
3155            $self->{state} = $self->{prev_state};
3156            ## Reconsume.
3157            redo A;
3158          }
3159        } elsif ($self->{state} == ENTITY_HASH_STATE) {
3160          if ($self->{nc} == 0x0078 or # x
3161              $self->{nc} == 0x0058) { # X
3162            !!!cp (995);
3163            $self->{state} = HEXREF_X_STATE;
3164            $self->{s_kwd} .= chr $self->{nc};
3165            !!!next-input-character;
3166            redo A;
3167          } elsif (0x0030 <= $self->{nc} and
3168                   $self->{nc} <= 0x0039) { # 0..9
3169            !!!cp (994);
3170            $self->{state} = NCR_NUM_STATE;
3171            $self->{s_kwd} = $self->{nc} - 0x0030;
3172            !!!next-input-character;
3173            redo A;
3174          } else {
3175            !!!parse-error (type => 'bare nero',
3176                            line => $self->{line_prev},
3177                            column => $self->{column_prev} - 1);
3178    
3179    if ({          ## NOTE: According to the spec algorithm, nothing is returned,
3180         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,          ## and then "&#" is appended to the parent element or the attribute
3181         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR          ## value in the later processing.
3182         $additional => 1,  
3183        }->{$self->{next_char}}) {          if ($self->{prev_state} == DATA_STATE) {
3184      !!!cp (1001);            !!!cp (1019);
3185      ## Don't consume            $self->{state} = $self->{prev_state};
3186      ## No error            ## Reconsume.
3187      return undef;            !!!emit ({type => CHARACTER_TOKEN,
3188    } elsif ($self->{next_char} == 0x0023) { # #                      data => '&#',
3189      !!!next-input-character;                      line => $self->{line_prev},
3190      if ($self->{next_char} == 0x0078 or # x                      column => $self->{column_prev} - 1,
3191          $self->{next_char} == 0x0058) { # X                     });
3192        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;  
3193          } else {          } else {
3194            !!!cp (1007);            !!!cp (993);
3195            !!!parse-error (type => 'no refc', line => $l, column => $c);            $self->{ca}->{value} .= '&#';
3196              $self->{state} = $self->{prev_state};
3197              ## Reconsume.
3198              redo A;
3199          }          }
3200          }
3201          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {      } elsif ($self->{state} == NCR_NUM_STATE) {
3202            !!!cp (1008);        if (0x0030 <= $self->{nc} and
3203            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);            $self->{nc} <= 0x0039) { # 0..9
           $code = 0xFFFD;  
         } elsif ($code > 0x10FFFF) {  
           !!!cp (1009);  
           !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);  
           $code = 0xFFFD;  
         } elsif ($code == 0x000D) {  
           !!!cp (1010);  
           !!!parse-error (type => 'CR character reference', line => $l, column => $c);  
           $code = 0x000A;  
         } elsif (0x80 <= $code and $code <= 0x9F) {  
           !!!cp (1011);  
           !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);  
           $code = $c1_entity_char->{$code};  
         }  
   
         return {type => CHARACTER_TOKEN, data => chr $code,  
                 has_reference => 1,  
                 line => $l, column => $c,  
                };  
       } # X  
     } elsif (0x0030 <= $self->{next_char} and  
              $self->{next_char} <= 0x0039) { # 0..9  
       my $code = $self->{next_char} - 0x0030;  
       !!!next-input-character;  
         
       while (0x0030 <= $self->{next_char} and  
                 $self->{next_char} <= 0x0039) { # 0..9  
3204          !!!cp (1012);          !!!cp (1012);
3205          $code *= 10;          $self->{s_kwd} *= 10;
3206          $code += $self->{next_char} - 0x0030;          $self->{s_kwd} += $self->{nc} - 0x0030;
3207                    
3208            ## Stay in the state.
3209          !!!next-input-character;          !!!next-input-character;
3210        }          redo A;
3211          } elsif ($self->{nc} == 0x003B) { # ;
       if ($self->{next_char} == 0x003B) { # ;  
3212          !!!cp (1013);          !!!cp (1013);
3213          !!!next-input-character;          !!!next-input-character;
3214            #
3215        } else {        } else {
3216          !!!cp (1014);          !!!cp (1014);
3217          !!!parse-error (type => 'no refc', line => $l, column => $c);          !!!parse-error (type => 'no refc');
3218            ## Reconsume.
3219            #
3220        }        }
3221    
3222          my $code = $self->{s_kwd};
3223          my $l = $self->{line_prev};
3224          my $c = $self->{column_prev};
3225        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3226          !!!cp (1015);          !!!cp (1015);
3227          !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);          !!!parse-error (type => 'invalid character reference',
3228                            text => (sprintf 'U+%04X', $code),
3229                            line => $l, column => $c);
3230          $code = 0xFFFD;          $code = 0xFFFD;
3231        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
3232          !!!cp (1016);          !!!cp (1016);
3233          !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);          !!!parse-error (type => 'invalid character reference',
3234                            text => (sprintf 'U-%08X', $code),
3235                            line => $l, column => $c);
3236          $code = 0xFFFD;          $code = 0xFFFD;
3237        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
3238          !!!cp (1017);          !!!cp (1017);
3239          !!!parse-error (type => 'CR character reference', line => $l, column => $c);          !!!parse-error (type => 'CR character reference',
3240                            line => $l, column => $c);
3241          $code = 0x000A;          $code = 0x000A;
3242        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3243          !!!cp (1018);          !!!cp (1018);
3244          !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);          !!!parse-error (type => 'C1 character reference',
3245                            text => (sprintf 'U+%04X', $code),
3246                            line => $l, column => $c);
3247          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3248        }        }
3249          
3250        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,        if ($self->{prev_state} == DATA_STATE) {
3251                line => $l, column => $c,          !!!cp (992);
3252               };          $self->{state} = $self->{prev_state};
3253      } else {          ## Reconsume.
3254        !!!cp (1019);          !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3255        !!!parse-error (type => 'bare nero', line => $l, column => $c);                    line => $l, column => $c,
3256        !!!back-next-input-character ($self->{next_char});                   });
3257        $self->{next_char} = 0x0023; # #          redo A;
3258        return undef;        } else {
3259      }          !!!cp (991);
3260    } elsif ((0x0041 <= $self->{next_char} and          $self->{ca}->{value} .= chr $code;
3261              $self->{next_char} <= 0x005A) or          $self->{ca}->{has_reference} = 1;
3262             (0x0061 <= $self->{next_char} and          $self->{state} = $self->{prev_state};
3263              $self->{next_char} <= 0x007A)) {          ## Reconsume.
3264      my $entity_name = chr $self->{next_char};          redo A;
3265      !!!next-input-character;        }
3266        } elsif ($self->{state} == HEXREF_X_STATE) {
3267      my $value = $entity_name;        if ((0x0030 <= $self->{nc} and $self->{nc} <= 0x0039) or
3268      my $match = 0;            (0x0041 <= $self->{nc} and $self->{nc} <= 0x0046) or
3269      require Whatpm::_NamedEntityList;            (0x0061 <= $self->{nc} and $self->{nc} <= 0x0066)) {
3270      our $EntityChar;          # 0..9, A..F, a..f
3271            !!!cp (990);
3272      while (length $entity_name < 30 and          $self->{state} = HEXREF_HEX_STATE;
3273             ## NOTE: Some number greater than the maximum length of entity name          $self->{s_kwd} = 0;
3274             ((0x0041 <= $self->{next_char} and # a          ## Reconsume.
3275               $self->{next_char} <= 0x005A) or # x          redo A;
3276              (0x0061 <= $self->{next_char} and # a        } else {
3277               $self->{next_char} <= 0x007A) or # z          !!!parse-error (type => 'bare hcro',
3278              (0x0030 <= $self->{next_char} and # 0                          line => $self->{line_prev},
3279               $self->{next_char} <= 0x0039) or # 9                          column => $self->{column_prev} - 2);
3280              $self->{next_char} == 0x003B)) { # ;  
3281        $entity_name .= chr $self->{next_char};          ## NOTE: According to the spec algorithm, nothing is returned,
3282        if (defined $EntityChar->{$entity_name}) {          ## and then "&#" followed by "X" or "x" is appended to the parent
3283          if ($self->{next_char} == 0x003B) { # ;          ## element or the attribute value in the later processing.
3284            !!!cp (1020);  
3285            $value = $EntityChar->{$entity_name};          if ($self->{prev_state} == DATA_STATE) {
3286            $match = 1;            !!!cp (1005);
3287            !!!next-input-character;            $self->{state} = $self->{prev_state};
3288            last;            ## Reconsume.
3289              !!!emit ({type => CHARACTER_TOKEN,
3290                        data => '&' . $self->{s_kwd},
3291                        line => $self->{line_prev},
3292                        column => $self->{column_prev} - length $self->{s_kwd},
3293                       });
3294              redo A;
3295            } else {
3296              !!!cp (989);
3297              $self->{ca}->{value} .= '&' . $self->{s_kwd};
3298              $self->{state} = $self->{prev_state};
3299              ## Reconsume.
3300              redo A;
3301            }
3302          }
3303        } elsif ($self->{state} == HEXREF_HEX_STATE) {
3304          if (0x0030 <= $self->{nc} and $self->{nc} <= 0x0039) {
3305            # 0..9
3306            !!!cp (1002);
3307            $self->{s_kwd} *= 0x10;
3308            $self->{s_kwd} += $self->{nc} - 0x0030;
3309            ## Stay in the state.
3310            !!!next-input-character;
3311            redo A;
3312          } elsif (0x0061 <= $self->{nc} and
3313                   $self->{nc} <= 0x0066) { # a..f
3314            !!!cp (1003);
3315            $self->{s_kwd} *= 0x10;
3316            $self->{s_kwd} += $self->{nc} - 0x0060 + 9;
3317            ## Stay in the state.
3318            !!!next-input-character;
3319            redo A;
3320          } elsif (0x0041 <= $self->{nc} and
3321                   $self->{nc} <= 0x0046) { # A..F
3322            !!!cp (1004);
3323            $self->{s_kwd} *= 0x10;
3324            $self->{s_kwd} += $self->{nc} - 0x0040 + 9;
3325            ## Stay in the state.
3326            !!!next-input-character;
3327            redo A;
3328          } elsif ($self->{nc} == 0x003B) { # ;
3329            !!!cp (1006);
3330            !!!next-input-character;
3331            #
3332          } else {
3333            !!!cp (1007);
3334            !!!parse-error (type => 'no refc',
3335                            line => $self->{line},
3336                            column => $self->{column});
3337            ## Reconsume.
3338            #
3339          }
3340    
3341          my $code = $self->{s_kwd};
3342          my $l = $self->{line_prev};
3343          my $c = $self->{column_prev};
3344          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3345            !!!cp (1008);
3346            !!!parse-error (type => 'invalid character reference',
3347                            text => (sprintf 'U+%04X', $code),
3348                            line => $l, column => $c);
3349            $code = 0xFFFD;
3350          } elsif ($code > 0x10FFFF) {
3351            !!!cp (1009);
3352            !!!parse-error (type => 'invalid character reference',
3353                            text => (sprintf 'U-%08X', $code),
3354                            line => $l, column => $c);
3355            $code = 0xFFFD;
3356          } elsif ($code == 0x000D) {
3357            !!!cp (1010);
3358            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
3359            $code = 0x000A;
3360          } elsif (0x80 <= $code and $code <= 0x9F) {
3361            !!!cp (1011);
3362            !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
3363            $code = $c1_entity_char->{$code};
3364          }
3365    
3366          if ($self->{prev_state} == DATA_STATE) {
3367            !!!cp (988);
3368            $self->{state} = $self->{prev_state};
3369            ## Reconsume.
3370            !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3371                      line => $l, column => $c,
3372                     });
3373            redo A;
3374          } else {
3375            !!!cp (987);
3376            $self->{ca}->{value} .= chr $code;
3377            $self->{ca}->{has_reference} = 1;
3378            $self->{state} = $self->{prev_state};
3379            ## Reconsume.
3380            redo A;
3381          }
3382        } elsif ($self->{state} == ENTITY_NAME_STATE) {
3383          if (length $self->{s_kwd} < 30 and
3384              ## NOTE: Some number greater than the maximum length of entity name
3385              ((0x0041 <= $self->{nc} and # a
3386                $self->{nc} <= 0x005A) or # x
3387               (0x0061 <= $self->{nc} and # a
3388                $self->{nc} <= 0x007A) or # z
3389               (0x0030 <= $self->{nc} and # 0
3390                $self->{nc} <= 0x0039) or # 9
3391               $self->{nc} == 0x003B)) { # ;
3392            our $EntityChar;
3393            $self->{s_kwd} .= chr $self->{nc};
3394            if (defined $EntityChar->{$self->{s_kwd}}) {
3395              if ($self->{nc} == 0x003B) { # ;
3396                !!!cp (1020);
3397                $self->{entity__value} = $EntityChar->{$self->{s_kwd}};
3398                $self->{entity__match} = 1;
3399                !!!next-input-character;
3400                #
3401              } else {
3402                !!!cp (1021);
3403                $self->{entity__value} = $EntityChar->{$self->{s_kwd}};
3404                $self->{entity__match} = -1;
3405                ## Stay in the state.
3406                !!!next-input-character;
3407                redo A;
3408              }
3409          } else {          } else {
3410            !!!cp (1021);            !!!cp (1022);
3411            $value = $EntityChar->{$entity_name};            $self->{entity__value} .= chr $self->{nc};
3412            $match = -1;            $self->{entity__match} *= 2;
3413              ## Stay in the state.
3414            !!!next-input-character;            !!!next-input-character;
3415              redo A;
3416            }
3417          }
3418    
3419          my $data;
3420          my $has_ref;
3421          if ($self->{entity__match} > 0) {
3422            !!!cp (1023);
3423            $data = $self->{entity__value};
3424            $has_ref = 1;
3425            #
3426          } elsif ($self->{entity__match} < 0) {
3427            !!!parse-error (type => 'no refc');
3428            if ($self->{prev_state} != DATA_STATE and # in attribute
3429                $self->{entity__match} < -1) {
3430              !!!cp (1024);
3431              $data = '&' . $self->{s_kwd};
3432              #
3433            } else {
3434              !!!cp (1025);
3435              $data = $self->{entity__value};
3436              $has_ref = 1;
3437              #
3438          }          }
3439        } else {        } else {
3440          !!!cp (1022);          !!!cp (1026);
3441          $value .= chr $self->{next_char};          !!!parse-error (type => 'bare ero',
3442          $match *= 2;                          line => $self->{line_prev},
3443          !!!next-input-character;                          column => $self->{column_prev} - length $self->{s_kwd});
3444            $data = '&' . $self->{s_kwd};
3445            #
3446        }        }
3447      }    
3448              ## NOTE: In these cases, when a character reference is found,
3449      if ($match > 0) {        ## it is consumed and a character token is returned, or, otherwise,
3450        !!!cp (1023);        ## nothing is consumed and returned, according to the spec algorithm.
3451        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,        ## In this implementation, anything that has been examined by the
3452                line => $l, column => $c,        ## tokenizer is appended to the parent element or the attribute value
3453               };        ## as string, either literal string when no character reference or
3454      } elsif ($match < 0) {        ## entity-replaced string otherwise, in this stage, since any characters
3455        !!!parse-error (type => 'no refc', line => $l, column => $c);        ## that would not be consumed are appended in the data state or in an
3456        if ($in_attr and $match < -1) {        ## appropriate attribute value state anyway.
3457          !!!cp (1024);  
3458          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,        if ($self->{prev_state} == DATA_STATE) {
3459                  line => $l, column => $c,          !!!cp (986);
3460                 };          $self->{state} = $self->{prev_state};
3461        } else {          ## Reconsume.
3462          !!!cp (1025);          !!!emit ({type => CHARACTER_TOKEN,
3463          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,                    data => $data,
3464                  line => $l, column => $c,                    line => $self->{line_prev},
3465                 };                    column => $self->{column_prev} + 1 - length $self->{s_kwd},
3466                     });
3467            redo A;
3468          } else {
3469            !!!cp (985);
3470            $self->{ca}->{value} .= $data;
3471            $self->{ca}->{has_reference} = 1 if $has_ref;
3472            $self->{state} = $self->{prev_state};
3473            ## Reconsume.
3474            redo A;
3475        }        }
3476      } else {      } else {
3477        !!!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,  
              };  
3478      }      }
3479    } else {    } # A  
3480      !!!cp (1027);  
3481      ## no characters are consumed    die "$0: _get_next_token: unexpected case";
3482      !!!parse-error (type => 'bare ero', line => $l, column => $c);  } # _get_next_token
     return undef;  
   }  
 } # _tokenize_attempt_to_consume_an_entity  
3483    
3484  sub _initialize_tree_constructor ($) {  sub _initialize_tree_constructor ($) {
3485    my $self = shift;    my $self = shift;
# Line 3047  sub _initialize_tree_constructor ($) { Line 3488  sub _initialize_tree_constructor ($) {
3488    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3489    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3490    $self->{document}->manakai_is_html (1); # MUST    $self->{document}->manakai_is_html (1); # MUST
3491      $self->{document}->set_user_data (manakai_source_line => 1);
3492      $self->{document}->set_user_data (manakai_source_column => 1);
3493  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3494    
3495  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 3101  sub _tree_construction_initial ($) { Line 3544  sub _tree_construction_initial ($) {
3544        ## language.        ## language.
3545        my $doctype_name = $token->{name};        my $doctype_name = $token->{name};
3546        $doctype_name = '' unless defined $doctype_name;        $doctype_name = '' unless defined $doctype_name;
3547        $doctype_name =~ tr/a-z/A-Z/;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3548        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
3549            defined $token->{public_identifier} or            defined $token->{sysid}) {
           defined $token->{system_identifier}) {  
3550          !!!cp ('t1');          !!!cp ('t1');
3551          !!!parse-error (type => 'not HTML5', token => $token);          !!!parse-error (type => 'not HTML5', token => $token);
3552        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3553          !!!cp ('t2');          !!!cp ('t2');
         ## ISSUE: ASCII case-insensitive? (in fact it does not matter)  
3554          !!!parse-error (type => 'not HTML5', token => $token);          !!!parse-error (type => 'not HTML5', token => $token);
3555          } elsif (defined $token->{pubid}) {
3556            if ($token->{pubid} eq 'XSLT-compat') {
3557              !!!cp ('t1.2');
3558              !!!parse-error (type => 'XSLT-compat', token => $token,
3559                              level => $self->{level}->{should});
3560            } else {
3561              !!!parse-error (type => 'not HTML5', token => $token);
3562            }
3563        } else {        } else {
3564          !!!cp ('t3');          !!!cp ('t3');
3565            #
3566        }        }
3567                
3568        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3569          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3570        ## NOTE: Default value for both |public_id| and |system_id| attributes        ## NOTE: Default value for both |public_id| and |system_id| attributes
3571        ## 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.
3572        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{pubid}) if defined $token->{pubid};
3573            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};  
3574        ## NOTE: Other DocumentType attributes are null or empty lists.        ## NOTE: Other DocumentType attributes are null or empty lists.
3575        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
3576        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
# Line 3130  sub _tree_construction_initial ($) { Line 3578  sub _tree_construction_initial ($) {
3578        if ($token->{quirks} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
3579          !!!cp ('t4');          !!!cp ('t4');
3580          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
3581        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{pubid}) {
3582          my $pubid = $token->{public_identifier};          my $pubid = $token->{pubid};
3583          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3584          my $prefix = [          my $prefix = [
3585            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
# Line 3205  sub _tree_construction_initial ($) { Line 3653  sub _tree_construction_initial ($) {
3653            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3654          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3655                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3656            if (defined $token->{system_identifier}) {            if (defined $token->{sysid}) {
3657              !!!cp ('t6');              !!!cp ('t6');
3658              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
3659            } else {            } else {
# Line 3222  sub _tree_construction_initial ($) { Line 3670  sub _tree_construction_initial ($) {
3670        } else {        } else {
3671          !!!cp ('t10');          !!!cp ('t10');
3672        }        }
3673        if (defined $token->{system_identifier}) {        if (defined $token->{sysid}) {
3674          my $sysid = $token->{system_identifier};          my $sysid = $token->{sysid};
3675          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3676          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") {
3677            ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is            ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
# Line 3614  sub _tree_construction_main ($) { Line 4062  sub _tree_construction_main ($) {
4062        ## NOTE: An end-of-file token.        ## NOTE: An end-of-file token.
4063        if ($content_model_flag == CDATA_CONTENT_MODEL) {        if ($content_model_flag == CDATA_CONTENT_MODEL) {
4064          !!!cp ('t43');          !!!cp ('t43');
4065          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);          !!!parse-error (type => 'in CDATA:#eof', token => $token);
4066        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
4067          !!!cp ('t44');          !!!cp ('t44');
4068          !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);          !!!parse-error (type => 'in RCDATA:#eof', token => $token);
4069        } else {        } else {
4070          die "$0: $content_model_flag in parse_rcdata";          die "$0: $content_model_flag in parse_rcdata";
4071        }        }
# Line 3654  sub _tree_construction_main ($) { Line 4102  sub _tree_construction_main ($) {
4102        ## Ignore the token        ## Ignore the token
4103      } else {      } else {
4104        !!!cp ('t48');        !!!cp ('t48');
4105        !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);        !!!parse-error (type => 'in CDATA:#eof', token => $token);
4106        ## ISSUE: And ignore?        ## ISSUE: And ignore?
4107        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
4108      }      }
# Line 3705  sub _tree_construction_main ($) { Line 4153  sub _tree_construction_main ($) {
4153        } # AFE        } # AFE
4154        unless (defined $formatting_element) {        unless (defined $formatting_element) {
4155          !!!cp ('t53');          !!!cp ('t53');
4156          !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);          !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
4157          ## Ignore the token          ## Ignore the token
4158          !!!next-token;          !!!next-token;
4159          return;          return;
# Line 3722  sub _tree_construction_main ($) { Line 4170  sub _tree_construction_main ($) {
4170              last INSCOPE;              last INSCOPE;
4171            } else { # in open elements but not in scope            } else { # in open elements but not in scope
4172              !!!cp ('t55');              !!!cp ('t55');
4173              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},              !!!parse-error (type => 'unmatched end tag',
4174                                text => $token->{tag_name},
4175                              token => $end_tag_token);                              token => $end_tag_token);
4176              ## Ignore the token              ## Ignore the token
4177              !!!next-token;              !!!next-token;
# Line 3735  sub _tree_construction_main ($) { Line 4184  sub _tree_construction_main ($) {
4184        } # INSCOPE        } # INSCOPE
4185        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
4186          !!!cp ('t57');          !!!cp ('t57');
4187          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},          !!!parse-error (type => 'unmatched end tag',
4188                            text => $token->{tag_name},
4189                          token => $end_tag_token);                          token => $end_tag_token);
4190          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
4191          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
# Line 3744  sub _tree_construction_main ($) { Line 4194  sub _tree_construction_main ($) {
4194        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
4195          !!!cp ('t58');          !!!cp ('t58');
4196          !!!parse-error (type => 'not closed',          !!!parse-error (type => 'not closed',
4197                          value => $self->{open_elements}->[-1]->[0]                          text => $self->{open_elements}->[-1]->[0]
4198                              ->manakai_local_name,                              ->manakai_local_name,
4199                          token => $end_tag_token);                          token => $end_tag_token);
4200        }        }
# Line 3953  sub _tree_construction_main ($) { Line 4403  sub _tree_construction_main ($) {
4403    B: while (1) {    B: while (1) {
4404      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
4405        !!!cp ('t73');        !!!cp ('t73');
4406        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4407        ## Ignore the token        ## Ignore the token
4408        ## Stay in the phase        ## Stay in the phase
4409        !!!next-token;        !!!next-token;
# Line 3962  sub _tree_construction_main ($) { Line 4412  sub _tree_construction_main ($) {
4412               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
4413        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4414          !!!cp ('t79');          !!!cp ('t79');
4415          !!!parse-error (type => 'after html:html', token => $token);          !!!parse-error (type => 'after html', text => 'html', token => $token);
4416          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
4417        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4418          !!!cp ('t80');          !!!cp ('t80');
4419          !!!parse-error (type => 'after html:html', token => $token);          !!!parse-error (type => 'after html', text => 'html', token => $token);
4420          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4421        } else {        } else {
4422          !!!cp ('t81');          !!!cp ('t81');
# Line 4027  sub _tree_construction_main ($) { Line 4477  sub _tree_construction_main ($) {
4477                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
4478            !!!cp ('t87.2');            !!!cp ('t87.2');
4479            !!!parse-error (type => 'not closed',            !!!parse-error (type => 'not closed',
4480                            value => $self->{open_elements}->[-1]->[0]                            text => $self->{open_elements}->[-1]->[0]
4481                                ->manakai_local_name,                                ->manakai_local_name,
4482                            token => $token);                            token => $token);
4483    
# Line 4105  sub _tree_construction_main ($) { Line 4555  sub _tree_construction_main ($) {
4555        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4556          !!!cp ('t87.6');          !!!cp ('t87.6');
4557          !!!parse-error (type => 'not closed',          !!!parse-error (type => 'not closed',
4558                          value => $self->{open_elements}->[-1]->[0]                          text => $self->{open_elements}->[-1]->[0]
4559                              ->manakai_local_name,                              ->manakai_local_name,
4560                          token => $token);                          token => $token);
4561    
# Line 4126  sub _tree_construction_main ($) { Line 4576  sub _tree_construction_main ($) {
4576            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4577              !!!cp ('t88.2');              !!!cp ('t88.2');
4578              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4579                #
4580            } else {            } else {
4581              !!!cp ('t88.1');              !!!cp ('t88.1');
4582              ## Ignore the token.              ## Ignore the token.
4583              !!!next-token;              #
             next B;  
4584            }            }
4585            unless (length $token->{data}) {            unless (length $token->{data}) {
4586              !!!cp ('t88');              !!!cp ('t88');
4587              !!!next-token;              !!!next-token;
4588              next B;              next B;
4589            }            }
4590    ## TODO: set $token->{column} appropriately
4591          }          }
4592    
4593          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
# Line 4155  sub _tree_construction_main ($) { Line 4606  sub _tree_construction_main ($) {
4606            !!!cp ('t90');            !!!cp ('t90');
4607            ## As if </noscript>            ## As if </noscript>
4608            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4609            !!!parse-error (type => 'in noscript:#character', token => $token);            !!!parse-error (type => 'in noscript:#text', token => $token);
4610                        
4611            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4612            ## As if </head>            ## As if </head>
# Line 4192  sub _tree_construction_main ($) { Line 4643  sub _tree_construction_main ($) {
4643              next B;              next B;
4644            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4645              !!!cp ('t93.2');              !!!cp ('t93.2');
4646              !!!parse-error (type => 'after head:head', token => $token); ## TODO: error type              !!!parse-error (type => 'after head', text => 'head',
4647                                token => $token);
4648              ## Ignore the token              ## Ignore the token
4649              !!!nack ('t93.3');              !!!nack ('t93.3');
4650              !!!next-token;              !!!next-token;
4651              next B;              next B;
4652            } else {            } else {
4653              !!!cp ('t95');              !!!cp ('t95');
4654              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript              !!!parse-error (type => 'in head:head',
4655                                token => $token); # or in head noscript
4656              ## Ignore the token              ## Ignore the token
4657              !!!nack ('t95.1');              !!!nack ('t95.1');
4658              !!!next-token;              !!!next-token;
# Line 4224  sub _tree_construction_main ($) { Line 4677  sub _tree_construction_main ($) {
4677                  !!!cp ('t98');                  !!!cp ('t98');
4678                  ## As if </noscript>                  ## As if </noscript>
4679                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4680                  !!!parse-error (type => 'in noscript:base', token => $token);                  !!!parse-error (type => 'in noscript', text => 'base',
4681                                    token => $token);
4682                                
4683                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4684                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 4235  sub _tree_construction_main ($) { Line 4689  sub _tree_construction_main ($) {
4689                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4690                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4691                  !!!cp ('t100');                  !!!cp ('t100');
4692                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4693                                    text => $token->{tag_name}, token => $token);
4694                  push @{$self->{open_elements}},                  push @{$self->{open_elements}},
4695                      [$self->{head_element}, $el_category->{head}];                      [$self->{head_element}, $el_category->{head}];
4696                } else {                } else {
# Line 4252  sub _tree_construction_main ($) { Line 4707  sub _tree_construction_main ($) {
4707                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4708                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4709                  !!!cp ('t102');                  !!!cp ('t102');
4710                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4711                                    text => $token->{tag_name}, token => $token);
4712                  push @{$self->{open_elements}},                  push @{$self->{open_elements}},
4713                      [$self->{head_element}, $el_category->{head}];                      [$self->{head_element}, $el_category->{head}];
4714                } else {                } else {
# Line 4269  sub _tree_construction_main ($) { Line 4725  sub _tree_construction_main ($) {
4725                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4726                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4727                  !!!cp ('t104');                  !!!cp ('t104');
4728                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4729                                    text => $token->{tag_name}, token => $token);
4730                  push @{$self->{open_elements}},                  push @{$self->{open_elements}},
4731                      [$self->{head_element}, $el_category->{head}];                      [$self->{head_element}, $el_category->{head}];
4732                } else {                } else {
# Line 4338  sub _tree_construction_main ($) { Line 4795  sub _tree_construction_main ($) {
4795                  !!!cp ('t111');                  !!!cp ('t111');
4796                  ## As if </noscript>                  ## As if </noscript>
4797                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4798                  !!!parse-error (type => 'in noscript:title', token => $token);                  !!!parse-error (type => 'in noscript', text => 'title',
4799                                    token => $token);
4800                                
4801                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4802                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4803                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4804                  !!!cp ('t112');                  !!!cp ('t112');
4805                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4806                                    text => $token->{tag_name}, token => $token);
4807                  push @{$self->{open_elements}},                  push @{$self->{open_elements}},
4808                      [$self->{head_element}, $el_category->{head}];                      [$self->{head_element}, $el_category->{head}];
4809                } else {                } else {
# Line 4365  sub _tree_construction_main ($) { Line 4824  sub _tree_construction_main ($) {
4824                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4825                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4826                  !!!cp ('t114');                  !!!cp ('t114');
4827                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4828                                    text => $token->{tag_name}, token => $token);
4829                  push @{$self->{open_elements}},                  push @{$self->{open_elements}},
4830                      [$self->{head_element}, $el_category->{head}];                      [$self->{head_element}, $el_category->{head}];
4831                } else {                } else {
# Line 4386  sub _tree_construction_main ($) { Line 4846  sub _tree_construction_main ($) {
4846                  next B;                  next B;
4847                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4848                  !!!cp ('t117');                  !!!cp ('t117');
4849                  !!!parse-error (type => 'in noscript:noscript', token => $token);                  !!!parse-error (type => 'in noscript', text => 'noscript',
4850                                    token => $token);
4851                  ## Ignore the token                  ## Ignore the token
4852                  !!!nack ('t117.1');                  !!!nack ('t117.1');
4853                  !!!next-token;                  !!!next-token;
# Line 4400  sub _tree_construction_main ($) { Line 4861  sub _tree_construction_main ($) {
4861                  !!!cp ('t119');                  !!!cp ('t119');
4862                  ## As if </noscript>                  ## As if </noscript>
4863                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4864                  !!!parse-error (type => 'in noscript:script', token => $token);                  !!!parse-error (type => 'in noscript', text => 'script',
4865                                    token => $token);
4866                                
4867                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4868                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4869                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4870                  !!!cp ('t120');                  !!!cp ('t120');
4871                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4872                                    text => $token->{tag_name}, token => $token);
4873                  push @{$self->{open_elements}},                  push @{$self->{open_elements}},
4874                      [$self->{head_element}, $el_category->{head}];                      [$self->{head_element}, $el_category->{head}];
4875                } else {                } else {
# Line 4424  sub _tree_construction_main ($) { Line 4887  sub _tree_construction_main ($) {
4887                  !!!cp ('t122');                  !!!cp ('t122');
4888                  ## As if </noscript>                  ## As if </noscript>
4889                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4890                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'in noscript',
4891                                    text => $token->{tag_name}, token => $token);
4892                                    
4893                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4894                  ## As if </head>                  ## As if </head>
# Line 4463  sub _tree_construction_main ($) { Line 4927  sub _tree_construction_main ($) {
4927                !!!cp ('t129');                !!!cp ('t129');
4928                ## As if </noscript>                ## As if </noscript>
4929                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4930                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'in noscript:/',
4931                                  text => $token->{tag_name}, token => $token);
4932                                
4933                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4934                ## As if </head>                ## As if </head>
# Line 4506  sub _tree_construction_main ($) { Line 4971  sub _tree_construction_main ($) {
4971                  !!!cp ('t133');                  !!!cp ('t133');
4972                  ## As if </noscript>                  ## As if </noscript>
4973                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4974                  !!!parse-error (type => 'in noscript:/head', token => $token);                  !!!parse-error (type => 'in noscript:/',
4975                                    text => 'head', token => $token);
4976                                    
4977                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4978                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4521  sub _tree_construction_main ($) { Line 4987  sub _tree_construction_main ($) {
4987                  next B;                  next B;
4988                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4989                  !!!cp ('t134.1');                  !!!cp ('t134.1');
4990                  !!!parse-error (type => 'unmatched end tag:head', token => $token);                  !!!parse-error (type => 'unmatched end tag', text => 'head',
4991                                    token => $token);
4992                  ## Ignore the token                  ## Ignore the token
4993                  !!!next-token;                  !!!next-token;
4994                  next B;                  next B;
# Line 4538  sub _tree_construction_main ($) { Line 5005  sub _tree_construction_main ($) {
5005                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
5006                         $self->{insertion_mode} == AFTER_HEAD_IM) {                         $self->{insertion_mode} == AFTER_HEAD_IM) {
5007                  !!!cp ('t137');                  !!!cp ('t137');
5008                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);                  !!!parse-error (type => 'unmatched end tag',
5009                                    text => 'noscript', token => $token);
5010                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
5011                  !!!next-token;                  !!!next-token;
5012                  next B;                  next B;
# Line 4553  sub _tree_construction_main ($) { Line 5021  sub _tree_construction_main ($) {
5021                    $self->{insertion_mode} == IN_HEAD_IM or                    $self->{insertion_mode} == IN_HEAD_IM or
5022                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5023                  !!!cp ('t140');                  !!!cp ('t140');
5024                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5025                                    text => $token->{tag_name}, token => $token);
5026                  ## Ignore the token                  ## Ignore the token
5027                  !!!next-token;                  !!!next-token;
5028                  next B;                  next B;
5029                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
5030                  !!!cp ('t140.1');                  !!!cp ('t140.1');
5031                  !!!parse-error (type => 'unmatched end tag:' . $token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5032                                    text => $token->{tag_name}, token => $token);
5033                  ## Ignore the token                  ## Ignore the token
5034                  !!!next-token;                  !!!next-token;
5035                  next B;                  next B;
# Line 4568  sub _tree_construction_main ($) { Line 5038  sub _tree_construction_main ($) {
5038                }                }
5039              } elsif ($token->{tag_name} eq 'p') {              } elsif ($token->{tag_name} eq 'p') {
5040                !!!cp ('t142');                !!!cp ('t142');
5041                !!!parse-error (type => 'unmatched end tag:p', token => $token);                !!!parse-error (type => 'unmatched end tag',
5042                                  text => $token->{tag_name}, token => $token);
5043                ## Ignore the token                ## Ignore the token
5044                !!!next-token;                !!!next-token;
5045                next B;                next B;
# Line 4591  sub _tree_construction_main ($) { Line 5062  sub _tree_construction_main ($) {
5062                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5063                  !!!cp ('t143.3');                  !!!cp ('t143.3');
5064                  ## ISSUE: Two parse errors for <head><noscript></br>                  ## ISSUE: Two parse errors for <head><noscript></br>
5065                  !!!parse-error (type => 'unmatched end tag:br', token => $token);                  !!!parse-error (type => 'unmatched end tag',
5066                                    text => 'br', token => $token);
5067                  ## As if </noscript>                  ## As if </noscript>
5068                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5069                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
# Line 4610  sub _tree_construction_main ($) { Line 5082  sub _tree_construction_main ($) {
5082                }                }
5083    
5084                ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.                ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
5085                !!!parse-error (type => 'unmatched end tag:br', token => $token);                !!!parse-error (type => 'unmatched end tag',
5086                                  text => 'br', token => $token);
5087                ## Ignore the token                ## Ignore the token
5088                !!!next-token;                !!!next-token;
5089                next B;                next B;
5090              } else {              } else {
5091                !!!cp ('t145');                !!!cp ('t145');
5092                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
5093                                  text => $token->{tag_name}, token => $token);
5094                ## Ignore the token                ## Ignore the token
5095                !!!next-token;                !!!next-token;
5096                next B;                next B;
# Line 4626  sub _tree_construction_main ($) { Line 5100  sub _tree_construction_main ($) {
5100                !!!cp ('t146');                !!!cp ('t146');
5101                ## As if </noscript>                ## As if </noscript>
5102                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5103                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'in noscript:/',
5104                                  text => $token->{tag_name}, token => $token);
5105                                
5106                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
5107                ## As if </head>                ## As if </head>
# Line 4642  sub _tree_construction_main ($) { Line 5117  sub _tree_construction_main ($) {
5117              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5118  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
5119                !!!cp ('t148');                !!!cp ('t148');
5120                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
5121                                  text => $token->{tag_name}, token => $token);
5122                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
5123                !!!next-token;                !!!next-token;
5124                next B;                next B;
# Line 4753  sub _tree_construction_main ($) { Line 5229  sub _tree_construction_main ($) {
5229    
5230                  !!!cp ('t153');                  !!!cp ('t153');
5231                  !!!parse-error (type => 'start tag not allowed',                  !!!parse-error (type => 'start tag not allowed',
5232                      value => $token->{tag_name}, token => $token);                      text => $token->{tag_name}, token => $token);
5233                  ## Ignore the token                  ## Ignore the token
5234                  !!!nack ('t153.1');                  !!!nack ('t153.1');
5235                  !!!next-token;                  !!!next-token;
5236                  next B;                  next B;
5237                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5238                  !!!parse-error (type => 'not closed:caption', token => $token);                  !!!parse-error (type => 'not closed', text => 'caption',
5239                                    token => $token);
5240                                    
5241                  ## NOTE: As if </caption>.                  ## NOTE: As if </caption>.
5242                  ## have a table element in table scope                  ## have a table element in table scope
# Line 4779  sub _tree_construction_main ($) { Line 5256  sub _tree_construction_main ($) {
5256    
5257                    !!!cp ('t157');                    !!!cp ('t157');
5258                    !!!parse-error (type => 'start tag not allowed',                    !!!parse-error (type => 'start tag not allowed',
5259                                    value => $token->{tag_name}, token => $token);                                    text => $token->{tag_name}, token => $token);
5260                    ## Ignore the token                    ## Ignore the token
5261                    !!!nack ('t157.1');                    !!!nack ('t157.1');
5262                    !!!next-token;                    !!!next-token;
# Line 4796  sub _tree_construction_main ($) { Line 5273  sub _tree_construction_main ($) {
5273                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5274                    !!!cp ('t159');                    !!!cp ('t159');
5275                    !!!parse-error (type => 'not closed',                    !!!parse-error (type => 'not closed',
5276                                    value => $self->{open_elements}->[-1]->[0]                                    text => $self->{open_elements}->[-1]->[0]
5277                                        ->manakai_local_name,                                        ->manakai_local_name,
5278                                    token => $token);                                    token => $token);
5279                  } else {                  } else {
# Line 4838  sub _tree_construction_main ($) { Line 5315  sub _tree_construction_main ($) {
5315                  } # INSCOPE                  } # INSCOPE
5316                    unless (defined $i) {                    unless (defined $i) {
5317                      !!!cp ('t165');                      !!!cp ('t165');
5318                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag',
5319                                        text => $token->{tag_name},
5320                                        token => $token);
5321                      ## Ignore the token                      ## Ignore the token
5322                      !!!next-token;                      !!!next-token;
5323                      next B;                      next B;
# Line 4855  sub _tree_construction_main ($) { Line 5334  sub _tree_construction_main ($) {
5334                          ne $token->{tag_name}) {                          ne $token->{tag_name}) {
5335                    !!!cp ('t167');                    !!!cp ('t167');
5336                    !!!parse-error (type => 'not closed',                    !!!parse-error (type => 'not closed',
5337                                    value => $self->{open_elements}->[-1]->[0]                                    text => $self->{open_elements}->[-1]->[0]
5338                                        ->manakai_local_name,                                        ->manakai_local_name,
5339                                    token => $token);                                    token => $token);
5340                  } else {                  } else {
# Line 4872  sub _tree_construction_main ($) { Line 5351  sub _tree_construction_main ($) {
5351                  next B;                  next B;
5352                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5353                  !!!cp ('t169');                  !!!cp ('t169');
5354                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5355                                    text => $token->{tag_name}, token => $token);
5356                  ## Ignore the token                  ## Ignore the token
5357                  !!!next-token;                  !!!next-token;
5358                  next B;                  next B;
# Line 4899  sub _tree_construction_main ($) { Line 5379  sub _tree_construction_main ($) {
5379    
5380                    !!!cp ('t173');                    !!!cp ('t173');
5381                    !!!parse-error (type => 'unmatched end tag',                    !!!parse-error (type => 'unmatched end tag',
5382                                    value => $token->{tag_name}, token => $token);                                    text => $token->{tag_name}, token => $token);
5383                    ## Ignore the token                    ## Ignore the token
5384                    !!!next-token;                    !!!next-token;
5385                    next B;                    next B;
# Line 4915  sub _tree_construction_main ($) { Line 5395  sub _tree_construction_main ($) {
5395                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5396                    !!!cp ('t175');                    !!!cp ('t175');
5397                    !!!parse-error (type => 'not closed',                    !!!parse-error (type => 'not closed',
5398                                    value => $self->{open_elements}->[-1]->[0]                                    text => $self->{open_elements}->[-1]->[0]
5399                                        ->manakai_local_name,                                        ->manakai_local_name,
5400                                    token => $token);                                    token => $token);
5401                  } else {                  } else {
# Line 4932  sub _tree_construction_main ($) { Line 5412  sub _tree_construction_main ($) {
5412                  next B;                  next B;
5413                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5414                  !!!cp ('t177');                  !!!cp ('t177');
5415                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5416                                    text => $token->{tag_name}, token => $token);
5417                  ## Ignore the token                  ## Ignore the token
5418                  !!!next-token;                  !!!next-token;
5419                  next B;                  next B;
# Line 4975  sub _tree_construction_main ($) { Line 5456  sub _tree_construction_main ($) {
5456    
5457                  !!!cp ('t182');                  !!!cp ('t182');
5458                  !!!parse-error (type => 'unmatched end tag',                  !!!parse-error (type => 'unmatched end tag',
5459                      value => $token->{tag_name}, token => $token);                      text => $token->{tag_name}, token => $token);
5460                  ## Ignore the token                  ## Ignore the token
5461                  !!!next-token;                  !!!next-token;
5462                  next B;                  next B;
5463                } # INSCOPE                } # INSCOPE
5464              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5465                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5466                !!!parse-error (type => 'not closed:caption', token => $token);                !!!parse-error (type => 'not closed', text => 'caption',
5467                                  token => $token);
5468    
5469                ## As if </caption>                ## As if </caption>
5470                ## have a table element in table scope                ## have a table element in table scope
# Line 5000  sub _tree_construction_main ($) { Line 5482  sub _tree_construction_main ($) {
5482                } # INSCOPE                } # INSCOPE
5483                unless (defined $i) {                unless (defined $i) {
5484                  !!!cp ('t186');                  !!!cp ('t186');
5485                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);                  !!!parse-error (type => 'unmatched end tag',
5486                                    text => 'caption', token => $token);
5487                  ## Ignore the token                  ## Ignore the token
5488                  !!!next-token;                  !!!next-token;
5489                  next B;                  next B;
# Line 5015  sub _tree_construction_main ($) { Line 5498  sub _tree_construction_main ($) {
5498                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5499                  !!!cp ('t188');                  !!!cp ('t188');
5500                  !!!parse-error (type => 'not closed',                  !!!parse-error (type => 'not closed',
5501                                  value => $self->{open_elements}->[-1]->[0]                                  text => $self->{open_elements}->[-1]->[0]
5502                                      ->manakai_local_name,                                      ->manakai_local_name,
5503                                  token => $token);                                  token => $token);
5504                } else {                } else {
# Line 5035  sub _tree_construction_main ($) { Line 5518  sub _tree_construction_main ($) {
5518                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5519                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5520                  !!!cp ('t190');                  !!!cp ('t190');
5521                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5522                                    text => $token->{tag_name}, token => $token);
5523                  ## Ignore the token                  ## Ignore the token
5524                  !!!next-token;                  !!!next-token;
5525                  next B;                  next B;
# Line 5049  sub _tree_construction_main ($) { Line 5533  sub _tree_construction_main ($) {
5533                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5534                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5535                !!!cp ('t192');                !!!cp ('t192');
5536                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
5537                                  text => $token->{tag_name}, token => $token);
5538                ## Ignore the token                ## Ignore the token
5539                !!!next-token;                !!!next-token;
5540                next B;                next B;
# Line 5089  sub _tree_construction_main ($) { Line 5574  sub _tree_construction_main ($) {
5574            }            }
5575          }          }
5576    
5577              !!!parse-error (type => 'in table:#character', token => $token);          !!!parse-error (type => 'in table:#text', token => $token);
5578    
5579              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5580              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 5140  sub _tree_construction_main ($) { Line 5625  sub _tree_construction_main ($) {
5625          !!!next-token;          !!!next-token;
5626          next B;          next B;
5627        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5628              if ({          if ({
5629                   tr => ($self->{insertion_mode} != IN_ROW_IM),               tr => ($self->{insertion_mode} != IN_ROW_IM),
5630                   th => 1, td => 1,               th => 1, td => 1,
5631                  }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
5632                if ($self->{insertion_mode} == IN_TABLE_IM) {            if ($self->{insertion_mode} == IN_TABLE_IM) {
5633                  ## Clear back to table context              ## Clear back to table context
5634                  while (not ($self->{open_elements}->[-1]->[1]              while (not ($self->{open_elements}->[-1]->[1]
5635                                  & TABLE_SCOPING_EL)) {                              & TABLE_SCOPING_EL)) {
5636                    !!!cp ('t201');                !!!cp ('t201');
5637                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5638                  }              }
5639                                
5640                  !!!insert-element ('tbody',, $token);              !!!insert-element ('tbody',, $token);
5641                  $self->{insertion_mode} = IN_TABLE_BODY_IM;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5642                  ## reprocess in the "in table body" insertion mode...              ## reprocess in the "in table body" insertion mode...
5643                }            }
5644              
5645                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5646                  unless ($token->{tag_name} eq 'tr') {              unless ($token->{tag_name} eq 'tr') {
5647                    !!!cp ('t202');                !!!cp ('t202');
5648                    !!!parse-error (type => 'missing start tag:tr', token => $token);                !!!parse-error (type => 'missing start tag:tr', token => $token);
5649                  }              }
5650                                    
5651                  ## Clear back to table body context              ## Clear back to table body context
5652                  while (not ($self->{open_elements}->[-1]->[1]              while (not ($self->{open_elements}->[-1]->[1]
5653                                  & TABLE_ROWS_SCOPING_EL)) {                              & TABLE_ROWS_SCOPING_EL)) {
5654                    !!!cp ('t203');                !!!cp ('t203');
5655                    ## ISSUE: Can this case be reached?                ## ISSUE: Can this case be reached?
5656                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5657                  }              }
5658                                    
5659                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5660                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
# Line 5225  sub _tree_construction_main ($) { Line 5710  sub _tree_construction_main ($) {
5710                  unless (defined $i) {                  unless (defined $i) {
5711                    !!!cp ('t210');                    !!!cp ('t210');
5712  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5713                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmacthed end tag',
5714                                      text => $token->{tag_name}, token => $token);
5715                    ## Ignore the token                    ## Ignore the token
5716                    !!!nack ('t210.1');                    !!!nack ('t210.1');
5717                    !!!next-token;                    !!!next-token;
# Line 5269  sub _tree_construction_main ($) { Line 5755  sub _tree_construction_main ($) {
5755                  } # INSCOPE                  } # INSCOPE
5756                  unless (defined $i) {                  unless (defined $i) {
5757                    !!!cp ('t216');                    !!!cp ('t216');
5758  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type is wrong.
5759                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5760                                      text => $token->{tag_name}, token => $token);
5761                    ## Ignore the token                    ## Ignore the token
5762                    !!!nack ('t216.1');                    !!!nack ('t216.1');
5763                    !!!next-token;                    !!!next-token;
# Line 5345  sub _tree_construction_main ($) { Line 5832  sub _tree_construction_main ($) {
5832                }                }
5833              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5834                !!!parse-error (type => 'not closed',                !!!parse-error (type => 'not closed',
5835                                value => $self->{open_elements}->[-1]->[0]                                text => $self->{open_elements}->[-1]->[0]
5836                                    ->manakai_local_name,                                    ->manakai_local_name,
5837                                token => $token);                                token => $token);
5838    
# Line 5366  sub _tree_construction_main ($) { Line 5853  sub _tree_construction_main ($) {
5853                unless (defined $i) {                unless (defined $i) {
5854                  !!!cp ('t223');                  !!!cp ('t223');
5855  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5856                  !!!parse-error (type => 'unmatched end tag:table', token => $token);                  !!!parse-error (type => 'unmatched end tag', text => 'table',
5857                                    token => $token);
5858                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5859                  !!!nack ('t223.1');                  !!!nack ('t223.1');
5860                  !!!next-token;                  !!!next-token;
5861                  next B;                  next B;
5862                }                }
5863                                
5864  ## TODO: Followings are removed from the latest spec.  ## TODO: Followings are removed from the latest spec.
5865                ## generate implied end tags                ## generate implied end tags
5866                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5867                  !!!cp ('t224');                  !!!cp ('t224');
# Line 5384  sub _tree_construction_main ($) { Line 5872  sub _tree_construction_main ($) {
5872                  !!!cp ('t225');                  !!!cp ('t225');
5873                  ## NOTE: |<table><tr><table>|                  ## NOTE: |<table><tr><table>|
5874                  !!!parse-error (type => 'not closed',                  !!!parse-error (type => 'not closed',
5875                                  value => $self->{open_elements}->[-1]->[0]                                  text => $self->{open_elements}->[-1]->[0]
5876                                      ->manakai_local_name,                                      ->manakai_local_name,
5877                                  token => $token);                                  token => $token);
5878                } else {                } else {
# Line 5425  sub _tree_construction_main ($) { Line 5913  sub _tree_construction_main ($) {
5913                my $type = lc $token->{attributes}->{type}->{value};                my $type = lc $token->{attributes}->{type}->{value};
5914                if ($type eq 'hidden') {                if ($type eq 'hidden') {
5915                  !!!cp ('t227.3');                  !!!cp ('t227.3');
5916                  !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'in table',
5917                                    text => $token->{tag_name}, token => $token);
5918    
5919                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5920    
# Line 5453  sub _tree_construction_main ($) { Line 5942  sub _tree_construction_main ($) {
5942            #            #
5943          }          }
5944    
5945          !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'in table', text => $token->{tag_name},
5946                            token => $token);
5947    
5948          $insert = $insert_to_foster;          $insert = $insert_to_foster;
5949          #          #
# Line 5475  sub _tree_construction_main ($) { Line 5965  sub _tree_construction_main ($) {
5965                } # INSCOPE                } # INSCOPE
5966                unless (defined $i) {                unless (defined $i) {
5967                  !!!cp ('t230');                  !!!cp ('t230');
5968                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5969                                    text => $token->{tag_name}, token => $token);
5970                  ## Ignore the token                  ## Ignore the token
5971                  !!!nack ('t230.1');                  !!!nack ('t230.1');
5972                  !!!next-token;                  !!!next-token;
# Line 5516  sub _tree_construction_main ($) { Line 6007  sub _tree_construction_main ($) {
6007                  unless (defined $i) {                  unless (defined $i) {
6008                    !!!cp ('t235');                    !!!cp ('t235');
6009  ## TODO: The following is wrong.  ## TODO: The following is wrong.
6010                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
6011                                      text => $token->{type}, token => $token);
6012                    ## Ignore the token                    ## Ignore the token
6013                    !!!nack ('t236.1');                    !!!nack ('t236.1');
6014                    !!!next-token;                    !!!next-token;
# Line 5552  sub _tree_construction_main ($) { Line 6044  sub _tree_construction_main ($) {
6044                  } # INSCOPE                  } # INSCOPE
6045                  unless (defined $i) {                  unless (defined $i) {
6046                    !!!cp ('t239');                    !!!cp ('t239');
6047                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
6048                                      text => $token->{tag_name}, token => $token);
6049                    ## Ignore the token                    ## Ignore the token
6050                    !!!nack ('t239.1');                    !!!nack ('t239.1');
6051                    !!!next-token;                    !!!next-token;
# Line 5598  sub _tree_construction_main ($) { Line 6091  sub _tree_construction_main ($) {
6091                } # INSCOPE                } # INSCOPE
6092                unless (defined $i) {                unless (defined $i) {
6093                  !!!cp ('t243');                  !!!cp ('t243');
6094                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
6095                                    text => $token->{tag_name}, token => $token);
6096                  ## Ignore the token                  ## Ignore the token
6097                  !!!nack ('t243.1');                  !!!nack ('t243.1');
6098                  !!!next-token;                  !!!next-token;
# Line 5632  sub _tree_construction_main ($) { Line 6126  sub _tree_construction_main ($) {
6126                  } # INSCOPE                  } # INSCOPE
6127                    unless (defined $i) {                    unless (defined $i) {
6128                      !!!cp ('t249');                      !!!cp ('t249');
6129                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag',
6130                                        text => $token->{tag_name}, token => $token);
6131                      ## Ignore the token                      ## Ignore the token
6132                      !!!nack ('t249.1');                      !!!nack ('t249.1');
6133                      !!!next-token;                      !!!next-token;
# Line 5655  sub _tree_construction_main ($) { Line 6150  sub _tree_construction_main ($) {
6150                  } # INSCOPE                  } # INSCOPE
6151                    unless (defined $i) {                    unless (defined $i) {
6152                      !!!cp ('t252');                      !!!cp ('t252');
6153                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);                      !!!parse-error (type => 'unmatched end tag',
6154                                        text => 'tr', token => $token);
6155                      ## Ignore the token                      ## Ignore the token
6156                      !!!nack ('t252.1');                      !!!nack ('t252.1');
6157                      !!!next-token;                      !!!next-token;
# Line 5690  sub _tree_construction_main ($) { Line 6186  sub _tree_construction_main ($) {
6186                } # INSCOPE                } # INSCOPE
6187                unless (defined $i) {                unless (defined $i) {
6188                  !!!cp ('t256');                  !!!cp ('t256');
6189                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
6190                                    text => $token->{tag_name}, token => $token);
6191                  ## Ignore the token                  ## Ignore the token
6192                  !!!nack ('t256.1');                  !!!nack ('t256.1');
6193                  !!!next-token;                  !!!next-token;
# Line 5717  sub _tree_construction_main ($) { Line 6214  sub _tree_construction_main ($) {
6214                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
6215                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
6216            !!!cp ('t258');            !!!cp ('t258');
6217            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
6218                              text => $token->{tag_name}, token => $token);
6219            ## Ignore the token            ## Ignore the token
6220            !!!nack ('t258.1');            !!!nack ('t258.1');
6221             !!!next-token;             !!!next-token;
6222            next B;            next B;
6223          } else {          } else {
6224            !!!cp ('t259');            !!!cp ('t259');
6225            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in table:/',
6226                              text => $token->{tag_name}, token => $token);
6227    
6228            $insert = $insert_to_foster;            $insert = $insert_to_foster;
6229            #            #
# Line 5774  sub _tree_construction_main ($) { Line 6273  sub _tree_construction_main ($) {
6273              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
6274                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6275                  !!!cp ('t264');                  !!!cp ('t264');
6276                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);                  !!!parse-error (type => 'unmatched end tag',
6277                                    text => 'colgroup', token => $token);
6278                  ## Ignore the token                  ## Ignore the token
6279                  !!!next-token;                  !!!next-token;
6280                  next B;                  next B;
# Line 5787  sub _tree_construction_main ($) { Line 6287  sub _tree_construction_main ($) {
6287                }                }
6288              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
6289                !!!cp ('t266');                !!!cp ('t266');
6290                !!!parse-error (type => 'unmatched end tag:col', token => $token);                !!!parse-error (type => 'unmatched end tag',
6291                                  text => 'col', token => $token);
6292                ## Ignore the token                ## Ignore the token
6293                !!!next-token;                !!!next-token;
6294                next B;                next B;
# Line 5817  sub _tree_construction_main ($) { Line 6318  sub _tree_construction_main ($) {
6318            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6319              !!!cp ('t269');              !!!cp ('t269');
6320  ## TODO: Wrong error type?  ## TODO: Wrong error type?
6321              !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);              !!!parse-error (type => 'unmatched end tag',
6322                                text => 'colgroup', token => $token);
6323              ## Ignore the token              ## Ignore the token
6324              !!!nack ('t269.1');              !!!nack ('t269.1');
6325              !!!next-token;              !!!next-token;
# Line 5881  sub _tree_construction_main ($) { Line 6383  sub _tree_construction_main ($) {
6383                     tr => 1, td => 1, th => 1,                     tr => 1, td => 1, th => 1,
6384                    }->{$token->{tag_name}})) {                    }->{$token->{tag_name}})) {
6385            ## TODO: The type below is not good - <select> is replaced by </select>            ## TODO: The type below is not good - <select> is replaced by </select>
6386            !!!parse-error (type => 'not closed:select', token => $token);            !!!parse-error (type => 'not closed', text => 'select',
6387                              token => $token);
6388            ## NOTE: As if the token were </select> (<select> case) or            ## NOTE: As if the token were </select> (<select> case) or
6389            ## as if there were </select> (otherwise).            ## as if there were </select> (otherwise).
6390            ## have an element in table scope            ## have an element in table scope
# Line 5899  sub _tree_construction_main ($) { Line 6402  sub _tree_construction_main ($) {
6402            } # INSCOPE            } # INSCOPE
6403            unless (defined $i) {            unless (defined $i) {
6404              !!!cp ('t280');              !!!cp ('t280');
6405              !!!parse-error (type => 'unmatched end tag:select', token => $token);              !!!parse-error (type => 'unmatched end tag',
6406                                text => 'select', token => $token);
6407              ## Ignore the token              ## Ignore the token
6408              !!!nack ('t280.1');              !!!nack ('t280.1');
6409              !!!next-token;              !!!next-token;
# Line 5923  sub _tree_construction_main ($) { Line 6427  sub _tree_construction_main ($) {
6427            }            }
6428          } else {          } else {
6429            !!!cp ('t282');            !!!cp ('t282');
6430            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select',
6431                              text => $token->{tag_name}, token => $token);
6432            ## Ignore the token            ## Ignore the token
6433            !!!nack ('t282.1');            !!!nack ('t282.1');
6434            !!!next-token;            !!!next-token;
# Line 5941  sub _tree_construction_main ($) { Line 6446  sub _tree_construction_main ($) {
6446              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6447            } else {            } else {
6448              !!!cp ('t285');              !!!cp ('t285');
6449              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6450                                text => $token->{tag_name}, token => $token);
6451              ## Ignore the token              ## Ignore the token
6452            }            }
6453            !!!nack ('t285.1');            !!!nack ('t285.1');
# Line 5953  sub _tree_construction_main ($) { Line 6459  sub _tree_construction_main ($) {
6459              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6460            } else {            } else {
6461              !!!cp ('t287');              !!!cp ('t287');
6462              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6463                                text => $token->{tag_name}, token => $token);
6464              ## Ignore the token              ## Ignore the token
6465            }            }
6466            !!!nack ('t287.1');            !!!nack ('t287.1');
# Line 5975  sub _tree_construction_main ($) { Line 6482  sub _tree_construction_main ($) {
6482            } # INSCOPE            } # INSCOPE
6483            unless (defined $i) {            unless (defined $i) {
6484              !!!cp ('t290');              !!!cp ('t290');
6485              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6486                                text => $token->{tag_name}, token => $token);
6487              ## Ignore the token              ## Ignore the token
6488              !!!nack ('t290.1');              !!!nack ('t290.1');
6489              !!!next-token;              !!!next-token;
# Line 5996  sub _tree_construction_main ($) { Line 6504  sub _tree_construction_main ($) {
6504                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6505                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
6506  ## TODO: The following is wrong?  ## TODO: The following is wrong?
6507            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
6508                              text => $token->{tag_name}, token => $token);
6509                                
6510            ## have an element in table scope            ## have an element in table scope
6511            my $i;            my $i;
# Line 6037  sub _tree_construction_main ($) { Line 6546  sub _tree_construction_main ($) {
6546            unless (defined $i) {            unless (defined $i) {
6547              !!!cp ('t297');              !!!cp ('t297');
6548  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
6549              !!!parse-error (type => 'unmatched end tag:select', token => $token);              !!!parse-error (type => 'unmatched end tag',
6550                                text => 'select', token => $token);
6551              ## Ignore the </select> token              ## Ignore the </select> token
6552              !!!nack ('t297.1');              !!!nack ('t297.1');
6553              !!!next-token; ## TODO: ok?              !!!next-token; ## TODO: ok?
# Line 6054  sub _tree_construction_main ($) { Line 6564  sub _tree_construction_main ($) {
6564            next B;            next B;
6565          } else {          } else {
6566            !!!cp ('t299');            !!!cp ('t299');
6567            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select:/',
6568                              text => $token->{tag_name}, token => $token);
6569            ## Ignore the token            ## Ignore the token
6570            !!!nack ('t299.3');            !!!nack ('t299.3');
6571            !!!next-token;            !!!next-token;
# Line 6092  sub _tree_construction_main ($) { Line 6603  sub _tree_construction_main ($) {
6603                    
6604          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6605            !!!cp ('t301');            !!!cp ('t301');
6606            !!!parse-error (type => 'after html:#character', token => $token);            !!!parse-error (type => 'after html:#text', token => $token);
6607    
6608            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6609          } else {          } else {
# Line 6100  sub _tree_construction_main ($) { Line 6611  sub _tree_construction_main ($) {
6611          }          }
6612                    
6613          ## "after body" insertion mode          ## "after body" insertion mode
6614          !!!parse-error (type => 'after body:#character', token => $token);          !!!parse-error (type => 'after body:#text', token => $token);
6615    
6616          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6617          ## reprocess          ## reprocess
# Line 6108  sub _tree_construction_main ($) { Line 6619  sub _tree_construction_main ($) {
6619        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6620          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6621            !!!cp ('t303');            !!!cp ('t303');
6622            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after html',
6623                              text => $token->{tag_name}, token => $token);
6624                        
6625            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6626          } else {          } else {
# Line 6116  sub _tree_construction_main ($) { Line 6628  sub _tree_construction_main ($) {
6628          }          }
6629    
6630          ## "after body" insertion mode          ## "after body" insertion mode
6631          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'after body',
6632                            text => $token->{tag_name}, token => $token);
6633    
6634          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6635          !!!ack-later;          !!!ack-later;
# Line 6125  sub _tree_construction_main ($) { Line 6638  sub _tree_construction_main ($) {
6638        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6639          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6640            !!!cp ('t305');            !!!cp ('t305');
6641            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after html:/',
6642                              text => $token->{tag_name}, token => $token);
6643                        
6644            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6645            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
# Line 6137  sub _tree_construction_main ($) { Line 6651  sub _tree_construction_main ($) {
6651          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6652            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6653              !!!cp ('t307');              !!!cp ('t307');
6654              !!!parse-error (type => 'unmatched end tag:html', token => $token);              !!!parse-error (type => 'unmatched end tag',
6655                                text => 'html', token => $token);
6656              ## Ignore the token              ## Ignore the token
6657              !!!next-token;              !!!next-token;
6658              next B;              next B;
# Line 6149  sub _tree_construction_main ($) { Line 6664  sub _tree_construction_main ($) {
6664            }            }
6665          } else {          } else {
6666            !!!cp ('t309');            !!!cp ('t309');
6667            !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after body:/',
6668                              text => $token->{tag_name}, token => $token);
6669    
6670            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6671            ## reprocess            ## reprocess
# Line 6177  sub _tree_construction_main ($) { Line 6693  sub _tree_construction_main ($) {
6693          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6694            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6695              !!!cp ('t311');              !!!cp ('t311');
6696              !!!parse-error (type => 'in frameset:#character', token => $token);              !!!parse-error (type => 'in frameset:#text', token => $token);
6697            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6698              !!!cp ('t312');              !!!cp ('t312');
6699              !!!parse-error (type => 'after frameset:#character', token => $token);              !!!parse-error (type => 'after frameset:#text', token => $token);
6700            } else { # "after html frameset"            } else { # "after after frameset"
6701              !!!cp ('t313');              !!!cp ('t313');
6702              !!!parse-error (type => 'after html:#character', token => $token);              !!!parse-error (type => 'after html:#text', token => $token);
   
             $self->{insertion_mode} = AFTER_FRAMESET_IM;  
             ## Reprocess in the "after frameset" insertion mode.  
             !!!parse-error (type => 'after frameset:#character', token => $token);  
6703            }            }
6704                        
6705            ## Ignore the token.            ## Ignore the token.
# Line 6203  sub _tree_construction_main ($) { Line 6715  sub _tree_construction_main ($) {
6715                    
6716          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6717        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
         if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {  
           !!!cp ('t316');  
           !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);  
   
           $self->{insertion_mode} = AFTER_FRAMESET_IM;  
           ## Process in the "after frameset" insertion mode.  
         } else {  
           !!!cp ('t317');  
         }  
   
6718          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6719              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6720            !!!cp ('t318');            !!!cp ('t318');
# Line 6233  sub _tree_construction_main ($) { Line 6735  sub _tree_construction_main ($) {
6735            ## NOTE: As if in head.            ## NOTE: As if in head.
6736            $parse_rcdata->(CDATA_CONTENT_MODEL);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6737            next B;            next B;
6738    
6739              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6740              ## has no parse error.
6741          } else {          } else {
6742            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6743              !!!cp ('t321');              !!!cp ('t321');
6744              !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'in frameset',
6745            } else {                              text => $token->{tag_name}, token => $token);
6746              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6747              !!!cp ('t322');              !!!cp ('t322');
6748              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'after frameset',
6749                                text => $token->{tag_name}, token => $token);
6750              } else { # "after after frameset"
6751                !!!cp ('t322.2');
6752                !!!parse-error (type => 'after after frameset',
6753                                text => $token->{tag_name}, token => $token);
6754            }            }
6755            ## Ignore the token            ## Ignore the token
6756            !!!nack ('t322.1');            !!!nack ('t322.1');
# Line 6247  sub _tree_construction_main ($) { Line 6758  sub _tree_construction_main ($) {
6758            next B;            next B;
6759          }          }
6760        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
         if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {  
           !!!cp ('t323');  
           !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);  
   
           $self->{insertion_mode} = AFTER_FRAMESET_IM;  
           ## Process in the "after frameset" insertion mode.  
         } else {  
           !!!cp ('t324');  
         }  
   
6761          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6762              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6763            if ($self->{open_elements}->[-1]->[1] & HTML_EL and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6764                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6765              !!!cp ('t325');              !!!cp ('t325');
6766              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6767                                text => $token->{tag_name}, token => $token);
6768              ## Ignore the token              ## Ignore the token
6769              !!!next-token;              !!!next-token;
6770            } else {            } else {
# Line 6288  sub _tree_construction_main ($) { Line 6790  sub _tree_construction_main ($) {
6790          } else {          } else {
6791            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6792              !!!cp ('t330');              !!!cp ('t330');
6793              !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'in frameset:/',
6794            } else {                              text => $token->{tag_name}, token => $token);
6795              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6796                !!!cp ('t330.1');
6797                !!!parse-error (type => 'after frameset:/',
6798                                text => $token->{tag_name}, token => $token);
6799              } else { # "after after html"
6800              !!!cp ('t331');              !!!cp ('t331');
6801              !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'after after frameset:/',
6802                                text => $token->{tag_name}, token => $token);
6803            }            }
6804            ## Ignore the token            ## Ignore the token
6805            !!!next-token;            !!!next-token;
# Line 6399  sub _tree_construction_main ($) { Line 6907  sub _tree_construction_main ($) {
6907          $parse_rcdata->(RCDATA_CONTENT_MODEL);          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6908          next B;          next B;
6909        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6910          !!!parse-error (type => 'in body:body', token => $token);          !!!parse-error (type => 'in body', text => 'body', token => $token);
6911                                
6912          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6913              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
# Line 6519  sub _tree_construction_main ($) { Line 7027  sub _tree_construction_main ($) {
7027              if ($i != -1) {              if ($i != -1) {
7028                !!!cp ('t355');                !!!cp ('t355');
7029                !!!parse-error (type => 'not closed',                !!!parse-error (type => 'not closed',
7030                                value => $self->{open_elements}->[-1]->[0]                                text => $self->{open_elements}->[-1]->[0]
7031                                    ->manakai_local_name,                                    ->manakai_local_name,
7032                                token => $token);                                token => $token);
7033              } else {              } else {
# Line 6787  sub _tree_construction_main ($) { Line 7295  sub _tree_construction_main ($) {
7295            ## Ignore the token            ## Ignore the token
7296          } else {          } else {
7297            !!!cp ('t398');            !!!cp ('t398');
7298            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
7299          }          }
7300          !!!next-token;          !!!next-token;
7301          next B;          next B;
7302          } elsif ($token->{tag_name} eq 'rt' or
7303                   $token->{tag_name} eq 'rp') {
7304            ## has a |ruby| element in scope
7305            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7306              my $node = $self->{open_elements}->[$_];
7307              if ($node->[1] & RUBY_EL) {
7308                !!!cp ('t398.1');
7309                ## generate implied end tags
7310                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7311                  !!!cp ('t398.2');
7312                  pop @{$self->{open_elements}};
7313                }
7314                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
7315                  !!!cp ('t398.3');
7316                  !!!parse-error (type => 'not closed',
7317                                  text => $self->{open_elements}->[-1]->[0]
7318                                      ->manakai_local_name,
7319                                  token => $token);
7320                  pop @{$self->{open_elements}}
7321                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
7322                }
7323                last INSCOPE;
7324              } elsif ($node->[1] & SCOPING_EL) {
7325                !!!cp ('t398.4');
7326                last INSCOPE;
7327              }
7328            } # INSCOPE
7329    
7330            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7331    
7332            !!!nack ('t398.5');
7333            !!!next-token;
7334            redo B;
7335        } elsif ($token->{tag_name} eq 'math' or        } elsif ($token->{tag_name} eq 'math' or
7336                 $token->{tag_name} eq 'svg') {                 $token->{tag_name} eq 'svg') {
7337          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7338    
7339            ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
7340    
7341          ## "adjust SVG attributes" ('svg' only) - done in insert-element-f          ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
7342    
7343          ## "adjust foreign attributes" - done in insert-element-f          ## "adjust foreign attributes" - done in insert-element-f
# Line 6821  sub _tree_construction_main ($) { Line 7364  sub _tree_construction_main ($) {
7364                  thead => 1, tr => 1,                  thead => 1, tr => 1,
7365                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7366          !!!cp ('t401');          !!!cp ('t401');
7367          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'in body',
7368                            text => $token->{tag_name}, token => $token);
7369          ## Ignore the token          ## Ignore the token
7370          !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.          !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7371          !!!next-token;          !!!next-token;
# Line 6906  sub _tree_construction_main ($) { Line 7450  sub _tree_construction_main ($) {
7450            }            }
7451    
7452            !!!parse-error (type => 'start tag not allowed',            !!!parse-error (type => 'start tag not allowed',
7453                            value => $token->{tag_name}, token => $token);                            text => $token->{tag_name}, token => $token);
7454            ## NOTE: Ignore the token.            ## NOTE: Ignore the token.
7455            !!!next-token;            !!!next-token;
7456            next B;            next B;
# Line 6916  sub _tree_construction_main ($) { Line 7460  sub _tree_construction_main ($) {
7460            unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {            unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7461              !!!cp ('t403');              !!!cp ('t403');
7462              !!!parse-error (type => 'not closed',              !!!parse-error (type => 'not closed',
7463                              value => $_->[0]->manakai_local_name,                              text => $_->[0]->manakai_local_name,
7464                              token => $token);                              token => $token);
7465              last;              last;
7466            } else {            } else {
# Line 6936  sub _tree_construction_main ($) { Line 7480  sub _tree_construction_main ($) {
7480            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7481              !!!cp ('t406');              !!!cp ('t406');
7482              !!!parse-error (type => 'not closed',              !!!parse-error (type => 'not closed',
7483                              value => $self->{open_elements}->[1]->[0]                              text => $self->{open_elements}->[1]->[0]
7484                                  ->manakai_local_name,                                  ->manakai_local_name,
7485                              token => $token);                              token => $token);
7486            } else {            } else {
# Line 6947  sub _tree_construction_main ($) { Line 7491  sub _tree_construction_main ($) {
7491            next B;            next B;
7492          } else {          } else {
7493            !!!cp ('t408');            !!!cp ('t408');
7494            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7495                              text => $token->{tag_name}, token => $token);
7496            ## Ignore the token            ## Ignore the token
7497            !!!next-token;            !!!next-token;
7498            next B;            next B;
# Line 6975  sub _tree_construction_main ($) { Line 7520  sub _tree_construction_main ($) {
7520    
7521          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7522            !!!cp ('t413');            !!!cp ('t413');
7523            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7524                              text => $token->{tag_name}, token => $token);
7525              ## NOTE: Ignore the token.
7526          } else {          } else {
7527            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7528            while ({            while ({
7529                      ## END_TAG_OPTIONAL_EL
7530                    dd => ($token->{tag_name} ne 'dd'),                    dd => ($token->{tag_name} ne 'dd'),
7531                    dt => ($token->{tag_name} ne 'dt'),                    dt => ($token->{tag_name} ne 'dt'),
7532                    li => ($token->{tag_name} ne 'li'),                    li => ($token->{tag_name} ne 'li'),
7533                    p => 1,                    p => 1,
7534                      rt => 1,
7535                      rp => 1,
7536                   }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {                   }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7537              !!!cp ('t409');              !!!cp ('t409');
7538              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
# Line 6993  sub _tree_construction_main ($) { Line 7543  sub _tree_construction_main ($) {
7543                    ne $token->{tag_name}) {                    ne $token->{tag_name}) {
7544              !!!cp ('t412');              !!!cp ('t412');
7545              !!!parse-error (type => 'not closed',              !!!parse-error (type => 'not closed',
7546                              value => $self->{open_elements}->[-1]->[0]                              text => $self->{open_elements}->[-1]->[0]
7547                                  ->manakai_local_name,                                  ->manakai_local_name,
7548                              token => $token);                              token => $token);
7549            } else {            } else {
# Line 7030  sub _tree_construction_main ($) { Line 7580  sub _tree_construction_main ($) {
7580    
7581          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7582            !!!cp ('t421');            !!!cp ('t421');
7583            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7584                              text => $token->{tag_name}, token => $token);
7585              ## NOTE: Ignore the token.
7586          } else {          } else {
7587            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7588            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
# Line 7043  sub _tree_construction_main ($) { Line 7595  sub _tree_construction_main ($) {
7595                    ne $token->{tag_name}) {                    ne $token->{tag_name}) {
7596              !!!cp ('t417.1');              !!!cp ('t417.1');
7597              !!!parse-error (type => 'not closed',              !!!parse-error (type => 'not closed',
7598                              value => $self->{open_elements}->[-1]->[0]                              text => $self->{open_elements}->[-1]->[0]
7599                                  ->manakai_local_name,                                  ->manakai_local_name,
7600                              token => $token);                              token => $token);
7601            } else {            } else {
# Line 7075  sub _tree_construction_main ($) { Line 7627  sub _tree_construction_main ($) {
7627    
7628          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7629            !!!cp ('t425.1');            !!!cp ('t425.1');
7630            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7631                              text => $token->{tag_name}, token => $token);
7632              ## NOTE: Ignore the token.
7633          } else {          } else {
7634            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7635            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
# Line 7087  sub _tree_construction_main ($) { Line 7641  sub _tree_construction_main ($) {
7641            if ($self->{open_elements}->[-1]->[0]->manakai_local_name            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7642                    ne $token->{tag_name}) {                    ne $token->{tag_name}) {
7643              !!!cp ('t425');              !!!cp ('t425');
7644              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
7645                                text => $token->{tag_name}, token => $token);
7646            } else {            } else {
7647              !!!cp ('t426');              !!!cp ('t426');
7648            }            }
# Line 7118  sub _tree_construction_main ($) { Line 7673  sub _tree_construction_main ($) {
7673                    ne $token->{tag_name}) {                    ne $token->{tag_name}) {
7674              !!!cp ('t412.1');              !!!cp ('t412.1');
7675              !!!parse-error (type => 'not closed',              !!!parse-error (type => 'not closed',
7676                              value => $self->{open_elements}->[-1]->[0]                              text => $self->{open_elements}->[-1]->[0]
7677                                  ->manakai_local_name,                                  ->manakai_local_name,
7678                              token => $token);                              token => $token);
7679            } else {            } else {
# Line 7128  sub _tree_construction_main ($) { Line 7683  sub _tree_construction_main ($) {
7683            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7684          } else {          } else {
7685            !!!cp ('t413.1');            !!!cp ('t413.1');
7686            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7687                              text => $token->{tag_name}, token => $token);
7688    
7689            !!!cp ('t415.1');            !!!cp ('t415.1');
7690            ## As if <p>, then reprocess the current token            ## As if <p>, then reprocess the current token
# Line 7151  sub _tree_construction_main ($) { Line 7707  sub _tree_construction_main ($) {
7707          next B;          next B;
7708        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7709          !!!cp ('t428');          !!!cp ('t428');
7710          !!!parse-error (type => 'unmatched end tag:br', token => $token);          !!!parse-error (type => 'unmatched end tag',
7711                            text => 'br', token => $token);
7712    
7713          ## As if <br>          ## As if <br>
7714          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
# Line 7176  sub _tree_construction_main ($) { Line 7733  sub _tree_construction_main ($) {
7733                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7734                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7735          !!!cp ('t429');          !!!cp ('t429');
7736          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'unmatched end tag',
7737                            text => $token->{tag_name}, token => $token);
7738          ## Ignore the token          ## Ignore the token
7739          !!!next-token;          !!!next-token;
7740          next B;          next B;
# Line 7195  sub _tree_construction_main ($) { Line 7753  sub _tree_construction_main ($) {
7753              ## generate implied end tags              ## generate implied end tags
7754              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7755                !!!cp ('t430');                !!!cp ('t430');
7756                ## ISSUE: Can this case be reached?                ## NOTE: |<ruby><rt></ruby>|.
7757                  ## ISSUE: <ruby><rt></rt> will also take this code path,
7758                  ## which seems wrong.
7759                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
7760                  $node_i++;
7761              }              }
7762                    
7763              ## Step 2              ## Step 2
# Line 7205  sub _tree_construction_main ($) { Line 7766  sub _tree_construction_main ($) {
7766                !!!cp ('t431');                !!!cp ('t431');
7767                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7768                !!!parse-error (type => 'not closed',                !!!parse-error (type => 'not closed',
7769                                value => $self->{open_elements}->[-1]->[0]                                text => $self->{open_elements}->[-1]->[0]
7770                                    ->manakai_local_name,                                    ->manakai_local_name,
7771                                token => $token);                                token => $token);
7772              } else {              } else {
# Line 7213  sub _tree_construction_main ($) { Line 7774  sub _tree_construction_main ($) {
7774              }              }
7775                            
7776              ## Step 3              ## Step 3
7777              splice @{$self->{open_elements}}, $node_i;              splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7778    
7779              !!!next-token;              !!!next-token;
7780              last S2;              last S2;
# Line 7224  sub _tree_construction_main ($) { Line 7785  sub _tree_construction_main ($) {
7785                  ($node->[1] & SPECIAL_EL or                  ($node->[1] & SPECIAL_EL or
7786                   $node->[1] & SCOPING_EL)) {                   $node->[1] & SCOPING_EL)) {
7787                !!!cp ('t433');                !!!cp ('t433');
7788                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
7789                                  text => $token->{tag_name}, token => $token);
7790                ## Ignore the token                ## Ignore the token
7791                !!!next-token;                !!!next-token;
7792                last S2;                last S2;
# Line 7270  sub _tree_construction_main ($) { Line 7832  sub _tree_construction_main ($) {
7832    ## TODO: script stuffs    ## TODO: script stuffs
7833  } # _tree_construct_main  } # _tree_construct_main
7834    
7835  sub set_inner_html ($$$) {  sub set_inner_html ($$$$;$) {
7836    my $class = shift;    my $class = shift;
7837    my $node = shift;    my $node = shift;
7838    my $s = \$_[0];    #my $s = \$_[0];
7839    my $onerror = $_[1];    my $onerror = $_[1];
7840      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7841    
7842    ## ISSUE: Should {confident} be true?    ## ISSUE: Should {confident} be true?
7843    
# Line 7293  sub set_inner_html ($$$) { Line 7856  sub set_inner_html ($$$) {
7856      }      }
7857    
7858      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7859      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($_[0] => $node, $onerror, $get_wrapper);
7860    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7861      ## TODO: If non-html element      ## TODO: If non-html element
7862    
7863      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7864    
7865    ## TODO: Support for $get_wrapper
7866    
7867      ## Step 1 # MUST      ## Step 1 # MUST
7868      my $this_doc = $node->owner_document;      my $this_doc = $node->owner_document;
7869      my $doc = $this_doc->implementation->create_document;      my $doc = $this_doc->implementation->create_document;
# Line 7310  sub set_inner_html ($$$) { Line 7875  sub set_inner_html ($$$) {
7875      my $i = 0;      my $i = 0;
7876      $p->{line_prev} = $p->{line} = 1;      $p->{line_prev} = $p->{line} = 1;
7877      $p->{column_prev} = $p->{column} = 0;      $p->{column_prev} = $p->{column} = 0;
7878      $p->{set_next_char} = sub {      require Whatpm::Charset::DecodeHandle;
7879        my $input = Whatpm::Charset::DecodeHandle::CharString->new (\($_[0]));
7880        $input = $get_wrapper->($input);
7881        $p->{set_nc} = sub {
7882        my $self = shift;        my $self = shift;
7883    
7884        pop @{$self->{prev_char}};        my $char = '';
7885        unshift @{$self->{prev_char}}, $self->{next_char};        if (defined $self->{next_nc}) {
7886            $char = $self->{next_nc};
7887        $self->{next_char} = -1 and return if $i >= length $$s;          delete $self->{next_nc};
7888        $self->{next_char} = ord substr $$s, $i++, 1;          $self->{nc} = ord $char;
7889          } else {
7890            $self->{char_buffer} = '';
7891            $self->{char_buffer_pos} = 0;
7892            
7893            my $count = $input->manakai_read_until
7894                ($self->{char_buffer}, qr/[^\x00\x0A\x0D]/,
7895                 $self->{char_buffer_pos});
7896            if ($count) {
7897              $self->{line_prev} = $self->{line};
7898              $self->{column_prev} = $self->{column};
7899              $self->{column}++;
7900              $self->{nc}
7901                  = ord substr ($self->{char_buffer},
7902                                $self->{char_buffer_pos}++, 1);
7903              return;
7904            }
7905            
7906            if ($input->read ($char, 1)) {
7907              $self->{nc} = ord $char;
7908            } else {
7909              $self->{nc} = -1;
7910              return;
7911            }
7912          }
7913    
7914        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7915        $p->{column}++;        $p->{column}++;
7916    
7917        if ($self->{next_char} == 0x000A) { # LF        if ($self->{nc} == 0x000A) { # LF
7918          $p->{line}++;          $p->{line}++;
7919          $p->{column} = 0;          $p->{column} = 0;
7920          !!!cp ('i1');          !!!cp ('i1');
7921        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{nc} == 0x000D) { # CR
7922          $i++ if substr ($$s, $i, 1) eq "\x0A";  ## TODO: support for abort/streaming
7923          $self->{next_char} = 0x000A; # LF # MUST          my $next = '';
7924            if ($input->read ($next, 1) and $next ne "\x0A") {
7925              $self->{next_nc} = $next;
7926            }
7927            $self->{nc} = 0x000A; # LF # MUST
7928          $p->{line}++;          $p->{line}++;
7929          $p->{column} = 0;          $p->{column} = 0;
7930          !!!cp ('i2');          !!!cp ('i2');
7931        } 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  
7932          !!!cp ('i4');          !!!cp ('i4');
7933          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7934          $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');  
         !!!parse-error (type => 'control char', level => $self->{must_level});  
 ## TODO: error type documentation  
7935        }        }
7936      };      };
7937      $p->{prev_char} = [-1, -1, -1];  
7938      $p->{next_char} = -1;      $p->{read_until} = sub {
7939              #my ($scalar, $specials_range, $offset) = @_;
7940          return 0 if defined $p->{next_nc};
7941    
7942          my $pattern = qr/[^$_[1]\x00\x0A\x0D]/;
7943          my $offset = $_[2] || 0;
7944          
7945          if ($p->{char_buffer_pos} < length $p->{char_buffer}) {
7946            pos ($p->{char_buffer}) = $p->{char_buffer_pos};
7947            if ($p->{char_buffer} =~ /\G(?>$pattern)+/) {
7948              substr ($_[0], $offset)
7949                  = substr ($p->{char_buffer}, $-[0], $+[0] - $-[0]);
7950              my $count = $+[0] - $-[0];
7951              if ($count) {
7952                $p->{column} += $count;
7953                $p->{char_buffer_pos} += $count;
7954                $p->{line_prev} = $p->{line};
7955                $p->{column_prev} = $p->{column} - 1;
7956                $p->{nc} = -1;
7957              }
7958              return $count;
7959            } else {
7960              return 0;
7961            }
7962          } else {
7963            my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
7964            if ($count) {
7965              $p->{column} += $count;
7966              $p->{column_prev} += $count;
7967              $p->{nc} = -1;
7968            }
7969            return $count;
7970          }
7971        }; # $p->{read_until}
7972    
7973      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7974        my (%opt) = @_;        my (%opt) = @_;
7975        my $line = $opt{line};        my $line = $opt{line};
# Line 7381  sub set_inner_html ($$$) { Line 7984  sub set_inner_html ($$$) {
7984        $ponerror->(line => $p->{line}, column => $p->{column}, @_);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7985      };      };
7986            
7987        my $char_onerror = sub {
7988          my (undef, $type, %opt) = @_;
7989          $ponerror->(layer => 'encode',
7990                      line => $p->{line}, column => $p->{column} + 1,
7991                      %opt, type => $type);
7992        }; # $char_onerror
7993        $input->onerror ($char_onerror);
7994    
7995      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7996      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7997    

Legend:
Removed from v.1.150  
changed lines
  Added in v.1.185

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24