/[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.139 by wakaba, Sat May 24 04:26:27 2008 UTC revision 1.182 by wakaba, Mon Sep 15 07:19:03 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->{column} = 0;
655    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
656      my $self = shift;      my $self = shift;
657    
658      pop @{$self->{prev_char}};      my $char = '';
     unshift @{$self->{prev_char}}, $self->{next_char};  
   
     my $char;  
659      if (defined $self->{next_next_char}) {      if (defined $self->{next_next_char}) {
660        $char = $self->{next_next_char};        $char = $self->{next_next_char};
661        delete $self->{next_next_char};        delete $self->{next_next_char};
662          $self->{next_char} = ord $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->{next_char}
674                = ord substr ($self->{char_buffer}, $self->{char_buffer_pos}++, 1);
675            return;
676          }
677    
678          if ($input->read ($char, 1)) {
679            $self->{next_char} = ord $char;
680          } else {
681            $self->{next_char} = -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});
# Line 616  sub parse_char_stream ($$$;$) { Line 693  sub parse_char_stream ($$$;$) {
693        $self->{column} = 0;        $self->{column} = 0;
694      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 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          if ($input->read ($next, 1) and $next ne "\x0A") {
699          $self->{next_next_char} = $next;          $self->{next_next_char} = $next;
700        }        }
701        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
702        $self->{line}++;        $self->{line}++;
703        $self->{column} = 0;        $self->{column} = 0;
     } elsif ($self->{next_char} > 0x10FFFF) {  
       !!!cp ('j3');  
       $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST  
704      } elsif ($self->{next_char} == 0x0000) { # NULL      } 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->{next_char} = 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_next_char};
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->{prev_char} = [-1, -1, -1];
730              $self->{next_char} = -1;
731            }
732            return $count;
733          } else {
734            return 0;
735          }
736        } else {
737          my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
738          if ($count) {
739            $self->{column} += $count;
740            $self->{line_prev} = $self->{line};
741            $self->{column_prev} = $self->{column} - 1;
742            $self->{prev_char} = [-1, -1, -1];
743            $self->{next_char} = -1;
744          }
745          return $count;
746        }
747      }; # $self->{read_until}
748    
749    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
750      my (%opt) = @_;      my (%opt) = @_;
# Line 664  sub parse_char_stream ($$$;$) { Line 756  sub parse_char_stream ($$$;$) {
756      $onerror->(line => $self->{line}, column => $self->{column}, @_);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
757    };    };
758    
759      my $char_onerror = sub {
760        my (undef, $type, %opt) = @_;
761        !!!parse-error (layer => 'encode',
762                        line => $self->{line}, column => $self->{column} + 1,
763                        %opt, type => $type);
764      }; # $char_onerror
765    
766      if ($_[3]) {
767        $input = $_[3]->($input);
768        $input->onerror ($char_onerror);
769      } else {
770        $input->onerror ($char_onerror) unless defined $input->onerror;
771      }
772    
773    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
774    $self->_initialize_tree_constructor;    $self->_initialize_tree_constructor;
775    $self->_construct_tree;    $self->_construct_tree;
# Line 677  sub parse_char_stream ($$$;$) { Line 783  sub parse_char_stream ($$$;$) {
783  sub new ($) {  sub new ($) {
784    my $class = shift;    my $class = shift;
785    my $self = bless {    my $self = bless {
786      must_level => 'm',      level => {must => 'm',
787      should_level => 's',                should => 's',
788      good_level => 'w',                warn => 'w',
789      warn_level => 'w',                info => 'i',
790      info_level => 'i',                uncertain => 'u'},
     unsupported_level => 'u',  
791    }, $class;    }, $class;
792    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
793      $self->{next_char} = -1;      $self->{next_char} = -1;
# Line 712  sub RCDATA_CONTENT_MODEL () { CM_ENTITY Line 817  sub RCDATA_CONTENT_MODEL () { CM_ENTITY
817  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
818    
819  sub DATA_STATE () { 0 }  sub DATA_STATE () { 0 }
820  sub ENTITY_DATA_STATE () { 1 }  #sub ENTITY_DATA_STATE () { 1 }
821  sub TAG_OPEN_STATE () { 2 }  sub TAG_OPEN_STATE () { 2 }
822  sub CLOSE_TAG_OPEN_STATE () { 3 }  sub CLOSE_TAG_OPEN_STATE () { 3 }
823  sub TAG_NAME_STATE () { 4 }  sub TAG_NAME_STATE () { 4 }
# Line 723  sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 Line 828  sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8
828  sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }  sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
829  sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }  sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
830  sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }  sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
831  sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }  #sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
832  sub MARKUP_DECLARATION_OPEN_STATE () { 13 }  sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
833  sub COMMENT_START_STATE () { 14 }  sub COMMENT_START_STATE () { 14 }
834  sub COMMENT_START_DASH_STATE () { 15 }  sub COMMENT_START_DASH_STATE () { 15 }
# Line 746  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STAT Line 851  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STAT
851  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
852  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
853  sub SELF_CLOSING_START_TAG_STATE () { 34 }  sub SELF_CLOSING_START_TAG_STATE () { 34 }
854  sub CDATA_BLOCK_STATE () { 35 }  sub CDATA_SECTION_STATE () { 35 }
855    sub MD_HYPHEN_STATE () { 36 } # "markup declaration open state" in the spec
856    sub MD_DOCTYPE_STATE () { 37 } # "markup declaration open state" in the spec
857    sub MD_CDATA_STATE () { 38 } # "markup declaration open state" in the spec
858    sub CDATA_PCDATA_CLOSE_TAG_STATE () { 39 } # "close tag open state" in the spec
859    sub CDATA_SECTION_MSE1_STATE () { 40 } # "CDATA section state" in the spec
860    sub CDATA_SECTION_MSE2_STATE () { 41 } # "CDATA section state" in the spec
861    sub PUBLIC_STATE () { 42 } # "after DOCTYPE name state" in the spec
862    sub SYSTEM_STATE () { 43 } # "after DOCTYPE name state" in the spec
863    ## NOTE: "Entity data state", "entity in attribute value state", and
864    ## "consume a character reference" algorithm are jointly implemented
865    ## using the following six states:
866    sub ENTITY_STATE () { 44 }
867    sub ENTITY_HASH_STATE () { 45 }
868    sub NCR_NUM_STATE () { 46 }
869    sub HEXREF_X_STATE () { 47 }
870    sub HEXREF_HEX_STATE () { 48 }
871    sub ENTITY_NAME_STATE () { 49 }
872    
873  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
874  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 799  sub IN_COLUMN_GROUP_IM () { 0b10 } Line 921  sub IN_COLUMN_GROUP_IM () { 0b10 }
921  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
922    my $self = shift;    my $self = shift;
923    $self->{state} = DATA_STATE; # MUST    $self->{state} = DATA_STATE; # MUST
924      #$self->{state_keyword}; # initialized when used
925      #$self->{entity__value}; # initialized when used
926      #$self->{entity__match}; # initialized when used
927    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
928    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token};
929    undef $self->{current_attribute};    undef $self->{current_attribute};
930    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
931    undef $self->{last_attribute_value_state};    #$self->{prev_state}; # initialized when used
932    delete $self->{self_closing};    delete $self->{self_closing};
933    $self->{char} = [];    $self->{char_buffer} = '';
934    # $self->{next_char}    $self->{char_buffer_pos} = 0;
935      $self->{prev_char} = [-1, -1, -1];
936      $self->{next_char} = -1;
937    !!!next-input-character;    !!!next-input-character;
938    $self->{token} = [];    $self->{token} = [];
939    # $self->{escape}    # $self->{escape}
# Line 829  sub _initialize_tokenizer ($) { Line 956  sub _initialize_tokenizer ($) {
956  ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|  ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
957  ##     while the token is pushed back to the stack.  ##     while the token is pushed back to the stack.
958    
 ## 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?  
   
959  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
960    
961  ## 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 964  sub _initialize_tokenizer ($) {
964  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
965  ## and removed from the list.  ## and removed from the list.
966    
967  ## NOTE: HTML5 "Writing HTML documents" section, applied to  ## TODO: Polytheistic slash SHOULD NOT be used. (Applied only to atheists.)
968  ## documents and not to user agents and conformance checkers,  ## (This requirement was dropped from HTML5 spec, unfortunately.)
 ## contains some requirements that are not detected by the  
 ## parsing algorithm:  
 ## - Some requirements on character encoding declarations. ## TODO  
 ## - "Elements MUST NOT contain content that their content model disallows."  
 ##   ... Some are parse error, some are not (will be reported by c.c.).  
 ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO  
 ## - Text (in elements, attributes, and comments) SHOULD NOT contain  
 ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)  
   
 ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot  
 ## be detected by the HTML5 parsing algorithm:  
 ## - Text,  
969    
970  sub _get_next_token ($) {  sub _get_next_token ($) {
971    my $self = shift;    my $self = shift;
# Line 878  sub _get_next_token ($) { Line 989  sub _get_next_token ($) {
989          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
990              not $self->{escape}) {              not $self->{escape}) {
991            !!!cp (1);            !!!cp (1);
992            $self->{state} = ENTITY_DATA_STATE;            ## NOTE: In the spec, the tokenizer is switched to the
993              ## "entity data state".  In this implementation, the tokenizer
994              ## is switched to the |ENTITY_STATE|, which is an implementation
995              ## of the "consume a character reference" algorithm.
996              $self->{entity_additional} = -1;
997              $self->{prev_state} = DATA_STATE;
998              $self->{state} = ENTITY_STATE;
999            !!!next-input-character;            !!!next-input-character;
1000            redo A;            redo A;
1001          } else {          } else {
# Line 942  sub _get_next_token ($) { Line 1059  sub _get_next_token ($) {
1059                     data => chr $self->{next_char},                     data => chr $self->{next_char},
1060                     line => $self->{line}, column => $self->{column},                     line => $self->{line}, column => $self->{column},
1061                    };                    };
1062          $self->{read_until}->($token->{data}, q[-!<>&], length $token->{data});
1063    
1064        ## Stay in the data state        ## Stay in the data state
1065        !!!next-input-character;        !!!next-input-character;
1066    
1067        !!!emit ($token);        !!!emit ($token);
1068    
1069        redo A;        redo A;
     } elsif ($self->{state} == ENTITY_DATA_STATE) {  
       ## (cannot happen in CDATA state)  
   
       my ($l, $c) = ($self->{line_prev}, $self->{column_prev});  
         
       my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);  
   
       $self->{state} = DATA_STATE;  
       # next-input-character is already done  
   
       unless (defined $token) {  
         !!!cp (13);  
         !!!emit ({type => CHARACTER_TOKEN, data => '&',  
                   line => $l, column => $c,  
                  });  
       } else {  
         !!!cp (14);  
         !!!emit ($token);  
       }  
   
       redo A;  
1070      } elsif ($self->{state} == TAG_OPEN_STATE) {      } elsif ($self->{state} == TAG_OPEN_STATE) {
1071        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1072          if ($self->{next_char} == 0x002F) { # /          if ($self->{next_char} == 0x002F) { # /
# Line 1065  sub _get_next_token ($) { Line 1163  sub _get_next_token ($) {
1163          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1164        }        }
1165      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1166          ## NOTE: The "close tag open state" in the spec is implemented as
1167          ## |CLOSE_TAG_OPEN_STATE| and |CDATA_PCDATA_CLOSE_TAG_STATE|.
1168    
1169        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1170        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1171          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1172              $self->{state} = CDATA_PCDATA_CLOSE_TAG_STATE;
1173            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            $self->{state_keyword} = '';
1174            my @next_char;            ## Reconsume.
1175            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...  
           }  
1176          } else {          } else {
1177            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1178              ## NOTE: See <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>.
1179            !!!cp (28);            !!!cp (28);
           # next-input-character is already done  
1180            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1181              ## Reconsume.
1182            !!!emit ({type => CHARACTER_TOKEN, data => '</',            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1183                      line => $l, column => $c,                      line => $l, column => $c,
1184                     });                     });
1185            redo A;            redo A;
1186          }          }
1187        }        }
1188          
1189        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{next_char} and
1190            $self->{next_char} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1191          !!!cp (29);          !!!cp (29);
# Line 1174  sub _get_next_token ($) { Line 1232  sub _get_next_token ($) {
1232                                    line => $self->{line_prev}, # "<" of "</"                                    line => $self->{line_prev}, # "<" of "</"
1233                                    column => $self->{column_prev} - 1,                                    column => $self->{column_prev} - 1,
1234                                   };                                   };
1235          ## $self->{next_char} is intentionally left as is          ## NOTE: $self->{next_char} is intentionally left as is.
1236          redo A;          ## Although the "anything else" case of the spec not explicitly
1237            ## states that the next input character is to be reconsumed,
1238            ## it will be included to the |data| of the comment token
1239            ## generated from the bogus end tag, as defined in the
1240            ## "bogus comment state" entry.
1241            redo A;
1242          }
1243        } elsif ($self->{state} == CDATA_PCDATA_CLOSE_TAG_STATE) {
1244          my $ch = substr $self->{last_emitted_start_tag_name}, length $self->{state_keyword}, 1;
1245          if (length $ch) {
1246            my $CH = $ch;
1247            $ch =~ tr/a-z/A-Z/;
1248            my $nch = chr $self->{next_char};
1249            if ($nch eq $ch or $nch eq $CH) {
1250              !!!cp (24);
1251              ## Stay in the state.
1252              $self->{state_keyword} .= $nch;
1253              !!!next-input-character;
1254              redo A;
1255            } else {
1256              !!!cp (25);
1257              $self->{state} = DATA_STATE;
1258              ## Reconsume.
1259              !!!emit ({type => CHARACTER_TOKEN,
1260                        data => '</' . $self->{state_keyword},
1261                        line => $self->{line_prev},
1262                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1263                       });
1264              redo A;
1265            }
1266          } else { # after "<{tag-name}"
1267            unless ({
1268                     0x0009 => 1, # HT
1269                     0x000A => 1, # LF
1270                     0x000B => 1, # VT
1271                     0x000C => 1, # FF
1272                     0x0020 => 1, # SP
1273                     0x003E => 1, # >
1274                     0x002F => 1, # /
1275                     -1 => 1, # EOF
1276                    }->{$self->{next_char}}) {
1277              !!!cp (26);
1278              ## Reconsume.
1279              $self->{state} = DATA_STATE;
1280              !!!emit ({type => CHARACTER_TOKEN,
1281                        data => '</' . $self->{state_keyword},
1282                        line => $self->{line_prev},
1283                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1284                       });
1285              redo A;
1286            } else {
1287              !!!cp (27);
1288              $self->{current_token}
1289                  = {type => END_TAG_TOKEN,
1290                     tag_name => $self->{last_emitted_start_tag_name},
1291                     line => $self->{line_prev},
1292                     column => $self->{column_prev} - 1 - length $self->{state_keyword}};
1293              $self->{state} = TAG_NAME_STATE;
1294              ## Reconsume.
1295              redo A;
1296            }
1297        }        }
1298      } elsif ($self->{state} == TAG_NAME_STATE) {      } elsif ($self->{state} == TAG_NAME_STATE) {
1299        if ($self->{next_char} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
# Line 1345  sub _get_next_token ($) { Line 1463  sub _get_next_token ($) {
1463          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1464              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1465            !!!cp (57);            !!!cp (57);
1466            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});            !!!parse-error (type => 'duplicate attribute', text => $self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1467            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1468          } else {          } else {
1469            !!!cp (58);            !!!cp (58);
# Line 1516  sub _get_next_token ($) { Line 1634  sub _get_next_token ($) {
1634    
1635          redo A;          redo A;
1636        } else {        } else {
1637          !!!cp (82);          if ($self->{next_char} == 0x0022 or # "
1638                $self->{next_char} == 0x0027) { # '
1639              !!!cp (78);
1640              !!!parse-error (type => 'bad attribute name');
1641            } else {
1642              !!!cp (82);
1643            }
1644          $self->{current_attribute}          $self->{current_attribute}
1645              = {name => chr ($self->{next_char}),              = {name => chr ($self->{next_char}),
1646                 value => '',                 value => '',
# Line 1551  sub _get_next_token ($) { Line 1675  sub _get_next_token ($) {
1675          !!!next-input-character;          !!!next-input-character;
1676          redo A;          redo A;
1677        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1678            !!!parse-error (type => 'empty unquoted attribute value');
1679          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1680            !!!cp (87);            !!!cp (87);
1681            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
# Line 1615  sub _get_next_token ($) { Line 1740  sub _get_next_token ($) {
1740          redo A;          redo A;
1741        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1742          !!!cp (96);          !!!cp (96);
1743          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1744          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1745            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1746            ## implementation of the "consume a character reference" algorithm.
1747            $self->{prev_state} = $self->{state};
1748            $self->{entity_additional} = 0x0022; # "
1749            $self->{state} = ENTITY_STATE;
1750          !!!next-input-character;          !!!next-input-character;
1751          redo A;          redo A;
1752        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
# Line 1645  sub _get_next_token ($) { Line 1775  sub _get_next_token ($) {
1775        } else {        } else {
1776          !!!cp (100);          !!!cp (100);
1777          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{current_attribute}->{value} .= chr ($self->{next_char});
1778            $self->{read_until}->($self->{current_attribute}->{value},
1779                                  q["&],
1780                                  length $self->{current_attribute}->{value});
1781    
1782          ## Stay in the state          ## Stay in the state
1783          !!!next-input-character;          !!!next-input-character;
1784          redo A;          redo A;
# Line 1657  sub _get_next_token ($) { Line 1791  sub _get_next_token ($) {
1791          redo A;          redo A;
1792        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1793          !!!cp (102);          !!!cp (102);
1794          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1795          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1796            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1797            ## implementation of the "consume a character reference" algorithm.
1798            $self->{entity_additional} = 0x0027; # '
1799            $self->{prev_state} = $self->{state};
1800            $self->{state} = ENTITY_STATE;
1801          !!!next-input-character;          !!!next-input-character;
1802          redo A;          redo A;
1803        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
# Line 1687  sub _get_next_token ($) { Line 1826  sub _get_next_token ($) {
1826        } else {        } else {
1827          !!!cp (106);          !!!cp (106);
1828          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{current_attribute}->{value} .= chr ($self->{next_char});
1829            $self->{read_until}->($self->{current_attribute}->{value},
1830                                  q['&],
1831                                  length $self->{current_attribute}->{value});
1832    
1833          ## Stay in the state          ## Stay in the state
1834          !!!next-input-character;          !!!next-input-character;
1835          redo A;          redo A;
# Line 1703  sub _get_next_token ($) { Line 1846  sub _get_next_token ($) {
1846          redo A;          redo A;
1847        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1848          !!!cp (108);          !!!cp (108);
1849          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1850          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1851            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1852            ## implementation of the "consume a character reference" algorithm.
1853            $self->{entity_additional} = -1;
1854            $self->{prev_state} = $self->{state};
1855            $self->{state} = ENTITY_STATE;
1856          !!!next-input-character;          !!!next-input-character;
1857          redo A;          redo A;
1858        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
# Line 1764  sub _get_next_token ($) { Line 1912  sub _get_next_token ($) {
1912            !!!cp (116);            !!!cp (116);
1913          }          }
1914          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{current_attribute}->{value} .= chr ($self->{next_char});
1915            $self->{read_until}->($self->{current_attribute}->{value},
1916                                  q["'=& >],
1917                                  length $self->{current_attribute}->{value});
1918    
1919          ## Stay in the state          ## Stay in the state
1920          !!!next-input-character;          !!!next-input-character;
1921          redo A;          redo A;
1922        }        }
     } 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;  
1923      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1924        if ($self->{next_char} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1925            $self->{next_char} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
# Line 1827  sub _get_next_token ($) { Line 1957  sub _get_next_token ($) {
1957          $self->{state} = SELF_CLOSING_START_TAG_STATE;          $self->{state} = SELF_CLOSING_START_TAG_STATE;
1958          !!!next-input-character;          !!!next-input-character;
1959          redo A;          redo A;
1960          } elsif ($self->{next_char} == -1) {
1961            !!!parse-error (type => 'unclosed tag');
1962            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1963              !!!cp (122.3);
1964              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1965            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1966              if ($self->{current_token}->{attributes}) {
1967                !!!cp (122.1);
1968                !!!parse-error (type => 'end tag attribute');
1969              } else {
1970                ## NOTE: This state should never be reached.
1971                !!!cp (122.2);
1972              }
1973            } else {
1974              die "$0: $self->{current_token}->{type}: Unknown token type";
1975            }
1976            $self->{state} = DATA_STATE;
1977            ## Reconsume.
1978            !!!emit ($self->{current_token}); # start tag or end tag
1979            redo A;
1980        } else {        } else {
1981          !!!cp ('124.1');          !!!cp ('124.1');
1982          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
# Line 1859  sub _get_next_token ($) { Line 2009  sub _get_next_token ($) {
2009          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
2010    
2011          redo A;          redo A;
2012          } elsif ($self->{next_char} == -1) {
2013            !!!parse-error (type => 'unclosed tag');
2014            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
2015              !!!cp (124.7);
2016              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
2017            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
2018              if ($self->{current_token}->{attributes}) {
2019                !!!cp (124.5);
2020                !!!parse-error (type => 'end tag attribute');
2021              } else {
2022                ## NOTE: This state should never be reached.
2023                !!!cp (124.6);
2024              }
2025            } else {
2026              die "$0: $self->{current_token}->{type}: Unknown token type";
2027            }
2028            $self->{state} = DATA_STATE;
2029            ## Reconsume.
2030            !!!emit ($self->{current_token}); # start tag or end tag
2031            redo A;
2032        } else {        } else {
2033          !!!cp ('124.4');          !!!cp ('124.4');
2034          !!!parse-error (type => 'nestc');          !!!parse-error (type => 'nestc');
# Line 1869  sub _get_next_token ($) { Line 2039  sub _get_next_token ($) {
2039        }        }
2040      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
2041        ## (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  
2042    
2043            redo A;        ## NOTE: Unlike spec's "bogus comment state", this implementation
2044          } elsif ($self->{next_char} == -1) {        ## consumes characters one-by-one basis.
2045            !!!cp (125);        
2046            $self->{state} = DATA_STATE;        if ($self->{next_char} == 0x003E) { # >
2047            ## reconsume          !!!cp (124);
2048            $self->{state} = DATA_STATE;
2049            !!!next-input-character;
2050    
2051            !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2052            redo A;
2053          } elsif ($self->{next_char} == -1) {
2054            !!!cp (125);
2055            $self->{state} = DATA_STATE;
2056            ## reconsume
2057    
2058            redo A;          !!!emit ($self->{current_token}); # comment
2059          } else {          redo A;
2060            !!!cp (126);        } else {
2061            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment          !!!cp (126);
2062            !!!next-input-character;          $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2063            redo BC;          $self->{read_until}->($self->{current_token}->{data},
2064          }                                q[>],
2065        } # BC                                length $self->{current_token}->{data});
2066    
2067        die "$0: _get_next_token: unexpected case [BC]";          ## Stay in the state.
2068            !!!next-input-character;
2069            redo A;
2070          }
2071      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2072        ## (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};  
2073                
2074        if ($self->{next_char} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2075            !!!cp (133);
2076            $self->{state} = MD_HYPHEN_STATE;
2077          !!!next-input-character;          !!!next-input-character;
2078          push @next_char, $self->{next_char};          redo A;
         if ($self->{next_char} == 0x002D) { # -  
           !!!cp (127);  
           $self->{current_token} = {type => COMMENT_TOKEN, data => '',  
                                     line => $l, column => $c,  
                                    };  
           $self->{state} = COMMENT_START_STATE;  
           !!!next-input-character;  
           redo A;  
         } else {  
           !!!cp (128);  
         }  
2079        } elsif ($self->{next_char} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
2080                 $self->{next_char} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
2081            ## ASCII case-insensitive.
2082            !!!cp (130);
2083            $self->{state} = MD_DOCTYPE_STATE;
2084            $self->{state_keyword} = chr $self->{next_char};
2085          !!!next-input-character;          !!!next-input-character;
2086          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);  
         }  
2087        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2088                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2089                 $self->{next_char} == 0x005B) { # [                 $self->{next_char} == 0x005B) { # [
2090            !!!cp (135.4);                
2091            $self->{state} = MD_CDATA_STATE;
2092            $self->{state_keyword} = '[';
2093          !!!next-input-character;          !!!next-input-character;
2094          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);  
         }  
2095        } else {        } else {
2096          !!!cp (136);          !!!cp (136);
2097        }        }
2098    
2099        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment',
2100        $self->{next_char} = shift @next_char;                        line => $self->{line_prev},
2101        !!!back-next-input-character (@next_char);                        column => $self->{column_prev} - 1);
2102          ## Reconsume.
2103        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2104        $self->{current_token} = {type => COMMENT_TOKEN, data => '',        $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2105                                  line => $l, column => $c,                                  line => $self->{line_prev},
2106                                    column => $self->{column_prev} - 1,
2107                                 };                                 };
2108        redo A;        redo A;
2109              } elsif ($self->{state} == MD_HYPHEN_STATE) {
2110        ## ISSUE: typos in spec: chacacters, is is a parse error        if ($self->{next_char} == 0x002D) { # -
2111        ## 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);
2112            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2113                                      line => $self->{line_prev},
2114                                      column => $self->{column_prev} - 2,
2115                                     };
2116            $self->{state} = COMMENT_START_STATE;
2117            !!!next-input-character;
2118            redo A;
2119          } else {
2120            !!!cp (128);
2121            !!!parse-error (type => 'bogus comment',
2122                            line => $self->{line_prev},
2123                            column => $self->{column_prev} - 2);
2124            $self->{state} = BOGUS_COMMENT_STATE;
2125            ## Reconsume.
2126            $self->{current_token} = {type => COMMENT_TOKEN,
2127                                      data => '-',
2128                                      line => $self->{line_prev},
2129                                      column => $self->{column_prev} - 2,
2130                                     };
2131            redo A;
2132          }
2133        } elsif ($self->{state} == MD_DOCTYPE_STATE) {
2134          ## ASCII case-insensitive.
2135          if ($self->{next_char} == [
2136                undef,
2137                0x004F, # O
2138                0x0043, # C
2139                0x0054, # T
2140                0x0059, # Y
2141                0x0050, # P
2142              ]->[length $self->{state_keyword}] or
2143              $self->{next_char} == [
2144                undef,
2145                0x006F, # o
2146                0x0063, # c
2147                0x0074, # t
2148                0x0079, # y
2149                0x0070, # p
2150              ]->[length $self->{state_keyword}]) {
2151            !!!cp (131);
2152            ## Stay in the state.
2153            $self->{state_keyword} .= chr $self->{next_char};
2154            !!!next-input-character;
2155            redo A;
2156          } elsif ((length $self->{state_keyword}) == 6 and
2157                   ($self->{next_char} == 0x0045 or # E
2158                    $self->{next_char} == 0x0065)) { # e
2159            !!!cp (129);
2160            $self->{state} = DOCTYPE_STATE;
2161            $self->{current_token} = {type => DOCTYPE_TOKEN,
2162                                      quirks => 1,
2163                                      line => $self->{line_prev},
2164                                      column => $self->{column_prev} - 7,
2165                                     };
2166            !!!next-input-character;
2167            redo A;
2168          } else {
2169            !!!cp (132);        
2170            !!!parse-error (type => 'bogus comment',
2171                            line => $self->{line_prev},
2172                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2173            $self->{state} = BOGUS_COMMENT_STATE;
2174            ## Reconsume.
2175            $self->{current_token} = {type => COMMENT_TOKEN,
2176                                      data => $self->{state_keyword},
2177                                      line => $self->{line_prev},
2178                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2179                                     };
2180            redo A;
2181          }
2182        } elsif ($self->{state} == MD_CDATA_STATE) {
2183          if ($self->{next_char} == {
2184                '[' => 0x0043, # C
2185                '[C' => 0x0044, # D
2186                '[CD' => 0x0041, # A
2187                '[CDA' => 0x0054, # T
2188                '[CDAT' => 0x0041, # A
2189              }->{$self->{state_keyword}}) {
2190            !!!cp (135.1);
2191            ## Stay in the state.
2192            $self->{state_keyword} .= chr $self->{next_char};
2193            !!!next-input-character;
2194            redo A;
2195          } elsif ($self->{state_keyword} eq '[CDATA' and
2196                   $self->{next_char} == 0x005B) { # [
2197            !!!cp (135.2);
2198            $self->{current_token} = {type => CHARACTER_TOKEN,
2199                                      data => '',
2200                                      line => $self->{line_prev},
2201                                      column => $self->{column_prev} - 7};
2202            $self->{state} = CDATA_SECTION_STATE;
2203            !!!next-input-character;
2204            redo A;
2205          } else {
2206            !!!cp (135.3);
2207            !!!parse-error (type => 'bogus comment',
2208                            line => $self->{line_prev},
2209                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2210            $self->{state} = BOGUS_COMMENT_STATE;
2211            ## Reconsume.
2212            $self->{current_token} = {type => COMMENT_TOKEN,
2213                                      data => $self->{state_keyword},
2214                                      line => $self->{line_prev},
2215                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2216                                     };
2217            redo A;
2218          }
2219      } elsif ($self->{state} == COMMENT_START_STATE) {      } elsif ($self->{state} == COMMENT_START_STATE) {
2220        if ($self->{next_char} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2221          !!!cp (137);          !!!cp (137);
# Line 2114  sub _get_next_token ($) { Line 2298  sub _get_next_token ($) {
2298        } else {        } else {
2299          !!!cp (147);          !!!cp (147);
2300          $self->{current_token}->{data} .= chr ($self->{next_char}); # comment          $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2301            $self->{read_until}->($self->{current_token}->{data},
2302                                  q[-],
2303                                  length $self->{current_token}->{data});
2304    
2305          ## Stay in the state          ## Stay in the state
2306          !!!next-input-character;          !!!next-input-character;
2307          redo A;          redo A;
# Line 2298  sub _get_next_token ($) { Line 2486  sub _get_next_token ($) {
2486          redo A;          redo A;
2487        } elsif ($self->{next_char} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
2488                 $self->{next_char} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
2489            $self->{state} = PUBLIC_STATE;
2490            $self->{state_keyword} = chr $self->{next_char};
2491          !!!next-input-character;          !!!next-input-character;
2492          if ($self->{next_char} == 0x0055 or # U          redo A;
             $self->{next_char} == 0x0075) { # u  
           !!!next-input-character;  
           if ($self->{next_char} == 0x0042 or # B  
               $self->{next_char} == 0x0062) { # b  
             !!!next-input-character;  
             if ($self->{next_char} == 0x004C or # L  
                 $self->{next_char} == 0x006C) { # l  
               !!!next-input-character;  
               if ($self->{next_char} == 0x0049 or # I  
                   $self->{next_char} == 0x0069) { # i  
                 !!!next-input-character;  
                 if ($self->{next_char} == 0x0043 or # C  
                     $self->{next_char} == 0x0063) { # c  
                   !!!cp (168);  
                   $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;  
                   !!!next-input-character;  
                   redo A;  
                 } else {  
                   !!!cp (169);  
                 }  
               } else {  
                 !!!cp (170);  
               }  
             } else {  
               !!!cp (171);  
             }  
           } else {  
             !!!cp (172);  
           }  
         } else {  
           !!!cp (173);  
         }  
   
         #  
2493        } elsif ($self->{next_char} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
2494                 $self->{next_char} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
2495            $self->{state} = SYSTEM_STATE;
2496            $self->{state_keyword} = chr $self->{next_char};
2497          !!!next-input-character;          !!!next-input-character;
2498          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);  
         }  
   
         #  
2499        } else {        } else {
2500          !!!cp (180);          !!!cp (180);
2501            !!!parse-error (type => 'string after DOCTYPE name');
2502            $self->{current_token}->{quirks} = 1;
2503    
2504            $self->{state} = BOGUS_DOCTYPE_STATE;
2505          !!!next-input-character;          !!!next-input-character;
2506          #          redo A;
2507        }        }
2508        } elsif ($self->{state} == PUBLIC_STATE) {
2509          ## ASCII case-insensitive
2510          if ($self->{next_char} == [
2511                undef,
2512                0x0055, # U
2513                0x0042, # B
2514                0x004C, # L
2515                0x0049, # I
2516              ]->[length $self->{state_keyword}] or
2517              $self->{next_char} == [
2518                undef,
2519                0x0075, # u
2520                0x0062, # b
2521                0x006C, # l
2522                0x0069, # i
2523              ]->[length $self->{state_keyword}]) {
2524            !!!cp (175);
2525            ## Stay in the state.
2526            $self->{state_keyword} .= chr $self->{next_char};
2527            !!!next-input-character;
2528            redo A;
2529          } elsif ((length $self->{state_keyword}) == 5 and
2530                   ($self->{next_char} == 0x0043 or # C
2531                    $self->{next_char} == 0x0063)) { # c
2532            !!!cp (168);
2533            $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2534            !!!next-input-character;
2535            redo A;
2536          } else {
2537            !!!cp (169);
2538            !!!parse-error (type => 'string after DOCTYPE name',
2539                            line => $self->{line_prev},
2540                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2541            $self->{current_token}->{quirks} = 1;
2542    
2543        !!!parse-error (type => 'string after DOCTYPE name');          $self->{state} = BOGUS_DOCTYPE_STATE;
2544        $self->{current_token}->{quirks} = 1;          ## Reconsume.
2545            redo A;
2546          }
2547        } elsif ($self->{state} == SYSTEM_STATE) {
2548          ## ASCII case-insensitive
2549          if ($self->{next_char} == [
2550                undef,
2551                0x0059, # Y
2552                0x0053, # S
2553                0x0054, # T
2554                0x0045, # E
2555              ]->[length $self->{state_keyword}] or
2556              $self->{next_char} == [
2557                undef,
2558                0x0079, # y
2559                0x0073, # s
2560                0x0074, # t
2561                0x0065, # e
2562              ]->[length $self->{state_keyword}]) {
2563            !!!cp (170);
2564            ## Stay in the state.
2565            $self->{state_keyword} .= chr $self->{next_char};
2566            !!!next-input-character;
2567            redo A;
2568          } elsif ((length $self->{state_keyword}) == 5 and
2569                   ($self->{next_char} == 0x004D or # M
2570                    $self->{next_char} == 0x006D)) { # m
2571            !!!cp (171);
2572            $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2573            !!!next-input-character;
2574            redo A;
2575          } else {
2576            !!!cp (172);
2577            !!!parse-error (type => 'string after DOCTYPE name',
2578                            line => $self->{line_prev},
2579                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2580            $self->{current_token}->{quirks} = 1;
2581    
2582        $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2583        # next-input-character is already done          ## Reconsume.
2584        redo A;          redo A;
2585          }
2586      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2587        if ({        if ({
2588              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
# Line 2468  sub _get_next_token ($) { Line 2667  sub _get_next_token ($) {
2667          !!!cp (190);          !!!cp (190);
2668          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2669              .= chr $self->{next_char};              .= chr $self->{next_char};
2670            $self->{read_until}->($self->{current_token}->{public_identifier},
2671                                  q[">],
2672                                  length $self->{current_token}->{public_identifier});
2673    
2674          ## Stay in the state          ## Stay in the state
2675          !!!next-input-character;          !!!next-input-character;
2676          redo A;          redo A;
# Line 2504  sub _get_next_token ($) { Line 2707  sub _get_next_token ($) {
2707          !!!cp (194);          !!!cp (194);
2708          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2709              .= chr $self->{next_char};              .= chr $self->{next_char};
2710            $self->{read_until}->($self->{current_token}->{public_identifier},
2711                                  q['>],
2712                                  length $self->{current_token}->{public_identifier});
2713    
2714          ## Stay in the state          ## Stay in the state
2715          !!!next-input-character;          !!!next-input-character;
2716          redo A;          redo A;
# Line 2616  sub _get_next_token ($) { Line 2823  sub _get_next_token ($) {
2823          redo A;          redo A;
2824        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2825          !!!cp (208);          !!!cp (208);
2826          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2827    
2828          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2829          !!!next-input-character;          !!!next-input-character;
# Line 2640  sub _get_next_token ($) { Line 2847  sub _get_next_token ($) {
2847          !!!cp (210);          !!!cp (210);
2848          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2849              .= chr $self->{next_char};              .= chr $self->{next_char};
2850            $self->{read_until}->($self->{current_token}->{system_identifier},
2851                                  q[">],
2852                                  length $self->{current_token}->{system_identifier});
2853    
2854          ## Stay in the state          ## Stay in the state
2855          !!!next-input-character;          !!!next-input-character;
2856          redo A;          redo A;
# Line 2652  sub _get_next_token ($) { Line 2863  sub _get_next_token ($) {
2863          redo A;          redo A;
2864        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2865          !!!cp (212);          !!!cp (212);
2866          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2867    
2868          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2869          !!!next-input-character;          !!!next-input-character;
# Line 2676  sub _get_next_token ($) { Line 2887  sub _get_next_token ($) {
2887          !!!cp (214);          !!!cp (214);
2888          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2889              .= chr $self->{next_char};              .= chr $self->{next_char};
2890            $self->{read_until}->($self->{current_token}->{system_identifier},
2891                                  q['>],
2892                                  length $self->{current_token}->{system_identifier});
2893    
2894          ## Stay in the state          ## Stay in the state
2895          !!!next-input-character;          !!!next-input-character;
2896          redo A;          redo A;
# Line 2700  sub _get_next_token ($) { Line 2915  sub _get_next_token ($) {
2915        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
2916          !!!cp (217);          !!!cp (217);
2917          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
   
2918          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2919          ## reconsume          ## reconsume
2920    
# Line 2737  sub _get_next_token ($) { Line 2951  sub _get_next_token ($) {
2951          redo A;          redo A;
2952        } else {        } else {
2953          !!!cp (221);          !!!cp (221);
2954            my $s = '';
2955            $self->{read_until}->($s, q[>], 0);
2956    
2957          ## Stay in the state          ## Stay in the state
2958          !!!next-input-character;          !!!next-input-character;
2959          redo A;          redo A;
2960        }        }
2961      } elsif ($self->{state} == CDATA_BLOCK_STATE) {      } elsif ($self->{state} == CDATA_SECTION_STATE) {
2962        my $s = '';        ## NOTE: "CDATA section state" in the state is jointly implemented
2963          ## by three states, |CDATA_SECTION_STATE|, |CDATA_SECTION_MSE1_STATE|,
2964          ## and |CDATA_SECTION_MSE2_STATE|.
2965                
2966        my ($l, $c) = ($self->{line}, $self->{column});        if ($self->{next_char} == 0x005D) { # ]
2967            !!!cp (221.1);
2968        CS: while ($self->{next_char} != -1) {          $self->{state} = CDATA_SECTION_MSE1_STATE;
2969          if ($self->{next_char} == 0x005D) { # ]          !!!next-input-character;
2970            !!!next-input-character;          redo A;
2971            if ($self->{next_char} == 0x005D) { # ]        } elsif ($self->{next_char} == -1) {
2972              !!!next-input-character;          $self->{state} = DATA_STATE;
             MDC: {  
               if ($self->{next_char} == 0x003E) { # >  
                 !!!cp (221.1);  
                 !!!next-input-character;  
                 last CS;  
               } elsif ($self->{next_char} == 0x005D) { # ]  
                 !!!cp (221.2);  
                 $s .= ']';  
                 !!!next-input-character;  
                 redo MDC;  
               } else {  
                 !!!cp (221.3);  
                 $s .= ']]';  
                 #  
               }  
             } # MDC  
           } else {  
             !!!cp (221.4);  
             $s .= ']';  
             #  
           }  
         } else {  
           !!!cp (221.5);  
           #  
         }  
         $s .= chr $self->{next_char};  
2973          !!!next-input-character;          !!!next-input-character;
2974        } # CS          if (length $self->{current_token}->{data}) { # character
2975              !!!cp (221.2);
2976              !!!emit ($self->{current_token}); # character
2977            } else {
2978              !!!cp (221.3);
2979              ## No token to emit. $self->{current_token} is discarded.
2980            }        
2981            redo A;
2982          } else {
2983            !!!cp (221.4);
2984            $self->{current_token}->{data} .= chr $self->{next_char};
2985            $self->{read_until}->($self->{current_token}->{data},
2986                                  q<]>,
2987                                  length $self->{current_token}->{data});
2988    
2989        $self->{state} = DATA_STATE;          ## Stay in the state.
2990        ## next-input-character done or EOF, which is reconsumed.          !!!next-input-character;
2991            redo A;
2992          }
2993    
2994        if (length $s) {        ## ISSUE: "text tokens" in spec.
2995        } elsif ($self->{state} == CDATA_SECTION_MSE1_STATE) {
2996          if ($self->{next_char} == 0x005D) { # ]
2997            !!!cp (221.5);
2998            $self->{state} = CDATA_SECTION_MSE2_STATE;
2999            !!!next-input-character;
3000            redo A;
3001          } else {
3002          !!!cp (221.6);          !!!cp (221.6);
3003          !!!emit ({type => CHARACTER_TOKEN, data => $s,          $self->{current_token}->{data} .= ']';
3004                    line => $l, column => $c});          $self->{state} = CDATA_SECTION_STATE;
3005            ## Reconsume.
3006            redo A;
3007          }
3008        } elsif ($self->{state} == CDATA_SECTION_MSE2_STATE) {
3009          if ($self->{next_char} == 0x003E) { # >
3010            $self->{state} = DATA_STATE;
3011            !!!next-input-character;
3012            if (length $self->{current_token}->{data}) { # character
3013              !!!cp (221.7);
3014              !!!emit ($self->{current_token}); # character
3015            } else {
3016              !!!cp (221.8);
3017              ## No token to emit. $self->{current_token} is discarded.
3018            }
3019            redo A;
3020          } elsif ($self->{next_char} == 0x005D) { # ]
3021            !!!cp (221.9); # character
3022            $self->{current_token}->{data} .= ']'; ## Add first "]" of "]]]".
3023            ## Stay in the state.
3024            !!!next-input-character;
3025            redo A;
3026          } else {
3027            !!!cp (221.11);
3028            $self->{current_token}->{data} .= ']]'; # character
3029            $self->{state} = CDATA_SECTION_STATE;
3030            ## Reconsume.
3031            redo A;
3032          }
3033        } elsif ($self->{state} == ENTITY_STATE) {
3034          if ({
3035            0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
3036            0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, &
3037            $self->{entity_additional} => 1,
3038          }->{$self->{next_char}}) {
3039            !!!cp (1001);
3040            ## Don't consume
3041            ## No error
3042            ## Return nothing.
3043            #
3044          } elsif ($self->{next_char} == 0x0023) { # #
3045            !!!cp (999);
3046            $self->{state} = ENTITY_HASH_STATE;
3047            $self->{state_keyword} = '#';
3048            !!!next-input-character;
3049            redo A;
3050          } elsif ((0x0041 <= $self->{next_char} and
3051                    $self->{next_char} <= 0x005A) or # A..Z
3052                   (0x0061 <= $self->{next_char} and
3053                    $self->{next_char} <= 0x007A)) { # a..z
3054            !!!cp (998);
3055            require Whatpm::_NamedEntityList;
3056            $self->{state} = ENTITY_NAME_STATE;
3057            $self->{state_keyword} = chr $self->{next_char};
3058            $self->{entity__value} = $self->{state_keyword};
3059            $self->{entity__match} = 0;
3060            !!!next-input-character;
3061            redo A;
3062        } else {        } else {
3063          !!!cp (221.7);          !!!cp (1027);
3064            !!!parse-error (type => 'bare ero');
3065            ## Return nothing.
3066            #
3067        }        }
3068    
3069        redo A;        ## NOTE: No character is consumed by the "consume a character
3070          ## reference" algorithm.  In other word, there is an "&" character
3071        ## ISSUE: "text tokens" in spec.        ## that does not introduce a character reference, which would be
3072        ## TODO: Streaming support        ## appended to the parent element or the attribute value in later
3073      } else {        ## process of the tokenizer.
3074        die "$0: $self->{state}: Unknown state";  
3075      }        if ($self->{prev_state} == DATA_STATE) {
3076    } # A            !!!cp (997);
3077            $self->{state} = $self->{prev_state};
3078    die "$0: _get_next_token: unexpected case";          ## Reconsume.
3079  } # _get_next_token          !!!emit ({type => CHARACTER_TOKEN, data => '&',
3080                      line => $self->{line_prev},
3081  sub _tokenize_attempt_to_consume_an_entity ($$$) {                    column => $self->{column_prev},
3082    my ($self, $in_attr, $additional) = @_;                   });
3083            redo A;
3084    my ($l, $c) = ($self->{line_prev}, $self->{column_prev});        } else {
3085            !!!cp (996);
3086            $self->{current_attribute}->{value} .= '&';
3087            $self->{state} = $self->{prev_state};
3088            ## Reconsume.
3089            redo A;
3090          }
3091        } elsif ($self->{state} == ENTITY_HASH_STATE) {
3092          if ($self->{next_char} == 0x0078 or # x
3093              $self->{next_char} == 0x0058) { # X
3094            !!!cp (995);
3095            $self->{state} = HEXREF_X_STATE;
3096            $self->{state_keyword} .= chr $self->{next_char};
3097            !!!next-input-character;
3098            redo A;
3099          } elsif (0x0030 <= $self->{next_char} and
3100                   $self->{next_char} <= 0x0039) { # 0..9
3101            !!!cp (994);
3102            $self->{state} = NCR_NUM_STATE;
3103            $self->{state_keyword} = $self->{next_char} - 0x0030;
3104            !!!next-input-character;
3105            redo A;
3106          } else {
3107            !!!parse-error (type => 'bare nero',
3108                            line => $self->{line_prev},
3109                            column => $self->{column_prev} - 1);
3110    
3111    if ({          ## NOTE: According to the spec algorithm, nothing is returned,
3112         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,          ## and then "&#" is appended to the parent element or the attribute
3113         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR          ## value in the later processing.
3114         $additional => 1,  
3115        }->{$self->{next_char}}) {          if ($self->{prev_state} == DATA_STATE) {
3116      !!!cp (1001);            !!!cp (1019);
3117      ## Don't consume            $self->{state} = $self->{prev_state};
3118      ## No error            ## Reconsume.
3119      return undef;            !!!emit ({type => CHARACTER_TOKEN,
3120    } elsif ($self->{next_char} == 0x0023) { # #                      data => '&#',
3121      !!!next-input-character;                      line => $self->{line_prev},
3122      if ($self->{next_char} == 0x0078 or # x                      column => $self->{column_prev} - 1,
3123          $self->{next_char} == 0x0058) { # X                     });
3124        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;  
3125          } else {          } else {
3126            !!!cp (1007);            !!!cp (993);
3127            !!!parse-error (type => 'no refc', line => $l, column => $c);            $self->{current_attribute}->{value} .= '&#';
3128              $self->{state} = $self->{prev_state};
3129              ## Reconsume.
3130              redo A;
3131          }          }
3132          }
3133          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {      } elsif ($self->{state} == NCR_NUM_STATE) {
3134            !!!cp (1008);        if (0x0030 <= $self->{next_char} and
3135            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);            $self->{next_char} <= 0x0039) { # 0..9
           $code = 0xFFFD;  
         } elsif ($code > 0x10FFFF) {  
           !!!cp (1009);  
           !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);  
           $code = 0xFFFD;  
         } elsif ($code == 0x000D) {  
           !!!cp (1010);  
           !!!parse-error (type => 'CR character reference', line => $l, column => $c);  
           $code = 0x000A;  
         } elsif (0x80 <= $code and $code <= 0x9F) {  
           !!!cp (1011);  
           !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);  
           $code = $c1_entity_char->{$code};  
         }  
   
         return {type => CHARACTER_TOKEN, data => chr $code,  
                 has_reference => 1,  
                 line => $l, column => $c,  
                };  
       } # X  
     } elsif (0x0030 <= $self->{next_char} and  
              $self->{next_char} <= 0x0039) { # 0..9  
       my $code = $self->{next_char} - 0x0030;  
       !!!next-input-character;  
         
       while (0x0030 <= $self->{next_char} and  
                 $self->{next_char} <= 0x0039) { # 0..9  
3136          !!!cp (1012);          !!!cp (1012);
3137          $code *= 10;          $self->{state_keyword} *= 10;
3138          $code += $self->{next_char} - 0x0030;          $self->{state_keyword} += $self->{next_char} - 0x0030;
3139                    
3140            ## Stay in the state.
3141          !!!next-input-character;          !!!next-input-character;
3142        }          redo A;
3143          } elsif ($self->{next_char} == 0x003B) { # ;
       if ($self->{next_char} == 0x003B) { # ;  
3144          !!!cp (1013);          !!!cp (1013);
3145          !!!next-input-character;          !!!next-input-character;
3146            #
3147        } else {        } else {
3148          !!!cp (1014);          !!!cp (1014);
3149          !!!parse-error (type => 'no refc', line => $l, column => $c);          !!!parse-error (type => 'no refc');
3150            ## Reconsume.
3151            #
3152        }        }
3153    
3154          my $code = $self->{state_keyword};
3155          my $l = $self->{line_prev};
3156          my $c = $self->{column_prev};
3157        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3158          !!!cp (1015);          !!!cp (1015);
3159          !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);          !!!parse-error (type => 'invalid character reference',
3160                            text => (sprintf 'U+%04X', $code),
3161                            line => $l, column => $c);
3162          $code = 0xFFFD;          $code = 0xFFFD;
3163        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
3164          !!!cp (1016);          !!!cp (1016);
3165          !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);          !!!parse-error (type => 'invalid character reference',
3166                            text => (sprintf 'U-%08X', $code),
3167                            line => $l, column => $c);
3168          $code = 0xFFFD;          $code = 0xFFFD;
3169        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
3170          !!!cp (1017);          !!!cp (1017);
3171          !!!parse-error (type => 'CR character reference', line => $l, column => $c);          !!!parse-error (type => 'CR character reference',
3172                            line => $l, column => $c);
3173          $code = 0x000A;          $code = 0x000A;
3174        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3175          !!!cp (1018);          !!!cp (1018);
3176          !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);          !!!parse-error (type => 'C1 character reference',
3177                            text => (sprintf 'U+%04X', $code),
3178                            line => $l, column => $c);
3179          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3180        }        }
3181          
3182        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,        if ($self->{prev_state} == DATA_STATE) {
3183                line => $l, column => $c,          !!!cp (992);
3184               };          $self->{state} = $self->{prev_state};
3185      } else {          ## Reconsume.
3186        !!!cp (1019);          !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3187        !!!parse-error (type => 'bare nero', line => $l, column => $c);                    line => $l, column => $c,
3188        !!!back-next-input-character ($self->{next_char});                   });
3189        $self->{next_char} = 0x0023; # #          redo A;
3190        return undef;        } else {
3191      }          !!!cp (991);
3192    } elsif ((0x0041 <= $self->{next_char} and          $self->{current_attribute}->{value} .= chr $code;
3193              $self->{next_char} <= 0x005A) or          $self->{current_attribute}->{has_reference} = 1;
3194             (0x0061 <= $self->{next_char} and          $self->{state} = $self->{prev_state};
3195              $self->{next_char} <= 0x007A)) {          ## Reconsume.
3196      my $entity_name = chr $self->{next_char};          redo A;
3197      !!!next-input-character;        }
3198        } elsif ($self->{state} == HEXREF_X_STATE) {
3199      my $value = $entity_name;        if ((0x0030 <= $self->{next_char} and $self->{next_char} <= 0x0039) or
3200      my $match = 0;            (0x0041 <= $self->{next_char} and $self->{next_char} <= 0x0046) or
3201      require Whatpm::_NamedEntityList;            (0x0061 <= $self->{next_char} and $self->{next_char} <= 0x0066)) {
3202      our $EntityChar;          # 0..9, A..F, a..f
3203            !!!cp (990);
3204      while (length $entity_name < 30 and          $self->{state} = HEXREF_HEX_STATE;
3205             ## NOTE: Some number greater than the maximum length of entity name          $self->{state_keyword} = 0;
3206             ((0x0041 <= $self->{next_char} and # a          ## Reconsume.
3207               $self->{next_char} <= 0x005A) or # x          redo A;
3208              (0x0061 <= $self->{next_char} and # a        } else {
3209               $self->{next_char} <= 0x007A) or # z          !!!parse-error (type => 'bare hcro',
3210              (0x0030 <= $self->{next_char} and # 0                          line => $self->{line_prev},
3211               $self->{next_char} <= 0x0039) or # 9                          column => $self->{column_prev} - 2);
3212              $self->{next_char} == 0x003B)) { # ;  
3213        $entity_name .= chr $self->{next_char};          ## NOTE: According to the spec algorithm, nothing is returned,
3214        if (defined $EntityChar->{$entity_name}) {          ## and then "&#" followed by "X" or "x" is appended to the parent
3215          if ($self->{next_char} == 0x003B) { # ;          ## element or the attribute value in the later processing.
3216            !!!cp (1020);  
3217            $value = $EntityChar->{$entity_name};          if ($self->{prev_state} == DATA_STATE) {
3218            $match = 1;            !!!cp (1005);
3219            !!!next-input-character;            $self->{state} = $self->{prev_state};
3220            last;            ## Reconsume.
3221              !!!emit ({type => CHARACTER_TOKEN,
3222                        data => '&' . $self->{state_keyword},
3223                        line => $self->{line_prev},
3224                        column => $self->{column_prev} - length $self->{state_keyword},
3225                       });
3226              redo A;
3227            } else {
3228              !!!cp (989);
3229              $self->{current_attribute}->{value} .= '&' . $self->{state_keyword};
3230              $self->{state} = $self->{prev_state};
3231              ## Reconsume.
3232              redo A;
3233            }
3234          }
3235        } elsif ($self->{state} == HEXREF_HEX_STATE) {
3236          if (0x0030 <= $self->{next_char} and $self->{next_char} <= 0x0039) {
3237            # 0..9
3238            !!!cp (1002);
3239            $self->{state_keyword} *= 0x10;
3240            $self->{state_keyword} += $self->{next_char} - 0x0030;
3241            ## Stay in the state.
3242            !!!next-input-character;
3243            redo A;
3244          } elsif (0x0061 <= $self->{next_char} and
3245                   $self->{next_char} <= 0x0066) { # a..f
3246            !!!cp (1003);
3247            $self->{state_keyword} *= 0x10;
3248            $self->{state_keyword} += $self->{next_char} - 0x0060 + 9;
3249            ## Stay in the state.
3250            !!!next-input-character;
3251            redo A;
3252          } elsif (0x0041 <= $self->{next_char} and
3253                   $self->{next_char} <= 0x0046) { # A..F
3254            !!!cp (1004);
3255            $self->{state_keyword} *= 0x10;
3256            $self->{state_keyword} += $self->{next_char} - 0x0040 + 9;
3257            ## Stay in the state.
3258            !!!next-input-character;
3259            redo A;
3260          } elsif ($self->{next_char} == 0x003B) { # ;
3261            !!!cp (1006);
3262            !!!next-input-character;
3263            #
3264          } else {
3265            !!!cp (1007);
3266            !!!parse-error (type => 'no refc',
3267                            line => $self->{line},
3268                            column => $self->{column});
3269            ## Reconsume.
3270            #
3271          }
3272    
3273          my $code = $self->{state_keyword};
3274          my $l = $self->{line_prev};
3275          my $c = $self->{column_prev};
3276          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3277            !!!cp (1008);
3278            !!!parse-error (type => 'invalid character reference',
3279                            text => (sprintf 'U+%04X', $code),
3280                            line => $l, column => $c);
3281            $code = 0xFFFD;
3282          } elsif ($code > 0x10FFFF) {
3283            !!!cp (1009);
3284            !!!parse-error (type => 'invalid character reference',
3285                            text => (sprintf 'U-%08X', $code),
3286                            line => $l, column => $c);
3287            $code = 0xFFFD;
3288          } elsif ($code == 0x000D) {
3289            !!!cp (1010);
3290            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
3291            $code = 0x000A;
3292          } elsif (0x80 <= $code and $code <= 0x9F) {
3293            !!!cp (1011);
3294            !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
3295            $code = $c1_entity_char->{$code};
3296          }
3297    
3298          if ($self->{prev_state} == DATA_STATE) {
3299            !!!cp (988);
3300            $self->{state} = $self->{prev_state};
3301            ## Reconsume.
3302            !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3303                      line => $l, column => $c,
3304                     });
3305            redo A;
3306          } else {
3307            !!!cp (987);
3308            $self->{current_attribute}->{value} .= chr $code;
3309            $self->{current_attribute}->{has_reference} = 1;
3310            $self->{state} = $self->{prev_state};
3311            ## Reconsume.
3312            redo A;
3313          }
3314        } elsif ($self->{state} == ENTITY_NAME_STATE) {
3315          if (length $self->{state_keyword} < 30 and
3316              ## NOTE: Some number greater than the maximum length of entity name
3317              ((0x0041 <= $self->{next_char} and # a
3318                $self->{next_char} <= 0x005A) or # x
3319               (0x0061 <= $self->{next_char} and # a
3320                $self->{next_char} <= 0x007A) or # z
3321               (0x0030 <= $self->{next_char} and # 0
3322                $self->{next_char} <= 0x0039) or # 9
3323               $self->{next_char} == 0x003B)) { # ;
3324            our $EntityChar;
3325            $self->{state_keyword} .= chr $self->{next_char};
3326            if (defined $EntityChar->{$self->{state_keyword}}) {
3327              if ($self->{next_char} == 0x003B) { # ;
3328                !!!cp (1020);
3329                $self->{entity__value} = $EntityChar->{$self->{state_keyword}};
3330                $self->{entity__match} = 1;
3331                !!!next-input-character;
3332                #
3333              } else {
3334                !!!cp (1021);
3335                $self->{entity__value} = $EntityChar->{$self->{state_keyword}};
3336                $self->{entity__match} = -1;
3337                ## Stay in the state.
3338                !!!next-input-character;
3339                redo A;
3340              }
3341          } else {          } else {
3342            !!!cp (1021);            !!!cp (1022);
3343            $value = $EntityChar->{$entity_name};            $self->{entity__value} .= chr $self->{next_char};
3344            $match = -1;            $self->{entity__match} *= 2;
3345              ## Stay in the state.
3346            !!!next-input-character;            !!!next-input-character;
3347              redo A;
3348            }
3349          }
3350    
3351          my $data;
3352          my $has_ref;
3353          if ($self->{entity__match} > 0) {
3354            !!!cp (1023);
3355            $data = $self->{entity__value};
3356            $has_ref = 1;
3357            #
3358          } elsif ($self->{entity__match} < 0) {
3359            !!!parse-error (type => 'no refc');
3360            if ($self->{prev_state} != DATA_STATE and # in attribute
3361                $self->{entity__match} < -1) {
3362              !!!cp (1024);
3363              $data = '&' . $self->{state_keyword};
3364              #
3365            } else {
3366              !!!cp (1025);
3367              $data = $self->{entity__value};
3368              $has_ref = 1;
3369              #
3370          }          }
3371        } else {        } else {
3372          !!!cp (1022);          !!!cp (1026);
3373          $value .= chr $self->{next_char};          !!!parse-error (type => 'bare ero',
3374          $match *= 2;                          line => $self->{line_prev},
3375          !!!next-input-character;                          column => $self->{column_prev} - length $self->{state_keyword});
3376            $data = '&' . $self->{state_keyword};
3377            #
3378        }        }
3379      }    
3380              ## NOTE: In these cases, when a character reference is found,
3381      if ($match > 0) {        ## it is consumed and a character token is returned, or, otherwise,
3382        !!!cp (1023);        ## nothing is consumed and returned, according to the spec algorithm.
3383        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,        ## In this implementation, anything that has been examined by the
3384                line => $l, column => $c,        ## tokenizer is appended to the parent element or the attribute value
3385               };        ## as string, either literal string when no character reference or
3386      } elsif ($match < 0) {        ## entity-replaced string otherwise, in this stage, since any characters
3387        !!!parse-error (type => 'no refc', line => $l, column => $c);        ## that would not be consumed are appended in the data state or in an
3388        if ($in_attr and $match < -1) {        ## appropriate attribute value state anyway.
3389          !!!cp (1024);  
3390          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,        if ($self->{prev_state} == DATA_STATE) {
3391                  line => $l, column => $c,          !!!cp (986);
3392                 };          $self->{state} = $self->{prev_state};
3393        } else {          ## Reconsume.
3394          !!!cp (1025);          !!!emit ({type => CHARACTER_TOKEN,
3395          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,                    data => $data,
3396                  line => $l, column => $c,                    line => $self->{line_prev},
3397                 };                    column => $self->{column_prev} + 1 - length $self->{state_keyword},
3398                     });
3399            redo A;
3400          } else {
3401            !!!cp (985);
3402            $self->{current_attribute}->{value} .= $data;
3403            $self->{current_attribute}->{has_reference} = 1 if $has_ref;
3404            $self->{state} = $self->{prev_state};
3405            ## Reconsume.
3406            redo A;
3407        }        }
3408      } else {      } else {
3409        !!!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,  
              };  
3410      }      }
3411    } else {    } # A  
3412      !!!cp (1027);  
3413      ## no characters are consumed    die "$0: _get_next_token: unexpected case";
3414      !!!parse-error (type => 'bare ero', line => $l, column => $c);  } # _get_next_token
     return undef;  
   }  
 } # _tokenize_attempt_to_consume_an_entity  
3415    
3416  sub _initialize_tree_constructor ($) {  sub _initialize_tree_constructor ($) {
3417    my $self = shift;    my $self = shift;
# Line 3017  sub _initialize_tree_constructor ($) { Line 3420  sub _initialize_tree_constructor ($) {
3420    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3421    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3422    $self->{document}->manakai_is_html (1); # MUST    $self->{document}->manakai_is_html (1); # MUST
3423      $self->{document}->set_user_data (manakai_source_line => 1);
3424      $self->{document}->set_user_data (manakai_source_column => 1);
3425  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3426    
3427  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 3071  sub _tree_construction_initial ($) { Line 3476  sub _tree_construction_initial ($) {
3476        ## language.        ## language.
3477        my $doctype_name = $token->{name};        my $doctype_name = $token->{name};
3478        $doctype_name = '' unless defined $doctype_name;        $doctype_name = '' unless defined $doctype_name;
3479        $doctype_name =~ tr/a-z/A-Z/;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3480        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
           defined $token->{public_identifier} or  
3481            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3482          !!!cp ('t1');          !!!cp ('t1');
3483          !!!parse-error (type => 'not HTML5', token => $token);          !!!parse-error (type => 'not HTML5', token => $token);
3484        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3485          !!!cp ('t2');          !!!cp ('t2');
         ## ISSUE: ASCII case-insensitive? (in fact it does not matter)  
3486          !!!parse-error (type => 'not HTML5', token => $token);          !!!parse-error (type => 'not HTML5', token => $token);
3487          } elsif (defined $token->{public_identifier}) {
3488            if ($token->{public_identifier} eq 'XSLT-compat') {
3489              !!!cp ('t1.2');
3490              !!!parse-error (type => 'XSLT-compat', token => $token,
3491                              level => $self->{level}->{should});
3492            } else {
3493              !!!parse-error (type => 'not HTML5', token => $token);
3494            }
3495        } else {        } else {
3496          !!!cp ('t3');          !!!cp ('t3');
3497            #
3498        }        }
3499                
3500        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
# Line 3103  sub _tree_construction_initial ($) { Line 3515  sub _tree_construction_initial ($) {
3515        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3516          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3517          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3518          if ({          my $prefix = [
3519            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3520            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3521            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3522            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3523            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3524            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3525            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3526            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3527            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3528            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3529            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3530            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3531            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3532            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3533            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3534            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3535            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3536            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3537            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3538            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3539            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3540            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3541            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3542            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3543            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3544            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3545            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3546            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3547            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3548            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3549            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3550            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3551            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3552            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3553            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3554            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3555            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3556            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3557            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3558            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3559            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3560            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3561            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3562            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3563            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3564            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3565            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3566            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3567            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3568            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3569            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3570            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD W3 HTML//",
3571            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3572            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3573            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3574            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,          ]; # $prefix
3575            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,          my $match;
3576            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,          for (@$prefix) {
3577            "-//W3C//DTD HTML 3.2//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3578            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,              $match = 1;
3579            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,              last;
3580            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            }
3581            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,          }
3582            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,          if ($match or
3583            "-//W3C//DTD W3 HTML//EN" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3584            "-//W3O//DTD W3 HTML 3.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3585            "-//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}) {  
3586            !!!cp ('t5');            !!!cp ('t5');
3587            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3588          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3589                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3590            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3591              !!!cp ('t6');              !!!cp ('t6');
3592              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
# Line 3188  sub _tree_construction_initial ($) { Line 3594  sub _tree_construction_initial ($) {
3594              !!!cp ('t7');              !!!cp ('t7');
3595              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3596            }            }
3597          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3598                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3599            !!!cp ('t8');            !!!cp ('t8');
3600            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3601          } else {          } else {
# Line 3202  sub _tree_construction_initial ($) { Line 3608  sub _tree_construction_initial ($) {
3608          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3609          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3610          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") {
3611            ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"            ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3612              ## marked as quirks.
3613            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3614            !!!cp ('t11');            !!!cp ('t11');
3615          } else {          } else {
# Line 3374  sub _reset_insertion_mode ($) { Line 3781  sub _reset_insertion_mode ($) {
3781        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3782          $last = 1;          $last = 1;
3783          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3784            if ($self->{inner_html_node}->[1] & TABLE_CELL_EL) {            !!!cp ('t28');
3785              !!!cp ('t27');            $node = $self->{inner_html_node};
3786              #          } else {
3787            } else {            die "_reset_insertion_mode: t27";
             !!!cp ('t28');  
             $node = $self->{inner_html_node};  
           }  
3788          }          }
3789        }        }
3790              
3791      ## Step 4..14        ## Step 4..14
3792      my $new_mode;        my $new_mode;
3793      if ($node->[1] & FOREIGN_EL) {        if ($node->[1] & FOREIGN_EL) {
3794        ## NOTE: Strictly spaking, the line below only applies to MathML and          !!!cp ('t28.1');
3795        ## SVG elements.  Currently the HTML syntax supports only MathML and          ## NOTE: Strictly spaking, the line below only applies to MathML and
3796        ## SVG elements as foreigners.          ## SVG elements.  Currently the HTML syntax supports only MathML and
3797        $new_mode = $self->{insertion_mode} | IN_FOREIGN_CONTENT_IM;          ## SVG elements as foreigners.
3798        ## ISSUE: What is set as the secondary insertion mode?          $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3799      } else {        } elsif ($node->[1] & TABLE_CELL_EL) {
3800        $new_mode = {          if ($last) {
3801              !!!cp ('t28.2');
3802              #
3803            } else {
3804              !!!cp ('t28.3');
3805              $new_mode = IN_CELL_IM;
3806            }
3807          } else {
3808            !!!cp ('t28.4');
3809            $new_mode = {
3810                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3811                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3812                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
                       td => IN_CELL_IM,  
                       th => IN_CELL_IM,  
3813                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3814                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3815                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 3410  sub _reset_insertion_mode ($) { Line 3821  sub _reset_insertion_mode ($) {
3821                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3822                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3823                       }->{$node->[0]->manakai_local_name};                       }->{$node->[0]->manakai_local_name};
3824      }        }
3825      $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3826                
3827        ## Step 15        ## Step 15
3828        if ($node->[1] & HTML_EL) {        if ($node->[1] & HTML_EL) {
# Line 3585  sub _tree_construction_main ($) { Line 3996  sub _tree_construction_main ($) {
3996        ## NOTE: An end-of-file token.        ## NOTE: An end-of-file token.
3997        if ($content_model_flag == CDATA_CONTENT_MODEL) {        if ($content_model_flag == CDATA_CONTENT_MODEL) {
3998          !!!cp ('t43');          !!!cp ('t43');
3999          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);          !!!parse-error (type => 'in CDATA:#eof', token => $token);
4000        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
4001          !!!cp ('t44');          !!!cp ('t44');
4002          !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);          !!!parse-error (type => 'in RCDATA:#eof', token => $token);
4003        } else {        } else {
4004          die "$0: $content_model_flag in parse_rcdata";          die "$0: $content_model_flag in parse_rcdata";
4005        }        }
# Line 3625  sub _tree_construction_main ($) { Line 4036  sub _tree_construction_main ($) {
4036        ## Ignore the token        ## Ignore the token
4037      } else {      } else {
4038        !!!cp ('t48');        !!!cp ('t48');
4039        !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);        !!!parse-error (type => 'in CDATA:#eof', token => $token);
4040        ## ISSUE: And ignore?        ## ISSUE: And ignore?
4041        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
4042      }      }
# Line 3676  sub _tree_construction_main ($) { Line 4087  sub _tree_construction_main ($) {
4087        } # AFE        } # AFE
4088        unless (defined $formatting_element) {        unless (defined $formatting_element) {
4089          !!!cp ('t53');          !!!cp ('t53');
4090          !!!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);
4091          ## Ignore the token          ## Ignore the token
4092          !!!next-token;          !!!next-token;
4093          return;          return;
# Line 3693  sub _tree_construction_main ($) { Line 4104  sub _tree_construction_main ($) {
4104              last INSCOPE;              last INSCOPE;
4105            } else { # in open elements but not in scope            } else { # in open elements but not in scope
4106              !!!cp ('t55');              !!!cp ('t55');
4107              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},              !!!parse-error (type => 'unmatched end tag',
4108                                text => $token->{tag_name},
4109                              token => $end_tag_token);                              token => $end_tag_token);
4110              ## Ignore the token              ## Ignore the token
4111              !!!next-token;              !!!next-token;
# Line 3706  sub _tree_construction_main ($) { Line 4118  sub _tree_construction_main ($) {
4118        } # INSCOPE        } # INSCOPE
4119        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
4120          !!!cp ('t57');          !!!cp ('t57');
4121          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},          !!!parse-error (type => 'unmatched end tag',
4122                            text => $token->{tag_name},
4123                          token => $end_tag_token);                          token => $end_tag_token);
4124          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
4125          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
# Line 3715  sub _tree_construction_main ($) { Line 4128  sub _tree_construction_main ($) {
4128        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
4129          !!!cp ('t58');          !!!cp ('t58');
4130          !!!parse-error (type => 'not closed',          !!!parse-error (type => 'not closed',
4131                          value => $self->{open_elements}->[-1]->[0]                          text => $self->{open_elements}->[-1]->[0]
4132                              ->manakai_local_name,                              ->manakai_local_name,
4133                          token => $end_tag_token);                          token => $end_tag_token);
4134        }        }
# Line 3924  sub _tree_construction_main ($) { Line 4337  sub _tree_construction_main ($) {
4337    B: while (1) {    B: while (1) {
4338      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
4339        !!!cp ('t73');        !!!cp ('t73');
4340        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4341        ## Ignore the token        ## Ignore the token
4342        ## Stay in the phase        ## Stay in the phase
4343        !!!next-token;        !!!next-token;
# Line 3933  sub _tree_construction_main ($) { Line 4346  sub _tree_construction_main ($) {
4346               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
4347        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4348          !!!cp ('t79');          !!!cp ('t79');
4349          !!!parse-error (type => 'after html:html', token => $token);          !!!parse-error (type => 'after html', text => 'html', token => $token);
4350          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
4351        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4352          !!!cp ('t80');          !!!cp ('t80');
4353          !!!parse-error (type => 'after html:html', token => $token);          !!!parse-error (type => 'after html', text => 'html', token => $token);
4354          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4355        } else {        } else {
4356          !!!cp ('t81');          !!!cp ('t81');
# Line 3988  sub _tree_construction_main ($) { Line 4401  sub _tree_construction_main ($) {
4401            #            #
4402          } elsif ({          } elsif ({
4403                    b => 1, big => 1, blockquote => 1, body => 1, br => 1,                    b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4404                    center => 1, code => 1, dd => 1, div => 1, dl => 1, em => 1,                    center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4405                    embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1, ## No h4!                    em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4406                    h5 => 1, h6 => 1, head => 1, hr => 1, i => 1, img => 1,                    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4407                    li => 1, menu => 1, meta => 1, nobr => 1, p => 1, pre => 1,                    img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4408                    ruby => 1, s => 1, small => 1, span => 1, strong => 1,                    nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4409                    sub => 1, sup => 1, table => 1, tt => 1, u => 1, ul => 1,                    small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4410                    var => 1,                    sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4411                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
4412            !!!cp ('t87.2');            !!!cp ('t87.2');
4413            !!!parse-error (type => 'not closed',            !!!parse-error (type => 'not closed',
4414                            value => $self->{open_elements}->[-1]->[0]                            text => $self->{open_elements}->[-1]->[0]
4415                                ->manakai_local_name,                                ->manakai_local_name,
4416                            token => $token);                            token => $token);
4417    
# Line 4074  sub _tree_construction_main ($) { Line 4487  sub _tree_construction_main ($) {
4487          !!!cp ('t87.5');          !!!cp ('t87.5');
4488          #          #
4489        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
         ## NOTE: "using the rules for secondary insertion mode" then "continue"  
4490          !!!cp ('t87.6');          !!!cp ('t87.6');
4491          #          !!!parse-error (type => 'not closed',
4492          ## TODO: ...                          text => $self->{open_elements}->[-1]->[0]
4493                                ->manakai_local_name,
4494                            token => $token);
4495    
4496            pop @{$self->{open_elements}}
4497                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4498    
4499            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4500            ## Reprocess.
4501            next B;
4502        } else {        } else {
4503          die "$0: $token->{type}: Unknown token type";                  die "$0: $token->{type}: Unknown token type";        
4504        }        }
# Line 4089  sub _tree_construction_main ($) { Line 4510  sub _tree_construction_main ($) {
4510            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4511              !!!cp ('t88.2');              !!!cp ('t88.2');
4512              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4513                #
4514            } else {            } else {
4515              !!!cp ('t88.1');              !!!cp ('t88.1');
4516              ## Ignore the token.              ## Ignore the token.
4517              !!!next-token;              #
             next B;  
4518            }            }
4519            unless (length $token->{data}) {            unless (length $token->{data}) {
4520              !!!cp ('t88');              !!!cp ('t88');
4521              !!!next-token;              !!!next-token;
4522              next B;              next B;
4523            }            }
4524    ## TODO: set $token->{column} appropriately
4525          }          }
4526    
4527          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
# Line 4118  sub _tree_construction_main ($) { Line 4540  sub _tree_construction_main ($) {
4540            !!!cp ('t90');            !!!cp ('t90');
4541            ## As if </noscript>            ## As if </noscript>
4542            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4543            !!!parse-error (type => 'in noscript:#character', token => $token);            !!!parse-error (type => 'in noscript:#text', token => $token);
4544                        
4545            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4546            ## As if </head>            ## As if </head>
# Line 4155  sub _tree_construction_main ($) { Line 4577  sub _tree_construction_main ($) {
4577              next B;              next B;
4578            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4579              !!!cp ('t93.2');              !!!cp ('t93.2');
4580              !!!parse-error (type => 'after head:head', token => $token); ## TODO: error type              !!!parse-error (type => 'after head', text => 'head',
4581                                token => $token);
4582              ## Ignore the token              ## Ignore the token
4583              !!!nack ('t93.3');              !!!nack ('t93.3');
4584              !!!next-token;              !!!next-token;
4585              next B;              next B;
4586            } else {            } else {
4587              !!!cp ('t95');              !!!cp ('t95');
4588              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript              !!!parse-error (type => 'in head:head',
4589                                token => $token); # or in head noscript
4590              ## Ignore the token              ## Ignore the token
4591              !!!nack ('t95.1');              !!!nack ('t95.1');
4592              !!!next-token;              !!!next-token;
# Line 4187  sub _tree_construction_main ($) { Line 4611  sub _tree_construction_main ($) {
4611                  !!!cp ('t98');                  !!!cp ('t98');
4612                  ## As if </noscript>                  ## As if </noscript>
4613                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4614                  !!!parse-error (type => 'in noscript:base', token => $token);                  !!!parse-error (type => 'in noscript', text => 'base',
4615                                    token => $token);
4616                                
4617                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4618                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 4198  sub _tree_construction_main ($) { Line 4623  sub _tree_construction_main ($) {
4623                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4624                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4625                  !!!cp ('t100');                  !!!cp ('t100');
4626                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4627                                    text => $token->{tag_name}, token => $token);
4628                  push @{$self->{open_elements}},                  push @{$self->{open_elements}},
4629                      [$self->{head_element}, $el_category->{head}];                      [$self->{head_element}, $el_category->{head}];
4630                } else {                } else {
# Line 4215  sub _tree_construction_main ($) { Line 4641  sub _tree_construction_main ($) {
4641                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4642                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4643                  !!!cp ('t102');                  !!!cp ('t102');
4644                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4645                                    text => $token->{tag_name}, token => $token);
4646                  push @{$self->{open_elements}},                  push @{$self->{open_elements}},
4647                      [$self->{head_element}, $el_category->{head}];                      [$self->{head_element}, $el_category->{head}];
4648                } else {                } else {
# Line 4232  sub _tree_construction_main ($) { Line 4659  sub _tree_construction_main ($) {
4659                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4660                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4661                  !!!cp ('t104');                  !!!cp ('t104');
4662                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4663                                    text => $token->{tag_name}, token => $token);
4664                  push @{$self->{open_elements}},                  push @{$self->{open_elements}},
4665                      [$self->{head_element}, $el_category->{head}];                      [$self->{head_element}, $el_category->{head}];
4666                } else {                } else {
# Line 4256  sub _tree_construction_main ($) { Line 4684  sub _tree_construction_main ($) {
4684                                                 ->{has_reference});                                                 ->{has_reference});
4685                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
4686                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4687                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4688                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4689                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4690                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4691                      !!!cp ('t107');                      !!!cp ('t107');
4692                      ## NOTE: Whether the encoding is supported or not is handled                      ## NOTE: Whether the encoding is supported or not is handled
4693                      ## in the {change_encoding} callback.                      ## in the {change_encoding} callback.
# Line 4301  sub _tree_construction_main ($) { Line 4729  sub _tree_construction_main ($) {
4729                  !!!cp ('t111');                  !!!cp ('t111');
4730                  ## As if </noscript>                  ## As if </noscript>
4731                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4732                  !!!parse-error (type => 'in noscript:title', token => $token);                  !!!parse-error (type => 'in noscript', text => 'title',
4733                                    token => $token);
4734                                
4735                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4736                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4737                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4738                  !!!cp ('t112');                  !!!cp ('t112');
4739                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4740                                    text => $token->{tag_name}, token => $token);
4741                  push @{$self->{open_elements}},                  push @{$self->{open_elements}},
4742                      [$self->{head_element}, $el_category->{head}];                      [$self->{head_element}, $el_category->{head}];
4743                } else {                } else {
# Line 4321  sub _tree_construction_main ($) { Line 4751  sub _tree_construction_main ($) {
4751                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4752                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4753                next B;                next B;
4754              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style' or
4755                         $token->{tag_name} eq 'noframes') {
4756                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4757                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4758                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4759                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4760                  !!!cp ('t114');                  !!!cp ('t114');
4761                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4762                                    text => $token->{tag_name}, token => $token);
4763                  push @{$self->{open_elements}},                  push @{$self->{open_elements}},
4764                      [$self->{head_element}, $el_category->{head}];                      [$self->{head_element}, $el_category->{head}];
4765                } else {                } else {
# Line 4348  sub _tree_construction_main ($) { Line 4780  sub _tree_construction_main ($) {
4780                  next B;                  next B;
4781                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4782                  !!!cp ('t117');                  !!!cp ('t117');
4783                  !!!parse-error (type => 'in noscript:noscript', token => $token);                  !!!parse-error (type => 'in noscript', text => 'noscript',
4784                                    token => $token);
4785                  ## Ignore the token                  ## Ignore the token
4786                  !!!nack ('t117.1');                  !!!nack ('t117.1');
4787                  !!!next-token;                  !!!next-token;
# Line 4362  sub _tree_construction_main ($) { Line 4795  sub _tree_construction_main ($) {
4795                  !!!cp ('t119');                  !!!cp ('t119');
4796                  ## As if </noscript>                  ## As if </noscript>
4797                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4798                  !!!parse-error (type => 'in noscript:script', token => $token);                  !!!parse-error (type => 'in noscript', text => 'script',
4799                                    token => $token);
4800                                
4801                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4802                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4803                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4804                  !!!cp ('t120');                  !!!cp ('t120');
4805                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4806                                    text => $token->{tag_name}, token => $token);
4807                  push @{$self->{open_elements}},                  push @{$self->{open_elements}},
4808                      [$self->{head_element}, $el_category->{head}];                      [$self->{head_element}, $el_category->{head}];
4809                } else {                } else {
# Line 4386  sub _tree_construction_main ($) { Line 4821  sub _tree_construction_main ($) {
4821                  !!!cp ('t122');                  !!!cp ('t122');
4822                  ## As if </noscript>                  ## As if </noscript>
4823                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4824                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'in noscript',
4825                                    text => $token->{tag_name}, token => $token);
4826                                    
4827                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4828                  ## As if </head>                  ## As if </head>
# Line 4425  sub _tree_construction_main ($) { Line 4861  sub _tree_construction_main ($) {
4861                !!!cp ('t129');                !!!cp ('t129');
4862                ## As if </noscript>                ## As if </noscript>
4863                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4864                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'in noscript:/',
4865                                  text => $token->{tag_name}, token => $token);
4866                                
4867                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4868                ## As if </head>                ## As if </head>
# Line 4468  sub _tree_construction_main ($) { Line 4905  sub _tree_construction_main ($) {
4905                  !!!cp ('t133');                  !!!cp ('t133');
4906                  ## As if </noscript>                  ## As if </noscript>
4907                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4908                  !!!parse-error (type => 'in noscript:/head', token => $token);                  !!!parse-error (type => 'in noscript:/',
4909                                    text => 'head', token => $token);
4910                                    
4911                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4912                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4483  sub _tree_construction_main ($) { Line 4921  sub _tree_construction_main ($) {
4921                  next B;                  next B;
4922                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4923                  !!!cp ('t134.1');                  !!!cp ('t134.1');
4924                  !!!parse-error (type => 'unmatched end tag:head', token => $token);                  !!!parse-error (type => 'unmatched end tag', text => 'head',
4925                                    token => $token);
4926                  ## Ignore the token                  ## Ignore the token
4927                  !!!next-token;                  !!!next-token;
4928                  next B;                  next B;
# Line 4500  sub _tree_construction_main ($) { Line 4939  sub _tree_construction_main ($) {
4939                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4940                         $self->{insertion_mode} == AFTER_HEAD_IM) {                         $self->{insertion_mode} == AFTER_HEAD_IM) {
4941                  !!!cp ('t137');                  !!!cp ('t137');
4942                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);                  !!!parse-error (type => 'unmatched end tag',
4943                                    text => 'noscript', token => $token);
4944                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4945                  !!!next-token;                  !!!next-token;
4946                  next B;                  next B;
# Line 4515  sub _tree_construction_main ($) { Line 4955  sub _tree_construction_main ($) {
4955                    $self->{insertion_mode} == IN_HEAD_IM or                    $self->{insertion_mode} == IN_HEAD_IM or
4956                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4957                  !!!cp ('t140');                  !!!cp ('t140');
4958                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
4959                                    text => $token->{tag_name}, token => $token);
4960                  ## Ignore the token                  ## Ignore the token
4961                  !!!next-token;                  !!!next-token;
4962                  next B;                  next B;
4963                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4964                  !!!cp ('t140.1');                  !!!cp ('t140.1');
4965                  !!!parse-error (type => 'unmatched end tag:' . $token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
4966                                    text => $token->{tag_name}, token => $token);
4967                  ## Ignore the token                  ## Ignore the token
4968                  !!!next-token;                  !!!next-token;
4969                  next B;                  next B;
# Line 4530  sub _tree_construction_main ($) { Line 4972  sub _tree_construction_main ($) {
4972                }                }
4973              } elsif ($token->{tag_name} eq 'p') {              } elsif ($token->{tag_name} eq 'p') {
4974                !!!cp ('t142');                !!!cp ('t142');
4975                !!!parse-error (type => 'unmatched end tag:p', token => $token);                !!!parse-error (type => 'unmatched end tag',
4976                                  text => $token->{tag_name}, token => $token);
4977                ## Ignore the token                ## Ignore the token
4978                !!!next-token;                !!!next-token;
4979                next B;                next B;
# Line 4553  sub _tree_construction_main ($) { Line 4996  sub _tree_construction_main ($) {
4996                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4997                  !!!cp ('t143.3');                  !!!cp ('t143.3');
4998                  ## ISSUE: Two parse errors for <head><noscript></br>                  ## ISSUE: Two parse errors for <head><noscript></br>
4999                  !!!parse-error (type => 'unmatched end tag:br', token => $token);                  !!!parse-error (type => 'unmatched end tag',
5000                                    text => 'br', token => $token);
5001                  ## As if </noscript>                  ## As if </noscript>
5002                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5003                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
# Line 4572  sub _tree_construction_main ($) { Line 5016  sub _tree_construction_main ($) {
5016                }                }
5017    
5018                ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.                ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
5019                !!!parse-error (type => 'unmatched end tag:br', token => $token);                !!!parse-error (type => 'unmatched end tag',
5020                                  text => 'br', token => $token);
5021                ## Ignore the token                ## Ignore the token
5022                !!!next-token;                !!!next-token;
5023                next B;                next B;
5024              } else {              } else {
5025                !!!cp ('t145');                !!!cp ('t145');
5026                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
5027                                  text => $token->{tag_name}, token => $token);
5028                ## Ignore the token                ## Ignore the token
5029                !!!next-token;                !!!next-token;
5030                next B;                next B;
# Line 4588  sub _tree_construction_main ($) { Line 5034  sub _tree_construction_main ($) {
5034                !!!cp ('t146');                !!!cp ('t146');
5035                ## As if </noscript>                ## As if </noscript>
5036                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5037                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'in noscript:/',
5038                                  text => $token->{tag_name}, token => $token);
5039                                
5040                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
5041                ## As if </head>                ## As if </head>
# Line 4604  sub _tree_construction_main ($) { Line 5051  sub _tree_construction_main ($) {
5051              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5052  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
5053                !!!cp ('t148');                !!!cp ('t148');
5054                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
5055                                  text => $token->{tag_name}, token => $token);
5056                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
5057                !!!next-token;                !!!next-token;
5058                next B;                next B;
# Line 4715  sub _tree_construction_main ($) { Line 5163  sub _tree_construction_main ($) {
5163    
5164                  !!!cp ('t153');                  !!!cp ('t153');
5165                  !!!parse-error (type => 'start tag not allowed',                  !!!parse-error (type => 'start tag not allowed',
5166                      value => $token->{tag_name}, token => $token);                      text => $token->{tag_name}, token => $token);
5167                  ## Ignore the token                  ## Ignore the token
5168                  !!!nack ('t153.1');                  !!!nack ('t153.1');
5169                  !!!next-token;                  !!!next-token;
5170                  next B;                  next B;
5171                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5172                  !!!parse-error (type => 'not closed:caption', token => $token);                  !!!parse-error (type => 'not closed', text => 'caption',
5173                                    token => $token);
5174                                    
5175                  ## NOTE: As if </caption>.                  ## NOTE: As if </caption>.
5176                  ## have a table element in table scope                  ## have a table element in table scope
# Line 4741  sub _tree_construction_main ($) { Line 5190  sub _tree_construction_main ($) {
5190    
5191                    !!!cp ('t157');                    !!!cp ('t157');
5192                    !!!parse-error (type => 'start tag not allowed',                    !!!parse-error (type => 'start tag not allowed',
5193                                    value => $token->{tag_name}, token => $token);                                    text => $token->{tag_name}, token => $token);
5194                    ## Ignore the token                    ## Ignore the token
5195                    !!!nack ('t157.1');                    !!!nack ('t157.1');
5196                    !!!next-token;                    !!!next-token;
# Line 4758  sub _tree_construction_main ($) { Line 5207  sub _tree_construction_main ($) {
5207                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5208                    !!!cp ('t159');                    !!!cp ('t159');
5209                    !!!parse-error (type => 'not closed',                    !!!parse-error (type => 'not closed',
5210                                    value => $self->{open_elements}->[-1]->[0]                                    text => $self->{open_elements}->[-1]->[0]
5211                                        ->manakai_local_name,                                        ->manakai_local_name,
5212                                    token => $token);                                    token => $token);
5213                  } else {                  } else {
# Line 4800  sub _tree_construction_main ($) { Line 5249  sub _tree_construction_main ($) {
5249                  } # INSCOPE                  } # INSCOPE
5250                    unless (defined $i) {                    unless (defined $i) {
5251                      !!!cp ('t165');                      !!!cp ('t165');
5252                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag',
5253                                        text => $token->{tag_name},
5254                                        token => $token);
5255                      ## Ignore the token                      ## Ignore the token
5256                      !!!next-token;                      !!!next-token;
5257                      next B;                      next B;
# Line 4817  sub _tree_construction_main ($) { Line 5268  sub _tree_construction_main ($) {
5268                          ne $token->{tag_name}) {                          ne $token->{tag_name}) {
5269                    !!!cp ('t167');                    !!!cp ('t167');
5270                    !!!parse-error (type => 'not closed',                    !!!parse-error (type => 'not closed',
5271                                    value => $self->{open_elements}->[-1]->[0]                                    text => $self->{open_elements}->[-1]->[0]
5272                                        ->manakai_local_name,                                        ->manakai_local_name,
5273                                    token => $token);                                    token => $token);
5274                  } else {                  } else {
# Line 4834  sub _tree_construction_main ($) { Line 5285  sub _tree_construction_main ($) {
5285                  next B;                  next B;
5286                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5287                  !!!cp ('t169');                  !!!cp ('t169');
5288                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5289                                    text => $token->{tag_name}, token => $token);
5290                  ## Ignore the token                  ## Ignore the token
5291                  !!!next-token;                  !!!next-token;
5292                  next B;                  next B;
# Line 4861  sub _tree_construction_main ($) { Line 5313  sub _tree_construction_main ($) {
5313    
5314                    !!!cp ('t173');                    !!!cp ('t173');
5315                    !!!parse-error (type => 'unmatched end tag',                    !!!parse-error (type => 'unmatched end tag',
5316                                    value => $token->{tag_name}, token => $token);                                    text => $token->{tag_name}, token => $token);
5317                    ## Ignore the token                    ## Ignore the token
5318                    !!!next-token;                    !!!next-token;
5319                    next B;                    next B;
# Line 4877  sub _tree_construction_main ($) { Line 5329  sub _tree_construction_main ($) {
5329                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5330                    !!!cp ('t175');                    !!!cp ('t175');
5331                    !!!parse-error (type => 'not closed',                    !!!parse-error (type => 'not closed',
5332                                    value => $self->{open_elements}->[-1]->[0]                                    text => $self->{open_elements}->[-1]->[0]
5333                                        ->manakai_local_name,                                        ->manakai_local_name,
5334                                    token => $token);                                    token => $token);
5335                  } else {                  } else {
# Line 4894  sub _tree_construction_main ($) { Line 5346  sub _tree_construction_main ($) {
5346                  next B;                  next B;
5347                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5348                  !!!cp ('t177');                  !!!cp ('t177');
5349                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5350                                    text => $token->{tag_name}, token => $token);
5351                  ## Ignore the token                  ## Ignore the token
5352                  !!!next-token;                  !!!next-token;
5353                  next B;                  next B;
# Line 4937  sub _tree_construction_main ($) { Line 5390  sub _tree_construction_main ($) {
5390    
5391                  !!!cp ('t182');                  !!!cp ('t182');
5392                  !!!parse-error (type => 'unmatched end tag',                  !!!parse-error (type => 'unmatched end tag',
5393                      value => $token->{tag_name}, token => $token);                      text => $token->{tag_name}, token => $token);
5394                  ## Ignore the token                  ## Ignore the token
5395                  !!!next-token;                  !!!next-token;
5396                  next B;                  next B;
5397                } # INSCOPE                } # INSCOPE
5398              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5399                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5400                !!!parse-error (type => 'not closed:caption', token => $token);                !!!parse-error (type => 'not closed', text => 'caption',
5401                                  token => $token);
5402    
5403                ## As if </caption>                ## As if </caption>
5404                ## have a table element in table scope                ## have a table element in table scope
# Line 4962  sub _tree_construction_main ($) { Line 5416  sub _tree_construction_main ($) {
5416                } # INSCOPE                } # INSCOPE
5417                unless (defined $i) {                unless (defined $i) {
5418                  !!!cp ('t186');                  !!!cp ('t186');
5419                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);                  !!!parse-error (type => 'unmatched end tag',
5420                                    text => 'caption', token => $token);
5421                  ## Ignore the token                  ## Ignore the token
5422                  !!!next-token;                  !!!next-token;
5423                  next B;                  next B;
# Line 4977  sub _tree_construction_main ($) { Line 5432  sub _tree_construction_main ($) {
5432                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5433                  !!!cp ('t188');                  !!!cp ('t188');
5434                  !!!parse-error (type => 'not closed',                  !!!parse-error (type => 'not closed',
5435                                  value => $self->{open_elements}->[-1]->[0]                                  text => $self->{open_elements}->[-1]->[0]
5436                                      ->manakai_local_name,                                      ->manakai_local_name,
5437                                  token => $token);                                  token => $token);
5438                } else {                } else {
# Line 4997  sub _tree_construction_main ($) { Line 5452  sub _tree_construction_main ($) {
5452                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5453                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5454                  !!!cp ('t190');                  !!!cp ('t190');
5455                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5456                                    text => $token->{tag_name}, token => $token);
5457                  ## Ignore the token                  ## Ignore the token
5458                  !!!next-token;                  !!!next-token;
5459                  next B;                  next B;
# Line 5011  sub _tree_construction_main ($) { Line 5467  sub _tree_construction_main ($) {
5467                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5468                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5469                !!!cp ('t192');                !!!cp ('t192');
5470                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
5471                                  text => $token->{tag_name}, token => $token);
5472                ## Ignore the token                ## Ignore the token
5473                !!!next-token;                !!!next-token;
5474                next B;                next B;
# Line 5051  sub _tree_construction_main ($) { Line 5508  sub _tree_construction_main ($) {
5508            }            }
5509          }          }
5510    
5511              !!!parse-error (type => 'in table:#character', token => $token);          !!!parse-error (type => 'in table:#text', token => $token);
5512    
5513              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5514              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 5102  sub _tree_construction_main ($) { Line 5559  sub _tree_construction_main ($) {
5559          !!!next-token;          !!!next-token;
5560          next B;          next B;
5561        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5562              if ({          if ({
5563                   tr => ($self->{insertion_mode} != IN_ROW_IM),               tr => ($self->{insertion_mode} != IN_ROW_IM),
5564                   th => 1, td => 1,               th => 1, td => 1,
5565                  }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
5566                if ($self->{insertion_mode} == IN_TABLE_IM) {            if ($self->{insertion_mode} == IN_TABLE_IM) {
5567                  ## Clear back to table context              ## Clear back to table context
5568                  while (not ($self->{open_elements}->[-1]->[1]              while (not ($self->{open_elements}->[-1]->[1]
5569                                  & TABLE_SCOPING_EL)) {                              & TABLE_SCOPING_EL)) {
5570                    !!!cp ('t201');                !!!cp ('t201');
5571                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5572                  }              }
5573                                
5574                  !!!insert-element ('tbody',, $token);              !!!insert-element ('tbody',, $token);
5575                  $self->{insertion_mode} = IN_TABLE_BODY_IM;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5576                  ## reprocess in the "in table body" insertion mode...              ## reprocess in the "in table body" insertion mode...
5577                }            }
5578              
5579                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5580                  unless ($token->{tag_name} eq 'tr') {              unless ($token->{tag_name} eq 'tr') {
5581                    !!!cp ('t202');                !!!cp ('t202');
5582                    !!!parse-error (type => 'missing start tag:tr', token => $token);                !!!parse-error (type => 'missing start tag:tr', token => $token);
5583                  }              }
5584                                    
5585                  ## Clear back to table body context              ## Clear back to table body context
5586                  while (not ($self->{open_elements}->[-1]->[1]              while (not ($self->{open_elements}->[-1]->[1]
5587                                  & TABLE_ROWS_SCOPING_EL)) {                              & TABLE_ROWS_SCOPING_EL)) {
5588                    !!!cp ('t203');                !!!cp ('t203');
5589                    ## ISSUE: Can this case be reached?                ## ISSUE: Can this case be reached?
5590                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5591                  }              }
5592                                    
5593                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5594                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
# Line 5187  sub _tree_construction_main ($) { Line 5644  sub _tree_construction_main ($) {
5644                  unless (defined $i) {                  unless (defined $i) {
5645                    !!!cp ('t210');                    !!!cp ('t210');
5646  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5647                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmacthed end tag',
5648                                      text => $token->{tag_name}, token => $token);
5649                    ## Ignore the token                    ## Ignore the token
5650                    !!!nack ('t210.1');                    !!!nack ('t210.1');
5651                    !!!next-token;                    !!!next-token;
# Line 5231  sub _tree_construction_main ($) { Line 5689  sub _tree_construction_main ($) {
5689                  } # INSCOPE                  } # INSCOPE
5690                  unless (defined $i) {                  unless (defined $i) {
5691                    !!!cp ('t216');                    !!!cp ('t216');
5692  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type is wrong.
5693                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5694                                      text => $token->{tag_name}, token => $token);
5695                    ## Ignore the token                    ## Ignore the token
5696                    !!!nack ('t216.1');                    !!!nack ('t216.1');
5697                    !!!next-token;                    !!!next-token;
# Line 5307  sub _tree_construction_main ($) { Line 5766  sub _tree_construction_main ($) {
5766                }                }
5767              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5768                !!!parse-error (type => 'not closed',                !!!parse-error (type => 'not closed',
5769                                value => $self->{open_elements}->[-1]->[0]                                text => $self->{open_elements}->[-1]->[0]
5770                                    ->manakai_local_name,                                    ->manakai_local_name,
5771                                token => $token);                                token => $token);
5772    
# Line 5328  sub _tree_construction_main ($) { Line 5787  sub _tree_construction_main ($) {
5787                unless (defined $i) {                unless (defined $i) {
5788                  !!!cp ('t223');                  !!!cp ('t223');
5789  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5790                  !!!parse-error (type => 'unmatched end tag:table', token => $token);                  !!!parse-error (type => 'unmatched end tag', text => 'table',
5791                                    token => $token);
5792                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5793                  !!!nack ('t223.1');                  !!!nack ('t223.1');
5794                  !!!next-token;                  !!!next-token;
5795                  next B;                  next B;
5796                }                }
5797                                
5798  ## TODO: Followings are removed from the latest spec.  ## TODO: Followings are removed from the latest spec.
5799                ## generate implied end tags                ## generate implied end tags
5800                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5801                  !!!cp ('t224');                  !!!cp ('t224');
# Line 5346  sub _tree_construction_main ($) { Line 5806  sub _tree_construction_main ($) {
5806                  !!!cp ('t225');                  !!!cp ('t225');
5807                  ## NOTE: |<table><tr><table>|                  ## NOTE: |<table><tr><table>|
5808                  !!!parse-error (type => 'not closed',                  !!!parse-error (type => 'not closed',
5809                                  value => $self->{open_elements}->[-1]->[0]                                  text => $self->{open_elements}->[-1]->[0]
5810                                      ->manakai_local_name,                                      ->manakai_local_name,
5811                                  token => $token);                                  token => $token);
5812                } else {                } else {
# Line 5387  sub _tree_construction_main ($) { Line 5847  sub _tree_construction_main ($) {
5847                my $type = lc $token->{attributes}->{type}->{value};                my $type = lc $token->{attributes}->{type}->{value};
5848                if ($type eq 'hidden') {                if ($type eq 'hidden') {
5849                  !!!cp ('t227.3');                  !!!cp ('t227.3');
5850                  !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'in table',
5851                                    text => $token->{tag_name}, token => $token);
5852    
5853                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5854    
# Line 5415  sub _tree_construction_main ($) { Line 5876  sub _tree_construction_main ($) {
5876            #            #
5877          }          }
5878    
5879          !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'in table', text => $token->{tag_name},
5880                            token => $token);
5881    
5882          $insert = $insert_to_foster;          $insert = $insert_to_foster;
5883          #          #
# Line 5437  sub _tree_construction_main ($) { Line 5899  sub _tree_construction_main ($) {
5899                } # INSCOPE                } # INSCOPE
5900                unless (defined $i) {                unless (defined $i) {
5901                  !!!cp ('t230');                  !!!cp ('t230');
5902                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5903                                    text => $token->{tag_name}, token => $token);
5904                  ## Ignore the token                  ## Ignore the token
5905                  !!!nack ('t230.1');                  !!!nack ('t230.1');
5906                  !!!next-token;                  !!!next-token;
# Line 5478  sub _tree_construction_main ($) { Line 5941  sub _tree_construction_main ($) {
5941                  unless (defined $i) {                  unless (defined $i) {
5942                    !!!cp ('t235');                    !!!cp ('t235');
5943  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5944                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5945                                      text => $token->{type}, token => $token);
5946                    ## Ignore the token                    ## Ignore the token
5947                    !!!nack ('t236.1');                    !!!nack ('t236.1');
5948                    !!!next-token;                    !!!next-token;
# Line 5514  sub _tree_construction_main ($) { Line 5978  sub _tree_construction_main ($) {
5978                  } # INSCOPE                  } # INSCOPE
5979                  unless (defined $i) {                  unless (defined $i) {
5980                    !!!cp ('t239');                    !!!cp ('t239');
5981                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5982                                      text => $token->{tag_name}, token => $token);
5983                    ## Ignore the token                    ## Ignore the token
5984                    !!!nack ('t239.1');                    !!!nack ('t239.1');
5985                    !!!next-token;                    !!!next-token;
# Line 5560  sub _tree_construction_main ($) { Line 6025  sub _tree_construction_main ($) {
6025                } # INSCOPE                } # INSCOPE
6026                unless (defined $i) {                unless (defined $i) {
6027                  !!!cp ('t243');                  !!!cp ('t243');
6028                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
6029                                    text => $token->{tag_name}, token => $token);
6030                  ## Ignore the token                  ## Ignore the token
6031                  !!!nack ('t243.1');                  !!!nack ('t243.1');
6032                  !!!next-token;                  !!!next-token;
# Line 5594  sub _tree_construction_main ($) { Line 6060  sub _tree_construction_main ($) {
6060                  } # INSCOPE                  } # INSCOPE
6061                    unless (defined $i) {                    unless (defined $i) {
6062                      !!!cp ('t249');                      !!!cp ('t249');
6063                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag',
6064                                        text => $token->{tag_name}, token => $token);
6065                      ## Ignore the token                      ## Ignore the token
6066                      !!!nack ('t249.1');                      !!!nack ('t249.1');
6067                      !!!next-token;                      !!!next-token;
# Line 5617  sub _tree_construction_main ($) { Line 6084  sub _tree_construction_main ($) {
6084                  } # INSCOPE                  } # INSCOPE
6085                    unless (defined $i) {                    unless (defined $i) {
6086                      !!!cp ('t252');                      !!!cp ('t252');
6087                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);                      !!!parse-error (type => 'unmatched end tag',
6088                                        text => 'tr', token => $token);
6089                      ## Ignore the token                      ## Ignore the token
6090                      !!!nack ('t252.1');                      !!!nack ('t252.1');
6091                      !!!next-token;                      !!!next-token;
# Line 5652  sub _tree_construction_main ($) { Line 6120  sub _tree_construction_main ($) {
6120                } # INSCOPE                } # INSCOPE
6121                unless (defined $i) {                unless (defined $i) {
6122                  !!!cp ('t256');                  !!!cp ('t256');
6123                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
6124                                    text => $token->{tag_name}, token => $token);
6125                  ## Ignore the token                  ## Ignore the token
6126                  !!!nack ('t256.1');                  !!!nack ('t256.1');
6127                  !!!next-token;                  !!!next-token;
# Line 5679  sub _tree_construction_main ($) { Line 6148  sub _tree_construction_main ($) {
6148                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
6149                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
6150            !!!cp ('t258');            !!!cp ('t258');
6151            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
6152                              text => $token->{tag_name}, token => $token);
6153            ## Ignore the token            ## Ignore the token
6154            !!!nack ('t258.1');            !!!nack ('t258.1');
6155             !!!next-token;             !!!next-token;
6156            next B;            next B;
6157          } else {          } else {
6158            !!!cp ('t259');            !!!cp ('t259');
6159            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in table:/',
6160                              text => $token->{tag_name}, token => $token);
6161    
6162            $insert = $insert_to_foster;            $insert = $insert_to_foster;
6163            #            #
# Line 5736  sub _tree_construction_main ($) { Line 6207  sub _tree_construction_main ($) {
6207              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
6208                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6209                  !!!cp ('t264');                  !!!cp ('t264');
6210                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);                  !!!parse-error (type => 'unmatched end tag',
6211                                    text => 'colgroup', token => $token);
6212                  ## Ignore the token                  ## Ignore the token
6213                  !!!next-token;                  !!!next-token;
6214                  next B;                  next B;
# Line 5749  sub _tree_construction_main ($) { Line 6221  sub _tree_construction_main ($) {
6221                }                }
6222              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
6223                !!!cp ('t266');                !!!cp ('t266');
6224                !!!parse-error (type => 'unmatched end tag:col', token => $token);                !!!parse-error (type => 'unmatched end tag',
6225                                  text => 'col', token => $token);
6226                ## Ignore the token                ## Ignore the token
6227                !!!next-token;                !!!next-token;
6228                next B;                next B;
# Line 5779  sub _tree_construction_main ($) { Line 6252  sub _tree_construction_main ($) {
6252            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6253              !!!cp ('t269');              !!!cp ('t269');
6254  ## TODO: Wrong error type?  ## TODO: Wrong error type?
6255              !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);              !!!parse-error (type => 'unmatched end tag',
6256                                text => 'colgroup', token => $token);
6257              ## Ignore the token              ## Ignore the token
6258              !!!nack ('t269.1');              !!!nack ('t269.1');
6259              !!!next-token;              !!!next-token;
# Line 5833  sub _tree_construction_main ($) { Line 6307  sub _tree_construction_main ($) {
6307            !!!nack ('t277.1');            !!!nack ('t277.1');
6308            !!!next-token;            !!!next-token;
6309            next B;            next B;
6310          } elsif ($token->{tag_name} eq 'select' or          } elsif ({
6311                   $token->{tag_name} eq 'input' or                     select => 1, input => 1, textarea => 1,
6312                     }->{$token->{tag_name}} or
6313                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6314                    {                    {
6315                     caption => 1, table => 1,                     caption => 1, table => 1,
# Line 5842  sub _tree_construction_main ($) { Line 6317  sub _tree_construction_main ($) {
6317                     tr => 1, td => 1, th => 1,                     tr => 1, td => 1, th => 1,
6318                    }->{$token->{tag_name}})) {                    }->{$token->{tag_name}})) {
6319            ## TODO: The type below is not good - <select> is replaced by </select>            ## TODO: The type below is not good - <select> is replaced by </select>
6320            !!!parse-error (type => 'not closed:select', token => $token);            !!!parse-error (type => 'not closed', text => 'select',
6321                              token => $token);
6322            ## NOTE: As if the token were </select> (<select> case) or            ## NOTE: As if the token were </select> (<select> case) or
6323            ## as if there were </select> (otherwise).            ## as if there were </select> (otherwise).
6324            ## have an element in table scope            ## have an element in table scope
# Line 5860  sub _tree_construction_main ($) { Line 6336  sub _tree_construction_main ($) {
6336            } # INSCOPE            } # INSCOPE
6337            unless (defined $i) {            unless (defined $i) {
6338              !!!cp ('t280');              !!!cp ('t280');
6339              !!!parse-error (type => 'unmatched end tag:select', token => $token);              !!!parse-error (type => 'unmatched end tag',
6340                                text => 'select', token => $token);
6341              ## Ignore the token              ## Ignore the token
6342              !!!nack ('t280.1');              !!!nack ('t280.1');
6343              !!!next-token;              !!!next-token;
# Line 5884  sub _tree_construction_main ($) { Line 6361  sub _tree_construction_main ($) {
6361            }            }
6362          } else {          } else {
6363            !!!cp ('t282');            !!!cp ('t282');
6364            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select',
6365                              text => $token->{tag_name}, token => $token);
6366            ## Ignore the token            ## Ignore the token
6367            !!!nack ('t282.1');            !!!nack ('t282.1');
6368            !!!next-token;            !!!next-token;
# Line 5902  sub _tree_construction_main ($) { Line 6380  sub _tree_construction_main ($) {
6380              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6381            } else {            } else {
6382              !!!cp ('t285');              !!!cp ('t285');
6383              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6384                                text => $token->{tag_name}, token => $token);
6385              ## Ignore the token              ## Ignore the token
6386            }            }
6387            !!!nack ('t285.1');            !!!nack ('t285.1');
# Line 5914  sub _tree_construction_main ($) { Line 6393  sub _tree_construction_main ($) {
6393              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6394            } else {            } else {
6395              !!!cp ('t287');              !!!cp ('t287');
6396              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6397                                text => $token->{tag_name}, token => $token);
6398              ## Ignore the token              ## Ignore the token
6399            }            }
6400            !!!nack ('t287.1');            !!!nack ('t287.1');
# Line 5936  sub _tree_construction_main ($) { Line 6416  sub _tree_construction_main ($) {
6416            } # INSCOPE            } # INSCOPE
6417            unless (defined $i) {            unless (defined $i) {
6418              !!!cp ('t290');              !!!cp ('t290');
6419              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6420                                text => $token->{tag_name}, token => $token);
6421              ## Ignore the token              ## Ignore the token
6422              !!!nack ('t290.1');              !!!nack ('t290.1');
6423              !!!next-token;              !!!next-token;
# Line 5957  sub _tree_construction_main ($) { Line 6438  sub _tree_construction_main ($) {
6438                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6439                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
6440  ## TODO: The following is wrong?  ## TODO: The following is wrong?
6441            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
6442                              text => $token->{tag_name}, token => $token);
6443                                
6444            ## have an element in table scope            ## have an element in table scope
6445            my $i;            my $i;
# Line 5998  sub _tree_construction_main ($) { Line 6480  sub _tree_construction_main ($) {
6480            unless (defined $i) {            unless (defined $i) {
6481              !!!cp ('t297');              !!!cp ('t297');
6482  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
6483              !!!parse-error (type => 'unmatched end tag:select', token => $token);              !!!parse-error (type => 'unmatched end tag',
6484                                text => 'select', token => $token);
6485              ## Ignore the </select> token              ## Ignore the </select> token
6486              !!!nack ('t297.1');              !!!nack ('t297.1');
6487              !!!next-token; ## TODO: ok?              !!!next-token; ## TODO: ok?
# Line 6015  sub _tree_construction_main ($) { Line 6498  sub _tree_construction_main ($) {
6498            next B;            next B;
6499          } else {          } else {
6500            !!!cp ('t299');            !!!cp ('t299');
6501            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select:/',
6502                              text => $token->{tag_name}, token => $token);
6503            ## Ignore the token            ## Ignore the token
6504            !!!nack ('t299.3');            !!!nack ('t299.3');
6505            !!!next-token;            !!!next-token;
# Line 6053  sub _tree_construction_main ($) { Line 6537  sub _tree_construction_main ($) {
6537                    
6538          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6539            !!!cp ('t301');            !!!cp ('t301');
6540            !!!parse-error (type => 'after html:#character', token => $token);            !!!parse-error (type => 'after html:#text', token => $token);
6541    
6542            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6543          } else {          } else {
# Line 6061  sub _tree_construction_main ($) { Line 6545  sub _tree_construction_main ($) {
6545          }          }
6546                    
6547          ## "after body" insertion mode          ## "after body" insertion mode
6548          !!!parse-error (type => 'after body:#character', token => $token);          !!!parse-error (type => 'after body:#text', token => $token);
6549    
6550          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6551          ## reprocess          ## reprocess
# Line 6069  sub _tree_construction_main ($) { Line 6553  sub _tree_construction_main ($) {
6553        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6554          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6555            !!!cp ('t303');            !!!cp ('t303');
6556            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after html',
6557                              text => $token->{tag_name}, token => $token);
6558                        
6559            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6560          } else {          } else {
# Line 6077  sub _tree_construction_main ($) { Line 6562  sub _tree_construction_main ($) {
6562          }          }
6563    
6564          ## "after body" insertion mode          ## "after body" insertion mode
6565          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'after body',
6566                            text => $token->{tag_name}, token => $token);
6567    
6568          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6569          !!!ack-later;          !!!ack-later;
# Line 6086  sub _tree_construction_main ($) { Line 6572  sub _tree_construction_main ($) {
6572        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6573          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6574            !!!cp ('t305');            !!!cp ('t305');
6575            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after html:/',
6576                              text => $token->{tag_name}, token => $token);
6577                        
6578            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6579            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
# Line 6098  sub _tree_construction_main ($) { Line 6585  sub _tree_construction_main ($) {
6585          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6586            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6587              !!!cp ('t307');              !!!cp ('t307');
6588              !!!parse-error (type => 'unmatched end tag:html', token => $token);              !!!parse-error (type => 'unmatched end tag',
6589                                text => 'html', token => $token);
6590              ## Ignore the token              ## Ignore the token
6591              !!!next-token;              !!!next-token;
6592              next B;              next B;
# Line 6110  sub _tree_construction_main ($) { Line 6598  sub _tree_construction_main ($) {
6598            }            }
6599          } else {          } else {
6600            !!!cp ('t309');            !!!cp ('t309');
6601            !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after body:/',
6602                              text => $token->{tag_name}, token => $token);
6603    
6604            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6605            ## reprocess            ## reprocess
# Line 6138  sub _tree_construction_main ($) { Line 6627  sub _tree_construction_main ($) {
6627          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6628            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6629              !!!cp ('t311');              !!!cp ('t311');
6630              !!!parse-error (type => 'in frameset:#character', token => $token);              !!!parse-error (type => 'in frameset:#text', token => $token);
6631            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6632              !!!cp ('t312');              !!!cp ('t312');
6633              !!!parse-error (type => 'after frameset:#character', token => $token);              !!!parse-error (type => 'after frameset:#text', token => $token);
6634            } else { # "after html frameset"            } else { # "after after frameset"
6635              !!!cp ('t313');              !!!cp ('t313');
6636              !!!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);  
6637            }            }
6638                        
6639            ## Ignore the token.            ## Ignore the token.
# Line 6164  sub _tree_construction_main ($) { Line 6649  sub _tree_construction_main ($) {
6649                    
6650          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6651        } 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');  
         }  
   
6652          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6653              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6654            !!!cp ('t318');            !!!cp ('t318');
# Line 6191  sub _tree_construction_main ($) { Line 6666  sub _tree_construction_main ($) {
6666            next B;            next B;
6667          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6668            !!!cp ('t320');            !!!cp ('t320');
6669            ## NOTE: As if in body.            ## NOTE: As if in head.
6670            $parse_rcdata->(CDATA_CONTENT_MODEL);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6671            next B;            next B;
6672    
6673              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6674              ## has no parse error.
6675          } else {          } else {
6676            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6677              !!!cp ('t321');              !!!cp ('t321');
6678              !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'in frameset',
6679            } else {                              text => $token->{tag_name}, token => $token);
6680              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6681              !!!cp ('t322');              !!!cp ('t322');
6682              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'after frameset',
6683                                text => $token->{tag_name}, token => $token);
6684              } else { # "after after frameset"
6685                !!!cp ('t322.2');
6686                !!!parse-error (type => 'after after frameset',
6687                                text => $token->{tag_name}, token => $token);
6688            }            }
6689            ## Ignore the token            ## Ignore the token
6690            !!!nack ('t322.1');            !!!nack ('t322.1');
# Line 6208  sub _tree_construction_main ($) { Line 6692  sub _tree_construction_main ($) {
6692            next B;            next B;
6693          }          }
6694        } 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');  
         }  
   
6695          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6696              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6697            if ($self->{open_elements}->[-1]->[1] & HTML_EL and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6698                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6699              !!!cp ('t325');              !!!cp ('t325');
6700              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6701                                text => $token->{tag_name}, token => $token);
6702              ## Ignore the token              ## Ignore the token
6703              !!!next-token;              !!!next-token;
6704            } else {            } else {
# Line 6249  sub _tree_construction_main ($) { Line 6724  sub _tree_construction_main ($) {
6724          } else {          } else {
6725            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6726              !!!cp ('t330');              !!!cp ('t330');
6727              !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'in frameset:/',
6728            } else {                              text => $token->{tag_name}, token => $token);
6729              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6730                !!!cp ('t330.1');
6731                !!!parse-error (type => 'after frameset:/',
6732                                text => $token->{tag_name}, token => $token);
6733              } else { # "after after html"
6734              !!!cp ('t331');              !!!cp ('t331');
6735              !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'after after frameset:/',
6736                                text => $token->{tag_name}, token => $token);
6737            }            }
6738            ## Ignore the token            ## Ignore the token
6739            !!!next-token;            !!!next-token;
# Line 6319  sub _tree_construction_main ($) { Line 6800  sub _tree_construction_main ($) {
6800                                           ->{has_reference});                                           ->{has_reference});
6801            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
6802              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6803                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6804                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6805                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6806                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6807                !!!cp ('t336');                !!!cp ('t336');
6808                ## NOTE: Whether the encoding is supported or not is handled                ## NOTE: Whether the encoding is supported or not is handled
6809                ## in the {change_encoding} callback.                ## in the {change_encoding} callback.
# Line 6360  sub _tree_construction_main ($) { Line 6841  sub _tree_construction_main ($) {
6841          $parse_rcdata->(RCDATA_CONTENT_MODEL);          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6842          next B;          next B;
6843        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6844          !!!parse-error (type => 'in body:body', token => $token);          !!!parse-error (type => 'in body', text => 'body', token => $token);
6845                                
6846          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6847              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
# Line 6480  sub _tree_construction_main ($) { Line 6961  sub _tree_construction_main ($) {
6961              if ($i != -1) {              if ($i != -1) {
6962                !!!cp ('t355');                !!!cp ('t355');
6963                !!!parse-error (type => 'not closed',                !!!parse-error (type => 'not closed',
6964                                value => $self->{open_elements}->[-1]->[0]                                text => $self->{open_elements}->[-1]->[0]
6965                                    ->manakai_local_name,                                    ->manakai_local_name,
6966                                token => $token);                                token => $token);
6967              } else {              } else {
# Line 6634  sub _tree_construction_main ($) { Line 7115  sub _tree_construction_main ($) {
7115                  xmp => 1,                  xmp => 1,
7116                  iframe => 1,                  iframe => 1,
7117                  noembed => 1,                  noembed => 1,
7118                  noframes => 1,                  noframes => 1, ## NOTE: This is an "as if in head" code clone.
7119                  noscript => 0, ## TODO: 1 if scripting is enabled                  noscript => 0, ## TODO: 1 if scripting is enabled
7120                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7121          if ($token->{tag_name} eq 'xmp') {          if ($token->{tag_name} eq 'xmp') {
# Line 6656  sub _tree_construction_main ($) { Line 7137  sub _tree_construction_main ($) {
7137            !!!next-token;            !!!next-token;
7138            next B;            next B;
7139          } else {          } else {
7140              !!!ack ('t391.1');
7141    
7142            my $at = $token->{attributes};            my $at = $token->{attributes};
7143            my $form_attrs;            my $form_attrs;
7144            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 6699  sub _tree_construction_main ($) { Line 7182  sub _tree_construction_main ($) {
7182                           line => $token->{line}, column => $token->{column}},                           line => $token->{line}, column => $token->{column}},
7183                          {type => END_TAG_TOKEN, tag_name => 'form',                          {type => END_TAG_TOKEN, tag_name => 'form',
7184                           line => $token->{line}, column => $token->{column}};                           line => $token->{line}, column => $token->{column}};
           !!!nack ('t391.1'); ## NOTE: Not acknowledged.  
7185            !!!back-token (@tokens);            !!!back-token (@tokens);
7186            !!!next-token;            !!!next-token;
7187            next B;            next B;
# Line 6747  sub _tree_construction_main ($) { Line 7229  sub _tree_construction_main ($) {
7229            ## Ignore the token            ## Ignore the token
7230          } else {          } else {
7231            !!!cp ('t398');            !!!cp ('t398');
7232            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
7233          }          }
7234          !!!next-token;          !!!next-token;
7235          next B;          next B;
7236          } elsif ($token->{tag_name} eq 'rt' or
7237                   $token->{tag_name} eq 'rp') {
7238            ## has a |ruby| element in scope
7239            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7240              my $node = $self->{open_elements}->[$_];
7241              if ($node->[1] & RUBY_EL) {
7242                !!!cp ('t398.1');
7243                ## generate implied end tags
7244                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7245                  !!!cp ('t398.2');
7246                  pop @{$self->{open_elements}};
7247                }
7248                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
7249                  !!!cp ('t398.3');
7250                  !!!parse-error (type => 'not closed',
7251                                  text => $self->{open_elements}->[-1]->[0]
7252                                      ->manakai_local_name,
7253                                  token => $token);
7254                  pop @{$self->{open_elements}}
7255                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
7256                }
7257                last INSCOPE;
7258              } elsif ($node->[1] & SCOPING_EL) {
7259                !!!cp ('t398.4');
7260                last INSCOPE;
7261              }
7262            } # INSCOPE
7263    
7264            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7265    
7266            !!!nack ('t398.5');
7267            !!!next-token;
7268            redo B;
7269        } elsif ($token->{tag_name} eq 'math' or        } elsif ($token->{tag_name} eq 'math' or
7270                 $token->{tag_name} eq 'svg') {                 $token->{tag_name} eq 'svg') {
7271          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7272    
7273            ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
7274    
7275          ## "adjust SVG attributes" ('svg' only) - done in insert-element-f          ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
7276    
7277          ## "adjust foreign attributes" - done in insert-element-f          ## "adjust foreign attributes" - done in insert-element-f
# Line 6781  sub _tree_construction_main ($) { Line 7298  sub _tree_construction_main ($) {
7298                  thead => 1, tr => 1,                  thead => 1, tr => 1,
7299                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7300          !!!cp ('t401');          !!!cp ('t401');
7301          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'in body',
7302                            text => $token->{tag_name}, token => $token);
7303          ## Ignore the token          ## Ignore the token
7304          !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.          !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7305          !!!next-token;          !!!next-token;
# Line 6866  sub _tree_construction_main ($) { Line 7384  sub _tree_construction_main ($) {
7384            }            }
7385    
7386            !!!parse-error (type => 'start tag not allowed',            !!!parse-error (type => 'start tag not allowed',
7387                            value => $token->{tag_name}, token => $token);                            text => $token->{tag_name}, token => $token);
7388            ## NOTE: Ignore the token.            ## NOTE: Ignore the token.
7389            !!!next-token;            !!!next-token;
7390            next B;            next B;
# Line 6876  sub _tree_construction_main ($) { Line 7394  sub _tree_construction_main ($) {
7394            unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {            unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7395              !!!cp ('t403');              !!!cp ('t403');
7396              !!!parse-error (type => 'not closed',              !!!parse-error (type => 'not closed',
7397                              value => $_->[0]->manakai_local_name,                              text => $_->[0]->manakai_local_name,
7398                              token => $token);                              token => $token);
7399              last;              last;
7400            } else {            } else {
# Line 6896  sub _tree_construction_main ($) { Line 7414  sub _tree_construction_main ($) {
7414            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7415              !!!cp ('t406');              !!!cp ('t406');
7416              !!!parse-error (type => 'not closed',              !!!parse-error (type => 'not closed',
7417                              value => $self->{open_elements}->[1]->[0]                              text => $self->{open_elements}->[1]->[0]
7418                                  ->manakai_local_name,                                  ->manakai_local_name,
7419                              token => $token);                              token => $token);
7420            } else {            } else {
# Line 6907  sub _tree_construction_main ($) { Line 7425  sub _tree_construction_main ($) {
7425            next B;            next B;
7426          } else {          } else {
7427            !!!cp ('t408');            !!!cp ('t408');
7428            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7429                              text => $token->{tag_name}, token => $token);
7430            ## Ignore the token            ## Ignore the token
7431            !!!next-token;            !!!next-token;
7432            next B;            next B;
# Line 6935  sub _tree_construction_main ($) { Line 7454  sub _tree_construction_main ($) {
7454    
7455          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7456            !!!cp ('t413');            !!!cp ('t413');
7457            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7458                              text => $token->{tag_name}, token => $token);
7459              ## NOTE: Ignore the token.
7460          } else {          } else {
7461            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7462            while ({            while ({
7463                      ## END_TAG_OPTIONAL_EL
7464                    dd => ($token->{tag_name} ne 'dd'),                    dd => ($token->{tag_name} ne 'dd'),
7465                    dt => ($token->{tag_name} ne 'dt'),                    dt => ($token->{tag_name} ne 'dt'),
7466                    li => ($token->{tag_name} ne 'li'),                    li => ($token->{tag_name} ne 'li'),
7467                    p => 1,                    p => 1,
7468                      rt => 1,
7469                      rp => 1,
7470                   }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {                   }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7471              !!!cp ('t409');              !!!cp ('t409');
7472              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
# Line 6953  sub _tree_construction_main ($) { Line 7477  sub _tree_construction_main ($) {
7477                    ne $token->{tag_name}) {                    ne $token->{tag_name}) {
7478              !!!cp ('t412');              !!!cp ('t412');
7479              !!!parse-error (type => 'not closed',              !!!parse-error (type => 'not closed',
7480                              value => $self->{open_elements}->[-1]->[0]                              text => $self->{open_elements}->[-1]->[0]
7481                                  ->manakai_local_name,                                  ->manakai_local_name,
7482                              token => $token);                              token => $token);
7483            } else {            } else {
# Line 6990  sub _tree_construction_main ($) { Line 7514  sub _tree_construction_main ($) {
7514    
7515          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7516            !!!cp ('t421');            !!!cp ('t421');
7517            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7518                              text => $token->{tag_name}, token => $token);
7519              ## NOTE: Ignore the token.
7520          } else {          } else {
7521            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7522            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
# Line 7003  sub _tree_construction_main ($) { Line 7529  sub _tree_construction_main ($) {
7529                    ne $token->{tag_name}) {                    ne $token->{tag_name}) {
7530              !!!cp ('t417.1');              !!!cp ('t417.1');
7531              !!!parse-error (type => 'not closed',              !!!parse-error (type => 'not closed',
7532                              value => $self->{open_elements}->[-1]->[0]                              text => $self->{open_elements}->[-1]->[0]
7533                                  ->manakai_local_name,                                  ->manakai_local_name,
7534                              token => $token);                              token => $token);
7535            } else {            } else {
# Line 7035  sub _tree_construction_main ($) { Line 7561  sub _tree_construction_main ($) {
7561    
7562          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7563            !!!cp ('t425.1');            !!!cp ('t425.1');
7564            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7565                              text => $token->{tag_name}, token => $token);
7566              ## NOTE: Ignore the token.
7567          } else {          } else {
7568            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7569            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
# Line 7047  sub _tree_construction_main ($) { Line 7575  sub _tree_construction_main ($) {
7575            if ($self->{open_elements}->[-1]->[0]->manakai_local_name            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7576                    ne $token->{tag_name}) {                    ne $token->{tag_name}) {
7577              !!!cp ('t425');              !!!cp ('t425');
7578              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
7579                                text => $token->{tag_name}, token => $token);
7580            } else {            } else {
7581              !!!cp ('t426');              !!!cp ('t426');
7582            }            }
# Line 7078  sub _tree_construction_main ($) { Line 7607  sub _tree_construction_main ($) {
7607                    ne $token->{tag_name}) {                    ne $token->{tag_name}) {
7608              !!!cp ('t412.1');              !!!cp ('t412.1');
7609              !!!parse-error (type => 'not closed',              !!!parse-error (type => 'not closed',
7610                              value => $self->{open_elements}->[-1]->[0]                              text => $self->{open_elements}->[-1]->[0]
7611                                  ->manakai_local_name,                                  ->manakai_local_name,
7612                              token => $token);                              token => $token);
7613            } else {            } else {
# Line 7088  sub _tree_construction_main ($) { Line 7617  sub _tree_construction_main ($) {
7617            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7618          } else {          } else {
7619            !!!cp ('t413.1');            !!!cp ('t413.1');
7620            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7621                              text => $token->{tag_name}, token => $token);
7622    
7623            !!!cp ('t415.1');            !!!cp ('t415.1');
7624            ## As if <p>, then reprocess the current token            ## As if <p>, then reprocess the current token
# Line 7111  sub _tree_construction_main ($) { Line 7641  sub _tree_construction_main ($) {
7641          next B;          next B;
7642        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7643          !!!cp ('t428');          !!!cp ('t428');
7644          !!!parse-error (type => 'unmatched end tag:br', token => $token);          !!!parse-error (type => 'unmatched end tag',
7645                            text => 'br', token => $token);
7646    
7647          ## As if <br>          ## As if <br>
7648          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
# Line 7136  sub _tree_construction_main ($) { Line 7667  sub _tree_construction_main ($) {
7667                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7668                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7669          !!!cp ('t429');          !!!cp ('t429');
7670          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'unmatched end tag',
7671                            text => $token->{tag_name}, token => $token);
7672          ## Ignore the token          ## Ignore the token
7673          !!!next-token;          !!!next-token;
7674          next B;          next B;
# Line 7155  sub _tree_construction_main ($) { Line 7687  sub _tree_construction_main ($) {
7687              ## generate implied end tags              ## generate implied end tags
7688              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7689                !!!cp ('t430');                !!!cp ('t430');
7690                ## ISSUE: Can this case be reached?                ## NOTE: |<ruby><rt></ruby>|.
7691                  ## ISSUE: <ruby><rt></rt> will also take this code path,
7692                  ## which seems wrong.
7693                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
7694                  $node_i++;
7695              }              }
7696                    
7697              ## Step 2              ## Step 2
# Line 7165  sub _tree_construction_main ($) { Line 7700  sub _tree_construction_main ($) {
7700                !!!cp ('t431');                !!!cp ('t431');
7701                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7702                !!!parse-error (type => 'not closed',                !!!parse-error (type => 'not closed',
7703                                value => $self->{open_elements}->[-1]->[0]                                text => $self->{open_elements}->[-1]->[0]
7704                                    ->manakai_local_name,                                    ->manakai_local_name,
7705                                token => $token);                                token => $token);
7706              } else {              } else {
# Line 7173  sub _tree_construction_main ($) { Line 7708  sub _tree_construction_main ($) {
7708              }              }
7709                            
7710              ## Step 3              ## Step 3
7711              splice @{$self->{open_elements}}, $node_i;              splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7712    
7713              !!!next-token;              !!!next-token;
7714              last S2;              last S2;
# Line 7184  sub _tree_construction_main ($) { Line 7719  sub _tree_construction_main ($) {
7719                  ($node->[1] & SPECIAL_EL or                  ($node->[1] & SPECIAL_EL or
7720                   $node->[1] & SCOPING_EL)) {                   $node->[1] & SCOPING_EL)) {
7721                !!!cp ('t433');                !!!cp ('t433');
7722                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
7723                                  text => $token->{tag_name}, token => $token);
7724                ## Ignore the token                ## Ignore the token
7725                !!!next-token;                !!!next-token;
7726                last S2;                last S2;
# Line 7230  sub _tree_construction_main ($) { Line 7766  sub _tree_construction_main ($) {
7766    ## TODO: script stuffs    ## TODO: script stuffs
7767  } # _tree_construct_main  } # _tree_construct_main
7768    
7769  sub set_inner_html ($$$) {  sub set_inner_html ($$$$;$) {
7770    my $class = shift;    my $class = shift;
7771    my $node = shift;    my $node = shift;
7772    my $s = \$_[0];    #my $s = \$_[0];
7773    my $onerror = $_[1];    my $onerror = $_[1];
7774      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7775    
7776    ## ISSUE: Should {confident} be true?    ## ISSUE: Should {confident} be true?
7777    
# Line 7253  sub set_inner_html ($$$) { Line 7790  sub set_inner_html ($$$) {
7790      }      }
7791    
7792      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7793      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($_[0] => $node, $onerror, $get_wrapper);
7794    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7795      ## TODO: If non-html element      ## TODO: If non-html element
7796    
7797      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7798    
7799    ## TODO: Support for $get_wrapper
7800    
7801      ## Step 1 # MUST      ## Step 1 # MUST
7802      my $this_doc = $node->owner_document;      my $this_doc = $node->owner_document;
7803      my $doc = $this_doc->implementation->create_document;      my $doc = $this_doc->implementation->create_document;
# Line 7270  sub set_inner_html ($$$) { Line 7809  sub set_inner_html ($$$) {
7809      my $i = 0;      my $i = 0;
7810      $p->{line_prev} = $p->{line} = 1;      $p->{line_prev} = $p->{line} = 1;
7811      $p->{column_prev} = $p->{column} = 0;      $p->{column_prev} = $p->{column} = 0;
7812        require Whatpm::Charset::DecodeHandle;
7813        my $input = Whatpm::Charset::DecodeHandle::CharString->new (\($_[0]));
7814        $input = $get_wrapper->($input);
7815      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
7816        my $self = shift;        my $self = shift;
7817    
7818        pop @{$self->{prev_char}};        my $char = '';
7819        unshift @{$self->{prev_char}}, $self->{next_char};        if (defined $self->{next_next_char}) {
7820            $char = $self->{next_next_char};
7821        $self->{next_char} = -1 and return if $i >= length $$s;          delete $self->{next_next_char};
7822        $self->{next_char} = ord substr $$s, $i++, 1;          $self->{next_char} = ord $char;
7823          } else {
7824            $self->{char_buffer} = '';
7825            $self->{char_buffer_pos} = 0;
7826            
7827            my $count = $input->manakai_read_until
7828                ($self->{char_buffer}, qr/[^\x00\x0A\x0D]/,
7829                 $self->{char_buffer_pos});
7830            if ($count) {
7831              $self->{line_prev} = $self->{line};
7832              $self->{column_prev} = $self->{column};
7833              $self->{column}++;
7834              $self->{next_char}
7835                  = ord substr ($self->{char_buffer},
7836                                $self->{char_buffer_pos}++, 1);
7837              return;
7838            }
7839            
7840            if ($input->read ($char, 1)) {
7841              $self->{next_char} = ord $char;
7842            } else {
7843              $self->{next_char} = -1;
7844              return;
7845            }
7846          }
7847    
7848        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7849        $p->{column}++;        $p->{column}++;
# Line 7287  sub set_inner_html ($$$) { Line 7853  sub set_inner_html ($$$) {
7853          $p->{column} = 0;          $p->{column} = 0;
7854          !!!cp ('i1');          !!!cp ('i1');
7855        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7856          $i++ if substr ($$s, $i, 1) eq "\x0A";  ## TODO: support for abort/streaming
7857            my $next = '';
7858            if ($input->read ($next, 1) and $next ne "\x0A") {
7859              $self->{next_next_char} = $next;
7860            }
7861          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7862          $p->{line}++;          $p->{line}++;
7863          $p->{column} = 0;          $p->{column} = 0;
7864          !!!cp ('i2');          !!!cp ('i2');
       } elsif ($self->{next_char} > 0x10FFFF) {  
         $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST  
         !!!cp ('i3');  
7865        } elsif ($self->{next_char} == 0x0000) { # NULL        } elsif ($self->{next_char} == 0x0000) { # NULL
7866          !!!cp ('i4');          !!!cp ('i4');
7867          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7868          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 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  
7869        }        }
7870      };      };
7871      $p->{prev_char} = [-1, -1, -1];  
7872      $p->{next_char} = -1;      $p->{read_until} = sub {
7873              #my ($scalar, $specials_range, $offset) = @_;
7874          return 0 if defined $p->{next_next_char};
7875    
7876          my $pattern = qr/[^$_[1]\x00\x0A\x0D]/;
7877          my $offset = $_[2] || 0;
7878          
7879          if ($p->{char_buffer_pos} < length $p->{char_buffer}) {
7880            pos ($p->{char_buffer}) = $p->{char_buffer_pos};
7881            if ($p->{char_buffer} =~ /\G(?>$pattern)+/) {
7882              substr ($_[0], $offset)
7883                  = substr ($p->{char_buffer}, $-[0], $+[0] - $-[0]);
7884              my $count = $+[0] - $-[0];
7885              if ($count) {
7886                $p->{column} += $count;
7887                $p->{char_buffer_pos} += $count;
7888                $p->{line_prev} = $p->{line};
7889                $p->{column_prev} = $p->{column} - 1;
7890                $p->{prev_char} = [-1, -1, -1];
7891                $p->{next_char} = -1;
7892              }
7893              return $count;
7894            } else {
7895              return 0;
7896            }
7897          } else {
7898            my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
7899            if ($count) {
7900              $p->{column} += $count;
7901              $p->{column_prev} += $count;
7902              $p->{prev_char} = [-1, -1, -1];
7903              $p->{next_char} = -1;
7904            }
7905            return $count;
7906          }
7907        }; # $p->{read_until}
7908    
7909      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7910        my (%opt) = @_;        my (%opt) = @_;
7911        my $line = $opt{line};        my $line = $opt{line};
# Line 7341  sub set_inner_html ($$$) { Line 7920  sub set_inner_html ($$$) {
7920        $ponerror->(line => $p->{line}, column => $p->{column}, @_);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7921      };      };
7922            
7923        my $char_onerror = sub {
7924          my (undef, $type, %opt) = @_;
7925          $ponerror->(layer => 'encode',
7926                      line => $p->{line}, column => $p->{column} + 1,
7927                      %opt, type => $type);
7928        }; # $char_onerror
7929        $input->onerror ($char_onerror);
7930    
7931      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7932      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7933    

Legend:
Removed from v.1.139  
changed lines
  Added in v.1.182

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24