/[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.137 by wakaba, Sun May 18 03:46:27 2008 UTC revision 1.179 by wakaba, Sun Sep 14 13:09:01 2008 UTC
# Line 8  use Error qw(:try); Line 8  use Error qw(:try);
8  ## doc.write ('');  ## doc.write ('');
9  ## alert (doc.compatMode);  ## alert (doc.compatMode);
10    
11  ## TODO: 1252 parse error (revision 1264)  require IO::Handle;
 ## TODO: 8859-11 = 874 (revision 1271)  
12    
13  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
14  my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;  my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
# Line 46  sub MISC_SPECIAL_EL () { 0b1000000000000 Line 45  sub MISC_SPECIAL_EL () { 0b1000000000000
45  sub FOREIGN_EL () { 0b10000000000000000000000000 }  sub FOREIGN_EL () { 0b10000000000000000000000000 }
46  sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }  sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
47  sub MML_AXML_EL () { 0b1000000000000000000000000000 }  sub MML_AXML_EL () { 0b1000000000000000000000000000 }
48    sub RUBY_EL () { 0b10000000000000000000000000000 }
49    sub RUBY_COMPONENT_EL () { 0b100000000000000000000000000000 }
50    
51  sub TABLE_ROWS_EL () {  sub TABLE_ROWS_EL () {
52    TABLE_EL |    TABLE_EL |
# Line 53  sub TABLE_ROWS_EL () { Line 54  sub TABLE_ROWS_EL () {
54    TABLE_ROW_GROUP_EL    TABLE_ROW_GROUP_EL
55  }  }
56    
57    ## NOTE: Used in "generate implied end tags" algorithm.
58    ## NOTE: There is a code where a modified version of END_TAG_OPTIONAL_EL
59    ## is used in "generate implied end tags" implementation (search for the
60    ## function mae).
61  sub END_TAG_OPTIONAL_EL () {  sub END_TAG_OPTIONAL_EL () {
62    DD_EL |    DD_EL |
63    DT_EL |    DT_EL |
64    LI_EL |    LI_EL |
65    P_EL    P_EL |
66      RUBY_COMPONENT_EL
67  }  }
68    
69    ## NOTE: Used in </body> and EOF algorithms.
70  sub ALL_END_TAG_OPTIONAL_EL () {  sub ALL_END_TAG_OPTIONAL_EL () {
71    END_TAG_OPTIONAL_EL |    DD_EL |
72      DT_EL |
73      LI_EL |
74      P_EL |
75    
76    BODY_EL |    BODY_EL |
77    HTML_EL |    HTML_EL |
78    TABLE_CELL_EL |    TABLE_CELL_EL |
# Line 97  sub SPECIAL_EL () { Line 108  sub SPECIAL_EL () {
108    ADDRESS_EL |    ADDRESS_EL |
109    BODY_EL |    BODY_EL |
110    DIV_EL |    DIV_EL |
111    END_TAG_OPTIONAL_EL |  
112      DD_EL |
113      DT_EL |
114      LI_EL |
115      P_EL |
116    
117    FORM_EL |    FORM_EL |
118    FRAMESET_EL |    FRAMESET_EL |
119    HEADING_EL |    HEADING_EL |
# Line 171  my $el_category = { Line 187  my $el_category = {
187    param => MISC_SPECIAL_EL,    param => MISC_SPECIAL_EL,
188    plaintext => MISC_SPECIAL_EL,    plaintext => MISC_SPECIAL_EL,
189    pre => MISC_SPECIAL_EL,    pre => MISC_SPECIAL_EL,
190      rp => RUBY_COMPONENT_EL,
191      rt => RUBY_COMPONENT_EL,
192      ruby => RUBY_EL,
193    s => FORMATTING_EL,    s => FORMATTING_EL,
194    script => MISC_SPECIAL_EL,    script => MISC_SPECIAL_EL,
195    select => SELECT_EL,    select => SELECT_EL,
# Line 212  my $el_category_f = { Line 231  my $el_category_f = {
231  };  };
232    
233  my $svg_attr_name = {  my $svg_attr_name = {
234      attributename => 'attributeName',
235    attributetype => 'attributeType',    attributetype => 'attributeType',
236    basefrequency => 'baseFrequency',    basefrequency => 'baseFrequency',
237    baseprofile => 'baseProfile',    baseprofile => 'baseProfile',
# Line 222  my $svg_attr_name = { Line 242  my $svg_attr_name = {
242    diffuseconstant => 'diffuseConstant',    diffuseconstant => 'diffuseConstant',
243    edgemode => 'edgeMode',    edgemode => 'edgeMode',
244    externalresourcesrequired => 'externalResourcesRequired',    externalresourcesrequired => 'externalResourcesRequired',
   fecolormatrix => 'feColorMatrix',  
   fecomposite => 'feComposite',  
   fegaussianblur => 'feGaussianBlur',  
   femorphology => 'feMorphology',  
   fetile => 'feTile',  
245    filterres => 'filterRes',    filterres => 'filterRes',
246    filterunits => 'filterUnits',    filterunits => 'filterUnits',
247    glyphref => 'glyphRef',    glyphref => 'glyphRef',
# Line 260  my $svg_attr_name = { Line 275  my $svg_attr_name = {
275    repeatcount => 'repeatCount',    repeatcount => 'repeatCount',
276    repeatdur => 'repeatDur',    repeatdur => 'repeatDur',
277    requiredextensions => 'requiredExtensions',    requiredextensions => 'requiredExtensions',
278      requiredfeatures => 'requiredFeatures',
279    specularconstant => 'specularConstant',    specularconstant => 'specularConstant',
280    specularexponent => 'specularExponent',    specularexponent => 'specularExponent',
281    spreadmethod => 'spreadMethod',    spreadmethod => 'spreadMethod',
# Line 332  my $c1_entity_char = { Line 348  my $c1_entity_char = {
348  }; # $c1_entity_char  }; # $c1_entity_char
349    
350  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
351      my $self = shift;
352      my $charset_name = shift;
353      open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
354      return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
355    } # parse_byte_string
356    
357    sub parse_byte_stream ($$$$;$$) {
358      # my ($self, $charset_name, $byte_stream, $doc, $onerror, $get_wrapper) = @_;
359    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
360    my $charset_name = shift;    my $charset_name = shift;
361    open my $byte_stream, '<', ref $_[0] ? $_[0] : \($_[0]);    my $byte_stream = $_[0];
362    
363    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
364      my (%opt) = @_;      my (%opt) = @_;
# Line 342  sub parse_byte_string ($$$$;$) { Line 366  sub parse_byte_string ($$$$;$) {
366    };    };
367    $self->{parse_error} = $onerror; # updated later by parse_char_string    $self->{parse_error} = $onerror; # updated later by parse_char_string
368    
369      my $get_wrapper = $_[3] || sub ($) {
370        return $_[0]; # $_[0] = byte stream handle, returned = arg to char handle
371      };
372    
373    ## HTML5 encoding sniffing algorithm    ## HTML5 encoding sniffing algorithm
374    require Message::Charset::Info;    require Message::Charset::Info;
375    my $charset;    my $charset;
# Line 349  sub parse_byte_string ($$$$;$) { Line 377  sub parse_byte_string ($$$$;$) {
377    my ($char_stream, $e_status);    my ($char_stream, $e_status);
378    
379    SNIFFING: {    SNIFFING: {
380        ## NOTE: By setting |allow_fallback| option true when the
381        ## |get_decode_handle| method is invoked, we ignore what the HTML5
382        ## spec requires, i.e. unsupported encoding should be ignored.
383          ## TODO: We should not do this unless the parser is invoked
384          ## in the conformance checking mode, in which this behavior
385          ## would be useful.
386    
387      ## Step 1      ## Step 1
388      if (defined $charset_name) {      if (defined $charset_name) {
389        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);        $charset = Message::Charset::Info->get_by_html_name ($charset_name);
390              ## TODO: Is this ok?  Transfer protocol's parameter should be
391              ## interpreted in its semantics?
392    
393        ## ISSUE: Unsupported encoding is not ignored according to the spec.        ## ISSUE: Unsupported encoding is not ignored according to the spec.
394        ($char_stream, $e_status) = $charset->get_decode_handle        ($char_stream, $e_status) = $charset->get_decode_handle
# Line 376  sub parse_byte_string ($$$$;$) { Line 412  sub parse_byte_string ($$$$;$) {
412    
413      ## Step 3      ## Step 3
414      if ($byte_buffer =~ /^\xFE\xFF/) {      if ($byte_buffer =~ /^\xFE\xFF/) {
415        $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');        $charset = Message::Charset::Info->get_by_html_name ('utf-16be');
416        ($char_stream, $e_status) = $charset->get_decode_handle        ($char_stream, $e_status) = $charset->get_decode_handle
417            ($byte_stream, allow_error_reporting => 1,            ($byte_stream, allow_error_reporting => 1,
418             allow_fallback => 1, byte_buffer => \$byte_buffer);             allow_fallback => 1, byte_buffer => \$byte_buffer);
419        $self->{confident} = 1;        $self->{confident} = 1;
420        last SNIFFING;        last SNIFFING;
421      } elsif ($byte_buffer =~ /^\xFF\xFE/) {      } elsif ($byte_buffer =~ /^\xFF\xFE/) {
422        $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');        $charset = Message::Charset::Info->get_by_html_name ('utf-16le');
423        ($char_stream, $e_status) = $charset->get_decode_handle        ($char_stream, $e_status) = $charset->get_decode_handle
424            ($byte_stream, allow_error_reporting => 1,            ($byte_stream, allow_error_reporting => 1,
425             allow_fallback => 1, byte_buffer => \$byte_buffer);             allow_fallback => 1, byte_buffer => \$byte_buffer);
426        $self->{confident} = 1;        $self->{confident} = 1;
427        last SNIFFING;        last SNIFFING;
428      } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {      } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
429        $charset = Message::Charset::Info->get_by_iana_name ('utf-8');        $charset = Message::Charset::Info->get_by_html_name ('utf-8');
430        ($char_stream, $e_status) = $charset->get_decode_handle        ($char_stream, $e_status) = $charset->get_decode_handle
431            ($byte_stream, allow_error_reporting => 1,            ($byte_stream, allow_error_reporting => 1,
432             allow_fallback => 1, byte_buffer => \$byte_buffer);             allow_fallback => 1, byte_buffer => \$byte_buffer);
# Line 409  sub parse_byte_string ($$$$;$) { Line 445  sub parse_byte_string ($$$$;$) {
445      $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string      $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
446          ($byte_buffer);          ($byte_buffer);
447      if (defined $charset_name) {      if (defined $charset_name) {
448        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);        $charset = Message::Charset::Info->get_by_html_name ($charset_name);
449    
450        ## ISSUE: Unsupported encoding is not ignored according to the spec.        ## ISSUE: Unsupported encoding is not ignored according to the spec.
451        require Whatpm::Charset::DecodeHandle;        require Whatpm::Charset::DecodeHandle;
# Line 420  sub parse_byte_string ($$$$;$) { Line 456  sub parse_byte_string ($$$$;$) {
456             allow_fallback => 1, byte_buffer => \$byte_buffer);             allow_fallback => 1, byte_buffer => \$byte_buffer);
457        if ($char_stream) {        if ($char_stream) {
458          $buffer->{buffer} = $byte_buffer;          $buffer->{buffer} = $byte_buffer;
459          !!!parse-error (type => 'sniffing:chardet', ## TODO: type name          !!!parse-error (type => 'sniffing:chardet',
460                          value => $charset_name,                          text => $charset_name,
461                          level => $self->{info_level},                          level => $self->{level}->{info},
462                            layer => 'encode',
463                          line => 1, column => 1);                          line => 1, column => 1);
464          $self->{confident} = 0;          $self->{confident} = 0;
465          last SNIFFING;          last SNIFFING;
# Line 431  sub parse_byte_string ($$$$;$) { Line 468  sub parse_byte_string ($$$$;$) {
468    
469      ## Step 7: default      ## Step 7: default
470      ## TODO: Make this configurable.      ## TODO: Make this configurable.
471      $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');      $charset = Message::Charset::Info->get_by_html_name ('windows-1252');
472          ## NOTE: We choose |windows-1252| here, since |utf-8| should be          ## NOTE: We choose |windows-1252| here, since |utf-8| should be
473          ## detectable in the step 6.          ## detectable in the step 6.
474      require Whatpm::Charset::DecodeHandle;      require Whatpm::Charset::DecodeHandle;
# Line 443  sub parse_byte_string ($$$$;$) { Line 480  sub parse_byte_string ($$$$;$) {
480                                         allow_fallback => 1,                                         allow_fallback => 1,
481                                         byte_buffer => \$byte_buffer);                                         byte_buffer => \$byte_buffer);
482      $buffer->{buffer} = $byte_buffer;      $buffer->{buffer} = $byte_buffer;
483      !!!parse-error (type => 'sniffing:default', ## TODO: type name      !!!parse-error (type => 'sniffing:default',
484                      value => 'windows-1252',                      text => 'windows-1252',
485                      level => $self->{info_level},                      level => $self->{level}->{info},
486                      line => 1, column => 1);                      line => 1, column => 1,
487                        layer => 'encode');
488      $self->{confident} = 0;      $self->{confident} = 0;
489    } # SNIFFING    } # SNIFFING
490    
   $self->{input_encoding} = $charset->get_iana_name;  
491    if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {    if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
492      !!!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?
493                      value => $self->{input_encoding},      !!!parse-error (type => 'chardecode:fallback',
494                      level => $self->{unsupported_level},                      #text => $self->{input_encoding},
495                      line => 1, column => 1);                      level => $self->{level}->{uncertain},
496                        line => 1, column => 1,
497                        layer => 'encode');
498    } elsif (not ($e_status &    } elsif (not ($e_status &
499                  Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {                  Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL ())) {
500      !!!parse-error (type => 'chardecode:no error', ## TODO: type name      $self->{input_encoding} = $charset->get_iana_name;
501                      value => $self->{input_encoding},      !!!parse-error (type => 'chardecode:no error',
502                      level => $self->{unsupported_level},                      text => $self->{input_encoding},
503                      line => 1, column => 1);                      level => $self->{level}->{uncertain},
504                        line => 1, column => 1,
505                        layer => 'encode');
506      } else {
507        $self->{input_encoding} = $charset->get_iana_name;
508    }    }
509    
510    $self->{change_encoding} = sub {    $self->{change_encoding} = sub {
# Line 469  sub parse_byte_string ($$$$;$) { Line 512  sub parse_byte_string ($$$$;$) {
512      $charset_name = shift;      $charset_name = shift;
513      my $token = shift;      my $token = shift;
514    
515      $charset = Message::Charset::Info->get_by_iana_name ($charset_name);      $charset = Message::Charset::Info->get_by_html_name ($charset_name);
516      ($char_stream, $e_status) = $charset->get_decode_handle      ($char_stream, $e_status) = $charset->get_decode_handle
517          ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,          ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
518           byte_buffer => \ $buffer->{buffer});           byte_buffer => \ $buffer->{buffer});
# Line 478  sub parse_byte_string ($$$$;$) { Line 521  sub parse_byte_string ($$$$;$) {
521        ## "Change the encoding" algorithm:        ## "Change the encoding" algorithm:
522    
523        ## Step 1            ## Step 1    
524        if ($charset->{iana_names}->{'utf-16'}) { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?        if ($charset->{category} &
525          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');            Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
526            $charset = Message::Charset::Info->get_by_html_name ('utf-8');
527          ($char_stream, $e_status) = $charset->get_decode_handle          ($char_stream, $e_status) = $charset->get_decode_handle
528              ($byte_stream,              ($byte_stream,
529               byte_buffer => \ $buffer->{buffer});               byte_buffer => \ $buffer->{buffer});
# Line 489  sub parse_byte_string ($$$$;$) { Line 533  sub parse_byte_string ($$$$;$) {
533        ## Step 2        ## Step 2
534        if (defined $self->{input_encoding} and        if (defined $self->{input_encoding} and
535            $self->{input_encoding} eq $charset_name) {            $self->{input_encoding} eq $charset_name) {
536          !!!parse-error (type => 'charset label:matching', ## TODO: type          !!!parse-error (type => 'charset label:matching',
537                          value => $charset_name,                          text => $charset_name,
538                          level => $self->{info_level});                          level => $self->{level}->{info});
539          $self->{confident} = 1;          $self->{confident} = 1;
540          return;          return;
541        }        }
542    
543        !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.        !!!parse-error (type => 'charset label detected',
544            ':'.$charset_name, level => 'w', token => $token);                        text => $self->{input_encoding},
545                          value => $charset_name,
546                          level => $self->{level}->{warn},
547                          token => $token);
548                
549        ## Step 3        ## Step 3
550        # if (can) {        # if (can) {
# Line 513  sub parse_byte_string ($$$$;$) { Line 560  sub parse_byte_string ($$$$;$) {
560    
561    my $char_onerror = sub {    my $char_onerror = sub {
562      my (undef, $type, %opt) = @_;      my (undef, $type, %opt) = @_;
563      !!!parse-error (%opt, type => $type,      !!!parse-error (layer => 'encode',
564                      line => $self->{line}, column => $self->{column} + 1);                      line => $self->{line}, column => $self->{column} + 1,
565                        %opt, type => $type);
566      if ($opt{octets}) {      if ($opt{octets}) {
567        ${$opt{octets}} = "\x{FFFD}"; # relacement character        ${$opt{octets}} = "\x{FFFD}"; # relacement character
568      }      }
569    };    };
570    $char_stream->onerror ($char_onerror);  
571      my $wrapped_char_stream = $get_wrapper->($char_stream);
572      $wrapped_char_stream->onerror ($char_onerror);
573    
574    my @args = @_; shift @args; # $s    my @args = @_; shift @args; # $s
575    my $return;    my $return;
576    try {    try {
577      $return = $self->parse_char_stream ($char_stream, @args);        $return = $self->parse_char_stream ($wrapped_char_stream, @args);  
578    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
579      ## NOTE: Invoked after {change_encoding}.      ## NOTE: Invoked after {change_encoding}.
580    
     $self->{input_encoding} = $charset->get_iana_name;  
581      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
582        !!!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?
583                        value => $self->{input_encoding},        !!!parse-error (type => 'chardecode:fallback',
584                        level => $self->{unsupported_level},                        level => $self->{level}->{uncertain},
585                        line => 1, column => 1);                        #text => $self->{input_encoding},
586                          line => 1, column => 1,
587                          layer => 'encode');
588      } elsif (not ($e_status &      } elsif (not ($e_status &
589                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL ())) {
590        !!!parse-error (type => 'chardecode:no error', ## TODO: type name        $self->{input_encoding} = $charset->get_iana_name;
591                        value => $self->{input_encoding},        !!!parse-error (type => 'chardecode:no error',
592                        level => $self->{unsupported_level},                        text => $self->{input_encoding},
593                        line => 1, column => 1);                        level => $self->{level}->{uncertain},
594                          line => 1, column => 1,
595                          layer => 'encode');
596        } else {
597          $self->{input_encoding} = $charset->get_iana_name;
598      }      }
599      $self->{confident} = 1;      $self->{confident} = 1;
600      $char_stream->onerror ($char_onerror);  
601      $return = $self->parse_char_stream ($char_stream, @args);      $wrapped_char_stream = $get_wrapper->($char_stream);
602        $wrapped_char_stream->onerror ($char_onerror);
603    
604        $return = $self->parse_char_stream ($wrapped_char_stream, @args);
605    };    };
606    return $return;    return $return;
607  } # parse_byte_string  } # parse_byte_stream
608    
609  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
610  ## and the HTML layer MUST ignore it.  However, we does strip BOM in  ## and the HTML layer MUST ignore it.  However, we does strip BOM in
# Line 557  sub parse_byte_string ($$$$;$) { Line 615  sub parse_byte_string ($$$$;$) {
615  ## such as |parse_byte_string| in this module, must ensure that it does  ## such as |parse_byte_string| in this module, must ensure that it does
616  ## strip the BOM and never strip any ZWNBSP.  ## strip the BOM and never strip any ZWNBSP.
617    
618  sub parse_char_string ($$$;$) {  sub parse_char_string ($$$;$$) {
619      #my ($self, $s, $doc, $onerror, $get_wrapper) = @_;
620    my $self = shift;    my $self = shift;
621    open my $input, '<:utf8', ref $_[0] ? $_[0] : \($_[0]);    my $s = ref $_[0] ? $_[0] : \($_[0]);
622      require Whatpm::Charset::DecodeHandle;
623      my $input = Whatpm::Charset::DecodeHandle::CharString->new ($s);
624      if ($_[3]) {
625        $input = $_[3]->($input);
626      }
627    return $self->parse_char_stream ($input, @_[1..$#_]);    return $self->parse_char_stream ($input, @_[1..$#_]);
628  } # parse_char_string  } # parse_char_string
629  *parse_string = \&parse_char_string;  *parse_string = \&parse_char_string; ## NOTE: Alias for backward compatibility.
630    
631  sub parse_char_stream ($$$;$) {  sub parse_char_stream ($$$;$) {
632    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
# Line 575  sub parse_char_stream ($$$;$) { Line 639  sub parse_char_stream ($$$;$) {
639    $self->{confident} = 1 unless exists $self->{confident};    $self->{confident} = 1 unless exists $self->{confident};
640    $self->{document}->input_encoding ($self->{input_encoding})    $self->{document}->input_encoding ($self->{input_encoding})
641        if defined $self->{input_encoding};        if defined $self->{input_encoding};
642    ## TODO: |{input_encoding}| is needless?
643    
644    my $i = 0;    my $i = 0;
645    $self->{line_prev} = $self->{line} = 1;    $self->{line_prev} = $self->{line} = 1;
646    $self->{column_prev} = $self->{column} = 0;    $self->{column_prev} = -1;
647      $self->{column} = 0;
648    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
649      my $self = shift;      my $self = shift;
650    
651      pop @{$self->{prev_char}};      my $char = '';
652      unshift @{$self->{prev_char}}, $self->{next_char};      if (defined $self->{next_next_char}) {
653          $char = $self->{next_next_char};
654          delete $self->{next_next_char};
655          $self->{next_char} = ord $char;
656        } else {
657          $self->{char_buffer} = '';
658          $self->{char_buffer_pos} = 0;
659    
660      my $char = $input->getc;        my $count = $input->manakai_read_until
661      $self->{next_char} = -1 and return unless defined $char;           ($self->{char_buffer},
662      $self->{next_char} = ord $char;            qr/(?!\x{FDD0}-\x{FDDF}\x{FFFE}\x{FFFF}\x{1FFFE}\x{1FFFF}\x{2FFFE}\x{2FFFF}\x{3FFFE}\x{3FFFF}\x{4FFFE}\x{4FFFF}\x{5FFFE}\x{5FFFF}\x{6FFFE}\x{6FFFF}\x{7FFFE}\x{7FFFF}\x{8FFFE}\x{8FFFF}\x{9FFFE}\x{9FFFF}\x{AFFFE}\x{AFFFF}\x{BFFFE}\x{BFFFF}\x{CFFFE}\x{CFFFF}\x{DFFFE}\x{DFFFF}\x{EFFFE}\x{EFFFF}\x{FFFFE}\x{FFFFF}])[\x20-\x7E\xA0-\x{D7FF}\x{E000}-\x{10FFFD}]/,
663              $self->{char_buffer_pos});
664          if ($count) {
665            $self->{line_prev} = $self->{line};
666            $self->{column_prev} = $self->{column};
667            $self->{column}++;
668            $self->{next_char}
669                = ord substr ($self->{char_buffer}, $self->{char_buffer_pos}++, 1);
670            return;
671          }
672    
673          if ($input->read ($char, 1)) {
674            $self->{next_char} = ord $char;
675          } else {
676            $self->{next_char} = -1;
677            return;
678          }
679        }
680    
681      ($self->{line_prev}, $self->{column_prev})      ($self->{line_prev}, $self->{column_prev})
682          = ($self->{line}, $self->{column});          = ($self->{line}, $self->{column});
# Line 599  sub parse_char_stream ($$$;$) { Line 688  sub parse_char_stream ($$$;$) {
688        $self->{column} = 0;        $self->{column} = 0;
689      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
690        !!!cp ('j2');        !!!cp ('j2');
691        my $next = $input->getc;  ## TODO: support for abort/streaming
692        if ($next ne "\x0A") {        my $next = '';
693          $input->ungetc ($next);        if ($input->read ($next, 1) and $next ne "\x0A") {
694            $self->{next_next_char} = $next;
695        }        }
696        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
697        $self->{line}++;        $self->{line}++;
# Line 618  sub parse_char_stream ($$$;$) { Line 708  sub parse_char_stream ($$$;$) {
708               (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or               (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
709               (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or               (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
710               (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or               (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
711    ## ISSUE: U+FDE0-U+FDEF are not excluded
712               {               {
713                0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,                0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
714                0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,                0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
# Line 630  sub parse_char_stream ($$$;$) { Line 721  sub parse_char_stream ($$$;$) {
721                0x10FFFE => 1, 0x10FFFF => 1,                0x10FFFE => 1, 0x10FFFF => 1,
722               }->{$self->{next_char}}) {               }->{$self->{next_char}}) {
723        !!!cp ('j5');        !!!cp ('j5');
724        !!!parse-error (type => 'control char', level => $self->{must_level});        if ($self->{next_char} < 0x10000) {
725  ## TODO: error type documentation          !!!parse-error (type => 'control char',
726                            text => (sprintf 'U+%04X', $self->{next_char}));
727          } else {
728            !!!parse-error (type => 'control char',
729                            text => (sprintf 'U-%08X', $self->{next_char}));
730          }
731      }      }
732    };    };
733    $self->{prev_char} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
734    $self->{next_char} = -1;    $self->{next_char} = -1;
735    
736      $self->{read_until} = sub {
737        #my ($scalar, $specials_range, $offset) = @_;
738        my $specials_range = $_[1];
739        return 0 if defined $self->{next_next_char};
740        my $count = $input->manakai_read_until
741           ($_[0],
742            qr/(?![$specials_range\x{FDD0}-\x{FDDF}\x{FFFE}\x{FFFF}\x{1FFFE}\x{1FFFF}\x{2FFFE}\x{2FFFF}\x{3FFFE}\x{3FFFF}\x{4FFFE}\x{4FFFF}\x{5FFFE}\x{5FFFF}\x{6FFFE}\x{6FFFF}\x{7FFFE}\x{7FFFF}\x{8FFFE}\x{8FFFF}\x{9FFFE}\x{9FFFF}\x{AFFFE}\x{AFFFF}\x{BFFFE}\x{BFFFF}\x{CFFFE}\x{CFFFF}\x{DFFFE}\x{DFFFF}\x{EFFFE}\x{EFFFF}\x{FFFFE}\x{FFFFF}])[\x20-\x7E\xA0-\x{D7FF}\x{E000}-\x{10FFFD}]/,
743            $_[2]);
744        if ($count) {
745          $self->{column} += $count;
746          $self->{column_prev} += $count;
747          $self->{prev_char} = [-1, -1, -1];
748          $self->{next_char} = -1;
749        }
750        return $count;
751      }; # $self->{read_until}
752    $self->{read_until}=sub{0};
753    
754    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
755      my (%opt) = @_;      my (%opt) = @_;
756      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
# Line 660  sub parse_char_stream ($$$;$) { Line 774  sub parse_char_stream ($$$;$) {
774  sub new ($) {  sub new ($) {
775    my $class = shift;    my $class = shift;
776    my $self = bless {    my $self = bless {
777      must_level => 'm',      level => {must => 'm',
778      should_level => 's',                should => 's',
779      good_level => 'w',                warn => 'w',
780      warn_level => 'w',                info => 'i',
781      info_level => 'i',                uncertain => 'u'},
     unsupported_level => 'u',  
782    }, $class;    }, $class;
783    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
784      $self->{next_char} = -1;      $self->{next_char} = -1;
# Line 695  sub RCDATA_CONTENT_MODEL () { CM_ENTITY Line 808  sub RCDATA_CONTENT_MODEL () { CM_ENTITY
808  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
809    
810  sub DATA_STATE () { 0 }  sub DATA_STATE () { 0 }
811  sub ENTITY_DATA_STATE () { 1 }  #sub ENTITY_DATA_STATE () { 1 }
812  sub TAG_OPEN_STATE () { 2 }  sub TAG_OPEN_STATE () { 2 }
813  sub CLOSE_TAG_OPEN_STATE () { 3 }  sub CLOSE_TAG_OPEN_STATE () { 3 }
814  sub TAG_NAME_STATE () { 4 }  sub TAG_NAME_STATE () { 4 }
# Line 706  sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 Line 819  sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8
819  sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }  sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
820  sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }  sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
821  sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }  sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
822  sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }  #sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
823  sub MARKUP_DECLARATION_OPEN_STATE () { 13 }  sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
824  sub COMMENT_START_STATE () { 14 }  sub COMMENT_START_STATE () { 14 }
825  sub COMMENT_START_DASH_STATE () { 15 }  sub COMMENT_START_DASH_STATE () { 15 }
# Line 729  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STAT Line 842  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STAT
842  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
843  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
844  sub SELF_CLOSING_START_TAG_STATE () { 34 }  sub SELF_CLOSING_START_TAG_STATE () { 34 }
845  sub CDATA_BLOCK_STATE () { 35 }  sub CDATA_SECTION_STATE () { 35 }
846    sub MD_HYPHEN_STATE () { 36 } # "markup declaration open state" in the spec
847    sub MD_DOCTYPE_STATE () { 37 } # "markup declaration open state" in the spec
848    sub MD_CDATA_STATE () { 38 } # "markup declaration open state" in the spec
849    sub CDATA_PCDATA_CLOSE_TAG_STATE () { 39 } # "close tag open state" in the spec
850    sub CDATA_SECTION_MSE1_STATE () { 40 } # "CDATA section state" in the spec
851    sub CDATA_SECTION_MSE2_STATE () { 41 } # "CDATA section state" in the spec
852    sub PUBLIC_STATE () { 42 } # "after DOCTYPE name state" in the spec
853    sub SYSTEM_STATE () { 43 } # "after DOCTYPE name state" in the spec
854    ## NOTE: "Entity data state", "entity in attribute value state", and
855    ## "consume a character reference" algorithm are jointly implemented
856    ## using the following six states:
857    sub ENTITY_STATE () { 44 }
858    sub ENTITY_HASH_STATE () { 45 }
859    sub NCR_NUM_STATE () { 46 }
860    sub HEXREF_X_STATE () { 47 }
861    sub HEXREF_HEX_STATE () { 48 }
862    sub ENTITY_NAME_STATE () { 49 }
863    
864  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
865  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 782  sub IN_COLUMN_GROUP_IM () { 0b10 } Line 912  sub IN_COLUMN_GROUP_IM () { 0b10 }
912  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
913    my $self = shift;    my $self = shift;
914    $self->{state} = DATA_STATE; # MUST    $self->{state} = DATA_STATE; # MUST
915      #$self->{state_keyword}; # initialized when used
916      #$self->{entity__value}; # initialized when used
917      #$self->{entity__match}; # initialized when used
918    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
919    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token};
920    undef $self->{current_attribute};    undef $self->{current_attribute};
921    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
922    undef $self->{last_attribute_value_state};    #$self->{prev_state}; # initialized when used
923    delete $self->{self_closing};    delete $self->{self_closing};
924    $self->{char} = [];    $self->{char_buffer} = '';
925      $self->{char_buffer_pos} = 0;
926    # $self->{next_char}    # $self->{next_char}
927    !!!next-input-character;    !!!next-input-character;
928    $self->{token} = [];    $self->{token} = [];
# Line 812  sub _initialize_tokenizer ($) { Line 946  sub _initialize_tokenizer ($) {
946  ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|  ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
947  ##     while the token is pushed back to the stack.  ##     while the token is pushed back to the stack.
948    
 ## 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?  
   
949  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
950    
951  ## 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 824  sub _initialize_tokenizer ($) { Line 954  sub _initialize_tokenizer ($) {
954  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
955  ## and removed from the list.  ## and removed from the list.
956    
957  ## NOTE: HTML5 "Writing HTML documents" section, applied to  ## TODO: Polytheistic slash SHOULD NOT be used. (Applied only to atheists.)
958  ## 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,  
959    
960  sub _get_next_token ($) {  sub _get_next_token ($) {
961    my $self = shift;    my $self = shift;
# Line 861  sub _get_next_token ($) { Line 979  sub _get_next_token ($) {
979          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
980              not $self->{escape}) {              not $self->{escape}) {
981            !!!cp (1);            !!!cp (1);
982            $self->{state} = ENTITY_DATA_STATE;            ## NOTE: In the spec, the tokenizer is switched to the
983              ## "entity data state".  In this implementation, the tokenizer
984              ## is switched to the |ENTITY_STATE|, which is an implementation
985              ## of the "consume a character reference" algorithm.
986              $self->{entity_additional} = -1;
987              $self->{prev_state} = DATA_STATE;
988              $self->{state} = ENTITY_STATE;
989            !!!next-input-character;            !!!next-input-character;
990            redo A;            redo A;
991          } else {          } else {
# Line 925  sub _get_next_token ($) { Line 1049  sub _get_next_token ($) {
1049                     data => chr $self->{next_char},                     data => chr $self->{next_char},
1050                     line => $self->{line}, column => $self->{column},                     line => $self->{line}, column => $self->{column},
1051                    };                    };
1052          $self->{read_until}->($token->{data}, q[-!<>&], length $token->{data});
1053    
1054        ## Stay in the data state        ## Stay in the data state
1055        !!!next-input-character;        !!!next-input-character;
1056    
1057        !!!emit ($token);        !!!emit ($token);
1058    
1059        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;  
1060      } elsif ($self->{state} == TAG_OPEN_STATE) {      } elsif ($self->{state} == TAG_OPEN_STATE) {
1061        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1062          if ($self->{next_char} == 0x002F) { # /          if ($self->{next_char} == 0x002F) { # /
# Line 1048  sub _get_next_token ($) { Line 1153  sub _get_next_token ($) {
1153          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1154        }        }
1155      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1156          ## NOTE: The "close tag open state" in the spec is implemented as
1157          ## |CLOSE_TAG_OPEN_STATE| and |CDATA_PCDATA_CLOSE_TAG_STATE|.
1158    
1159        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1160        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1161          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1162              $self->{state} = CDATA_PCDATA_CLOSE_TAG_STATE;
1163            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            $self->{state_keyword} = '';
1164            my @next_char;            ## Reconsume.
1165            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...  
           }  
1166          } else {          } else {
1167            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1168              ## NOTE: See <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>.
1169            !!!cp (28);            !!!cp (28);
           # next-input-character is already done  
1170            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1171              ## Reconsume.
1172            !!!emit ({type => CHARACTER_TOKEN, data => '</',            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1173                      line => $l, column => $c,                      line => $l, column => $c,
1174                     });                     });
1175            redo A;            redo A;
1176          }          }
1177        }        }
1178          
1179        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{next_char} and
1180            $self->{next_char} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1181          !!!cp (29);          !!!cp (29);
# Line 1157  sub _get_next_token ($) { Line 1222  sub _get_next_token ($) {
1222                                    line => $self->{line_prev}, # "<" of "</"                                    line => $self->{line_prev}, # "<" of "</"
1223                                    column => $self->{column_prev} - 1,                                    column => $self->{column_prev} - 1,
1224                                   };                                   };
1225          ## $self->{next_char} is intentionally left as is          ## NOTE: $self->{next_char} is intentionally left as is.
1226          redo A;          ## Although the "anything else" case of the spec not explicitly
1227            ## states that the next input character is to be reconsumed,
1228            ## it will be included to the |data| of the comment token
1229            ## generated from the bogus end tag, as defined in the
1230            ## "bogus comment state" entry.
1231            redo A;
1232          }
1233        } elsif ($self->{state} == CDATA_PCDATA_CLOSE_TAG_STATE) {
1234          my $ch = substr $self->{last_emitted_start_tag_name}, length $self->{state_keyword}, 1;
1235          if (length $ch) {
1236            my $CH = $ch;
1237            $ch =~ tr/a-z/A-Z/;
1238            my $nch = chr $self->{next_char};
1239            if ($nch eq $ch or $nch eq $CH) {
1240              !!!cp (24);
1241              ## Stay in the state.
1242              $self->{state_keyword} .= $nch;
1243              !!!next-input-character;
1244              redo A;
1245            } else {
1246              !!!cp (25);
1247              $self->{state} = DATA_STATE;
1248              ## Reconsume.
1249              !!!emit ({type => CHARACTER_TOKEN,
1250                        data => '</' . $self->{state_keyword},
1251                        line => $self->{line_prev},
1252                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1253                       });
1254              redo A;
1255            }
1256          } else { # after "<{tag-name}"
1257            unless ({
1258                     0x0009 => 1, # HT
1259                     0x000A => 1, # LF
1260                     0x000B => 1, # VT
1261                     0x000C => 1, # FF
1262                     0x0020 => 1, # SP
1263                     0x003E => 1, # >
1264                     0x002F => 1, # /
1265                     -1 => 1, # EOF
1266                    }->{$self->{next_char}}) {
1267              !!!cp (26);
1268              ## Reconsume.
1269              $self->{state} = DATA_STATE;
1270              !!!emit ({type => CHARACTER_TOKEN,
1271                        data => '</' . $self->{state_keyword},
1272                        line => $self->{line_prev},
1273                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1274                       });
1275              redo A;
1276            } else {
1277              !!!cp (27);
1278              $self->{current_token}
1279                  = {type => END_TAG_TOKEN,
1280                     tag_name => $self->{last_emitted_start_tag_name},
1281                     line => $self->{line_prev},
1282                     column => $self->{column_prev} - 1 - length $self->{state_keyword}};
1283              $self->{state} = TAG_NAME_STATE;
1284              ## Reconsume.
1285              redo A;
1286            }
1287        }        }
1288      } elsif ($self->{state} == TAG_NAME_STATE) {      } elsif ($self->{state} == TAG_NAME_STATE) {
1289        if ($self->{next_char} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
# Line 1328  sub _get_next_token ($) { Line 1453  sub _get_next_token ($) {
1453          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1454              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1455            !!!cp (57);            !!!cp (57);
1456            !!!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});
1457            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1458          } else {          } else {
1459            !!!cp (58);            !!!cp (58);
# Line 1499  sub _get_next_token ($) { Line 1624  sub _get_next_token ($) {
1624    
1625          redo A;          redo A;
1626        } else {        } else {
1627          !!!cp (82);          if ($self->{next_char} == 0x0022 or # "
1628                $self->{next_char} == 0x0027) { # '
1629              !!!cp (78);
1630              !!!parse-error (type => 'bad attribute name');
1631            } else {
1632              !!!cp (82);
1633            }
1634          $self->{current_attribute}          $self->{current_attribute}
1635              = {name => chr ($self->{next_char}),              = {name => chr ($self->{next_char}),
1636                 value => '',                 value => '',
# Line 1534  sub _get_next_token ($) { Line 1665  sub _get_next_token ($) {
1665          !!!next-input-character;          !!!next-input-character;
1666          redo A;          redo A;
1667        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1668            !!!parse-error (type => 'empty unquoted attribute value');
1669          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1670            !!!cp (87);            !!!cp (87);
1671            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
# Line 1598  sub _get_next_token ($) { Line 1730  sub _get_next_token ($) {
1730          redo A;          redo A;
1731        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1732          !!!cp (96);          !!!cp (96);
1733          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1734          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1735            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1736            ## implementation of the "consume a character reference" algorithm.
1737            $self->{prev_state} = $self->{state};
1738            $self->{entity_additional} = 0x0022; # "
1739            $self->{state} = ENTITY_STATE;
1740          !!!next-input-character;          !!!next-input-character;
1741          redo A;          redo A;
1742        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
# Line 1628  sub _get_next_token ($) { Line 1765  sub _get_next_token ($) {
1765        } else {        } else {
1766          !!!cp (100);          !!!cp (100);
1767          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{current_attribute}->{value} .= chr ($self->{next_char});
1768            $self->{read_until}->($self->{current_attribute}->{value},
1769                                  q["&],
1770                                  length $self->{current_attribute}->{value});
1771    
1772          ## Stay in the state          ## Stay in the state
1773          !!!next-input-character;          !!!next-input-character;
1774          redo A;          redo A;
# Line 1640  sub _get_next_token ($) { Line 1781  sub _get_next_token ($) {
1781          redo A;          redo A;
1782        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1783          !!!cp (102);          !!!cp (102);
1784          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1785          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1786            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1787            ## implementation of the "consume a character reference" algorithm.
1788            $self->{entity_additional} = 0x0027; # '
1789            $self->{prev_state} = $self->{state};
1790            $self->{state} = ENTITY_STATE;
1791          !!!next-input-character;          !!!next-input-character;
1792          redo A;          redo A;
1793        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
# Line 1670  sub _get_next_token ($) { Line 1816  sub _get_next_token ($) {
1816        } else {        } else {
1817          !!!cp (106);          !!!cp (106);
1818          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{current_attribute}->{value} .= chr ($self->{next_char});
1819            $self->{read_until}->($self->{current_attribute}->{value},
1820                                  q['&],
1821                                  length $self->{current_attribute}->{value});
1822    
1823          ## Stay in the state          ## Stay in the state
1824          !!!next-input-character;          !!!next-input-character;
1825          redo A;          redo A;
# Line 1686  sub _get_next_token ($) { Line 1836  sub _get_next_token ($) {
1836          redo A;          redo A;
1837        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1838          !!!cp (108);          !!!cp (108);
1839          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1840          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1841            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1842            ## implementation of the "consume a character reference" algorithm.
1843            $self->{entity_additional} = -1;
1844            $self->{prev_state} = $self->{state};
1845            $self->{state} = ENTITY_STATE;
1846          !!!next-input-character;          !!!next-input-character;
1847          redo A;          redo A;
1848        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
# Line 1747  sub _get_next_token ($) { Line 1902  sub _get_next_token ($) {
1902            !!!cp (116);            !!!cp (116);
1903          }          }
1904          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{current_attribute}->{value} .= chr ($self->{next_char});
1905            $self->{read_until}->($self->{current_attribute}->{value},
1906                                  q["'=& >],
1907                                  length $self->{current_attribute}->{value});
1908    
1909          ## Stay in the state          ## Stay in the state
1910          !!!next-input-character;          !!!next-input-character;
1911          redo A;          redo A;
1912        }        }
     } 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;  
1913      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1914        if ($self->{next_char} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1915            $self->{next_char} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
# Line 1810  sub _get_next_token ($) { Line 1947  sub _get_next_token ($) {
1947          $self->{state} = SELF_CLOSING_START_TAG_STATE;          $self->{state} = SELF_CLOSING_START_TAG_STATE;
1948          !!!next-input-character;          !!!next-input-character;
1949          redo A;          redo A;
1950          } elsif ($self->{next_char} == -1) {
1951            !!!parse-error (type => 'unclosed tag');
1952            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1953              !!!cp (122.3);
1954              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1955            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1956              if ($self->{current_token}->{attributes}) {
1957                !!!cp (122.1);
1958                !!!parse-error (type => 'end tag attribute');
1959              } else {
1960                ## NOTE: This state should never be reached.
1961                !!!cp (122.2);
1962              }
1963            } else {
1964              die "$0: $self->{current_token}->{type}: Unknown token type";
1965            }
1966            $self->{state} = DATA_STATE;
1967            ## Reconsume.
1968            !!!emit ($self->{current_token}); # start tag or end tag
1969            redo A;
1970        } else {        } else {
1971          !!!cp ('124.1');          !!!cp ('124.1');
1972          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
# Line 1842  sub _get_next_token ($) { Line 1999  sub _get_next_token ($) {
1999          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
2000    
2001          redo A;          redo A;
2002          } elsif ($self->{next_char} == -1) {
2003            !!!parse-error (type => 'unclosed tag');
2004            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
2005              !!!cp (124.7);
2006              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
2007            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
2008              if ($self->{current_token}->{attributes}) {
2009                !!!cp (124.5);
2010                !!!parse-error (type => 'end tag attribute');
2011              } else {
2012                ## NOTE: This state should never be reached.
2013                !!!cp (124.6);
2014              }
2015            } else {
2016              die "$0: $self->{current_token}->{type}: Unknown token type";
2017            }
2018            $self->{state} = DATA_STATE;
2019            ## Reconsume.
2020            !!!emit ($self->{current_token}); # start tag or end tag
2021            redo A;
2022        } else {        } else {
2023          !!!cp ('124.4');          !!!cp ('124.4');
2024          !!!parse-error (type => 'nestc');          !!!parse-error (type => 'nestc');
# Line 1852  sub _get_next_token ($) { Line 2029  sub _get_next_token ($) {
2029        }        }
2030      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
2031        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
         
       ## NOTE: Set by the previous state  
       #my $token = {type => COMMENT_TOKEN, data => ''};  
2032    
2033        BC: {        ## NOTE: Unlike spec's "bogus comment state", this implementation
2034          if ($self->{next_char} == 0x003E) { # >        ## consumes characters one-by-one basis.
2035            !!!cp (124);        
2036            $self->{state} = DATA_STATE;        if ($self->{next_char} == 0x003E) { # >
2037            !!!next-input-character;          !!!cp (124);
2038            $self->{state} = DATA_STATE;
2039            !!!emit ($self->{current_token}); # comment          !!!next-input-character;
   
           redo A;  
         } elsif ($self->{next_char} == -1) {  
           !!!cp (125);  
           $self->{state} = DATA_STATE;  
           ## reconsume  
2040    
2041            !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2042            redo A;
2043          } elsif ($self->{next_char} == -1) {
2044            !!!cp (125);
2045            $self->{state} = DATA_STATE;
2046            ## reconsume
2047    
2048            redo A;          !!!emit ($self->{current_token}); # comment
2049          } else {          redo A;
2050            !!!cp (126);        } else {
2051            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment          !!!cp (126);
2052            !!!next-input-character;          $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2053            redo BC;          $self->{read_until}->($self->{current_token}->{data},
2054          }                                q[>],
2055        } # BC                                length $self->{current_token}->{data});
2056    
2057        die "$0: _get_next_token: unexpected case [BC]";          ## Stay in the state.
2058            !!!next-input-character;
2059            redo A;
2060          }
2061      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2062        ## (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};  
2063                
2064        if ($self->{next_char} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2065            !!!cp (133);
2066            $self->{state} = MD_HYPHEN_STATE;
2067          !!!next-input-character;          !!!next-input-character;
2068          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);  
         }  
2069        } elsif ($self->{next_char} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
2070                 $self->{next_char} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
2071            ## ASCII case-insensitive.
2072            !!!cp (130);
2073            $self->{state} = MD_DOCTYPE_STATE;
2074            $self->{state_keyword} = chr $self->{next_char};
2075          !!!next-input-character;          !!!next-input-character;
2076          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);  
         }  
2077        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2078                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2079                 $self->{next_char} == 0x005B) { # [                 $self->{next_char} == 0x005B) { # [
2080            !!!cp (135.4);                
2081            $self->{state} = MD_CDATA_STATE;
2082            $self->{state_keyword} = '[';
2083          !!!next-input-character;          !!!next-input-character;
2084          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);  
         }  
2085        } else {        } else {
2086          !!!cp (136);          !!!cp (136);
2087        }        }
2088    
2089        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment',
2090        $self->{next_char} = shift @next_char;                        line => $self->{line_prev},
2091        !!!back-next-input-character (@next_char);                        column => $self->{column_prev} - 1);
2092          ## Reconsume.
2093        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2094        $self->{current_token} = {type => COMMENT_TOKEN, data => '',        $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2095                                  line => $l, column => $c,                                  line => $self->{line_prev},
2096                                    column => $self->{column_prev} - 1,
2097                                 };                                 };
2098        redo A;        redo A;
2099              } elsif ($self->{state} == MD_HYPHEN_STATE) {
2100        ## ISSUE: typos in spec: chacacters, is is a parse error        if ($self->{next_char} == 0x002D) { # -
2101        ## 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);
2102            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2103                                      line => $self->{line_prev},
2104                                      column => $self->{column_prev} - 2,
2105                                     };
2106            $self->{state} = COMMENT_START_STATE;
2107            !!!next-input-character;
2108            redo A;
2109          } else {
2110            !!!cp (128);
2111            !!!parse-error (type => 'bogus comment',
2112                            line => $self->{line_prev},
2113                            column => $self->{column_prev} - 2);
2114            $self->{state} = BOGUS_COMMENT_STATE;
2115            ## Reconsume.
2116            $self->{current_token} = {type => COMMENT_TOKEN,
2117                                      data => '-',
2118                                      line => $self->{line_prev},
2119                                      column => $self->{column_prev} - 2,
2120                                     };
2121            redo A;
2122          }
2123        } elsif ($self->{state} == MD_DOCTYPE_STATE) {
2124          ## ASCII case-insensitive.
2125          if ($self->{next_char} == [
2126                undef,
2127                0x004F, # O
2128                0x0043, # C
2129                0x0054, # T
2130                0x0059, # Y
2131                0x0050, # P
2132              ]->[length $self->{state_keyword}] or
2133              $self->{next_char} == [
2134                undef,
2135                0x006F, # o
2136                0x0063, # c
2137                0x0074, # t
2138                0x0079, # y
2139                0x0070, # p
2140              ]->[length $self->{state_keyword}]) {
2141            !!!cp (131);
2142            ## Stay in the state.
2143            $self->{state_keyword} .= chr $self->{next_char};
2144            !!!next-input-character;
2145            redo A;
2146          } elsif ((length $self->{state_keyword}) == 6 and
2147                   ($self->{next_char} == 0x0045 or # E
2148                    $self->{next_char} == 0x0065)) { # e
2149            !!!cp (129);
2150            $self->{state} = DOCTYPE_STATE;
2151            $self->{current_token} = {type => DOCTYPE_TOKEN,
2152                                      quirks => 1,
2153                                      line => $self->{line_prev},
2154                                      column => $self->{column_prev} - 7,
2155                                     };
2156            !!!next-input-character;
2157            redo A;
2158          } else {
2159            !!!cp (132);        
2160            !!!parse-error (type => 'bogus comment',
2161                            line => $self->{line_prev},
2162                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2163            $self->{state} = BOGUS_COMMENT_STATE;
2164            ## Reconsume.
2165            $self->{current_token} = {type => COMMENT_TOKEN,
2166                                      data => $self->{state_keyword},
2167                                      line => $self->{line_prev},
2168                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2169                                     };
2170            redo A;
2171          }
2172        } elsif ($self->{state} == MD_CDATA_STATE) {
2173          if ($self->{next_char} == {
2174                '[' => 0x0043, # C
2175                '[C' => 0x0044, # D
2176                '[CD' => 0x0041, # A
2177                '[CDA' => 0x0054, # T
2178                '[CDAT' => 0x0041, # A
2179              }->{$self->{state_keyword}}) {
2180            !!!cp (135.1);
2181            ## Stay in the state.
2182            $self->{state_keyword} .= chr $self->{next_char};
2183            !!!next-input-character;
2184            redo A;
2185          } elsif ($self->{state_keyword} eq '[CDATA' and
2186                   $self->{next_char} == 0x005B) { # [
2187            !!!cp (135.2);
2188            $self->{current_token} = {type => CHARACTER_TOKEN,
2189                                      data => '',
2190                                      line => $self->{line_prev},
2191                                      column => $self->{column_prev} - 7};
2192            $self->{state} = CDATA_SECTION_STATE;
2193            !!!next-input-character;
2194            redo A;
2195          } else {
2196            !!!cp (135.3);
2197            !!!parse-error (type => 'bogus comment',
2198                            line => $self->{line_prev},
2199                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2200            $self->{state} = BOGUS_COMMENT_STATE;
2201            ## Reconsume.
2202            $self->{current_token} = {type => COMMENT_TOKEN,
2203                                      data => $self->{state_keyword},
2204                                      line => $self->{line_prev},
2205                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2206                                     };
2207            redo A;
2208          }
2209      } elsif ($self->{state} == COMMENT_START_STATE) {      } elsif ($self->{state} == COMMENT_START_STATE) {
2210        if ($self->{next_char} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2211          !!!cp (137);          !!!cp (137);
# Line 2097  sub _get_next_token ($) { Line 2288  sub _get_next_token ($) {
2288        } else {        } else {
2289          !!!cp (147);          !!!cp (147);
2290          $self->{current_token}->{data} .= chr ($self->{next_char}); # comment          $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2291            $self->{read_until}->($self->{current_token}->{data},
2292                                  q[-],
2293                                  length $self->{current_token}->{data});
2294    
2295          ## Stay in the state          ## Stay in the state
2296          !!!next-input-character;          !!!next-input-character;
2297          redo A;          redo A;
# Line 2281  sub _get_next_token ($) { Line 2476  sub _get_next_token ($) {
2476          redo A;          redo A;
2477        } elsif ($self->{next_char} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
2478                 $self->{next_char} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
2479            $self->{state} = PUBLIC_STATE;
2480            $self->{state_keyword} = chr $self->{next_char};
2481          !!!next-input-character;          !!!next-input-character;
2482          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);  
         }  
   
         #  
2483        } elsif ($self->{next_char} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
2484                 $self->{next_char} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
2485            $self->{state} = SYSTEM_STATE;
2486            $self->{state_keyword} = chr $self->{next_char};
2487          !!!next-input-character;          !!!next-input-character;
2488          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);  
         }  
   
         #  
2489        } else {        } else {
2490          !!!cp (180);          !!!cp (180);
2491            !!!parse-error (type => 'string after DOCTYPE name');
2492            $self->{current_token}->{quirks} = 1;
2493    
2494            $self->{state} = BOGUS_DOCTYPE_STATE;
2495          !!!next-input-character;          !!!next-input-character;
2496          #          redo A;
2497        }        }
2498        } elsif ($self->{state} == PUBLIC_STATE) {
2499          ## ASCII case-insensitive
2500          if ($self->{next_char} == [
2501                undef,
2502                0x0055, # U
2503                0x0042, # B
2504                0x004C, # L
2505                0x0049, # I
2506              ]->[length $self->{state_keyword}] or
2507              $self->{next_char} == [
2508                undef,
2509                0x0075, # u
2510                0x0062, # b
2511                0x006C, # l
2512                0x0069, # i
2513              ]->[length $self->{state_keyword}]) {
2514            !!!cp (175);
2515            ## Stay in the state.
2516            $self->{state_keyword} .= chr $self->{next_char};
2517            !!!next-input-character;
2518            redo A;
2519          } elsif ((length $self->{state_keyword}) == 5 and
2520                   ($self->{next_char} == 0x0043 or # C
2521                    $self->{next_char} == 0x0063)) { # c
2522            !!!cp (168);
2523            $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2524            !!!next-input-character;
2525            redo A;
2526          } else {
2527            !!!cp (169);
2528            !!!parse-error (type => 'string after DOCTYPE name',
2529                            line => $self->{line_prev},
2530                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2531            $self->{current_token}->{quirks} = 1;
2532    
2533        !!!parse-error (type => 'string after DOCTYPE name');          $self->{state} = BOGUS_DOCTYPE_STATE;
2534        $self->{current_token}->{quirks} = 1;          ## Reconsume.
2535            redo A;
2536          }
2537        } elsif ($self->{state} == SYSTEM_STATE) {
2538          ## ASCII case-insensitive
2539          if ($self->{next_char} == [
2540                undef,
2541                0x0059, # Y
2542                0x0053, # S
2543                0x0054, # T
2544                0x0045, # E
2545              ]->[length $self->{state_keyword}] or
2546              $self->{next_char} == [
2547                undef,
2548                0x0079, # y
2549                0x0073, # s
2550                0x0074, # t
2551                0x0065, # e
2552              ]->[length $self->{state_keyword}]) {
2553            !!!cp (170);
2554            ## Stay in the state.
2555            $self->{state_keyword} .= chr $self->{next_char};
2556            !!!next-input-character;
2557            redo A;
2558          } elsif ((length $self->{state_keyword}) == 5 and
2559                   ($self->{next_char} == 0x004D or # M
2560                    $self->{next_char} == 0x006D)) { # m
2561            !!!cp (171);
2562            $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2563            !!!next-input-character;
2564            redo A;
2565          } else {
2566            !!!cp (172);
2567            !!!parse-error (type => 'string after DOCTYPE name',
2568                            line => $self->{line_prev},
2569                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2570            $self->{current_token}->{quirks} = 1;
2571    
2572        $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2573        # next-input-character is already done          ## Reconsume.
2574        redo A;          redo A;
2575          }
2576      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2577        if ({        if ({
2578              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
# Line 2451  sub _get_next_token ($) { Line 2657  sub _get_next_token ($) {
2657          !!!cp (190);          !!!cp (190);
2658          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2659              .= chr $self->{next_char};              .= chr $self->{next_char};
2660            $self->{read_until}->($self->{current_token}->{public_identifier},
2661                                  q[">],
2662                                  length $self->{current_token}->{public_identifier});
2663    
2664          ## Stay in the state          ## Stay in the state
2665          !!!next-input-character;          !!!next-input-character;
2666          redo A;          redo A;
# Line 2487  sub _get_next_token ($) { Line 2697  sub _get_next_token ($) {
2697          !!!cp (194);          !!!cp (194);
2698          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2699              .= chr $self->{next_char};              .= chr $self->{next_char};
2700            $self->{read_until}->($self->{current_token}->{public_identifier},
2701                                  q['>],
2702                                  length $self->{current_token}->{public_identifier});
2703    
2704          ## Stay in the state          ## Stay in the state
2705          !!!next-input-character;          !!!next-input-character;
2706          redo A;          redo A;
# Line 2599  sub _get_next_token ($) { Line 2813  sub _get_next_token ($) {
2813          redo A;          redo A;
2814        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2815          !!!cp (208);          !!!cp (208);
2816          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2817    
2818          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2819          !!!next-input-character;          !!!next-input-character;
# Line 2623  sub _get_next_token ($) { Line 2837  sub _get_next_token ($) {
2837          !!!cp (210);          !!!cp (210);
2838          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2839              .= chr $self->{next_char};              .= chr $self->{next_char};
2840            $self->{read_until}->($self->{current_token}->{system_identifier},
2841                                  q[">],
2842                                  length $self->{current_token}->{system_identifier});
2843    
2844          ## Stay in the state          ## Stay in the state
2845          !!!next-input-character;          !!!next-input-character;
2846          redo A;          redo A;
# Line 2635  sub _get_next_token ($) { Line 2853  sub _get_next_token ($) {
2853          redo A;          redo A;
2854        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2855          !!!cp (212);          !!!cp (212);
2856          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2857    
2858          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2859          !!!next-input-character;          !!!next-input-character;
# Line 2659  sub _get_next_token ($) { Line 2877  sub _get_next_token ($) {
2877          !!!cp (214);          !!!cp (214);
2878          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2879              .= chr $self->{next_char};              .= chr $self->{next_char};
2880            $self->{read_until}->($self->{current_token}->{system_identifier},
2881                                  q['>],
2882                                  length $self->{current_token}->{system_identifier});
2883    
2884          ## Stay in the state          ## Stay in the state
2885          !!!next-input-character;          !!!next-input-character;
2886          redo A;          redo A;
# Line 2683  sub _get_next_token ($) { Line 2905  sub _get_next_token ($) {
2905        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
2906          !!!cp (217);          !!!cp (217);
2907          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
   
2908          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2909          ## reconsume          ## reconsume
2910    
# Line 2720  sub _get_next_token ($) { Line 2941  sub _get_next_token ($) {
2941          redo A;          redo A;
2942        } else {        } else {
2943          !!!cp (221);          !!!cp (221);
2944            my $s = '';
2945            $self->{read_until}->($s, q[>], 0);
2946    
2947          ## Stay in the state          ## Stay in the state
2948          !!!next-input-character;          !!!next-input-character;
2949          redo A;          redo A;
2950        }        }
2951      } elsif ($self->{state} == CDATA_BLOCK_STATE) {      } elsif ($self->{state} == CDATA_SECTION_STATE) {
2952        my $s = '';        ## NOTE: "CDATA section state" in the state is jointly implemented
2953          ## by three states, |CDATA_SECTION_STATE|, |CDATA_SECTION_MSE1_STATE|,
2954          ## and |CDATA_SECTION_MSE2_STATE|.
2955                
2956        my ($l, $c) = ($self->{line}, $self->{column});        if ($self->{next_char} == 0x005D) { # ]
2957            !!!cp (221.1);
2958        CS: while ($self->{next_char} != -1) {          $self->{state} = CDATA_SECTION_MSE1_STATE;
         if ($self->{next_char} == 0x005D) { # ]  
           !!!next-input-character;  
           if ($self->{next_char} == 0x005D) { # ]  
             !!!next-input-character;  
             MDC: {  
               if ($self->{next_char} == 0x003E) { # >  
                 !!!cp (221.1);  
                 !!!next-input-character;  
                 last CS;  
               } elsif ($self->{next_char} == 0x005D) { # ]  
                 !!!cp (221.2);  
                 $s .= ']';  
                 !!!next-input-character;  
                 redo MDC;  
               } else {  
                 !!!cp (221.3);  
                 $s .= ']]';  
                 #  
               }  
             } # MDC  
           } else {  
             !!!cp (221.4);  
             $s .= ']';  
             #  
           }  
         } else {  
           !!!cp (221.5);  
           #  
         }  
         $s .= chr $self->{next_char};  
2959          !!!next-input-character;          !!!next-input-character;
2960        } # CS          redo A;
2961          } elsif ($self->{next_char} == -1) {
2962            $self->{state} = DATA_STATE;
2963            !!!next-input-character;
2964            if (length $self->{current_token}->{data}) { # character
2965              !!!cp (221.2);
2966              !!!emit ($self->{current_token}); # character
2967            } else {
2968              !!!cp (221.3);
2969              ## No token to emit. $self->{current_token} is discarded.
2970            }        
2971            redo A;
2972          } else {
2973            !!!cp (221.4);
2974            $self->{current_token}->{data} .= chr $self->{next_char};
2975            $self->{read_until}->($self->{current_token}->{data},
2976                                  q<]>,
2977                                  length $self->{current_token}->{data});
2978    
2979        $self->{state} = DATA_STATE;          ## Stay in the state.
2980        ## next-input-character done or EOF, which is reconsumed.          !!!next-input-character;
2981            redo A;
2982          }
2983    
2984        if (length $s) {        ## ISSUE: "text tokens" in spec.
2985        } elsif ($self->{state} == CDATA_SECTION_MSE1_STATE) {
2986          if ($self->{next_char} == 0x005D) { # ]
2987            !!!cp (221.5);
2988            $self->{state} = CDATA_SECTION_MSE2_STATE;
2989            !!!next-input-character;
2990            redo A;
2991          } else {
2992          !!!cp (221.6);          !!!cp (221.6);
2993          !!!emit ({type => CHARACTER_TOKEN, data => $s,          $self->{current_token}->{data} .= ']';
2994                    line => $l, column => $c});          $self->{state} = CDATA_SECTION_STATE;
2995            ## Reconsume.
2996            redo A;
2997          }
2998        } elsif ($self->{state} == CDATA_SECTION_MSE2_STATE) {
2999          if ($self->{next_char} == 0x003E) { # >
3000            $self->{state} = DATA_STATE;
3001            !!!next-input-character;
3002            if (length $self->{current_token}->{data}) { # character
3003              !!!cp (221.7);
3004              !!!emit ($self->{current_token}); # character
3005            } else {
3006              !!!cp (221.8);
3007              ## No token to emit. $self->{current_token} is discarded.
3008            }
3009            redo A;
3010          } elsif ($self->{next_char} == 0x005D) { # ]
3011            !!!cp (221.9); # character
3012            $self->{current_token}->{data} .= ']'; ## Add first "]" of "]]]".
3013            ## Stay in the state.
3014            !!!next-input-character;
3015            redo A;
3016        } else {        } else {
3017          !!!cp (221.7);          !!!cp (221.11);
3018            $self->{current_token}->{data} .= ']]'; # character
3019            $self->{state} = CDATA_SECTION_STATE;
3020            ## Reconsume.
3021            redo A;
3022          }
3023        } elsif ($self->{state} == ENTITY_STATE) {
3024          if ({
3025            0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
3026            0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, &
3027            $self->{entity_additional} => 1,
3028          }->{$self->{next_char}}) {
3029            !!!cp (1001);
3030            ## Don't consume
3031            ## No error
3032            ## Return nothing.
3033            #
3034          } elsif ($self->{next_char} == 0x0023) { # #
3035            !!!cp (999);
3036            $self->{state} = ENTITY_HASH_STATE;
3037            $self->{state_keyword} = '#';
3038            !!!next-input-character;
3039            redo A;
3040          } elsif ((0x0041 <= $self->{next_char} and
3041                    $self->{next_char} <= 0x005A) or # A..Z
3042                   (0x0061 <= $self->{next_char} and
3043                    $self->{next_char} <= 0x007A)) { # a..z
3044            !!!cp (998);
3045            require Whatpm::_NamedEntityList;
3046            $self->{state} = ENTITY_NAME_STATE;
3047            $self->{state_keyword} = chr $self->{next_char};
3048            $self->{entity__value} = $self->{state_keyword};
3049            $self->{entity__match} = 0;
3050            !!!next-input-character;
3051            redo A;
3052          } else {
3053            !!!cp (1027);
3054            !!!parse-error (type => 'bare ero');
3055            ## Return nothing.
3056            #
3057        }        }
3058    
3059        redo A;        ## NOTE: No character is consumed by the "consume a character
3060          ## reference" algorithm.  In other word, there is an "&" character
3061        ## ISSUE: "text tokens" in spec.        ## that does not introduce a character reference, which would be
3062        ## TODO: Streaming support        ## appended to the parent element or the attribute value in later
3063      } else {        ## process of the tokenizer.
3064        die "$0: $self->{state}: Unknown state";  
3065      }        if ($self->{prev_state} == DATA_STATE) {
3066    } # A            !!!cp (997);
3067            $self->{state} = $self->{prev_state};
3068    die "$0: _get_next_token: unexpected case";          ## Reconsume.
3069  } # _get_next_token          !!!emit ({type => CHARACTER_TOKEN, data => '&',
3070                      line => $self->{line_prev},
3071  sub _tokenize_attempt_to_consume_an_entity ($$$) {                    column => $self->{column_prev},
3072    my ($self, $in_attr, $additional) = @_;                   });
3073            redo A;
3074    my ($l, $c) = ($self->{line_prev}, $self->{column_prev});        } else {
3075            !!!cp (996);
3076            $self->{current_attribute}->{value} .= '&';
3077            $self->{state} = $self->{prev_state};
3078            ## Reconsume.
3079            redo A;
3080          }
3081        } elsif ($self->{state} == ENTITY_HASH_STATE) {
3082          if ($self->{next_char} == 0x0078 or # x
3083              $self->{next_char} == 0x0058) { # X
3084            !!!cp (995);
3085            $self->{state} = HEXREF_X_STATE;
3086            $self->{state_keyword} .= chr $self->{next_char};
3087            !!!next-input-character;
3088            redo A;
3089          } elsif (0x0030 <= $self->{next_char} and
3090                   $self->{next_char} <= 0x0039) { # 0..9
3091            !!!cp (994);
3092            $self->{state} = NCR_NUM_STATE;
3093            $self->{state_keyword} = $self->{next_char} - 0x0030;
3094            !!!next-input-character;
3095            redo A;
3096          } else {
3097            !!!parse-error (type => 'bare nero',
3098                            line => $self->{line_prev},
3099                            column => $self->{column_prev} - 1);
3100    
3101    if ({          ## NOTE: According to the spec algorithm, nothing is returned,
3102         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,          ## and then "&#" is appended to the parent element or the attribute
3103         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR          ## value in the later processing.
3104         $additional => 1,  
3105        }->{$self->{next_char}}) {          if ($self->{prev_state} == DATA_STATE) {
3106      !!!cp (1001);            !!!cp (1019);
3107      ## Don't consume            $self->{state} = $self->{prev_state};
3108      ## No error            ## Reconsume.
3109      return undef;            !!!emit ({type => CHARACTER_TOKEN,
3110    } elsif ($self->{next_char} == 0x0023) { # #                      data => '&#',
3111      !!!next-input-character;                      line => $self->{line_prev},
3112      if ($self->{next_char} == 0x0078 or # x                      column => $self->{column_prev} - 1,
3113          $self->{next_char} == 0x0058) { # X                     });
3114        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;  
3115          } else {          } else {
3116            !!!cp (1007);            !!!cp (993);
3117            !!!parse-error (type => 'no refc', line => $l, column => $c);            $self->{current_attribute}->{value} .= '&#';
3118              $self->{state} = $self->{prev_state};
3119              ## Reconsume.
3120              redo A;
3121          }          }
3122          }
3123          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {      } elsif ($self->{state} == NCR_NUM_STATE) {
3124            !!!cp (1008);        if (0x0030 <= $self->{next_char} and
3125            !!!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  
3126          !!!cp (1012);          !!!cp (1012);
3127          $code *= 10;          $self->{state_keyword} *= 10;
3128          $code += $self->{next_char} - 0x0030;          $self->{state_keyword} += $self->{next_char} - 0x0030;
3129                    
3130            ## Stay in the state.
3131          !!!next-input-character;          !!!next-input-character;
3132        }          redo A;
3133          } elsif ($self->{next_char} == 0x003B) { # ;
       if ($self->{next_char} == 0x003B) { # ;  
3134          !!!cp (1013);          !!!cp (1013);
3135          !!!next-input-character;          !!!next-input-character;
3136            #
3137        } else {        } else {
3138          !!!cp (1014);          !!!cp (1014);
3139          !!!parse-error (type => 'no refc', line => $l, column => $c);          !!!parse-error (type => 'no refc');
3140            ## Reconsume.
3141            #
3142        }        }
3143    
3144          my $code = $self->{state_keyword};
3145          my $l = $self->{line_prev};
3146          my $c = $self->{column_prev};
3147        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3148          !!!cp (1015);          !!!cp (1015);
3149          !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);          !!!parse-error (type => 'invalid character reference',
3150                            text => (sprintf 'U+%04X', $code),
3151                            line => $l, column => $c);
3152          $code = 0xFFFD;          $code = 0xFFFD;
3153        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
3154          !!!cp (1016);          !!!cp (1016);
3155          !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);          !!!parse-error (type => 'invalid character reference',
3156                            text => (sprintf 'U-%08X', $code),
3157                            line => $l, column => $c);
3158          $code = 0xFFFD;          $code = 0xFFFD;
3159        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
3160          !!!cp (1017);          !!!cp (1017);
3161          !!!parse-error (type => 'CR character reference', line => $l, column => $c);          !!!parse-error (type => 'CR character reference',
3162                            line => $l, column => $c);
3163          $code = 0x000A;          $code = 0x000A;
3164        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3165          !!!cp (1018);          !!!cp (1018);
3166          !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);          !!!parse-error (type => 'C1 character reference',
3167                            text => (sprintf 'U+%04X', $code),
3168                            line => $l, column => $c);
3169          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3170        }        }
3171          
3172        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,        if ($self->{prev_state} == DATA_STATE) {
3173                line => $l, column => $c,          !!!cp (992);
3174               };          $self->{state} = $self->{prev_state};
3175      } else {          ## Reconsume.
3176        !!!cp (1019);          !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3177        !!!parse-error (type => 'bare nero', line => $l, column => $c);                    line => $l, column => $c,
3178        !!!back-next-input-character ($self->{next_char});                   });
3179        $self->{next_char} = 0x0023; # #          redo A;
3180        return undef;        } else {
3181      }          !!!cp (991);
3182    } elsif ((0x0041 <= $self->{next_char} and          $self->{current_attribute}->{value} .= chr $code;
3183              $self->{next_char} <= 0x005A) or          $self->{current_attribute}->{has_reference} = 1;
3184             (0x0061 <= $self->{next_char} and          $self->{state} = $self->{prev_state};
3185              $self->{next_char} <= 0x007A)) {          ## Reconsume.
3186      my $entity_name = chr $self->{next_char};          redo A;
3187      !!!next-input-character;        }
3188        } elsif ($self->{state} == HEXREF_X_STATE) {
3189      my $value = $entity_name;        if ((0x0030 <= $self->{next_char} and $self->{next_char} <= 0x0039) or
3190      my $match = 0;            (0x0041 <= $self->{next_char} and $self->{next_char} <= 0x0046) or
3191      require Whatpm::_NamedEntityList;            (0x0061 <= $self->{next_char} and $self->{next_char} <= 0x0066)) {
3192      our $EntityChar;          # 0..9, A..F, a..f
3193            !!!cp (990);
3194      while (length $entity_name < 30 and          $self->{state} = HEXREF_HEX_STATE;
3195             ## NOTE: Some number greater than the maximum length of entity name          $self->{state_keyword} = 0;
3196             ((0x0041 <= $self->{next_char} and # a          ## Reconsume.
3197               $self->{next_char} <= 0x005A) or # x          redo A;
3198              (0x0061 <= $self->{next_char} and # a        } else {
3199               $self->{next_char} <= 0x007A) or # z          !!!parse-error (type => 'bare hcro',
3200              (0x0030 <= $self->{next_char} and # 0                          line => $self->{line_prev},
3201               $self->{next_char} <= 0x0039) or # 9                          column => $self->{column_prev} - 2);
3202              $self->{next_char} == 0x003B)) { # ;  
3203        $entity_name .= chr $self->{next_char};          ## NOTE: According to the spec algorithm, nothing is returned,
3204        if (defined $EntityChar->{$entity_name}) {          ## and then "&#" followed by "X" or "x" is appended to the parent
3205          if ($self->{next_char} == 0x003B) { # ;          ## element or the attribute value in the later processing.
3206            !!!cp (1020);  
3207            $value = $EntityChar->{$entity_name};          if ($self->{prev_state} == DATA_STATE) {
3208            $match = 1;            !!!cp (1005);
3209            !!!next-input-character;            $self->{state} = $self->{prev_state};
3210            last;            ## Reconsume.
3211              !!!emit ({type => CHARACTER_TOKEN,
3212                        data => '&' . $self->{state_keyword},
3213                        line => $self->{line_prev},
3214                        column => $self->{column_prev} - length $self->{state_keyword},
3215                       });
3216              redo A;
3217          } else {          } else {
3218            !!!cp (1021);            !!!cp (989);
3219            $value = $EntityChar->{$entity_name};            $self->{current_attribute}->{value} .= '&' . $self->{state_keyword};
3220            $match = -1;            $self->{state} = $self->{prev_state};
3221              ## Reconsume.
3222              redo A;
3223            }
3224          }
3225        } elsif ($self->{state} == HEXREF_HEX_STATE) {
3226          if (0x0030 <= $self->{next_char} and $self->{next_char} <= 0x0039) {
3227            # 0..9
3228            !!!cp (1002);
3229            $self->{state_keyword} *= 0x10;
3230            $self->{state_keyword} += $self->{next_char} - 0x0030;
3231            ## Stay in the state.
3232            !!!next-input-character;
3233            redo A;
3234          } elsif (0x0061 <= $self->{next_char} and
3235                   $self->{next_char} <= 0x0066) { # a..f
3236            !!!cp (1003);
3237            $self->{state_keyword} *= 0x10;
3238            $self->{state_keyword} += $self->{next_char} - 0x0060 + 9;
3239            ## Stay in the state.
3240            !!!next-input-character;
3241            redo A;
3242          } elsif (0x0041 <= $self->{next_char} and
3243                   $self->{next_char} <= 0x0046) { # A..F
3244            !!!cp (1004);
3245            $self->{state_keyword} *= 0x10;
3246            $self->{state_keyword} += $self->{next_char} - 0x0040 + 9;
3247            ## Stay in the state.
3248            !!!next-input-character;
3249            redo A;
3250          } elsif ($self->{next_char} == 0x003B) { # ;
3251            !!!cp (1006);
3252            !!!next-input-character;
3253            #
3254          } else {
3255            !!!cp (1007);
3256            !!!parse-error (type => 'no refc',
3257                            line => $self->{line},
3258                            column => $self->{column});
3259            ## Reconsume.
3260            #
3261          }
3262    
3263          my $code = $self->{state_keyword};
3264          my $l = $self->{line_prev};
3265          my $c = $self->{column_prev};
3266          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3267            !!!cp (1008);
3268            !!!parse-error (type => 'invalid character reference',
3269                            text => (sprintf 'U+%04X', $code),
3270                            line => $l, column => $c);
3271            $code = 0xFFFD;
3272          } elsif ($code > 0x10FFFF) {
3273            !!!cp (1009);
3274            !!!parse-error (type => 'invalid character reference',
3275                            text => (sprintf 'U-%08X', $code),
3276                            line => $l, column => $c);
3277            $code = 0xFFFD;
3278          } elsif ($code == 0x000D) {
3279            !!!cp (1010);
3280            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
3281            $code = 0x000A;
3282          } elsif (0x80 <= $code and $code <= 0x9F) {
3283            !!!cp (1011);
3284            !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
3285            $code = $c1_entity_char->{$code};
3286          }
3287    
3288          if ($self->{prev_state} == DATA_STATE) {
3289            !!!cp (988);
3290            $self->{state} = $self->{prev_state};
3291            ## Reconsume.
3292            !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3293                      line => $l, column => $c,
3294                     });
3295            redo A;
3296          } else {
3297            !!!cp (987);
3298            $self->{current_attribute}->{value} .= chr $code;
3299            $self->{current_attribute}->{has_reference} = 1;
3300            $self->{state} = $self->{prev_state};
3301            ## Reconsume.
3302            redo A;
3303          }
3304        } elsif ($self->{state} == ENTITY_NAME_STATE) {
3305          if (length $self->{state_keyword} < 30 and
3306              ## NOTE: Some number greater than the maximum length of entity name
3307              ((0x0041 <= $self->{next_char} and # a
3308                $self->{next_char} <= 0x005A) or # x
3309               (0x0061 <= $self->{next_char} and # a
3310                $self->{next_char} <= 0x007A) or # z
3311               (0x0030 <= $self->{next_char} and # 0
3312                $self->{next_char} <= 0x0039) or # 9
3313               $self->{next_char} == 0x003B)) { # ;
3314            our $EntityChar;
3315            $self->{state_keyword} .= chr $self->{next_char};
3316            if (defined $EntityChar->{$self->{state_keyword}}) {
3317              if ($self->{next_char} == 0x003B) { # ;
3318                !!!cp (1020);
3319                $self->{entity__value} = $EntityChar->{$self->{state_keyword}};
3320                $self->{entity__match} = 1;
3321                !!!next-input-character;
3322                #
3323              } else {
3324                !!!cp (1021);
3325                $self->{entity__value} = $EntityChar->{$self->{state_keyword}};
3326                $self->{entity__match} = -1;
3327                ## Stay in the state.
3328                !!!next-input-character;
3329                redo A;
3330              }
3331            } else {
3332              !!!cp (1022);
3333              $self->{entity__value} .= chr $self->{next_char};
3334              $self->{entity__match} *= 2;
3335              ## Stay in the state.
3336            !!!next-input-character;            !!!next-input-character;
3337              redo A;
3338            }
3339          }
3340    
3341          my $data;
3342          my $has_ref;
3343          if ($self->{entity__match} > 0) {
3344            !!!cp (1023);
3345            $data = $self->{entity__value};
3346            $has_ref = 1;
3347            #
3348          } elsif ($self->{entity__match} < 0) {
3349            !!!parse-error (type => 'no refc');
3350            if ($self->{prev_state} != DATA_STATE and # in attribute
3351                $self->{entity__match} < -1) {
3352              !!!cp (1024);
3353              $data = '&' . $self->{state_keyword};
3354              #
3355            } else {
3356              !!!cp (1025);
3357              $data = $self->{entity__value};
3358              $has_ref = 1;
3359              #
3360          }          }
3361        } else {        } else {
3362          !!!cp (1022);          !!!cp (1026);
3363          $value .= chr $self->{next_char};          !!!parse-error (type => 'bare ero',
3364          $match *= 2;                          line => $self->{line_prev},
3365          !!!next-input-character;                          column => $self->{column_prev} - length $self->{state_keyword});
3366            $data = '&' . $self->{state_keyword};
3367            #
3368        }        }
3369      }    
3370              ## NOTE: In these cases, when a character reference is found,
3371      if ($match > 0) {        ## it is consumed and a character token is returned, or, otherwise,
3372        !!!cp (1023);        ## nothing is consumed and returned, according to the spec algorithm.
3373        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,        ## In this implementation, anything that has been examined by the
3374                line => $l, column => $c,        ## tokenizer is appended to the parent element or the attribute value
3375               };        ## as string, either literal string when no character reference or
3376      } elsif ($match < 0) {        ## entity-replaced string otherwise, in this stage, since any characters
3377        !!!parse-error (type => 'no refc', line => $l, column => $c);        ## that would not be consumed are appended in the data state or in an
3378        if ($in_attr and $match < -1) {        ## appropriate attribute value state anyway.
3379          !!!cp (1024);  
3380          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,        if ($self->{prev_state} == DATA_STATE) {
3381                  line => $l, column => $c,          !!!cp (986);
3382                 };          $self->{state} = $self->{prev_state};
3383        } else {          ## Reconsume.
3384          !!!cp (1025);          !!!emit ({type => CHARACTER_TOKEN,
3385          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,                    data => $data,
3386                  line => $l, column => $c,                    line => $self->{line_prev},
3387                 };                    column => $self->{column_prev} + 1 - length $self->{state_keyword},
3388                     });
3389            redo A;
3390          } else {
3391            !!!cp (985);
3392            $self->{current_attribute}->{value} .= $data;
3393            $self->{current_attribute}->{has_reference} = 1 if $has_ref;
3394            $self->{state} = $self->{prev_state};
3395            ## Reconsume.
3396            redo A;
3397        }        }
3398      } else {      } else {
3399        !!!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,  
              };  
3400      }      }
3401    } else {    } # A  
3402      !!!cp (1027);  
3403      ## no characters are consumed    die "$0: _get_next_token: unexpected case";
3404      !!!parse-error (type => 'bare ero', line => $l, column => $c);  } # _get_next_token
     return undef;  
   }  
 } # _tokenize_attempt_to_consume_an_entity  
3405    
3406  sub _initialize_tree_constructor ($) {  sub _initialize_tree_constructor ($) {
3407    my $self = shift;    my $self = shift;
# Line 3000  sub _initialize_tree_constructor ($) { Line 3410  sub _initialize_tree_constructor ($) {
3410    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3411    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3412    $self->{document}->manakai_is_html (1); # MUST    $self->{document}->manakai_is_html (1); # MUST
3413      $self->{document}->set_user_data (manakai_source_line => 1);
3414      $self->{document}->set_user_data (manakai_source_column => 1);
3415  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3416    
3417  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 3054  sub _tree_construction_initial ($) { Line 3466  sub _tree_construction_initial ($) {
3466        ## language.        ## language.
3467        my $doctype_name = $token->{name};        my $doctype_name = $token->{name};
3468        $doctype_name = '' unless defined $doctype_name;        $doctype_name = '' unless defined $doctype_name;
3469        $doctype_name =~ tr/a-z/A-Z/;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3470        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
           defined $token->{public_identifier} or  
3471            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3472          !!!cp ('t1');          !!!cp ('t1');
3473          !!!parse-error (type => 'not HTML5', token => $token);          !!!parse-error (type => 'not HTML5', token => $token);
3474        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3475          !!!cp ('t2');          !!!cp ('t2');
         ## ISSUE: ASCII case-insensitive? (in fact it does not matter)  
3476          !!!parse-error (type => 'not HTML5', token => $token);          !!!parse-error (type => 'not HTML5', token => $token);
3477          } elsif (defined $token->{public_identifier}) {
3478            if ($token->{public_identifier} eq 'XSLT-compat') {
3479              !!!cp ('t1.2');
3480              !!!parse-error (type => 'XSLT-compat', token => $token,
3481                              level => $self->{level}->{should});
3482            } else {
3483              !!!parse-error (type => 'not HTML5', token => $token);
3484            }
3485        } else {        } else {
3486          !!!cp ('t3');          !!!cp ('t3');
3487            #
3488        }        }
3489                
3490        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
# Line 3086  sub _tree_construction_initial ($) { Line 3505  sub _tree_construction_initial ($) {
3505        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3506          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3507          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3508          if ({          my $prefix = [
3509            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3510            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3511            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3512            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3513            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3514            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3515            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3516            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3517            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3518            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3519            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3520            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3521            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3522            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3523            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3524            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3525            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3526            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3527            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3528            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3529            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3530            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3531            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3532            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3533            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3534            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3535            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3536            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3537            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3538            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3539            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3540            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3541            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3542            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3543            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3544            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3545            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3546            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3547            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3548            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3549            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3550            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3551            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3552            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3553            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3554            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3555            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3556            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3557            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3558            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3559            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3560            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD W3 HTML//",
3561            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3562            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3563            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3564            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,          ]; # $prefix
3565            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,          my $match;
3566            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,          for (@$prefix) {
3567            "-//W3C//DTD HTML 3.2//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3568            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,              $match = 1;
3569            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,              last;
3570            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            }
3571            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,          }
3572            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,          if ($match or
3573            "-//W3C//DTD W3 HTML//EN" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3574            "-//W3O//DTD W3 HTML 3.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3575            "-//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}) {  
3576            !!!cp ('t5');            !!!cp ('t5');
3577            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3578          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3579                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3580            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3581              !!!cp ('t6');              !!!cp ('t6');
3582              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
# Line 3171  sub _tree_construction_initial ($) { Line 3584  sub _tree_construction_initial ($) {
3584              !!!cp ('t7');              !!!cp ('t7');
3585              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3586            }            }
3587          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3588                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3589            !!!cp ('t8');            !!!cp ('t8');
3590            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3591          } else {          } else {
# Line 3185  sub _tree_construction_initial ($) { Line 3598  sub _tree_construction_initial ($) {
3598          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3599          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3600          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") {
3601            ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"            ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3602              ## marked as quirks.
3603            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3604            !!!cp ('t11');            !!!cp ('t11');
3605          } else {          } else {
# Line 3357  sub _reset_insertion_mode ($) { Line 3771  sub _reset_insertion_mode ($) {
3771        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3772          $last = 1;          $last = 1;
3773          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3774            if ($self->{inner_html_node}->[1] & TABLE_CELL_EL) {            !!!cp ('t28');
3775              !!!cp ('t27');            $node = $self->{inner_html_node};
3776              #          } else {
3777            } else {            die "_reset_insertion_mode: t27";
             !!!cp ('t28');  
             $node = $self->{inner_html_node};  
           }  
3778          }          }
3779        }        }
3780              
3781      ## Step 4..14        ## Step 4..14
3782      my $new_mode;        my $new_mode;
3783      if ($node->[1] & FOREIGN_EL) {        if ($node->[1] & FOREIGN_EL) {
3784        ## NOTE: Strictly spaking, the line below only applies to MathML and          !!!cp ('t28.1');
3785        ## SVG elements.  Currently the HTML syntax supports only MathML and          ## NOTE: Strictly spaking, the line below only applies to MathML and
3786        ## SVG elements as foreigners.          ## SVG elements.  Currently the HTML syntax supports only MathML and
3787        $new_mode = $self->{insertion_mode} | IN_FOREIGN_CONTENT_IM;          ## SVG elements as foreigners.
3788        ## ISSUE: What is set as the secondary insertion mode?          $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3789      } else {        } elsif ($node->[1] & TABLE_CELL_EL) {
3790        $new_mode = {          if ($last) {
3791              !!!cp ('t28.2');
3792              #
3793            } else {
3794              !!!cp ('t28.3');
3795              $new_mode = IN_CELL_IM;
3796            }
3797          } else {
3798            !!!cp ('t28.4');
3799            $new_mode = {
3800                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3801                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3802                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
                       td => IN_CELL_IM,  
                       th => IN_CELL_IM,  
3803                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3804                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3805                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 3393  sub _reset_insertion_mode ($) { Line 3811  sub _reset_insertion_mode ($) {
3811                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3812                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3813                       }->{$node->[0]->manakai_local_name};                       }->{$node->[0]->manakai_local_name};
3814      }        }
3815      $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3816                
3817        ## Step 15        ## Step 15
3818        if ($node->[1] & HTML_EL) {        if ($node->[1] & HTML_EL) {
# Line 3568  sub _tree_construction_main ($) { Line 3986  sub _tree_construction_main ($) {
3986        ## NOTE: An end-of-file token.        ## NOTE: An end-of-file token.
3987        if ($content_model_flag == CDATA_CONTENT_MODEL) {        if ($content_model_flag == CDATA_CONTENT_MODEL) {
3988          !!!cp ('t43');          !!!cp ('t43');
3989          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);          !!!parse-error (type => 'in CDATA:#eof', token => $token);
3990        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3991          !!!cp ('t44');          !!!cp ('t44');
3992          !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);          !!!parse-error (type => 'in RCDATA:#eof', token => $token);
3993        } else {        } else {
3994          die "$0: $content_model_flag in parse_rcdata";          die "$0: $content_model_flag in parse_rcdata";
3995        }        }
# Line 3608  sub _tree_construction_main ($) { Line 4026  sub _tree_construction_main ($) {
4026        ## Ignore the token        ## Ignore the token
4027      } else {      } else {
4028        !!!cp ('t48');        !!!cp ('t48');
4029        !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);        !!!parse-error (type => 'in CDATA:#eof', token => $token);
4030        ## ISSUE: And ignore?        ## ISSUE: And ignore?
4031        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
4032      }      }
# Line 3659  sub _tree_construction_main ($) { Line 4077  sub _tree_construction_main ($) {
4077        } # AFE        } # AFE
4078        unless (defined $formatting_element) {        unless (defined $formatting_element) {
4079          !!!cp ('t53');          !!!cp ('t53');
4080          !!!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);
4081          ## Ignore the token          ## Ignore the token
4082          !!!next-token;          !!!next-token;
4083          return;          return;
# Line 3676  sub _tree_construction_main ($) { Line 4094  sub _tree_construction_main ($) {
4094              last INSCOPE;              last INSCOPE;
4095            } else { # in open elements but not in scope            } else { # in open elements but not in scope
4096              !!!cp ('t55');              !!!cp ('t55');
4097              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},              !!!parse-error (type => 'unmatched end tag',
4098                                text => $token->{tag_name},
4099                              token => $end_tag_token);                              token => $end_tag_token);
4100              ## Ignore the token              ## Ignore the token
4101              !!!next-token;              !!!next-token;
# Line 3689  sub _tree_construction_main ($) { Line 4108  sub _tree_construction_main ($) {
4108        } # INSCOPE        } # INSCOPE
4109        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
4110          !!!cp ('t57');          !!!cp ('t57');
4111          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},          !!!parse-error (type => 'unmatched end tag',
4112                            text => $token->{tag_name},
4113                          token => $end_tag_token);                          token => $end_tag_token);
4114          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
4115          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
# Line 3698  sub _tree_construction_main ($) { Line 4118  sub _tree_construction_main ($) {
4118        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
4119          !!!cp ('t58');          !!!cp ('t58');
4120          !!!parse-error (type => 'not closed',          !!!parse-error (type => 'not closed',
4121                          value => $self->{open_elements}->[-1]->[0]                          text => $self->{open_elements}->[-1]->[0]
4122                              ->manakai_local_name,                              ->manakai_local_name,
4123                          token => $end_tag_token);                          token => $end_tag_token);
4124        }        }
# Line 3907  sub _tree_construction_main ($) { Line 4327  sub _tree_construction_main ($) {
4327    B: while (1) {    B: while (1) {
4328      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
4329        !!!cp ('t73');        !!!cp ('t73');
4330        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4331        ## Ignore the token        ## Ignore the token
4332        ## Stay in the phase        ## Stay in the phase
4333        !!!next-token;        !!!next-token;
# Line 3916  sub _tree_construction_main ($) { Line 4336  sub _tree_construction_main ($) {
4336               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
4337        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4338          !!!cp ('t79');          !!!cp ('t79');
4339          !!!parse-error (type => 'after html:html', token => $token);          !!!parse-error (type => 'after html', text => 'html', token => $token);
4340          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
4341        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4342          !!!cp ('t80');          !!!cp ('t80');
4343          !!!parse-error (type => 'after html:html', token => $token);          !!!parse-error (type => 'after html', text => 'html', token => $token);
4344          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4345        } else {        } else {
4346          !!!cp ('t81');          !!!cp ('t81');
# Line 3971  sub _tree_construction_main ($) { Line 4391  sub _tree_construction_main ($) {
4391            #            #
4392          } elsif ({          } elsif ({
4393                    b => 1, big => 1, blockquote => 1, body => 1, br => 1,                    b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4394                    center => 1, code => 1, dd => 1, div => 1, dl => 1, em => 1,                    center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4395                    embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1, ## No h4!                    em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4396                    h5 => 1, h6 => 1, head => 1, hr => 1, i => 1, img => 1,                    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4397                    li => 1, menu => 1, meta => 1, nobr => 1, p => 1, pre => 1,                    img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4398                    ruby => 1, s => 1, small => 1, span => 1, strong => 1,                    nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4399                    sub => 1, sup => 1, table => 1, tt => 1, u => 1, ul => 1,                    small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4400                    var => 1,                    sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4401                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
4402            !!!cp ('t87.2');            !!!cp ('t87.2');
4403            !!!parse-error (type => 'not closed',            !!!parse-error (type => 'not closed',
4404                            value => $self->{open_elements}->[-1]->[0]                            text => $self->{open_elements}->[-1]->[0]
4405                                ->manakai_local_name,                                ->manakai_local_name,
4406                            token => $token);                            token => $token);
4407    
# Line 4057  sub _tree_construction_main ($) { Line 4477  sub _tree_construction_main ($) {
4477          !!!cp ('t87.5');          !!!cp ('t87.5');
4478          #          #
4479        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
         ## NOTE: "using the rules for secondary insertion mode" then "continue"  
4480          !!!cp ('t87.6');          !!!cp ('t87.6');
4481          #          !!!parse-error (type => 'not closed',
4482          ## TODO: ...                          text => $self->{open_elements}->[-1]->[0]
4483                                ->manakai_local_name,
4484                            token => $token);
4485    
4486            pop @{$self->{open_elements}}
4487                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4488    
4489            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4490            ## Reprocess.
4491            next B;
4492        } else {        } else {
4493          die "$0: $token->{type}: Unknown token type";                  die "$0: $token->{type}: Unknown token type";        
4494        }        }
# Line 4072  sub _tree_construction_main ($) { Line 4500  sub _tree_construction_main ($) {
4500            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4501              !!!cp ('t88.2');              !!!cp ('t88.2');
4502              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4503                #
4504            } else {            } else {
4505              !!!cp ('t88.1');              !!!cp ('t88.1');
4506              ## Ignore the token.              ## Ignore the token.
4507              !!!next-token;              #
             next B;  
4508            }            }
4509            unless (length $token->{data}) {            unless (length $token->{data}) {
4510              !!!cp ('t88');              !!!cp ('t88');
4511              !!!next-token;              !!!next-token;
4512              next B;              next B;
4513            }            }
4514    ## TODO: set $token->{column} appropriately
4515          }          }
4516    
4517          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
# Line 4101  sub _tree_construction_main ($) { Line 4530  sub _tree_construction_main ($) {
4530            !!!cp ('t90');            !!!cp ('t90');
4531            ## As if </noscript>            ## As if </noscript>
4532            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4533            !!!parse-error (type => 'in noscript:#character', token => $token);            !!!parse-error (type => 'in noscript:#text', token => $token);
4534                        
4535            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4536            ## As if </head>            ## As if </head>
# Line 4137  sub _tree_construction_main ($) { Line 4566  sub _tree_construction_main ($) {
4566              !!!next-token;              !!!next-token;
4567              next B;              next B;
4568            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4569              !!!cp ('t94');              !!!cp ('t93.2');
4570              #              !!!parse-error (type => 'after head', text => 'head',
4571                                token => $token);
4572                ## Ignore the token
4573                !!!nack ('t93.3');
4574                !!!next-token;
4575                next B;
4576            } else {            } else {
4577              !!!cp ('t95');              !!!cp ('t95');
4578              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript              !!!parse-error (type => 'in head:head',
4579                                token => $token); # or in head noscript
4580              ## Ignore the token              ## Ignore the token
4581              !!!nack ('t95.1');              !!!nack ('t95.1');
4582              !!!next-token;              !!!next-token;
# Line 4166  sub _tree_construction_main ($) { Line 4601  sub _tree_construction_main ($) {
4601                  !!!cp ('t98');                  !!!cp ('t98');
4602                  ## As if </noscript>                  ## As if </noscript>
4603                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4604                  !!!parse-error (type => 'in noscript:base', token => $token);                  !!!parse-error (type => 'in noscript', text => 'base',
4605                                    token => $token);
4606                                
4607                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4608                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 4177  sub _tree_construction_main ($) { Line 4613  sub _tree_construction_main ($) {
4613                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4614                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4615                  !!!cp ('t100');                  !!!cp ('t100');
4616                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4617                                    text => $token->{tag_name}, token => $token);
4618                  push @{$self->{open_elements}},                  push @{$self->{open_elements}},
4619                      [$self->{head_element}, $el_category->{head}];                      [$self->{head_element}, $el_category->{head}];
4620                } else {                } else {
# Line 4194  sub _tree_construction_main ($) { Line 4631  sub _tree_construction_main ($) {
4631                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4632                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4633                  !!!cp ('t102');                  !!!cp ('t102');
4634                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4635                                    text => $token->{tag_name}, token => $token);
4636                  push @{$self->{open_elements}},                  push @{$self->{open_elements}},
4637                      [$self->{head_element}, $el_category->{head}];                      [$self->{head_element}, $el_category->{head}];
4638                } else {                } else {
# Line 4211  sub _tree_construction_main ($) { Line 4649  sub _tree_construction_main ($) {
4649                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4650                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4651                  !!!cp ('t104');                  !!!cp ('t104');
4652                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4653                                    text => $token->{tag_name}, token => $token);
4654                  push @{$self->{open_elements}},                  push @{$self->{open_elements}},
4655                      [$self->{head_element}, $el_category->{head}];                      [$self->{head_element}, $el_category->{head}];
4656                } else {                } else {
# Line 4235  sub _tree_construction_main ($) { Line 4674  sub _tree_construction_main ($) {
4674                                                 ->{has_reference});                                                 ->{has_reference});
4675                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
4676                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4677                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4678                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4679                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4680                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4681                      !!!cp ('t107');                      !!!cp ('t107');
4682                      ## NOTE: Whether the encoding is supported or not is handled                      ## NOTE: Whether the encoding is supported or not is handled
4683                      ## in the {change_encoding} callback.                      ## in the {change_encoding} callback.
# Line 4280  sub _tree_construction_main ($) { Line 4719  sub _tree_construction_main ($) {
4719                  !!!cp ('t111');                  !!!cp ('t111');
4720                  ## As if </noscript>                  ## As if </noscript>
4721                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4722                  !!!parse-error (type => 'in noscript:title', token => $token);                  !!!parse-error (type => 'in noscript', text => 'title',
4723                                    token => $token);
4724                                
4725                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4726                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4727                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4728                  !!!cp ('t112');                  !!!cp ('t112');
4729                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4730                                    text => $token->{tag_name}, token => $token);
4731                  push @{$self->{open_elements}},                  push @{$self->{open_elements}},
4732                      [$self->{head_element}, $el_category->{head}];                      [$self->{head_element}, $el_category->{head}];
4733                } else {                } else {
# Line 4300  sub _tree_construction_main ($) { Line 4741  sub _tree_construction_main ($) {
4741                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4742                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4743                next B;                next B;
4744              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style' or
4745                         $token->{tag_name} eq 'noframes') {
4746                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4747                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4748                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4749                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4750                  !!!cp ('t114');                  !!!cp ('t114');
4751                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4752                                    text => $token->{tag_name}, token => $token);
4753                  push @{$self->{open_elements}},                  push @{$self->{open_elements}},
4754                      [$self->{head_element}, $el_category->{head}];                      [$self->{head_element}, $el_category->{head}];
4755                } else {                } else {
# Line 4327  sub _tree_construction_main ($) { Line 4770  sub _tree_construction_main ($) {
4770                  next B;                  next B;
4771                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4772                  !!!cp ('t117');                  !!!cp ('t117');
4773                  !!!parse-error (type => 'in noscript:noscript', token => $token);                  !!!parse-error (type => 'in noscript', text => 'noscript',
4774                                    token => $token);
4775                  ## Ignore the token                  ## Ignore the token
4776                  !!!nack ('t117.1');                  !!!nack ('t117.1');
4777                  !!!next-token;                  !!!next-token;
# Line 4341  sub _tree_construction_main ($) { Line 4785  sub _tree_construction_main ($) {
4785                  !!!cp ('t119');                  !!!cp ('t119');
4786                  ## As if </noscript>                  ## As if </noscript>
4787                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4788                  !!!parse-error (type => 'in noscript:script', token => $token);                  !!!parse-error (type => 'in noscript', text => 'script',
4789                                    token => $token);
4790                                
4791                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4792                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4793                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4794                  !!!cp ('t120');                  !!!cp ('t120');
4795                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4796                                    text => $token->{tag_name}, token => $token);
4797                  push @{$self->{open_elements}},                  push @{$self->{open_elements}},
4798                      [$self->{head_element}, $el_category->{head}];                      [$self->{head_element}, $el_category->{head}];
4799                } else {                } else {
# Line 4365  sub _tree_construction_main ($) { Line 4811  sub _tree_construction_main ($) {
4811                  !!!cp ('t122');                  !!!cp ('t122');
4812                  ## As if </noscript>                  ## As if </noscript>
4813                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4814                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'in noscript',
4815                                    text => $token->{tag_name}, token => $token);
4816                                    
4817                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4818                  ## As if </head>                  ## As if </head>
# Line 4404  sub _tree_construction_main ($) { Line 4851  sub _tree_construction_main ($) {
4851                !!!cp ('t129');                !!!cp ('t129');
4852                ## As if </noscript>                ## As if </noscript>
4853                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4854                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'in noscript:/',
4855                                  text => $token->{tag_name}, token => $token);
4856                                
4857                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4858                ## As if </head>                ## As if </head>
# Line 4447  sub _tree_construction_main ($) { Line 4895  sub _tree_construction_main ($) {
4895                  !!!cp ('t133');                  !!!cp ('t133');
4896                  ## As if </noscript>                  ## As if </noscript>
4897                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4898                  !!!parse-error (type => 'in noscript:/head', token => $token);                  !!!parse-error (type => 'in noscript:/',
4899                                    text => 'head', token => $token);
4900                                    
4901                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4902                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4460  sub _tree_construction_main ($) { Line 4909  sub _tree_construction_main ($) {
4909                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4910                  !!!next-token;                  !!!next-token;
4911                  next B;                  next B;
4912                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4913                    !!!cp ('t134.1');
4914                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4915                                    token => $token);
4916                    ## Ignore the token
4917                    !!!next-token;
4918                    next B;
4919                } else {                } else {
4920                  !!!cp ('t135');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 #  
4921                }                }
4922              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4923                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 4471  sub _tree_construction_main ($) { Line 4926  sub _tree_construction_main ($) {
4926                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4927                  !!!next-token;                  !!!next-token;
4928                  next B;                  next B;
4929                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4930                           $self->{insertion_mode} == AFTER_HEAD_IM) {
4931                  !!!cp ('t137');                  !!!cp ('t137');
4932                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);                  !!!parse-error (type => 'unmatched end tag',
4933                                    text => 'noscript', token => $token);
4934                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4935                  !!!next-token;                  !!!next-token;
4936                  next B;                  next B;
# Line 4484  sub _tree_construction_main ($) { Line 4941  sub _tree_construction_main ($) {
4941              } elsif ({              } elsif ({
4942                        body => 1, html => 1,                        body => 1, html => 1,
4943                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4944                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4945                  !!!cp ('t139');                    $self->{insertion_mode} == IN_HEAD_IM or
4946                  ## As if <head>                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
                 !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);  
                 $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
                 push @{$self->{open_elements}},  
                     [$self->{head_element}, $el_category->{head}];  
   
                 $self->{insertion_mode} = IN_HEAD_IM;  
                 ## Reprocess in the "in head" insertion mode...  
               } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {  
4947                  !!!cp ('t140');                  !!!cp ('t140');
4948                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
4949                                    text => $token->{tag_name}, token => $token);
4950                    ## Ignore the token
4951                    !!!next-token;
4952                    next B;
4953                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4954                    !!!cp ('t140.1');
4955                    !!!parse-error (type => 'unmatched end tag',
4956                                    text => $token->{tag_name}, token => $token);
4957                  ## Ignore the token                  ## Ignore the token
4958                  !!!next-token;                  !!!next-token;
4959                  next B;                  next B;
4960                } else {                } else {
4961                  !!!cp ('t141');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4962                }                }
4963                              } elsif ($token->{tag_name} eq 'p') {
4964                #                !!!cp ('t142');
4965              } elsif ({                !!!parse-error (type => 'unmatched end tag',
4966                        p => 1, br => 1,                                text => $token->{tag_name}, token => $token);
4967                       }->{$token->{tag_name}}) {                ## Ignore the token
4968                  !!!next-token;
4969                  next B;
4970                } elsif ($token->{tag_name} eq 'br') {
4971                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4972                  !!!cp ('t142');                  !!!cp ('t142.2');
4973                  ## As if <head>                  ## (before head) as if <head>, (in head) as if </head>
4974                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4975                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4976                  push @{$self->{open_elements}},                  $self->{insertion_mode} = AFTER_HEAD_IM;
4977                      [$self->{head_element}, $el_category->{head}];    
4978                    ## Reprocess in the "after head" insertion mode...
4979                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4980                    !!!cp ('t143.2');
4981                    ## As if </head>
4982                    pop @{$self->{open_elements}};
4983                    $self->{insertion_mode} = AFTER_HEAD_IM;
4984      
4985                    ## Reprocess in the "after head" insertion mode...
4986                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4987                    !!!cp ('t143.3');
4988                    ## ISSUE: Two parse errors for <head><noscript></br>
4989                    !!!parse-error (type => 'unmatched end tag',
4990                                    text => 'br', token => $token);
4991                    ## As if </noscript>
4992                    pop @{$self->{open_elements}};
4993                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4994    
4995                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4996                } else {                  ## As if </head>
4997                  !!!cp ('t143');                  pop @{$self->{open_elements}};
4998                }                  $self->{insertion_mode} = AFTER_HEAD_IM;
4999    
5000                #                  ## Reprocess in the "after head" insertion mode...
5001              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
5002                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
                 !!!cp ('t144');  
5003                  #                  #
5004                } else {                } else {
5005                  !!!cp ('t145');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);  
                 ## Ignore the token  
                 !!!next-token;  
                 next B;  
5006                }                }
5007    
5008                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
5009                  !!!parse-error (type => 'unmatched end tag',
5010                                  text => 'br', token => $token);
5011                  ## Ignore the token
5012                  !!!next-token;
5013                  next B;
5014                } else {
5015                  !!!cp ('t145');
5016                  !!!parse-error (type => 'unmatched end tag',
5017                                  text => $token->{tag_name}, token => $token);
5018                  ## Ignore the token
5019                  !!!next-token;
5020                  next B;
5021              }              }
5022    
5023              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5024                !!!cp ('t146');                !!!cp ('t146');
5025                ## As if </noscript>                ## As if </noscript>
5026                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5027                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'in noscript:/',
5028                                  text => $token->{tag_name}, token => $token);
5029                                
5030                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
5031                ## As if </head>                ## As if </head>
# Line 4556  sub _tree_construction_main ($) { Line 5041  sub _tree_construction_main ($) {
5041              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5042  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
5043                !!!cp ('t148');                !!!cp ('t148');
5044                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
5045                                  text => $token->{tag_name}, token => $token);
5046                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
5047                !!!next-token;                !!!next-token;
5048                next B;                next B;
# Line 4667  sub _tree_construction_main ($) { Line 5153  sub _tree_construction_main ($) {
5153    
5154                  !!!cp ('t153');                  !!!cp ('t153');
5155                  !!!parse-error (type => 'start tag not allowed',                  !!!parse-error (type => 'start tag not allowed',
5156                      value => $token->{tag_name}, token => $token);                      text => $token->{tag_name}, token => $token);
5157                  ## Ignore the token                  ## Ignore the token
5158                  !!!nack ('t153.1');                  !!!nack ('t153.1');
5159                  !!!next-token;                  !!!next-token;
5160                  next B;                  next B;
5161                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5162                  !!!parse-error (type => 'not closed:caption', token => $token);                  !!!parse-error (type => 'not closed', text => 'caption',
5163                                    token => $token);
5164                                    
5165                  ## NOTE: As if </caption>.                  ## NOTE: As if </caption>.
5166                  ## have a table element in table scope                  ## have a table element in table scope
# Line 4693  sub _tree_construction_main ($) { Line 5180  sub _tree_construction_main ($) {
5180    
5181                    !!!cp ('t157');                    !!!cp ('t157');
5182                    !!!parse-error (type => 'start tag not allowed',                    !!!parse-error (type => 'start tag not allowed',
5183                                    value => $token->{tag_name}, token => $token);                                    text => $token->{tag_name}, token => $token);
5184                    ## Ignore the token                    ## Ignore the token
5185                    !!!nack ('t157.1');                    !!!nack ('t157.1');
5186                    !!!next-token;                    !!!next-token;
# Line 4710  sub _tree_construction_main ($) { Line 5197  sub _tree_construction_main ($) {
5197                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5198                    !!!cp ('t159');                    !!!cp ('t159');
5199                    !!!parse-error (type => 'not closed',                    !!!parse-error (type => 'not closed',
5200                                    value => $self->{open_elements}->[-1]->[0]                                    text => $self->{open_elements}->[-1]->[0]
5201                                        ->manakai_local_name,                                        ->manakai_local_name,
5202                                    token => $token);                                    token => $token);
5203                  } else {                  } else {
# Line 4752  sub _tree_construction_main ($) { Line 5239  sub _tree_construction_main ($) {
5239                  } # INSCOPE                  } # INSCOPE
5240                    unless (defined $i) {                    unless (defined $i) {
5241                      !!!cp ('t165');                      !!!cp ('t165');
5242                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag',
5243                                        text => $token->{tag_name},
5244                                        token => $token);
5245                      ## Ignore the token                      ## Ignore the token
5246                      !!!next-token;                      !!!next-token;
5247                      next B;                      next B;
# Line 4769  sub _tree_construction_main ($) { Line 5258  sub _tree_construction_main ($) {
5258                          ne $token->{tag_name}) {                          ne $token->{tag_name}) {
5259                    !!!cp ('t167');                    !!!cp ('t167');
5260                    !!!parse-error (type => 'not closed',                    !!!parse-error (type => 'not closed',
5261                                    value => $self->{open_elements}->[-1]->[0]                                    text => $self->{open_elements}->[-1]->[0]
5262                                        ->manakai_local_name,                                        ->manakai_local_name,
5263                                    token => $token);                                    token => $token);
5264                  } else {                  } else {
# Line 4786  sub _tree_construction_main ($) { Line 5275  sub _tree_construction_main ($) {
5275                  next B;                  next B;
5276                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5277                  !!!cp ('t169');                  !!!cp ('t169');
5278                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5279                                    text => $token->{tag_name}, token => $token);
5280                  ## Ignore the token                  ## Ignore the token
5281                  !!!next-token;                  !!!next-token;
5282                  next B;                  next B;
# Line 4813  sub _tree_construction_main ($) { Line 5303  sub _tree_construction_main ($) {
5303    
5304                    !!!cp ('t173');                    !!!cp ('t173');
5305                    !!!parse-error (type => 'unmatched end tag',                    !!!parse-error (type => 'unmatched end tag',
5306                                    value => $token->{tag_name}, token => $token);                                    text => $token->{tag_name}, token => $token);
5307                    ## Ignore the token                    ## Ignore the token
5308                    !!!next-token;                    !!!next-token;
5309                    next B;                    next B;
# Line 4829  sub _tree_construction_main ($) { Line 5319  sub _tree_construction_main ($) {
5319                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5320                    !!!cp ('t175');                    !!!cp ('t175');
5321                    !!!parse-error (type => 'not closed',                    !!!parse-error (type => 'not closed',
5322                                    value => $self->{open_elements}->[-1]->[0]                                    text => $self->{open_elements}->[-1]->[0]
5323                                        ->manakai_local_name,                                        ->manakai_local_name,
5324                                    token => $token);                                    token => $token);
5325                  } else {                  } else {
# Line 4846  sub _tree_construction_main ($) { Line 5336  sub _tree_construction_main ($) {
5336                  next B;                  next B;
5337                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5338                  !!!cp ('t177');                  !!!cp ('t177');
5339                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5340                                    text => $token->{tag_name}, token => $token);
5341                  ## Ignore the token                  ## Ignore the token
5342                  !!!next-token;                  !!!next-token;
5343                  next B;                  next B;
# Line 4889  sub _tree_construction_main ($) { Line 5380  sub _tree_construction_main ($) {
5380    
5381                  !!!cp ('t182');                  !!!cp ('t182');
5382                  !!!parse-error (type => 'unmatched end tag',                  !!!parse-error (type => 'unmatched end tag',
5383                      value => $token->{tag_name}, token => $token);                      text => $token->{tag_name}, token => $token);
5384                  ## Ignore the token                  ## Ignore the token
5385                  !!!next-token;                  !!!next-token;
5386                  next B;                  next B;
5387                } # INSCOPE                } # INSCOPE
5388              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5389                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5390                !!!parse-error (type => 'not closed:caption', token => $token);                !!!parse-error (type => 'not closed', text => 'caption',
5391                                  token => $token);
5392    
5393                ## As if </caption>                ## As if </caption>
5394                ## have a table element in table scope                ## have a table element in table scope
# Line 4914  sub _tree_construction_main ($) { Line 5406  sub _tree_construction_main ($) {
5406                } # INSCOPE                } # INSCOPE
5407                unless (defined $i) {                unless (defined $i) {
5408                  !!!cp ('t186');                  !!!cp ('t186');
5409                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);                  !!!parse-error (type => 'unmatched end tag',
5410                                    text => 'caption', token => $token);
5411                  ## Ignore the token                  ## Ignore the token
5412                  !!!next-token;                  !!!next-token;
5413                  next B;                  next B;
# Line 4929  sub _tree_construction_main ($) { Line 5422  sub _tree_construction_main ($) {
5422                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5423                  !!!cp ('t188');                  !!!cp ('t188');
5424                  !!!parse-error (type => 'not closed',                  !!!parse-error (type => 'not closed',
5425                                  value => $self->{open_elements}->[-1]->[0]                                  text => $self->{open_elements}->[-1]->[0]
5426                                      ->manakai_local_name,                                      ->manakai_local_name,
5427                                  token => $token);                                  token => $token);
5428                } else {                } else {
# Line 4949  sub _tree_construction_main ($) { Line 5442  sub _tree_construction_main ($) {
5442                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5443                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5444                  !!!cp ('t190');                  !!!cp ('t190');
5445                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5446                                    text => $token->{tag_name}, token => $token);
5447                  ## Ignore the token                  ## Ignore the token
5448                  !!!next-token;                  !!!next-token;
5449                  next B;                  next B;
# Line 4963  sub _tree_construction_main ($) { Line 5457  sub _tree_construction_main ($) {
5457                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5458                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5459                !!!cp ('t192');                !!!cp ('t192');
5460                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
5461                                  text => $token->{tag_name}, token => $token);
5462                ## Ignore the token                ## Ignore the token
5463                !!!next-token;                !!!next-token;
5464                next B;                next B;
# Line 5003  sub _tree_construction_main ($) { Line 5498  sub _tree_construction_main ($) {
5498            }            }
5499          }          }
5500    
5501              !!!parse-error (type => 'in table:#character', token => $token);          !!!parse-error (type => 'in table:#text', token => $token);
5502    
5503              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5504              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 5054  sub _tree_construction_main ($) { Line 5549  sub _tree_construction_main ($) {
5549          !!!next-token;          !!!next-token;
5550          next B;          next B;
5551        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5552              if ({          if ({
5553                   tr => ($self->{insertion_mode} != IN_ROW_IM),               tr => ($self->{insertion_mode} != IN_ROW_IM),
5554                   th => 1, td => 1,               th => 1, td => 1,
5555                  }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
5556                if ($self->{insertion_mode} == IN_TABLE_IM) {            if ($self->{insertion_mode} == IN_TABLE_IM) {
5557                  ## Clear back to table context              ## Clear back to table context
5558                  while (not ($self->{open_elements}->[-1]->[1]              while (not ($self->{open_elements}->[-1]->[1]
5559                                  & TABLE_SCOPING_EL)) {                              & TABLE_SCOPING_EL)) {
5560                    !!!cp ('t201');                !!!cp ('t201');
5561                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5562                  }              }
5563                                
5564                  !!!insert-element ('tbody',, $token);              !!!insert-element ('tbody',, $token);
5565                  $self->{insertion_mode} = IN_TABLE_BODY_IM;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5566                  ## reprocess in the "in table body" insertion mode...              ## reprocess in the "in table body" insertion mode...
5567                }            }
5568              
5569                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5570                  unless ($token->{tag_name} eq 'tr') {              unless ($token->{tag_name} eq 'tr') {
5571                    !!!cp ('t202');                !!!cp ('t202');
5572                    !!!parse-error (type => 'missing start tag:tr', token => $token);                !!!parse-error (type => 'missing start tag:tr', token => $token);
5573                  }              }
5574                                    
5575                  ## Clear back to table body context              ## Clear back to table body context
5576                  while (not ($self->{open_elements}->[-1]->[1]              while (not ($self->{open_elements}->[-1]->[1]
5577                                  & TABLE_ROWS_SCOPING_EL)) {                              & TABLE_ROWS_SCOPING_EL)) {
5578                    !!!cp ('t203');                !!!cp ('t203');
5579                    ## ISSUE: Can this case be reached?                ## ISSUE: Can this case be reached?
5580                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5581                  }              }
5582                                    
5583                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5584                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
# Line 5139  sub _tree_construction_main ($) { Line 5634  sub _tree_construction_main ($) {
5634                  unless (defined $i) {                  unless (defined $i) {
5635                    !!!cp ('t210');                    !!!cp ('t210');
5636  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5637                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmacthed end tag',
5638                                      text => $token->{tag_name}, token => $token);
5639                    ## Ignore the token                    ## Ignore the token
5640                    !!!nack ('t210.1');                    !!!nack ('t210.1');
5641                    !!!next-token;                    !!!next-token;
# Line 5183  sub _tree_construction_main ($) { Line 5679  sub _tree_construction_main ($) {
5679                  } # INSCOPE                  } # INSCOPE
5680                  unless (defined $i) {                  unless (defined $i) {
5681                    !!!cp ('t216');                    !!!cp ('t216');
5682  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type is wrong.
5683                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5684                                      text => $token->{tag_name}, token => $token);
5685                    ## Ignore the token                    ## Ignore the token
5686                    !!!nack ('t216.1');                    !!!nack ('t216.1');
5687                    !!!next-token;                    !!!next-token;
# Line 5259  sub _tree_construction_main ($) { Line 5756  sub _tree_construction_main ($) {
5756                }                }
5757              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5758                !!!parse-error (type => 'not closed',                !!!parse-error (type => 'not closed',
5759                                value => $self->{open_elements}->[-1]->[0]                                text => $self->{open_elements}->[-1]->[0]
5760                                    ->manakai_local_name,                                    ->manakai_local_name,
5761                                token => $token);                                token => $token);
5762    
# Line 5280  sub _tree_construction_main ($) { Line 5777  sub _tree_construction_main ($) {
5777                unless (defined $i) {                unless (defined $i) {
5778                  !!!cp ('t223');                  !!!cp ('t223');
5779  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5780                  !!!parse-error (type => 'unmatched end tag:table', token => $token);                  !!!parse-error (type => 'unmatched end tag', text => 'table',
5781                                    token => $token);
5782                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5783                  !!!nack ('t223.1');                  !!!nack ('t223.1');
5784                  !!!next-token;                  !!!next-token;
5785                  next B;                  next B;
5786                }                }
5787                                
5788  ## TODO: Followings are removed from the latest spec.  ## TODO: Followings are removed from the latest spec.
5789                ## generate implied end tags                ## generate implied end tags
5790                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5791                  !!!cp ('t224');                  !!!cp ('t224');
# Line 5298  sub _tree_construction_main ($) { Line 5796  sub _tree_construction_main ($) {
5796                  !!!cp ('t225');                  !!!cp ('t225');
5797                  ## NOTE: |<table><tr><table>|                  ## NOTE: |<table><tr><table>|
5798                  !!!parse-error (type => 'not closed',                  !!!parse-error (type => 'not closed',
5799                                  value => $self->{open_elements}->[-1]->[0]                                  text => $self->{open_elements}->[-1]->[0]
5800                                      ->manakai_local_name,                                      ->manakai_local_name,
5801                                  token => $token);                                  token => $token);
5802                } else {                } else {
# Line 5339  sub _tree_construction_main ($) { Line 5837  sub _tree_construction_main ($) {
5837                my $type = lc $token->{attributes}->{type}->{value};                my $type = lc $token->{attributes}->{type}->{value};
5838                if ($type eq 'hidden') {                if ($type eq 'hidden') {
5839                  !!!cp ('t227.3');                  !!!cp ('t227.3');
5840                  !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'in table',
5841                                    text => $token->{tag_name}, token => $token);
5842    
5843                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5844    
# Line 5367  sub _tree_construction_main ($) { Line 5866  sub _tree_construction_main ($) {
5866            #            #
5867          }          }
5868    
5869          !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'in table', text => $token->{tag_name},
5870                            token => $token);
5871    
5872          $insert = $insert_to_foster;          $insert = $insert_to_foster;
5873          #          #
# Line 5389  sub _tree_construction_main ($) { Line 5889  sub _tree_construction_main ($) {
5889                } # INSCOPE                } # INSCOPE
5890                unless (defined $i) {                unless (defined $i) {
5891                  !!!cp ('t230');                  !!!cp ('t230');
5892                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5893                                    text => $token->{tag_name}, token => $token);
5894                  ## Ignore the token                  ## Ignore the token
5895                  !!!nack ('t230.1');                  !!!nack ('t230.1');
5896                  !!!next-token;                  !!!next-token;
# Line 5430  sub _tree_construction_main ($) { Line 5931  sub _tree_construction_main ($) {
5931                  unless (defined $i) {                  unless (defined $i) {
5932                    !!!cp ('t235');                    !!!cp ('t235');
5933  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5934                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5935                                      text => $token->{type}, token => $token);
5936                    ## Ignore the token                    ## Ignore the token
5937                    !!!nack ('t236.1');                    !!!nack ('t236.1');
5938                    !!!next-token;                    !!!next-token;
# Line 5466  sub _tree_construction_main ($) { Line 5968  sub _tree_construction_main ($) {
5968                  } # INSCOPE                  } # INSCOPE
5969                  unless (defined $i) {                  unless (defined $i) {
5970                    !!!cp ('t239');                    !!!cp ('t239');
5971                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5972                                      text => $token->{tag_name}, token => $token);
5973                    ## Ignore the token                    ## Ignore the token
5974                    !!!nack ('t239.1');                    !!!nack ('t239.1');
5975                    !!!next-token;                    !!!next-token;
# Line 5512  sub _tree_construction_main ($) { Line 6015  sub _tree_construction_main ($) {
6015                } # INSCOPE                } # INSCOPE
6016                unless (defined $i) {                unless (defined $i) {
6017                  !!!cp ('t243');                  !!!cp ('t243');
6018                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
6019                                    text => $token->{tag_name}, token => $token);
6020                  ## Ignore the token                  ## Ignore the token
6021                  !!!nack ('t243.1');                  !!!nack ('t243.1');
6022                  !!!next-token;                  !!!next-token;
# Line 5546  sub _tree_construction_main ($) { Line 6050  sub _tree_construction_main ($) {
6050                  } # INSCOPE                  } # INSCOPE
6051                    unless (defined $i) {                    unless (defined $i) {
6052                      !!!cp ('t249');                      !!!cp ('t249');
6053                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag',
6054                                        text => $token->{tag_name}, token => $token);
6055                      ## Ignore the token                      ## Ignore the token
6056                      !!!nack ('t249.1');                      !!!nack ('t249.1');
6057                      !!!next-token;                      !!!next-token;
# Line 5569  sub _tree_construction_main ($) { Line 6074  sub _tree_construction_main ($) {
6074                  } # INSCOPE                  } # INSCOPE
6075                    unless (defined $i) {                    unless (defined $i) {
6076                      !!!cp ('t252');                      !!!cp ('t252');
6077                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);                      !!!parse-error (type => 'unmatched end tag',
6078                                        text => 'tr', token => $token);
6079                      ## Ignore the token                      ## Ignore the token
6080                      !!!nack ('t252.1');                      !!!nack ('t252.1');
6081                      !!!next-token;                      !!!next-token;
# Line 5604  sub _tree_construction_main ($) { Line 6110  sub _tree_construction_main ($) {
6110                } # INSCOPE                } # INSCOPE
6111                unless (defined $i) {                unless (defined $i) {
6112                  !!!cp ('t256');                  !!!cp ('t256');
6113                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
6114                                    text => $token->{tag_name}, token => $token);
6115                  ## Ignore the token                  ## Ignore the token
6116                  !!!nack ('t256.1');                  !!!nack ('t256.1');
6117                  !!!next-token;                  !!!next-token;
# Line 5631  sub _tree_construction_main ($) { Line 6138  sub _tree_construction_main ($) {
6138                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
6139                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
6140            !!!cp ('t258');            !!!cp ('t258');
6141            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
6142                              text => $token->{tag_name}, token => $token);
6143            ## Ignore the token            ## Ignore the token
6144            !!!nack ('t258.1');            !!!nack ('t258.1');
6145             !!!next-token;             !!!next-token;
6146            next B;            next B;
6147          } else {          } else {
6148            !!!cp ('t259');            !!!cp ('t259');
6149            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in table:/',
6150                              text => $token->{tag_name}, token => $token);
6151    
6152            $insert = $insert_to_foster;            $insert = $insert_to_foster;
6153            #            #
# Line 5688  sub _tree_construction_main ($) { Line 6197  sub _tree_construction_main ($) {
6197              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
6198                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6199                  !!!cp ('t264');                  !!!cp ('t264');
6200                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);                  !!!parse-error (type => 'unmatched end tag',
6201                                    text => 'colgroup', token => $token);
6202                  ## Ignore the token                  ## Ignore the token
6203                  !!!next-token;                  !!!next-token;
6204                  next B;                  next B;
# Line 5701  sub _tree_construction_main ($) { Line 6211  sub _tree_construction_main ($) {
6211                }                }
6212              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
6213                !!!cp ('t266');                !!!cp ('t266');
6214                !!!parse-error (type => 'unmatched end tag:col', token => $token);                !!!parse-error (type => 'unmatched end tag',
6215                                  text => 'col', token => $token);
6216                ## Ignore the token                ## Ignore the token
6217                !!!next-token;                !!!next-token;
6218                next B;                next B;
# Line 5731  sub _tree_construction_main ($) { Line 6242  sub _tree_construction_main ($) {
6242            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6243              !!!cp ('t269');              !!!cp ('t269');
6244  ## TODO: Wrong error type?  ## TODO: Wrong error type?
6245              !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);              !!!parse-error (type => 'unmatched end tag',
6246                                text => 'colgroup', token => $token);
6247              ## Ignore the token              ## Ignore the token
6248              !!!nack ('t269.1');              !!!nack ('t269.1');
6249              !!!next-token;              !!!next-token;
# Line 5785  sub _tree_construction_main ($) { Line 6297  sub _tree_construction_main ($) {
6297            !!!nack ('t277.1');            !!!nack ('t277.1');
6298            !!!next-token;            !!!next-token;
6299            next B;            next B;
6300          } elsif ($token->{tag_name} eq 'select' or          } elsif ({
6301                   $token->{tag_name} eq 'input' or                     select => 1, input => 1, textarea => 1,
6302                     }->{$token->{tag_name}} or
6303                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6304                    {                    {
6305                     caption => 1, table => 1,                     caption => 1, table => 1,
# Line 5794  sub _tree_construction_main ($) { Line 6307  sub _tree_construction_main ($) {
6307                     tr => 1, td => 1, th => 1,                     tr => 1, td => 1, th => 1,
6308                    }->{$token->{tag_name}})) {                    }->{$token->{tag_name}})) {
6309            ## TODO: The type below is not good - <select> is replaced by </select>            ## TODO: The type below is not good - <select> is replaced by </select>
6310            !!!parse-error (type => 'not closed:select', token => $token);            !!!parse-error (type => 'not closed', text => 'select',
6311                              token => $token);
6312            ## NOTE: As if the token were </select> (<select> case) or            ## NOTE: As if the token were </select> (<select> case) or
6313            ## as if there were </select> (otherwise).            ## as if there were </select> (otherwise).
6314            ## have an element in table scope            ## have an element in table scope
# Line 5812  sub _tree_construction_main ($) { Line 6326  sub _tree_construction_main ($) {
6326            } # INSCOPE            } # INSCOPE
6327            unless (defined $i) {            unless (defined $i) {
6328              !!!cp ('t280');              !!!cp ('t280');
6329              !!!parse-error (type => 'unmatched end tag:select', token => $token);              !!!parse-error (type => 'unmatched end tag',
6330                                text => 'select', token => $token);
6331              ## Ignore the token              ## Ignore the token
6332              !!!nack ('t280.1');              !!!nack ('t280.1');
6333              !!!next-token;              !!!next-token;
# Line 5836  sub _tree_construction_main ($) { Line 6351  sub _tree_construction_main ($) {
6351            }            }
6352          } else {          } else {
6353            !!!cp ('t282');            !!!cp ('t282');
6354            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select',
6355                              text => $token->{tag_name}, token => $token);
6356            ## Ignore the token            ## Ignore the token
6357            !!!nack ('t282.1');            !!!nack ('t282.1');
6358            !!!next-token;            !!!next-token;
# Line 5854  sub _tree_construction_main ($) { Line 6370  sub _tree_construction_main ($) {
6370              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6371            } else {            } else {
6372              !!!cp ('t285');              !!!cp ('t285');
6373              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6374                                text => $token->{tag_name}, token => $token);
6375              ## Ignore the token              ## Ignore the token
6376            }            }
6377            !!!nack ('t285.1');            !!!nack ('t285.1');
# Line 5866  sub _tree_construction_main ($) { Line 6383  sub _tree_construction_main ($) {
6383              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6384            } else {            } else {
6385              !!!cp ('t287');              !!!cp ('t287');
6386              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6387                                text => $token->{tag_name}, token => $token);
6388              ## Ignore the token              ## Ignore the token
6389            }            }
6390            !!!nack ('t287.1');            !!!nack ('t287.1');
# Line 5888  sub _tree_construction_main ($) { Line 6406  sub _tree_construction_main ($) {
6406            } # INSCOPE            } # INSCOPE
6407            unless (defined $i) {            unless (defined $i) {
6408              !!!cp ('t290');              !!!cp ('t290');
6409              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6410                                text => $token->{tag_name}, token => $token);
6411              ## Ignore the token              ## Ignore the token
6412              !!!nack ('t290.1');              !!!nack ('t290.1');
6413              !!!next-token;              !!!next-token;
# Line 5909  sub _tree_construction_main ($) { Line 6428  sub _tree_construction_main ($) {
6428                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6429                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
6430  ## TODO: The following is wrong?  ## TODO: The following is wrong?
6431            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
6432                              text => $token->{tag_name}, token => $token);
6433                                
6434            ## have an element in table scope            ## have an element in table scope
6435            my $i;            my $i;
# Line 5950  sub _tree_construction_main ($) { Line 6470  sub _tree_construction_main ($) {
6470            unless (defined $i) {            unless (defined $i) {
6471              !!!cp ('t297');              !!!cp ('t297');
6472  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
6473              !!!parse-error (type => 'unmatched end tag:select', token => $token);              !!!parse-error (type => 'unmatched end tag',
6474                                text => 'select', token => $token);
6475              ## Ignore the </select> token              ## Ignore the </select> token
6476              !!!nack ('t297.1');              !!!nack ('t297.1');
6477              !!!next-token; ## TODO: ok?              !!!next-token; ## TODO: ok?
# Line 5967  sub _tree_construction_main ($) { Line 6488  sub _tree_construction_main ($) {
6488            next B;            next B;
6489          } else {          } else {
6490            !!!cp ('t299');            !!!cp ('t299');
6491            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select:/',
6492                              text => $token->{tag_name}, token => $token);
6493            ## Ignore the token            ## Ignore the token
6494            !!!nack ('t299.3');            !!!nack ('t299.3');
6495            !!!next-token;            !!!next-token;
# Line 6005  sub _tree_construction_main ($) { Line 6527  sub _tree_construction_main ($) {
6527                    
6528          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6529            !!!cp ('t301');            !!!cp ('t301');
6530            !!!parse-error (type => 'after html:#character', token => $token);            !!!parse-error (type => 'after html:#text', token => $token);
6531    
6532            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6533          } else {          } else {
# Line 6013  sub _tree_construction_main ($) { Line 6535  sub _tree_construction_main ($) {
6535          }          }
6536                    
6537          ## "after body" insertion mode          ## "after body" insertion mode
6538          !!!parse-error (type => 'after body:#character', token => $token);          !!!parse-error (type => 'after body:#text', token => $token);
6539    
6540          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6541          ## reprocess          ## reprocess
# Line 6021  sub _tree_construction_main ($) { Line 6543  sub _tree_construction_main ($) {
6543        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6544          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6545            !!!cp ('t303');            !!!cp ('t303');
6546            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after html',
6547                              text => $token->{tag_name}, token => $token);
6548                        
6549            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6550          } else {          } else {
# Line 6029  sub _tree_construction_main ($) { Line 6552  sub _tree_construction_main ($) {
6552          }          }
6553    
6554          ## "after body" insertion mode          ## "after body" insertion mode
6555          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'after body',
6556                            text => $token->{tag_name}, token => $token);
6557    
6558          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6559          !!!ack-later;          !!!ack-later;
# Line 6038  sub _tree_construction_main ($) { Line 6562  sub _tree_construction_main ($) {
6562        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6563          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6564            !!!cp ('t305');            !!!cp ('t305');
6565            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after html:/',
6566                              text => $token->{tag_name}, token => $token);
6567                        
6568            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6569            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
# Line 6050  sub _tree_construction_main ($) { Line 6575  sub _tree_construction_main ($) {
6575          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6576            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6577              !!!cp ('t307');              !!!cp ('t307');
6578              !!!parse-error (type => 'unmatched end tag:html', token => $token);              !!!parse-error (type => 'unmatched end tag',
6579                                text => 'html', token => $token);
6580              ## Ignore the token              ## Ignore the token
6581              !!!next-token;              !!!next-token;
6582              next B;              next B;
# Line 6062  sub _tree_construction_main ($) { Line 6588  sub _tree_construction_main ($) {
6588            }            }
6589          } else {          } else {
6590            !!!cp ('t309');            !!!cp ('t309');
6591            !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after body:/',
6592                              text => $token->{tag_name}, token => $token);
6593    
6594            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6595            ## reprocess            ## reprocess
# Line 6090  sub _tree_construction_main ($) { Line 6617  sub _tree_construction_main ($) {
6617          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6618            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6619              !!!cp ('t311');              !!!cp ('t311');
6620              !!!parse-error (type => 'in frameset:#character', token => $token);              !!!parse-error (type => 'in frameset:#text', token => $token);
6621            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6622              !!!cp ('t312');              !!!cp ('t312');
6623              !!!parse-error (type => 'after frameset:#character', token => $token);              !!!parse-error (type => 'after frameset:#text', token => $token);
6624            } else { # "after html frameset"            } else { # "after after frameset"
6625              !!!cp ('t313');              !!!cp ('t313');
6626              !!!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);  
6627            }            }
6628                        
6629            ## Ignore the token.            ## Ignore the token.
# Line 6116  sub _tree_construction_main ($) { Line 6639  sub _tree_construction_main ($) {
6639                    
6640          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6641        } 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');  
         }  
   
6642          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6643              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6644            !!!cp ('t318');            !!!cp ('t318');
# Line 6143  sub _tree_construction_main ($) { Line 6656  sub _tree_construction_main ($) {
6656            next B;            next B;
6657          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6658            !!!cp ('t320');            !!!cp ('t320');
6659            ## NOTE: As if in body.            ## NOTE: As if in head.
6660            $parse_rcdata->(CDATA_CONTENT_MODEL);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6661            next B;            next B;
6662    
6663              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6664              ## has no parse error.
6665          } else {          } else {
6666            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6667              !!!cp ('t321');              !!!cp ('t321');
6668              !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'in frameset',
6669            } else {                              text => $token->{tag_name}, token => $token);
6670              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6671              !!!cp ('t322');              !!!cp ('t322');
6672              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'after frameset',
6673                                text => $token->{tag_name}, token => $token);
6674              } else { # "after after frameset"
6675                !!!cp ('t322.2');
6676                !!!parse-error (type => 'after after frameset',
6677                                text => $token->{tag_name}, token => $token);
6678            }            }
6679            ## Ignore the token            ## Ignore the token
6680            !!!nack ('t322.1');            !!!nack ('t322.1');
# Line 6160  sub _tree_construction_main ($) { Line 6682  sub _tree_construction_main ($) {
6682            next B;            next B;
6683          }          }
6684        } 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');  
         }  
   
6685          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6686              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6687            if ($self->{open_elements}->[-1]->[1] & HTML_EL and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6688                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6689              !!!cp ('t325');              !!!cp ('t325');
6690              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6691                                text => $token->{tag_name}, token => $token);
6692              ## Ignore the token              ## Ignore the token
6693              !!!next-token;              !!!next-token;
6694            } else {            } else {
# Line 6201  sub _tree_construction_main ($) { Line 6714  sub _tree_construction_main ($) {
6714          } else {          } else {
6715            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6716              !!!cp ('t330');              !!!cp ('t330');
6717              !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'in frameset:/',
6718            } else {                              text => $token->{tag_name}, token => $token);
6719              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6720                !!!cp ('t330.1');
6721                !!!parse-error (type => 'after frameset:/',
6722                                text => $token->{tag_name}, token => $token);
6723              } else { # "after after html"
6724              !!!cp ('t331');              !!!cp ('t331');
6725              !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'after after frameset:/',
6726                                text => $token->{tag_name}, token => $token);
6727            }            }
6728            ## Ignore the token            ## Ignore the token
6729            !!!next-token;            !!!next-token;
# Line 6271  sub _tree_construction_main ($) { Line 6790  sub _tree_construction_main ($) {
6790                                           ->{has_reference});                                           ->{has_reference});
6791            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
6792              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6793                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6794                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6795                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6796                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6797                !!!cp ('t336');                !!!cp ('t336');
6798                ## NOTE: Whether the encoding is supported or not is handled                ## NOTE: Whether the encoding is supported or not is handled
6799                ## in the {change_encoding} callback.                ## in the {change_encoding} callback.
# Line 6312  sub _tree_construction_main ($) { Line 6831  sub _tree_construction_main ($) {
6831          $parse_rcdata->(RCDATA_CONTENT_MODEL);          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6832          next B;          next B;
6833        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6834          !!!parse-error (type => 'in body:body', token => $token);          !!!parse-error (type => 'in body', text => 'body', token => $token);
6835                                
6836          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6837              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
# Line 6432  sub _tree_construction_main ($) { Line 6951  sub _tree_construction_main ($) {
6951              if ($i != -1) {              if ($i != -1) {
6952                !!!cp ('t355');                !!!cp ('t355');
6953                !!!parse-error (type => 'not closed',                !!!parse-error (type => 'not closed',
6954                                value => $self->{open_elements}->[-1]->[0]                                text => $self->{open_elements}->[-1]->[0]
6955                                    ->manakai_local_name,                                    ->manakai_local_name,
6956                                token => $token);                                token => $token);
6957              } else {              } else {
# Line 6586  sub _tree_construction_main ($) { Line 7105  sub _tree_construction_main ($) {
7105                  xmp => 1,                  xmp => 1,
7106                  iframe => 1,                  iframe => 1,
7107                  noembed => 1,                  noembed => 1,
7108                  noframes => 1,                  noframes => 1, ## NOTE: This is an "as if in head" code clone.
7109                  noscript => 0, ## TODO: 1 if scripting is enabled                  noscript => 0, ## TODO: 1 if scripting is enabled
7110                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7111          if ($token->{tag_name} eq 'xmp') {          if ($token->{tag_name} eq 'xmp') {
# Line 6608  sub _tree_construction_main ($) { Line 7127  sub _tree_construction_main ($) {
7127            !!!next-token;            !!!next-token;
7128            next B;            next B;
7129          } else {          } else {
7130              !!!ack ('t391.1');
7131    
7132            my $at = $token->{attributes};            my $at = $token->{attributes};
7133            my $form_attrs;            my $form_attrs;
7134            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 6651  sub _tree_construction_main ($) { Line 7172  sub _tree_construction_main ($) {
7172                           line => $token->{line}, column => $token->{column}},                           line => $token->{line}, column => $token->{column}},
7173                          {type => END_TAG_TOKEN, tag_name => 'form',                          {type => END_TAG_TOKEN, tag_name => 'form',
7174                           line => $token->{line}, column => $token->{column}};                           line => $token->{line}, column => $token->{column}};
           !!!nack ('t391.1'); ## NOTE: Not acknowledged.  
7175            !!!back-token (@tokens);            !!!back-token (@tokens);
7176            !!!next-token;            !!!next-token;
7177            next B;            next B;
# Line 6699  sub _tree_construction_main ($) { Line 7219  sub _tree_construction_main ($) {
7219            ## Ignore the token            ## Ignore the token
7220          } else {          } else {
7221            !!!cp ('t398');            !!!cp ('t398');
7222            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
7223          }          }
7224          !!!next-token;          !!!next-token;
7225          next B;          next B;
7226          } elsif ($token->{tag_name} eq 'rt' or
7227                   $token->{tag_name} eq 'rp') {
7228            ## has a |ruby| element in scope
7229            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7230              my $node = $self->{open_elements}->[$_];
7231              if ($node->[1] & RUBY_EL) {
7232                !!!cp ('t398.1');
7233                ## generate implied end tags
7234                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7235                  !!!cp ('t398.2');
7236                  pop @{$self->{open_elements}};
7237                }
7238                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
7239                  !!!cp ('t398.3');
7240                  !!!parse-error (type => 'not closed',
7241                                  text => $self->{open_elements}->[-1]->[0]
7242                                      ->manakai_local_name,
7243                                  token => $token);
7244                  pop @{$self->{open_elements}}
7245                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
7246                }
7247                last INSCOPE;
7248              } elsif ($node->[1] & SCOPING_EL) {
7249                !!!cp ('t398.4');
7250                last INSCOPE;
7251              }
7252            } # INSCOPE
7253    
7254            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7255    
7256            !!!nack ('t398.5');
7257            !!!next-token;
7258            redo B;
7259        } elsif ($token->{tag_name} eq 'math' or        } elsif ($token->{tag_name} eq 'math' or
7260                 $token->{tag_name} eq 'svg') {                 $token->{tag_name} eq 'svg') {
7261          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7262    
7263            ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
7264    
7265          ## "adjust SVG attributes" ('svg' only) - done in insert-element-f          ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
7266    
7267          ## "adjust foreign attributes" - done in insert-element-f          ## "adjust foreign attributes" - done in insert-element-f
# Line 6733  sub _tree_construction_main ($) { Line 7288  sub _tree_construction_main ($) {
7288                  thead => 1, tr => 1,                  thead => 1, tr => 1,
7289                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7290          !!!cp ('t401');          !!!cp ('t401');
7291          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'in body',
7292                            text => $token->{tag_name}, token => $token);
7293          ## Ignore the token          ## Ignore the token
7294          !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.          !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7295          !!!next-token;          !!!next-token;
# Line 6818  sub _tree_construction_main ($) { Line 7374  sub _tree_construction_main ($) {
7374            }            }
7375    
7376            !!!parse-error (type => 'start tag not allowed',            !!!parse-error (type => 'start tag not allowed',
7377                            value => $token->{tag_name}, token => $token);                            text => $token->{tag_name}, token => $token);
7378            ## NOTE: Ignore the token.            ## NOTE: Ignore the token.
7379            !!!next-token;            !!!next-token;
7380            next B;            next B;
# Line 6828  sub _tree_construction_main ($) { Line 7384  sub _tree_construction_main ($) {
7384            unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {            unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7385              !!!cp ('t403');              !!!cp ('t403');
7386              !!!parse-error (type => 'not closed',              !!!parse-error (type => 'not closed',
7387                              value => $_->[0]->manakai_local_name,                              text => $_->[0]->manakai_local_name,
7388                              token => $token);                              token => $token);
7389              last;              last;
7390            } else {            } else {
# Line 6848  sub _tree_construction_main ($) { Line 7404  sub _tree_construction_main ($) {
7404            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7405              !!!cp ('t406');              !!!cp ('t406');
7406              !!!parse-error (type => 'not closed',              !!!parse-error (type => 'not closed',
7407                              value => $self->{open_elements}->[1]->[0]                              text => $self->{open_elements}->[1]->[0]
7408                                  ->manakai_local_name,                                  ->manakai_local_name,
7409                              token => $token);                              token => $token);
7410            } else {            } else {
# Line 6859  sub _tree_construction_main ($) { Line 7415  sub _tree_construction_main ($) {
7415            next B;            next B;
7416          } else {          } else {
7417            !!!cp ('t408');            !!!cp ('t408');
7418            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7419                              text => $token->{tag_name}, token => $token);
7420            ## Ignore the token            ## Ignore the token
7421            !!!next-token;            !!!next-token;
7422            next B;            next B;
# Line 6887  sub _tree_construction_main ($) { Line 7444  sub _tree_construction_main ($) {
7444    
7445          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7446            !!!cp ('t413');            !!!cp ('t413');
7447            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7448                              text => $token->{tag_name}, token => $token);
7449              ## NOTE: Ignore the token.
7450          } else {          } else {
7451            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7452            while ({            while ({
7453                      ## END_TAG_OPTIONAL_EL
7454                    dd => ($token->{tag_name} ne 'dd'),                    dd => ($token->{tag_name} ne 'dd'),
7455                    dt => ($token->{tag_name} ne 'dt'),                    dt => ($token->{tag_name} ne 'dt'),
7456                    li => ($token->{tag_name} ne 'li'),                    li => ($token->{tag_name} ne 'li'),
7457                    p => 1,                    p => 1,
7458                      rt => 1,
7459                      rp => 1,
7460                   }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {                   }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7461              !!!cp ('t409');              !!!cp ('t409');
7462              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
# Line 6905  sub _tree_construction_main ($) { Line 7467  sub _tree_construction_main ($) {
7467                    ne $token->{tag_name}) {                    ne $token->{tag_name}) {
7468              !!!cp ('t412');              !!!cp ('t412');
7469              !!!parse-error (type => 'not closed',              !!!parse-error (type => 'not closed',
7470                              value => $self->{open_elements}->[-1]->[0]                              text => $self->{open_elements}->[-1]->[0]
7471                                  ->manakai_local_name,                                  ->manakai_local_name,
7472                              token => $token);                              token => $token);
7473            } else {            } else {
# Line 6942  sub _tree_construction_main ($) { Line 7504  sub _tree_construction_main ($) {
7504    
7505          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7506            !!!cp ('t421');            !!!cp ('t421');
7507            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7508                              text => $token->{tag_name}, token => $token);
7509              ## NOTE: Ignore the token.
7510          } else {          } else {
7511            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7512            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
# Line 6955  sub _tree_construction_main ($) { Line 7519  sub _tree_construction_main ($) {
7519                    ne $token->{tag_name}) {                    ne $token->{tag_name}) {
7520              !!!cp ('t417.1');              !!!cp ('t417.1');
7521              !!!parse-error (type => 'not closed',              !!!parse-error (type => 'not closed',
7522                              value => $self->{open_elements}->[-1]->[0]                              text => $self->{open_elements}->[-1]->[0]
7523                                  ->manakai_local_name,                                  ->manakai_local_name,
7524                              token => $token);                              token => $token);
7525            } else {            } else {
# Line 6987  sub _tree_construction_main ($) { Line 7551  sub _tree_construction_main ($) {
7551    
7552          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7553            !!!cp ('t425.1');            !!!cp ('t425.1');
7554            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7555                              text => $token->{tag_name}, token => $token);
7556              ## NOTE: Ignore the token.
7557          } else {          } else {
7558            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7559            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
# Line 6999  sub _tree_construction_main ($) { Line 7565  sub _tree_construction_main ($) {
7565            if ($self->{open_elements}->[-1]->[0]->manakai_local_name            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7566                    ne $token->{tag_name}) {                    ne $token->{tag_name}) {
7567              !!!cp ('t425');              !!!cp ('t425');
7568              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
7569                                text => $token->{tag_name}, token => $token);
7570            } else {            } else {
7571              !!!cp ('t426');              !!!cp ('t426');
7572            }            }
# Line 7030  sub _tree_construction_main ($) { Line 7597  sub _tree_construction_main ($) {
7597                    ne $token->{tag_name}) {                    ne $token->{tag_name}) {
7598              !!!cp ('t412.1');              !!!cp ('t412.1');
7599              !!!parse-error (type => 'not closed',              !!!parse-error (type => 'not closed',
7600                              value => $self->{open_elements}->[-1]->[0]                              text => $self->{open_elements}->[-1]->[0]
7601                                  ->manakai_local_name,                                  ->manakai_local_name,
7602                              token => $token);                              token => $token);
7603            } else {            } else {
# Line 7040  sub _tree_construction_main ($) { Line 7607  sub _tree_construction_main ($) {
7607            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7608          } else {          } else {
7609            !!!cp ('t413.1');            !!!cp ('t413.1');
7610            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7611                              text => $token->{tag_name}, token => $token);
7612    
7613            !!!cp ('t415.1');            !!!cp ('t415.1');
7614            ## As if <p>, then reprocess the current token            ## As if <p>, then reprocess the current token
# Line 7063  sub _tree_construction_main ($) { Line 7631  sub _tree_construction_main ($) {
7631          next B;          next B;
7632        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7633          !!!cp ('t428');          !!!cp ('t428');
7634          !!!parse-error (type => 'unmatched end tag:br', token => $token);          !!!parse-error (type => 'unmatched end tag',
7635                            text => 'br', token => $token);
7636    
7637          ## As if <br>          ## As if <br>
7638          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
# Line 7088  sub _tree_construction_main ($) { Line 7657  sub _tree_construction_main ($) {
7657                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7658                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7659          !!!cp ('t429');          !!!cp ('t429');
7660          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'unmatched end tag',
7661                            text => $token->{tag_name}, token => $token);
7662          ## Ignore the token          ## Ignore the token
7663          !!!next-token;          !!!next-token;
7664          next B;          next B;
# Line 7107  sub _tree_construction_main ($) { Line 7677  sub _tree_construction_main ($) {
7677              ## generate implied end tags              ## generate implied end tags
7678              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7679                !!!cp ('t430');                !!!cp ('t430');
7680                ## ISSUE: Can this case be reached?                ## NOTE: |<ruby><rt></ruby>|.
7681                  ## ISSUE: <ruby><rt></rt> will also take this code path,
7682                  ## which seems wrong.
7683                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
7684                  $node_i++;
7685              }              }
7686                    
7687              ## Step 2              ## Step 2
# Line 7117  sub _tree_construction_main ($) { Line 7690  sub _tree_construction_main ($) {
7690                !!!cp ('t431');                !!!cp ('t431');
7691                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7692                !!!parse-error (type => 'not closed',                !!!parse-error (type => 'not closed',
7693                                value => $self->{open_elements}->[-1]->[0]                                text => $self->{open_elements}->[-1]->[0]
7694                                    ->manakai_local_name,                                    ->manakai_local_name,
7695                                token => $token);                                token => $token);
7696              } else {              } else {
# Line 7125  sub _tree_construction_main ($) { Line 7698  sub _tree_construction_main ($) {
7698              }              }
7699                            
7700              ## Step 3              ## Step 3
7701              splice @{$self->{open_elements}}, $node_i;              splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7702    
7703              !!!next-token;              !!!next-token;
7704              last S2;              last S2;
# Line 7136  sub _tree_construction_main ($) { Line 7709  sub _tree_construction_main ($) {
7709                  ($node->[1] & SPECIAL_EL or                  ($node->[1] & SPECIAL_EL or
7710                   $node->[1] & SCOPING_EL)) {                   $node->[1] & SCOPING_EL)) {
7711                !!!cp ('t433');                !!!cp ('t433');
7712                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
7713                                  text => $token->{tag_name}, token => $token);
7714                ## Ignore the token                ## Ignore the token
7715                !!!next-token;                !!!next-token;
7716                last S2;                last S2;
# Line 7182  sub _tree_construction_main ($) { Line 7756  sub _tree_construction_main ($) {
7756    ## TODO: script stuffs    ## TODO: script stuffs
7757  } # _tree_construct_main  } # _tree_construct_main
7758    
7759  sub set_inner_html ($$$) {  sub set_inner_html ($$$$;$) {
7760    my $class = shift;    my $class = shift;
7761    my $node = shift;    my $node = shift;
7762    my $s = \$_[0];    #my $s = \$_[0];
7763    my $onerror = $_[1];    my $onerror = $_[1];
7764      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7765    
7766    ## ISSUE: Should {confident} be true?    ## ISSUE: Should {confident} be true?
7767    
# Line 7205  sub set_inner_html ($$$) { Line 7780  sub set_inner_html ($$$) {
7780      }      }
7781    
7782      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7783      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($_[0] => $node, $onerror, $get_wrapper);
7784    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7785      ## TODO: If non-html element      ## TODO: If non-html element
7786    
7787      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7788    
7789    ## TODO: Support for $get_wrapper
7790    
7791      ## Step 1 # MUST      ## Step 1 # MUST
7792      my $this_doc = $node->owner_document;      my $this_doc = $node->owner_document;
7793      my $doc = $this_doc->implementation->create_document;      my $doc = $this_doc->implementation->create_document;
# Line 7222  sub set_inner_html ($$$) { Line 7799  sub set_inner_html ($$$) {
7799      my $i = 0;      my $i = 0;
7800      $p->{line_prev} = $p->{line} = 1;      $p->{line_prev} = $p->{line} = 1;
7801      $p->{column_prev} = $p->{column} = 0;      $p->{column_prev} = $p->{column} = 0;
7802        require Whatpm::Charset::DecodeHandle;
7803        my $input = Whatpm::Charset::DecodeHandle::CharString->new (\($_[0]));
7804        $input = $get_wrapper->($input);
7805      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
7806        my $self = shift;        my $self = shift;
7807    
7808        pop @{$self->{prev_char}};        pop @{$self->{prev_char}};
7809        unshift @{$self->{prev_char}}, $self->{next_char};        unshift @{$self->{prev_char}}, $self->{next_char};
7810    
7811        $self->{next_char} = -1 and return if $i >= length $$s;        my $char = '';
7812        $self->{next_char} = ord substr $$s, $i++, 1;        if (defined $self->{next_next_char}) {
7813            $char = $self->{next_next_char};
7814            delete $self->{next_next_char};
7815            $self->{next_char} = ord $char;
7816          } else {
7817            if ($input->read ($char, 1)) {
7818              $self->{next_char} = ord $char;
7819            } else {
7820              $self->{next_char} = -1;
7821              return;
7822            }
7823          }
7824    
7825        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7826        $p->{column}++;        $p->{column}++;
# Line 7239  sub set_inner_html ($$$) { Line 7830  sub set_inner_html ($$$) {
7830          $p->{column} = 0;          $p->{column} = 0;
7831          !!!cp ('i1');          !!!cp ('i1');
7832        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7833          $i++ if substr ($$s, $i, 1) eq "\x0A";  ## TODO: support for abort/streaming
7834            my $next = '';
7835            if ($input->read ($next, 1) and $next ne "\x0A") {
7836              $self->{next_next_char} = $next;
7837            }
7838          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7839          $p->{line}++;          $p->{line}++;
7840          $p->{column} = 0;          $p->{column} = 0;
# Line 7272  sub set_inner_html ($$$) { Line 7867  sub set_inner_html ($$$) {
7867                  0x10FFFE => 1, 0x10FFFF => 1,                  0x10FFFE => 1, 0x10FFFF => 1,
7868                 }->{$self->{next_char}}) {                 }->{$self->{next_char}}) {
7869          !!!cp ('i4.1');          !!!cp ('i4.1');
7870          !!!parse-error (type => 'control char', level => $self->{must_level});          if ($self->{next_char} < 0x10000) {
7871  ## TODO: error type documentation            !!!parse-error (type => 'control char',
7872                              text => (sprintf 'U+%04X', $self->{next_char}));
7873            } else {
7874              !!!parse-error (type => 'control char',
7875                              text => (sprintf 'U-%08X', $self->{next_char}));
7876            }
7877        }        }
7878      };      };
7879      $p->{prev_char} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
7880      $p->{next_char} = -1;      $p->{next_char} = -1;
7881        
7882        $p->{read_until} = sub {
7883          #my ($scalar, $specials_range, $offset) = @_;
7884          my $specials_range = $_[1];
7885          return 0 if defined $p->{next_next_char};
7886          my $count = $input->manakai_read_until
7887            ($_[0],
7888             qr/(?![$specials_range\x{FDD0}-\x{FDDF}\x{FFFE}\x{FFFF}\x{1FFFE}\x{1FFFF}\x{2FFFE}\x{2FFFF}\x{3FFFE}\x{3FFFF}\x{4FFFE}\x{4FFFF}\x{5FFFE}\x{5FFFF}\x{6FFFE}\x{6FFFF}\x{7FFFE}\x{7FFFF}\x{8FFFE}\x{8FFFF}\x{9FFFE}\x{9FFFF}\x{AFFFE}\x{AFFFF}\x{BFFFE}\x{BFFFF}\x{CFFFE}\x{CFFFF}\x{DFFFE}\x{DFFFF}\x{EFFFE}\x{EFFFF}\x{FFFFE}\x{FFFFF}])[\x20-\x7E\xA0-\x{D7FF}\x{E000}-\x{10FFFD}]/,
7889             $_[2]);
7890          if ($count) {
7891            $p->{column} += $count;
7892            $p->{column_prev} += $count;
7893            $p->{prev_char} = [-1, -1, -1];
7894            $p->{next_char} = -1;
7895          }
7896          return $count;
7897        }; # $p->{read_until}
7898    
7899      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7900        my (%opt) = @_;        my (%opt) = @_;
7901        my $line = $opt{line};        my $line = $opt{line};
# Line 7293  sub set_inner_html ($$$) { Line 7910  sub set_inner_html ($$$) {
7910        $ponerror->(line => $p->{line}, column => $p->{column}, @_);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7911      };      };
7912            
7913        my $char_onerror = sub {
7914          my (undef, $type, %opt) = @_;
7915          $ponerror->(layer => 'encode',
7916                      line => $p->{line}, column => $p->{column} + 1,
7917                      %opt, type => $type);
7918        }; # $char_onerror
7919        $input->onerror ($char_onerror);
7920    
7921      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7922      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7923    

Legend:
Removed from v.1.137  
changed lines
  Added in v.1.179

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24