/[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.154 by wakaba, Sat Aug 16 07:35:23 2008 UTC revision 1.170 by wakaba, Sat Sep 13 12:25:44 2008 UTC
# Line 354  sub parse_byte_string ($$$$;$) { Line 354  sub parse_byte_string ($$$$;$) {
354    return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);    return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
355  } # parse_byte_string  } # parse_byte_string
356    
357  sub parse_byte_stream ($$$$;$) {  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 $byte_stream = $_[0];    my $byte_stream = $_[0];
# Line 365  sub parse_byte_stream ($$$$;$) { Line 366  sub parse_byte_stream ($$$$;$) {
366    };    };
367    $self->{parse_error} = $onerror; # updated later by parse_char_string    $self->{parse_error} = $onerror; # updated later by parse_char_string
368    
369      my $get_wrapper = $_[3] || sub ($) {
370        return $_[0]; # $_[0] = byte stream handle, returned = arg to char handle
371      };
372    
373    ## HTML5 encoding sniffing algorithm    ## HTML5 encoding sniffing algorithm
374    require Message::Charset::Info;    require Message::Charset::Info;
375    my $charset;    my $charset;
# Line 372  sub parse_byte_stream ($$$$;$) { Line 377  sub parse_byte_stream ($$$$;$) {
377    my ($char_stream, $e_status);    my ($char_stream, $e_status);
378    
379    SNIFFING: {    SNIFFING: {
380        ## NOTE: By setting |allow_fallback| option true when the
381        ## |get_decode_handle| method is invoked, we ignore what the HTML5
382        ## spec requires, i.e. unsupported encoding should be ignored.
383          ## TODO: We should not do this unless the parser is invoked
384          ## in the conformance checking mode, in which this behavior
385          ## would be useful.
386    
387      ## Step 1      ## Step 1
388      if (defined $charset_name) {      if (defined $charset_name) {
389        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);        $charset = Message::Charset::Info->get_by_html_name ($charset_name);
390              ## TODO: Is this ok?  Transfer protocol's parameter should be
391              ## interpreted in its semantics?
392    
393        ## ISSUE: Unsupported encoding is not ignored according to the spec.        ## ISSUE: Unsupported encoding is not ignored according to the spec.
394        ($char_stream, $e_status) = $charset->get_decode_handle        ($char_stream, $e_status) = $charset->get_decode_handle
# Line 399  sub parse_byte_stream ($$$$;$) { Line 412  sub parse_byte_stream ($$$$;$) {
412    
413      ## Step 3      ## Step 3
414      if ($byte_buffer =~ /^\xFE\xFF/) {      if ($byte_buffer =~ /^\xFE\xFF/) {
415        $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');        $charset = Message::Charset::Info->get_by_html_name ('utf-16be');
416        ($char_stream, $e_status) = $charset->get_decode_handle        ($char_stream, $e_status) = $charset->get_decode_handle
417            ($byte_stream, allow_error_reporting => 1,            ($byte_stream, allow_error_reporting => 1,
418             allow_fallback => 1, byte_buffer => \$byte_buffer);             allow_fallback => 1, byte_buffer => \$byte_buffer);
419        $self->{confident} = 1;        $self->{confident} = 1;
420        last SNIFFING;        last SNIFFING;
421      } elsif ($byte_buffer =~ /^\xFF\xFE/) {      } elsif ($byte_buffer =~ /^\xFF\xFE/) {
422        $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');        $charset = Message::Charset::Info->get_by_html_name ('utf-16le');
423        ($char_stream, $e_status) = $charset->get_decode_handle        ($char_stream, $e_status) = $charset->get_decode_handle
424            ($byte_stream, allow_error_reporting => 1,            ($byte_stream, allow_error_reporting => 1,
425             allow_fallback => 1, byte_buffer => \$byte_buffer);             allow_fallback => 1, byte_buffer => \$byte_buffer);
426        $self->{confident} = 1;        $self->{confident} = 1;
427        last SNIFFING;        last SNIFFING;
428      } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {      } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
429        $charset = Message::Charset::Info->get_by_iana_name ('utf-8');        $charset = Message::Charset::Info->get_by_html_name ('utf-8');
430        ($char_stream, $e_status) = $charset->get_decode_handle        ($char_stream, $e_status) = $charset->get_decode_handle
431            ($byte_stream, allow_error_reporting => 1,            ($byte_stream, allow_error_reporting => 1,
432             allow_fallback => 1, byte_buffer => \$byte_buffer);             allow_fallback => 1, byte_buffer => \$byte_buffer);
# Line 432  sub parse_byte_stream ($$$$;$) { Line 445  sub parse_byte_stream ($$$$;$) {
445      $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string      $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
446          ($byte_buffer);          ($byte_buffer);
447      if (defined $charset_name) {      if (defined $charset_name) {
448        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);        $charset = Message::Charset::Info->get_by_html_name ($charset_name);
449    
450        ## ISSUE: Unsupported encoding is not ignored according to the spec.        ## ISSUE: Unsupported encoding is not ignored according to the spec.
451        require Whatpm::Charset::DecodeHandle;        require Whatpm::Charset::DecodeHandle;
# Line 455  sub parse_byte_stream ($$$$;$) { Line 468  sub parse_byte_stream ($$$$;$) {
468    
469      ## Step 7: default      ## Step 7: default
470      ## TODO: Make this configurable.      ## TODO: Make this configurable.
471      $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');      $charset = Message::Charset::Info->get_by_html_name ('windows-1252');
472          ## NOTE: We choose |windows-1252| here, since |utf-8| should be          ## NOTE: We choose |windows-1252| here, since |utf-8| should be
473          ## detectable in the step 6.          ## detectable in the step 6.
474      require Whatpm::Charset::DecodeHandle;      require Whatpm::Charset::DecodeHandle;
# Line 475  sub parse_byte_stream ($$$$;$) { Line 488  sub parse_byte_stream ($$$$;$) {
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        $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
493      !!!parse-error (type => 'chardecode:fallback',      !!!parse-error (type => 'chardecode:fallback',
494                      text => $self->{input_encoding},                      #text => $self->{input_encoding},
495                      level => $self->{level}->{uncertain},                      level => $self->{level}->{uncertain},
496                      line => 1, column => 1,                      line => 1, column => 1,
497                      layer => 'encode');                      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        $self->{input_encoding} = $charset->get_iana_name;
501      !!!parse-error (type => 'chardecode:no error',      !!!parse-error (type => 'chardecode:no error',
502                      text => $self->{input_encoding},                      text => $self->{input_encoding},
503                      level => $self->{level}->{uncertain},                      level => $self->{level}->{uncertain},
504                      line => 1, column => 1,                      line => 1, column => 1,
505                      layer => 'encode');                      layer => 'encode');
506      } else {
507        $self->{input_encoding} = $charset->get_iana_name;
508    }    }
509    
510    $self->{change_encoding} = sub {    $self->{change_encoding} = sub {
# Line 496  sub parse_byte_stream ($$$$;$) { Line 512  sub parse_byte_stream ($$$$;$) {
512      $charset_name = shift;      $charset_name = shift;
513      my $token = shift;      my $token = shift;
514    
515      $charset = Message::Charset::Info->get_by_iana_name ($charset_name);      $charset = Message::Charset::Info->get_by_html_name ($charset_name);
516      ($char_stream, $e_status) = $charset->get_decode_handle      ($char_stream, $e_status) = $charset->get_decode_handle
517          ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,          ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
518           byte_buffer => \ $buffer->{buffer});           byte_buffer => \ $buffer->{buffer});
# Line 507  sub parse_byte_stream ($$$$;$) { Line 523  sub parse_byte_stream ($$$$;$) {
523        ## Step 1            ## Step 1    
524        if ($charset->{category} &        if ($charset->{category} &
525            Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {            Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
526          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');          $charset = Message::Charset::Info->get_by_html_name ('utf-8');
527          ($char_stream, $e_status) = $charset->get_decode_handle          ($char_stream, $e_status) = $charset->get_decode_handle
528              ($byte_stream,              ($byte_stream,
529               byte_buffer => \ $buffer->{buffer});               byte_buffer => \ $buffer->{buffer});
# Line 551  sub parse_byte_stream ($$$$;$) { Line 567  sub parse_byte_stream ($$$$;$) {
567        ${$opt{octets}} = "\x{FFFD}"; # relacement character        ${$opt{octets}} = "\x{FFFD}"; # relacement character
568      }      }
569    };    };
570    $char_stream->onerror ($char_onerror);  
571      my $wrapped_char_stream = $get_wrapper->($char_stream);
572      $wrapped_char_stream->onerror ($char_onerror);
573    
574    my @args = @_; shift @args; # $s    my @args = @_; shift @args; # $s
575    my $return;    my $return;
576    try {    try {
577      $return = $self->parse_char_stream ($char_stream, @args);        $return = $self->parse_char_stream ($wrapped_char_stream, @args);  
578    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
579      ## NOTE: Invoked after {change_encoding}.      ## NOTE: Invoked after {change_encoding}.
580    
     $self->{input_encoding} = $charset->get_iana_name;  
581      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
582          $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
583        !!!parse-error (type => 'chardecode:fallback',        !!!parse-error (type => 'chardecode:fallback',
                       text => $self->{input_encoding},  
584                        level => $self->{level}->{uncertain},                        level => $self->{level}->{uncertain},
585                          #text => $self->{input_encoding},
586                        line => 1, column => 1,                        line => 1, column => 1,
587                        layer => 'encode');                        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          $self->{input_encoding} = $charset->get_iana_name;
591        !!!parse-error (type => 'chardecode:no error',        !!!parse-error (type => 'chardecode:no error',
592                        text => $self->{input_encoding},                        text => $self->{input_encoding},
593                        level => $self->{level}->{uncertain},                        level => $self->{level}->{uncertain},
594                        line => 1, column => 1,                        line => 1, column => 1,
595                        layer => 'encode');                        layer => 'encode');
596        } else {
597          $self->{input_encoding} = $charset->get_iana_name;
598      }      }
599      $self->{confident} = 1;      $self->{confident} = 1;
600      $char_stream->onerror ($char_onerror);  
601      $return = $self->parse_char_stream ($char_stream, @args);      $wrapped_char_stream = $get_wrapper->($char_stream);
602        $wrapped_char_stream->onerror ($char_onerror);
603    
604        $return = $self->parse_char_stream ($wrapped_char_stream, @args);
605    };    };
606    return $return;    return $return;
607  } # parse_byte_stream  } # parse_byte_stream
# Line 591  sub parse_byte_stream ($$$$;$) { Line 615  sub parse_byte_stream ($$$$;$) {
615  ## such as |parse_byte_string| in this module, must ensure that it does  ## such as |parse_byte_string| in this module, must ensure that it does
616  ## strip the BOM and never strip any ZWNBSP.  ## strip the BOM and never strip any ZWNBSP.
617    
618  sub parse_char_string ($$$;$) {  sub parse_char_string ($$$;$$) {
619      #my ($self, $s, $doc, $onerror, $get_wrapper) = @_;
620    my $self = shift;    my $self = shift;
621    require utf8;    require utf8;
622    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $s = ref $_[0] ? $_[0] : \($_[0]);
623    open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;    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..$#_]);    return $self->parse_char_stream ($input, @_[1..$#_]);
628  } # parse_char_string  } # parse_char_string
629  *parse_string = \&parse_char_string;  *parse_string = \&parse_char_string; ## NOTE: Alias for backward compatibility.
630    
631  sub parse_char_stream ($$$;$) {  sub parse_char_stream ($$$;$) {
632    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
# Line 641  sub parse_char_stream ($$$;$) { Line 669  sub parse_char_stream ($$$;$) {
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    ## TODO: support for abort/streaming
673        my $next = $input->getc;        my $next = $input->getc;
674        if (defined $next and $next ne "\x0A") {        if (defined $next and $next ne "\x0A") {
675          $self->{next_next_char} = $next;          $self->{next_next_char} = $next;
# Line 708  sub new ($) { Line 737  sub new ($) {
737    my $class = shift;    my $class = shift;
738    my $self = bless {    my $self = bless {
739      level => {must => 'm',      level => {must => 'm',
740                  should => 's',
741                warn => 'w',                warn => 'w',
742                info => 'i',                info => 'i',
743                uncertain => 'u'},                uncertain => 'u'},
# Line 740  sub RCDATA_CONTENT_MODEL () { CM_ENTITY Line 770  sub RCDATA_CONTENT_MODEL () { CM_ENTITY
770  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
771    
772  sub DATA_STATE () { 0 }  sub DATA_STATE () { 0 }
773  sub ENTITY_DATA_STATE () { 1 }  #sub ENTITY_DATA_STATE () { 1 }
774  sub TAG_OPEN_STATE () { 2 }  sub TAG_OPEN_STATE () { 2 }
775  sub CLOSE_TAG_OPEN_STATE () { 3 }  sub CLOSE_TAG_OPEN_STATE () { 3 }
776  sub TAG_NAME_STATE () { 4 }  sub TAG_NAME_STATE () { 4 }
# Line 751  sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 Line 781  sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8
781  sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }  sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
782  sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }  sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
783  sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }  sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
784  sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }  #sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
785  sub MARKUP_DECLARATION_OPEN_STATE () { 13 }  sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
786  sub COMMENT_START_STATE () { 14 }  sub COMMENT_START_STATE () { 14 }
787  sub COMMENT_START_DASH_STATE () { 15 }  sub COMMENT_START_DASH_STATE () { 15 }
# Line 774  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STAT Line 804  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STAT
804  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
805  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
806  sub SELF_CLOSING_START_TAG_STATE () { 34 }  sub SELF_CLOSING_START_TAG_STATE () { 34 }
807  sub CDATA_BLOCK_STATE () { 35 }  sub CDATA_SECTION_STATE () { 35 }
808    sub MD_HYPHEN_STATE () { 36 } # "markup declaration open state" in the spec
809    sub MD_DOCTYPE_STATE () { 37 } # "markup declaration open state" in the spec
810    sub MD_CDATA_STATE () { 38 } # "markup declaration open state" in the spec
811    sub CDATA_PCDATA_CLOSE_TAG_STATE () { 39 } # "close tag open state" in the spec
812    sub CDATA_SECTION_MSE1_STATE () { 40 } # "CDATA section state" in the spec
813    sub CDATA_SECTION_MSE2_STATE () { 41 } # "CDATA section state" in the spec
814    sub PUBLIC_STATE () { 42 } # "after DOCTYPE name state" in the spec
815    sub SYSTEM_STATE () { 43 } # "after DOCTYPE name state" in the spec
816    ## NOTE: "Entity data state", "entity in attribute value state", and
817    ## "consume a character reference" algorithm are jointly implemented
818    ## using the following six states:
819    sub ENTITY_STATE () { 44 }
820    sub ENTITY_HASH_STATE () { 45 }
821    sub NCR_NUM_STATE () { 46 }
822    sub HEXREF_X_STATE () { 47 }
823    sub HEXREF_HEX_STATE () { 48 }
824    sub ENTITY_NAME_STATE () { 49 }
825    
826  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
827  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 827  sub IN_COLUMN_GROUP_IM () { 0b10 } Line 874  sub IN_COLUMN_GROUP_IM () { 0b10 }
874  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
875    my $self = shift;    my $self = shift;
876    $self->{state} = DATA_STATE; # MUST    $self->{state} = DATA_STATE; # MUST
877      #$self->{state_keyword}; # initialized when used
878      #$self->{entity__value}; # initialized when used
879      #$self->{entity__match}; # initialized when used
880    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
881    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token};
882    undef $self->{current_attribute};    undef $self->{current_attribute};
883    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
884    undef $self->{last_attribute_value_state};    #$self->{prev_state}; # initialized when used
885    delete $self->{self_closing};    delete $self->{self_closing};
   $self->{char} = [];  
886    # $self->{next_char}    # $self->{next_char}
887    !!!next-input-character;    !!!next-input-character;
888    $self->{token} = [];    $self->{token} = [];
# Line 865  sub _initialize_tokenizer ($) { Line 914  sub _initialize_tokenizer ($) {
914  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
915  ## and removed from the list.  ## and removed from the list.
916    
917  ## NOTE: HTML5 "Writing HTML documents" section, applied to  ## TODO: Polytheistic slash SHOULD NOT be used. (Applied only to atheists.)
918  ## documents and not to user agents and conformance checkers,  ## (This requirement was dropped from HTML5 spec, unfortunately.)
 ## contains some requirements that are not detected by the  
 ## parsing algorithm:  
 ## - Some requirements on character encoding declarations. ## TODO  
 ## - "Elements MUST NOT contain content that their content model disallows."  
 ##   ... Some are parse error, some are not (will be reported by c.c.).  
 ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO  
 ## - Text (in elements, attributes, and comments) SHOULD NOT contain  
 ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)  
   
 ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot  
 ## be detected by the HTML5 parsing algorithm:  
 ## - Text,  
919    
920  sub _get_next_token ($) {  sub _get_next_token ($) {
921    my $self = shift;    my $self = shift;
# Line 902  sub _get_next_token ($) { Line 939  sub _get_next_token ($) {
939          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
940              not $self->{escape}) {              not $self->{escape}) {
941            !!!cp (1);            !!!cp (1);
942            $self->{state} = ENTITY_DATA_STATE;            ## NOTE: In the spec, the tokenizer is switched to the
943              ## "entity data state".  In this implementation, the tokenizer
944              ## is switched to the |ENTITY_STATE|, which is an implementation
945              ## of the "consume a character reference" algorithm.
946              $self->{entity_additional} = -1;
947              $self->{prev_state} = DATA_STATE;
948              $self->{state} = ENTITY_STATE;
949            !!!next-input-character;            !!!next-input-character;
950            redo A;            redo A;
951          } else {          } else {
# Line 972  sub _get_next_token ($) { Line 1015  sub _get_next_token ($) {
1015        !!!emit ($token);        !!!emit ($token);
1016    
1017        redo A;        redo A;
     } elsif ($self->{state} == ENTITY_DATA_STATE) {  
       ## (cannot happen in CDATA state)  
   
       my ($l, $c) = ($self->{line_prev}, $self->{column_prev});  
         
       my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);  
   
       $self->{state} = DATA_STATE;  
       # next-input-character is already done  
   
       unless (defined $token) {  
         !!!cp (13);  
         !!!emit ({type => CHARACTER_TOKEN, data => '&',  
                   line => $l, column => $c,  
                  });  
       } else {  
         !!!cp (14);  
         !!!emit ($token);  
       }  
   
       redo A;  
1018      } elsif ($self->{state} == TAG_OPEN_STATE) {      } elsif ($self->{state} == TAG_OPEN_STATE) {
1019        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1020          if ($self->{next_char} == 0x002F) { # /          if ($self->{next_char} == 0x002F) { # /
# Line 1089  sub _get_next_token ($) { Line 1111  sub _get_next_token ($) {
1111          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1112        }        }
1113      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1114          ## NOTE: The "close tag open state" in the spec is implemented as
1115          ## |CLOSE_TAG_OPEN_STATE| and |CDATA_PCDATA_CLOSE_TAG_STATE|.
1116    
1117        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1118        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1119          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1120              $self->{state} = CDATA_PCDATA_CLOSE_TAG_STATE;
1121            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            $self->{state_keyword} = '';
1122            my @next_char;            ## Reconsume.
1123            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...  
           }  
1124          } else {          } else {
1125            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1126              ## NOTE: See <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>.
1127            !!!cp (28);            !!!cp (28);
           # next-input-character is already done  
1128            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1129              ## Reconsume.
1130            !!!emit ({type => CHARACTER_TOKEN, data => '</',            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1131                      line => $l, column => $c,                      line => $l, column => $c,
1132                     });                     });
1133            redo A;            redo A;
1134          }          }
1135        }        }
1136          
1137        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{next_char} and
1138            $self->{next_char} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1139          !!!cp (29);          !!!cp (29);
# Line 1198  sub _get_next_token ($) { Line 1180  sub _get_next_token ($) {
1180                                    line => $self->{line_prev}, # "<" of "</"                                    line => $self->{line_prev}, # "<" of "</"
1181                                    column => $self->{column_prev} - 1,                                    column => $self->{column_prev} - 1,
1182                                   };                                   };
1183          ## $self->{next_char} is intentionally left as is          ## NOTE: $self->{next_char} is intentionally left as is.
1184          redo A;          ## Although the "anything else" case of the spec not explicitly
1185            ## states that the next input character is to be reconsumed,
1186            ## it will be included to the |data| of the comment token
1187            ## generated from the bogus end tag, as defined in the
1188            ## "bogus comment state" entry.
1189            redo A;
1190          }
1191        } elsif ($self->{state} == CDATA_PCDATA_CLOSE_TAG_STATE) {
1192          my $ch = substr $self->{last_emitted_start_tag_name}, length $self->{state_keyword}, 1;
1193          if (length $ch) {
1194            my $CH = $ch;
1195            $ch =~ tr/a-z/A-Z/;
1196            my $nch = chr $self->{next_char};
1197            if ($nch eq $ch or $nch eq $CH) {
1198              !!!cp (24);
1199              ## Stay in the state.
1200              $self->{state_keyword} .= $nch;
1201              !!!next-input-character;
1202              redo A;
1203            } else {
1204              !!!cp (25);
1205              $self->{state} = DATA_STATE;
1206              ## Reconsume.
1207              !!!emit ({type => CHARACTER_TOKEN,
1208                        data => '</' . $self->{state_keyword},
1209                        line => $self->{line_prev},
1210                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1211                       });
1212              redo A;
1213            }
1214          } else { # after "<{tag-name}"
1215            unless ({
1216                     0x0009 => 1, # HT
1217                     0x000A => 1, # LF
1218                     0x000B => 1, # VT
1219                     0x000C => 1, # FF
1220                     0x0020 => 1, # SP
1221                     0x003E => 1, # >
1222                     0x002F => 1, # /
1223                     -1 => 1, # EOF
1224                    }->{$self->{next_char}}) {
1225              !!!cp (26);
1226              ## Reconsume.
1227              $self->{state} = DATA_STATE;
1228              !!!emit ({type => CHARACTER_TOKEN,
1229                        data => '</' . $self->{state_keyword},
1230                        line => $self->{line_prev},
1231                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1232                       });
1233              redo A;
1234            } else {
1235              !!!cp (27);
1236              $self->{current_token}
1237                  = {type => END_TAG_TOKEN,
1238                     tag_name => $self->{last_emitted_start_tag_name},
1239                     line => $self->{line_prev},
1240                     column => $self->{column_prev} - 1 - length $self->{state_keyword}};
1241              $self->{state} = TAG_NAME_STATE;
1242              ## Reconsume.
1243              redo A;
1244            }
1245        }        }
1246      } elsif ($self->{state} == TAG_NAME_STATE) {      } elsif ($self->{state} == TAG_NAME_STATE) {
1247        if ($self->{next_char} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
# Line 1540  sub _get_next_token ($) { Line 1582  sub _get_next_token ($) {
1582    
1583          redo A;          redo A;
1584        } else {        } else {
1585          !!!cp (82);          if ($self->{next_char} == 0x0022 or # "
1586                $self->{next_char} == 0x0027) { # '
1587              !!!cp (78);
1588              !!!parse-error (type => 'bad attribute name');
1589            } else {
1590              !!!cp (82);
1591            }
1592          $self->{current_attribute}          $self->{current_attribute}
1593              = {name => chr ($self->{next_char}),              = {name => chr ($self->{next_char}),
1594                 value => '',                 value => '',
# Line 1575  sub _get_next_token ($) { Line 1623  sub _get_next_token ($) {
1623          !!!next-input-character;          !!!next-input-character;
1624          redo A;          redo A;
1625        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1626            !!!parse-error (type => 'empty unquoted attribute value');
1627          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1628            !!!cp (87);            !!!cp (87);
1629            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
# Line 1639  sub _get_next_token ($) { Line 1688  sub _get_next_token ($) {
1688          redo A;          redo A;
1689        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1690          !!!cp (96);          !!!cp (96);
1691          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1692          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1693            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1694            ## implementation of the "consume a character reference" algorithm.
1695            $self->{prev_state} = $self->{state};
1696            $self->{entity_additional} = 0x0022; # "
1697            $self->{state} = ENTITY_STATE;
1698          !!!next-input-character;          !!!next-input-character;
1699          redo A;          redo A;
1700        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
# Line 1681  sub _get_next_token ($) { Line 1735  sub _get_next_token ($) {
1735          redo A;          redo A;
1736        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1737          !!!cp (102);          !!!cp (102);
1738          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1739          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1740            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1741            ## implementation of the "consume a character reference" algorithm.
1742            $self->{entity_additional} = 0x0027; # '
1743            $self->{prev_state} = $self->{state};
1744            $self->{state} = ENTITY_STATE;
1745          !!!next-input-character;          !!!next-input-character;
1746          redo A;          redo A;
1747        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
# Line 1727  sub _get_next_token ($) { Line 1786  sub _get_next_token ($) {
1786          redo A;          redo A;
1787        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1788          !!!cp (108);          !!!cp (108);
1789          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1790          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1791            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1792            ## implementation of the "consume a character reference" algorithm.
1793            $self->{entity_additional} = -1;
1794            $self->{prev_state} = $self->{state};
1795            $self->{state} = ENTITY_STATE;
1796          !!!next-input-character;          !!!next-input-character;
1797          redo A;          redo A;
1798        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
# Line 1792  sub _get_next_token ($) { Line 1856  sub _get_next_token ($) {
1856          !!!next-input-character;          !!!next-input-character;
1857          redo A;          redo A;
1858        }        }
     } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {  
       my $token = $self->_tokenize_attempt_to_consume_an_entity  
           (1,  
            $self->{last_attribute_value_state}  
              == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "  
            $self->{last_attribute_value_state}  
              == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '  
            -1);  
   
       unless (defined $token) {  
         !!!cp (117);  
         $self->{current_attribute}->{value} .= '&';  
       } else {  
         !!!cp (118);  
         $self->{current_attribute}->{value} .= $token->{data};  
         $self->{current_attribute}->{has_reference} = $token->{has_reference};  
         ## ISSUE: spec says "append the returned character token to the current attribute's value"  
       }  
   
       $self->{state} = $self->{last_attribute_value_state};  
       # next-input-character is already done  
       redo A;  
1859      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1860        if ($self->{next_char} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1861            $self->{next_char} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
# Line 1933  sub _get_next_token ($) { Line 1975  sub _get_next_token ($) {
1975        }        }
1976      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1977        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
         
       ## NOTE: Set by the previous state  
       #my $token = {type => COMMENT_TOKEN, data => ''};  
   
       BC: {  
         if ($self->{next_char} == 0x003E) { # >  
           !!!cp (124);  
           $self->{state} = DATA_STATE;  
           !!!next-input-character;  
   
           !!!emit ($self->{current_token}); # comment  
1978    
1979            redo A;        ## NOTE: Unlike spec's "bogus comment state", this implementation
1980          } elsif ($self->{next_char} == -1) {        ## consumes characters one-by-one basis.
1981            !!!cp (125);        
1982            $self->{state} = DATA_STATE;        if ($self->{next_char} == 0x003E) { # >
1983            ## reconsume          !!!cp (124);
1984            $self->{state} = DATA_STATE;
1985            !!!emit ($self->{current_token}); # comment          !!!next-input-character;
1986    
1987            redo A;          !!!emit ($self->{current_token}); # comment
1988          } else {          redo A;
1989            !!!cp (126);        } elsif ($self->{next_char} == -1) {
1990            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment          !!!cp (125);
1991            !!!next-input-character;          $self->{state} = DATA_STATE;
1992            redo BC;          ## reconsume
         }  
       } # BC  
1993    
1994        die "$0: _get_next_token: unexpected case [BC]";          !!!emit ($self->{current_token}); # comment
1995            redo A;
1996          } else {
1997            !!!cp (126);
1998            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1999            ## Stay in the state.
2000            !!!next-input-character;
2001            redo A;
2002          }
2003      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2004        ## (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};  
2005                
2006        if ($self->{next_char} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2007            !!!cp (133);
2008            $self->{state} = MD_HYPHEN_STATE;
2009          !!!next-input-character;          !!!next-input-character;
2010          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);  
         }  
2011        } elsif ($self->{next_char} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
2012                 $self->{next_char} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
2013            ## ASCII case-insensitive.
2014            !!!cp (130);
2015            $self->{state} = MD_DOCTYPE_STATE;
2016            $self->{state_keyword} = chr $self->{next_char};
2017          !!!next-input-character;          !!!next-input-character;
2018          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);  
         }  
2019        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2020                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2021                 $self->{next_char} == 0x005B) { # [                 $self->{next_char} == 0x005B) { # [
2022            !!!cp (135.4);                
2023            $self->{state} = MD_CDATA_STATE;
2024            $self->{state_keyword} = '[';
2025          !!!next-input-character;          !!!next-input-character;
2026          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);  
         }  
2027        } else {        } else {
2028          !!!cp (136);          !!!cp (136);
2029        }        }
2030    
2031        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment',
2032        $self->{next_char} = shift @next_char;                        line => $self->{line_prev},
2033        !!!back-next-input-character (@next_char);                        column => $self->{column_prev} - 1);
2034          ## Reconsume.
2035        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2036        $self->{current_token} = {type => COMMENT_TOKEN, data => '',        $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2037                                  line => $l, column => $c,                                  line => $self->{line_prev},
2038                                    column => $self->{column_prev} - 1,
2039                                 };                                 };
2040        redo A;        redo A;
2041              } elsif ($self->{state} == MD_HYPHEN_STATE) {
2042        ## ISSUE: typos in spec: chacacters, is is a parse error        if ($self->{next_char} == 0x002D) { # -
2043        ## 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);
2044            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2045                                      line => $self->{line_prev},
2046                                      column => $self->{column_prev} - 2,
2047                                     };
2048            $self->{state} = COMMENT_START_STATE;
2049            !!!next-input-character;
2050            redo A;
2051          } else {
2052            !!!cp (128);
2053            !!!parse-error (type => 'bogus comment',
2054                            line => $self->{line_prev},
2055                            column => $self->{column_prev} - 2);
2056            $self->{state} = BOGUS_COMMENT_STATE;
2057            ## Reconsume.
2058            $self->{current_token} = {type => COMMENT_TOKEN,
2059                                      data => '-',
2060                                      line => $self->{line_prev},
2061                                      column => $self->{column_prev} - 2,
2062                                     };
2063            redo A;
2064          }
2065        } elsif ($self->{state} == MD_DOCTYPE_STATE) {
2066          ## ASCII case-insensitive.
2067          if ($self->{next_char} == [
2068                undef,
2069                0x004F, # O
2070                0x0043, # C
2071                0x0054, # T
2072                0x0059, # Y
2073                0x0050, # P
2074              ]->[length $self->{state_keyword}] or
2075              $self->{next_char} == [
2076                undef,
2077                0x006F, # o
2078                0x0063, # c
2079                0x0074, # t
2080                0x0079, # y
2081                0x0070, # p
2082              ]->[length $self->{state_keyword}]) {
2083            !!!cp (131);
2084            ## Stay in the state.
2085            $self->{state_keyword} .= chr $self->{next_char};
2086            !!!next-input-character;
2087            redo A;
2088          } elsif ((length $self->{state_keyword}) == 6 and
2089                   ($self->{next_char} == 0x0045 or # E
2090                    $self->{next_char} == 0x0065)) { # e
2091            !!!cp (129);
2092            $self->{state} = DOCTYPE_STATE;
2093            $self->{current_token} = {type => DOCTYPE_TOKEN,
2094                                      quirks => 1,
2095                                      line => $self->{line_prev},
2096                                      column => $self->{column_prev} - 7,
2097                                     };
2098            !!!next-input-character;
2099            redo A;
2100          } else {
2101            !!!cp (132);        
2102            !!!parse-error (type => 'bogus comment',
2103                            line => $self->{line_prev},
2104                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2105            $self->{state} = BOGUS_COMMENT_STATE;
2106            ## Reconsume.
2107            $self->{current_token} = {type => COMMENT_TOKEN,
2108                                      data => $self->{state_keyword},
2109                                      line => $self->{line_prev},
2110                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2111                                     };
2112            redo A;
2113          }
2114        } elsif ($self->{state} == MD_CDATA_STATE) {
2115          if ($self->{next_char} == {
2116                '[' => 0x0043, # C
2117                '[C' => 0x0044, # D
2118                '[CD' => 0x0041, # A
2119                '[CDA' => 0x0054, # T
2120                '[CDAT' => 0x0041, # A
2121              }->{$self->{state_keyword}}) {
2122            !!!cp (135.1);
2123            ## Stay in the state.
2124            $self->{state_keyword} .= chr $self->{next_char};
2125            !!!next-input-character;
2126            redo A;
2127          } elsif ($self->{state_keyword} eq '[CDATA' and
2128                   $self->{next_char} == 0x005B) { # [
2129            !!!cp (135.2);
2130            $self->{current_token} = {type => CHARACTER_TOKEN,
2131                                      data => '',
2132                                      line => $self->{line_prev},
2133                                      column => $self->{column_prev} - 7};
2134            $self->{state} = CDATA_SECTION_STATE;
2135            !!!next-input-character;
2136            redo A;
2137          } else {
2138            !!!cp (135.3);
2139            !!!parse-error (type => 'bogus comment',
2140                            line => $self->{line_prev},
2141                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2142            $self->{state} = BOGUS_COMMENT_STATE;
2143            ## Reconsume.
2144            $self->{current_token} = {type => COMMENT_TOKEN,
2145                                      data => $self->{state_keyword},
2146                                      line => $self->{line_prev},
2147                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2148                                     };
2149            redo A;
2150          }
2151      } elsif ($self->{state} == COMMENT_START_STATE) {      } elsif ($self->{state} == COMMENT_START_STATE) {
2152        if ($self->{next_char} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2153          !!!cp (137);          !!!cp (137);
# Line 2362  sub _get_next_token ($) { Line 2414  sub _get_next_token ($) {
2414          redo A;          redo A;
2415        } elsif ($self->{next_char} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
2416                 $self->{next_char} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
2417            $self->{state} = PUBLIC_STATE;
2418            $self->{state_keyword} = chr $self->{next_char};
2419          !!!next-input-character;          !!!next-input-character;
2420          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);  
         }  
   
         #  
2421        } elsif ($self->{next_char} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
2422                 $self->{next_char} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
2423            $self->{state} = SYSTEM_STATE;
2424            $self->{state_keyword} = chr $self->{next_char};
2425          !!!next-input-character;          !!!next-input-character;
2426          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);  
         }  
   
         #  
2427        } else {        } else {
2428          !!!cp (180);          !!!cp (180);
2429            !!!parse-error (type => 'string after DOCTYPE name');
2430            $self->{current_token}->{quirks} = 1;
2431    
2432            $self->{state} = BOGUS_DOCTYPE_STATE;
2433          !!!next-input-character;          !!!next-input-character;
2434          #          redo A;
2435        }        }
2436        } elsif ($self->{state} == PUBLIC_STATE) {
2437          ## ASCII case-insensitive
2438          if ($self->{next_char} == [
2439                undef,
2440                0x0055, # U
2441                0x0042, # B
2442                0x004C, # L
2443                0x0049, # I
2444              ]->[length $self->{state_keyword}] or
2445              $self->{next_char} == [
2446                undef,
2447                0x0075, # u
2448                0x0062, # b
2449                0x006C, # l
2450                0x0069, # i
2451              ]->[length $self->{state_keyword}]) {
2452            !!!cp (175);
2453            ## Stay in the state.
2454            $self->{state_keyword} .= chr $self->{next_char};
2455            !!!next-input-character;
2456            redo A;
2457          } elsif ((length $self->{state_keyword}) == 5 and
2458                   ($self->{next_char} == 0x0043 or # C
2459                    $self->{next_char} == 0x0063)) { # c
2460            !!!cp (168);
2461            $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2462            !!!next-input-character;
2463            redo A;
2464          } else {
2465            !!!cp (169);
2466            !!!parse-error (type => 'string after DOCTYPE name',
2467                            line => $self->{line_prev},
2468                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2469            $self->{current_token}->{quirks} = 1;
2470    
2471        !!!parse-error (type => 'string after DOCTYPE name');          $self->{state} = BOGUS_DOCTYPE_STATE;
2472        $self->{current_token}->{quirks} = 1;          ## Reconsume.
2473            redo A;
2474          }
2475        } elsif ($self->{state} == SYSTEM_STATE) {
2476          ## ASCII case-insensitive
2477          if ($self->{next_char} == [
2478                undef,
2479                0x0059, # Y
2480                0x0053, # S
2481                0x0054, # T
2482                0x0045, # E
2483              ]->[length $self->{state_keyword}] or
2484              $self->{next_char} == [
2485                undef,
2486                0x0079, # y
2487                0x0073, # s
2488                0x0074, # t
2489                0x0065, # e
2490              ]->[length $self->{state_keyword}]) {
2491            !!!cp (170);
2492            ## Stay in the state.
2493            $self->{state_keyword} .= chr $self->{next_char};
2494            !!!next-input-character;
2495            redo A;
2496          } elsif ((length $self->{state_keyword}) == 5 and
2497                   ($self->{next_char} == 0x004D or # M
2498                    $self->{next_char} == 0x006D)) { # m
2499            !!!cp (171);
2500            $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2501            !!!next-input-character;
2502            redo A;
2503          } else {
2504            !!!cp (172);
2505            !!!parse-error (type => 'string after DOCTYPE name',
2506                            line => $self->{line_prev},
2507                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2508            $self->{current_token}->{quirks} = 1;
2509    
2510        $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2511        # next-input-character is already done          ## Reconsume.
2512        redo A;          redo A;
2513          }
2514      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2515        if ({        if ({
2516              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
# Line 2804  sub _get_next_token ($) { Line 2867  sub _get_next_token ($) {
2867          !!!next-input-character;          !!!next-input-character;
2868          redo A;          redo A;
2869        }        }
2870      } elsif ($self->{state} == CDATA_BLOCK_STATE) {      } elsif ($self->{state} == CDATA_SECTION_STATE) {
2871        my $s = '';        ## NOTE: "CDATA section state" in the state is jointly implemented
2872          ## by three states, |CDATA_SECTION_STATE|, |CDATA_SECTION_MSE1_STATE|,
2873          ## and |CDATA_SECTION_MSE2_STATE|.
2874                
2875        my ($l, $c) = ($self->{line}, $self->{column});        if ($self->{next_char} == 0x005D) { # ]
2876            !!!cp (221.1);
2877            $self->{state} = CDATA_SECTION_MSE1_STATE;
2878            !!!next-input-character;
2879            redo A;
2880          } elsif ($self->{next_char} == -1) {
2881            $self->{state} = DATA_STATE;
2882            !!!next-input-character;
2883            if (length $self->{current_token}->{data}) { # character
2884              !!!cp (221.2);
2885              !!!emit ($self->{current_token}); # character
2886            } else {
2887              !!!cp (221.3);
2888              ## No token to emit. $self->{current_token} is discarded.
2889            }        
2890            redo A;
2891          } else {
2892            !!!cp (221.4);
2893            $self->{current_token}->{data} .= chr $self->{next_char};
2894            ## Stay in the state.
2895            !!!next-input-character;
2896            redo A;
2897          }
2898    
2899        CS: while ($self->{next_char} != -1) {        ## ISSUE: "text tokens" in spec.
2900          if ($self->{next_char} == 0x005D) { # ]      } elsif ($self->{state} == CDATA_SECTION_MSE1_STATE) {
2901            !!!next-input-character;        if ($self->{next_char} == 0x005D) { # ]
2902            if ($self->{next_char} == 0x005D) { # ]          !!!cp (221.5);
2903              !!!next-input-character;          $self->{state} = CDATA_SECTION_MSE2_STATE;
2904              MDC: {          !!!next-input-character;
2905                if ($self->{next_char} == 0x003E) { # >          redo A;
2906                  !!!cp (221.1);        } else {
2907                  !!!next-input-character;          !!!cp (221.6);
2908                  last CS;          $self->{current_token}->{data} .= ']';
2909                } elsif ($self->{next_char} == 0x005D) { # ]          $self->{state} = CDATA_SECTION_STATE;
2910                  !!!cp (221.2);          ## Reconsume.
2911                  $s .= ']';          redo A;
2912                  !!!next-input-character;        }
2913                  redo MDC;      } elsif ($self->{state} == CDATA_SECTION_MSE2_STATE) {
2914                } else {        if ($self->{next_char} == 0x003E) { # >
2915                  !!!cp (221.3);          $self->{state} = DATA_STATE;
2916                  $s .= ']]';          !!!next-input-character;
2917                  #          if (length $self->{current_token}->{data}) { # character
2918                }            !!!cp (221.7);
2919              } # MDC            !!!emit ($self->{current_token}); # character
           } else {  
             !!!cp (221.4);  
             $s .= ']';  
             #  
           }  
2920          } else {          } else {
2921            !!!cp (221.5);            !!!cp (221.8);
2922            #            ## No token to emit. $self->{current_token} is discarded.
2923          }          }
2924          $s .= chr $self->{next_char};          redo A;
2925          } elsif ($self->{next_char} == 0x005D) { # ]
2926            !!!cp (221.9); # character
2927            $self->{current_token}->{data} .= ']'; ## Add first "]" of "]]]".
2928            ## Stay in the state.
2929          !!!next-input-character;          !!!next-input-character;
2930        } # 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});  
2931        } else {        } else {
2932          !!!cp (221.7);          !!!cp (221.11);
2933            $self->{current_token}->{data} .= ']]'; # character
2934            $self->{state} = CDATA_SECTION_STATE;
2935            ## Reconsume.
2936            redo A;
2937          }
2938        } elsif ($self->{state} == ENTITY_STATE) {
2939          if ({
2940            0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2941            0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, &
2942            $self->{entity_additional} => 1,
2943          }->{$self->{next_char}}) {
2944            !!!cp (1001);
2945            ## Don't consume
2946            ## No error
2947            ## Return nothing.
2948            #
2949          } elsif ($self->{next_char} == 0x0023) { # #
2950            !!!cp (999);
2951            $self->{state} = ENTITY_HASH_STATE;
2952            $self->{state_keyword} = '#';
2953            !!!next-input-character;
2954            redo A;
2955          } elsif ((0x0041 <= $self->{next_char} and
2956                    $self->{next_char} <= 0x005A) or # A..Z
2957                   (0x0061 <= $self->{next_char} and
2958                    $self->{next_char} <= 0x007A)) { # a..z
2959            !!!cp (998);
2960            require Whatpm::_NamedEntityList;
2961            $self->{state} = ENTITY_NAME_STATE;
2962            $self->{state_keyword} = chr $self->{next_char};
2963            $self->{entity__value} = $self->{state_keyword};
2964            $self->{entity__match} = 0;
2965            !!!next-input-character;
2966            redo A;
2967          } else {
2968            !!!cp (1027);
2969            !!!parse-error (type => 'bare ero');
2970            ## Return nothing.
2971            #
2972        }        }
2973    
2974        redo A;        ## NOTE: No character is consumed by the "consume a character
2975          ## reference" algorithm.  In other word, there is an "&" character
2976        ## ISSUE: "text tokens" in spec.        ## that does not introduce a character reference, which would be
2977        ## TODO: Streaming support        ## appended to the parent element or the attribute value in later
2978      } else {        ## process of the tokenizer.
2979        die "$0: $self->{state}: Unknown state";  
2980      }        if ($self->{prev_state} == DATA_STATE) {
2981    } # A            !!!cp (997);
2982            $self->{state} = $self->{prev_state};
2983    die "$0: _get_next_token: unexpected case";          ## Reconsume.
2984  } # _get_next_token          !!!emit ({type => CHARACTER_TOKEN, data => '&',
2985                      line => $self->{line_prev},
2986  sub _tokenize_attempt_to_consume_an_entity ($$$) {                    column => $self->{column_prev},
2987    my ($self, $in_attr, $additional) = @_;                   });
2988            redo A;
2989    my ($l, $c) = ($self->{line_prev}, $self->{column_prev});        } else {
2990            !!!cp (996);
2991            $self->{current_attribute}->{value} .= '&';
2992            $self->{state} = $self->{prev_state};
2993            ## Reconsume.
2994            redo A;
2995          }
2996        } elsif ($self->{state} == ENTITY_HASH_STATE) {
2997          if ($self->{next_char} == 0x0078 or # x
2998              $self->{next_char} == 0x0058) { # X
2999            !!!cp (995);
3000            $self->{state} = HEXREF_X_STATE;
3001            $self->{state_keyword} .= chr $self->{next_char};
3002            !!!next-input-character;
3003            redo A;
3004          } elsif (0x0030 <= $self->{next_char} and
3005                   $self->{next_char} <= 0x0039) { # 0..9
3006            !!!cp (994);
3007            $self->{state} = NCR_NUM_STATE;
3008            $self->{state_keyword} = $self->{next_char} - 0x0030;
3009            !!!next-input-character;
3010            redo A;
3011          } else {
3012            !!!parse-error (type => 'bare nero',
3013                            line => $self->{line_prev},
3014                            column => $self->{column_prev} - 1);
3015    
3016    if ({          ## NOTE: According to the spec algorithm, nothing is returned,
3017         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,          ## and then "&#" is appended to the parent element or the attribute
3018         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR          ## value in the later processing.
3019         $additional => 1,  
3020        }->{$self->{next_char}}) {          if ($self->{prev_state} == DATA_STATE) {
3021      !!!cp (1001);            !!!cp (1019);
3022      ## Don't consume            $self->{state} = $self->{prev_state};
3023      ## No error            ## Reconsume.
3024      return undef;            !!!emit ({type => CHARACTER_TOKEN,
3025    } elsif ($self->{next_char} == 0x0023) { # #                      data => '&#',
3026      !!!next-input-character;                      line => $self->{line_prev},
3027      if ($self->{next_char} == 0x0078 or # x                      column => $self->{column_prev} - 1,
3028          $self->{next_char} == 0x0058) { # X                     });
3029        my $code;            redo A;
       X: {  
         my $x_char = $self->{next_char};  
         !!!next-input-character;  
         if (0x0030 <= $self->{next_char} and  
             $self->{next_char} <= 0x0039) { # 0..9  
           !!!cp (1002);  
           $code ||= 0;  
           $code *= 0x10;  
           $code += $self->{next_char} - 0x0030;  
           redo X;  
         } elsif (0x0061 <= $self->{next_char} and  
                  $self->{next_char} <= 0x0066) { # a..f  
           !!!cp (1003);  
           $code ||= 0;  
           $code *= 0x10;  
           $code += $self->{next_char} - 0x0060 + 9;  
           redo X;  
         } elsif (0x0041 <= $self->{next_char} and  
                  $self->{next_char} <= 0x0046) { # A..F  
           !!!cp (1004);  
           $code ||= 0;  
           $code *= 0x10;  
           $code += $self->{next_char} - 0x0040 + 9;  
           redo X;  
         } elsif (not defined $code) { # no hexadecimal digit  
           !!!cp (1005);  
           !!!parse-error (type => 'bare hcro', line => $l, column => $c);  
           !!!back-next-input-character ($x_char, $self->{next_char});  
           $self->{next_char} = 0x0023; # #  
           return undef;  
         } elsif ($self->{next_char} == 0x003B) { # ;  
           !!!cp (1006);  
           !!!next-input-character;  
3030          } else {          } else {
3031            !!!cp (1007);            !!!cp (993);
3032            !!!parse-error (type => 'no refc', line => $l, column => $c);            $self->{current_attribute}->{value} .= '&#';
3033              $self->{state} = $self->{prev_state};
3034              ## Reconsume.
3035              redo A;
3036          }          }
3037          }
3038          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {      } elsif ($self->{state} == NCR_NUM_STATE) {
3039            !!!cp (1008);        if (0x0030 <= $self->{next_char} and
3040            !!!parse-error (type => 'invalid character reference',            $self->{next_char} <= 0x0039) { # 0..9
                           text => (sprintf 'U+%04X', $code),  
                           line => $l, column => $c);  
           $code = 0xFFFD;  
         } elsif ($code > 0x10FFFF) {  
           !!!cp (1009);  
           !!!parse-error (type => 'invalid character reference',  
                           text => (sprintf 'U-%08X', $code),  
                           line => $l, column => $c);  
           $code = 0xFFFD;  
         } elsif ($code == 0x000D) {  
           !!!cp (1010);  
           !!!parse-error (type => 'CR character reference', line => $l, column => $c);  
           $code = 0x000A;  
         } elsif (0x80 <= $code and $code <= 0x9F) {  
           !!!cp (1011);  
           !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);  
           $code = $c1_entity_char->{$code};  
         }  
   
         return {type => CHARACTER_TOKEN, data => chr $code,  
                 has_reference => 1,  
                 line => $l, column => $c,  
                };  
       } # X  
     } elsif (0x0030 <= $self->{next_char} and  
              $self->{next_char} <= 0x0039) { # 0..9  
       my $code = $self->{next_char} - 0x0030;  
       !!!next-input-character;  
         
       while (0x0030 <= $self->{next_char} and  
                 $self->{next_char} <= 0x0039) { # 0..9  
3041          !!!cp (1012);          !!!cp (1012);
3042          $code *= 10;          $self->{state_keyword} *= 10;
3043          $code += $self->{next_char} - 0x0030;          $self->{state_keyword} += $self->{next_char} - 0x0030;
3044                    
3045            ## Stay in the state.
3046          !!!next-input-character;          !!!next-input-character;
3047        }          redo A;
3048          } elsif ($self->{next_char} == 0x003B) { # ;
       if ($self->{next_char} == 0x003B) { # ;  
3049          !!!cp (1013);          !!!cp (1013);
3050          !!!next-input-character;          !!!next-input-character;
3051            #
3052        } else {        } else {
3053          !!!cp (1014);          !!!cp (1014);
3054          !!!parse-error (type => 'no refc', line => $l, column => $c);          !!!parse-error (type => 'no refc');
3055            ## Reconsume.
3056            #
3057        }        }
3058    
3059          my $code = $self->{state_keyword};
3060          my $l = $self->{line_prev};
3061          my $c = $self->{column_prev};
3062        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3063          !!!cp (1015);          !!!cp (1015);
3064          !!!parse-error (type => 'invalid character reference',          !!!parse-error (type => 'invalid character reference',
# Line 2996  sub _tokenize_attempt_to_consume_an_enti Line 3083  sub _tokenize_attempt_to_consume_an_enti
3083                          line => $l, column => $c);                          line => $l, column => $c);
3084          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3085        }        }
3086          
3087        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,        if ($self->{prev_state} == DATA_STATE) {
3088                line => $l, column => $c,          !!!cp (992);
3089               };          $self->{state} = $self->{prev_state};
3090      } else {          ## Reconsume.
3091        !!!cp (1019);          !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3092        !!!parse-error (type => 'bare nero', line => $l, column => $c);                    line => $l, column => $c,
3093        !!!back-next-input-character ($self->{next_char});                   });
3094        $self->{next_char} = 0x0023; # #          redo A;
3095        return undef;        } else {
3096      }          !!!cp (991);
3097    } elsif ((0x0041 <= $self->{next_char} and          $self->{current_attribute}->{value} .= chr $code;
3098              $self->{next_char} <= 0x005A) or          $self->{current_attribute}->{has_reference} = 1;
3099             (0x0061 <= $self->{next_char} and          $self->{state} = $self->{prev_state};
3100              $self->{next_char} <= 0x007A)) {          ## Reconsume.
3101      my $entity_name = chr $self->{next_char};          redo A;
3102      !!!next-input-character;        }
3103        } elsif ($self->{state} == HEXREF_X_STATE) {
3104      my $value = $entity_name;        if ((0x0030 <= $self->{next_char} and $self->{next_char} <= 0x0039) or
3105      my $match = 0;            (0x0041 <= $self->{next_char} and $self->{next_char} <= 0x0046) or
3106      require Whatpm::_NamedEntityList;            (0x0061 <= $self->{next_char} and $self->{next_char} <= 0x0066)) {
3107      our $EntityChar;          # 0..9, A..F, a..f
3108            !!!cp (990);
3109      while (length $entity_name < 30 and          $self->{state} = HEXREF_HEX_STATE;
3110             ## NOTE: Some number greater than the maximum length of entity name          $self->{state_keyword} = 0;
3111             ((0x0041 <= $self->{next_char} and # a          ## Reconsume.
3112               $self->{next_char} <= 0x005A) or # x          redo A;
3113              (0x0061 <= $self->{next_char} and # a        } else {
3114               $self->{next_char} <= 0x007A) or # z          !!!parse-error (type => 'bare hcro',
3115              (0x0030 <= $self->{next_char} and # 0                          line => $self->{line_prev},
3116               $self->{next_char} <= 0x0039) or # 9                          column => $self->{column_prev} - 2);
3117              $self->{next_char} == 0x003B)) { # ;  
3118        $entity_name .= chr $self->{next_char};          ## NOTE: According to the spec algorithm, nothing is returned,
3119        if (defined $EntityChar->{$entity_name}) {          ## and then "&#" followed by "X" or "x" is appended to the parent
3120          if ($self->{next_char} == 0x003B) { # ;          ## element or the attribute value in the later processing.
3121            !!!cp (1020);  
3122            $value = $EntityChar->{$entity_name};          if ($self->{prev_state} == DATA_STATE) {
3123            $match = 1;            !!!cp (1005);
3124            !!!next-input-character;            $self->{state} = $self->{prev_state};
3125            last;            ## Reconsume.
3126              !!!emit ({type => CHARACTER_TOKEN,
3127                        data => '&' . $self->{state_keyword},
3128                        line => $self->{line_prev},
3129                        column => $self->{column_prev} - length $self->{state_keyword},
3130                       });
3131              redo A;
3132            } else {
3133              !!!cp (989);
3134              $self->{current_attribute}->{value} .= '&' . $self->{state_keyword};
3135              $self->{state} = $self->{prev_state};
3136              ## Reconsume.
3137              redo A;
3138            }
3139          }
3140        } elsif ($self->{state} == HEXREF_HEX_STATE) {
3141          if (0x0030 <= $self->{next_char} and $self->{next_char} <= 0x0039) {
3142            # 0..9
3143            !!!cp (1002);
3144            $self->{state_keyword} *= 0x10;
3145            $self->{state_keyword} += $self->{next_char} - 0x0030;
3146            ## Stay in the state.
3147            !!!next-input-character;
3148            redo A;
3149          } elsif (0x0061 <= $self->{next_char} and
3150                   $self->{next_char} <= 0x0066) { # a..f
3151            !!!cp (1003);
3152            $self->{state_keyword} *= 0x10;
3153            $self->{state_keyword} += $self->{next_char} - 0x0060 + 9;
3154            ## Stay in the state.
3155            !!!next-input-character;
3156            redo A;
3157          } elsif (0x0041 <= $self->{next_char} and
3158                   $self->{next_char} <= 0x0046) { # A..F
3159            !!!cp (1004);
3160            $self->{state_keyword} *= 0x10;
3161            $self->{state_keyword} += $self->{next_char} - 0x0040 + 9;
3162            ## Stay in the state.
3163            !!!next-input-character;
3164            redo A;
3165          } elsif ($self->{next_char} == 0x003B) { # ;
3166            !!!cp (1006);
3167            !!!next-input-character;
3168            #
3169          } else {
3170            !!!cp (1007);
3171            !!!parse-error (type => 'no refc',
3172                            line => $self->{line},
3173                            column => $self->{column});
3174            ## Reconsume.
3175            #
3176          }
3177    
3178          my $code = $self->{state_keyword};
3179          my $l = $self->{line_prev};
3180          my $c = $self->{column_prev};
3181          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3182            !!!cp (1008);
3183            !!!parse-error (type => 'invalid character reference',
3184                            text => (sprintf 'U+%04X', $code),
3185                            line => $l, column => $c);
3186            $code = 0xFFFD;
3187          } elsif ($code > 0x10FFFF) {
3188            !!!cp (1009);
3189            !!!parse-error (type => 'invalid character reference',
3190                            text => (sprintf 'U-%08X', $code),
3191                            line => $l, column => $c);
3192            $code = 0xFFFD;
3193          } elsif ($code == 0x000D) {
3194            !!!cp (1010);
3195            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
3196            $code = 0x000A;
3197          } elsif (0x80 <= $code and $code <= 0x9F) {
3198            !!!cp (1011);
3199            !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
3200            $code = $c1_entity_char->{$code};
3201          }
3202    
3203          if ($self->{prev_state} == DATA_STATE) {
3204            !!!cp (988);
3205            $self->{state} = $self->{prev_state};
3206            ## Reconsume.
3207            !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3208                      line => $l, column => $c,
3209                     });
3210            redo A;
3211          } else {
3212            !!!cp (987);
3213            $self->{current_attribute}->{value} .= chr $code;
3214            $self->{current_attribute}->{has_reference} = 1;
3215            $self->{state} = $self->{prev_state};
3216            ## Reconsume.
3217            redo A;
3218          }
3219        } elsif ($self->{state} == ENTITY_NAME_STATE) {
3220          if (length $self->{state_keyword} < 30 and
3221              ## NOTE: Some number greater than the maximum length of entity name
3222              ((0x0041 <= $self->{next_char} and # a
3223                $self->{next_char} <= 0x005A) or # x
3224               (0x0061 <= $self->{next_char} and # a
3225                $self->{next_char} <= 0x007A) or # z
3226               (0x0030 <= $self->{next_char} and # 0
3227                $self->{next_char} <= 0x0039) or # 9
3228               $self->{next_char} == 0x003B)) { # ;
3229            our $EntityChar;
3230            $self->{state_keyword} .= chr $self->{next_char};
3231            if (defined $EntityChar->{$self->{state_keyword}}) {
3232              if ($self->{next_char} == 0x003B) { # ;
3233                !!!cp (1020);
3234                $self->{entity__value} = $EntityChar->{$self->{state_keyword}};
3235                $self->{entity__match} = 1;
3236                !!!next-input-character;
3237                #
3238              } else {
3239                !!!cp (1021);
3240                $self->{entity__value} = $EntityChar->{$self->{state_keyword}};
3241                $self->{entity__match} = -1;
3242                ## Stay in the state.
3243                !!!next-input-character;
3244                redo A;
3245              }
3246          } else {          } else {
3247            !!!cp (1021);            !!!cp (1022);
3248            $value = $EntityChar->{$entity_name};            $self->{entity__value} .= chr $self->{next_char};
3249            $match = -1;            $self->{entity__match} *= 2;
3250              ## Stay in the state.
3251            !!!next-input-character;            !!!next-input-character;
3252              redo A;
3253            }
3254          }
3255    
3256          my $data;
3257          my $has_ref;
3258          if ($self->{entity__match} > 0) {
3259            !!!cp (1023);
3260            $data = $self->{entity__value};
3261            $has_ref = 1;
3262            #
3263          } elsif ($self->{entity__match} < 0) {
3264            !!!parse-error (type => 'no refc');
3265            if ($self->{prev_state} != DATA_STATE and # in attribute
3266                $self->{entity__match} < -1) {
3267              !!!cp (1024);
3268              $data = '&' . $self->{state_keyword};
3269              #
3270            } else {
3271              !!!cp (1025);
3272              $data = $self->{entity__value};
3273              $has_ref = 1;
3274              #
3275          }          }
3276        } else {        } else {
3277          !!!cp (1022);          !!!cp (1026);
3278          $value .= chr $self->{next_char};          !!!parse-error (type => 'bare ero',
3279          $match *= 2;                          line => $self->{line_prev},
3280          !!!next-input-character;                          column => $self->{column_prev});
3281            $data = '&' . $self->{state_keyword};
3282            #
3283        }        }
3284      }    
3285              ## NOTE: In these cases, when a character reference is found,
3286      if ($match > 0) {        ## it is consumed and a character token is returned, or, otherwise,
3287        !!!cp (1023);        ## nothing is consumed and returned, according to the spec algorithm.
3288        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,        ## In this implementation, anything that has been examined by the
3289                line => $l, column => $c,        ## tokenizer is appended to the parent element or the attribute value
3290               };        ## as string, either literal string when no character reference or
3291      } elsif ($match < 0) {        ## entity-replaced string otherwise, in this stage, since any characters
3292        !!!parse-error (type => 'no refc', line => $l, column => $c);        ## that would not be consumed are appended in the data state or in an
3293        if ($in_attr and $match < -1) {        ## appropriate attribute value state anyway.
3294          !!!cp (1024);  
3295          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,        if ($self->{prev_state} == DATA_STATE) {
3296                  line => $l, column => $c,          !!!cp (986);
3297                 };          $self->{state} = $self->{prev_state};
3298        } else {          ## Reconsume.
3299          !!!cp (1025);          !!!emit ({type => CHARACTER_TOKEN,
3300          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,                    data => $data,
3301                  line => $l, column => $c,                    line => $self->{line_prev},
3302                 };                    column => $self->{column_prev} + 1 - length $self->{state_keyword},
3303                     });
3304            redo A;
3305          } else {
3306            !!!cp (985);
3307            $self->{current_attribute}->{value} .= $data;
3308            $self->{current_attribute}->{has_reference} = 1 if $has_ref;
3309            $self->{state} = $self->{prev_state};
3310            ## Reconsume.
3311            redo A;
3312        }        }
3313      } else {      } else {
3314        !!!cp (1026);        die "$0: $self->{state}: Unknown state";
       !!!parse-error (type => 'bare ero', line => $l, column => $c);  
       ## NOTE: "No characters are consumed" in the spec.  
       return {type => CHARACTER_TOKEN, data => '&'.$value,  
               line => $l, column => $c,  
              };  
3315      }      }
3316    } else {    } # A  
3317      !!!cp (1027);  
3318      ## no characters are consumed    die "$0: _get_next_token: unexpected case";
3319      !!!parse-error (type => 'bare ero', line => $l, column => $c);  } # _get_next_token
     return undef;  
   }  
 } # _tokenize_attempt_to_consume_an_entity  
3320    
3321  sub _initialize_tree_constructor ($) {  sub _initialize_tree_constructor ($) {
3322    my $self = shift;    my $self = shift;
# Line 3147  sub _tree_construction_initial ($) { Line 3381  sub _tree_construction_initial ($) {
3381        ## language.        ## language.
3382        my $doctype_name = $token->{name};        my $doctype_name = $token->{name};
3383        $doctype_name = '' unless defined $doctype_name;        $doctype_name = '' unless defined $doctype_name;
3384        $doctype_name =~ tr/a-z/A-Z/;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3385        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
           defined $token->{public_identifier} or  
3386            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3387          !!!cp ('t1');          !!!cp ('t1');
3388          !!!parse-error (type => 'not HTML5', token => $token);          !!!parse-error (type => 'not HTML5', token => $token);
3389        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3390          !!!cp ('t2');          !!!cp ('t2');
         ## ISSUE: ASCII case-insensitive? (in fact it does not matter)  
3391          !!!parse-error (type => 'not HTML5', token => $token);          !!!parse-error (type => 'not HTML5', token => $token);
3392          } elsif (defined $token->{public_identifier}) {
3393            if ($token->{public_identifier} eq 'XSLT-compat') {
3394              !!!cp ('t1.2');
3395              !!!parse-error (type => 'XSLT-compat', token => $token,
3396                              level => $self->{level}->{should});
3397            } else {
3398              !!!parse-error (type => 'not HTML5', token => $token);
3399            }
3400        } else {        } else {
3401          !!!cp ('t3');          !!!cp ('t3');
3402            #
3403        }        }
3404                
3405        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
# Line 6294  sub _tree_construction_main ($) { Line 6535  sub _tree_construction_main ($) {
6535            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6536              !!!cp ('t312');              !!!cp ('t312');
6537              !!!parse-error (type => 'after frameset:#text', token => $token);              !!!parse-error (type => 'after frameset:#text', token => $token);
6538            } else { # "after html frameset"            } else { # "after after frameset"
6539              !!!cp ('t313');              !!!cp ('t313');
6540              !!!parse-error (type => 'after html:#text', 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:#text', token => $token);  
6541            }            }
6542                        
6543            ## Ignore the token.            ## Ignore the token.
# Line 6316  sub _tree_construction_main ($) { Line 6553  sub _tree_construction_main ($) {
6553                    
6554          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6555        } 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',  
                           text => $token->{tag_name}, token => $token);  
   
           $self->{insertion_mode} = AFTER_FRAMESET_IM;  
           ## Process in the "after frameset" insertion mode.  
         } else {  
           !!!cp ('t317');  
         }  
   
6556          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6557              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6558            !!!cp ('t318');            !!!cp ('t318');
# Line 6347  sub _tree_construction_main ($) { Line 6573  sub _tree_construction_main ($) {
6573            ## NOTE: As if in head.            ## NOTE: As if in head.
6574            $parse_rcdata->(CDATA_CONTENT_MODEL);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6575            next B;            next B;
6576    
6577              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6578              ## has no parse error.
6579          } else {          } else {
6580            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6581              !!!cp ('t321');              !!!cp ('t321');
6582              !!!parse-error (type => 'in frameset',              !!!parse-error (type => 'in frameset',
6583                              text => $token->{tag_name}, token => $token);                              text => $token->{tag_name}, token => $token);
6584            } else {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6585              !!!cp ('t322');              !!!cp ('t322');
6586              !!!parse-error (type => 'after frameset',              !!!parse-error (type => 'after frameset',
6587                              text => $token->{tag_name}, token => $token);                              text => $token->{tag_name}, token => $token);
6588              } else { # "after after frameset"
6589                !!!cp ('t322.2');
6590                !!!parse-error (type => 'after after frameset',
6591                                text => $token->{tag_name}, token => $token);
6592            }            }
6593            ## Ignore the token            ## Ignore the token
6594            !!!nack ('t322.1');            !!!nack ('t322.1');
# Line 6363  sub _tree_construction_main ($) { Line 6596  sub _tree_construction_main ($) {
6596            next B;            next B;
6597          }          }
6598        } 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:/',  
                           text => $token->{tag_name}, token => $token);  
   
           $self->{insertion_mode} = AFTER_FRAMESET_IM;  
           ## Process in the "after frameset" insertion mode.  
         } else {  
           !!!cp ('t324');  
         }  
   
6599          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6600              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6601            if ($self->{open_elements}->[-1]->[1] & HTML_EL and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
# Line 6408  sub _tree_construction_main ($) { Line 6630  sub _tree_construction_main ($) {
6630              !!!cp ('t330');              !!!cp ('t330');
6631              !!!parse-error (type => 'in frameset:/',              !!!parse-error (type => 'in frameset:/',
6632                              text => $token->{tag_name}, token => $token);                              text => $token->{tag_name}, token => $token);
6633            } else {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6634              !!!cp ('t331');              !!!cp ('t330.1');
6635              !!!parse-error (type => 'after frameset:/',              !!!parse-error (type => 'after frameset:/',
6636                              text => $token->{tag_name}, token => $token);                              text => $token->{tag_name}, token => $token);
6637              } else { # "after after html"
6638                !!!cp ('t331');
6639                !!!parse-error (type => 'after after frameset:/',
6640                                text => $token->{tag_name}, token => $token);
6641            }            }
6642            ## Ignore the token            ## Ignore the token
6643            !!!next-token;            !!!next-token;
# Line 6948  sub _tree_construction_main ($) { Line 7174  sub _tree_construction_main ($) {
7174                 $token->{tag_name} eq 'svg') {                 $token->{tag_name} eq 'svg') {
7175          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7176    
7177            ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
7178    
7179          ## "adjust SVG attributes" ('svg' only) - done in insert-element-f          ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
7180    
7181          ## "adjust foreign attributes" - done in insert-element-f          ## "adjust foreign attributes" - done in insert-element-f
# Line 7132  sub _tree_construction_main ($) { Line 7360  sub _tree_construction_main ($) {
7360            !!!cp ('t413');            !!!cp ('t413');
7361            !!!parse-error (type => 'unmatched end tag',            !!!parse-error (type => 'unmatched end tag',
7362                            text => $token->{tag_name}, token => $token);                            text => $token->{tag_name}, token => $token);
7363              ## NOTE: Ignore the token.
7364          } else {          } else {
7365            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7366            while ({            while ({
# Line 7191  sub _tree_construction_main ($) { Line 7420  sub _tree_construction_main ($) {
7420            !!!cp ('t421');            !!!cp ('t421');
7421            !!!parse-error (type => 'unmatched end tag',            !!!parse-error (type => 'unmatched end tag',
7422                            text => $token->{tag_name}, token => $token);                            text => $token->{tag_name}, token => $token);
7423              ## NOTE: Ignore the token.
7424          } else {          } else {
7425            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7426            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
# Line 7237  sub _tree_construction_main ($) { Line 7467  sub _tree_construction_main ($) {
7467            !!!cp ('t425.1');            !!!cp ('t425.1');
7468            !!!parse-error (type => 'unmatched end tag',            !!!parse-error (type => 'unmatched end tag',
7469                            text => $token->{tag_name}, token => $token);                            text => $token->{tag_name}, token => $token);
7470              ## NOTE: Ignore the token.
7471          } else {          } else {
7472            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7473            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
# Line 7439  sub _tree_construction_main ($) { Line 7670  sub _tree_construction_main ($) {
7670    ## TODO: script stuffs    ## TODO: script stuffs
7671  } # _tree_construct_main  } # _tree_construct_main
7672    
7673  sub set_inner_html ($$$) {  sub set_inner_html ($$$;$) {
7674    my $class = shift;    my $class = shift;
7675    my $node = shift;    my $node = shift;
7676    my $s = \$_[0];    my $s = \$_[0];
7677    my $onerror = $_[1];    my $onerror = $_[1];
7678      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7679    
7680    ## ISSUE: Should {confident} be true?    ## ISSUE: Should {confident} be true?
7681    
# Line 7462  sub set_inner_html ($$$) { Line 7694  sub set_inner_html ($$$) {
7694      }      }
7695    
7696      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7697      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($$s => $node, $onerror, $get_wrapper);
7698    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7699      ## TODO: If non-html element      ## TODO: If non-html element
7700    
7701      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7702    
7703    ## TODO: Support for $get_wrapper
7704    
7705      ## Step 1 # MUST      ## Step 1 # MUST
7706      my $this_doc = $node->owner_document;      my $this_doc = $node->owner_document;
7707      my $doc = $this_doc->implementation->create_document;      my $doc = $this_doc->implementation->create_document;

Legend:
Removed from v.1.154  
changed lines
  Added in v.1.170

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24