/[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.134 by wakaba, Sat May 17 05:34:23 2008 UTC revision 1.166 by wakaba, Sat Sep 13 08:21:35 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    my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);    my $byte_stream = $_[0];
   my $s;  
362    
363    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
364      my (%opt) = @_;      my (%opt) = @_;
# Line 343  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;
376    my ($e, $e_status);    my $buffer;
377      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        ($e, $e_status) = $charset->get_perl_encoding        ($char_stream, $e_status) = $charset->get_decode_handle
395            (allow_error_reporting => 1,            ($byte_stream, allow_error_reporting => 1,
396             allow_fallback => 1);             allow_fallback => 1);
397        if ($e) {        if ($char_stream) {
398          $self->{confident} = 1;          $self->{confident} = 1;
399          last SNIFFING;          last SNIFFING;
400          } else {
401            ## TODO: unsupported error
402        }        }
403      }      }
404    
405      ## Step 2      ## Step 2
406      # wait      my $byte_buffer = '';
407        for (1..1024) {
408          my $char = $byte_stream->getc;
409          last unless defined $char;
410          $byte_buffer .= $char;
411        } ## TODO: timeout
412    
413      ## Step 3      ## Step 3
414      my $head = substr ($$bytes_s, 0, 3);      if ($byte_buffer =~ /^\xFE\xFF/) {
415      if ($head =~ /^\xFE\xFF/) {        $charset = Message::Charset::Info->get_by_html_name ('utf-16be');
416        $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');        ($char_stream, $e_status) = $charset->get_decode_handle
417        ($e, $e_status) = $charset->get_perl_encoding            ($byte_stream, allow_error_reporting => 1,
418            (allow_error_reporting => 1,             allow_fallback => 1, byte_buffer => \$byte_buffer);
            allow_fallback => 1);  
419        $self->{confident} = 1;        $self->{confident} = 1;
420        last SNIFFING;        last SNIFFING;
421      } elsif ($head =~ /^\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        ($e, $e_status) = $charset->get_perl_encoding        ($char_stream, $e_status) = $charset->get_decode_handle
424            (allow_error_reporting => 1,            ($byte_stream, allow_error_reporting => 1,
425             allow_fallback => 1);             allow_fallback => 1, byte_buffer => \$byte_buffer);
426        $self->{confident} = 1;        $self->{confident} = 1;
427        last SNIFFING;        last SNIFFING;
428      } elsif ($head eq "\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        ($e, $e_status) = $charset->get_perl_encoding        ($char_stream, $e_status) = $charset->get_decode_handle
431            (allow_error_reporting => 1,            ($byte_stream, allow_error_reporting => 1,
432             allow_fallback => 1);             allow_fallback => 1, byte_buffer => \$byte_buffer);
433        $self->{confident} = 1;        $self->{confident} = 1;
434        last SNIFFING;        last SNIFFING;
435      }      }
# Line 401  sub parse_byte_string ($$$$;$) { Line 443  sub parse_byte_string ($$$$;$) {
443      ## Step 6      ## Step 6
444      require Whatpm::Charset::UniversalCharDet;      require Whatpm::Charset::UniversalCharDet;
445      $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string      $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
446          (substr ($$bytes_s, 0, 1024));          ($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        ($e, $e_status) = $charset->get_perl_encoding        require Whatpm::Charset::DecodeHandle;
452            (allow_error_reporting => 1,        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
453             allow_fallback => 1);            ($byte_stream);
454        if ($e) {        ($char_stream, $e_status) = $charset->get_decode_handle
455          !!!parse-error (type => 'sniffing:chardet', ## TODO: type name            ($buffer, allow_error_reporting => 1,
456                          value => $charset_name,             allow_fallback => 1, byte_buffer => \$byte_buffer);
457                          level => $self->{info_level},        if ($char_stream) {
458            $buffer->{buffer} = $byte_buffer;
459            !!!parse-error (type => 'sniffing:chardet',
460                            text => $charset_name,
461                            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 421  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      ($e, $e_status) = $charset->get_perl_encoding (allow_error_reporting => 1,      require Whatpm::Charset::DecodeHandle;
475                                                     allow_fallback => 1);      $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
476      !!!parse-error (type => 'sniffing:default', ## TODO: type name          ($byte_stream);
477                      value => 'windows-1252',      ($char_stream, $e_status)
478                      level => $self->{info_level},          = $charset->get_decode_handle ($buffer,
479                      line => 1, column => 1);                                         allow_error_reporting => 1,
480                                           allow_fallback => 1,
481                                           byte_buffer => \$byte_buffer);
482        $buffer->{buffer} = $byte_buffer;
483        !!!parse-error (type => 'sniffing:default',
484                        text => 'windows-1252',
485                        level => $self->{level}->{info},
486                        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 => $e->name,      !!!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    }    }
   $s = \ $e->decode ($$bytes_s);  
509    
510    $self->{change_encoding} = sub {    $self->{change_encoding} = sub {
511      my $self = shift;      my $self = shift;
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      ($e, $e_status) = $charset->get_perl_encoding      ($char_stream, $e_status) = $charset->get_decode_handle
517          (allow_error_reporting => 1, allow_fallback => 1);          ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
518             byte_buffer => \ $buffer->{buffer});
519            
520      if ($e) { # if supported      if ($char_stream) { # if supported
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          ($e, $e_status) = $charset->get_perl_encoding;          $charset = Message::Charset::Info->get_by_html_name ('utf-8');
527            ($char_stream, $e_status) = $charset->get_decode_handle
528                ($byte_stream,
529                 byte_buffer => \ $buffer->{buffer});
530        }        }
531        $charset_name = $charset->get_iana_name;        $charset_name = $charset->get_iana_name;
532                
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 492  sub parse_byte_string ($$$$;$) { Line 558  sub parse_byte_string ($$$$;$) {
558      }      }
559    }; # $self->{change_encoding}    }; # $self->{change_encoding}
560    
561      my $char_onerror = sub {
562        my (undef, $type, %opt) = @_;
563        !!!parse-error (layer => 'encode',
564                        %opt, type => $type,
565                        line => $self->{line}, column => $self->{column} + 1);
566        if ($opt{octets}) {
567          ${$opt{octets}} = "\x{FFFD}"; # relacement character
568        }
569      };
570    
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_string ($s, @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 => $e->name,        !!!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      }      }
     $s = \ $e->decode ($$bytes_s);  
599      $self->{confident} = 1;      $self->{confident} = 1;
600      $return = $self->parse_char_string ($s, @args);  
601        $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 528  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  *parse_char_string = \&parse_string;  sub parse_char_string ($$$;$$) {
619      #my ($self, $s, $doc, $onerror, $get_wrapper) = @_;
620      my $self = shift;
621      require utf8;
622      my $s = ref $_[0] ? $_[0] : \($_[0]);
623      open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;
624      if ($_[3]) {
625        $input = $_[3]->($input);
626      }
627      return $self->parse_char_stream ($input, @_[1..$#_]);
628    } # parse_char_string
629    *parse_string = \&parse_char_string; ## NOTE: Alias for backward compatibility.
630    
631  sub parse_string ($$$;$) {  sub parse_char_stream ($$$;$) {
632    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
633    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $input = $_[0];
634    $self->{document} = $_[1];    $self->{document} = $_[1];
635    @{$self->{document}->child_nodes} = ();    @{$self->{document}->child_nodes} = ();
636    
# Line 551  sub parse_string ($$$;$) { Line 649  sub parse_string ($$$;$) {
649      pop @{$self->{prev_char}};      pop @{$self->{prev_char}};
650      unshift @{$self->{prev_char}}, $self->{next_char};      unshift @{$self->{prev_char}}, $self->{next_char};
651    
652      $self->{next_char} = -1 and return if $i >= length $$s;      my $char;
653      $self->{next_char} = ord substr $$s, $i++, 1;      if (defined $self->{next_next_char}) {
654          $char = $self->{next_next_char};
655          delete $self->{next_next_char};
656        } else {
657          $char = $input->getc;
658        }
659        $self->{next_char} = -1 and return unless defined $char;
660        $self->{next_char} = ord $char;
661    
662      ($self->{line_prev}, $self->{column_prev})      ($self->{line_prev}, $self->{column_prev})
663          = ($self->{line}, $self->{column});          = ($self->{line}, $self->{column});
# Line 564  sub parse_string ($$$;$) { Line 669  sub parse_string ($$$;$) {
669        $self->{column} = 0;        $self->{column} = 0;
670      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
671        !!!cp ('j2');        !!!cp ('j2');
672        $i++ if substr ($$s, $i, 1) eq "\x0A";        my $next = $input->getc;
673          if (defined $next and $next ne "\x0A") {
674            $self->{next_next_char} = $next;
675          }
676        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
677        $self->{line}++;        $self->{line}++;
678        $self->{column} = 0;        $self->{column} = 0;
# Line 592  sub parse_string ($$$;$) { Line 700  sub parse_string ($$$;$) {
700                0x10FFFE => 1, 0x10FFFF => 1,                0x10FFFE => 1, 0x10FFFF => 1,
701               }->{$self->{next_char}}) {               }->{$self->{next_char}}) {
702        !!!cp ('j5');        !!!cp ('j5');
703        !!!parse-error (type => 'control char', level => $self->{must_level});        if ($self->{next_char} < 0x10000) {
704  ## TODO: error type documentation          !!!parse-error (type => 'control char',
705                            text => (sprintf 'U+%04X', $self->{next_char}));
706          } else {
707            !!!parse-error (type => 'control char',
708                            text => (sprintf 'U-%08X', $self->{next_char}));
709          }
710      }      }
711    };    };
712    $self->{prev_char} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
# Line 617  sub parse_string ($$$;$) { Line 730  sub parse_string ($$$;$) {
730    delete $self->{parse_error}; # remove loop    delete $self->{parse_error}; # remove loop
731    
732    return $self->{document};    return $self->{document};
733  } # parse_string  } # parse_char_stream
734    
735  sub new ($) {  sub new ($) {
736    my $class = shift;    my $class = shift;
737    my $self = bless {    my $self = bless {
738      must_level => 'm',      level => {must => 'm',
739      should_level => 's',                should => 's',
740      good_level => 'w',                warn => 'w',
741      warn_level => 'w',                info => 'i',
742      info_level => 'i',                uncertain => 'u'},
     unsupported_level => 'u',  
743    }, $class;    }, $class;
744    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
745      $self->{next_char} = -1;      $self->{next_char} = -1;
# Line 691  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STAT Line 803  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STAT
803  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
804  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
805  sub SELF_CLOSING_START_TAG_STATE () { 34 }  sub SELF_CLOSING_START_TAG_STATE () { 34 }
806  sub CDATA_BLOCK_STATE () { 35 }  sub CDATA_SECTION_STATE () { 35 }
807    sub MD_HYPHEN_STATE () { 36 } # "markup declaration open state" in the spec
808    sub MD_DOCTYPE_STATE () { 37 } # "markup declaration open state" in the spec
809    sub MD_CDATA_STATE () { 38 } # "markup declaration open state" in the spec
810    sub CDATA_PCDATA_CLOSE_TAG_STATE () { 39 } # "close tag open state" in the spec
811    sub CDATA_SECTION_MSE1_STATE () { 40 } # "CDATA section state" in the spec
812    sub CDATA_SECTION_MSE2_STATE () { 41 } # "CDATA section state" in the spec
813    sub PUBLIC_STATE () { 42 } # "after DOCTYPE name state" in the spec
814    sub SYSTEM_STATE () { 43 } # "after DOCTYPE name state" in the spec
815    
816  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
817  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 744  sub IN_COLUMN_GROUP_IM () { 0b10 } Line 864  sub IN_COLUMN_GROUP_IM () { 0b10 }
864  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
865    my $self = shift;    my $self = shift;
866    $self->{state} = DATA_STATE; # MUST    $self->{state} = DATA_STATE; # MUST
867      #$self->{state_keyword}; # initialized when used
868    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
869    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token};
870    undef $self->{current_attribute};    undef $self->{current_attribute};
871    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
872    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
# Line 774  sub _initialize_tokenizer ($) { Line 895  sub _initialize_tokenizer ($) {
895  ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|  ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
896  ##     while the token is pushed back to the stack.  ##     while the token is pushed back to the stack.
897    
 ## 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?  
   
898  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
899    
900  ## 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 993  sub _get_next_token ($) { Line 1110  sub _get_next_token ($) {
1110            redo A;            redo A;
1111          } else {          } else {
1112            !!!cp (23);            !!!cp (23);
1113            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago',
1114                              line => $self->{line_prev},
1115                              column => $self->{column_prev});
1116            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1117            ## reconsume            ## reconsume
1118    
# Line 1008  sub _get_next_token ($) { Line 1127  sub _get_next_token ($) {
1127          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1128        }        }
1129      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1130          ## NOTE: The "close tag open state" in the spec is implemented as
1131          ## |CLOSE_TAG_OPEN_STATE| and |CDATA_PCDATA_CLOSE_TAG_STATE|.
1132    
1133        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1134        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1135          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1136              $self->{state} = CDATA_PCDATA_CLOSE_TAG_STATE;
1137            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            $self->{state_keyword} = '';
1138            my @next_char;            ## Reconsume.
1139            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...  
           }  
1140          } else {          } else {
1141            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1142              ## NOTE: See <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>.
1143            !!!cp (28);            !!!cp (28);
           # next-input-character is already done  
1144            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1145              ## Reconsume.
1146            !!!emit ({type => CHARACTER_TOKEN, data => '</',            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1147                      line => $l, column => $c,                      line => $l, column => $c,
1148                     });                     });
1149            redo A;            redo A;
1150          }          }
1151        }        }
1152          
1153        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{next_char} and
1154            $self->{next_char} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1155          !!!cp (29);          !!!cp (29);
# Line 1117  sub _get_next_token ($) { Line 1196  sub _get_next_token ($) {
1196                                    line => $self->{line_prev}, # "<" of "</"                                    line => $self->{line_prev}, # "<" of "</"
1197                                    column => $self->{column_prev} - 1,                                    column => $self->{column_prev} - 1,
1198                                   };                                   };
1199          ## $self->{next_char} is intentionally left as is          ## NOTE: $self->{next_char} is intentionally left as is.
1200          redo A;          ## Although the "anything else" case of the spec not explicitly
1201            ## states that the next input character is to be reconsumed,
1202            ## it will be included to the |data| of the comment token
1203            ## generated from the bogus end tag, as defined in the
1204            ## "bogus comment state" entry.
1205            redo A;
1206          }
1207        } elsif ($self->{state} == CDATA_PCDATA_CLOSE_TAG_STATE) {
1208          my $ch = substr $self->{last_emitted_start_tag_name}, length $self->{state_keyword}, 1;
1209          if (length $ch) {
1210            my $CH = $ch;
1211            $ch =~ tr/a-z/A-Z/;
1212            my $nch = chr $self->{next_char};
1213            if ($nch eq $ch or $nch eq $CH) {
1214              !!!cp (24);
1215              ## Stay in the state.
1216              $self->{state_keyword} .= $nch;
1217              !!!next-input-character;
1218              redo A;
1219            } else {
1220              !!!cp (25);
1221              $self->{state} = DATA_STATE;
1222              ## Reconsume.
1223              !!!emit ({type => CHARACTER_TOKEN,
1224                        data => '</' . $self->{state_keyword},
1225                        line => $self->{line_prev},
1226                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1227                       });
1228              redo A;
1229            }
1230          } else { # after "<{tag-name}"
1231            unless ({
1232                     0x0009 => 1, # HT
1233                     0x000A => 1, # LF
1234                     0x000B => 1, # VT
1235                     0x000C => 1, # FF
1236                     0x0020 => 1, # SP
1237                     0x003E => 1, # >
1238                     0x002F => 1, # /
1239                     -1 => 1, # EOF
1240                    }->{$self->{next_char}}) {
1241              !!!cp (26);
1242              ## Reconsume.
1243              $self->{state} = DATA_STATE;
1244              !!!emit ({type => CHARACTER_TOKEN,
1245                        data => '</' . $self->{state_keyword},
1246                        line => $self->{line_prev},
1247                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1248                       });
1249              redo A;
1250            } else {
1251              !!!cp (27);
1252              $self->{current_token}
1253                  = {type => END_TAG_TOKEN,
1254                     tag_name => $self->{last_emitted_start_tag_name},
1255                     line => $self->{line_prev},
1256                     column => $self->{column_prev} - 1 - length $self->{state_keyword}};
1257              $self->{state} = TAG_NAME_STATE;
1258              ## Reconsume.
1259              redo A;
1260            }
1261        }        }
1262      } elsif ($self->{state} == TAG_NAME_STATE) {      } elsif ($self->{state} == TAG_NAME_STATE) {
1263        if ($self->{next_char} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
# Line 1288  sub _get_next_token ($) { Line 1427  sub _get_next_token ($) {
1427          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1428              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1429            !!!cp (57);            !!!cp (57);
1430            !!!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});
1431            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1432          } else {          } else {
1433            !!!cp (58);            !!!cp (58);
# Line 1459  sub _get_next_token ($) { Line 1598  sub _get_next_token ($) {
1598    
1599          redo A;          redo A;
1600        } else {        } else {
1601          !!!cp (82);          if ($self->{next_char} == 0x0022 or # "
1602                $self->{next_char} == 0x0027) { # '
1603              !!!cp (78);
1604              !!!parse-error (type => 'bad attribute name');
1605            } else {
1606              !!!cp (82);
1607            }
1608          $self->{current_attribute}          $self->{current_attribute}
1609              = {name => chr ($self->{next_char}),              = {name => chr ($self->{next_char}),
1610                 value => '',                 value => '',
# Line 1494  sub _get_next_token ($) { Line 1639  sub _get_next_token ($) {
1639          !!!next-input-character;          !!!next-input-character;
1640          redo A;          redo A;
1641        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1642            !!!parse-error (type => 'empty unquoted attribute value');
1643          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1644            !!!cp (87);            !!!cp (87);
1645            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
# Line 1770  sub _get_next_token ($) { Line 1916  sub _get_next_token ($) {
1916          $self->{state} = SELF_CLOSING_START_TAG_STATE;          $self->{state} = SELF_CLOSING_START_TAG_STATE;
1917          !!!next-input-character;          !!!next-input-character;
1918          redo A;          redo A;
1919          } elsif ($self->{next_char} == -1) {
1920            !!!parse-error (type => 'unclosed tag');
1921            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1922              !!!cp (122.3);
1923              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1924            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1925              if ($self->{current_token}->{attributes}) {
1926                !!!cp (122.1);
1927                !!!parse-error (type => 'end tag attribute');
1928              } else {
1929                ## NOTE: This state should never be reached.
1930                !!!cp (122.2);
1931              }
1932            } else {
1933              die "$0: $self->{current_token}->{type}: Unknown token type";
1934            }
1935            $self->{state} = DATA_STATE;
1936            ## Reconsume.
1937            !!!emit ($self->{current_token}); # start tag or end tag
1938            redo A;
1939        } else {        } else {
1940          !!!cp ('124.1');          !!!cp ('124.1');
1941          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
# Line 1802  sub _get_next_token ($) { Line 1968  sub _get_next_token ($) {
1968          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1969    
1970          redo A;          redo A;
1971          } elsif ($self->{next_char} == -1) {
1972            !!!parse-error (type => 'unclosed tag');
1973            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1974              !!!cp (124.7);
1975              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1976            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1977              if ($self->{current_token}->{attributes}) {
1978                !!!cp (124.5);
1979                !!!parse-error (type => 'end tag attribute');
1980              } else {
1981                ## NOTE: This state should never be reached.
1982                !!!cp (124.6);
1983              }
1984            } else {
1985              die "$0: $self->{current_token}->{type}: Unknown token type";
1986            }
1987            $self->{state} = DATA_STATE;
1988            ## Reconsume.
1989            !!!emit ($self->{current_token}); # start tag or end tag
1990            redo A;
1991        } else {        } else {
1992          !!!cp ('124.4');          !!!cp ('124.4');
1993          !!!parse-error (type => 'nestc');          !!!parse-error (type => 'nestc');
# Line 1844  sub _get_next_token ($) { Line 2030  sub _get_next_token ($) {
2030        die "$0: _get_next_token: unexpected case [BC]";        die "$0: _get_next_token: unexpected case [BC]";
2031      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2032        ## (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};  
2033                
2034        if ($self->{next_char} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2035            !!!cp (133);
2036            $self->{state} = MD_HYPHEN_STATE;
2037          !!!next-input-character;          !!!next-input-character;
2038          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);  
         }  
2039        } elsif ($self->{next_char} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
2040                 $self->{next_char} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
2041            ## ASCII case-insensitive.
2042            !!!cp (130);
2043            $self->{state} = MD_DOCTYPE_STATE;
2044            $self->{state_keyword} = chr $self->{next_char};
2045          !!!next-input-character;          !!!next-input-character;
2046          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);  
         }  
2047        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2048                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2049                 $self->{next_char} == 0x005B) { # [                 $self->{next_char} == 0x005B) { # [
2050            !!!cp (135.4);                
2051            $self->{state} = MD_CDATA_STATE;
2052            $self->{state_keyword} = '[';
2053          !!!next-input-character;          !!!next-input-character;
2054          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);  
         }  
2055        } else {        } else {
2056          !!!cp (136);          !!!cp (136);
2057        }        }
2058    
2059        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment',
2060        $self->{next_char} = shift @next_char;                        line => $self->{line_prev},
2061        !!!back-next-input-character (@next_char);                        column => $self->{column_prev} - 1);
2062          ## Reconsume.
2063        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2064        $self->{current_token} = {type => COMMENT_TOKEN, data => '',        $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2065                                  line => $l, column => $c,                                  line => $self->{line_prev},
2066                                    column => $self->{column_prev} - 1,
2067                                 };                                 };
2068        redo A;        redo A;
2069              } elsif ($self->{state} == MD_HYPHEN_STATE) {
2070        ## ISSUE: typos in spec: chacacters, is is a parse error        if ($self->{next_char} == 0x002D) { # -
2071        ## 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);
2072            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2073                                      line => $self->{line_prev},
2074                                      column => $self->{column_prev} - 2,
2075                                     };
2076            $self->{state} = COMMENT_START_STATE;
2077            !!!next-input-character;
2078            redo A;
2079          } else {
2080            !!!cp (128);
2081            !!!parse-error (type => 'bogus comment',
2082                            line => $self->{line_prev},
2083                            column => $self->{column_prev} - 2);
2084            $self->{state} = BOGUS_COMMENT_STATE;
2085            ## Reconsume.
2086            $self->{current_token} = {type => COMMENT_TOKEN,
2087                                      data => '-',
2088                                      line => $self->{line_prev},
2089                                      column => $self->{column_prev} - 2,
2090                                     };
2091            redo A;
2092          }
2093        } elsif ($self->{state} == MD_DOCTYPE_STATE) {
2094          ## ASCII case-insensitive.
2095          if ($self->{next_char} == [
2096                undef,
2097                0x004F, # O
2098                0x0043, # C
2099                0x0054, # T
2100                0x0059, # Y
2101                0x0050, # P
2102              ]->[length $self->{state_keyword}] or
2103              $self->{next_char} == [
2104                undef,
2105                0x006F, # o
2106                0x0063, # c
2107                0x0074, # t
2108                0x0079, # y
2109                0x0070, # p
2110              ]->[length $self->{state_keyword}]) {
2111            !!!cp (131);
2112            ## Stay in the state.
2113            $self->{state_keyword} .= chr $self->{next_char};
2114            !!!next-input-character;
2115            redo A;
2116          } elsif ((length $self->{state_keyword}) == 6 and
2117                   ($self->{next_char} == 0x0045 or # E
2118                    $self->{next_char} == 0x0065)) { # e
2119            !!!cp (129);
2120            $self->{state} = DOCTYPE_STATE;
2121            $self->{current_token} = {type => DOCTYPE_TOKEN,
2122                                      quirks => 1,
2123                                      line => $self->{line_prev},
2124                                      column => $self->{column_prev} - 7,
2125                                     };
2126            !!!next-input-character;
2127            redo A;
2128          } else {
2129            !!!cp (132);        
2130            !!!parse-error (type => 'bogus comment',
2131                            line => $self->{line_prev},
2132                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2133            $self->{state} = BOGUS_COMMENT_STATE;
2134            ## Reconsume.
2135            $self->{current_token} = {type => COMMENT_TOKEN,
2136                                      data => $self->{state_keyword},
2137                                      line => $self->{line_prev},
2138                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2139                                     };
2140            redo A;
2141          }
2142        } elsif ($self->{state} == MD_CDATA_STATE) {
2143          if ($self->{next_char} == {
2144                '[' => 0x0043, # C
2145                '[C' => 0x0044, # D
2146                '[CD' => 0x0041, # A
2147                '[CDA' => 0x0054, # T
2148                '[CDAT' => 0x0041, # A
2149              }->{$self->{state_keyword}}) {
2150            !!!cp (135.1);
2151            ## Stay in the state.
2152            $self->{state_keyword} .= chr $self->{next_char};
2153            !!!next-input-character;
2154            redo A;
2155          } elsif ($self->{state_keyword} eq '[CDATA' and
2156                   $self->{next_char} == 0x005B) { # [
2157            !!!cp (135.2);
2158            $self->{current_token} = {type => CHARACTER_TOKEN,
2159                                      data => '',
2160                                      line => $self->{line_prev},
2161                                      column => $self->{column_prev} - 7};
2162            $self->{state} = CDATA_SECTION_STATE;
2163            !!!next-input-character;
2164            redo A;
2165          } else {
2166            !!!cp (135.3);
2167            !!!parse-error (type => 'bogus comment',
2168                            line => $self->{line_prev},
2169                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2170            $self->{state} = BOGUS_COMMENT_STATE;
2171            ## Reconsume.
2172            $self->{current_token} = {type => COMMENT_TOKEN,
2173                                      data => $self->{state_keyword},
2174                                      line => $self->{line_prev},
2175                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2176                                     };
2177            redo A;
2178          }
2179      } elsif ($self->{state} == COMMENT_START_STATE) {      } elsif ($self->{state} == COMMENT_START_STATE) {
2180        if ($self->{next_char} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2181          !!!cp (137);          !!!cp (137);
# Line 2241  sub _get_next_token ($) { Line 2442  sub _get_next_token ($) {
2442          redo A;          redo A;
2443        } elsif ($self->{next_char} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
2444                 $self->{next_char} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
2445            $self->{state} = PUBLIC_STATE;
2446            $self->{state_keyword} = chr $self->{next_char};
2447          !!!next-input-character;          !!!next-input-character;
2448          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);  
         }  
   
         #  
2449        } elsif ($self->{next_char} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
2450                 $self->{next_char} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
2451            $self->{state} = SYSTEM_STATE;
2452            $self->{state_keyword} = chr $self->{next_char};
2453          !!!next-input-character;          !!!next-input-character;
2454          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);  
         }  
   
         #  
2455        } else {        } else {
2456          !!!cp (180);          !!!cp (180);
2457            !!!parse-error (type => 'string after DOCTYPE name');
2458            $self->{current_token}->{quirks} = 1;
2459    
2460            $self->{state} = BOGUS_DOCTYPE_STATE;
2461          !!!next-input-character;          !!!next-input-character;
2462          #          redo A;
2463        }        }
2464        } elsif ($self->{state} == PUBLIC_STATE) {
2465          ## ASCII case-insensitive
2466          if ($self->{next_char} == [
2467                undef,
2468                0x0055, # U
2469                0x0042, # B
2470                0x004C, # L
2471                0x0049, # I
2472              ]->[length $self->{state_keyword}] or
2473              $self->{next_char} == [
2474                undef,
2475                0x0075, # u
2476                0x0062, # b
2477                0x006C, # l
2478                0x0069, # i
2479              ]->[length $self->{state_keyword}]) {
2480            !!!cp (175);
2481            ## Stay in the state.
2482            $self->{state_keyword} .= chr $self->{next_char};
2483            !!!next-input-character;
2484            redo A;
2485          } elsif ((length $self->{state_keyword}) == 5 and
2486                   ($self->{next_char} == 0x0043 or # C
2487                    $self->{next_char} == 0x0063)) { # c
2488            !!!cp (168);
2489            $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2490            !!!next-input-character;
2491            redo A;
2492          } else {
2493            !!!cp (169);
2494            !!!parse-error (type => 'string after DOCTYPE name',
2495                            line => $self->{line_prev},
2496                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2497            $self->{current_token}->{quirks} = 1;
2498    
2499        !!!parse-error (type => 'string after DOCTYPE name');          $self->{state} = BOGUS_DOCTYPE_STATE;
2500        $self->{current_token}->{quirks} = 1;          ## Reconsume.
2501            redo A;
2502          }
2503        } elsif ($self->{state} == SYSTEM_STATE) {
2504          ## ASCII case-insensitive
2505          if ($self->{next_char} == [
2506                undef,
2507                0x0059, # Y
2508                0x0053, # S
2509                0x0054, # T
2510                0x0045, # E
2511              ]->[length $self->{state_keyword}] or
2512              $self->{next_char} == [
2513                undef,
2514                0x0079, # y
2515                0x0073, # s
2516                0x0074, # t
2517                0x0065, # e
2518              ]->[length $self->{state_keyword}]) {
2519            !!!cp (170);
2520            ## Stay in the state.
2521            $self->{state_keyword} .= chr $self->{next_char};
2522            !!!next-input-character;
2523            redo A;
2524          } elsif ((length $self->{state_keyword}) == 5 and
2525                   ($self->{next_char} == 0x004D or # M
2526                    $self->{next_char} == 0x006D)) { # m
2527            !!!cp (171);
2528            $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2529            !!!next-input-character;
2530            redo A;
2531          } else {
2532            !!!cp (172);
2533            !!!parse-error (type => 'string after DOCTYPE name',
2534                            line => $self->{line_prev},
2535                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2536            $self->{current_token}->{quirks} = 1;
2537    
2538        $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2539        # next-input-character is already done          ## Reconsume.
2540        redo A;          redo A;
2541          }
2542      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2543        if ({        if ({
2544              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
# Line 2559  sub _get_next_token ($) { Line 2771  sub _get_next_token ($) {
2771          redo A;          redo A;
2772        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2773          !!!cp (208);          !!!cp (208);
2774          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2775    
2776          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2777          !!!next-input-character;          !!!next-input-character;
# Line 2595  sub _get_next_token ($) { Line 2807  sub _get_next_token ($) {
2807          redo A;          redo A;
2808        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2809          !!!cp (212);          !!!cp (212);
2810          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2811    
2812          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2813          !!!next-input-character;          !!!next-input-character;
# Line 2643  sub _get_next_token ($) { Line 2855  sub _get_next_token ($) {
2855        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
2856          !!!cp (217);          !!!cp (217);
2857          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
   
2858          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2859          ## reconsume          ## reconsume
2860    
# Line 2684  sub _get_next_token ($) { Line 2895  sub _get_next_token ($) {
2895          !!!next-input-character;          !!!next-input-character;
2896          redo A;          redo A;
2897        }        }
2898      } elsif ($self->{state} == CDATA_BLOCK_STATE) {      } elsif ($self->{state} == CDATA_SECTION_STATE) {
2899        my $s = '';        ## NOTE: "CDATA section state" in the state is jointly implemented
2900          ## by three states, |CDATA_SECTION_STATE|, |CDATA_SECTION_MSE1_STATE|,
2901          ## and |CDATA_SECTION_MSE2_STATE|.
2902                
2903        my ($l, $c) = ($self->{line}, $self->{column});        if ($self->{next_char} == 0x005D) { # ]
2904            !!!cp (221.1);
2905            $self->{state} = CDATA_SECTION_MSE1_STATE;
2906            !!!next-input-character;
2907            redo A;
2908          } elsif ($self->{next_char} == -1) {
2909            $self->{state} = DATA_STATE;
2910            !!!next-input-character;
2911            if (length $self->{current_token}->{data}) { # character
2912              !!!cp (221.2);
2913              !!!emit ($self->{current_token}); # character
2914            } else {
2915              !!!cp (221.3);
2916              ## No token to emit. $self->{current_token} is discarded.
2917            }        
2918            redo A;
2919          } else {
2920            !!!cp (221.4);
2921            $self->{current_token}->{data} .= chr $self->{next_char};
2922            ## Stay in the state.
2923            !!!next-input-character;
2924            redo A;
2925          }
2926    
2927        CS: while ($self->{next_char} != -1) {        ## ISSUE: "text tokens" in spec.
2928          if ($self->{next_char} == 0x005D) { # ]      } elsif ($self->{state} == CDATA_SECTION_MSE1_STATE) {
2929            !!!next-input-character;        if ($self->{next_char} == 0x005D) { # ]
2930            if ($self->{next_char} == 0x005D) { # ]          !!!cp (221.5);
2931              !!!next-input-character;          $self->{state} = CDATA_SECTION_MSE2_STATE;
2932              MDC: {          !!!next-input-character;
2933                if ($self->{next_char} == 0x003E) { # >          redo A;
2934                  !!!cp (221.1);        } else {
2935                  !!!next-input-character;          !!!cp (221.6);
2936                  last CS;          $self->{current_token}->{data} .= ']';
2937                } elsif ($self->{next_char} == 0x005D) { # ]          $self->{state} = CDATA_SECTION_STATE;
2938                  !!!cp (221.2);          ## Reconsume.
2939                  $s .= ']';          redo A;
2940                  !!!next-input-character;        }
2941                  redo MDC;      } elsif ($self->{state} == CDATA_SECTION_MSE2_STATE) {
2942                } else {        if ($self->{next_char} == 0x003E) { # >
2943                  !!!cp (221.3);          $self->{state} = DATA_STATE;
2944                  $s .= ']]';          !!!next-input-character;
2945                  #          if (length $self->{current_token}->{data}) { # character
2946                }            !!!cp (221.7);
2947              } # MDC            !!!emit ($self->{current_token}); # character
           } else {  
             !!!cp (221.4);  
             $s .= ']';  
             #  
           }  
2948          } else {          } else {
2949            !!!cp (221.5);            !!!cp (221.8);
2950            #            ## No token to emit. $self->{current_token} is discarded.
2951          }          }
2952          $s .= chr $self->{next_char};          redo A;
2953          } elsif ($self->{next_char} == 0x005D) { # ]
2954            !!!cp (221.9); # character
2955            $self->{current_token}->{data} .= ']'; ## Add first "]" of "]]]".
2956            ## Stay in the state.
2957          !!!next-input-character;          !!!next-input-character;
2958        } # CS          redo A;
   
       $self->{state} = DATA_STATE;  
       ## next-input-character done or EOF, which is reconsumed.  
   
       if (length $s) {  
         !!!cp (221.6);  
         !!!emit ({type => CHARACTER_TOKEN, data => $s,  
                   line => $l, column => $c});  
2959        } else {        } else {
2960          !!!cp (221.7);          !!!cp (221.11);
2961            $self->{current_token}->{data} .= ']]'; # character
2962            $self->{state} = CDATA_SECTION_STATE;
2963            ## Reconsume.
2964            redo A;
2965        }        }
   
       redo A;  
   
       ## ISSUE: "text tokens" in spec.  
       ## TODO: Streaming support  
2966      } else {      } else {
2967        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2968      }      }
# Line 2805  sub _tokenize_attempt_to_consume_an_enti Line 3030  sub _tokenize_attempt_to_consume_an_enti
3030    
3031          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3032            !!!cp (1008);            !!!cp (1008);
3033            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);            !!!parse-error (type => 'invalid character reference',
3034                              text => (sprintf 'U+%04X', $code),
3035                              line => $l, column => $c);
3036            $code = 0xFFFD;            $code = 0xFFFD;
3037          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
3038            !!!cp (1009);            !!!cp (1009);
3039            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);            !!!parse-error (type => 'invalid character reference',
3040                              text => (sprintf 'U-%08X', $code),
3041                              line => $l, column => $c);
3042            $code = 0xFFFD;            $code = 0xFFFD;
3043          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
3044            !!!cp (1010);            !!!cp (1010);
# Line 2817  sub _tokenize_attempt_to_consume_an_enti Line 3046  sub _tokenize_attempt_to_consume_an_enti
3046            $code = 0x000A;            $code = 0x000A;
3047          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
3048            !!!cp (1011);            !!!cp (1011);
3049            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);            !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
3050            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
3051          }          }
3052    
# Line 2850  sub _tokenize_attempt_to_consume_an_enti Line 3079  sub _tokenize_attempt_to_consume_an_enti
3079    
3080        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3081          !!!cp (1015);          !!!cp (1015);
3082          !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);          !!!parse-error (type => 'invalid character reference',
3083                            text => (sprintf 'U+%04X', $code),
3084                            line => $l, column => $c);
3085          $code = 0xFFFD;          $code = 0xFFFD;
3086        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
3087          !!!cp (1016);          !!!cp (1016);
3088          !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);          !!!parse-error (type => 'invalid character reference',
3089                            text => (sprintf 'U-%08X', $code),
3090                            line => $l, column => $c);
3091          $code = 0xFFFD;          $code = 0xFFFD;
3092        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
3093          !!!cp (1017);          !!!cp (1017);
3094          !!!parse-error (type => 'CR character reference', line => $l, column => $c);          !!!parse-error (type => 'CR character reference',
3095                            line => $l, column => $c);
3096          $code = 0x000A;          $code = 0x000A;
3097        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3098          !!!cp (1018);          !!!cp (1018);
3099          !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);          !!!parse-error (type => 'C1 character reference',
3100                            text => (sprintf 'U+%04X', $code),
3101                            line => $l, column => $c);
3102          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3103        }        }
3104                
# Line 2960  sub _initialize_tree_constructor ($) { Line 3196  sub _initialize_tree_constructor ($) {
3196    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3197    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3198    $self->{document}->manakai_is_html (1); # MUST    $self->{document}->manakai_is_html (1); # MUST
3199      $self->{document}->set_user_data (manakai_source_line => 1);
3200      $self->{document}->set_user_data (manakai_source_column => 1);
3201  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3202    
3203  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 3014  sub _tree_construction_initial ($) { Line 3252  sub _tree_construction_initial ($) {
3252        ## language.        ## language.
3253        my $doctype_name = $token->{name};        my $doctype_name = $token->{name};
3254        $doctype_name = '' unless defined $doctype_name;        $doctype_name = '' unless defined $doctype_name;
3255        $doctype_name =~ tr/a-z/A-Z/;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3256        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
           defined $token->{public_identifier} or  
3257            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3258          !!!cp ('t1');          !!!cp ('t1');
3259          !!!parse-error (type => 'not HTML5', token => $token);          !!!parse-error (type => 'not HTML5', token => $token);
3260        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3261          !!!cp ('t2');          !!!cp ('t2');
         ## ISSUE: ASCII case-insensitive? (in fact it does not matter)  
3262          !!!parse-error (type => 'not HTML5', token => $token);          !!!parse-error (type => 'not HTML5', token => $token);
3263          } elsif (defined $token->{public_identifier}) {
3264            if ($token->{public_identifier} eq 'XSLT-compat') {
3265              !!!cp ('t1.2');
3266              !!!parse-error (type => 'XSLT-compat', token => $token,
3267                              level => $self->{level}->{should});
3268            } else {
3269              !!!parse-error (type => 'not HTML5', token => $token);
3270            }
3271        } else {        } else {
3272          !!!cp ('t3');          !!!cp ('t3');
3273            #
3274        }        }
3275                
3276        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
# Line 3046  sub _tree_construction_initial ($) { Line 3291  sub _tree_construction_initial ($) {
3291        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3292          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3293          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3294          if ({          my $prefix = [
3295            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3296            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3297            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3298            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3299            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3300            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3301            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3302            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3303            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3304            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3305            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3306            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3307            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3308            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3309            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3310            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3311            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3312            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3313            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3314            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3315            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3316            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3317            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3318            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3319            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3320            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3321            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3322            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3323            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3324            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3325            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3326            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3327            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3328            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3329            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3330            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3331            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3332            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3333            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3334            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3335            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3336            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3337            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3338            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3339            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3340            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3341            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3342            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3343            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3344            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3345            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3346            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD W3 HTML//",
3347            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3348            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3349            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3350            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,          ]; # $prefix
3351            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,          my $match;
3352            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,          for (@$prefix) {
3353            "-//W3C//DTD HTML 3.2//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3354            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,              $match = 1;
3355            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,              last;
3356            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            }
3357            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,          }
3358            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,          if ($match or
3359            "-//W3C//DTD W3 HTML//EN" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3360            "-//W3O//DTD W3 HTML 3.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3361            "-//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}) {  
3362            !!!cp ('t5');            !!!cp ('t5');
3363            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3364          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3365                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3366            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3367              !!!cp ('t6');              !!!cp ('t6');
3368              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
# Line 3131  sub _tree_construction_initial ($) { Line 3370  sub _tree_construction_initial ($) {
3370              !!!cp ('t7');              !!!cp ('t7');
3371              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3372            }            }
3373          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3374                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3375            !!!cp ('t8');            !!!cp ('t8');
3376            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3377          } else {          } else {
# Line 3145  sub _tree_construction_initial ($) { Line 3384  sub _tree_construction_initial ($) {
3384          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3385          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3386          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") {
3387            ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"            ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3388              ## marked as quirks.
3389            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3390            !!!cp ('t11');            !!!cp ('t11');
3391          } else {          } else {
# Line 3317  sub _reset_insertion_mode ($) { Line 3557  sub _reset_insertion_mode ($) {
3557        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3558          $last = 1;          $last = 1;
3559          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3560            if ($self->{inner_html_node}->[1] & TABLE_CELL_EL) {            !!!cp ('t28');
3561              !!!cp ('t27');            $node = $self->{inner_html_node};
3562              #          } else {
3563            } else {            die "_reset_insertion_mode: t27";
             !!!cp ('t28');  
             $node = $self->{inner_html_node};  
           }  
3564          }          }
3565        }        }
3566              
3567      ## Step 4..14        ## Step 4..14
3568      my $new_mode;        my $new_mode;
3569      if ($node->[1] & FOREIGN_EL) {        if ($node->[1] & FOREIGN_EL) {
3570        ## NOTE: Strictly spaking, the line below only applies to MathML and          !!!cp ('t28.1');
3571        ## SVG elements.  Currently the HTML syntax supports only MathML and          ## NOTE: Strictly spaking, the line below only applies to MathML and
3572        ## SVG elements as foreigners.          ## SVG elements.  Currently the HTML syntax supports only MathML and
3573        $new_mode = $self->{insertion_mode} | IN_FOREIGN_CONTENT_IM;          ## SVG elements as foreigners.
3574        ## ISSUE: What is set as the secondary insertion mode?          $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3575      } else {        } elsif ($node->[1] & TABLE_CELL_EL) {
3576        $new_mode = {          if ($last) {
3577              !!!cp ('t28.2');
3578              #
3579            } else {
3580              !!!cp ('t28.3');
3581              $new_mode = IN_CELL_IM;
3582            }
3583          } else {
3584            !!!cp ('t28.4');
3585            $new_mode = {
3586                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3587                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3588                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
                       td => IN_CELL_IM,  
                       th => IN_CELL_IM,  
3589                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3590                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3591                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 3353  sub _reset_insertion_mode ($) { Line 3597  sub _reset_insertion_mode ($) {
3597                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3598                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3599                       }->{$node->[0]->manakai_local_name};                       }->{$node->[0]->manakai_local_name};
3600      }        }
3601      $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3602                
3603        ## Step 15        ## Step 15
3604        if ($node->[1] & HTML_EL) {        if ($node->[1] & HTML_EL) {
# Line 3528  sub _tree_construction_main ($) { Line 3772  sub _tree_construction_main ($) {
3772        ## NOTE: An end-of-file token.        ## NOTE: An end-of-file token.
3773        if ($content_model_flag == CDATA_CONTENT_MODEL) {        if ($content_model_flag == CDATA_CONTENT_MODEL) {
3774          !!!cp ('t43');          !!!cp ('t43');
3775          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);          !!!parse-error (type => 'in CDATA:#eof', token => $token);
3776        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3777          !!!cp ('t44');          !!!cp ('t44');
3778          !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);          !!!parse-error (type => 'in RCDATA:#eof', token => $token);
3779        } else {        } else {
3780          die "$0: $content_model_flag in parse_rcdata";          die "$0: $content_model_flag in parse_rcdata";
3781        }        }
# Line 3568  sub _tree_construction_main ($) { Line 3812  sub _tree_construction_main ($) {
3812        ## Ignore the token        ## Ignore the token
3813      } else {      } else {
3814        !!!cp ('t48');        !!!cp ('t48');
3815        !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);        !!!parse-error (type => 'in CDATA:#eof', token => $token);
3816        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3817        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3818      }      }
# Line 3619  sub _tree_construction_main ($) { Line 3863  sub _tree_construction_main ($) {
3863        } # AFE        } # AFE
3864        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3865          !!!cp ('t53');          !!!cp ('t53');
3866          !!!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);
3867          ## Ignore the token          ## Ignore the token
3868          !!!next-token;          !!!next-token;
3869          return;          return;
# Line 3636  sub _tree_construction_main ($) { Line 3880  sub _tree_construction_main ($) {
3880              last INSCOPE;              last INSCOPE;
3881            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3882              !!!cp ('t55');              !!!cp ('t55');
3883              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},              !!!parse-error (type => 'unmatched end tag',
3884                                text => $token->{tag_name},
3885                              token => $end_tag_token);                              token => $end_tag_token);
3886              ## Ignore the token              ## Ignore the token
3887              !!!next-token;              !!!next-token;
# Line 3649  sub _tree_construction_main ($) { Line 3894  sub _tree_construction_main ($) {
3894        } # INSCOPE        } # INSCOPE
3895        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3896          !!!cp ('t57');          !!!cp ('t57');
3897          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},          !!!parse-error (type => 'unmatched end tag',
3898                            text => $token->{tag_name},
3899                          token => $end_tag_token);                          token => $end_tag_token);
3900          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3901          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
# Line 3658  sub _tree_construction_main ($) { Line 3904  sub _tree_construction_main ($) {
3904        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3905          !!!cp ('t58');          !!!cp ('t58');
3906          !!!parse-error (type => 'not closed',          !!!parse-error (type => 'not closed',
3907                          value => $self->{open_elements}->[-1]->[0]                          text => $self->{open_elements}->[-1]->[0]
3908                              ->manakai_local_name,                              ->manakai_local_name,
3909                          token => $end_tag_token);                          token => $end_tag_token);
3910        }        }
# Line 3867  sub _tree_construction_main ($) { Line 4113  sub _tree_construction_main ($) {
4113    B: while (1) {    B: while (1) {
4114      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
4115        !!!cp ('t73');        !!!cp ('t73');
4116        !!!parse-error (type => 'DOCTYPE in the middle', token => $token);        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4117        ## Ignore the token        ## Ignore the token
4118        ## Stay in the phase        ## Stay in the phase
4119        !!!next-token;        !!!next-token;
# Line 3876  sub _tree_construction_main ($) { Line 4122  sub _tree_construction_main ($) {
4122               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
4123        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4124          !!!cp ('t79');          !!!cp ('t79');
4125          !!!parse-error (type => 'after html:html', token => $token);          !!!parse-error (type => 'after html', text => 'html', token => $token);
4126          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
4127        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4128          !!!cp ('t80');          !!!cp ('t80');
4129          !!!parse-error (type => 'after html:html', token => $token);          !!!parse-error (type => 'after html', text => 'html', token => $token);
4130          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4131        } else {        } else {
4132          !!!cp ('t81');          !!!cp ('t81');
# Line 3931  sub _tree_construction_main ($) { Line 4177  sub _tree_construction_main ($) {
4177            #            #
4178          } elsif ({          } elsif ({
4179                    b => 1, big => 1, blockquote => 1, body => 1, br => 1,                    b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4180                    center => 1, code => 1, dd => 1, div => 1, dl => 1, em => 1,                    center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4181                    embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1, ## No h4!                    em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4182                    h5 => 1, h6 => 1, head => 1, hr => 1, i => 1, img => 1,                    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4183                    li => 1, menu => 1, meta => 1, nobr => 1, p => 1, pre => 1,                    img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4184                    ruby => 1, s => 1, small => 1, span => 1, strong => 1,                    nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4185                    sub => 1, sup => 1, table => 1, tt => 1, u => 1, ul => 1,                    small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4186                    var => 1,                    sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4187                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
4188            !!!cp ('t87.2');            !!!cp ('t87.2');
4189            !!!parse-error (type => 'not closed',            !!!parse-error (type => 'not closed',
4190                            value => $self->{open_elements}->[-1]->[0]                            text => $self->{open_elements}->[-1]->[0]
4191                                ->manakai_local_name,                                ->manakai_local_name,
4192                            token => $token);                            token => $token);
4193    
# Line 4017  sub _tree_construction_main ($) { Line 4263  sub _tree_construction_main ($) {
4263          !!!cp ('t87.5');          !!!cp ('t87.5');
4264          #          #
4265        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
         ## NOTE: "using the rules for secondary insertion mode" then "continue"  
4266          !!!cp ('t87.6');          !!!cp ('t87.6');
4267          #          !!!parse-error (type => 'not closed',
4268          ## TODO: ...                          text => $self->{open_elements}->[-1]->[0]
4269                                ->manakai_local_name,
4270                            token => $token);
4271    
4272            pop @{$self->{open_elements}}
4273                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4274    
4275            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4276            ## Reprocess.
4277            next B;
4278        } else {        } else {
4279          die "$0: $token->{type}: Unknown token type";                  die "$0: $token->{type}: Unknown token type";        
4280        }        }
# Line 4061  sub _tree_construction_main ($) { Line 4315  sub _tree_construction_main ($) {
4315            !!!cp ('t90');            !!!cp ('t90');
4316            ## As if </noscript>            ## As if </noscript>
4317            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4318            !!!parse-error (type => 'in noscript:#character', token => $token);            !!!parse-error (type => 'in noscript:#text', token => $token);
4319                        
4320            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4321            ## As if </head>            ## As if </head>
# Line 4097  sub _tree_construction_main ($) { Line 4351  sub _tree_construction_main ($) {
4351              !!!next-token;              !!!next-token;
4352              next B;              next B;
4353            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4354              !!!cp ('t94');              !!!cp ('t93.2');
4355              #              !!!parse-error (type => 'after head', text => 'head',
4356                                token => $token);
4357                ## Ignore the token
4358                !!!nack ('t93.3');
4359                !!!next-token;
4360                next B;
4361            } else {            } else {
4362              !!!cp ('t95');              !!!cp ('t95');
4363              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript              !!!parse-error (type => 'in head:head',
4364                                token => $token); # or in head noscript
4365              ## Ignore the token              ## Ignore the token
4366              !!!nack ('t95.1');              !!!nack ('t95.1');
4367              !!!next-token;              !!!next-token;
# Line 4126  sub _tree_construction_main ($) { Line 4386  sub _tree_construction_main ($) {
4386                  !!!cp ('t98');                  !!!cp ('t98');
4387                  ## As if </noscript>                  ## As if </noscript>
4388                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4389                  !!!parse-error (type => 'in noscript:base', token => $token);                  !!!parse-error (type => 'in noscript', text => 'base',
4390                                    token => $token);
4391                                
4392                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4393                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 4137  sub _tree_construction_main ($) { Line 4398  sub _tree_construction_main ($) {
4398                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4399                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4400                  !!!cp ('t100');                  !!!cp ('t100');
4401                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4402                                    text => $token->{tag_name}, token => $token);
4403                  push @{$self->{open_elements}},                  push @{$self->{open_elements}},
4404                      [$self->{head_element}, $el_category->{head}];                      [$self->{head_element}, $el_category->{head}];
4405                } else {                } else {
# Line 4154  sub _tree_construction_main ($) { Line 4416  sub _tree_construction_main ($) {
4416                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4417                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4418                  !!!cp ('t102');                  !!!cp ('t102');
4419                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4420                                    text => $token->{tag_name}, token => $token);
4421                  push @{$self->{open_elements}},                  push @{$self->{open_elements}},
4422                      [$self->{head_element}, $el_category->{head}];                      [$self->{head_element}, $el_category->{head}];
4423                } else {                } else {
# Line 4171  sub _tree_construction_main ($) { Line 4434  sub _tree_construction_main ($) {
4434                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4435                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4436                  !!!cp ('t104');                  !!!cp ('t104');
4437                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4438                                    text => $token->{tag_name}, token => $token);
4439                  push @{$self->{open_elements}},                  push @{$self->{open_elements}},
4440                      [$self->{head_element}, $el_category->{head}];                      [$self->{head_element}, $el_category->{head}];
4441                } else {                } else {
# Line 4195  sub _tree_construction_main ($) { Line 4459  sub _tree_construction_main ($) {
4459                                                 ->{has_reference});                                                 ->{has_reference});
4460                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
4461                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4462                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4463                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4464                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4465                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4466                      !!!cp ('t107');                      !!!cp ('t107');
4467                      ## NOTE: Whether the encoding is supported or not is handled                      ## NOTE: Whether the encoding is supported or not is handled
4468                      ## in the {change_encoding} callback.                      ## in the {change_encoding} callback.
# Line 4240  sub _tree_construction_main ($) { Line 4504  sub _tree_construction_main ($) {
4504                  !!!cp ('t111');                  !!!cp ('t111');
4505                  ## As if </noscript>                  ## As if </noscript>
4506                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4507                  !!!parse-error (type => 'in noscript:title', token => $token);                  !!!parse-error (type => 'in noscript', text => 'title',
4508                                    token => $token);
4509                                
4510                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4511                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4512                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4513                  !!!cp ('t112');                  !!!cp ('t112');
4514                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4515                                    text => $token->{tag_name}, token => $token);
4516                  push @{$self->{open_elements}},                  push @{$self->{open_elements}},
4517                      [$self->{head_element}, $el_category->{head}];                      [$self->{head_element}, $el_category->{head}];
4518                } else {                } else {
# Line 4260  sub _tree_construction_main ($) { Line 4526  sub _tree_construction_main ($) {
4526                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4527                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4528                next B;                next B;
4529              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style' or
4530                         $token->{tag_name} eq 'noframes') {
4531                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4532                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4533                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4534                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4535                  !!!cp ('t114');                  !!!cp ('t114');
4536                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4537                                    text => $token->{tag_name}, token => $token);
4538                  push @{$self->{open_elements}},                  push @{$self->{open_elements}},
4539                      [$self->{head_element}, $el_category->{head}];                      [$self->{head_element}, $el_category->{head}];
4540                } else {                } else {
# Line 4287  sub _tree_construction_main ($) { Line 4555  sub _tree_construction_main ($) {
4555                  next B;                  next B;
4556                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4557                  !!!cp ('t117');                  !!!cp ('t117');
4558                  !!!parse-error (type => 'in noscript:noscript', token => $token);                  !!!parse-error (type => 'in noscript', text => 'noscript',
4559                                    token => $token);
4560                  ## Ignore the token                  ## Ignore the token
4561                  !!!nack ('t117.1');                  !!!nack ('t117.1');
4562                  !!!next-token;                  !!!next-token;
# Line 4301  sub _tree_construction_main ($) { Line 4570  sub _tree_construction_main ($) {
4570                  !!!cp ('t119');                  !!!cp ('t119');
4571                  ## As if </noscript>                  ## As if </noscript>
4572                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4573                  !!!parse-error (type => 'in noscript:script', token => $token);                  !!!parse-error (type => 'in noscript', text => 'script',
4574                                    token => $token);
4575                                
4576                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4577                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4578                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4579                  !!!cp ('t120');                  !!!cp ('t120');
4580                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'after head',
4581                                    text => $token->{tag_name}, token => $token);
4582                  push @{$self->{open_elements}},                  push @{$self->{open_elements}},
4583                      [$self->{head_element}, $el_category->{head}];                      [$self->{head_element}, $el_category->{head}];
4584                } else {                } else {
# Line 4325  sub _tree_construction_main ($) { Line 4596  sub _tree_construction_main ($) {
4596                  !!!cp ('t122');                  !!!cp ('t122');
4597                  ## As if </noscript>                  ## As if </noscript>
4598                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4599                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'in noscript',
4600                                    text => $token->{tag_name}, token => $token);
4601                                    
4602                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4603                  ## As if </head>                  ## As if </head>
# Line 4364  sub _tree_construction_main ($) { Line 4636  sub _tree_construction_main ($) {
4636                !!!cp ('t129');                !!!cp ('t129');
4637                ## As if </noscript>                ## As if </noscript>
4638                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4639                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'in noscript:/',
4640                                  text => $token->{tag_name}, token => $token);
4641                                
4642                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4643                ## As if </head>                ## As if </head>
# Line 4407  sub _tree_construction_main ($) { Line 4680  sub _tree_construction_main ($) {
4680                  !!!cp ('t133');                  !!!cp ('t133');
4681                  ## As if </noscript>                  ## As if </noscript>
4682                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4683                  !!!parse-error (type => 'in noscript:/head', token => $token);                  !!!parse-error (type => 'in noscript:/',
4684                                    text => 'head', token => $token);
4685                                    
4686                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4687                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4420  sub _tree_construction_main ($) { Line 4694  sub _tree_construction_main ($) {
4694                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4695                  !!!next-token;                  !!!next-token;
4696                  next B;                  next B;
4697                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4698                    !!!cp ('t134.1');
4699                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4700                                    token => $token);
4701                    ## Ignore the token
4702                    !!!next-token;
4703                    next B;
4704                } else {                } else {
4705                  !!!cp ('t135');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 #  
4706                }                }
4707              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4708                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 4431  sub _tree_construction_main ($) { Line 4711  sub _tree_construction_main ($) {
4711                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4712                  !!!next-token;                  !!!next-token;
4713                  next B;                  next B;
4714                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4715                           $self->{insertion_mode} == AFTER_HEAD_IM) {
4716                  !!!cp ('t137');                  !!!cp ('t137');
4717                  !!!parse-error (type => 'unmatched end tag:noscript', token => $token);                  !!!parse-error (type => 'unmatched end tag',
4718                                    text => 'noscript', token => $token);
4719                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4720                  !!!next-token;                  !!!next-token;
4721                  next B;                  next B;
# Line 4444  sub _tree_construction_main ($) { Line 4726  sub _tree_construction_main ($) {
4726              } elsif ({              } elsif ({
4727                        body => 1, html => 1,                        body => 1, html => 1,
4728                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4729                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4730                  !!!cp ('t139');                    $self->{insertion_mode} == IN_HEAD_IM or
4731                  ## 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) {  
4732                  !!!cp ('t140');                  !!!cp ('t140');
4733                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
4734                                    text => $token->{tag_name}, token => $token);
4735                    ## Ignore the token
4736                    !!!next-token;
4737                    next B;
4738                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4739                    !!!cp ('t140.1');
4740                    !!!parse-error (type => 'unmatched end tag',
4741                                    text => $token->{tag_name}, token => $token);
4742                  ## Ignore the token                  ## Ignore the token
4743                  !!!next-token;                  !!!next-token;
4744                  next B;                  next B;
4745                } else {                } else {
4746                  !!!cp ('t141');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4747                }                }
4748                              } elsif ($token->{tag_name} eq 'p') {
4749                #                !!!cp ('t142');
4750              } elsif ({                !!!parse-error (type => 'unmatched end tag',
4751                        p => 1, br => 1,                                text => $token->{tag_name}, token => $token);
4752                       }->{$token->{tag_name}}) {                ## Ignore the token
4753                  !!!next-token;
4754                  next B;
4755                } elsif ($token->{tag_name} eq 'br') {
4756                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4757                  !!!cp ('t142');                  !!!cp ('t142.2');
4758                  ## As if <head>                  ## (before head) as if <head>, (in head) as if </head>
4759                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4760                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4761                  push @{$self->{open_elements}},                  $self->{insertion_mode} = AFTER_HEAD_IM;
4762                      [$self->{head_element}, $el_category->{head}];    
4763                    ## Reprocess in the "after head" insertion mode...
4764                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4765                    !!!cp ('t143.2');
4766                    ## As if </head>
4767                    pop @{$self->{open_elements}};
4768                    $self->{insertion_mode} = AFTER_HEAD_IM;
4769      
4770                    ## Reprocess in the "after head" insertion mode...
4771                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4772                    !!!cp ('t143.3');
4773                    ## ISSUE: Two parse errors for <head><noscript></br>
4774                    !!!parse-error (type => 'unmatched end tag',
4775                                    text => 'br', token => $token);
4776                    ## As if </noscript>
4777                    pop @{$self->{open_elements}};
4778                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4779    
4780                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4781                } else {                  ## As if </head>
4782                  !!!cp ('t143');                  pop @{$self->{open_elements}};
4783                }                  $self->{insertion_mode} = AFTER_HEAD_IM;
4784    
4785                #                  ## Reprocess in the "after head" insertion mode...
4786              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4787                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
                 !!!cp ('t144');  
4788                  #                  #
4789                } else {                } else {
4790                  !!!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;  
4791                }                }
4792    
4793                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4794                  !!!parse-error (type => 'unmatched end tag',
4795                                  text => 'br', token => $token);
4796                  ## Ignore the token
4797                  !!!next-token;
4798                  next B;
4799                } else {
4800                  !!!cp ('t145');
4801                  !!!parse-error (type => 'unmatched end tag',
4802                                  text => $token->{tag_name}, token => $token);
4803                  ## Ignore the token
4804                  !!!next-token;
4805                  next B;
4806              }              }
4807    
4808              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4809                !!!cp ('t146');                !!!cp ('t146');
4810                ## As if </noscript>                ## As if </noscript>
4811                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4812                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'in noscript:/',
4813                                  text => $token->{tag_name}, token => $token);
4814                                
4815                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4816                ## As if </head>                ## As if </head>
# Line 4516  sub _tree_construction_main ($) { Line 4826  sub _tree_construction_main ($) {
4826              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4827  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
4828                !!!cp ('t148');                !!!cp ('t148');
4829                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
4830                                  text => $token->{tag_name}, token => $token);
4831                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4832                !!!next-token;                !!!next-token;
4833                next B;                next B;
# Line 4627  sub _tree_construction_main ($) { Line 4938  sub _tree_construction_main ($) {
4938    
4939                  !!!cp ('t153');                  !!!cp ('t153');
4940                  !!!parse-error (type => 'start tag not allowed',                  !!!parse-error (type => 'start tag not allowed',
4941                      value => $token->{tag_name}, token => $token);                      text => $token->{tag_name}, token => $token);
4942                  ## Ignore the token                  ## Ignore the token
4943                  !!!nack ('t153.1');                  !!!nack ('t153.1');
4944                  !!!next-token;                  !!!next-token;
4945                  next B;                  next B;
4946                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4947                  !!!parse-error (type => 'not closed:caption', token => $token);                  !!!parse-error (type => 'not closed', text => 'caption',
4948                                    token => $token);
4949                                    
4950                  ## NOTE: As if </caption>.                  ## NOTE: As if </caption>.
4951                  ## have a table element in table scope                  ## have a table element in table scope
# Line 4653  sub _tree_construction_main ($) { Line 4965  sub _tree_construction_main ($) {
4965    
4966                    !!!cp ('t157');                    !!!cp ('t157');
4967                    !!!parse-error (type => 'start tag not allowed',                    !!!parse-error (type => 'start tag not allowed',
4968                                    value => $token->{tag_name}, token => $token);                                    text => $token->{tag_name}, token => $token);
4969                    ## Ignore the token                    ## Ignore the token
4970                    !!!nack ('t157.1');                    !!!nack ('t157.1');
4971                    !!!next-token;                    !!!next-token;
# Line 4670  sub _tree_construction_main ($) { Line 4982  sub _tree_construction_main ($) {
4982                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4983                    !!!cp ('t159');                    !!!cp ('t159');
4984                    !!!parse-error (type => 'not closed',                    !!!parse-error (type => 'not closed',
4985                                    value => $self->{open_elements}->[-1]->[0]                                    text => $self->{open_elements}->[-1]->[0]
4986                                        ->manakai_local_name,                                        ->manakai_local_name,
4987                                    token => $token);                                    token => $token);
4988                  } else {                  } else {
# Line 4712  sub _tree_construction_main ($) { Line 5024  sub _tree_construction_main ($) {
5024                  } # INSCOPE                  } # INSCOPE
5025                    unless (defined $i) {                    unless (defined $i) {
5026                      !!!cp ('t165');                      !!!cp ('t165');
5027                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag',
5028                                        text => $token->{tag_name},
5029                                        token => $token);
5030                      ## Ignore the token                      ## Ignore the token
5031                      !!!next-token;                      !!!next-token;
5032                      next B;                      next B;
# Line 4729  sub _tree_construction_main ($) { Line 5043  sub _tree_construction_main ($) {
5043                          ne $token->{tag_name}) {                          ne $token->{tag_name}) {
5044                    !!!cp ('t167');                    !!!cp ('t167');
5045                    !!!parse-error (type => 'not closed',                    !!!parse-error (type => 'not closed',
5046                                    value => $self->{open_elements}->[-1]->[0]                                    text => $self->{open_elements}->[-1]->[0]
5047                                        ->manakai_local_name,                                        ->manakai_local_name,
5048                                    token => $token);                                    token => $token);
5049                  } else {                  } else {
# Line 4746  sub _tree_construction_main ($) { Line 5060  sub _tree_construction_main ($) {
5060                  next B;                  next B;
5061                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5062                  !!!cp ('t169');                  !!!cp ('t169');
5063                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5064                                    text => $token->{tag_name}, token => $token);
5065                  ## Ignore the token                  ## Ignore the token
5066                  !!!next-token;                  !!!next-token;
5067                  next B;                  next B;
# Line 4773  sub _tree_construction_main ($) { Line 5088  sub _tree_construction_main ($) {
5088    
5089                    !!!cp ('t173');                    !!!cp ('t173');
5090                    !!!parse-error (type => 'unmatched end tag',                    !!!parse-error (type => 'unmatched end tag',
5091                                    value => $token->{tag_name}, token => $token);                                    text => $token->{tag_name}, token => $token);
5092                    ## Ignore the token                    ## Ignore the token
5093                    !!!next-token;                    !!!next-token;
5094                    next B;                    next B;
# Line 4789  sub _tree_construction_main ($) { Line 5104  sub _tree_construction_main ($) {
5104                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5105                    !!!cp ('t175');                    !!!cp ('t175');
5106                    !!!parse-error (type => 'not closed',                    !!!parse-error (type => 'not closed',
5107                                    value => $self->{open_elements}->[-1]->[0]                                    text => $self->{open_elements}->[-1]->[0]
5108                                        ->manakai_local_name,                                        ->manakai_local_name,
5109                                    token => $token);                                    token => $token);
5110                  } else {                  } else {
# Line 4806  sub _tree_construction_main ($) { Line 5121  sub _tree_construction_main ($) {
5121                  next B;                  next B;
5122                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5123                  !!!cp ('t177');                  !!!cp ('t177');
5124                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5125                                    text => $token->{tag_name}, token => $token);
5126                  ## Ignore the token                  ## Ignore the token
5127                  !!!next-token;                  !!!next-token;
5128                  next B;                  next B;
# Line 4849  sub _tree_construction_main ($) { Line 5165  sub _tree_construction_main ($) {
5165    
5166                  !!!cp ('t182');                  !!!cp ('t182');
5167                  !!!parse-error (type => 'unmatched end tag',                  !!!parse-error (type => 'unmatched end tag',
5168                      value => $token->{tag_name}, token => $token);                      text => $token->{tag_name}, token => $token);
5169                  ## Ignore the token                  ## Ignore the token
5170                  !!!next-token;                  !!!next-token;
5171                  next B;                  next B;
5172                } # INSCOPE                } # INSCOPE
5173              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5174                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5175                !!!parse-error (type => 'not closed:caption', token => $token);                !!!parse-error (type => 'not closed', text => 'caption',
5176                                  token => $token);
5177    
5178                ## As if </caption>                ## As if </caption>
5179                ## have a table element in table scope                ## have a table element in table scope
# Line 4874  sub _tree_construction_main ($) { Line 5191  sub _tree_construction_main ($) {
5191                } # INSCOPE                } # INSCOPE
5192                unless (defined $i) {                unless (defined $i) {
5193                  !!!cp ('t186');                  !!!cp ('t186');
5194                  !!!parse-error (type => 'unmatched end tag:caption', token => $token);                  !!!parse-error (type => 'unmatched end tag',
5195                                    text => 'caption', token => $token);
5196                  ## Ignore the token                  ## Ignore the token
5197                  !!!next-token;                  !!!next-token;
5198                  next B;                  next B;
# Line 4889  sub _tree_construction_main ($) { Line 5207  sub _tree_construction_main ($) {
5207                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5208                  !!!cp ('t188');                  !!!cp ('t188');
5209                  !!!parse-error (type => 'not closed',                  !!!parse-error (type => 'not closed',
5210                                  value => $self->{open_elements}->[-1]->[0]                                  text => $self->{open_elements}->[-1]->[0]
5211                                      ->manakai_local_name,                                      ->manakai_local_name,
5212                                  token => $token);                                  token => $token);
5213                } else {                } else {
# Line 4909  sub _tree_construction_main ($) { Line 5227  sub _tree_construction_main ($) {
5227                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5228                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5229                  !!!cp ('t190');                  !!!cp ('t190');
5230                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5231                                    text => $token->{tag_name}, token => $token);
5232                  ## Ignore the token                  ## Ignore the token
5233                  !!!next-token;                  !!!next-token;
5234                  next B;                  next B;
# Line 4923  sub _tree_construction_main ($) { Line 5242  sub _tree_construction_main ($) {
5242                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5243                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5244                !!!cp ('t192');                !!!cp ('t192');
5245                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
5246                                  text => $token->{tag_name}, token => $token);
5247                ## Ignore the token                ## Ignore the token
5248                !!!next-token;                !!!next-token;
5249                next B;                next B;
# Line 4963  sub _tree_construction_main ($) { Line 5283  sub _tree_construction_main ($) {
5283            }            }
5284          }          }
5285    
5286              !!!parse-error (type => 'in table:#character', token => $token);          !!!parse-error (type => 'in table:#text', token => $token);
5287    
5288              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5289              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 5014  sub _tree_construction_main ($) { Line 5334  sub _tree_construction_main ($) {
5334          !!!next-token;          !!!next-token;
5335          next B;          next B;
5336        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5337              if ({          if ({
5338                   tr => ($self->{insertion_mode} != IN_ROW_IM),               tr => ($self->{insertion_mode} != IN_ROW_IM),
5339                   th => 1, td => 1,               th => 1, td => 1,
5340                  }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
5341                if ($self->{insertion_mode} == IN_TABLE_IM) {            if ($self->{insertion_mode} == IN_TABLE_IM) {
5342                  ## Clear back to table context              ## Clear back to table context
5343                  while (not ($self->{open_elements}->[-1]->[1]              while (not ($self->{open_elements}->[-1]->[1]
5344                                  & TABLE_SCOPING_EL)) {                              & TABLE_SCOPING_EL)) {
5345                    !!!cp ('t201');                !!!cp ('t201');
5346                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5347                  }              }
5348                                
5349                  !!!insert-element ('tbody',, $token);              !!!insert-element ('tbody',, $token);
5350                  $self->{insertion_mode} = IN_TABLE_BODY_IM;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5351                  ## reprocess in the "in table body" insertion mode...              ## reprocess in the "in table body" insertion mode...
5352                }            }
5353              
5354                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5355                  unless ($token->{tag_name} eq 'tr') {              unless ($token->{tag_name} eq 'tr') {
5356                    !!!cp ('t202');                !!!cp ('t202');
5357                    !!!parse-error (type => 'missing start tag:tr', token => $token);                !!!parse-error (type => 'missing start tag:tr', token => $token);
5358                  }              }
5359                                    
5360                  ## Clear back to table body context              ## Clear back to table body context
5361                  while (not ($self->{open_elements}->[-1]->[1]              while (not ($self->{open_elements}->[-1]->[1]
5362                                  & TABLE_ROWS_SCOPING_EL)) {                              & TABLE_ROWS_SCOPING_EL)) {
5363                    !!!cp ('t203');                !!!cp ('t203');
5364                    ## ISSUE: Can this case be reached?                ## ISSUE: Can this case be reached?
5365                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5366                  }              }
5367                                    
5368                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5369                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
# Line 5099  sub _tree_construction_main ($) { Line 5419  sub _tree_construction_main ($) {
5419                  unless (defined $i) {                  unless (defined $i) {
5420                    !!!cp ('t210');                    !!!cp ('t210');
5421  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5422                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmacthed end tag',
5423                                      text => $token->{tag_name}, token => $token);
5424                    ## Ignore the token                    ## Ignore the token
5425                    !!!nack ('t210.1');                    !!!nack ('t210.1');
5426                    !!!next-token;                    !!!next-token;
# Line 5143  sub _tree_construction_main ($) { Line 5464  sub _tree_construction_main ($) {
5464                  } # INSCOPE                  } # INSCOPE
5465                  unless (defined $i) {                  unless (defined $i) {
5466                    !!!cp ('t216');                    !!!cp ('t216');
5467  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type is wrong.
5468                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5469                                      text => $token->{tag_name}, token => $token);
5470                    ## Ignore the token                    ## Ignore the token
5471                    !!!nack ('t216.1');                    !!!nack ('t216.1');
5472                    !!!next-token;                    !!!next-token;
# Line 5219  sub _tree_construction_main ($) { Line 5541  sub _tree_construction_main ($) {
5541                }                }
5542              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5543                !!!parse-error (type => 'not closed',                !!!parse-error (type => 'not closed',
5544                                value => $self->{open_elements}->[-1]->[0]                                text => $self->{open_elements}->[-1]->[0]
5545                                    ->manakai_local_name,                                    ->manakai_local_name,
5546                                token => $token);                                token => $token);
5547    
# Line 5240  sub _tree_construction_main ($) { Line 5562  sub _tree_construction_main ($) {
5562                unless (defined $i) {                unless (defined $i) {
5563                  !!!cp ('t223');                  !!!cp ('t223');
5564  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5565                  !!!parse-error (type => 'unmatched end tag:table', token => $token);                  !!!parse-error (type => 'unmatched end tag', text => 'table',
5566                                    token => $token);
5567                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5568                  !!!nack ('t223.1');                  !!!nack ('t223.1');
5569                  !!!next-token;                  !!!next-token;
5570                  next B;                  next B;
5571                }                }
5572                                
5573  ## TODO: Followings are removed from the latest spec.  ## TODO: Followings are removed from the latest spec.
5574                ## generate implied end tags                ## generate implied end tags
5575                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5576                  !!!cp ('t224');                  !!!cp ('t224');
# Line 5258  sub _tree_construction_main ($) { Line 5581  sub _tree_construction_main ($) {
5581                  !!!cp ('t225');                  !!!cp ('t225');
5582                  ## NOTE: |<table><tr><table>|                  ## NOTE: |<table><tr><table>|
5583                  !!!parse-error (type => 'not closed',                  !!!parse-error (type => 'not closed',
5584                                  value => $self->{open_elements}->[-1]->[0]                                  text => $self->{open_elements}->[-1]->[0]
5585                                      ->manakai_local_name,                                      ->manakai_local_name,
5586                                  token => $token);                                  token => $token);
5587                } else {                } else {
# Line 5299  sub _tree_construction_main ($) { Line 5622  sub _tree_construction_main ($) {
5622                my $type = lc $token->{attributes}->{type}->{value};                my $type = lc $token->{attributes}->{type}->{value};
5623                if ($type eq 'hidden') {                if ($type eq 'hidden') {
5624                  !!!cp ('t227.3');                  !!!cp ('t227.3');
5625                  !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'in table',
5626                                    text => $token->{tag_name}, token => $token);
5627    
5628                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5629    
# Line 5327  sub _tree_construction_main ($) { Line 5651  sub _tree_construction_main ($) {
5651            #            #
5652          }          }
5653    
5654          !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'in table', text => $token->{tag_name},
5655                            token => $token);
5656    
5657          $insert = $insert_to_foster;          $insert = $insert_to_foster;
5658          #          #
# Line 5349  sub _tree_construction_main ($) { Line 5674  sub _tree_construction_main ($) {
5674                } # INSCOPE                } # INSCOPE
5675                unless (defined $i) {                unless (defined $i) {
5676                  !!!cp ('t230');                  !!!cp ('t230');
5677                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5678                                    text => $token->{tag_name}, token => $token);
5679                  ## Ignore the token                  ## Ignore the token
5680                  !!!nack ('t230.1');                  !!!nack ('t230.1');
5681                  !!!next-token;                  !!!next-token;
# Line 5390  sub _tree_construction_main ($) { Line 5716  sub _tree_construction_main ($) {
5716                  unless (defined $i) {                  unless (defined $i) {
5717                    !!!cp ('t235');                    !!!cp ('t235');
5718  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5719                    !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5720                                      text => $token->{type}, token => $token);
5721                    ## Ignore the token                    ## Ignore the token
5722                    !!!nack ('t236.1');                    !!!nack ('t236.1');
5723                    !!!next-token;                    !!!next-token;
# Line 5426  sub _tree_construction_main ($) { Line 5753  sub _tree_construction_main ($) {
5753                  } # INSCOPE                  } # INSCOPE
5754                  unless (defined $i) {                  unless (defined $i) {
5755                    !!!cp ('t239');                    !!!cp ('t239');
5756                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                    !!!parse-error (type => 'unmatched end tag',
5757                                      text => $token->{tag_name}, token => $token);
5758                    ## Ignore the token                    ## Ignore the token
5759                    !!!nack ('t239.1');                    !!!nack ('t239.1');
5760                    !!!next-token;                    !!!next-token;
# Line 5472  sub _tree_construction_main ($) { Line 5800  sub _tree_construction_main ($) {
5800                } # INSCOPE                } # INSCOPE
5801                unless (defined $i) {                unless (defined $i) {
5802                  !!!cp ('t243');                  !!!cp ('t243');
5803                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5804                                    text => $token->{tag_name}, token => $token);
5805                  ## Ignore the token                  ## Ignore the token
5806                  !!!nack ('t243.1');                  !!!nack ('t243.1');
5807                  !!!next-token;                  !!!next-token;
# Line 5506  sub _tree_construction_main ($) { Line 5835  sub _tree_construction_main ($) {
5835                  } # INSCOPE                  } # INSCOPE
5836                    unless (defined $i) {                    unless (defined $i) {
5837                      !!!cp ('t249');                      !!!cp ('t249');
5838                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                      !!!parse-error (type => 'unmatched end tag',
5839                                        text => $token->{tag_name}, token => $token);
5840                      ## Ignore the token                      ## Ignore the token
5841                      !!!nack ('t249.1');                      !!!nack ('t249.1');
5842                      !!!next-token;                      !!!next-token;
# Line 5529  sub _tree_construction_main ($) { Line 5859  sub _tree_construction_main ($) {
5859                  } # INSCOPE                  } # INSCOPE
5860                    unless (defined $i) {                    unless (defined $i) {
5861                      !!!cp ('t252');                      !!!cp ('t252');
5862                      !!!parse-error (type => 'unmatched end tag:tr', token => $token);                      !!!parse-error (type => 'unmatched end tag',
5863                                        text => 'tr', token => $token);
5864                      ## Ignore the token                      ## Ignore the token
5865                      !!!nack ('t252.1');                      !!!nack ('t252.1');
5866                      !!!next-token;                      !!!next-token;
# Line 5564  sub _tree_construction_main ($) { Line 5895  sub _tree_construction_main ($) {
5895                } # INSCOPE                } # INSCOPE
5896                unless (defined $i) {                unless (defined $i) {
5897                  !!!cp ('t256');                  !!!cp ('t256');
5898                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                  !!!parse-error (type => 'unmatched end tag',
5899                                    text => $token->{tag_name}, token => $token);
5900                  ## Ignore the token                  ## Ignore the token
5901                  !!!nack ('t256.1');                  !!!nack ('t256.1');
5902                  !!!next-token;                  !!!next-token;
# Line 5591  sub _tree_construction_main ($) { Line 5923  sub _tree_construction_main ($) {
5923                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5924                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5925            !!!cp ('t258');            !!!cp ('t258');
5926            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
5927                              text => $token->{tag_name}, token => $token);
5928            ## Ignore the token            ## Ignore the token
5929            !!!nack ('t258.1');            !!!nack ('t258.1');
5930             !!!next-token;             !!!next-token;
5931            next B;            next B;
5932          } else {          } else {
5933            !!!cp ('t259');            !!!cp ('t259');
5934            !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in table:/',
5935                              text => $token->{tag_name}, token => $token);
5936    
5937            $insert = $insert_to_foster;            $insert = $insert_to_foster;
5938            #            #
# Line 5648  sub _tree_construction_main ($) { Line 5982  sub _tree_construction_main ($) {
5982              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5983                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5984                  !!!cp ('t264');                  !!!cp ('t264');
5985                  !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);                  !!!parse-error (type => 'unmatched end tag',
5986                                    text => 'colgroup', token => $token);
5987                  ## Ignore the token                  ## Ignore the token
5988                  !!!next-token;                  !!!next-token;
5989                  next B;                  next B;
# Line 5661  sub _tree_construction_main ($) { Line 5996  sub _tree_construction_main ($) {
5996                }                }
5997              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5998                !!!cp ('t266');                !!!cp ('t266');
5999                !!!parse-error (type => 'unmatched end tag:col', token => $token);                !!!parse-error (type => 'unmatched end tag',
6000                                  text => 'col', token => $token);
6001                ## Ignore the token                ## Ignore the token
6002                !!!next-token;                !!!next-token;
6003                next B;                next B;
# Line 5691  sub _tree_construction_main ($) { Line 6027  sub _tree_construction_main ($) {
6027            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6028              !!!cp ('t269');              !!!cp ('t269');
6029  ## TODO: Wrong error type?  ## TODO: Wrong error type?
6030              !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);              !!!parse-error (type => 'unmatched end tag',
6031                                text => 'colgroup', token => $token);
6032              ## Ignore the token              ## Ignore the token
6033              !!!nack ('t269.1');              !!!nack ('t269.1');
6034              !!!next-token;              !!!next-token;
# Line 5745  sub _tree_construction_main ($) { Line 6082  sub _tree_construction_main ($) {
6082            !!!nack ('t277.1');            !!!nack ('t277.1');
6083            !!!next-token;            !!!next-token;
6084            next B;            next B;
6085          } elsif ($token->{tag_name} eq 'select' or          } elsif ({
6086                   $token->{tag_name} eq 'input' or                     select => 1, input => 1, textarea => 1,
6087                     }->{$token->{tag_name}} or
6088                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6089                    {                    {
6090                     caption => 1, table => 1,                     caption => 1, table => 1,
# Line 5754  sub _tree_construction_main ($) { Line 6092  sub _tree_construction_main ($) {
6092                     tr => 1, td => 1, th => 1,                     tr => 1, td => 1, th => 1,
6093                    }->{$token->{tag_name}})) {                    }->{$token->{tag_name}})) {
6094            ## TODO: The type below is not good - <select> is replaced by </select>            ## TODO: The type below is not good - <select> is replaced by </select>
6095            !!!parse-error (type => 'not closed:select', token => $token);            !!!parse-error (type => 'not closed', text => 'select',
6096                              token => $token);
6097            ## NOTE: As if the token were </select> (<select> case) or            ## NOTE: As if the token were </select> (<select> case) or
6098            ## as if there were </select> (otherwise).            ## as if there were </select> (otherwise).
6099            ## have an element in table scope            ## have an element in table scope
# Line 5772  sub _tree_construction_main ($) { Line 6111  sub _tree_construction_main ($) {
6111            } # INSCOPE            } # INSCOPE
6112            unless (defined $i) {            unless (defined $i) {
6113              !!!cp ('t280');              !!!cp ('t280');
6114              !!!parse-error (type => 'unmatched end tag:select', token => $token);              !!!parse-error (type => 'unmatched end tag',
6115                                text => 'select', token => $token);
6116              ## Ignore the token              ## Ignore the token
6117              !!!nack ('t280.1');              !!!nack ('t280.1');
6118              !!!next-token;              !!!next-token;
# Line 5796  sub _tree_construction_main ($) { Line 6136  sub _tree_construction_main ($) {
6136            }            }
6137          } else {          } else {
6138            !!!cp ('t282');            !!!cp ('t282');
6139            !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select',
6140                              text => $token->{tag_name}, token => $token);
6141            ## Ignore the token            ## Ignore the token
6142            !!!nack ('t282.1');            !!!nack ('t282.1');
6143            !!!next-token;            !!!next-token;
# Line 5814  sub _tree_construction_main ($) { Line 6155  sub _tree_construction_main ($) {
6155              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6156            } else {            } else {
6157              !!!cp ('t285');              !!!cp ('t285');
6158              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6159                                text => $token->{tag_name}, token => $token);
6160              ## Ignore the token              ## Ignore the token
6161            }            }
6162            !!!nack ('t285.1');            !!!nack ('t285.1');
# Line 5826  sub _tree_construction_main ($) { Line 6168  sub _tree_construction_main ($) {
6168              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6169            } else {            } else {
6170              !!!cp ('t287');              !!!cp ('t287');
6171              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6172                                text => $token->{tag_name}, token => $token);
6173              ## Ignore the token              ## Ignore the token
6174            }            }
6175            !!!nack ('t287.1');            !!!nack ('t287.1');
# Line 5848  sub _tree_construction_main ($) { Line 6191  sub _tree_construction_main ($) {
6191            } # INSCOPE            } # INSCOPE
6192            unless (defined $i) {            unless (defined $i) {
6193              !!!cp ('t290');              !!!cp ('t290');
6194              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6195                                text => $token->{tag_name}, token => $token);
6196              ## Ignore the token              ## Ignore the token
6197              !!!nack ('t290.1');              !!!nack ('t290.1');
6198              !!!next-token;              !!!next-token;
# Line 5869  sub _tree_construction_main ($) { Line 6213  sub _tree_construction_main ($) {
6213                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6214                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
6215  ## TODO: The following is wrong?  ## TODO: The following is wrong?
6216            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
6217                              text => $token->{tag_name}, token => $token);
6218                                
6219            ## have an element in table scope            ## have an element in table scope
6220            my $i;            my $i;
# Line 5910  sub _tree_construction_main ($) { Line 6255  sub _tree_construction_main ($) {
6255            unless (defined $i) {            unless (defined $i) {
6256              !!!cp ('t297');              !!!cp ('t297');
6257  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
6258              !!!parse-error (type => 'unmatched end tag:select', token => $token);              !!!parse-error (type => 'unmatched end tag',
6259                                text => 'select', token => $token);
6260              ## Ignore the </select> token              ## Ignore the </select> token
6261              !!!nack ('t297.1');              !!!nack ('t297.1');
6262              !!!next-token; ## TODO: ok?              !!!next-token; ## TODO: ok?
# Line 5927  sub _tree_construction_main ($) { Line 6273  sub _tree_construction_main ($) {
6273            next B;            next B;
6274          } else {          } else {
6275            !!!cp ('t299');            !!!cp ('t299');
6276            !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'in select:/',
6277                              text => $token->{tag_name}, token => $token);
6278            ## Ignore the token            ## Ignore the token
6279            !!!nack ('t299.3');            !!!nack ('t299.3');
6280            !!!next-token;            !!!next-token;
# Line 5965  sub _tree_construction_main ($) { Line 6312  sub _tree_construction_main ($) {
6312                    
6313          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6314            !!!cp ('t301');            !!!cp ('t301');
6315            !!!parse-error (type => 'after html:#character', token => $token);            !!!parse-error (type => 'after html:#text', token => $token);
6316    
6317            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6318          } else {          } else {
# Line 5973  sub _tree_construction_main ($) { Line 6320  sub _tree_construction_main ($) {
6320          }          }
6321                    
6322          ## "after body" insertion mode          ## "after body" insertion mode
6323          !!!parse-error (type => 'after body:#character', token => $token);          !!!parse-error (type => 'after body:#text', token => $token);
6324    
6325          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6326          ## reprocess          ## reprocess
# Line 5981  sub _tree_construction_main ($) { Line 6328  sub _tree_construction_main ($) {
6328        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6329          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6330            !!!cp ('t303');            !!!cp ('t303');
6331            !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after html',
6332                              text => $token->{tag_name}, token => $token);
6333                        
6334            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6335          } else {          } else {
# Line 5989  sub _tree_construction_main ($) { Line 6337  sub _tree_construction_main ($) {
6337          }          }
6338    
6339          ## "after body" insertion mode          ## "after body" insertion mode
6340          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'after body',
6341                            text => $token->{tag_name}, token => $token);
6342    
6343          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6344          !!!ack-later;          !!!ack-later;
# Line 5998  sub _tree_construction_main ($) { Line 6347  sub _tree_construction_main ($) {
6347        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6348          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6349            !!!cp ('t305');            !!!cp ('t305');
6350            !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after html:/',
6351                              text => $token->{tag_name}, token => $token);
6352                        
6353            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6354            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
# Line 6010  sub _tree_construction_main ($) { Line 6360  sub _tree_construction_main ($) {
6360          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6361            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6362              !!!cp ('t307');              !!!cp ('t307');
6363              !!!parse-error (type => 'unmatched end tag:html', token => $token);              !!!parse-error (type => 'unmatched end tag',
6364                                text => 'html', token => $token);
6365              ## Ignore the token              ## Ignore the token
6366              !!!next-token;              !!!next-token;
6367              next B;              next B;
# Line 6022  sub _tree_construction_main ($) { Line 6373  sub _tree_construction_main ($) {
6373            }            }
6374          } else {          } else {
6375            !!!cp ('t309');            !!!cp ('t309');
6376            !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'after body:/',
6377                              text => $token->{tag_name}, token => $token);
6378    
6379            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6380            ## reprocess            ## reprocess
# Line 6050  sub _tree_construction_main ($) { Line 6402  sub _tree_construction_main ($) {
6402          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6403            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6404              !!!cp ('t311');              !!!cp ('t311');
6405              !!!parse-error (type => 'in frameset:#character', token => $token);              !!!parse-error (type => 'in frameset:#text', token => $token);
6406            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6407              !!!cp ('t312');              !!!cp ('t312');
6408              !!!parse-error (type => 'after frameset:#character', token => $token);              !!!parse-error (type => 'after frameset:#text', token => $token);
6409            } else { # "after html frameset"            } else { # "after after frameset"
6410              !!!cp ('t313');              !!!cp ('t313');
6411              !!!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);  
6412            }            }
6413                        
6414            ## Ignore the token.            ## Ignore the token.
# Line 6076  sub _tree_construction_main ($) { Line 6424  sub _tree_construction_main ($) {
6424                    
6425          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6426        } 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');  
         }  
   
6427          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6428              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6429            !!!cp ('t318');            !!!cp ('t318');
# Line 6103  sub _tree_construction_main ($) { Line 6441  sub _tree_construction_main ($) {
6441            next B;            next B;
6442          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6443            !!!cp ('t320');            !!!cp ('t320');
6444            ## NOTE: As if in body.            ## NOTE: As if in head.
6445            $parse_rcdata->(CDATA_CONTENT_MODEL);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6446            next B;            next B;
6447    
6448              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6449              ## has no parse error.
6450          } else {          } else {
6451            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6452              !!!cp ('t321');              !!!cp ('t321');
6453              !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'in frameset',
6454            } else {                              text => $token->{tag_name}, token => $token);
6455              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6456              !!!cp ('t322');              !!!cp ('t322');
6457              !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'after frameset',
6458                                text => $token->{tag_name}, token => $token);
6459              } else { # "after after frameset"
6460                !!!cp ('t322.2');
6461                !!!parse-error (type => 'after after frameset',
6462                                text => $token->{tag_name}, token => $token);
6463            }            }
6464            ## Ignore the token            ## Ignore the token
6465            !!!nack ('t322.1');            !!!nack ('t322.1');
# Line 6120  sub _tree_construction_main ($) { Line 6467  sub _tree_construction_main ($) {
6467            next B;            next B;
6468          }          }
6469        } 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');  
         }  
   
6470          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6471              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6472            if ($self->{open_elements}->[-1]->[1] & HTML_EL and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6473                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6474              !!!cp ('t325');              !!!cp ('t325');
6475              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
6476                                text => $token->{tag_name}, token => $token);
6477              ## Ignore the token              ## Ignore the token
6478              !!!next-token;              !!!next-token;
6479            } else {            } else {
# Line 6161  sub _tree_construction_main ($) { Line 6499  sub _tree_construction_main ($) {
6499          } else {          } else {
6500            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6501              !!!cp ('t330');              !!!cp ('t330');
6502              !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'in frameset:/',
6503            } else {                              text => $token->{tag_name}, token => $token);
6504              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6505                !!!cp ('t330.1');
6506                !!!parse-error (type => 'after frameset:/',
6507                                text => $token->{tag_name}, token => $token);
6508              } else { # "after after html"
6509              !!!cp ('t331');              !!!cp ('t331');
6510              !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'after after frameset:/',
6511                                text => $token->{tag_name}, token => $token);
6512            }            }
6513            ## Ignore the token            ## Ignore the token
6514            !!!next-token;            !!!next-token;
# Line 6231  sub _tree_construction_main ($) { Line 6575  sub _tree_construction_main ($) {
6575                                           ->{has_reference});                                           ->{has_reference});
6576            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
6577              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6578                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6579                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6580                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6581                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6582                !!!cp ('t336');                !!!cp ('t336');
6583                ## NOTE: Whether the encoding is supported or not is handled                ## NOTE: Whether the encoding is supported or not is handled
6584                ## in the {change_encoding} callback.                ## in the {change_encoding} callback.
# Line 6272  sub _tree_construction_main ($) { Line 6616  sub _tree_construction_main ($) {
6616          $parse_rcdata->(RCDATA_CONTENT_MODEL);          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6617          next B;          next B;
6618        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6619          !!!parse-error (type => 'in body:body', token => $token);          !!!parse-error (type => 'in body', text => 'body', token => $token);
6620                                
6621          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6622              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
# Line 6392  sub _tree_construction_main ($) { Line 6736  sub _tree_construction_main ($) {
6736              if ($i != -1) {              if ($i != -1) {
6737                !!!cp ('t355');                !!!cp ('t355');
6738                !!!parse-error (type => 'not closed',                !!!parse-error (type => 'not closed',
6739                                value => $self->{open_elements}->[-1]->[0]                                text => $self->{open_elements}->[-1]->[0]
6740                                    ->manakai_local_name,                                    ->manakai_local_name,
6741                                token => $token);                                token => $token);
6742              } else {              } else {
# Line 6546  sub _tree_construction_main ($) { Line 6890  sub _tree_construction_main ($) {
6890                  xmp => 1,                  xmp => 1,
6891                  iframe => 1,                  iframe => 1,
6892                  noembed => 1,                  noembed => 1,
6893                  noframes => 1,                  noframes => 1, ## NOTE: This is an "as if in head" code clone.
6894                  noscript => 0, ## TODO: 1 if scripting is enabled                  noscript => 0, ## TODO: 1 if scripting is enabled
6895                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6896          if ($token->{tag_name} eq 'xmp') {          if ($token->{tag_name} eq 'xmp') {
# Line 6568  sub _tree_construction_main ($) { Line 6912  sub _tree_construction_main ($) {
6912            !!!next-token;            !!!next-token;
6913            next B;            next B;
6914          } else {          } else {
6915              !!!ack ('t391.1');
6916    
6917            my $at = $token->{attributes};            my $at = $token->{attributes};
6918            my $form_attrs;            my $form_attrs;
6919            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 6611  sub _tree_construction_main ($) { Line 6957  sub _tree_construction_main ($) {
6957                           line => $token->{line}, column => $token->{column}},                           line => $token->{line}, column => $token->{column}},
6958                          {type => END_TAG_TOKEN, tag_name => 'form',                          {type => END_TAG_TOKEN, tag_name => 'form',
6959                           line => $token->{line}, column => $token->{column}};                           line => $token->{line}, column => $token->{column}};
           !!!nack ('t391.1'); ## NOTE: Not acknowledged.  
6960            !!!back-token (@tokens);            !!!back-token (@tokens);
6961            !!!next-token;            !!!next-token;
6962            next B;            next B;
# Line 6659  sub _tree_construction_main ($) { Line 7004  sub _tree_construction_main ($) {
7004            ## Ignore the token            ## Ignore the token
7005          } else {          } else {
7006            !!!cp ('t398');            !!!cp ('t398');
7007            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
7008          }          }
7009          !!!next-token;          !!!next-token;
7010          next B;          next B;
7011          } elsif ($token->{tag_name} eq 'rt' or
7012                   $token->{tag_name} eq 'rp') {
7013            ## has a |ruby| element in scope
7014            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7015              my $node = $self->{open_elements}->[$_];
7016              if ($node->[1] & RUBY_EL) {
7017                !!!cp ('t398.1');
7018                ## generate implied end tags
7019                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7020                  !!!cp ('t398.2');
7021                  pop @{$self->{open_elements}};
7022                }
7023                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
7024                  !!!cp ('t398.3');
7025                  !!!parse-error (type => 'not closed',
7026                                  text => $self->{open_elements}->[-1]->[0]
7027                                      ->manakai_local_name,
7028                                  token => $token);
7029                  pop @{$self->{open_elements}}
7030                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
7031                }
7032                last INSCOPE;
7033              } elsif ($node->[1] & SCOPING_EL) {
7034                !!!cp ('t398.4');
7035                last INSCOPE;
7036              }
7037            } # INSCOPE
7038    
7039            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7040    
7041            !!!nack ('t398.5');
7042            !!!next-token;
7043            redo B;
7044        } elsif ($token->{tag_name} eq 'math' or        } elsif ($token->{tag_name} eq 'math' or
7045                 $token->{tag_name} eq 'svg') {                 $token->{tag_name} eq 'svg') {
7046          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7047    
7048            ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
7049    
7050          ## "adjust SVG attributes" ('svg' only) - done in insert-element-f          ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
7051    
7052          ## "adjust foreign attributes" - done in insert-element-f          ## "adjust foreign attributes" - done in insert-element-f
# Line 6693  sub _tree_construction_main ($) { Line 7073  sub _tree_construction_main ($) {
7073                  thead => 1, tr => 1,                  thead => 1, tr => 1,
7074                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7075          !!!cp ('t401');          !!!cp ('t401');
7076          !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'in body',
7077                            text => $token->{tag_name}, token => $token);
7078          ## Ignore the token          ## Ignore the token
7079          !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.          !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7080          !!!next-token;          !!!next-token;
# Line 6778  sub _tree_construction_main ($) { Line 7159  sub _tree_construction_main ($) {
7159            }            }
7160    
7161            !!!parse-error (type => 'start tag not allowed',            !!!parse-error (type => 'start tag not allowed',
7162                            value => $token->{tag_name}, token => $token);                            text => $token->{tag_name}, token => $token);
7163            ## NOTE: Ignore the token.            ## NOTE: Ignore the token.
7164            !!!next-token;            !!!next-token;
7165            next B;            next B;
# Line 6788  sub _tree_construction_main ($) { Line 7169  sub _tree_construction_main ($) {
7169            unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {            unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7170              !!!cp ('t403');              !!!cp ('t403');
7171              !!!parse-error (type => 'not closed',              !!!parse-error (type => 'not closed',
7172                              value => $_->[0]->manakai_local_name,                              text => $_->[0]->manakai_local_name,
7173                              token => $token);                              token => $token);
7174              last;              last;
7175            } else {            } else {
# Line 6808  sub _tree_construction_main ($) { Line 7189  sub _tree_construction_main ($) {
7189            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7190              !!!cp ('t406');              !!!cp ('t406');
7191              !!!parse-error (type => 'not closed',              !!!parse-error (type => 'not closed',
7192                              value => $self->{open_elements}->[1]->[0]                              text => $self->{open_elements}->[1]->[0]
7193                                  ->manakai_local_name,                                  ->manakai_local_name,
7194                              token => $token);                              token => $token);
7195            } else {            } else {
# Line 6819  sub _tree_construction_main ($) { Line 7200  sub _tree_construction_main ($) {
7200            next B;            next B;
7201          } else {          } else {
7202            !!!cp ('t408');            !!!cp ('t408');
7203            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7204                              text => $token->{tag_name}, token => $token);
7205            ## Ignore the token            ## Ignore the token
7206            !!!next-token;            !!!next-token;
7207            next B;            next B;
# Line 6847  sub _tree_construction_main ($) { Line 7229  sub _tree_construction_main ($) {
7229    
7230          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7231            !!!cp ('t413');            !!!cp ('t413');
7232            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7233                              text => $token->{tag_name}, token => $token);
7234              ## NOTE: Ignore the token.
7235          } else {          } else {
7236            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7237            while ({            while ({
7238                      ## END_TAG_OPTIONAL_EL
7239                    dd => ($token->{tag_name} ne 'dd'),                    dd => ($token->{tag_name} ne 'dd'),
7240                    dt => ($token->{tag_name} ne 'dt'),                    dt => ($token->{tag_name} ne 'dt'),
7241                    li => ($token->{tag_name} ne 'li'),                    li => ($token->{tag_name} ne 'li'),
7242                    p => 1,                    p => 1,
7243                      rt => 1,
7244                      rp => 1,
7245                   }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {                   }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7246              !!!cp ('t409');              !!!cp ('t409');
7247              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
# Line 6865  sub _tree_construction_main ($) { Line 7252  sub _tree_construction_main ($) {
7252                    ne $token->{tag_name}) {                    ne $token->{tag_name}) {
7253              !!!cp ('t412');              !!!cp ('t412');
7254              !!!parse-error (type => 'not closed',              !!!parse-error (type => 'not closed',
7255                              value => $self->{open_elements}->[-1]->[0]                              text => $self->{open_elements}->[-1]->[0]
7256                                  ->manakai_local_name,                                  ->manakai_local_name,
7257                              token => $token);                              token => $token);
7258            } else {            } else {
# Line 6902  sub _tree_construction_main ($) { Line 7289  sub _tree_construction_main ($) {
7289    
7290          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7291            !!!cp ('t421');            !!!cp ('t421');
7292            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7293                              text => $token->{tag_name}, token => $token);
7294              ## NOTE: Ignore the token.
7295          } else {          } else {
7296            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7297            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
# Line 6915  sub _tree_construction_main ($) { Line 7304  sub _tree_construction_main ($) {
7304                    ne $token->{tag_name}) {                    ne $token->{tag_name}) {
7305              !!!cp ('t417.1');              !!!cp ('t417.1');
7306              !!!parse-error (type => 'not closed',              !!!parse-error (type => 'not closed',
7307                              value => $self->{open_elements}->[-1]->[0]                              text => $self->{open_elements}->[-1]->[0]
7308                                  ->manakai_local_name,                                  ->manakai_local_name,
7309                              token => $token);                              token => $token);
7310            } else {            } else {
# Line 6947  sub _tree_construction_main ($) { Line 7336  sub _tree_construction_main ($) {
7336    
7337          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7338            !!!cp ('t425.1');            !!!cp ('t425.1');
7339            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7340                              text => $token->{tag_name}, token => $token);
7341              ## NOTE: Ignore the token.
7342          } else {          } else {
7343            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7344            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
# Line 6959  sub _tree_construction_main ($) { Line 7350  sub _tree_construction_main ($) {
7350            if ($self->{open_elements}->[-1]->[0]->manakai_local_name            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7351                    ne $token->{tag_name}) {                    ne $token->{tag_name}) {
7352              !!!cp ('t425');              !!!cp ('t425');
7353              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);              !!!parse-error (type => 'unmatched end tag',
7354                                text => $token->{tag_name}, token => $token);
7355            } else {            } else {
7356              !!!cp ('t426');              !!!cp ('t426');
7357            }            }
# Line 6990  sub _tree_construction_main ($) { Line 7382  sub _tree_construction_main ($) {
7382                    ne $token->{tag_name}) {                    ne $token->{tag_name}) {
7383              !!!cp ('t412.1');              !!!cp ('t412.1');
7384              !!!parse-error (type => 'not closed',              !!!parse-error (type => 'not closed',
7385                              value => $self->{open_elements}->[-1]->[0]                              text => $self->{open_elements}->[-1]->[0]
7386                                  ->manakai_local_name,                                  ->manakai_local_name,
7387                              token => $token);                              token => $token);
7388            } else {            } else {
# Line 7000  sub _tree_construction_main ($) { Line 7392  sub _tree_construction_main ($) {
7392            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7393          } else {          } else {
7394            !!!cp ('t413.1');            !!!cp ('t413.1');
7395            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);            !!!parse-error (type => 'unmatched end tag',
7396                              text => $token->{tag_name}, token => $token);
7397    
7398            !!!cp ('t415.1');            !!!cp ('t415.1');
7399            ## As if <p>, then reprocess the current token            ## As if <p>, then reprocess the current token
# Line 7023  sub _tree_construction_main ($) { Line 7416  sub _tree_construction_main ($) {
7416          next B;          next B;
7417        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7418          !!!cp ('t428');          !!!cp ('t428');
7419          !!!parse-error (type => 'unmatched end tag:br', token => $token);          !!!parse-error (type => 'unmatched end tag',
7420                            text => 'br', token => $token);
7421    
7422          ## As if <br>          ## As if <br>
7423          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
# Line 7048  sub _tree_construction_main ($) { Line 7442  sub _tree_construction_main ($) {
7442                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7443                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7444          !!!cp ('t429');          !!!cp ('t429');
7445          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);          !!!parse-error (type => 'unmatched end tag',
7446                            text => $token->{tag_name}, token => $token);
7447          ## Ignore the token          ## Ignore the token
7448          !!!next-token;          !!!next-token;
7449          next B;          next B;
# Line 7067  sub _tree_construction_main ($) { Line 7462  sub _tree_construction_main ($) {
7462              ## generate implied end tags              ## generate implied end tags
7463              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7464                !!!cp ('t430');                !!!cp ('t430');
7465                ## ISSUE: Can this case be reached?                ## NOTE: |<ruby><rt></ruby>|.
7466                  ## ISSUE: <ruby><rt></rt> will also take this code path,
7467                  ## which seems wrong.
7468                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
7469                  $node_i++;
7470              }              }
7471                    
7472              ## Step 2              ## Step 2
# Line 7077  sub _tree_construction_main ($) { Line 7475  sub _tree_construction_main ($) {
7475                !!!cp ('t431');                !!!cp ('t431');
7476                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7477                !!!parse-error (type => 'not closed',                !!!parse-error (type => 'not closed',
7478                                value => $self->{open_elements}->[-1]->[0]                                text => $self->{open_elements}->[-1]->[0]
7479                                    ->manakai_local_name,                                    ->manakai_local_name,
7480                                token => $token);                                token => $token);
7481              } else {              } else {
# Line 7085  sub _tree_construction_main ($) { Line 7483  sub _tree_construction_main ($) {
7483              }              }
7484                            
7485              ## Step 3              ## Step 3
7486              splice @{$self->{open_elements}}, $node_i;              splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7487    
7488              !!!next-token;              !!!next-token;
7489              last S2;              last S2;
# Line 7096  sub _tree_construction_main ($) { Line 7494  sub _tree_construction_main ($) {
7494                  ($node->[1] & SPECIAL_EL or                  ($node->[1] & SPECIAL_EL or
7495                   $node->[1] & SCOPING_EL)) {                   $node->[1] & SCOPING_EL)) {
7496                !!!cp ('t433');                !!!cp ('t433');
7497                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);                !!!parse-error (type => 'unmatched end tag',
7498                                  text => $token->{tag_name}, token => $token);
7499                ## Ignore the token                ## Ignore the token
7500                !!!next-token;                !!!next-token;
7501                last S2;                last S2;
# Line 7142  sub _tree_construction_main ($) { Line 7541  sub _tree_construction_main ($) {
7541    ## TODO: script stuffs    ## TODO: script stuffs
7542  } # _tree_construct_main  } # _tree_construct_main
7543    
7544  sub set_inner_html ($$$) {  sub set_inner_html ($$$;$) {
7545    my $class = shift;    my $class = shift;
7546    my $node = shift;    my $node = shift;
7547    my $s = \$_[0];    my $s = \$_[0];
7548    my $onerror = $_[1];    my $onerror = $_[1];
7549      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7550    
7551    ## ISSUE: Should {confident} be true?    ## ISSUE: Should {confident} be true?
7552    
# Line 7165  sub set_inner_html ($$$) { Line 7565  sub set_inner_html ($$$) {
7565      }      }
7566    
7567      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7568      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($$s => $node, $onerror, $get_wrapper);
7569    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7570      ## TODO: If non-html element      ## TODO: If non-html element
7571    
7572      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7573    
7574    ## TODO: Support for $get_wrapper
7575    
7576      ## Step 1 # MUST      ## Step 1 # MUST
7577      my $this_doc = $node->owner_document;      my $this_doc = $node->owner_document;
7578      my $doc = $this_doc->implementation->create_document;      my $doc = $this_doc->implementation->create_document;
# Line 7232  sub set_inner_html ($$$) { Line 7634  sub set_inner_html ($$$) {
7634                  0x10FFFE => 1, 0x10FFFF => 1,                  0x10FFFE => 1, 0x10FFFF => 1,
7635                 }->{$self->{next_char}}) {                 }->{$self->{next_char}}) {
7636          !!!cp ('i4.1');          !!!cp ('i4.1');
7637          !!!parse-error (type => 'control char', level => $self->{must_level});          if ($self->{next_char} < 0x10000) {
7638  ## TODO: error type documentation            !!!parse-error (type => 'control char',
7639                              text => (sprintf 'U+%04X', $self->{next_char}));
7640            } else {
7641              !!!parse-error (type => 'control char',
7642                              text => (sprintf 'U-%08X', $self->{next_char}));
7643            }
7644        }        }
7645      };      };
7646      $p->{prev_char} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];

Legend:
Removed from v.1.134  
changed lines
  Added in v.1.166

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24