/[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.141 by wakaba, Sat May 24 10:18:26 2008 UTC revision 1.189 by wakaba, Sat Sep 20 11:25:56 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 ('');
20  ## alert (doc.compatMode);  ## alert (doc.compatMode);
21    
 ## TODO: 1252 parse error (revision 1264)  
 ## TODO: 8859-11 = 874 (revision 1271)  
   
22  require IO::Handle;  require IO::Handle;
23    
24  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
# Line 48  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 55  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 99  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 173  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 214  my $el_category_f = { Line 242  my $el_category_f = {
242  };  };
243    
244  my $svg_attr_name = {  my $svg_attr_name = {
245      attributename => 'attributeName',
246    attributetype => 'attributeType',    attributetype => 'attributeType',
247    basefrequency => 'baseFrequency',    basefrequency => 'baseFrequency',
248    baseprofile => 'baseProfile',    baseprofile => 'baseProfile',
# Line 224  my $svg_attr_name = { Line 253  my $svg_attr_name = {
253    diffuseconstant => 'diffuseConstant',    diffuseconstant => 'diffuseConstant',
254    edgemode => 'edgeMode',    edgemode => 'edgeMode',
255    externalresourcesrequired => 'externalResourcesRequired',    externalresourcesrequired => 'externalResourcesRequired',
   fecolormatrix => 'feColorMatrix',  
   fecomposite => 'feComposite',  
   fegaussianblur => 'feGaussianBlur',  
   femorphology => 'feMorphology',  
   fetile => 'feTile',  
256    filterres => 'filterRes',    filterres => 'filterRes',
257    filterunits => 'filterUnits',    filterunits => 'filterUnits',
258    glyphref => 'glyphRef',    glyphref => 'glyphRef',
# Line 262  my $svg_attr_name = { Line 286  my $svg_attr_name = {
286    repeatcount => 'repeatCount',    repeatcount => 'repeatCount',
287    repeatdur => 'repeatDur',    repeatdur => 'repeatDur',
288    requiredextensions => 'requiredExtensions',    requiredextensions => 'requiredExtensions',
289      requiredfeatures => 'requiredFeatures',
290    specularconstant => 'specularConstant',    specularconstant => 'specularConstant',
291    specularexponent => 'specularExponent',    specularexponent => 'specularExponent',
292    spreadmethod => 'spreadMethod',    spreadmethod => 'spreadMethod',
# Line 340  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 351  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 358  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 385  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 418  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 429  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 440  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 452  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 478  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 487  sub parse_byte_stream ($$$$;$) { Line 532  sub parse_byte_stream ($$$$;$) {
532        ## "Change the encoding" algorithm:        ## "Change the encoding" algorithm:
533    
534        ## Step 1            ## Step 1    
535        if ($charset->{iana_names}->{'utf-16'}) { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?        if ($charset->{category} &
536          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');            Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
537            $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 498  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 522  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 566  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 586  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 664  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 677  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 712  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 723  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 746  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 799  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 817  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 829  sub _initialize_tokenizer ($) { Line 955  sub _initialize_tokenizer ($) {
955  ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|  ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
956  ##     while the token is pushed back to the stack.  ##     while the token is pushed back to the stack.
957    
 ## ISSUE: "When a DOCTYPE token is created, its  
 ## <i>self-closing flag</i> must be unset (its other state is that it  
 ## be set), and its attributes list must be empty.": Wrong subject?  
   
958  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
959    
960  ## Before each step, UA MAY check to see if either one of the scripts in  ## Before each step, UA MAY check to see if either one of the scripts in
# Line 841  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.)
968  ## contains some requirements that are not detected by the  
969  ## parsing algorithm:  my $is_space = {
970  ## - Some requirements on character encoding declarations. ## TODO    0x0009 => 1, # CHARACTER TABULATION (HT)
971  ## - "Elements MUST NOT contain content that their content model disallows."    0x000A => 1, # LINE FEED (LF)
972  ##   ... Some are parse error, some are not (will be reported by c.c.).    #0x000B => 0, # LINE TABULATION (VT)
973  ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO    0x000C => 1, # FORM FEED (FF)
974  ## - Text (in elements, attributes, and comments) SHOULD NOT contain    #0x000D => 1, # CARRIAGE RETURN (CR)
975  ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)    0x0020 => 1, # SPACE (SP)
976    };
 ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot  
 ## be detected by the HTML5 parsing algorithm:  
 ## - Text,  
977    
978  sub _get_next_token ($) {  sub _get_next_token ($) {
979    my $self = shift;    my $self = shift;
980    
981    if ($self->{self_closing}) {    if ($self->{self_closing}) {
982      !!!parse-error (type => 'nestc', token => $self->{current_token});      !!!parse-error (type => 'nestc', token => $self->{ct});
983      ## NOTE: The |self_closing| flag is only set by start tag token.      ## NOTE: The |self_closing| flag is only set by start tag token.
984      ## 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
985      ## |current_token|.      ## |ct|.
986      delete $self->{self_closing};      delete $self->{self_closing};
987    }    }
988    
# Line 873  sub _get_next_token ($) { Line 992  sub _get_next_token ($) {
992    }    }
993    
994    A: {    A: {
995      if ($self->{state} == DATA_STATE) {      if ($self->{state} == PCDATA_STATE) {
996        if ($self->{next_char} == 0x0026) { # &        ## NOTE: Same as |DATA_STATE|, but only for |PCDATA| content model.
997    
998          if ($self->{nc} == 0x0026) { # &
999            !!!cp (0.1);
1000            ## NOTE: In the spec, the tokenizer is switched to the
1001            ## "entity data state".  In this implementation, the tokenizer
1002            ## is switched to the |ENTITY_STATE|, which is an implementation
1003            ## of the "consume a character reference" algorithm.
1004            $self->{entity_add} = -1;
1005            $self->{prev_state} = DATA_STATE;
1006            $self->{state} = ENTITY_STATE;
1007            !!!next-input-character;
1008            redo A;
1009          } elsif ($self->{nc} == 0x003C) { # <
1010            !!!cp (0.2);
1011            $self->{state} = TAG_OPEN_STATE;
1012            !!!next-input-character;
1013            redo A;
1014          } elsif ($self->{nc} == -1) {
1015            !!!cp (0.3);
1016            !!!emit ({type => END_OF_FILE_TOKEN,
1017                      line => $self->{line}, column => $self->{column}});
1018            last A; ## TODO: ok?
1019          } else {
1020            !!!cp (0.4);
1021            #
1022          }
1023    
1024          # Anything else
1025          my $token = {type => CHARACTER_TOKEN,
1026                       data => chr $self->{nc},
1027                       line => $self->{line}, column => $self->{column},
1028                      };
1029          $self->{read_until}->($token->{data}, q[<&], length $token->{data});
1030    
1031          ## Stay in the state.
1032          !!!next-input-character;
1033          !!!emit ($token);
1034          redo A;
1035        } elsif ($self->{state} == DATA_STATE) {
1036          $self->{s_kwd} = '' unless defined $self->{s_kwd};
1037          if ($self->{nc} == 0x0026) { # &
1038            $self->{s_kwd} = '';
1039          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
1040              not $self->{escape}) {              not $self->{escape}) {
1041            !!!cp (1);            !!!cp (1);
1042            $self->{state} = ENTITY_DATA_STATE;            ## NOTE: In the spec, the tokenizer is switched to the
1043              ## "entity data state".  In this implementation, the tokenizer
1044              ## is switched to the |ENTITY_STATE|, which is an implementation
1045              ## of the "consume a character reference" algorithm.
1046              $self->{entity_add} = -1;
1047              $self->{prev_state} = DATA_STATE;
1048              $self->{state} = ENTITY_STATE;
1049            !!!next-input-character;            !!!next-input-character;
1050            redo A;            redo A;
1051          } else {          } else {
1052            !!!cp (2);            !!!cp (2);
1053            #            #
1054          }          }
1055        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{nc} == 0x002D) { # -
1056          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1057            unless ($self->{escape}) {            $self->{s_kwd} .= '-';
1058              if ($self->{prev_char}->[0] == 0x002D and # -            
1059                  $self->{prev_char}->[1] == 0x0021 and # !            if ($self->{s_kwd} eq '<!--') {
1060                  $self->{prev_char}->[2] == 0x003C) { # <              !!!cp (3);
1061                !!!cp (3);              $self->{escape} = 1; # unless $self->{escape};
1062                $self->{escape} = 1;              $self->{s_kwd} = '--';
1063              } else {              #
1064                !!!cp (4);            } elsif ($self->{s_kwd} eq '---') {
1065              }              !!!cp (4);
1066                $self->{s_kwd} = '--';
1067                #
1068            } else {            } else {
1069              !!!cp (5);              !!!cp (5);
1070                #
1071            }            }
1072          }          }
1073                    
1074          #          #
1075        } elsif ($self->{next_char} == 0x003C) { # <        } elsif ($self->{nc} == 0x0021) { # !
1076            if (length $self->{s_kwd}) {
1077              !!!cp (5.1);
1078              $self->{s_kwd} .= '!';
1079              #
1080            } else {
1081              !!!cp (5.2);
1082              #$self->{s_kwd} = '';
1083              #
1084            }
1085            #
1086          } elsif ($self->{nc} == 0x003C) { # <
1087          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
1088              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
1089               not $self->{escape})) {               not $self->{escape})) {
# Line 912  sub _get_next_token ($) { Line 1093  sub _get_next_token ($) {
1093            redo A;            redo A;
1094          } else {          } else {
1095            !!!cp (7);            !!!cp (7);
1096              $self->{s_kwd} = '';
1097            #            #
1098          }          }
1099        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1100          if ($self->{escape} and          if ($self->{escape} and
1101              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
1102            if ($self->{prev_char}->[0] == 0x002D and # -            if ($self->{s_kwd} eq '--') {
               $self->{prev_char}->[1] == 0x002D) { # -  
1103              !!!cp (8);              !!!cp (8);
1104              delete $self->{escape};              delete $self->{escape};
1105            } else {            } else {
# Line 928  sub _get_next_token ($) { Line 1109  sub _get_next_token ($) {
1109            !!!cp (10);            !!!cp (10);
1110          }          }
1111                    
1112            $self->{s_kwd} = '';
1113          #          #
1114        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1115          !!!cp (11);          !!!cp (11);
1116            $self->{s_kwd} = '';
1117          !!!emit ({type => END_OF_FILE_TOKEN,          !!!emit ({type => END_OF_FILE_TOKEN,
1118                    line => $self->{line}, column => $self->{column}});                    line => $self->{line}, column => $self->{column}});
1119          last A; ## TODO: ok?          last A; ## TODO: ok?
1120        } else {        } else {
1121          !!!cp (12);          !!!cp (12);
1122            $self->{s_kwd} = '';
1123            #
1124        }        }
1125    
1126        # Anything else        # Anything else
1127        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
1128                     data => chr $self->{next_char},                     data => chr $self->{nc},
1129                     line => $self->{line}, column => $self->{column},                     line => $self->{line}, column => $self->{column},
1130                    };                    };
1131        ## Stay in the data state        if ($self->{read_until}->($token->{data}, q[-!<>&],
1132        !!!next-input-character;                                  length $token->{data})) {
1133            $self->{s_kwd} = '';
1134        !!!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  
1135    
1136        unless (defined $token) {        ## Stay in the data state.
1137          if ($self->{content_model} == PCDATA_CONTENT_MODEL) {
1138          !!!cp (13);          !!!cp (13);
1139          !!!emit ({type => CHARACTER_TOKEN, data => '&',          $self->{state} = PCDATA_STATE;
                   line => $l, column => $c,  
                  });  
1140        } else {        } else {
1141          !!!cp (14);          !!!cp (14);
1142          !!!emit ($token);          ## Stay in the state.
1143        }        }
1144          !!!next-input-character;
1145          !!!emit ($token);
1146        redo A;        redo A;
1147      } elsif ($self->{state} == TAG_OPEN_STATE) {      } elsif ($self->{state} == TAG_OPEN_STATE) {
1148        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1149          if ($self->{next_char} == 0x002F) { # /          if ($self->{nc} == 0x002F) { # /
1150            !!!cp (15);            !!!cp (15);
1151            !!!next-input-character;            !!!next-input-character;
1152            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
1153            redo A;            redo A;
1154            } elsif ($self->{nc} == 0x0021) { # !
1155              !!!cp (15.1);
1156              $self->{s_kwd} = '<' unless $self->{escape};
1157              #
1158          } else {          } else {
1159            !!!cp (16);            !!!cp (16);
1160            ## reconsume            #
           $self->{state} = DATA_STATE;  
   
           !!!emit ({type => CHARACTER_TOKEN, data => '<',  
                     line => $self->{line_prev},  
                     column => $self->{column_prev},  
                    });  
   
           redo A;  
1161          }          }
1162    
1163            ## reconsume
1164            $self->{state} = DATA_STATE;
1165            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1166                      line => $self->{line_prev},
1167                      column => $self->{column_prev},
1168                     });
1169            redo A;
1170        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
1171          if ($self->{next_char} == 0x0021) { # !          if ($self->{nc} == 0x0021) { # !
1172            !!!cp (17);            !!!cp (17);
1173            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
1174            !!!next-input-character;            !!!next-input-character;
1175            redo A;            redo A;
1176          } elsif ($self->{next_char} == 0x002F) { # /          } elsif ($self->{nc} == 0x002F) { # /
1177            !!!cp (18);            !!!cp (18);
1178            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
1179            !!!next-input-character;            !!!next-input-character;
1180            redo A;            redo A;
1181          } elsif (0x0041 <= $self->{next_char} and          } elsif (0x0041 <= $self->{nc} and
1182                   $self->{next_char} <= 0x005A) { # A..Z                   $self->{nc} <= 0x005A) { # A..Z
1183            !!!cp (19);            !!!cp (19);
1184            $self->{current_token}            $self->{ct}
1185              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
1186                 tag_name => chr ($self->{next_char} + 0x0020),                 tag_name => chr ($self->{nc} + 0x0020),
1187                 line => $self->{line_prev},                 line => $self->{line_prev},
1188                 column => $self->{column_prev}};                 column => $self->{column_prev}};
1189            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1190            !!!next-input-character;            !!!next-input-character;
1191            redo A;            redo A;
1192          } elsif (0x0061 <= $self->{next_char} and          } elsif (0x0061 <= $self->{nc} and
1193                   $self->{next_char} <= 0x007A) { # a..z                   $self->{nc} <= 0x007A) { # a..z
1194            !!!cp (20);            !!!cp (20);
1195            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{ct} = {type => START_TAG_TOKEN,
1196                                      tag_name => chr ($self->{next_char}),                                      tag_name => chr ($self->{nc}),
1197                                      line => $self->{line_prev},                                      line => $self->{line_prev},
1198                                      column => $self->{column_prev}};                                      column => $self->{column_prev}};
1199            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1200            !!!next-input-character;            !!!next-input-character;
1201            redo A;            redo A;
1202          } elsif ($self->{next_char} == 0x003E) { # >          } elsif ($self->{nc} == 0x003E) { # >
1203            !!!cp (21);            !!!cp (21);
1204            !!!parse-error (type => 'empty start tag',            !!!parse-error (type => 'empty start tag',
1205                            line => $self->{line_prev},                            line => $self->{line_prev},
# Line 1034  sub _get_next_token ($) { Line 1213  sub _get_next_token ($) {
1213                     });                     });
1214    
1215            redo A;            redo A;
1216          } elsif ($self->{next_char} == 0x003F) { # ?          } elsif ($self->{nc} == 0x003F) { # ?
1217            !!!cp (22);            !!!cp (22);
1218            !!!parse-error (type => 'pio',            !!!parse-error (type => 'pio',
1219                            line => $self->{line_prev},                            line => $self->{line_prev},
1220                            column => $self->{column_prev});                            column => $self->{column_prev});
1221            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1222            $self->{current_token} = {type => COMMENT_TOKEN, data => '',            $self->{ct} = {type => COMMENT_TOKEN, data => '',
1223                                      line => $self->{line_prev},                                      line => $self->{line_prev},
1224                                      column => $self->{column_prev},                                      column => $self->{column_prev},
1225                                     };                                     };
1226            ## $self->{next_char} is intentionally left as is            ## $self->{nc} is intentionally left as is
1227            redo A;            redo A;
1228          } else {          } else {
1229            !!!cp (23);            !!!cp (23);
# Line 1065  sub _get_next_token ($) { Line 1244  sub _get_next_token ($) {
1244          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1245        }        }
1246      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1247          ## NOTE: The "close tag open state" in the spec is implemented as
1248          ## |CLOSE_TAG_OPEN_STATE| and |CDATA_RCDATA_CLOSE_TAG_STATE|.
1249    
1250        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1251        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1252          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_stag_name}) {
1253              $self->{state} = CDATA_RCDATA_CLOSE_TAG_STATE;
1254            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            $self->{s_kwd} = '';
1255            my @next_char;            ## Reconsume.
1256            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...  
           }  
1257          } else {          } else {
1258            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1259              ## NOTE: See <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>.
1260            !!!cp (28);            !!!cp (28);
           # next-input-character is already done  
1261            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1262              ## Reconsume.
1263            !!!emit ({type => CHARACTER_TOKEN, data => '</',            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1264                      line => $l, column => $c,                      line => $l, column => $c,
1265                     });                     });
1266            redo A;            redo A;
1267          }          }
1268        }        }
1269          
1270        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{nc} and
1271            $self->{next_char} <= 0x005A) { # A..Z            $self->{nc} <= 0x005A) { # A..Z
1272          !!!cp (29);          !!!cp (29);
1273          $self->{current_token}          $self->{ct}
1274              = {type => END_TAG_TOKEN,              = {type => END_TAG_TOKEN,
1275                 tag_name => chr ($self->{next_char} + 0x0020),                 tag_name => chr ($self->{nc} + 0x0020),
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 (0x0061 <= $self->{next_char} and        } elsif (0x0061 <= $self->{nc} and
1281                 $self->{next_char} <= 0x007A) { # a..z                 $self->{nc} <= 0x007A) { # a..z
1282          !!!cp (30);          !!!cp (30);
1283          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{ct} = {type => END_TAG_TOKEN,
1284                                    tag_name => chr ($self->{next_char}),                                    tag_name => chr ($self->{nc}),
1285                                    line => $l, column => $c};                                    line => $l, column => $c};
1286          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1287          !!!next-input-character;          !!!next-input-character;
1288          redo A;          redo A;
1289        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1290          !!!cp (31);          !!!cp (31);
1291          !!!parse-error (type => 'empty end tag',          !!!parse-error (type => 'empty end tag',
1292                          line => $self->{line_prev}, ## "<" in "</>"                          line => $self->{line_prev}, ## "<" in "</>"
# Line 1155  sub _get_next_token ($) { Line 1294  sub _get_next_token ($) {
1294          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1295          !!!next-input-character;          !!!next-input-character;
1296          redo A;          redo A;
1297        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1298          !!!cp (32);          !!!cp (32);
1299          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1300          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
# Line 1170  sub _get_next_token ($) { Line 1309  sub _get_next_token ($) {
1309          !!!cp (33);          !!!cp (33);
1310          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1311          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1312          $self->{current_token} = {type => COMMENT_TOKEN, data => '',          $self->{ct} = {type => COMMENT_TOKEN, data => '',
1313                                    line => $self->{line_prev}, # "<" of "</"                                    line => $self->{line_prev}, # "<" of "</"
1314                                    column => $self->{column_prev} - 1,                                    column => $self->{column_prev} - 1,
1315                                   };                                   };
1316          ## $self->{next_char} is intentionally left as is          ## NOTE: $self->{nc} is intentionally left as is.
1317          redo A;          ## Although the "anything else" case of the spec not explicitly
1318            ## states that the next input character is to be reconsumed,
1319            ## it will be included to the |data| of the comment token
1320            ## generated from the bogus end tag, as defined in the
1321            ## "bogus comment state" entry.
1322            redo A;
1323          }
1324        } elsif ($self->{state} == CDATA_RCDATA_CLOSE_TAG_STATE) {
1325          my $ch = substr $self->{last_stag_name}, length $self->{s_kwd}, 1;
1326          if (length $ch) {
1327            my $CH = $ch;
1328            $ch =~ tr/a-z/A-Z/;
1329            my $nch = chr $self->{nc};
1330            if ($nch eq $ch or $nch eq $CH) {
1331              !!!cp (24);
1332              ## Stay in the state.
1333              $self->{s_kwd} .= $nch;
1334              !!!next-input-character;
1335              redo A;
1336            } else {
1337              !!!cp (25);
1338              $self->{state} = DATA_STATE;
1339              ## Reconsume.
1340              !!!emit ({type => CHARACTER_TOKEN,
1341                        data => '</' . $self->{s_kwd},
1342                        line => $self->{line_prev},
1343                        column => $self->{column_prev} - 1 - length $self->{s_kwd},
1344                       });
1345              redo A;
1346            }
1347          } else { # after "<{tag-name}"
1348            unless ($is_space->{$self->{nc}} or
1349                    {
1350                     0x003E => 1, # >
1351                     0x002F => 1, # /
1352                     -1 => 1, # EOF
1353                    }->{$self->{nc}}) {
1354              !!!cp (26);
1355              ## Reconsume.
1356              $self->{state} = DATA_STATE;
1357              !!!emit ({type => CHARACTER_TOKEN,
1358                        data => '</' . $self->{s_kwd},
1359                        line => $self->{line_prev},
1360                        column => $self->{column_prev} - 1 - length $self->{s_kwd},
1361                       });
1362              redo A;
1363            } else {
1364              !!!cp (27);
1365              $self->{ct}
1366                  = {type => END_TAG_TOKEN,
1367                     tag_name => $self->{last_stag_name},
1368                     line => $self->{line_prev},
1369                     column => $self->{column_prev} - 1 - length $self->{s_kwd}};
1370              $self->{state} = TAG_NAME_STATE;
1371              ## Reconsume.
1372              redo A;
1373            }
1374        }        }
1375      } elsif ($self->{state} == TAG_NAME_STATE) {      } elsif ($self->{state} == TAG_NAME_STATE) {
1376        if ($self->{next_char} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
1377          !!!cp (34);          !!!cp (34);
1378          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1379          !!!next-input-character;          !!!next-input-character;
1380          redo A;          redo A;
1381        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1382          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1383            !!!cp (35);            !!!cp (35);
1384            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1385          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1386            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1387            #if ($self->{current_token}->{attributes}) {            #if ($self->{ct}->{attributes}) {
1388            #  ## NOTE: This should never be reached.            #  ## NOTE: This should never be reached.
1389            #  !!! cp (36);            #  !!! cp (36);
1390            #  !!! parse-error (type => 'end tag attribute');            #  !!! parse-error (type => 'end tag attribute');
# Line 1201  sub _get_next_token ($) { Line 1392  sub _get_next_token ($) {
1392              !!!cp (37);              !!!cp (37);
1393            #}            #}
1394          } else {          } else {
1395            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1396          }          }
1397          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1398          !!!next-input-character;          !!!next-input-character;
1399    
1400          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1401    
1402          redo A;          redo A;
1403        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{nc} and
1404                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1405          !!!cp (38);          !!!cp (38);
1406          $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);          $self->{ct}->{tag_name} .= chr ($self->{nc} + 0x0020);
1407            # start tag or end tag            # start tag or end tag
1408          ## Stay in this state          ## Stay in this state
1409          !!!next-input-character;          !!!next-input-character;
1410          redo A;          redo A;
1411        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1412          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1413          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1414            !!!cp (39);            !!!cp (39);
1415            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1416          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1417            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1418            #if ($self->{current_token}->{attributes}) {            #if ($self->{ct}->{attributes}) {
1419            #  ## NOTE: This state should never be reached.            #  ## NOTE: This state should never be reached.
1420            #  !!! cp (40);            #  !!! cp (40);
1421            #  !!! parse-error (type => 'end tag attribute');            #  !!! parse-error (type => 'end tag attribute');
# Line 1232  sub _get_next_token ($) { Line 1423  sub _get_next_token ($) {
1423              !!!cp (41);              !!!cp (41);
1424            #}            #}
1425          } else {          } else {
1426            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1427          }          }
1428          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1429          # reconsume          # reconsume
1430    
1431          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1432    
1433          redo A;          redo A;
1434        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1435          !!!cp (42);          !!!cp (42);
1436          $self->{state} = SELF_CLOSING_START_TAG_STATE;          $self->{state} = SELF_CLOSING_START_TAG_STATE;
1437          !!!next-input-character;          !!!next-input-character;
1438          redo A;          redo A;
1439        } else {        } else {
1440          !!!cp (44);          !!!cp (44);
1441          $self->{current_token}->{tag_name} .= chr $self->{next_char};          $self->{ct}->{tag_name} .= chr $self->{nc};
1442            # start tag or end tag            # start tag or end tag
1443          ## Stay in the state          ## Stay in the state
1444          !!!next-input-character;          !!!next-input-character;
1445          redo A;          redo A;
1446        }        }
1447      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1448        if ($self->{next_char} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
1449          !!!cp (45);          !!!cp (45);
1450          ## Stay in the state          ## Stay in the state
1451          !!!next-input-character;          !!!next-input-character;
1452          redo A;          redo A;
1453        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1454          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1455            !!!cp (46);            !!!cp (46);
1456            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1457          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1458            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1459            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1460              !!!cp (47);              !!!cp (47);
1461              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1462            } else {            } else {
1463              !!!cp (48);              !!!cp (48);
1464            }            }
1465          } else {          } else {
1466            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1467          }          }
1468          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1469          !!!next-input-character;          !!!next-input-character;
1470    
1471          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1472    
1473          redo A;          redo A;
1474        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{nc} and
1475                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1476          !!!cp (49);          !!!cp (49);
1477          $self->{current_attribute}          $self->{ca}
1478              = {name => chr ($self->{next_char} + 0x0020),              = {name => chr ($self->{nc} + 0x0020),
1479                 value => '',                 value => '',
1480                 line => $self->{line}, column => $self->{column}};                 line => $self->{line}, column => $self->{column}};
1481          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1482          !!!next-input-character;          !!!next-input-character;
1483          redo A;          redo A;
1484        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1485          !!!cp (50);          !!!cp (50);
1486          $self->{state} = SELF_CLOSING_START_TAG_STATE;          $self->{state} = SELF_CLOSING_START_TAG_STATE;
1487          !!!next-input-character;          !!!next-input-character;
1488          redo A;          redo A;
1489        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1490          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1491          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1492            !!!cp (52);            !!!cp (52);
1493            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1494          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1495            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1496            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1497              !!!cp (53);              !!!cp (53);
1498              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1499            } else {            } else {
1500              !!!cp (54);              !!!cp (54);
1501            }            }
1502          } else {          } else {
1503            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1504          }          }
1505          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1506          # reconsume          # reconsume
1507    
1508          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1509    
1510          redo A;          redo A;
1511        } else {        } else {
# Line 1326  sub _get_next_token ($) { Line 1513  sub _get_next_token ($) {
1513               0x0022 => 1, # "               0x0022 => 1, # "
1514               0x0027 => 1, # '               0x0027 => 1, # '
1515               0x003D => 1, # =               0x003D => 1, # =
1516              }->{$self->{next_char}}) {              }->{$self->{nc}}) {
1517            !!!cp (55);            !!!cp (55);
1518            !!!parse-error (type => 'bad attribute name');            !!!parse-error (type => 'bad attribute name');
1519          } else {          } else {
1520            !!!cp (56);            !!!cp (56);
1521          }          }
1522          $self->{current_attribute}          $self->{ca}
1523              = {name => chr ($self->{next_char}),              = {name => chr ($self->{nc}),
1524                 value => '',                 value => '',
1525                 line => $self->{line}, column => $self->{column}};                 line => $self->{line}, column => $self->{column}};
1526          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
# Line 1342  sub _get_next_token ($) { Line 1529  sub _get_next_token ($) {
1529        }        }
1530      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
1531        my $before_leave = sub {        my $before_leave = sub {
1532          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{ct}->{attributes} # start tag or end tag
1533              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{ca}->{name}}) { # MUST
1534            !!!cp (57);            !!!cp (57);
1535            !!!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});
1536            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{ca} # MUST
1537          } else {          } else {
1538            !!!cp (58);            !!!cp (58);
1539            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{ct}->{attributes}->{$self->{ca}->{name}}
1540              = $self->{current_attribute};              = $self->{ca};
1541          }          }
1542        }; # $before_leave        }; # $before_leave
1543    
1544        if ($self->{next_char} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
1545          !!!cp (59);          !!!cp (59);
1546          $before_leave->();          $before_leave->();
1547          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1548          !!!next-input-character;          !!!next-input-character;
1549          redo A;          redo A;
1550        } elsif ($self->{next_char} == 0x003D) { # =        } elsif ($self->{nc} == 0x003D) { # =
1551          !!!cp (60);          !!!cp (60);
1552          $before_leave->();          $before_leave->();
1553          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1554          !!!next-input-character;          !!!next-input-character;
1555          redo A;          redo A;
1556        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1557          $before_leave->();          $before_leave->();
1558          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1559            !!!cp (61);            !!!cp (61);
1560            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1561          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1562            !!!cp (62);            !!!cp (62);
1563            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1564            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1565              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1566            }            }
1567          } else {          } else {
1568            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1569          }          }
1570          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1571          !!!next-input-character;          !!!next-input-character;
1572    
1573          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1574    
1575          redo A;          redo A;
1576        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{nc} and
1577                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1578          !!!cp (63);          !!!cp (63);
1579          $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);          $self->{ca}->{name} .= chr ($self->{nc} + 0x0020);
1580          ## Stay in the state          ## Stay in the state
1581          !!!next-input-character;          !!!next-input-character;
1582          redo A;          redo A;
1583        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1584          !!!cp (64);          !!!cp (64);
1585          $before_leave->();          $before_leave->();
1586          $self->{state} = SELF_CLOSING_START_TAG_STATE;          $self->{state} = SELF_CLOSING_START_TAG_STATE;
1587          !!!next-input-character;          !!!next-input-character;
1588          redo A;          redo A;
1589        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1590          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1591          $before_leave->();          $before_leave->();
1592          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1593            !!!cp (66);            !!!cp (66);
1594            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1595          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1596            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1597            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1598              !!!cp (67);              !!!cp (67);
1599              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1600            } else {            } else {
# Line 1419  sub _get_next_token ($) { Line 1602  sub _get_next_token ($) {
1602              !!!cp (68);              !!!cp (68);
1603            }            }
1604          } else {          } else {
1605            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1606          }          }
1607          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1608          # reconsume          # reconsume
1609    
1610          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1611    
1612          redo A;          redo A;
1613        } else {        } else {
1614          if ($self->{next_char} == 0x0022 or # "          if ($self->{nc} == 0x0022 or # "
1615              $self->{next_char} == 0x0027) { # '              $self->{nc} == 0x0027) { # '
1616            !!!cp (69);            !!!cp (69);
1617            !!!parse-error (type => 'bad attribute name');            !!!parse-error (type => 'bad attribute name');
1618          } else {          } else {
1619            !!!cp (70);            !!!cp (70);
1620          }          }
1621          $self->{current_attribute}->{name} .= chr ($self->{next_char});          $self->{ca}->{name} .= chr ($self->{nc});
1622          ## Stay in the state          ## Stay in the state
1623          !!!next-input-character;          !!!next-input-character;
1624          redo A;          redo A;
1625        }        }
1626      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1627        if ($self->{next_char} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
1628          !!!cp (71);          !!!cp (71);
1629          ## Stay in the state          ## Stay in the state
1630          !!!next-input-character;          !!!next-input-character;
1631          redo A;          redo A;
1632        } elsif ($self->{next_char} == 0x003D) { # =        } elsif ($self->{nc} == 0x003D) { # =
1633          !!!cp (72);          !!!cp (72);
1634          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1635          !!!next-input-character;          !!!next-input-character;
1636          redo A;          redo A;
1637        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1638          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1639            !!!cp (73);            !!!cp (73);
1640            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1641          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1642            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1643            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1644              !!!cp (74);              !!!cp (74);
1645              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1646            } else {            } else {
# Line 1469  sub _get_next_token ($) { Line 1648  sub _get_next_token ($) {
1648              !!!cp (75);              !!!cp (75);
1649            }            }
1650          } else {          } else {
1651            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1652          }          }
1653          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1654          !!!next-input-character;          !!!next-input-character;
1655    
1656          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1657    
1658          redo A;          redo A;
1659        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{nc} and
1660                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1661          !!!cp (76);          !!!cp (76);
1662          $self->{current_attribute}          $self->{ca}
1663              = {name => chr ($self->{next_char} + 0x0020),              = {name => chr ($self->{nc} + 0x0020),
1664                 value => '',                 value => '',
1665                 line => $self->{line}, column => $self->{column}};                 line => $self->{line}, column => $self->{column}};
1666          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1667          !!!next-input-character;          !!!next-input-character;
1668          redo A;          redo A;
1669        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1670          !!!cp (77);          !!!cp (77);
1671          $self->{state} = SELF_CLOSING_START_TAG_STATE;          $self->{state} = SELF_CLOSING_START_TAG_STATE;
1672          !!!next-input-character;          !!!next-input-character;
1673          redo A;          redo A;
1674        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1675          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1676          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1677            !!!cp (79);            !!!cp (79);
1678            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1679          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1680            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1681            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1682              !!!cp (80);              !!!cp (80);
1683              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1684            } else {            } else {
# Line 1507  sub _get_next_token ($) { Line 1686  sub _get_next_token ($) {
1686              !!!cp (81);              !!!cp (81);
1687            }            }
1688          } else {          } else {
1689            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1690          }          }
1691          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1692          # reconsume          # reconsume
1693    
1694          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1695    
1696          redo A;          redo A;
1697        } else {        } else {
1698          !!!cp (82);          if ($self->{nc} == 0x0022 or # "
1699          $self->{current_attribute}              $self->{nc} == 0x0027) { # '
1700              = {name => chr ($self->{next_char}),            !!!cp (78);
1701              !!!parse-error (type => 'bad attribute name');
1702            } else {
1703              !!!cp (82);
1704            }
1705            $self->{ca}
1706                = {name => chr ($self->{nc}),
1707                 value => '',                 value => '',
1708                 line => $self->{line}, column => $self->{column}};                 line => $self->{line}, column => $self->{column}};
1709          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
# Line 1526  sub _get_next_token ($) { Line 1711  sub _get_next_token ($) {
1711          redo A;                  redo A;        
1712        }        }
1713      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1714        if ($self->{next_char} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP        
1715          !!!cp (83);          !!!cp (83);
1716          ## Stay in the state          ## Stay in the state
1717          !!!next-input-character;          !!!next-input-character;
1718          redo A;          redo A;
1719        } elsif ($self->{next_char} == 0x0022) { # "        } elsif ($self->{nc} == 0x0022) { # "
1720          !!!cp (84);          !!!cp (84);
1721          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1722          !!!next-input-character;          !!!next-input-character;
1723          redo A;          redo A;
1724        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{nc} == 0x0026) { # &
1725          !!!cp (85);          !!!cp (85);
1726          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1727          ## reconsume          ## reconsume
1728          redo A;          redo A;
1729        } elsif ($self->{next_char} == 0x0027) { # '        } elsif ($self->{nc} == 0x0027) { # '
1730          !!!cp (86);          !!!cp (86);
1731          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1732          !!!next-input-character;          !!!next-input-character;
1733          redo A;          redo A;
1734        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1735          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          !!!parse-error (type => 'empty unquoted attribute value');
1736            if ($self->{ct}->{type} == START_TAG_TOKEN) {
1737            !!!cp (87);            !!!cp (87);
1738            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1739          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1740            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1741            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1742              !!!cp (88);              !!!cp (88);
1743              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1744            } else {            } else {
# Line 1564  sub _get_next_token ($) { Line 1746  sub _get_next_token ($) {
1746              !!!cp (89);              !!!cp (89);
1747            }            }
1748          } else {          } else {
1749            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1750          }          }
1751          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1752          !!!next-input-character;          !!!next-input-character;
1753    
1754          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1755    
1756          redo A;          redo A;
1757        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1758          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1759          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1760            !!!cp (90);            !!!cp (90);
1761            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1762          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1763            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1764            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1765              !!!cp (91);              !!!cp (91);
1766              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1767            } else {            } else {
# Line 1587  sub _get_next_token ($) { Line 1769  sub _get_next_token ($) {
1769              !!!cp (92);              !!!cp (92);
1770            }            }
1771          } else {          } else {
1772            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1773          }          }
1774          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1775          ## reconsume          ## reconsume
1776    
1777          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1778    
1779          redo A;          redo A;
1780        } else {        } else {
1781          if ($self->{next_char} == 0x003D) { # =          if ($self->{nc} == 0x003D) { # =
1782            !!!cp (93);            !!!cp (93);
1783            !!!parse-error (type => 'bad attribute value');            !!!parse-error (type => 'bad attribute value');
1784          } else {          } else {
1785            !!!cp (94);            !!!cp (94);
1786          }          }
1787          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{ca}->{value} .= chr ($self->{nc});
1788          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1789          !!!next-input-character;          !!!next-input-character;
1790          redo A;          redo A;
1791        }        }
1792      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1793        if ($self->{next_char} == 0x0022) { # "        if ($self->{nc} == 0x0022) { # "
1794          !!!cp (95);          !!!cp (95);
1795          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1796          !!!next-input-character;          !!!next-input-character;
1797          redo A;          redo A;
1798        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{nc} == 0x0026) { # &
1799          !!!cp (96);          !!!cp (96);
1800          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1801          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1802            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1803            ## implementation of the "consume a character reference" algorithm.
1804            $self->{prev_state} = $self->{state};
1805            $self->{entity_add} = 0x0022; # "
1806            $self->{state} = ENTITY_STATE;
1807          !!!next-input-character;          !!!next-input-character;
1808          redo A;          redo A;
1809        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1810          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1811          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1812            !!!cp (97);            !!!cp (97);
1813            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1814          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1815            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1816            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1817              !!!cp (98);              !!!cp (98);
1818              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1819            } else {            } else {
# Line 1634  sub _get_next_token ($) { Line 1821  sub _get_next_token ($) {
1821              !!!cp (99);              !!!cp (99);
1822            }            }
1823          } else {          } else {
1824            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1825          }          }
1826          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1827          ## reconsume          ## reconsume
1828    
1829          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1830    
1831          redo A;          redo A;
1832        } else {        } else {
1833          !!!cp (100);          !!!cp (100);
1834          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{ca}->{value} .= chr ($self->{nc});
1835            $self->{read_until}->($self->{ca}->{value},
1836                                  q["&],
1837                                  length $self->{ca}->{value});
1838    
1839          ## Stay in the state          ## Stay in the state
1840          !!!next-input-character;          !!!next-input-character;
1841          redo A;          redo A;
1842        }        }
1843      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1844        if ($self->{next_char} == 0x0027) { # '        if ($self->{nc} == 0x0027) { # '
1845          !!!cp (101);          !!!cp (101);
1846          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1847          !!!next-input-character;          !!!next-input-character;
1848          redo A;          redo A;
1849        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{nc} == 0x0026) { # &
1850          !!!cp (102);          !!!cp (102);
1851          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1852          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1853            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1854            ## implementation of the "consume a character reference" algorithm.
1855            $self->{entity_add} = 0x0027; # '
1856            $self->{prev_state} = $self->{state};
1857            $self->{state} = ENTITY_STATE;
1858          !!!next-input-character;          !!!next-input-character;
1859          redo A;          redo A;
1860        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1861          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1862          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1863            !!!cp (103);            !!!cp (103);
1864            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1865          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1866            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1867            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1868              !!!cp (104);              !!!cp (104);
1869              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1870            } else {            } else {
# Line 1676  sub _get_next_token ($) { Line 1872  sub _get_next_token ($) {
1872              !!!cp (105);              !!!cp (105);
1873            }            }
1874          } else {          } else {
1875            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1876          }          }
1877          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1878          ## reconsume          ## reconsume
1879    
1880          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1881    
1882          redo A;          redo A;
1883        } else {        } else {
1884          !!!cp (106);          !!!cp (106);
1885          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{ca}->{value} .= chr ($self->{nc});
1886            $self->{read_until}->($self->{ca}->{value},
1887                                  q['&],
1888                                  length $self->{ca}->{value});
1889    
1890          ## Stay in the state          ## Stay in the state
1891          !!!next-input-character;          !!!next-input-character;
1892          redo A;          redo A;
1893        }        }
1894      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1895        if ($self->{next_char} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # HT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
1896          !!!cp (107);          !!!cp (107);
1897          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1898          !!!next-input-character;          !!!next-input-character;
1899          redo A;          redo A;
1900        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{nc} == 0x0026) { # &
1901          !!!cp (108);          !!!cp (108);
1902          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1903          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1904            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1905            ## implementation of the "consume a character reference" algorithm.
1906            $self->{entity_add} = -1;
1907            $self->{prev_state} = $self->{state};
1908            $self->{state} = ENTITY_STATE;
1909          !!!next-input-character;          !!!next-input-character;
1910          redo A;          redo A;
1911        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1912          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1913            !!!cp (109);            !!!cp (109);
1914            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1915          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1916            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1917            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1918              !!!cp (110);              !!!cp (110);
1919              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1920            } else {            } else {
# Line 1721  sub _get_next_token ($) { Line 1922  sub _get_next_token ($) {
1922              !!!cp (111);              !!!cp (111);
1923            }            }
1924          } else {          } else {
1925            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1926          }          }
1927          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1928          !!!next-input-character;          !!!next-input-character;
1929    
1930          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1931    
1932          redo A;          redo A;
1933        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1934          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1935          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1936            !!!cp (112);            !!!cp (112);
1937            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1938          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1939            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1940            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1941              !!!cp (113);              !!!cp (113);
1942              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1943            } else {            } else {
# Line 1744  sub _get_next_token ($) { Line 1945  sub _get_next_token ($) {
1945              !!!cp (114);              !!!cp (114);
1946            }            }
1947          } else {          } else {
1948            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1949          }          }
1950          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1951          ## reconsume          ## reconsume
1952    
1953          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1954    
1955          redo A;          redo A;
1956        } else {        } else {
# Line 1757  sub _get_next_token ($) { Line 1958  sub _get_next_token ($) {
1958               0x0022 => 1, # "               0x0022 => 1, # "
1959               0x0027 => 1, # '               0x0027 => 1, # '
1960               0x003D => 1, # =               0x003D => 1, # =
1961              }->{$self->{next_char}}) {              }->{$self->{nc}}) {
1962            !!!cp (115);            !!!cp (115);
1963            !!!parse-error (type => 'bad attribute value');            !!!parse-error (type => 'bad attribute value');
1964          } else {          } else {
1965            !!!cp (116);            !!!cp (116);
1966          }          }
1967          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{ca}->{value} .= chr ($self->{nc});
1968            $self->{read_until}->($self->{ca}->{value},
1969                                  q["'=& >],
1970                                  length $self->{ca}->{value});
1971    
1972          ## Stay in the state          ## Stay in the state
1973          !!!next-input-character;          !!!next-input-character;
1974          redo A;          redo A;
1975        }        }
     } 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;  
1976      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1977        if ($self->{next_char} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
1978          !!!cp (118);          !!!cp (118);
1979          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1980          !!!next-input-character;          !!!next-input-character;
1981          redo A;          redo A;
1982        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1983          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1984            !!!cp (119);            !!!cp (119);
1985            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1986          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1987            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1988            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1989              !!!cp (120);              !!!cp (120);
1990              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1991            } else {            } else {
# Line 1814  sub _get_next_token ($) { Line 1993  sub _get_next_token ($) {
1993              !!!cp (121);              !!!cp (121);
1994            }            }
1995          } else {          } else {
1996            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1997          }          }
1998          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1999          !!!next-input-character;          !!!next-input-character;
2000    
2001          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
2002    
2003          redo A;          redo A;
2004        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
2005          !!!cp (122);          !!!cp (122);
2006          $self->{state} = SELF_CLOSING_START_TAG_STATE;          $self->{state} = SELF_CLOSING_START_TAG_STATE;
2007          !!!next-input-character;          !!!next-input-character;
2008          redo A;          redo A;
2009        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2010          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
2011          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
2012            !!!cp (122.3);            !!!cp (122.3);
2013            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
2014          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
2015            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
2016              !!!cp (122.1);              !!!cp (122.1);
2017              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
2018            } else {            } else {
# Line 1841  sub _get_next_token ($) { Line 2020  sub _get_next_token ($) {
2020              !!!cp (122.2);              !!!cp (122.2);
2021            }            }
2022          } else {          } else {
2023            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
2024          }          }
2025          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2026          ## Reconsume.          ## Reconsume.
2027          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
2028          redo A;          redo A;
2029        } else {        } else {
2030          !!!cp ('124.1');          !!!cp ('124.1');
# Line 1855  sub _get_next_token ($) { Line 2034  sub _get_next_token ($) {
2034          redo A;          redo A;
2035        }        }
2036      } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {      } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
2037        if ($self->{next_char} == 0x003E) { # >        if ($self->{nc} == 0x003E) { # >
2038          if ($self->{current_token}->{type} == END_TAG_TOKEN) {          if ($self->{ct}->{type} == END_TAG_TOKEN) {
2039            !!!cp ('124.2');            !!!cp ('124.2');
2040            !!!parse-error (type => 'nestc', token => $self->{current_token});            !!!parse-error (type => 'nestc', token => $self->{ct});
2041            ## TODO: Different type than slash in start tag            ## TODO: Different type than slash in start tag
2042            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
2043            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
2044              !!!cp ('124.4');              !!!cp ('124.4');
2045              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
2046            } else {            } else {
# Line 1876  sub _get_next_token ($) { Line 2055  sub _get_next_token ($) {
2055          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2056          !!!next-input-character;          !!!next-input-character;
2057    
2058          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
2059    
2060          redo A;          redo A;
2061        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2062          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
2063          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
2064            !!!cp (124.7);            !!!cp (124.7);
2065            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
2066          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
2067            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
2068              !!!cp (124.5);              !!!cp (124.5);
2069              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
2070            } else {            } else {
# Line 1893  sub _get_next_token ($) { Line 2072  sub _get_next_token ($) {
2072              !!!cp (124.6);              !!!cp (124.6);
2073            }            }
2074          } else {          } else {
2075            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
2076          }          }
2077          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2078          ## Reconsume.          ## Reconsume.
2079          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
2080          redo A;          redo A;
2081        } else {        } else {
2082          !!!cp ('124.4');          !!!cp ('124.4');
# Line 1909  sub _get_next_token ($) { Line 2088  sub _get_next_token ($) {
2088        }        }
2089      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
2090        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
         
       ## NOTE: Set by the previous state  
       #my $token = {type => COMMENT_TOKEN, data => ''};  
   
       BC: {  
         if ($self->{next_char} == 0x003E) { # >  
           !!!cp (124);  
           $self->{state} = DATA_STATE;  
           !!!next-input-character;  
   
           !!!emit ($self->{current_token}); # comment  
2091    
2092            redo A;        ## NOTE: Unlike spec's "bogus comment state", this implementation
2093          } elsif ($self->{next_char} == -1) {        ## consumes characters one-by-one basis.
2094            !!!cp (125);        
2095            $self->{state} = DATA_STATE;        if ($self->{nc} == 0x003E) { # >
2096            ## reconsume          !!!cp (124);
2097            $self->{state} = DATA_STATE;
2098            !!!next-input-character;
2099    
2100            !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2101            redo A;
2102          } elsif ($self->{nc} == -1) {
2103            !!!cp (125);
2104            $self->{state} = DATA_STATE;
2105            ## reconsume
2106    
2107            redo A;          !!!emit ($self->{ct}); # comment
2108          } else {          redo A;
2109            !!!cp (126);        } else {
2110            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment          !!!cp (126);
2111            !!!next-input-character;          $self->{ct}->{data} .= chr ($self->{nc}); # comment
2112            redo BC;          $self->{read_until}->($self->{ct}->{data},
2113          }                                q[>],
2114        } # BC                                length $self->{ct}->{data});
2115    
2116        die "$0: _get_next_token: unexpected case [BC]";          ## Stay in the state.
2117            !!!next-input-character;
2118            redo A;
2119          }
2120      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2121        ## (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};  
2122                
2123        if ($self->{next_char} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2124            !!!cp (133);
2125            $self->{state} = MD_HYPHEN_STATE;
2126          !!!next-input-character;          !!!next-input-character;
2127          push @next_char, $self->{next_char};          redo A;
2128          if ($self->{next_char} == 0x002D) { # -        } elsif ($self->{nc} == 0x0044 or # D
2129            !!!cp (127);                 $self->{nc} == 0x0064) { # d
2130            $self->{current_token} = {type => COMMENT_TOKEN, data => '',          ## ASCII case-insensitive.
2131                                      line => $l, column => $c,          !!!cp (130);
2132                                     };          $self->{state} = MD_DOCTYPE_STATE;
2133            $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  
2134          !!!next-input-character;          !!!next-input-character;
2135          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);  
         }  
2136        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2137                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2138                 $self->{next_char} == 0x005B) { # [                 $self->{nc} == 0x005B) { # [
2139            !!!cp (135.4);                
2140            $self->{state} = MD_CDATA_STATE;
2141            $self->{s_kwd} = '[';
2142          !!!next-input-character;          !!!next-input-character;
2143          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);  
         }  
2144        } else {        } else {
2145          !!!cp (136);          !!!cp (136);
2146        }        }
2147    
2148        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment',
2149        $self->{next_char} = shift @next_char;                        line => $self->{line_prev},
2150        !!!back-next-input-character (@next_char);                        column => $self->{column_prev} - 1);
2151          ## Reconsume.
2152        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2153        $self->{current_token} = {type => COMMENT_TOKEN, data => '',        $self->{ct} = {type => COMMENT_TOKEN, data => '',
2154                                  line => $l, column => $c,                                  line => $self->{line_prev},
2155                                    column => $self->{column_prev} - 1,
2156                                 };                                 };
2157        redo A;        redo A;
2158              } elsif ($self->{state} == MD_HYPHEN_STATE) {
2159        ## ISSUE: typos in spec: chacacters, is is a parse error        if ($self->{nc} == 0x002D) { # -
2160        ## 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);
2161            $self->{ct} = {type => COMMENT_TOKEN, data => '',
2162                                      line => $self->{line_prev},
2163                                      column => $self->{column_prev} - 2,
2164                                     };
2165            $self->{state} = COMMENT_START_STATE;
2166            !!!next-input-character;
2167            redo A;
2168          } else {
2169            !!!cp (128);
2170            !!!parse-error (type => 'bogus comment',
2171                            line => $self->{line_prev},
2172                            column => $self->{column_prev} - 2);
2173            $self->{state} = BOGUS_COMMENT_STATE;
2174            ## Reconsume.
2175            $self->{ct} = {type => COMMENT_TOKEN,
2176                                      data => '-',
2177                                      line => $self->{line_prev},
2178                                      column => $self->{column_prev} - 2,
2179                                     };
2180            redo A;
2181          }
2182        } elsif ($self->{state} == MD_DOCTYPE_STATE) {
2183          ## ASCII case-insensitive.
2184          if ($self->{nc} == [
2185                undef,
2186                0x004F, # O
2187                0x0043, # C
2188                0x0054, # T
2189                0x0059, # Y
2190                0x0050, # P
2191              ]->[length $self->{s_kwd}] or
2192              $self->{nc} == [
2193                undef,
2194                0x006F, # o
2195                0x0063, # c
2196                0x0074, # t
2197                0x0079, # y
2198                0x0070, # p
2199              ]->[length $self->{s_kwd}]) {
2200            !!!cp (131);
2201            ## Stay in the state.
2202            $self->{s_kwd} .= chr $self->{nc};
2203            !!!next-input-character;
2204            redo A;
2205          } elsif ((length $self->{s_kwd}) == 6 and
2206                   ($self->{nc} == 0x0045 or # E
2207                    $self->{nc} == 0x0065)) { # e
2208            !!!cp (129);
2209            $self->{state} = DOCTYPE_STATE;
2210            $self->{ct} = {type => DOCTYPE_TOKEN,
2211                                      quirks => 1,
2212                                      line => $self->{line_prev},
2213                                      column => $self->{column_prev} - 7,
2214                                     };
2215            !!!next-input-character;
2216            redo A;
2217          } else {
2218            !!!cp (132);        
2219            !!!parse-error (type => 'bogus comment',
2220                            line => $self->{line_prev},
2221                            column => $self->{column_prev} - 1 - length $self->{s_kwd});
2222            $self->{state} = BOGUS_COMMENT_STATE;
2223            ## Reconsume.
2224            $self->{ct} = {type => COMMENT_TOKEN,
2225                                      data => $self->{s_kwd},
2226                                      line => $self->{line_prev},
2227                                      column => $self->{column_prev} - 1 - length $self->{s_kwd},
2228                                     };
2229            redo A;
2230          }
2231        } elsif ($self->{state} == MD_CDATA_STATE) {
2232          if ($self->{nc} == {
2233                '[' => 0x0043, # C
2234                '[C' => 0x0044, # D
2235                '[CD' => 0x0041, # A
2236                '[CDA' => 0x0054, # T
2237                '[CDAT' => 0x0041, # A
2238              }->{$self->{s_kwd}}) {
2239            !!!cp (135.1);
2240            ## Stay in the state.
2241            $self->{s_kwd} .= chr $self->{nc};
2242            !!!next-input-character;
2243            redo A;
2244          } elsif ($self->{s_kwd} eq '[CDATA' and
2245                   $self->{nc} == 0x005B) { # [
2246            !!!cp (135.2);
2247            $self->{ct} = {type => CHARACTER_TOKEN,
2248                                      data => '',
2249                                      line => $self->{line_prev},
2250                                      column => $self->{column_prev} - 7};
2251            $self->{state} = CDATA_SECTION_STATE;
2252            !!!next-input-character;
2253            redo A;
2254          } else {
2255            !!!cp (135.3);
2256            !!!parse-error (type => 'bogus comment',
2257                            line => $self->{line_prev},
2258                            column => $self->{column_prev} - 1 - length $self->{s_kwd});
2259            $self->{state} = BOGUS_COMMENT_STATE;
2260            ## Reconsume.
2261            $self->{ct} = {type => COMMENT_TOKEN,
2262                                      data => $self->{s_kwd},
2263                                      line => $self->{line_prev},
2264                                      column => $self->{column_prev} - 1 - length $self->{s_kwd},
2265                                     };
2266            redo A;
2267          }
2268      } elsif ($self->{state} == COMMENT_START_STATE) {      } elsif ($self->{state} == COMMENT_START_STATE) {
2269        if ($self->{next_char} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2270          !!!cp (137);          !!!cp (137);
2271          $self->{state} = COMMENT_START_DASH_STATE;          $self->{state} = COMMENT_START_DASH_STATE;
2272          !!!next-input-character;          !!!next-input-character;
2273          redo A;          redo A;
2274        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2275          !!!cp (138);          !!!cp (138);
2276          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2277          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2278          !!!next-input-character;          !!!next-input-character;
2279    
2280          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2281    
2282          redo A;          redo A;
2283        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2284          !!!cp (139);          !!!cp (139);
2285          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2286          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2287          ## reconsume          ## reconsume
2288    
2289          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2290    
2291          redo A;          redo A;
2292        } else {        } else {
2293          !!!cp (140);          !!!cp (140);
2294          $self->{current_token}->{data} # comment          $self->{ct}->{data} # comment
2295              .= chr ($self->{next_char});              .= chr ($self->{nc});
2296          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2297          !!!next-input-character;          !!!next-input-character;
2298          redo A;          redo A;
2299        }        }
2300      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2301        if ($self->{next_char} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2302          !!!cp (141);          !!!cp (141);
2303          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
2304          !!!next-input-character;          !!!next-input-character;
2305          redo A;          redo A;
2306        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2307          !!!cp (142);          !!!cp (142);
2308          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2309          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2310          !!!next-input-character;          !!!next-input-character;
2311    
2312          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2313    
2314          redo A;          redo A;
2315        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2316          !!!cp (143);          !!!cp (143);
2317          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2318          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2319          ## reconsume          ## reconsume
2320    
2321          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2322    
2323          redo A;          redo A;
2324        } else {        } else {
2325          !!!cp (144);          !!!cp (144);
2326          $self->{current_token}->{data} # comment          $self->{ct}->{data} # comment
2327              .= '-' . chr ($self->{next_char});              .= '-' . chr ($self->{nc});
2328          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2329          !!!next-input-character;          !!!next-input-character;
2330          redo A;          redo A;
2331        }        }
2332      } elsif ($self->{state} == COMMENT_STATE) {      } elsif ($self->{state} == COMMENT_STATE) {
2333        if ($self->{next_char} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2334          !!!cp (145);          !!!cp (145);
2335          $self->{state} = COMMENT_END_DASH_STATE;          $self->{state} = COMMENT_END_DASH_STATE;
2336          !!!next-input-character;          !!!next-input-character;
2337          redo A;          redo A;
2338        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2339          !!!cp (146);          !!!cp (146);
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 (147);          !!!cp (147);
2349          $self->{current_token}->{data} .= chr ($self->{next_char}); # comment          $self->{ct}->{data} .= chr ($self->{nc}); # comment
2350            $self->{read_until}->($self->{ct}->{data},
2351                                  q[-],
2352                                  length $self->{ct}->{data});
2353    
2354          ## Stay in the state          ## Stay in the state
2355          !!!next-input-character;          !!!next-input-character;
2356          redo A;          redo A;
2357        }        }
2358      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2359        if ($self->{next_char} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2360          !!!cp (148);          !!!cp (148);
2361          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
2362          !!!next-input-character;          !!!next-input-character;
2363          redo A;          redo A;
2364        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2365          !!!cp (149);          !!!cp (149);
2366          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2367          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2368          ## reconsume          ## reconsume
2369    
2370          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2371    
2372          redo A;          redo A;
2373        } else {        } else {
2374          !!!cp (150);          !!!cp (150);
2375          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment          $self->{ct}->{data} .= '-' . chr ($self->{nc}); # comment
2376          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2377          !!!next-input-character;          !!!next-input-character;
2378          redo A;          redo A;
2379        }        }
2380      } elsif ($self->{state} == COMMENT_END_STATE) {      } elsif ($self->{state} == COMMENT_END_STATE) {
2381        if ($self->{next_char} == 0x003E) { # >        if ($self->{nc} == 0x003E) { # >
2382          !!!cp (151);          !!!cp (151);
2383          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2384          !!!next-input-character;          !!!next-input-character;
2385    
2386          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2387    
2388          redo A;          redo A;
2389        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{nc} == 0x002D) { # -
2390          !!!cp (152);          !!!cp (152);
2391          !!!parse-error (type => 'dash in comment',          !!!parse-error (type => 'dash in comment',
2392                          line => $self->{line_prev},                          line => $self->{line_prev},
2393                          column => $self->{column_prev});                          column => $self->{column_prev});
2394          $self->{current_token}->{data} .= '-'; # comment          $self->{ct}->{data} .= '-'; # comment
2395          ## Stay in the state          ## Stay in the state
2396          !!!next-input-character;          !!!next-input-character;
2397          redo A;          redo A;
2398        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2399          !!!cp (153);          !!!cp (153);
2400          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2401          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2402          ## reconsume          ## reconsume
2403    
2404          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2405    
2406          redo A;          redo A;
2407        } else {        } else {
# Line 2212  sub _get_next_token ($) { Line 2409  sub _get_next_token ($) {
2409          !!!parse-error (type => 'dash in comment',          !!!parse-error (type => 'dash in comment',
2410                          line => $self->{line_prev},                          line => $self->{line_prev},
2411                          column => $self->{column_prev});                          column => $self->{column_prev});
2412          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment          $self->{ct}->{data} .= '--' . chr ($self->{nc}); # comment
2413          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2414          !!!next-input-character;          !!!next-input-character;
2415          redo A;          redo A;
2416        }        }
2417      } elsif ($self->{state} == DOCTYPE_STATE) {      } elsif ($self->{state} == DOCTYPE_STATE) {
2418        if ($self->{next_char} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
2419          !!!cp (155);          !!!cp (155);
2420          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2421          !!!next-input-character;          !!!next-input-character;
# Line 2235  sub _get_next_token ($) { Line 2428  sub _get_next_token ($) {
2428          redo A;          redo A;
2429        }        }
2430      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2431        if ($self->{next_char} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
2432          !!!cp (157);          !!!cp (157);
2433          ## Stay in the state          ## Stay in the state
2434          !!!next-input-character;          !!!next-input-character;
2435          redo A;          redo A;
2436        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2437          !!!cp (158);          !!!cp (158);
2438          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2439          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2440          !!!next-input-character;          !!!next-input-character;
2441    
2442          !!!emit ($self->{current_token}); # DOCTYPE (quirks)          !!!emit ($self->{ct}); # DOCTYPE (quirks)
2443    
2444          redo A;          redo A;
2445        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2446          !!!cp (159);          !!!cp (159);
2447          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2448          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2449          ## reconsume          ## reconsume
2450    
2451          !!!emit ($self->{current_token}); # DOCTYPE (quirks)          !!!emit ($self->{ct}); # DOCTYPE (quirks)
2452    
2453          redo A;          redo A;
2454        } else {        } else {
2455          !!!cp (160);          !!!cp (160);
2456          $self->{current_token}->{name} = chr $self->{next_char};          $self->{ct}->{name} = chr $self->{nc};
2457          delete $self->{current_token}->{quirks};          delete $self->{ct}->{quirks};
2458  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2459          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2460          !!!next-input-character;          !!!next-input-character;
# Line 2273  sub _get_next_token ($) { Line 2462  sub _get_next_token ($) {
2462        }        }
2463      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2464  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
2465        if ($self->{next_char} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
2466          !!!cp (161);          !!!cp (161);
2467          $self->{state} = AFTER_DOCTYPE_NAME_STATE;          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2468          !!!next-input-character;          !!!next-input-character;
2469          redo A;          redo A;
2470        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2471          !!!cp (162);          !!!cp (162);
2472          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2473          !!!next-input-character;          !!!next-input-character;
2474    
2475          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2476    
2477          redo A;          redo A;
2478        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2479          !!!cp (163);          !!!cp (163);
2480          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2481          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2482          ## reconsume          ## reconsume
2483    
2484          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2485          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2486    
2487          redo A;          redo A;
2488        } else {        } else {
2489          !!!cp (164);          !!!cp (164);
2490          $self->{current_token}->{name}          $self->{ct}->{name}
2491            .= chr ($self->{next_char}); # DOCTYPE            .= chr ($self->{nc}); # DOCTYPE
2492          ## Stay in the state          ## Stay in the state
2493          !!!next-input-character;          !!!next-input-character;
2494          redo A;          redo A;
2495        }        }
2496      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2497        if ($self->{next_char} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
2498          !!!cp (165);          !!!cp (165);
2499          ## Stay in the state          ## Stay in the state
2500          !!!next-input-character;          !!!next-input-character;
2501          redo A;          redo A;
2502        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2503          !!!cp (166);          !!!cp (166);
2504          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2505          !!!next-input-character;          !!!next-input-character;
2506    
2507          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2508    
2509          redo A;          redo A;
2510        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2511          !!!cp (167);          !!!cp (167);
2512          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2513          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2514          ## reconsume          ## reconsume
2515    
2516          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2517          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2518    
2519          redo A;          redo A;
2520        } elsif ($self->{next_char} == 0x0050 or # P        } elsif ($self->{nc} == 0x0050 or # P
2521                 $self->{next_char} == 0x0070) { # p                 $self->{nc} == 0x0070) { # p
2522            $self->{state} = PUBLIC_STATE;
2523            $self->{s_kwd} = chr $self->{nc};
2524          !!!next-input-character;          !!!next-input-character;
2525          if ($self->{next_char} == 0x0055 or # U          redo A;
2526              $self->{next_char} == 0x0075) { # u        } elsif ($self->{nc} == 0x0053 or # S
2527            !!!next-input-character;                 $self->{nc} == 0x0073) { # s
2528            if ($self->{next_char} == 0x0042 or # B          $self->{state} = SYSTEM_STATE;
2529                $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  
2530          !!!next-input-character;          !!!next-input-character;
2531          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);  
         }  
   
         #  
2532        } else {        } else {
2533          !!!cp (180);          !!!cp (180);
2534            !!!parse-error (type => 'string after DOCTYPE name');
2535            $self->{ct}->{quirks} = 1;
2536    
2537            $self->{state} = BOGUS_DOCTYPE_STATE;
2538          !!!next-input-character;          !!!next-input-character;
2539          #          redo A;
2540        }        }
2541        } elsif ($self->{state} == PUBLIC_STATE) {
2542          ## ASCII case-insensitive
2543          if ($self->{nc} == [
2544                undef,
2545                0x0055, # U
2546                0x0042, # B
2547                0x004C, # L
2548                0x0049, # I
2549              ]->[length $self->{s_kwd}] or
2550              $self->{nc} == [
2551                undef,
2552                0x0075, # u
2553                0x0062, # b
2554                0x006C, # l
2555                0x0069, # i
2556              ]->[length $self->{s_kwd}]) {
2557            !!!cp (175);
2558            ## Stay in the state.
2559            $self->{s_kwd} .= chr $self->{nc};
2560            !!!next-input-character;
2561            redo A;
2562          } elsif ((length $self->{s_kwd}) == 5 and
2563                   ($self->{nc} == 0x0043 or # C
2564                    $self->{nc} == 0x0063)) { # c
2565            !!!cp (168);
2566            $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2567            !!!next-input-character;
2568            redo A;
2569          } else {
2570            !!!cp (169);
2571            !!!parse-error (type => 'string after DOCTYPE name',
2572                            line => $self->{line_prev},
2573                            column => $self->{column_prev} + 1 - length $self->{s_kwd});
2574            $self->{ct}->{quirks} = 1;
2575    
2576        !!!parse-error (type => 'string after DOCTYPE name');          $self->{state} = BOGUS_DOCTYPE_STATE;
2577        $self->{current_token}->{quirks} = 1;          ## Reconsume.
2578            redo A;
2579          }
2580        } elsif ($self->{state} == SYSTEM_STATE) {
2581          ## ASCII case-insensitive
2582          if ($self->{nc} == [
2583                undef,
2584                0x0059, # Y
2585                0x0053, # S
2586                0x0054, # T
2587                0x0045, # E
2588              ]->[length $self->{s_kwd}] or
2589              $self->{nc} == [
2590                undef,
2591                0x0079, # y
2592                0x0073, # s
2593                0x0074, # t
2594                0x0065, # e
2595              ]->[length $self->{s_kwd}]) {
2596            !!!cp (170);
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} == 0x004D or # M
2603                    $self->{nc} == 0x006D)) { # m
2604            !!!cp (171);
2605            $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2606            !!!next-input-character;
2607            redo A;
2608          } else {
2609            !!!cp (172);
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        $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2616        # next-input-character is already done          ## Reconsume.
2617        redo A;          redo A;
2618          }
2619      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2620        if ({        if ($is_space->{$self->{nc}}) {
             0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,  
             #0x000D => 1, # HT, LF, VT, FF, SP, CR  
           }->{$self->{next_char}}) {  
2621          !!!cp (181);          !!!cp (181);
2622          ## Stay in the state          ## Stay in the state
2623          !!!next-input-character;          !!!next-input-character;
2624          redo A;          redo A;
2625        } elsif ($self->{next_char} eq 0x0022) { # "        } elsif ($self->{nc} eq 0x0022) { # "
2626          !!!cp (182);          !!!cp (182);
2627          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{ct}->{pubid} = ''; # DOCTYPE
2628          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2629          !!!next-input-character;          !!!next-input-character;
2630          redo A;          redo A;
2631        } elsif ($self->{next_char} eq 0x0027) { # '        } elsif ($self->{nc} eq 0x0027) { # '
2632          !!!cp (183);          !!!cp (183);
2633          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{ct}->{pubid} = ''; # DOCTYPE
2634          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2635          !!!next-input-character;          !!!next-input-character;
2636          redo A;          redo A;
2637        } elsif ($self->{next_char} eq 0x003E) { # >        } elsif ($self->{nc} eq 0x003E) { # >
2638          !!!cp (184);          !!!cp (184);
2639          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
2640    
2641          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2642          !!!next-input-character;          !!!next-input-character;
2643    
2644          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2645          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2646    
2647          redo A;          redo A;
2648        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2649          !!!cp (185);          !!!cp (185);
2650          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2651    
2652          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2653          ## reconsume          ## reconsume
2654    
2655          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2656          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2657    
2658          redo A;          redo A;
2659        } else {        } else {
2660          !!!cp (186);          !!!cp (186);
2661          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
2662          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2663    
2664          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2665          !!!next-input-character;          !!!next-input-character;
2666          redo A;          redo A;
2667        }        }
2668      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2669        if ($self->{next_char} == 0x0022) { # "        if ($self->{nc} == 0x0022) { # "
2670          !!!cp (187);          !!!cp (187);
2671          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2672          !!!next-input-character;          !!!next-input-character;
2673          redo A;          redo A;
2674        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2675          !!!cp (188);          !!!cp (188);
2676          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2677    
2678          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2679          !!!next-input-character;          !!!next-input-character;
2680    
2681          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2682          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2683    
2684          redo A;          redo A;
2685        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2686          !!!cp (189);          !!!cp (189);
2687          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2688    
2689          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2690          ## reconsume          ## reconsume
2691    
2692          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2693          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2694    
2695          redo A;          redo A;
2696        } else {        } else {
2697          !!!cp (190);          !!!cp (190);
2698          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{ct}->{pubid} # DOCTYPE
2699              .= chr $self->{next_char};              .= chr $self->{nc};
2700            $self->{read_until}->($self->{ct}->{pubid}, q[">],
2701                                  length $self->{ct}->{pubid});
2702    
2703          ## Stay in the state          ## Stay in the state
2704          !!!next-input-character;          !!!next-input-character;
2705          redo A;          redo A;
2706        }        }
2707      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2708        if ($self->{next_char} == 0x0027) { # '        if ($self->{nc} == 0x0027) { # '
2709          !!!cp (191);          !!!cp (191);
2710          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2711          !!!next-input-character;          !!!next-input-character;
2712          redo A;          redo A;
2713        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2714          !!!cp (192);          !!!cp (192);
2715          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2716    
2717          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2718          !!!next-input-character;          !!!next-input-character;
2719    
2720          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2721          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2722    
2723          redo A;          redo A;
2724        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2725          !!!cp (193);          !!!cp (193);
2726          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2727    
2728          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2729          ## reconsume          ## reconsume
2730    
2731          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2732          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2733    
2734          redo A;          redo A;
2735        } else {        } else {
2736          !!!cp (194);          !!!cp (194);
2737          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{ct}->{pubid} # DOCTYPE
2738              .= chr $self->{next_char};              .= chr $self->{nc};
2739            $self->{read_until}->($self->{ct}->{pubid}, q['>],
2740                                  length $self->{ct}->{pubid});
2741    
2742          ## Stay in the state          ## Stay in the state
2743          !!!next-input-character;          !!!next-input-character;
2744          redo A;          redo A;
2745        }        }
2746      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2747        if ({        if ($is_space->{$self->{nc}}) {
             0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,  
             #0x000D => 1, # HT, LF, VT, FF, SP, CR  
           }->{$self->{next_char}}) {  
2748          !!!cp (195);          !!!cp (195);
2749          ## Stay in the state          ## Stay in the state
2750          !!!next-input-character;          !!!next-input-character;
2751          redo A;          redo A;
2752        } elsif ($self->{next_char} == 0x0022) { # "        } elsif ($self->{nc} == 0x0022) { # "
2753          !!!cp (196);          !!!cp (196);
2754          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{ct}->{sysid} = ''; # DOCTYPE
2755          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2756          !!!next-input-character;          !!!next-input-character;
2757          redo A;          redo A;
2758        } elsif ($self->{next_char} == 0x0027) { # '        } elsif ($self->{nc} == 0x0027) { # '
2759          !!!cp (197);          !!!cp (197);
2760          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{ct}->{sysid} = ''; # DOCTYPE
2761          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2762          !!!next-input-character;          !!!next-input-character;
2763          redo A;          redo A;
2764        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2765          !!!cp (198);          !!!cp (198);
2766          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2767          !!!next-input-character;          !!!next-input-character;
2768    
2769          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2770    
2771          redo A;          redo A;
2772        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2773          !!!cp (199);          !!!cp (199);
2774          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2775    
2776          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2777          ## reconsume          ## reconsume
2778    
2779          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2780          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2781    
2782          redo A;          redo A;
2783        } else {        } else {
2784          !!!cp (200);          !!!cp (200);
2785          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2786          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2787    
2788          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2789          !!!next-input-character;          !!!next-input-character;
2790          redo A;          redo A;
2791        }        }
2792      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2793        if ({        if ($is_space->{$self->{nc}}) {
             0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,  
             #0x000D => 1, # HT, LF, VT, FF, SP, CR  
           }->{$self->{next_char}}) {  
2794          !!!cp (201);          !!!cp (201);
2795          ## Stay in the state          ## Stay in the state
2796          !!!next-input-character;          !!!next-input-character;
2797          redo A;          redo A;
2798        } elsif ($self->{next_char} == 0x0022) { # "        } elsif ($self->{nc} == 0x0022) { # "
2799          !!!cp (202);          !!!cp (202);
2800          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{ct}->{sysid} = ''; # DOCTYPE
2801          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2802          !!!next-input-character;          !!!next-input-character;
2803          redo A;          redo A;
2804        } elsif ($self->{next_char} == 0x0027) { # '        } elsif ($self->{nc} == 0x0027) { # '
2805          !!!cp (203);          !!!cp (203);
2806          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{ct}->{sysid} = ''; # DOCTYPE
2807          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2808          !!!next-input-character;          !!!next-input-character;
2809          redo A;          redo A;
2810        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2811          !!!cp (204);          !!!cp (204);
2812          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2813          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2814          !!!next-input-character;          !!!next-input-character;
2815    
2816          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2817          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2818    
2819          redo A;          redo A;
2820        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2821          !!!cp (205);          !!!cp (205);
2822          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2823    
2824          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2825          ## reconsume          ## reconsume
2826    
2827          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2828          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2829    
2830          redo A;          redo A;
2831        } else {        } else {
2832          !!!cp (206);          !!!cp (206);
2833          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2834          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2835    
2836          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2837          !!!next-input-character;          !!!next-input-character;
2838          redo A;          redo A;
2839        }        }
2840      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2841        if ($self->{next_char} == 0x0022) { # "        if ($self->{nc} == 0x0022) { # "
2842          !!!cp (207);          !!!cp (207);
2843          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2844          !!!next-input-character;          !!!next-input-character;
2845          redo A;          redo A;
2846        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2847          !!!cp (208);          !!!cp (208);
2848          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2849    
2850          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2851          !!!next-input-character;          !!!next-input-character;
2852    
2853          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2854          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2855    
2856          redo A;          redo A;
2857        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2858          !!!cp (209);          !!!cp (209);
2859          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2860    
2861          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2862          ## reconsume          ## reconsume
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        } else {        } else {
2869          !!!cp (210);          !!!cp (210);
2870          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{ct}->{sysid} # DOCTYPE
2871              .= chr $self->{next_char};              .= chr $self->{nc};
2872            $self->{read_until}->($self->{ct}->{sysid}, q[">],
2873                                  length $self->{ct}->{sysid});
2874    
2875          ## Stay in the state          ## Stay in the state
2876          !!!next-input-character;          !!!next-input-character;
2877          redo A;          redo A;
2878        }        }
2879      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2880        if ($self->{next_char} == 0x0027) { # '        if ($self->{nc} == 0x0027) { # '
2881          !!!cp (211);          !!!cp (211);
2882          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2883          !!!next-input-character;          !!!next-input-character;
2884          redo A;          redo A;
2885        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2886          !!!cp (212);          !!!cp (212);
2887          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2888    
2889          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2890          !!!next-input-character;          !!!next-input-character;
2891    
2892          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2893          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2894    
2895          redo A;          redo A;
2896        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2897          !!!cp (213);          !!!cp (213);
2898          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2899    
2900          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2901          ## reconsume          ## reconsume
2902    
2903          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2904          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2905    
2906          redo A;          redo A;
2907        } else {        } else {
2908          !!!cp (214);          !!!cp (214);
2909          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{ct}->{sysid} # DOCTYPE
2910              .= chr $self->{next_char};              .= chr $self->{nc};
2911            $self->{read_until}->($self->{ct}->{sysid}, q['>],
2912                                  length $self->{ct}->{sysid});
2913    
2914          ## Stay in the state          ## Stay in the state
2915          !!!next-input-character;          !!!next-input-character;
2916          redo A;          redo A;
2917        }        }
2918      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2919        if ({        if ($is_space->{$self->{nc}}) {
             0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,  
             #0x000D => 1, # HT, LF, VT, FF, SP, CR  
           }->{$self->{next_char}}) {  
2920          !!!cp (215);          !!!cp (215);
2921          ## Stay in the state          ## Stay in the state
2922          !!!next-input-character;          !!!next-input-character;
2923          redo A;          redo A;
2924        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2925          !!!cp (216);          !!!cp (216);
2926          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2927          !!!next-input-character;          !!!next-input-character;
2928    
2929          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2930    
2931          redo A;          redo A;
2932        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2933          !!!cp (217);          !!!cp (217);
2934          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
   
2935          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2936          ## reconsume          ## reconsume
2937    
2938          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2939          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2940    
2941          redo A;          redo A;
2942        } else {        } else {
2943          !!!cp (218);          !!!cp (218);
2944          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2945          #$self->{current_token}->{quirks} = 1;          #$self->{ct}->{quirks} = 1;
2946    
2947          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2948          !!!next-input-character;          !!!next-input-character;
2949          redo A;          redo A;
2950        }        }
2951      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2952        if ($self->{next_char} == 0x003E) { # >        if ($self->{nc} == 0x003E) { # >
2953          !!!cp (219);          !!!cp (219);
2954          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2955          !!!next-input-character;          !!!next-input-character;
2956    
2957          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2958    
2959          redo A;          redo A;
2960        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2961          !!!cp (220);          !!!cp (220);
2962          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2963          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2964          ## reconsume          ## reconsume
2965    
2966          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2967    
2968          redo A;          redo A;
2969        } else {        } else {
2970          !!!cp (221);          !!!cp (221);
2971            my $s = '';
2972            $self->{read_until}->($s, q[>], 0);
2973    
2974          ## Stay in the state          ## Stay in the state
2975          !!!next-input-character;          !!!next-input-character;
2976          redo A;          redo A;
2977        }        }
2978      } elsif ($self->{state} == CDATA_BLOCK_STATE) {      } elsif ($self->{state} == CDATA_SECTION_STATE) {
2979        my $s = '';        ## NOTE: "CDATA section state" in the state is jointly implemented
2980          ## by three states, |CDATA_SECTION_STATE|, |CDATA_SECTION_MSE1_STATE|,
2981          ## and |CDATA_SECTION_MSE2_STATE|.
2982                
2983        my ($l, $c) = ($self->{line}, $self->{column});        if ($self->{nc} == 0x005D) { # ]
2984            !!!cp (221.1);
2985        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};  
2986          !!!next-input-character;          !!!next-input-character;
2987        } # CS          redo A;
2988          } elsif ($self->{nc} == -1) {
2989            $self->{state} = DATA_STATE;
2990            !!!next-input-character;
2991            if (length $self->{ct}->{data}) { # character
2992              !!!cp (221.2);
2993              !!!emit ($self->{ct}); # character
2994            } else {
2995              !!!cp (221.3);
2996              ## No token to emit. $self->{ct} is discarded.
2997            }        
2998            redo A;
2999          } else {
3000            !!!cp (221.4);
3001            $self->{ct}->{data} .= chr $self->{nc};
3002            $self->{read_until}->($self->{ct}->{data},
3003                                  q<]>,
3004                                  length $self->{ct}->{data});
3005    
3006        $self->{state} = DATA_STATE;          ## Stay in the state.
3007        ## next-input-character done or EOF, which is reconsumed.          !!!next-input-character;
3008            redo A;
3009          }
3010    
3011        if (length $s) {        ## ISSUE: "text tokens" in spec.
3012        } elsif ($self->{state} == CDATA_SECTION_MSE1_STATE) {
3013          if ($self->{nc} == 0x005D) { # ]
3014            !!!cp (221.5);
3015            $self->{state} = CDATA_SECTION_MSE2_STATE;
3016            !!!next-input-character;
3017            redo A;
3018          } else {
3019          !!!cp (221.6);          !!!cp (221.6);
3020          !!!emit ({type => CHARACTER_TOKEN, data => $s,          $self->{ct}->{data} .= ']';
3021                    line => $l, column => $c});          $self->{state} = CDATA_SECTION_STATE;
3022            ## Reconsume.
3023            redo A;
3024          }
3025        } elsif ($self->{state} == CDATA_SECTION_MSE2_STATE) {
3026          if ($self->{nc} == 0x003E) { # >
3027            $self->{state} = DATA_STATE;
3028            !!!next-input-character;
3029            if (length $self->{ct}->{data}) { # character
3030              !!!cp (221.7);
3031              !!!emit ($self->{ct}); # character
3032            } else {
3033              !!!cp (221.8);
3034              ## No token to emit. $self->{ct} is discarded.
3035            }
3036            redo A;
3037          } elsif ($self->{nc} == 0x005D) { # ]
3038            !!!cp (221.9); # character
3039            $self->{ct}->{data} .= ']'; ## Add first "]" of "]]]".
3040            ## Stay in the state.
3041            !!!next-input-character;
3042            redo A;
3043        } else {        } else {
3044          !!!cp (221.7);          !!!cp (221.11);
3045            $self->{ct}->{data} .= ']]'; # character
3046            $self->{state} = CDATA_SECTION_STATE;
3047            ## Reconsume.
3048            redo A;
3049          }
3050        } elsif ($self->{state} == ENTITY_STATE) {
3051          if ($is_space->{$self->{nc}} or
3052              {
3053                0x003C => 1, 0x0026 => 1, -1 => 1, # <, &
3054                $self->{entity_add} => 1,
3055              }->{$self->{nc}}) {
3056            !!!cp (1001);
3057            ## Don't consume
3058            ## No error
3059            ## Return nothing.
3060            #
3061          } elsif ($self->{nc} == 0x0023) { # #
3062            !!!cp (999);
3063            $self->{state} = ENTITY_HASH_STATE;
3064            $self->{s_kwd} = '#';
3065            !!!next-input-character;
3066            redo A;
3067          } elsif ((0x0041 <= $self->{nc} and
3068                    $self->{nc} <= 0x005A) or # A..Z
3069                   (0x0061 <= $self->{nc} and
3070                    $self->{nc} <= 0x007A)) { # a..z
3071            !!!cp (998);
3072            require Whatpm::_NamedEntityList;
3073            $self->{state} = ENTITY_NAME_STATE;
3074            $self->{s_kwd} = chr $self->{nc};
3075            $self->{entity__value} = $self->{s_kwd};
3076            $self->{entity__match} = 0;
3077            !!!next-input-character;
3078            redo A;
3079          } else {
3080            !!!cp (1027);
3081            !!!parse-error (type => 'bare ero');
3082            ## Return nothing.
3083            #
3084        }        }
3085    
3086        redo A;        ## NOTE: No character is consumed by the "consume a character
3087          ## reference" algorithm.  In other word, there is an "&" character
3088        ## ISSUE: "text tokens" in spec.        ## that does not introduce a character reference, which would be
3089        ## TODO: Streaming support        ## appended to the parent element or the attribute value in later
3090      } else {        ## process of the tokenizer.
3091        die "$0: $self->{state}: Unknown state";  
3092      }        if ($self->{prev_state} == DATA_STATE) {
3093    } # A            !!!cp (997);
3094            $self->{state} = $self->{prev_state};
3095    die "$0: _get_next_token: unexpected case";          ## Reconsume.
3096  } # _get_next_token          !!!emit ({type => CHARACTER_TOKEN, data => '&',
3097                      line => $self->{line_prev},
3098  sub _tokenize_attempt_to_consume_an_entity ($$$) {                    column => $self->{column_prev},
3099    my ($self, $in_attr, $additional) = @_;                   });
3100            redo A;
3101    my ($l, $c) = ($self->{line_prev}, $self->{column_prev});        } else {
3102            !!!cp (996);
3103            $self->{ca}->{value} .= '&';
3104            $self->{state} = $self->{prev_state};
3105            ## Reconsume.
3106            redo A;
3107          }
3108        } elsif ($self->{state} == ENTITY_HASH_STATE) {
3109          if ($self->{nc} == 0x0078 or # x
3110              $self->{nc} == 0x0058) { # X
3111            !!!cp (995);
3112            $self->{state} = HEXREF_X_STATE;
3113            $self->{s_kwd} .= chr $self->{nc};
3114            !!!next-input-character;
3115            redo A;
3116          } elsif (0x0030 <= $self->{nc} and
3117                   $self->{nc} <= 0x0039) { # 0..9
3118            !!!cp (994);
3119            $self->{state} = NCR_NUM_STATE;
3120            $self->{s_kwd} = $self->{nc} - 0x0030;
3121            !!!next-input-character;
3122            redo A;
3123          } else {
3124            !!!parse-error (type => 'bare nero',
3125                            line => $self->{line_prev},
3126                            column => $self->{column_prev} - 1);
3127    
3128    if ({          ## NOTE: According to the spec algorithm, nothing is returned,
3129         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,          ## and then "&#" is appended to the parent element or the attribute
3130         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR          ## value in the later processing.
3131         $additional => 1,  
3132        }->{$self->{next_char}}) {          if ($self->{prev_state} == DATA_STATE) {
3133      !!!cp (1001);            !!!cp (1019);
3134      ## Don't consume            $self->{state} = $self->{prev_state};
3135      ## No error            ## Reconsume.
3136      return undef;            !!!emit ({type => CHARACTER_TOKEN,
3137    } elsif ($self->{next_char} == 0x0023) { # #                      data => '&#',
3138      !!!next-input-character;                      line => $self->{line_prev},
3139      if ($self->{next_char} == 0x0078 or # x                      column => $self->{column_prev} - 1,
3140          $self->{next_char} == 0x0058) { # X                     });
3141        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;  
3142          } else {          } else {
3143            !!!cp (1007);            !!!cp (993);
3144            !!!parse-error (type => 'no refc', line => $l, column => $c);            $self->{ca}->{value} .= '&#';
3145              $self->{state} = $self->{prev_state};
3146              ## Reconsume.
3147              redo A;
3148          }          }
3149          }
3150          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {      } elsif ($self->{state} == NCR_NUM_STATE) {
3151            !!!cp (1008);        if (0x0030 <= $self->{nc} and
3152            !!!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  
3153          !!!cp (1012);          !!!cp (1012);
3154          $code *= 10;          $self->{s_kwd} *= 10;
3155          $code += $self->{next_char} - 0x0030;          $self->{s_kwd} += $self->{nc} - 0x0030;
3156                    
3157            ## Stay in the state.
3158          !!!next-input-character;          !!!next-input-character;
3159        }          redo A;
3160          } elsif ($self->{nc} == 0x003B) { # ;
       if ($self->{next_char} == 0x003B) { # ;  
3161          !!!cp (1013);          !!!cp (1013);
3162          !!!next-input-character;          !!!next-input-character;
3163            #
3164        } else {        } else {
3165          !!!cp (1014);          !!!cp (1014);
3166          !!!parse-error (type => 'no refc', line => $l, column => $c);          !!!parse-error (type => 'no refc');
3167            ## Reconsume.
3168            #
3169        }        }
3170    
3171          my $code = $self->{s_kwd};
3172          my $l = $self->{line_prev};
3173          my $c = $self->{column_prev};
3174        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3175          !!!cp (1015);          !!!cp (1015);
3176          !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);          !!!parse-error (type => 'invalid character reference',
3177                            text => (sprintf 'U+%04X', $code),
3178                            line => $l, column => $c);
3179          $code = 0xFFFD;          $code = 0xFFFD;
3180        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
3181          !!!cp (1016);          !!!cp (1016);
3182          !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);          !!!parse-error (type => 'invalid character reference',
3183                            text => (sprintf 'U-%08X', $code),
3184                            line => $l, column => $c);
3185          $code = 0xFFFD;          $code = 0xFFFD;
3186        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
3187          !!!cp (1017);          !!!cp (1017);
3188          !!!parse-error (type => 'CR character reference', line => $l, column => $c);          !!!parse-error (type => 'CR character reference',
3189                            line => $l, column => $c);
3190          $code = 0x000A;          $code = 0x000A;
3191        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3192          !!!cp (1018);          !!!cp (1018);
3193          !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);          !!!parse-error (type => 'C1 character reference',
3194                            text => (sprintf 'U+%04X', $code),
3195                            line => $l, column => $c);
3196          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3197        }        }
3198          
3199        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,        if ($self->{prev_state} == DATA_STATE) {
3200                line => $l, column => $c,          !!!cp (992);
3201               };          $self->{state} = $self->{prev_state};
3202      } else {          ## Reconsume.
3203        !!!cp (1019);          !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3204        !!!parse-error (type => 'bare nero', line => $l, column => $c);                    line => $l, column => $c,
3205        !!!back-next-input-character ($self->{next_char});                   });
3206        $self->{next_char} = 0x0023; # #          redo A;
3207        return undef;        } else {
3208      }          !!!cp (991);
3209    } elsif ((0x0041 <= $self->{next_char} and          $self->{ca}->{value} .= chr $code;
3210              $self->{next_char} <= 0x005A) or          $self->{ca}->{has_reference} = 1;
3211             (0x0061 <= $self->{next_char} and          $self->{state} = $self->{prev_state};
3212              $self->{next_char} <= 0x007A)) {          ## Reconsume.
3213      my $entity_name = chr $self->{next_char};          redo A;
3214      !!!next-input-character;        }
3215        } elsif ($self->{state} == HEXREF_X_STATE) {
3216      my $value = $entity_name;        if ((0x0030 <= $self->{nc} and $self->{nc} <= 0x0039) or
3217      my $match = 0;            (0x0041 <= $self->{nc} and $self->{nc} <= 0x0046) or
3218      require Whatpm::_NamedEntityList;            (0x0061 <= $self->{nc} and $self->{nc} <= 0x0066)) {
3219      our $EntityChar;          # 0..9, A..F, a..f
3220            !!!cp (990);
3221      while (length $entity_name < 30 and          $self->{state} = HEXREF_HEX_STATE;
3222             ## NOTE: Some number greater than the maximum length of entity name          $self->{s_kwd} = 0;
3223             ((0x0041 <= $self->{next_char} and # a          ## Reconsume.
3224               $self->{next_char} <= 0x005A) or # x          redo A;
3225              (0x0061 <= $self->{next_char} and # a        } else {
3226               $self->{next_char} <= 0x007A) or # z          !!!parse-error (type => 'bare hcro',
3227              (0x0030 <= $self->{next_char} and # 0                          line => $self->{line_prev},
3228               $self->{next_char} <= 0x0039) or # 9                          column => $self->{column_prev} - 2);
3229              $self->{next_char} == 0x003B)) { # ;  
3230        $entity_name .= chr $self->{next_char};          ## NOTE: According to the spec algorithm, nothing is returned,
3231        if (defined $EntityChar->{$entity_name}) {          ## and then "&#" followed by "X" or "x" is appended to the parent
3232          if ($self->{next_char} == 0x003B) { # ;          ## element or the attribute value in the later processing.
3233            !!!cp (1020);  
3234            $value = $EntityChar->{$entity_name};          if ($self->{prev_state} == DATA_STATE) {
3235            $match = 1;            !!!cp (1005);
3236            !!!next-input-character;            $self->{state} = $self->{prev_state};
3237            last;            ## Reconsume.
3238              !!!emit ({type => CHARACTER_TOKEN,
3239                        data => '&' . $self->{s_kwd},
3240                        line => $self->{line_prev},
3241                        column => $self->{column_prev} - length $self->{s_kwd},
3242                       });
3243              redo A;
3244            } else {
3245              !!!cp (989);
3246              $self->{ca}->{value} .= '&' . $self->{s_kwd};
3247              $self->{state} = $self->{prev_state};
3248              ## Reconsume.
3249              redo A;
3250            }
3251          }
3252        } elsif ($self->{state} == HEXREF_HEX_STATE) {
3253          if (0x0030 <= $self->{nc} and $self->{nc} <= 0x0039) {
3254            # 0..9
3255            !!!cp (1002);
3256            $self->{s_kwd} *= 0x10;
3257            $self->{s_kwd} += $self->{nc} - 0x0030;
3258            ## Stay in the state.
3259            !!!next-input-character;
3260            redo A;
3261          } elsif (0x0061 <= $self->{nc} and
3262                   $self->{nc} <= 0x0066) { # a..f
3263            !!!cp (1003);
3264            $self->{s_kwd} *= 0x10;
3265            $self->{s_kwd} += $self->{nc} - 0x0060 + 9;
3266            ## Stay in the state.
3267            !!!next-input-character;
3268            redo A;
3269          } elsif (0x0041 <= $self->{nc} and
3270                   $self->{nc} <= 0x0046) { # A..F
3271            !!!cp (1004);
3272            $self->{s_kwd} *= 0x10;
3273            $self->{s_kwd} += $self->{nc} - 0x0040 + 9;
3274            ## Stay in the state.
3275            !!!next-input-character;
3276            redo A;
3277          } elsif ($self->{nc} == 0x003B) { # ;
3278            !!!cp (1006);
3279            !!!next-input-character;
3280            #
3281          } else {
3282            !!!cp (1007);
3283            !!!parse-error (type => 'no refc',
3284                            line => $self->{line},
3285                            column => $self->{column});
3286            ## Reconsume.
3287            #
3288          }
3289    
3290          my $code = $self->{s_kwd};
3291          my $l = $self->{line_prev};
3292          my $c = $self->{column_prev};
3293          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3294            !!!cp (1008);
3295            !!!parse-error (type => 'invalid character reference',
3296                            text => (sprintf 'U+%04X', $code),
3297                            line => $l, column => $c);
3298            $code = 0xFFFD;
3299          } elsif ($code > 0x10FFFF) {
3300            !!!cp (1009);
3301            !!!parse-error (type => 'invalid character reference',
3302                            text => (sprintf 'U-%08X', $code),
3303                            line => $l, column => $c);
3304            $code = 0xFFFD;
3305          } elsif ($code == 0x000D) {
3306            !!!cp (1010);
3307            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
3308            $code = 0x000A;
3309          } elsif (0x80 <= $code and $code <= 0x9F) {
3310            !!!cp (1011);
3311            !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
3312            $code = $c1_entity_char->{$code};
3313          }
3314    
3315          if ($self->{prev_state} == DATA_STATE) {
3316            !!!cp (988);
3317            $self->{state} = $self->{prev_state};
3318            ## Reconsume.
3319            !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3320                      line => $l, column => $c,
3321                     });
3322            redo A;
3323          } else {
3324            !!!cp (987);
3325            $self->{ca}->{value} .= chr $code;
3326            $self->{ca}->{has_reference} = 1;
3327            $self->{state} = $self->{prev_state};
3328            ## Reconsume.
3329            redo A;
3330          }
3331        } elsif ($self->{state} == ENTITY_NAME_STATE) {
3332          if (length $self->{s_kwd} < 30 and
3333              ## NOTE: Some number greater than the maximum length of entity name
3334              ((0x0041 <= $self->{nc} and # a
3335                $self->{nc} <= 0x005A) or # x
3336               (0x0061 <= $self->{nc} and # a
3337                $self->{nc} <= 0x007A) or # z
3338               (0x0030 <= $self->{nc} and # 0
3339                $self->{nc} <= 0x0039) or # 9
3340               $self->{nc} == 0x003B)) { # ;
3341            our $EntityChar;
3342            $self->{s_kwd} .= chr $self->{nc};
3343            if (defined $EntityChar->{$self->{s_kwd}}) {
3344              if ($self->{nc} == 0x003B) { # ;
3345                !!!cp (1020);
3346                $self->{entity__value} = $EntityChar->{$self->{s_kwd}};
3347                $self->{entity__match} = 1;
3348                !!!next-input-character;
3349                #
3350              } else {
3351                !!!cp (1021);
3352                $self->{entity__value} = $EntityChar->{$self->{s_kwd}};
3353                $self->{entity__match} = -1;
3354                ## Stay in the state.
3355                !!!next-input-character;
3356                redo A;
3357              }
3358          } else {          } else {
3359            !!!cp (1021);            !!!cp (1022);
3360            $value = $EntityChar->{$entity_name};            $self->{entity__value} .= chr $self->{nc};
3361            $match = -1;            $self->{entity__match} *= 2;
3362              ## Stay in the state.
3363            !!!next-input-character;            !!!next-input-character;
3364              redo A;
3365            }
3366          }
3367    
3368          my $data;
3369          my $has_ref;
3370          if ($self->{entity__match} > 0) {
3371            !!!cp (1023);
3372            $data = $self->{entity__value};
3373            $has_ref = 1;
3374            #
3375          } elsif ($self->{entity__match} < 0) {
3376            !!!parse-error (type => 'no refc');
3377            if ($self->{prev_state} != DATA_STATE and # in attribute
3378                $self->{entity__match} < -1) {
3379              !!!cp (1024);
3380              $data = '&' . $self->{s_kwd};
3381              #
3382            } else {
3383              !!!cp (1025);
3384              $data = $self->{entity__value};
3385              $has_ref = 1;
3386              #
3387          }          }
3388        } else {        } else {
3389          !!!cp (1022);          !!!cp (1026);
3390          $value .= chr $self->{next_char};          !!!parse-error (type => 'bare ero',
3391          $match *= 2;                          line => $self->{line_prev},
3392          !!!next-input-character;                          column => $self->{column_prev} - length $self->{s_kwd});
3393            $data = '&' . $self->{s_kwd};
3394            #
3395        }        }
3396      }    
3397              ## NOTE: In these cases, when a character reference is found,
3398      if ($match > 0) {        ## it is consumed and a character token is returned, or, otherwise,
3399        !!!cp (1023);        ## nothing is consumed and returned, according to the spec algorithm.
3400        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,        ## In this implementation, anything that has been examined by the
3401                line => $l, column => $c,        ## tokenizer is appended to the parent element or the attribute value
3402               };        ## as string, either literal string when no character reference or
3403      } elsif ($match < 0) {        ## entity-replaced string otherwise, in this stage, since any characters
3404        !!!parse-error (type => 'no refc', line => $l, column => $c);        ## that would not be consumed are appended in the data state or in an
3405        if ($in_attr and $match < -1) {        ## appropriate attribute value state anyway.
3406          !!!cp (1024);  
3407          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,        if ($self->{prev_state} == DATA_STATE) {
3408                  line => $l, column => $c,          !!!cp (986);
3409                 };          $self->{state} = $self->{prev_state};
3410        } else {          ## Reconsume.
3411          !!!cp (1025);          !!!emit ({type => CHARACTER_TOKEN,
3412          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,                    data => $data,
3413                  line => $l, column => $c,                    line => $self->{line_prev},
3414                 };                    column => $self->{column_prev} + 1 - length $self->{s_kwd},
3415                     });
3416            redo A;
3417          } else {
3418            !!!cp (985);
3419            $self->{ca}->{value} .= $data;
3420            $self->{ca}->{has_reference} = 1 if $has_ref;
3421            $self->{state} = $self->{prev_state};
3422            ## Reconsume.
3423            redo A;
3424        }        }
3425      } else {      } else {
3426        !!!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,  
              };  
3427      }      }
3428    } else {    } # A  
3429      !!!cp (1027);  
3430      ## no characters are consumed    die "$0: _get_next_token: unexpected case";
3431      !!!parse-error (type => 'bare ero', line => $l, column => $c);  } # _get_next_token
     return undef;  
   }  
 } # _tokenize_attempt_to_consume_an_entity  
3432    
3433  sub _initialize_tree_constructor ($) {  sub _initialize_tree_constructor ($) {
3434    my $self = shift;    my $self = shift;
# Line 3057  sub _initialize_tree_constructor ($) { Line 3437  sub _initialize_tree_constructor ($) {
3437    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3438    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3439    $self->{document}->manakai_is_html (1); # MUST    $self->{document}->manakai_is_html (1); # MUST
3440      $self->{document}->set_user_data (manakai_source_line => 1);
3441      $self->{document}->set_user_data (manakai_source_column => 1);
3442  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3443    
3444  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 3111  sub _tree_construction_initial ($) { Line 3493  sub _tree_construction_initial ($) {
3493        ## language.        ## language.
3494        my $doctype_name = $token->{name};        my $doctype_name = $token->{name};
3495        $doctype_name = '' unless defined $doctype_name;        $doctype_name = '' unless defined $doctype_name;
3496        $doctype_name =~ tr/a-z/A-Z/;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3497        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
3498            defined $token->{public_identifier} or            defined $token->{sysid}) {
           defined $token->{system_identifier}) {  
3499          !!!cp ('t1');          !!!cp ('t1');
3500          !!!parse-error (type => 'not HTML5', token => $token);          !!!parse-error (type => 'not HTML5', token => $token);
3501        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3502          !!!cp ('t2');          !!!cp ('t2');
         ## ISSUE: ASCII case-insensitive? (in fact it does not matter)  
3503          !!!parse-error (type => 'not HTML5', token => $token);          !!!parse-error (type => 'not HTML5', token => $token);
3504          } elsif (defined $token->{pubid}) {
3505            if ($token->{pubid} eq 'XSLT-compat') {
3506              !!!cp ('t1.2');
3507              !!!parse-error (type => 'XSLT-compat', token => $token,
3508                              level => $self->{level}->{should});
3509            } else {
3510              !!!parse-error (type => 'not HTML5', token => $token);
3511            }
3512        } else {        } else {
3513          !!!cp ('t3');          !!!cp ('t3');
3514            #
3515        }        }
3516                
3517        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3518          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3519        ## NOTE: Default value for both |public_id| and |system_id| attributes        ## NOTE: Default value for both |public_id| and |system_id| attributes
3520        ## 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.
3521        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{pubid}) if defined $token->{pubid};
3522            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};  
3523        ## NOTE: Other DocumentType attributes are null or empty lists.        ## NOTE: Other DocumentType attributes are null or empty lists.
3524        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
3525        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
# Line 3140  sub _tree_construction_initial ($) { Line 3527  sub _tree_construction_initial ($) {
3527        if ($token->{quirks} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
3528          !!!cp ('t4');          !!!cp ('t4');
3529          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
3530        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{pubid}) {
3531          my $pubid = $token->{public_identifier};          my $pubid = $token->{pubid};
3532          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3533          if ({          my $prefix = [
3534            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3535            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3536            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3537            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3538            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3539            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3540            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3541            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3542            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3543            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3544            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3545            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3546            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3547            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3548            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3549            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3550            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3551            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3552            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3553            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3554            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3555            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3556            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3557            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3558            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3559            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3560            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3561            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3562            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3563            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3564            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3565            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3566            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3567            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3568            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3569            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3570            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3571            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3572            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3573            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3574            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3575            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3576            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3577            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3578            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3579            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3580            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3581            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3582            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3583            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3584            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3585            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD W3 HTML//",
3586            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3587            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3588            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3589            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,          ]; # $prefix
3590            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,          my $match;
3591            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,          for (@$prefix) {
3592            "-//W3C//DTD HTML 3.2//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3593            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,              $match = 1;
3594            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,              last;
3595            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            }
3596            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,          }
3597            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,          if ($match or
3598            "-//W3C//DTD W3 HTML//EN" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3599            "-//W3O//DTD W3 HTML 3.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3600            "-//W3O//DTD W3 HTML 3.0//EN//" => 1,              $pubid eq "HTML") {
           "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,  
           "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,  
           "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,  
           "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,  
           "HTML" => 1,  
         }->{$pubid}) {  
3601            !!!cp ('t5');            !!!cp ('t5');
3602            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3603          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3604                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3605            if (defined $token->{system_identifier}) {            if (defined $token->{sysid}) {
3606              !!!cp ('t6');              !!!cp ('t6');
3607              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
3608            } else {            } else {
3609              !!!cp ('t7');              !!!cp ('t7');
3610              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3611            }            }
3612          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3613                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3614            !!!cp ('t8');            !!!cp ('t8');
3615            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3616          } else {          } else {
# Line 3238  sub _tree_construction_initial ($) { Line 3619  sub _tree_construction_initial ($) {
3619        } else {        } else {
3620          !!!cp ('t10');          !!!cp ('t10');
3621        }        }
3622        if (defined $token->{system_identifier}) {        if (defined $token->{sysid}) {
3623          my $sysid = $token->{system_identifier};          my $sysid = $token->{sysid};
3624          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3625          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") {
3626            ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"            ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3627              ## marked as quirks.
3628            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3629            !!!cp ('t11');            !!!cp ('t11');
3630          } else {          } else {
# Line 3268  sub _tree_construction_initial ($) { Line 3650  sub _tree_construction_initial ($) {
3650        !!!ack-later;        !!!ack-later;
3651        return;        return;
3652      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3653        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
3654          ## Ignore the token          ## Ignore the token
3655    
3656          unless (length $token->{data}) {          unless (length $token->{data}) {
# Line 3325  sub _tree_construction_root_element ($) Line 3707  sub _tree_construction_root_element ($)
3707          !!!next-token;          !!!next-token;
3708          redo B;          redo B;
3709        } elsif ($token->{type} == CHARACTER_TOKEN) {        } elsif ($token->{type} == CHARACTER_TOKEN) {
3710          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D          if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
3711            ## Ignore the token.            ## Ignore the token.
3712    
3713            unless (length $token->{data}) {            unless (length $token->{data}) {
# Line 3428  sub _reset_insertion_mode ($) { Line 3810  sub _reset_insertion_mode ($) {
3810          ## NOTE: Strictly spaking, the line below only applies to MathML and          ## NOTE: Strictly spaking, the line below only applies to MathML and
3811          ## SVG elements.  Currently the HTML syntax supports only MathML and          ## SVG elements.  Currently the HTML syntax supports only MathML and
3812          ## SVG elements as foreigners.          ## SVG elements as foreigners.
3813          $new_mode = $self->{insertion_mode} | IN_FOREIGN_CONTENT_IM;          $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
         ## ISSUE: What is set as the secondary insertion mode?  
3814        } elsif ($node->[1] & TABLE_CELL_EL) {        } elsif ($node->[1] & TABLE_CELL_EL) {
3815          if ($last) {          if ($last) {
3816            !!!cp ('t28.2');            !!!cp ('t28.2');
# Line 3630  sub _tree_construction_main ($) { Line 4011  sub _tree_construction_main ($) {
4011        ## NOTE: An end-of-file token.        ## NOTE: An end-of-file token.
4012        if ($content_model_flag == CDATA_CONTENT_MODEL) {        if ($content_model_flag == CDATA_CONTENT_MODEL) {
4013          !!!cp ('t43');          !!!cp ('t43');
4014          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);          !!!parse-error (type => 'in CDATA:#eof', token => $token);
4015        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
4016          !!!cp ('t44');          !!!cp ('t44');
4017          !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);          !!!parse-error (type => 'in RCDATA:#eof', token => $token);
4018        } else {        } else {
4019          die "$0: $content_model_flag in parse_rcdata";          die "$0: $content_model_flag in parse_rcdata";
4020        }        }
# Line 3670  sub _tree_construction_main ($) { Line 4051  sub _tree_construction_main ($) {
4051        ## Ignore the token        ## Ignore the token
4052      } else {      } else {
4053        !!!cp ('t48');        !!!cp ('t48');
4054        !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);        !!!parse-error (type => 'in CDATA:#eof', token => $token);
4055        ## ISSUE: And ignore?        ## ISSUE: And ignore?
4056        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
4057      }      }
# Line 3721  sub _tree_construction_main ($) { Line 4102  sub _tree_construction_main ($) {
4102        } # AFE        } # AFE
4103        unless (defined $formatting_element) {        unless (defined $formatting_element) {
4104          !!!cp ('t53');          !!!cp ('t53');
4105          !!!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);
4106          ## Ignore the token          ## Ignore the token
4107          !!!next-token;          !!!next-token;
4108          return;          return;
# Line 3738  sub _tree_construction_main ($) { Line 4119  sub _tree_construction_main ($) {
4119              last INSCOPE;              last INSCOPE;
4120            } else { # in open elements but not in scope            } else { # in open elements but not in scope
4121              !!!cp ('t55');              !!!cp ('t55');
4122              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},              !!!parse-error (type => 'unmatched end tag',
4123                                text => $token->{tag_name},
4124                              token => $end_tag_token);                              token => $end_tag_token);
4125              ## Ignore the token              ## Ignore the token
4126              !!!next-token;              !!!next-token;
# Line 3751  sub _tree_construction_main ($) { Line 4133  sub _tree_construction_main ($) {
4133        } # INSCOPE        } # INSCOPE
4134        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
4135          !!!cp ('t57');          !!!cp ('t57');
4136          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},          !!!parse-error (type => 'unmatched end tag',
4137                            text => $token->{tag_name},
4138                          token => $end_tag_token);                          token => $end_tag_token);
4139          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
4140          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
# Line 3760  sub _tree_construction_main ($) { Line 4143  sub _tree_construction_main ($) {
4143        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
4144          !!!cp ('t58');          !!!cp ('t58');
4145          !!!parse-error (type => 'not closed',          !!!parse-error (type => 'not closed',
4146                          value => $self->{open_elements}->[-1]->[0]                          text => $self->{open_elements}->[-1]->[0]
4147                              ->manakai_local_name,                              ->manakai_local_name,
4148                          token => $end_tag_token);                          token => $end_tag_token);
4149        }        }
# Line 3969  sub _tree_construction_main ($) { Line 4352  sub _tree_construction_main ($) {
4352    B: while (1) {    B: while (1) {
4353      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
4354        !!!cp ('t73');        !!!cp ('t73');
4355        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4356        ## Ignore the token        ## Ignore the token
4357        ## Stay in the phase        ## Stay in the phase
4358        !!!next-token;        !!!next-token;
# Line 3978  sub _tree_construction_main ($) { Line 4361  sub _tree_construction_main ($) {
4361               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
4362        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4363          !!!cp ('t79');          !!!cp ('t79');
4364          !!!parse-error (type => 'after html:html', token => $token);          !!!parse-error (type => 'after html', text => 'html', token => $token);
4365          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
4366        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4367          !!!cp ('t80');          !!!cp ('t80');
4368          !!!parse-error (type => 'after html:html', token => $token);          !!!parse-error (type => 'after html', text => 'html', token => $token);
4369          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4370        } else {        } else {
4371          !!!cp ('t81');          !!!cp ('t81');
# Line 4033  sub _tree_construction_main ($) { Line 4416  sub _tree_construction_main ($) {
4416            #            #
4417          } elsif ({          } elsif ({
4418                    b => 1, big => 1, blockquote => 1, body => 1, br => 1,                    b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4419                    center => 1, code => 1, dd => 1, div => 1, dl => 1, em => 1,                    center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4420                    embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1, ## No h4!                    em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4421                    h5 => 1, h6 => 1, head => 1, hr => 1, i => 1, img => 1,                    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4422                    li => 1, menu => 1, meta => 1, nobr => 1, p => 1, pre => 1,                    img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4423                    ruby => 1, s => 1, small => 1, span => 1, strong => 1,                    nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4424                    sub => 1, sup => 1, table => 1, tt => 1, u => 1, ul => 1,                    small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4425                    var => 1,                    sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4426                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
4427            !!!cp ('t87.2');            !!!cp ('t87.2');
4428            !!!parse-error (type => 'not closed',            !!!parse-error (type => 'not closed',
4429                            value => $self->{open_elements}->[-1]->[0]                            text => $self->{open_elements}->[-1]->[0]
4430                                ->manakai_local_name,                                ->manakai_local_name,
4431                            token => $token);                            token => $token);
4432    
# Line 4119  sub _tree_construction_main ($) { Line 4502  sub _tree_construction_main ($) {
4502          !!!cp ('t87.5');          !!!cp ('t87.5');
4503          #          #
4504        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
         ## NOTE: "using the rules for secondary insertion mode" then "continue"  
4505          !!!cp ('t87.6');          !!!cp ('t87.6');
4506          #          !!!parse-error (type => 'not closed',
4507          ## TODO: ...                          text => $self->{open_elements}->[-1]->[0]
4508                                ->manakai_local_name,
4509                            token => $token);
4510    
4511            pop @{$self->{open_elements}}
4512                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4513    
4514            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4515            ## Reprocess.
4516            next B;
4517        } else {        } else {
4518          die "$0: $token->{type}: Unknown token type";                  die "$0: $token->{type}: Unknown token type";        
4519        }        }
# Line 4130  sub _tree_construction_main ($) { Line 4521  sub _tree_construction_main ($) {
4521    
4522      if ($self->{insertion_mode} & HEAD_IMS) {      if ($self->{insertion_mode} & HEAD_IMS) {
4523        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4524          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
4525            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4526              !!!cp ('t88.2');              !!!cp ('t88.2');
4527              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4528                #
4529            } else {            } else {
4530              !!!cp ('t88.1');              !!!cp ('t88.1');
4531              ## Ignore the token.              ## Ignore the token.
4532              !!!next-token;              #
             next B;  
4533            }            }
4534            unless (length $token->{data}) {            unless (length $token->{data}) {
4535              !!!cp ('t88');              !!!cp ('t88');
4536              !!!next-token;              !!!next-token;
4537              next B;              next B;
4538            }            }
4539    ## TODO: set $token->{column} appropriately
4540          }          }
4541    
4542          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
# Line 4163  sub _tree_construction_main ($) { Line 4555  sub _tree_construction_main ($) {
4555            !!!cp ('t90');            !!!cp ('t90');
4556            ## As if </noscript>            ## As if </noscript>
4557            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4558            !!!parse-error (type => 'in noscript:#character', token => $token);            !!!parse-error (type => 'in noscript:#text', token => $token);
4559                        
4560            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4561            ## As if </head>            ## As if </head>
# Line 4200  sub _tree_construction_main ($) { Line 4592  sub _tree_construction_main ($) {
4592              next B;              next B;
4593            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4594              !!!cp ('t93.2');              !!!cp ('t93.2');
4595              !!!parse-error (type => 'after head:head', token => $token); ## TODO: error type              !!!parse-error (type => 'after head', text => 'head',
4596                                token => $token);
4597              ## Ignore the token              ## Ignore the token
4598              !!!nack ('t93.3');              !!!nack ('t93.3');
4599              !!!next-token;              !!!next-token;
4600              next B;              next B;
4601            } else {            } else {
4602              !!!cp ('t95');              !!!cp ('t95');
4603              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript              !!!parse-error (type => 'in head:head',
4604                                token => $token); # or in head noscript
4605              ## Ignore the token              ## Ignore the token
4606              !!!nack ('t95.1');              !!!nack ('t95.1');
4607              !!!next-token;              !!!next-token;
# Line 4232  sub _tree_construction_main ($) { Line 4626  sub _tree_construction_main ($) {
4626                  !!!cp ('t98');                  !!!cp ('t98');
4627                  ## As if </noscript>                  ## As if </noscript>
4628                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4629                  !!!parse-error (type => 'in noscript:base', token => $token);                  !!!parse-error (type => 'in noscript', text => 'base',
4630                                    token => $token);
4631                                
4632                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4633                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 4243  sub _tree_construction_main ($) { Line 4638  sub _tree_construction_main ($) {
4638                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4639                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4640                  !!!cp ('t100');                  !!!cp ('t100');
4641                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4642                                    text => $token->{tag_name}, token => $token);
4643                  push @{$self->{open_elements}},                  push @{$self->{open_elements}},
4644                      [$self->{head_element}, $el_category->{head}];                      [$self->{head_element}, $el_category->{head}];
4645                } else {                } else {
# Line 4260  sub _tree_construction_main ($) { Line 4656  sub _tree_construction_main ($) {
4656                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4657                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4658                  !!!cp ('t102');                  !!!cp ('t102');
4659                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4660                                    text => $token->{tag_name}, token => $token);
4661                  push @{$self->{open_elements}},                  push @{$self->{open_elements}},
4662                      [$self->{head_element}, $el_category->{head}];                      [$self->{head_element}, $el_category->{head}];
4663                } else {                } else {
# Line 4277  sub _tree_construction_main ($) { Line 4674  sub _tree_construction_main ($) {
4674                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4675                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4676                  !!!cp ('t104');                  !!!cp ('t104');
4677                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4678                                    text => $token->{tag_name}, token => $token);
4679                  push @{$self->{open_elements}},                  push @{$self->{open_elements}},
4680                      [$self->{head_element}, $el_category->{head}];                      [$self->{head_element}, $el_category->{head}];
4681                } else {                } else {
# Line 4301  sub _tree_construction_main ($) { Line 4699  sub _tree_construction_main ($) {
4699                                                 ->{has_reference});                                                 ->{has_reference});
4700                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
4701                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4702                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4703                            [\x09-\x0D\x20]*=                            [\x09\x0A\x0C\x0D\x20]*=
4704                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09\x0A\x0C\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4705                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09\x0A\x0C\x0D\x20]
4706                               [^\x09\x0A\x0C\x0D\x20\x3B]*))/x) {
4707                      !!!cp ('t107');                      !!!cp ('t107');
4708                      ## NOTE: Whether the encoding is supported or not is handled                      ## NOTE: Whether the encoding is supported or not is handled
4709                      ## in the {change_encoding} callback.                      ## in the {change_encoding} callback.
# Line 4346  sub _tree_construction_main ($) { Line 4745  sub _tree_construction_main ($) {
4745                  !!!cp ('t111');                  !!!cp ('t111');
4746                  ## As if </noscript>                  ## As if </noscript>
4747                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4748                  !!!parse-error (type => 'in noscript:title', token => $token);                  !!!parse-error (type => 'in noscript', text => 'title',
4749                                    token => $token);
4750                                
4751                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4752                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4753                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4754                  !!!cp ('t112');                  !!!cp ('t112');
4755                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4756                                    text => $token->{tag_name}, token => $token);
4757                  push @{$self->{open_elements}},                  push @{$self->{open_elements}},
4758                      [$self->{head_element}, $el_category->{head}];                      [$self->{head_element}, $el_category->{head}];
4759                } else {                } else {
# Line 4366  sub _tree_construction_main ($) { Line 4767  sub _tree_construction_main ($) {
4767                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4768                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4769                next B;                next B;
4770              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style' or
4771                         $token->{tag_name} eq 'noframes') {
4772                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4773                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4774                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4775                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4776                  !!!cp ('t114');                  !!!cp ('t114');
4777                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4778                                    text => $token->{tag_name}, token => $token);
4779                  push @{$self->{open_elements}},                  push @{$self->{open_elements}},
4780                      [$self->{head_element}, $el_category->{head}];                      [$self->{head_element}, $el_category->{head}];
4781                } else {                } else {
# Line 4393  sub _tree_construction_main ($) { Line 4796  sub _tree_construction_main ($) {
4796                  next B;                  next B;
4797                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4798                  !!!cp ('t117');                  !!!cp ('t117');
4799                  !!!parse-error (type => 'in noscript:noscript', token => $token);                  !!!parse-error (type => 'in noscript', text => 'noscript',
4800                                    token => $token);
4801                  ## Ignore the token                  ## Ignore the token
4802                  !!!nack ('t117.1');                  !!!nack ('t117.1');
4803                  !!!next-token;                  !!!next-token;
# Line 4407  sub _tree_construction_main ($) { Line 4811  sub _tree_construction_main ($) {
4811                  !!!cp ('t119');                  !!!cp ('t119');
4812                  ## As if </noscript>                  ## As if </noscript>
4813                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4814                  !!!parse-error (type => 'in noscript:script', token => $token);                  !!!parse-error (type => 'in noscript', text => 'script',
4815                                    token => $token);
4816                                
4817                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4818                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4819                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4820                  !!!cp ('t120');                  !!!cp ('t120');
4821                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4822                                    text => $token->{tag_name}, token => $token);
4823                  push @{$self->{open_elements}},                  push @{$self->{open_elements}},
4824                      [$self->{head_element}, $el_category->{head}];                      [$self->{head_element}, $el_category->{head}];
4825                } else {                } else {
# Line 4431  sub _tree_construction_main ($) { Line 4837  sub _tree_construction_main ($) {
4837                  !!!cp ('t122');                  !!!cp ('t122');
4838                  ## As if </noscript>                  ## As if </noscript>
4839                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4840                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'in noscript',
4841                                    text => $token->{tag_name}, token => $token);
4842                                    
4843                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4844                  ## As if </head>                  ## As if </head>
# Line 4470  sub _tree_construction_main ($) { Line 4877  sub _tree_construction_main ($) {
4877                !!!cp ('t129');                !!!cp ('t129');
4878                ## As if </noscript>                ## As if </noscript>
4879                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4880                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'in noscript:/',
4881                                  text => $token->{tag_name}, token => $token);
4882                                
4883                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4884                ## As if </head>                ## As if </head>
# Line 4513  sub _tree_construction_main ($) { Line 4921  sub _tree_construction_main ($) {
4921                  !!!cp ('t133');                  !!!cp ('t133');
4922                  ## As if </noscript>                  ## As if </noscript>
4923                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4924                  !!!parse-error (type => 'in noscript:/head', token => $token);                  !!!parse-error (type => 'in noscript:/',
4925                                    text => 'head', token => $token);
4926                                    
4927                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4928                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4528  sub _tree_construction_main ($) { Line 4937  sub _tree_construction_main ($) {
4937                  next B;                  next B;
4938                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4939                  !!!cp ('t134.1');                  !!!cp ('t134.1');
4940                  !!!parse-error (type => 'unmatched end tag:head', token => $token);                  !!!parse-error (type => 'unmatched end tag', text => 'head',
4941                                    token => $token);
4942                  ## Ignore the token                  ## Ignore the token
4943                  !!!next-token;                  !!!next-token;
4944                  next B;                  next B;
# Line 4545  sub _tree_construction_main ($) { Line 4955  sub _tree_construction_main ($) {
4955                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4956                         $self->{insertion_mode} == AFTER_HEAD_IM) {                         $self->{insertion_mode} == AFTER_HEAD_IM) {
4957                  !!!cp ('t137');                  !!!cp ('t137');
4958                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);                  !!!parse-error (type => 'unmatched end tag',
4959                                    text => 'noscript', token => $token);
4960                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4961                  !!!next-token;                  !!!next-token;
4962                  next B;                  next B;
# Line 4560  sub _tree_construction_main ($) { Line 4971  sub _tree_construction_main ($) {
4971                    $self->{insertion_mode} == IN_HEAD_IM or                    $self->{insertion_mode} == IN_HEAD_IM or
4972                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4973                  !!!cp ('t140');                  !!!cp ('t140');
4974                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
4975                                    text => $token->{tag_name}, token => $token);
4976                  ## Ignore the token                  ## Ignore the token
4977                  !!!next-token;                  !!!next-token;
4978                  next B;                  next B;
4979                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4980                  !!!cp ('t140.1');                  !!!cp ('t140.1');
4981                  !!!parse-error (type => 'unmatched end tag:' . $token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
4982                                    text => $token->{tag_name}, token => $token);
4983                  ## Ignore the token                  ## Ignore the token
4984                  !!!next-token;                  !!!next-token;
4985                  next B;                  next B;
# Line 4575  sub _tree_construction_main ($) { Line 4988  sub _tree_construction_main ($) {
4988                }                }
4989              } elsif ($token->{tag_name} eq 'p') {              } elsif ($token->{tag_name} eq 'p') {
4990                !!!cp ('t142');                !!!cp ('t142');
4991                !!!parse-error (type => 'unmatched end tag:p', token => $token);                !!!parse-error (type => 'unmatched end tag',
4992                                  text => $token->{tag_name}, token => $token);
4993                ## Ignore the token                ## Ignore the token
4994                !!!next-token;                !!!next-token;
4995                next B;                next B;
# Line 4598  sub _tree_construction_main ($) { Line 5012  sub _tree_construction_main ($) {
5012                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5013                  !!!cp ('t143.3');                  !!!cp ('t143.3');
5014                  ## ISSUE: Two parse errors for <head><noscript></br>                  ## ISSUE: Two parse errors for <head><noscript></br>
5015                  !!!parse-error (type => 'unmatched end tag:br', token => $token);                  !!!parse-error (type => 'unmatched end tag',
5016                                    text => 'br', token => $token);
5017                  ## As if </noscript>                  ## As if </noscript>
5018                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5019                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
# Line 4617  sub _tree_construction_main ($) { Line 5032  sub _tree_construction_main ($) {
5032                }                }
5033    
5034                ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.                ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
5035                !!!parse-error (type => 'unmatched end tag:br', token => $token);                !!!parse-error (type => 'unmatched end tag',
5036                                  text => 'br', token => $token);
5037                ## Ignore the token                ## Ignore the token
5038                !!!next-token;                !!!next-token;
5039                next B;                next B;
5040              } else {              } else {
5041                !!!cp ('t145');                !!!cp ('t145');
5042                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
5043                                  text => $token->{tag_name}, token => $token);
5044                ## Ignore the token                ## Ignore the token
5045                !!!next-token;                !!!next-token;
5046                next B;                next B;
# Line 4633  sub _tree_construction_main ($) { Line 5050  sub _tree_construction_main ($) {
5050                !!!cp ('t146');                !!!cp ('t146');
5051                ## As if </noscript>                ## As if </noscript>
5052                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5053                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'in noscript:/',
5054                                  text => $token->{tag_name}, token => $token);
5055                                
5056                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
5057                ## As if </head>                ## As if </head>
# Line 4649  sub _tree_construction_main ($) { Line 5067  sub _tree_construction_main ($) {
5067              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5068  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
5069                !!!cp ('t148');                !!!cp ('t148');
5070                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
5071                                  text => $token->{tag_name}, token => $token);
5072                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
5073                !!!next-token;                !!!next-token;
5074                next B;                next B;
# Line 4760  sub _tree_construction_main ($) { Line 5179  sub _tree_construction_main ($) {
5179    
5180                  !!!cp ('t153');                  !!!cp ('t153');
5181                  !!!parse-error (type => 'start tag not allowed',                  !!!parse-error (type => 'start tag not allowed',
5182                      value => $token->{tag_name}, token => $token);                      text => $token->{tag_name}, token => $token);
5183                  ## Ignore the token                  ## Ignore the token
5184                  !!!nack ('t153.1');                  !!!nack ('t153.1');
5185                  !!!next-token;                  !!!next-token;
5186                  next B;                  next B;
5187                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5188                  !!!parse-error (type => 'not closed:caption', token => $token);                  !!!parse-error (type => 'not closed', text => 'caption',
5189                                    token => $token);
5190                                    
5191                  ## NOTE: As if </caption>.                  ## NOTE: As if </caption>.
5192                  ## have a table element in table scope                  ## have a table element in table scope
# Line 4786  sub _tree_construction_main ($) { Line 5206  sub _tree_construction_main ($) {
5206    
5207                    !!!cp ('t157');                    !!!cp ('t157');
5208                    !!!parse-error (type => 'start tag not allowed',                    !!!parse-error (type => 'start tag not allowed',
5209                                    value => $token->{tag_name}, token => $token);                                    text => $token->{tag_name}, token => $token);
5210                    ## Ignore the token                    ## Ignore the token
5211                    !!!nack ('t157.1');                    !!!nack ('t157.1');
5212                    !!!next-token;                    !!!next-token;
# Line 4803  sub _tree_construction_main ($) { Line 5223  sub _tree_construction_main ($) {
5223                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5224                    !!!cp ('t159');                    !!!cp ('t159');
5225                    !!!parse-error (type => 'not closed',                    !!!parse-error (type => 'not closed',
5226                                    value => $self->{open_elements}->[-1]->[0]                                    text => $self->{open_elements}->[-1]->[0]
5227                                        ->manakai_local_name,                                        ->manakai_local_name,
5228                                    token => $token);                                    token => $token);
5229                  } else {                  } else {
# Line 4845  sub _tree_construction_main ($) { Line 5265  sub _tree_construction_main ($) {
5265                  } # INSCOPE                  } # INSCOPE
5266                    unless (defined $i) {                    unless (defined $i) {
5267                      !!!cp ('t165');                      !!!cp ('t165');
5268                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag',
5269                                        text => $token->{tag_name},
5270                                        token => $token);
5271                      ## Ignore the token                      ## Ignore the token
5272                      !!!next-token;                      !!!next-token;
5273                      next B;                      next B;
# Line 4862  sub _tree_construction_main ($) { Line 5284  sub _tree_construction_main ($) {
5284                          ne $token->{tag_name}) {                          ne $token->{tag_name}) {
5285                    !!!cp ('t167');                    !!!cp ('t167');
5286                    !!!parse-error (type => 'not closed',                    !!!parse-error (type => 'not closed',
5287                                    value => $self->{open_elements}->[-1]->[0]                                    text => $self->{open_elements}->[-1]->[0]
5288                                        ->manakai_local_name,                                        ->manakai_local_name,
5289                                    token => $token);                                    token => $token);
5290                  } else {                  } else {
# Line 4879  sub _tree_construction_main ($) { Line 5301  sub _tree_construction_main ($) {
5301                  next B;                  next B;
5302                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5303                  !!!cp ('t169');                  !!!cp ('t169');
5304                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5305                                    text => $token->{tag_name}, token => $token);
5306                  ## Ignore the token                  ## Ignore the token
5307                  !!!next-token;                  !!!next-token;
5308                  next B;                  next B;
# Line 4906  sub _tree_construction_main ($) { Line 5329  sub _tree_construction_main ($) {
5329    
5330                    !!!cp ('t173');                    !!!cp ('t173');
5331                    !!!parse-error (type => 'unmatched end tag',                    !!!parse-error (type => 'unmatched end tag',
5332                                    value => $token->{tag_name}, token => $token);                                    text => $token->{tag_name}, token => $token);
5333                    ## Ignore the token                    ## Ignore the token
5334                    !!!next-token;                    !!!next-token;
5335                    next B;                    next B;
# Line 4922  sub _tree_construction_main ($) { Line 5345  sub _tree_construction_main ($) {
5345                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5346                    !!!cp ('t175');                    !!!cp ('t175');
5347                    !!!parse-error (type => 'not closed',                    !!!parse-error (type => 'not closed',
5348                                    value => $self->{open_elements}->[-1]->[0]                                    text => $self->{open_elements}->[-1]->[0]
5349                                        ->manakai_local_name,                                        ->manakai_local_name,
5350                                    token => $token);                                    token => $token);
5351                  } else {                  } else {
# Line 4939  sub _tree_construction_main ($) { Line 5362  sub _tree_construction_main ($) {
5362                  next B;                  next B;
5363                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5364                  !!!cp ('t177');                  !!!cp ('t177');
5365                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5366                                    text => $token->{tag_name}, token => $token);
5367                  ## Ignore the token                  ## Ignore the token
5368                  !!!next-token;                  !!!next-token;
5369                  next B;                  next B;
# Line 4982  sub _tree_construction_main ($) { Line 5406  sub _tree_construction_main ($) {
5406    
5407                  !!!cp ('t182');                  !!!cp ('t182');
5408                  !!!parse-error (type => 'unmatched end tag',                  !!!parse-error (type => 'unmatched end tag',
5409                      value => $token->{tag_name}, token => $token);                      text => $token->{tag_name}, token => $token);
5410                  ## Ignore the token                  ## Ignore the token
5411                  !!!next-token;                  !!!next-token;
5412                  next B;                  next B;
5413                } # INSCOPE                } # INSCOPE
5414              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5415                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5416                !!!parse-error (type => 'not closed:caption', token => $token);                !!!parse-error (type => 'not closed', text => 'caption',
5417                                  token => $token);
5418    
5419                ## As if </caption>                ## As if </caption>
5420                ## have a table element in table scope                ## have a table element in table scope
# Line 5007  sub _tree_construction_main ($) { Line 5432  sub _tree_construction_main ($) {
5432                } # INSCOPE                } # INSCOPE
5433                unless (defined $i) {                unless (defined $i) {
5434                  !!!cp ('t186');                  !!!cp ('t186');
5435                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);                  !!!parse-error (type => 'unmatched end tag',
5436                                    text => 'caption', token => $token);
5437                  ## Ignore the token                  ## Ignore the token
5438                  !!!next-token;                  !!!next-token;
5439                  next B;                  next B;
# Line 5022  sub _tree_construction_main ($) { Line 5448  sub _tree_construction_main ($) {
5448                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5449                  !!!cp ('t188');                  !!!cp ('t188');
5450                  !!!parse-error (type => 'not closed',                  !!!parse-error (type => 'not closed',
5451                                  value => $self->{open_elements}->[-1]->[0]                                  text => $self->{open_elements}->[-1]->[0]
5452                                      ->manakai_local_name,                                      ->manakai_local_name,
5453                                  token => $token);                                  token => $token);
5454                } else {                } else {
# Line 5042  sub _tree_construction_main ($) { Line 5468  sub _tree_construction_main ($) {
5468                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5469                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5470                  !!!cp ('t190');                  !!!cp ('t190');
5471                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5472                                    text => $token->{tag_name}, token => $token);
5473                  ## Ignore the token                  ## Ignore the token
5474                  !!!next-token;                  !!!next-token;
5475                  next B;                  next B;
# Line 5056  sub _tree_construction_main ($) { Line 5483  sub _tree_construction_main ($) {
5483                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5484                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5485                !!!cp ('t192');                !!!cp ('t192');
5486                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
5487                                  text => $token->{tag_name}, token => $token);
5488                ## Ignore the token                ## Ignore the token
5489                !!!next-token;                !!!next-token;
5490                next B;                next B;
# Line 5084  sub _tree_construction_main ($) { Line 5512  sub _tree_construction_main ($) {
5512      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
5513        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5514          if (not $open_tables->[-1]->[1] and # tainted          if (not $open_tables->[-1]->[1] and # tainted
5515              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              $token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
5516            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5517                                
5518            unless (length $token->{data}) {            unless (length $token->{data}) {
# Line 5096  sub _tree_construction_main ($) { Line 5524  sub _tree_construction_main ($) {
5524            }            }
5525          }          }
5526    
5527              !!!parse-error (type => 'in table:#character', token => $token);          !!!parse-error (type => 'in table:#text', token => $token);
5528    
5529              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5530              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 5147  sub _tree_construction_main ($) { Line 5575  sub _tree_construction_main ($) {
5575          !!!next-token;          !!!next-token;
5576          next B;          next B;
5577        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5578              if ({          if ({
5579                   tr => ($self->{insertion_mode} != IN_ROW_IM),               tr => ($self->{insertion_mode} != IN_ROW_IM),
5580                   th => 1, td => 1,               th => 1, td => 1,
5581                  }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
5582                if ($self->{insertion_mode} == IN_TABLE_IM) {            if ($self->{insertion_mode} == IN_TABLE_IM) {
5583                  ## Clear back to table context              ## Clear back to table context
5584                  while (not ($self->{open_elements}->[-1]->[1]              while (not ($self->{open_elements}->[-1]->[1]
5585                                  & TABLE_SCOPING_EL)) {                              & TABLE_SCOPING_EL)) {
5586                    !!!cp ('t201');                !!!cp ('t201');
5587                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5588                  }              }
5589                                
5590                  !!!insert-element ('tbody',, $token);              !!!insert-element ('tbody',, $token);
5591                  $self->{insertion_mode} = IN_TABLE_BODY_IM;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5592                  ## reprocess in the "in table body" insertion mode...              ## reprocess in the "in table body" insertion mode...
5593                }            }
5594              
5595                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5596                  unless ($token->{tag_name} eq 'tr') {              unless ($token->{tag_name} eq 'tr') {
5597                    !!!cp ('t202');                !!!cp ('t202');
5598                    !!!parse-error (type => 'missing start tag:tr', token => $token);                !!!parse-error (type => 'missing start tag:tr', token => $token);
5599                  }              }
5600                                    
5601                  ## Clear back to table body context              ## Clear back to table body context
5602                  while (not ($self->{open_elements}->[-1]->[1]              while (not ($self->{open_elements}->[-1]->[1]
5603                                  & TABLE_ROWS_SCOPING_EL)) {                              & TABLE_ROWS_SCOPING_EL)) {
5604                    !!!cp ('t203');                !!!cp ('t203');
5605                    ## ISSUE: Can this case be reached?                ## ISSUE: Can this case be reached?
5606                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5607                  }              }
5608                                    
5609                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5610                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
# Line 5232  sub _tree_construction_main ($) { Line 5660  sub _tree_construction_main ($) {
5660                  unless (defined $i) {                  unless (defined $i) {
5661                    !!!cp ('t210');                    !!!cp ('t210');
5662  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5663                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmacthed end tag',
5664                                      text => $token->{tag_name}, token => $token);
5665                    ## Ignore the token                    ## Ignore the token
5666                    !!!nack ('t210.1');                    !!!nack ('t210.1');
5667                    !!!next-token;                    !!!next-token;
# Line 5276  sub _tree_construction_main ($) { Line 5705  sub _tree_construction_main ($) {
5705                  } # INSCOPE                  } # INSCOPE
5706                  unless (defined $i) {                  unless (defined $i) {
5707                    !!!cp ('t216');                    !!!cp ('t216');
5708  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type is wrong.
5709                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5710                                      text => $token->{tag_name}, token => $token);
5711                    ## Ignore the token                    ## Ignore the token
5712                    !!!nack ('t216.1');                    !!!nack ('t216.1');
5713                    !!!next-token;                    !!!next-token;
# Line 5352  sub _tree_construction_main ($) { Line 5782  sub _tree_construction_main ($) {
5782                }                }
5783              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5784                !!!parse-error (type => 'not closed',                !!!parse-error (type => 'not closed',
5785                                value => $self->{open_elements}->[-1]->[0]                                text => $self->{open_elements}->[-1]->[0]
5786                                    ->manakai_local_name,                                    ->manakai_local_name,
5787                                token => $token);                                token => $token);
5788    
# Line 5373  sub _tree_construction_main ($) { Line 5803  sub _tree_construction_main ($) {
5803                unless (defined $i) {                unless (defined $i) {
5804                  !!!cp ('t223');                  !!!cp ('t223');
5805  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5806                  !!!parse-error (type => 'unmatched end tag:table', token => $token);                  !!!parse-error (type => 'unmatched end tag', text => 'table',
5807                                    token => $token);
5808                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5809                  !!!nack ('t223.1');                  !!!nack ('t223.1');
5810                  !!!next-token;                  !!!next-token;
5811                  next B;                  next B;
5812                }                }
5813                                
5814  ## TODO: Followings are removed from the latest spec.  ## TODO: Followings are removed from the latest spec.
5815                ## generate implied end tags                ## generate implied end tags
5816                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5817                  !!!cp ('t224');                  !!!cp ('t224');
# Line 5391  sub _tree_construction_main ($) { Line 5822  sub _tree_construction_main ($) {
5822                  !!!cp ('t225');                  !!!cp ('t225');
5823                  ## NOTE: |<table><tr><table>|                  ## NOTE: |<table><tr><table>|
5824                  !!!parse-error (type => 'not closed',                  !!!parse-error (type => 'not closed',
5825                                  value => $self->{open_elements}->[-1]->[0]                                  text => $self->{open_elements}->[-1]->[0]
5826                                      ->manakai_local_name,                                      ->manakai_local_name,
5827                                  token => $token);                                  token => $token);
5828                } else {                } else {
# Line 5432  sub _tree_construction_main ($) { Line 5863  sub _tree_construction_main ($) {
5863                my $type = lc $token->{attributes}->{type}->{value};                my $type = lc $token->{attributes}->{type}->{value};
5864                if ($type eq 'hidden') {                if ($type eq 'hidden') {
5865                  !!!cp ('t227.3');                  !!!cp ('t227.3');
5866                  !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'in table',
5867                                    text => $token->{tag_name}, token => $token);
5868    
5869                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5870    
# Line 5460  sub _tree_construction_main ($) { Line 5892  sub _tree_construction_main ($) {
5892            #            #
5893          }          }
5894    
5895          !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'in table', text => $token->{tag_name},
5896                            token => $token);
5897    
5898          $insert = $insert_to_foster;          $insert = $insert_to_foster;
5899          #          #
# Line 5482  sub _tree_construction_main ($) { Line 5915  sub _tree_construction_main ($) {
5915                } # INSCOPE                } # INSCOPE
5916                unless (defined $i) {                unless (defined $i) {
5917                  !!!cp ('t230');                  !!!cp ('t230');
5918                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5919                                    text => $token->{tag_name}, token => $token);
5920                  ## Ignore the token                  ## Ignore the token
5921                  !!!nack ('t230.1');                  !!!nack ('t230.1');
5922                  !!!next-token;                  !!!next-token;
# Line 5523  sub _tree_construction_main ($) { Line 5957  sub _tree_construction_main ($) {
5957                  unless (defined $i) {                  unless (defined $i) {
5958                    !!!cp ('t235');                    !!!cp ('t235');
5959  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5960                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5961                                      text => $token->{type}, token => $token);
5962                    ## Ignore the token                    ## Ignore the token
5963                    !!!nack ('t236.1');                    !!!nack ('t236.1');
5964                    !!!next-token;                    !!!next-token;
# Line 5559  sub _tree_construction_main ($) { Line 5994  sub _tree_construction_main ($) {
5994                  } # INSCOPE                  } # INSCOPE
5995                  unless (defined $i) {                  unless (defined $i) {
5996                    !!!cp ('t239');                    !!!cp ('t239');
5997                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5998                                      text => $token->{tag_name}, token => $token);
5999                    ## Ignore the token                    ## Ignore the token
6000                    !!!nack ('t239.1');                    !!!nack ('t239.1');
6001                    !!!next-token;                    !!!next-token;
# Line 5605  sub _tree_construction_main ($) { Line 6041  sub _tree_construction_main ($) {
6041                } # INSCOPE                } # INSCOPE
6042                unless (defined $i) {                unless (defined $i) {
6043                  !!!cp ('t243');                  !!!cp ('t243');
6044                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
6045                                    text => $token->{tag_name}, token => $token);
6046                  ## Ignore the token                  ## Ignore the token
6047                  !!!nack ('t243.1');                  !!!nack ('t243.1');
6048                  !!!next-token;                  !!!next-token;
# Line 5639  sub _tree_construction_main ($) { Line 6076  sub _tree_construction_main ($) {
6076                  } # INSCOPE                  } # INSCOPE
6077                    unless (defined $i) {                    unless (defined $i) {
6078                      !!!cp ('t249');                      !!!cp ('t249');
6079                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag',
6080                                        text => $token->{tag_name}, token => $token);
6081                      ## Ignore the token                      ## Ignore the token
6082                      !!!nack ('t249.1');                      !!!nack ('t249.1');
6083                      !!!next-token;                      !!!next-token;
# Line 5662  sub _tree_construction_main ($) { Line 6100  sub _tree_construction_main ($) {
6100                  } # INSCOPE                  } # INSCOPE
6101                    unless (defined $i) {                    unless (defined $i) {
6102                      !!!cp ('t252');                      !!!cp ('t252');
6103                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);                      !!!parse-error (type => 'unmatched end tag',
6104                                        text => 'tr', token => $token);
6105                      ## Ignore the token                      ## Ignore the token
6106                      !!!nack ('t252.1');                      !!!nack ('t252.1');
6107                      !!!next-token;                      !!!next-token;
# Line 5697  sub _tree_construction_main ($) { Line 6136  sub _tree_construction_main ($) {
6136                } # INSCOPE                } # INSCOPE
6137                unless (defined $i) {                unless (defined $i) {
6138                  !!!cp ('t256');                  !!!cp ('t256');
6139                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
6140                                    text => $token->{tag_name}, token => $token);
6141                  ## Ignore the token                  ## Ignore the token
6142                  !!!nack ('t256.1');                  !!!nack ('t256.1');
6143                  !!!next-token;                  !!!next-token;
# Line 5724  sub _tree_construction_main ($) { Line 6164  sub _tree_construction_main ($) {
6164                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
6165                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
6166            !!!cp ('t258');            !!!cp ('t258');
6167            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
6168                              text => $token->{tag_name}, token => $token);
6169            ## Ignore the token            ## Ignore the token
6170            !!!nack ('t258.1');            !!!nack ('t258.1');
6171             !!!next-token;             !!!next-token;
6172            next B;            next B;
6173          } else {          } else {
6174            !!!cp ('t259');            !!!cp ('t259');
6175            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in table:/',
6176                              text => $token->{tag_name}, token => $token);
6177    
6178            $insert = $insert_to_foster;            $insert = $insert_to_foster;
6179            #            #
# Line 5754  sub _tree_construction_main ($) { Line 6196  sub _tree_construction_main ($) {
6196        }        }
6197      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6198            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6199              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
6200                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6201                unless (length $token->{data}) {                unless (length $token->{data}) {
6202                  !!!cp ('t260');                  !!!cp ('t260');
# Line 5781  sub _tree_construction_main ($) { Line 6223  sub _tree_construction_main ($) {
6223              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
6224                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6225                  !!!cp ('t264');                  !!!cp ('t264');
6226                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);                  !!!parse-error (type => 'unmatched end tag',
6227                                    text => 'colgroup', token => $token);
6228                  ## Ignore the token                  ## Ignore the token
6229                  !!!next-token;                  !!!next-token;
6230                  next B;                  next B;
# Line 5794  sub _tree_construction_main ($) { Line 6237  sub _tree_construction_main ($) {
6237                }                }
6238              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
6239                !!!cp ('t266');                !!!cp ('t266');
6240                !!!parse-error (type => 'unmatched end tag:col', token => $token);                !!!parse-error (type => 'unmatched end tag',
6241                                  text => 'col', token => $token);
6242                ## Ignore the token                ## Ignore the token
6243                !!!next-token;                !!!next-token;
6244                next B;                next B;
# Line 5824  sub _tree_construction_main ($) { Line 6268  sub _tree_construction_main ($) {
6268            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6269              !!!cp ('t269');              !!!cp ('t269');
6270  ## TODO: Wrong error type?  ## TODO: Wrong error type?
6271              !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);              !!!parse-error (type => 'unmatched end tag',
6272                                text => 'colgroup', token => $token);
6273              ## Ignore the token              ## Ignore the token
6274              !!!nack ('t269.1');              !!!nack ('t269.1');
6275              !!!next-token;              !!!next-token;
# Line 5878  sub _tree_construction_main ($) { Line 6323  sub _tree_construction_main ($) {
6323            !!!nack ('t277.1');            !!!nack ('t277.1');
6324            !!!next-token;            !!!next-token;
6325            next B;            next B;
6326          } elsif ($token->{tag_name} eq 'select' or          } elsif ({
6327                   $token->{tag_name} eq 'input' or                     select => 1, input => 1, textarea => 1,
6328                     }->{$token->{tag_name}} or
6329                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6330                    {                    {
6331                     caption => 1, table => 1,                     caption => 1, table => 1,
# Line 5887  sub _tree_construction_main ($) { Line 6333  sub _tree_construction_main ($) {
6333                     tr => 1, td => 1, th => 1,                     tr => 1, td => 1, th => 1,
6334                    }->{$token->{tag_name}})) {                    }->{$token->{tag_name}})) {
6335            ## TODO: The type below is not good - <select> is replaced by </select>            ## TODO: The type below is not good - <select> is replaced by </select>
6336            !!!parse-error (type => 'not closed:select', token => $token);            !!!parse-error (type => 'not closed', text => 'select',
6337                              token => $token);
6338            ## NOTE: As if the token were </select> (<select> case) or            ## NOTE: As if the token were </select> (<select> case) or
6339            ## as if there were </select> (otherwise).            ## as if there were </select> (otherwise).
6340            ## have an element in table scope            ## have an element in table scope
# Line 5905  sub _tree_construction_main ($) { Line 6352  sub _tree_construction_main ($) {
6352            } # INSCOPE            } # INSCOPE
6353            unless (defined $i) {            unless (defined $i) {
6354              !!!cp ('t280');              !!!cp ('t280');
6355              !!!parse-error (type => 'unmatched end tag:select', token => $token);              !!!parse-error (type => 'unmatched end tag',
6356                                text => 'select', token => $token);
6357              ## Ignore the token              ## Ignore the token
6358              !!!nack ('t280.1');              !!!nack ('t280.1');
6359              !!!next-token;              !!!next-token;
# Line 5929  sub _tree_construction_main ($) { Line 6377  sub _tree_construction_main ($) {
6377            }            }
6378          } else {          } else {
6379            !!!cp ('t282');            !!!cp ('t282');
6380            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select',
6381                              text => $token->{tag_name}, token => $token);
6382            ## Ignore the token            ## Ignore the token
6383            !!!nack ('t282.1');            !!!nack ('t282.1');
6384            !!!next-token;            !!!next-token;
# Line 5947  sub _tree_construction_main ($) { Line 6396  sub _tree_construction_main ($) {
6396              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6397            } else {            } else {
6398              !!!cp ('t285');              !!!cp ('t285');
6399              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6400                                text => $token->{tag_name}, token => $token);
6401              ## Ignore the token              ## Ignore the token
6402            }            }
6403            !!!nack ('t285.1');            !!!nack ('t285.1');
# Line 5959  sub _tree_construction_main ($) { Line 6409  sub _tree_construction_main ($) {
6409              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6410            } else {            } else {
6411              !!!cp ('t287');              !!!cp ('t287');
6412              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6413                                text => $token->{tag_name}, token => $token);
6414              ## Ignore the token              ## Ignore the token
6415            }            }
6416            !!!nack ('t287.1');            !!!nack ('t287.1');
# Line 5981  sub _tree_construction_main ($) { Line 6432  sub _tree_construction_main ($) {
6432            } # INSCOPE            } # INSCOPE
6433            unless (defined $i) {            unless (defined $i) {
6434              !!!cp ('t290');              !!!cp ('t290');
6435              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6436                                text => $token->{tag_name}, token => $token);
6437              ## Ignore the token              ## Ignore the token
6438              !!!nack ('t290.1');              !!!nack ('t290.1');
6439              !!!next-token;              !!!next-token;
# Line 6002  sub _tree_construction_main ($) { Line 6454  sub _tree_construction_main ($) {
6454                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6455                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
6456  ## TODO: The following is wrong?  ## TODO: The following is wrong?
6457            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
6458                              text => $token->{tag_name}, token => $token);
6459                                
6460            ## have an element in table scope            ## have an element in table scope
6461            my $i;            my $i;
# Line 6043  sub _tree_construction_main ($) { Line 6496  sub _tree_construction_main ($) {
6496            unless (defined $i) {            unless (defined $i) {
6497              !!!cp ('t297');              !!!cp ('t297');
6498  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
6499              !!!parse-error (type => 'unmatched end tag:select', token => $token);              !!!parse-error (type => 'unmatched end tag',
6500                                text => 'select', token => $token);
6501              ## Ignore the </select> token              ## Ignore the </select> token
6502              !!!nack ('t297.1');              !!!nack ('t297.1');
6503              !!!next-token; ## TODO: ok?              !!!next-token; ## TODO: ok?
# Line 6060  sub _tree_construction_main ($) { Line 6514  sub _tree_construction_main ($) {
6514            next B;            next B;
6515          } else {          } else {
6516            !!!cp ('t299');            !!!cp ('t299');
6517            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select:/',
6518                              text => $token->{tag_name}, token => $token);
6519            ## Ignore the token            ## Ignore the token
6520            !!!nack ('t299.3');            !!!nack ('t299.3');
6521            !!!next-token;            !!!next-token;
# Line 6082  sub _tree_construction_main ($) { Line 6537  sub _tree_construction_main ($) {
6537        }        }
6538      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
6539        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
6540          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
6541            my $data = $1;            my $data = $1;
6542            ## As if in body            ## As if in body
6543            $reconstruct_active_formatting_elements->($insert_to_current);            $reconstruct_active_formatting_elements->($insert_to_current);
# Line 6098  sub _tree_construction_main ($) { Line 6553  sub _tree_construction_main ($) {
6553                    
6554          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6555            !!!cp ('t301');            !!!cp ('t301');
6556            !!!parse-error (type => 'after html:#character', token => $token);            !!!parse-error (type => 'after html:#text', token => $token);
6557              #
           ## Reprocess in the "after body" insertion mode.  
6558          } else {          } else {
6559            !!!cp ('t302');            !!!cp ('t302');
6560              ## "after body" insertion mode
6561              !!!parse-error (type => 'after body:#text', token => $token);
6562              #
6563          }          }
           
         ## "after body" insertion mode  
         !!!parse-error (type => 'after body:#character', token => $token);  
6564    
6565          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6566          ## reprocess          ## reprocess
# Line 6114  sub _tree_construction_main ($) { Line 6568  sub _tree_construction_main ($) {
6568        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6569          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6570            !!!cp ('t303');            !!!cp ('t303');
6571            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after html',
6572                                        text => $token->{tag_name}, token => $token);
6573            ## Reprocess in the "after body" insertion mode.            #
6574          } else {          } else {
6575            !!!cp ('t304');            !!!cp ('t304');
6576              ## "after body" insertion mode
6577              !!!parse-error (type => 'after body',
6578                              text => $token->{tag_name}, token => $token);
6579              #
6580          }          }
6581    
         ## "after body" insertion mode  
         !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);  
   
6582          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6583          !!!ack-later;          !!!ack-later;
6584          ## reprocess          ## reprocess
# Line 6131  sub _tree_construction_main ($) { Line 6586  sub _tree_construction_main ($) {
6586        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6587          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6588            !!!cp ('t305');            !!!cp ('t305');
6589            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after html:/',
6590                              text => $token->{tag_name}, token => $token);
6591                        
6592            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6593            ## Reprocess in the "after body" insertion mode.            ## Reprocess.
6594              next B;
6595          } else {          } else {
6596            !!!cp ('t306');            !!!cp ('t306');
6597          }          }
# Line 6143  sub _tree_construction_main ($) { Line 6600  sub _tree_construction_main ($) {
6600          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6601            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6602              !!!cp ('t307');              !!!cp ('t307');
6603              !!!parse-error (type => 'unmatched end tag:html', token => $token);              !!!parse-error (type => 'unmatched end tag',
6604                                text => 'html', token => $token);
6605              ## Ignore the token              ## Ignore the token
6606              !!!next-token;              !!!next-token;
6607              next B;              next B;
# Line 6155  sub _tree_construction_main ($) { Line 6613  sub _tree_construction_main ($) {
6613            }            }
6614          } else {          } else {
6615            !!!cp ('t309');            !!!cp ('t309');
6616            !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after body:/',
6617                              text => $token->{tag_name}, token => $token);
6618    
6619            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6620            ## reprocess            ## reprocess
# Line 6170  sub _tree_construction_main ($) { Line 6629  sub _tree_construction_main ($) {
6629        }        }
6630      } elsif ($self->{insertion_mode} & FRAME_IMS) {      } elsif ($self->{insertion_mode} & FRAME_IMS) {
6631        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
6632          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
6633            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6634                        
6635            unless (length $token->{data}) {            unless (length $token->{data}) {
# Line 6180  sub _tree_construction_main ($) { Line 6639  sub _tree_construction_main ($) {
6639            }            }
6640          }          }
6641                    
6642          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0C\x20]+//) {
6643            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6644              !!!cp ('t311');              !!!cp ('t311');
6645              !!!parse-error (type => 'in frameset:#character', token => $token);              !!!parse-error (type => 'in frameset:#text', token => $token);
6646            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6647              !!!cp ('t312');              !!!cp ('t312');
6648              !!!parse-error (type => 'after frameset:#character', token => $token);              !!!parse-error (type => 'after frameset:#text', token => $token);
6649            } else { # "after html frameset"            } else { # "after after frameset"
6650              !!!cp ('t313');              !!!cp ('t313');
6651              !!!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);  
6652            }            }
6653                        
6654            ## Ignore the token.            ## Ignore the token.
# Line 6209  sub _tree_construction_main ($) { Line 6664  sub _tree_construction_main ($) {
6664                    
6665          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6666        } 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');  
         }  
   
6667          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6668              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6669            !!!cp ('t318');            !!!cp ('t318');
# Line 6236  sub _tree_construction_main ($) { Line 6681  sub _tree_construction_main ($) {
6681            next B;            next B;
6682          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6683            !!!cp ('t320');            !!!cp ('t320');
6684            ## NOTE: As if in body.            ## NOTE: As if in head.
6685            $parse_rcdata->(CDATA_CONTENT_MODEL);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6686            next B;            next B;
6687    
6688              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6689              ## has no parse error.
6690          } else {          } else {
6691            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6692              !!!cp ('t321');              !!!cp ('t321');
6693              !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'in frameset',
6694            } else {                              text => $token->{tag_name}, token => $token);
6695              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6696              !!!cp ('t322');              !!!cp ('t322');
6697              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'after frameset',
6698                                text => $token->{tag_name}, token => $token);
6699              } else { # "after after frameset"
6700                !!!cp ('t322.2');
6701                !!!parse-error (type => 'after after frameset',
6702                                text => $token->{tag_name}, token => $token);
6703            }            }
6704            ## Ignore the token            ## Ignore the token
6705            !!!nack ('t322.1');            !!!nack ('t322.1');
# Line 6253  sub _tree_construction_main ($) { Line 6707  sub _tree_construction_main ($) {
6707            next B;            next B;
6708          }          }
6709        } 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');  
         }  
   
6710          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6711              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6712            if ($self->{open_elements}->[-1]->[1] & HTML_EL and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6713                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6714              !!!cp ('t325');              !!!cp ('t325');
6715              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6716                                text => $token->{tag_name}, token => $token);
6717              ## Ignore the token              ## Ignore the token
6718              !!!next-token;              !!!next-token;
6719            } else {            } else {
# Line 6294  sub _tree_construction_main ($) { Line 6739  sub _tree_construction_main ($) {
6739          } else {          } else {
6740            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6741              !!!cp ('t330');              !!!cp ('t330');
6742              !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'in frameset:/',
6743            } else {                              text => $token->{tag_name}, token => $token);
6744              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6745                !!!cp ('t330.1');
6746                !!!parse-error (type => 'after frameset:/',
6747                                text => $token->{tag_name}, token => $token);
6748              } else { # "after after html"
6749              !!!cp ('t331');              !!!cp ('t331');
6750              !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'after after frameset:/',
6751                                text => $token->{tag_name}, token => $token);
6752            }            }
6753            ## Ignore the token            ## Ignore the token
6754            !!!next-token;            !!!next-token;
# Line 6364  sub _tree_construction_main ($) { Line 6815  sub _tree_construction_main ($) {
6815                                           ->{has_reference});                                           ->{has_reference});
6816            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
6817              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6818                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6819                      [\x09-\x0D\x20]*=                      [\x09\x0A\x0C\x0D\x20]*=
6820                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09\x0A\x0C\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6821                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09\x0A\x0C\x0D\x20][^\x09\x0A\x0C\x0D\x20\x3B]*))
6822                       /x) {
6823                !!!cp ('t336');                !!!cp ('t336');
6824                ## NOTE: Whether the encoding is supported or not is handled                ## NOTE: Whether the encoding is supported or not is handled
6825                ## in the {change_encoding} callback.                ## in the {change_encoding} callback.
# Line 6405  sub _tree_construction_main ($) { Line 6857  sub _tree_construction_main ($) {
6857          $parse_rcdata->(RCDATA_CONTENT_MODEL);          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6858          next B;          next B;
6859        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6860          !!!parse-error (type => 'in body:body', token => $token);          !!!parse-error (type => 'in body', text => 'body', token => $token);
6861                                
6862          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6863              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
# Line 6525  sub _tree_construction_main ($) { Line 6977  sub _tree_construction_main ($) {
6977              if ($i != -1) {              if ($i != -1) {
6978                !!!cp ('t355');                !!!cp ('t355');
6979                !!!parse-error (type => 'not closed',                !!!parse-error (type => 'not closed',
6980                                value => $self->{open_elements}->[-1]->[0]                                text => $self->{open_elements}->[-1]->[0]
6981                                    ->manakai_local_name,                                    ->manakai_local_name,
6982                                token => $token);                                token => $token);
6983              } else {              } else {
# Line 6679  sub _tree_construction_main ($) { Line 7131  sub _tree_construction_main ($) {
7131                  xmp => 1,                  xmp => 1,
7132                  iframe => 1,                  iframe => 1,
7133                  noembed => 1,                  noembed => 1,
7134                  noframes => 1,                  noframes => 1, ## NOTE: This is an "as if in head" code clone.
7135                  noscript => 0, ## TODO: 1 if scripting is enabled                  noscript => 0, ## TODO: 1 if scripting is enabled
7136                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7137          if ($token->{tag_name} eq 'xmp') {          if ($token->{tag_name} eq 'xmp') {
# Line 6701  sub _tree_construction_main ($) { Line 7153  sub _tree_construction_main ($) {
7153            !!!next-token;            !!!next-token;
7154            next B;            next B;
7155          } else {          } else {
7156              !!!ack ('t391.1');
7157    
7158            my $at = $token->{attributes};            my $at = $token->{attributes};
7159            my $form_attrs;            my $form_attrs;
7160            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 6744  sub _tree_construction_main ($) { Line 7198  sub _tree_construction_main ($) {
7198                           line => $token->{line}, column => $token->{column}},                           line => $token->{line}, column => $token->{column}},
7199                          {type => END_TAG_TOKEN, tag_name => 'form',                          {type => END_TAG_TOKEN, tag_name => 'form',
7200                           line => $token->{line}, column => $token->{column}};                           line => $token->{line}, column => $token->{column}};
           !!!nack ('t391.1'); ## NOTE: Not acknowledged.  
7201            !!!back-token (@tokens);            !!!back-token (@tokens);
7202            !!!next-token;            !!!next-token;
7203            next B;            next B;
# Line 6792  sub _tree_construction_main ($) { Line 7245  sub _tree_construction_main ($) {
7245            ## Ignore the token            ## Ignore the token
7246          } else {          } else {
7247            !!!cp ('t398');            !!!cp ('t398');
7248            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
7249          }          }
7250          !!!next-token;          !!!next-token;
7251          next B;          next B;
7252          } elsif ($token->{tag_name} eq 'rt' or
7253                   $token->{tag_name} eq 'rp') {
7254            ## has a |ruby| element in scope
7255            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7256              my $node = $self->{open_elements}->[$_];
7257              if ($node->[1] & RUBY_EL) {
7258                !!!cp ('t398.1');
7259                ## generate implied end tags
7260                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7261                  !!!cp ('t398.2');
7262                  pop @{$self->{open_elements}};
7263                }
7264                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
7265                  !!!cp ('t398.3');
7266                  !!!parse-error (type => 'not closed',
7267                                  text => $self->{open_elements}->[-1]->[0]
7268                                      ->manakai_local_name,
7269                                  token => $token);
7270                  pop @{$self->{open_elements}}
7271                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
7272                }
7273                last INSCOPE;
7274              } elsif ($node->[1] & SCOPING_EL) {
7275                !!!cp ('t398.4');
7276                last INSCOPE;
7277              }
7278            } # INSCOPE
7279    
7280            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7281    
7282            !!!nack ('t398.5');
7283            !!!next-token;
7284            redo B;
7285        } elsif ($token->{tag_name} eq 'math' or        } elsif ($token->{tag_name} eq 'math' or
7286                 $token->{tag_name} eq 'svg') {                 $token->{tag_name} eq 'svg') {
7287          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7288    
7289            ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
7290    
7291          ## "adjust SVG attributes" ('svg' only) - done in insert-element-f          ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
7292    
7293          ## "adjust foreign attributes" - done in insert-element-f          ## "adjust foreign attributes" - done in insert-element-f
# Line 6826  sub _tree_construction_main ($) { Line 7314  sub _tree_construction_main ($) {
7314                  thead => 1, tr => 1,                  thead => 1, tr => 1,
7315                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7316          !!!cp ('t401');          !!!cp ('t401');
7317          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'in body',
7318                            text => $token->{tag_name}, token => $token);
7319          ## Ignore the token          ## Ignore the token
7320          !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.          !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7321          !!!next-token;          !!!next-token;
# Line 6911  sub _tree_construction_main ($) { Line 7400  sub _tree_construction_main ($) {
7400            }            }
7401    
7402            !!!parse-error (type => 'start tag not allowed',            !!!parse-error (type => 'start tag not allowed',
7403                            value => $token->{tag_name}, token => $token);                            text => $token->{tag_name}, token => $token);
7404            ## NOTE: Ignore the token.            ## NOTE: Ignore the token.
7405            !!!next-token;            !!!next-token;
7406            next B;            next B;
# Line 6921  sub _tree_construction_main ($) { Line 7410  sub _tree_construction_main ($) {
7410            unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {            unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7411              !!!cp ('t403');              !!!cp ('t403');
7412              !!!parse-error (type => 'not closed',              !!!parse-error (type => 'not closed',
7413                              value => $_->[0]->manakai_local_name,                              text => $_->[0]->manakai_local_name,
7414                              token => $token);                              token => $token);
7415              last;              last;
7416            } else {            } else {
# Line 6941  sub _tree_construction_main ($) { Line 7430  sub _tree_construction_main ($) {
7430            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7431              !!!cp ('t406');              !!!cp ('t406');
7432              !!!parse-error (type => 'not closed',              !!!parse-error (type => 'not closed',
7433                              value => $self->{open_elements}->[1]->[0]                              text => $self->{open_elements}->[1]->[0]
7434                                  ->manakai_local_name,                                  ->manakai_local_name,
7435                              token => $token);                              token => $token);
7436            } else {            } else {
# Line 6952  sub _tree_construction_main ($) { Line 7441  sub _tree_construction_main ($) {
7441            next B;            next B;
7442          } else {          } else {
7443            !!!cp ('t408');            !!!cp ('t408');
7444            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7445                              text => $token->{tag_name}, token => $token);
7446            ## Ignore the token            ## Ignore the token
7447            !!!next-token;            !!!next-token;
7448            next B;            next B;
# Line 6980  sub _tree_construction_main ($) { Line 7470  sub _tree_construction_main ($) {
7470    
7471          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7472            !!!cp ('t413');            !!!cp ('t413');
7473            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7474                              text => $token->{tag_name}, token => $token);
7475              ## NOTE: Ignore the token.
7476          } else {          } else {
7477            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7478            while ({            while ({
7479                      ## END_TAG_OPTIONAL_EL
7480                    dd => ($token->{tag_name} ne 'dd'),                    dd => ($token->{tag_name} ne 'dd'),
7481                    dt => ($token->{tag_name} ne 'dt'),                    dt => ($token->{tag_name} ne 'dt'),
7482                    li => ($token->{tag_name} ne 'li'),                    li => ($token->{tag_name} ne 'li'),
7483                    p => 1,                    p => 1,
7484                      rt => 1,
7485                      rp => 1,
7486                   }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {                   }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7487              !!!cp ('t409');              !!!cp ('t409');
7488              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
# Line 6998  sub _tree_construction_main ($) { Line 7493  sub _tree_construction_main ($) {
7493                    ne $token->{tag_name}) {                    ne $token->{tag_name}) {
7494              !!!cp ('t412');              !!!cp ('t412');
7495              !!!parse-error (type => 'not closed',              !!!parse-error (type => 'not closed',
7496                              value => $self->{open_elements}->[-1]->[0]                              text => $self->{open_elements}->[-1]->[0]
7497                                  ->manakai_local_name,                                  ->manakai_local_name,
7498                              token => $token);                              token => $token);
7499            } else {            } else {
# Line 7035  sub _tree_construction_main ($) { Line 7530  sub _tree_construction_main ($) {
7530    
7531          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7532            !!!cp ('t421');            !!!cp ('t421');
7533            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7534                              text => $token->{tag_name}, token => $token);
7535              ## NOTE: Ignore the token.
7536          } else {          } else {
7537            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7538            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
# Line 7048  sub _tree_construction_main ($) { Line 7545  sub _tree_construction_main ($) {
7545                    ne $token->{tag_name}) {                    ne $token->{tag_name}) {
7546              !!!cp ('t417.1');              !!!cp ('t417.1');
7547              !!!parse-error (type => 'not closed',              !!!parse-error (type => 'not closed',
7548                              value => $self->{open_elements}->[-1]->[0]                              text => $self->{open_elements}->[-1]->[0]
7549                                  ->manakai_local_name,                                  ->manakai_local_name,
7550                              token => $token);                              token => $token);
7551            } else {            } else {
# Line 7080  sub _tree_construction_main ($) { Line 7577  sub _tree_construction_main ($) {
7577    
7578          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7579            !!!cp ('t425.1');            !!!cp ('t425.1');
7580            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7581                              text => $token->{tag_name}, token => $token);
7582              ## NOTE: Ignore the token.
7583          } else {          } else {
7584            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7585            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
# Line 7092  sub _tree_construction_main ($) { Line 7591  sub _tree_construction_main ($) {
7591            if ($self->{open_elements}->[-1]->[0]->manakai_local_name            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7592                    ne $token->{tag_name}) {                    ne $token->{tag_name}) {
7593              !!!cp ('t425');              !!!cp ('t425');
7594              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
7595                                text => $token->{tag_name}, token => $token);
7596            } else {            } else {
7597              !!!cp ('t426');              !!!cp ('t426');
7598            }            }
# Line 7123  sub _tree_construction_main ($) { Line 7623  sub _tree_construction_main ($) {
7623                    ne $token->{tag_name}) {                    ne $token->{tag_name}) {
7624              !!!cp ('t412.1');              !!!cp ('t412.1');
7625              !!!parse-error (type => 'not closed',              !!!parse-error (type => 'not closed',
7626                              value => $self->{open_elements}->[-1]->[0]                              text => $self->{open_elements}->[-1]->[0]
7627                                  ->manakai_local_name,                                  ->manakai_local_name,
7628                              token => $token);                              token => $token);
7629            } else {            } else {
# Line 7133  sub _tree_construction_main ($) { Line 7633  sub _tree_construction_main ($) {
7633            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7634          } else {          } else {
7635            !!!cp ('t413.1');            !!!cp ('t413.1');
7636            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7637                              text => $token->{tag_name}, token => $token);
7638    
7639            !!!cp ('t415.1');            !!!cp ('t415.1');
7640            ## As if <p>, then reprocess the current token            ## As if <p>, then reprocess the current token
# Line 7156  sub _tree_construction_main ($) { Line 7657  sub _tree_construction_main ($) {
7657          next B;          next B;
7658        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7659          !!!cp ('t428');          !!!cp ('t428');
7660          !!!parse-error (type => 'unmatched end tag:br', token => $token);          !!!parse-error (type => 'unmatched end tag',
7661                            text => 'br', token => $token);
7662    
7663          ## As if <br>          ## As if <br>
7664          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
# Line 7181  sub _tree_construction_main ($) { Line 7683  sub _tree_construction_main ($) {
7683                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7684                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7685          !!!cp ('t429');          !!!cp ('t429');
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          ## Ignore the token          ## Ignore the token
7689          !!!next-token;          !!!next-token;
7690          next B;          next B;
# Line 7200  sub _tree_construction_main ($) { Line 7703  sub _tree_construction_main ($) {
7703              ## generate implied end tags              ## generate implied end tags
7704              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7705                !!!cp ('t430');                !!!cp ('t430');
7706                ## ISSUE: Can this case be reached?                ## NOTE: |<ruby><rt></ruby>|.
7707                  ## ISSUE: <ruby><rt></rt> will also take this code path,
7708                  ## which seems wrong.
7709                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
7710                  $node_i++;
7711              }              }
7712                    
7713              ## Step 2              ## Step 2
# Line 7210  sub _tree_construction_main ($) { Line 7716  sub _tree_construction_main ($) {
7716                !!!cp ('t431');                !!!cp ('t431');
7717                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7718                !!!parse-error (type => 'not closed',                !!!parse-error (type => 'not closed',
7719                                value => $self->{open_elements}->[-1]->[0]                                text => $self->{open_elements}->[-1]->[0]
7720                                    ->manakai_local_name,                                    ->manakai_local_name,
7721                                token => $token);                                token => $token);
7722              } else {              } else {
# Line 7218  sub _tree_construction_main ($) { Line 7724  sub _tree_construction_main ($) {
7724              }              }
7725                            
7726              ## Step 3              ## Step 3
7727              splice @{$self->{open_elements}}, $node_i;              splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7728    
7729              !!!next-token;              !!!next-token;
7730              last S2;              last S2;
# Line 7229  sub _tree_construction_main ($) { Line 7735  sub _tree_construction_main ($) {
7735                  ($node->[1] & SPECIAL_EL or                  ($node->[1] & SPECIAL_EL or
7736                   $node->[1] & SCOPING_EL)) {                   $node->[1] & SCOPING_EL)) {
7737                !!!cp ('t433');                !!!cp ('t433');
7738                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
7739                                  text => $token->{tag_name}, token => $token);
7740                ## Ignore the token                ## Ignore the token
7741                !!!next-token;                !!!next-token;
7742                last S2;                last S2;
# Line 7275  sub _tree_construction_main ($) { Line 7782  sub _tree_construction_main ($) {
7782    ## TODO: script stuffs    ## TODO: script stuffs
7783  } # _tree_construct_main  } # _tree_construct_main
7784    
7785  sub set_inner_html ($$$) {  sub set_inner_html ($$$$;$) {
7786    my $class = shift;    my $class = shift;
7787    my $node = shift;    my $node = shift;
7788    my $s = \$_[0];    #my $s = \$_[0];
7789    my $onerror = $_[1];    my $onerror = $_[1];
7790      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7791    
7792    ## ISSUE: Should {confident} be true?    ## ISSUE: Should {confident} be true?
7793    
# Line 7298  sub set_inner_html ($$$) { Line 7806  sub set_inner_html ($$$) {
7806      }      }
7807    
7808      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7809      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($_[0] => $node, $onerror, $get_wrapper);
7810    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7811      ## TODO: If non-html element      ## TODO: If non-html element
7812    
7813      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7814    
7815    ## TODO: Support for $get_wrapper
7816    
7817      ## Step 1 # MUST      ## Step 1 # MUST
7818      my $this_doc = $node->owner_document;      my $this_doc = $node->owner_document;
7819      my $doc = $this_doc->implementation->create_document;      my $doc = $this_doc->implementation->create_document;
# Line 7315  sub set_inner_html ($$$) { Line 7825  sub set_inner_html ($$$) {
7825      my $i = 0;      my $i = 0;
7826      $p->{line_prev} = $p->{line} = 1;      $p->{line_prev} = $p->{line} = 1;
7827      $p->{column_prev} = $p->{column} = 0;      $p->{column_prev} = $p->{column} = 0;
7828      $p->{set_next_char} = sub {      require Whatpm::Charset::DecodeHandle;
7829        my $input = Whatpm::Charset::DecodeHandle::CharString->new (\($_[0]));
7830        $input = $get_wrapper->($input);
7831        $p->{set_nc} = sub {
7832        my $self = shift;        my $self = shift;
7833    
7834        pop @{$self->{prev_char}};        my $char = '';
7835        unshift @{$self->{prev_char}}, $self->{next_char};        if (defined $self->{next_nc}) {
7836            $char = $self->{next_nc};
7837        $self->{next_char} = -1 and return if $i >= length $$s;          delete $self->{next_nc};
7838        $self->{next_char} = ord substr $$s, $i++, 1;          $self->{nc} = ord $char;
7839          } else {
7840            $self->{char_buffer} = '';
7841            $self->{char_buffer_pos} = 0;
7842            
7843            my $count = $input->manakai_read_until
7844                ($self->{char_buffer}, qr/[^\x00\x0A\x0D]/,
7845                 $self->{char_buffer_pos});
7846            if ($count) {
7847              $self->{line_prev} = $self->{line};
7848              $self->{column_prev} = $self->{column};
7849              $self->{column}++;
7850              $self->{nc}
7851                  = ord substr ($self->{char_buffer},
7852                                $self->{char_buffer_pos}++, 1);
7853              return;
7854            }
7855            
7856            if ($input->read ($char, 1)) {
7857              $self->{nc} = ord $char;
7858            } else {
7859              $self->{nc} = -1;
7860              return;
7861            }
7862          }
7863    
7864        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7865        $p->{column}++;        $p->{column}++;
7866    
7867        if ($self->{next_char} == 0x000A) { # LF        if ($self->{nc} == 0x000A) { # LF
7868          $p->{line}++;          $p->{line}++;
7869          $p->{column} = 0;          $p->{column} = 0;
7870          !!!cp ('i1');          !!!cp ('i1');
7871        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{nc} == 0x000D) { # CR
7872          $i++ if substr ($$s, $i, 1) eq "\x0A";  ## TODO: support for abort/streaming
7873          $self->{next_char} = 0x000A; # LF # MUST          my $next = '';
7874            if ($input->read ($next, 1) and $next ne "\x0A") {
7875              $self->{next_nc} = $next;
7876            }
7877            $self->{nc} = 0x000A; # LF # MUST
7878          $p->{line}++;          $p->{line}++;
7879          $p->{column} = 0;          $p->{column} = 0;
7880          !!!cp ('i2');          !!!cp ('i2');
7881        } 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  
7882          !!!cp ('i4');          !!!cp ('i4');
7883          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7884          $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  
7885        }        }
7886      };      };
7887      $p->{prev_char} = [-1, -1, -1];  
7888      $p->{next_char} = -1;      $p->{read_until} = sub {
7889              #my ($scalar, $specials_range, $offset) = @_;
7890          return 0 if defined $p->{next_nc};
7891    
7892          my $pattern = qr/[^$_[1]\x00\x0A\x0D]/;
7893          my $offset = $_[2] || 0;
7894          
7895          if ($p->{char_buffer_pos} < length $p->{char_buffer}) {
7896            pos ($p->{char_buffer}) = $p->{char_buffer_pos};
7897            if ($p->{char_buffer} =~ /\G(?>$pattern)+/) {
7898              substr ($_[0], $offset)
7899                  = substr ($p->{char_buffer}, $-[0], $+[0] - $-[0]);
7900              my $count = $+[0] - $-[0];
7901              if ($count) {
7902                $p->{column} += $count;
7903                $p->{char_buffer_pos} += $count;
7904                $p->{line_prev} = $p->{line};
7905                $p->{column_prev} = $p->{column} - 1;
7906                $p->{nc} = -1;
7907              }
7908              return $count;
7909            } else {
7910              return 0;
7911            }
7912          } else {
7913            my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
7914            if ($count) {
7915              $p->{column} += $count;
7916              $p->{column_prev} += $count;
7917              $p->{nc} = -1;
7918            }
7919            return $count;
7920          }
7921        }; # $p->{read_until}
7922    
7923      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7924        my (%opt) = @_;        my (%opt) = @_;
7925        my $line = $opt{line};        my $line = $opt{line};
# Line 7386  sub set_inner_html ($$$) { Line 7934  sub set_inner_html ($$$) {
7934        $ponerror->(line => $p->{line}, column => $p->{column}, @_);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7935      };      };
7936            
7937        my $char_onerror = sub {
7938          my (undef, $type, %opt) = @_;
7939          $ponerror->(layer => 'encode',
7940                      line => $p->{line}, column => $p->{column} + 1,
7941                      %opt, type => $type);
7942        }; # $char_onerror
7943        $input->onerror ($char_onerror);
7944    
7945      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7946      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7947    

Legend:
Removed from v.1.141  
changed lines
  Added in v.1.189

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24