/[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.56 by wakaba, Sat Aug 11 07:19:18 2007 UTC revision 1.71 by wakaba, Sun Mar 2 03:39:41 2008 UTC
# Line 1  Line 1 
1  package Whatpm::HTML;  package Whatpm::HTML;
2  use strict;  use strict;
3  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4    use Error qw(:try);
5    
6  ## ISSUE:  ## ISSUE:
7  ## var doc = implementation.createDocument (null, null, null);  ## var doc = implementation.createDocument (null, null, null);
8  ## doc.write ('');  ## doc.write ('');
9  ## alert (doc.compatMode);  ## alert (doc.compatMode);
10    
11  ## ISSUE: HTML5 revision 967 says that the encoding layer MUST NOT  ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)
12  ## strip BOM and the HTML layer MUST ignore it.  Whether we can do it  ## TODO: 1252 parse error (revision 1264)
13  ## is not yet clear.  ## TODO: 8859-11 = 874 (revision 1271)
 ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?  
 ## "{U+FEFF}..." in GB18030?  
14    
15  my $permitted_slash_tag_name = {  my $permitted_slash_tag_name = {
16    base => 1,    base => 1,
# Line 19  my $permitted_slash_tag_name = { Line 18  my $permitted_slash_tag_name = {
18    meta => 1,    meta => 1,
19    hr => 1,    hr => 1,
20    br => 1,    br => 1,
21    img=> 1,    img => 1,
22    embed => 1,    embed => 1,
23    param => 1,    param => 1,
24    area => 1,    area => 1,
# Line 84  my $formatting_category = { Line 83  my $formatting_category = {
83  };  };
84  # $phrasing_category: all other elements  # $phrasing_category: all other elements
85    
86    sub parse_byte_string ($$$$;$) {
87      my $self = ref $_[0] ? shift : shift->new;
88      my $charset = shift;
89      my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);
90      my $s;
91      
92      if (defined $charset) {
93        require Encode; ## TODO: decode(utf8) don't delete BOM
94        $s = \ (Encode::decode ($charset, $$bytes_s));
95        $self->{input_encoding} = lc $charset; ## TODO: normalize name
96        $self->{confident} = 1;
97      } else {
98        ## TODO: Implement HTML5 detection algorithm
99        require Whatpm::Charset::UniversalCharDet;
100        $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string
101            (substr ($$bytes_s, 0, 1024));
102        $charset ||= 'windows-1252';
103        $s = \ (Encode::decode ($charset, $$bytes_s));
104        $self->{input_encoding} = $charset;
105        $self->{confident} = 0;
106      }
107    
108      $self->{change_encoding} = sub {
109        my $self = shift;
110        my $charset = lc shift;
111        ## TODO: if $charset is supported
112        ## TODO: normalize charset name
113    
114        ## "Change the encoding" algorithm:
115    
116        ## Step 1    
117        if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
118          $charset = 'utf-8';
119        }
120    
121        ## Step 2
122        if (defined $self->{input_encoding} and
123            $self->{input_encoding} eq $charset) {
124          $self->{confident} = 1;
125          return;
126        }
127    
128        !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
129            ':'.$charset, level => 'w');
130    
131        ## Step 3
132        # if (can) {
133          ## change the encoding on the fly.
134          #$self->{confident} = 1;
135          #return;
136        # }
137    
138        ## Step 4
139        throw Whatpm::HTML::RestartParser (charset => $charset);
140      }; # $self->{change_encoding}
141    
142      my @args = @_; shift @args; # $s
143      my $return;
144      try {
145        $return = $self->parse_char_string ($s, @args);  
146      } catch Whatpm::HTML::RestartParser with {
147        my $charset = shift->{charset};
148        $s = \ (Encode::decode ($charset, $$bytes_s));    
149        $self->{input_encoding} = $charset; ## TODO: normalize
150        $self->{confident} = 1;
151        $return = $self->parse_char_string ($s, @args);
152      };
153      return $return;
154    } # parse_byte_string
155    
156    ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
157    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
158    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
159    ## because the core part of our HTML parser expects a string of character,
160    ## not a string of bytes or code units or anything which might contain a BOM.
161    ## Therefore, any parser interface that accepts a string of bytes,
162    ## such as |parse_byte_string| in this module, must ensure that it does
163    ## strip the BOM and never strip any ZWNBSP.
164    
165    *parse_char_string = \&parse_string;
166    
167  sub parse_string ($$$;$) {  sub parse_string ($$$;$) {
168    my $self = shift->new;    my $self = ref $_[0] ? shift : shift->new;
169    my $s = \$_[0];    my $s = ref $_[0] ? $_[0] : \($_[0]);
170    $self->{document} = $_[1];    $self->{document} = $_[1];
171      @{$self->{document}->child_nodes} = ();
172    
173    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
174    
175      $self->{confident} = 1 unless exists $self->{confident};
176      $self->{document}->input_encoding ($self->{input_encoding})
177          if defined $self->{input_encoding};
178    
179    my $i = 0;    my $i = 0;
180    my $line = 1;    my $line = 1;
181    my $column = 0;    my $column = 0;
# Line 147  sub new ($) { Line 232  sub new ($) {
232    $self->{parse_error} = sub {    $self->{parse_error} = sub {
233      #      #
234    };    };
235      $self->{change_encoding} = sub {
236        # if ($_[0] is a supported encoding) {
237        #   run "change the encoding" algorithm;
238        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
239        # }
240      };
241      $self->{application_cache_selection} = sub {
242        #
243      };
244    return $self;    return $self;
245  } # new  } # new
246    
# Line 159  sub CDATA_CONTENT_MODEL () { CM_LIMITED_ Line 253  sub CDATA_CONTENT_MODEL () { CM_LIMITED_
253  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
254  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
255    
256    sub DATA_STATE () { 0 }
257    sub ENTITY_DATA_STATE () { 1 }
258    sub TAG_OPEN_STATE () { 2 }
259    sub CLOSE_TAG_OPEN_STATE () { 3 }
260    sub TAG_NAME_STATE () { 4 }
261    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
262    sub ATTRIBUTE_NAME_STATE () { 6 }
263    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
264    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
265    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
266    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
267    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
268    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
269    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
270    sub COMMENT_START_STATE () { 14 }
271    sub COMMENT_START_DASH_STATE () { 15 }
272    sub COMMENT_STATE () { 16 }
273    sub COMMENT_END_STATE () { 17 }
274    sub COMMENT_END_DASH_STATE () { 18 }
275    sub BOGUS_COMMENT_STATE () { 19 }
276    sub DOCTYPE_STATE () { 20 }
277    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
278    sub DOCTYPE_NAME_STATE () { 22 }
279    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
280    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
281    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
282    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
283    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
284    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
285    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
286    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
287    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
288    sub BOGUS_DOCTYPE_STATE () { 32 }
289    
290  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
291  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
292  sub START_TAG_TOKEN () { 3 }  sub START_TAG_TOKEN () { 3 }
# Line 197  sub IN_COLUMN_GROUP_IM () { 0b10 } Line 325  sub IN_COLUMN_GROUP_IM () { 0b10 }
325    
326  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
327    my $self = shift;    my $self = shift;
328    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
329    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
330    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
331    undef $self->{current_attribute};    undef $self->{current_attribute};
# Line 219  sub _initialize_tokenizer ($) { Line 347  sub _initialize_tokenizer ($) {
347  ##   ->{system_identifier} (DOCTYPE_TOKEN)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
348  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)
349  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
350    ##        ->{name}
351    ##        ->{value}
352    ##        ->{has_reference} == 1 or 0
353  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
354    
355  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
# Line 229  sub _initialize_tokenizer ($) { Line 360  sub _initialize_tokenizer ($) {
360  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
361  ## and removed from the list.  ## and removed from the list.
362    
363    ## NOTE: HTML5 "Writing HTML documents" section, applied to
364    ## documents and not to user agents and conformance checkers,
365    ## contains some requirements that are not detected by the
366    ## parsing algorithm:
367    ## - Some requirements on character encoding declarations. ## TODO
368    ## - "Elements MUST NOT contain content that their content model disallows."
369    ##   ... Some are parse error, some are not (will be reported by c.c.).
370    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
371    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
372    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
373    
374    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
375    ## be detected by the HTML5 parsing algorithm:
376    ## - Text,
377    
378  sub _get_next_token ($) {  sub _get_next_token ($) {
379    my $self = shift;    my $self = shift;
380    if (@{$self->{token}}) {    if (@{$self->{token}}) {
# Line 236  sub _get_next_token ($) { Line 382  sub _get_next_token ($) {
382    }    }
383    
384    A: {    A: {
385      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
386        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_input_character} == 0x0026) { # &
387          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA
388            $self->{state} = 'entity data';            $self->{state} = ENTITY_DATA_STATE;
389            !!!next-input-character;            !!!next-input-character;
390            redo A;            redo A;
391          } else {          } else {
# Line 261  sub _get_next_token ($) { Line 407  sub _get_next_token ($) {
407          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
408              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
409               not $self->{escape})) {               not $self->{escape})) {
410            $self->{state} = 'tag open';            $self->{state} = TAG_OPEN_STATE;
411            !!!next-input-character;            !!!next-input-character;
412            redo A;            redo A;
413          } else {          } else {
# Line 290  sub _get_next_token ($) { Line 436  sub _get_next_token ($) {
436        !!!emit ($token);        !!!emit ($token);
437    
438        redo A;        redo A;
439      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
440        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
441                
442        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);
443    
444        $self->{state} = 'data';        $self->{state} = DATA_STATE;
445        # next-input-character is already done        # next-input-character is already done
446    
447        unless (defined $token) {        unless (defined $token) {
# Line 305  sub _get_next_token ($) { Line 451  sub _get_next_token ($) {
451        }        }
452    
453        redo A;        redo A;
454      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
455        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
456          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_input_character} == 0x002F) { # /
457            !!!next-input-character;            !!!next-input-character;
458            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
459            redo A;            redo A;
460          } else {          } else {
461            ## reconsume            ## reconsume
462            $self->{state} = 'data';            $self->{state} = DATA_STATE;
463    
464            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
465    
# Line 321  sub _get_next_token ($) { Line 467  sub _get_next_token ($) {
467          }          }
468        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
469          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_input_character} == 0x0021) { # !
470            $self->{state} = 'markup declaration open';            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
471            !!!next-input-character;            !!!next-input-character;
472            redo A;            redo A;
473          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_input_character} == 0x002F) { # /
474            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
475            !!!next-input-character;            !!!next-input-character;
476            redo A;            redo A;
477          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
# Line 333  sub _get_next_token ($) { Line 479  sub _get_next_token ($) {
479            $self->{current_token}            $self->{current_token}
480              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
481                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_input_character} + 0x0020)};
482            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
483            !!!next-input-character;            !!!next-input-character;
484            redo A;            redo A;
485          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
486                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_input_character} <= 0x007A) { # a..z
487            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
488                              tag_name => chr ($self->{next_input_character})};                              tag_name => chr ($self->{next_input_character})};
489            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
490            !!!next-input-character;            !!!next-input-character;
491            redo A;            redo A;
492          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_input_character} == 0x003E) { # >
493            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag');
494            $self->{state} = 'data';            $self->{state} = DATA_STATE;
495            !!!next-input-character;            !!!next-input-character;
496    
497            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});
# Line 353  sub _get_next_token ($) { Line 499  sub _get_next_token ($) {
499            redo A;            redo A;
500          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_input_character} == 0x003F) { # ?
501            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio');
502            $self->{state} = 'bogus comment';            $self->{state} = BOGUS_COMMENT_STATE;
503            ## $self->{next_input_character} is intentionally left as is            ## $self->{next_input_character} is intentionally left as is
504            redo A;            redo A;
505          } else {          } else {
506            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
507            $self->{state} = 'data';            $self->{state} = DATA_STATE;
508            ## reconsume            ## reconsume
509    
510            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
# Line 368  sub _get_next_token ($) { Line 514  sub _get_next_token ($) {
514        } else {        } else {
515          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
516        }        }
517      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
518        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
519          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
520            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
# Line 383  sub _get_next_token ($) { Line 529  sub _get_next_token ($) {
529              } else {              } else {
530                $self->{next_input_character} = shift @next_char; # reconsume                $self->{next_input_character} = shift @next_char; # reconsume
531                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
532                $self->{state} = 'data';                $self->{state} = DATA_STATE;
533    
534                !!!emit ({type => CHARACTER_TOKEN, data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</'});
535        
# Line 402  sub _get_next_token ($) { Line 548  sub _get_next_token ($) {
548                    $self->{next_input_character} == -1) {                    $self->{next_input_character} == -1) {
549              $self->{next_input_character} = shift @next_char; # reconsume              $self->{next_input_character} = shift @next_char; # reconsume
550              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
551              $self->{state} = 'data';              $self->{state} = DATA_STATE;
552              !!!emit ({type => CHARACTER_TOKEN, data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</'});
553              redo A;              redo A;
554            } else {            } else {
# Line 413  sub _get_next_token ($) { Line 559  sub _get_next_token ($) {
559          } else {          } else {
560            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
561            # next-input-character is already done            # next-input-character is already done
562            $self->{state} = 'data';            $self->{state} = DATA_STATE;
563            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</'});
564            redo A;            redo A;
565          }          }
# Line 423  sub _get_next_token ($) { Line 569  sub _get_next_token ($) {
569            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_input_character} <= 0x005A) { # A..Z
570          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
571                            tag_name => chr ($self->{next_input_character} + 0x0020)};                            tag_name => chr ($self->{next_input_character} + 0x0020)};
572          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
573          !!!next-input-character;          !!!next-input-character;
574          redo A;          redo A;
575        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_input_character} and
576                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_input_character} <= 0x007A) { # a..z
577          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
578                            tag_name => chr ($self->{next_input_character})};                            tag_name => chr ($self->{next_input_character})};
579          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
580          !!!next-input-character;          !!!next-input-character;
581          redo A;          redo A;
582        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
583          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag');
584          $self->{state} = 'data';          $self->{state} = DATA_STATE;
585          !!!next-input-character;          !!!next-input-character;
586          redo A;          redo A;
587        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
588          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
589          $self->{state} = 'data';          $self->{state} = DATA_STATE;
590          # reconsume          # reconsume
591    
592          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</'});
# Line 448  sub _get_next_token ($) { Line 594  sub _get_next_token ($) {
594          redo A;          redo A;
595        } else {        } else {
596          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
597          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
598          ## $self->{next_input_character} is intentionally left as is          ## $self->{next_input_character} is intentionally left as is
599          redo A;          redo A;
600        }        }
601      } elsif ($self->{state} eq 'tag name') {      } elsif ($self->{state} == TAG_NAME_STATE) {
602        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
603            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
604            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
605            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
606            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
607          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
608          !!!next-input-character;          !!!next-input-character;
609          redo A;          redo A;
610        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
# Line 474  sub _get_next_token ($) { Line 620  sub _get_next_token ($) {
620          } else {          } else {
621            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
622          }          }
623          $self->{state} = 'data';          $self->{state} = DATA_STATE;
624          !!!next-input-character;          !!!next-input-character;
625    
626          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 501  sub _get_next_token ($) { Line 647  sub _get_next_token ($) {
647          } else {          } else {
648            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
649          }          }
650          $self->{state} = 'data';          $self->{state} = DATA_STATE;
651          # reconsume          # reconsume
652    
653          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 517  sub _get_next_token ($) { Line 663  sub _get_next_token ($) {
663          } else {          } else {
664            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
665          }          }
666          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
667          # next-input-character is already done          # next-input-character is already done
668          redo A;          redo A;
669        } else {        } else {
# Line 527  sub _get_next_token ($) { Line 673  sub _get_next_token ($) {
673          !!!next-input-character;          !!!next-input-character;
674          redo A;          redo A;
675        }        }
676      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
677        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
678            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
679            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 549  sub _get_next_token ($) { Line 695  sub _get_next_token ($) {
695          } else {          } else {
696            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
697          }          }
698          $self->{state} = 'data';          $self->{state} = DATA_STATE;
699          !!!next-input-character;          !!!next-input-character;
700    
701          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 559  sub _get_next_token ($) { Line 705  sub _get_next_token ($) {
705                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
706          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
707                                value => ''};                                value => ''};
708          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
709          !!!next-input-character;          !!!next-input-character;
710          redo A;          redo A;
711        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
# Line 589  sub _get_next_token ($) { Line 735  sub _get_next_token ($) {
735          } else {          } else {
736            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
737          }          }
738          $self->{state} = 'data';          $self->{state} = DATA_STATE;
739          # reconsume          # reconsume
740    
741          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 598  sub _get_next_token ($) { Line 744  sub _get_next_token ($) {
744        } else {        } else {
745          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
746                                value => ''};                                value => ''};
747          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
748          !!!next-input-character;          !!!next-input-character;
749          redo A;          redo A;
750        }        }
751      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
752        my $before_leave = sub {        my $before_leave = sub {
753          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
754              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
# Line 620  sub _get_next_token ($) { Line 766  sub _get_next_token ($) {
766            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
767            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
768          $before_leave->();          $before_leave->();
769          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
770          !!!next-input-character;          !!!next-input-character;
771          redo A;          redo A;
772        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
773          $before_leave->();          $before_leave->();
774          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
775          !!!next-input-character;          !!!next-input-character;
776          redo A;          redo A;
777        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
# Line 642  sub _get_next_token ($) { Line 788  sub _get_next_token ($) {
788          } else {          } else {
789            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
790          }          }
791          $self->{state} = 'data';          $self->{state} = DATA_STATE;
792          !!!next-input-character;          !!!next-input-character;
793    
794          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 665  sub _get_next_token ($) { Line 811  sub _get_next_token ($) {
811          } else {          } else {
812            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
813          }          }
814          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
815          # next-input-character is already done          # next-input-character is already done
816          redo A;          redo A;
817        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
# Line 683  sub _get_next_token ($) { Line 829  sub _get_next_token ($) {
829          } else {          } else {
830            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
831          }          }
832          $self->{state} = 'data';          $self->{state} = DATA_STATE;
833          # reconsume          # reconsume
834    
835          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 695  sub _get_next_token ($) { Line 841  sub _get_next_token ($) {
841          !!!next-input-character;          !!!next-input-character;
842          redo A;          redo A;
843        }        }
844      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
845        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
846            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
847            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 705  sub _get_next_token ($) { Line 851  sub _get_next_token ($) {
851          !!!next-input-character;          !!!next-input-character;
852          redo A;          redo A;
853        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
854          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
855          !!!next-input-character;          !!!next-input-character;
856          redo A;          redo A;
857        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
# Line 721  sub _get_next_token ($) { Line 867  sub _get_next_token ($) {
867          } else {          } else {
868            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
869          }          }
870          $self->{state} = 'data';          $self->{state} = DATA_STATE;
871          !!!next-input-character;          !!!next-input-character;
872    
873          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 731  sub _get_next_token ($) { Line 877  sub _get_next_token ($) {
877                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
878          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
879                                value => ''};                                value => ''};
880          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
881          !!!next-input-character;          !!!next-input-character;
882          redo A;          redo A;
883        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
# Line 745  sub _get_next_token ($) { Line 891  sub _get_next_token ($) {
891            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
892            ## TODO: Different error type for <aa / bb> than <aa/>            ## TODO: Different error type for <aa / bb> than <aa/>
893          }          }
894          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
895          # next-input-character is already done          # next-input-character is already done
896          redo A;          redo A;
897        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
# Line 762  sub _get_next_token ($) { Line 908  sub _get_next_token ($) {
908          } else {          } else {
909            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
910          }          }
911          $self->{state} = 'data';          $self->{state} = DATA_STATE;
912          # reconsume          # reconsume
913    
914          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 771  sub _get_next_token ($) { Line 917  sub _get_next_token ($) {
917        } else {        } else {
918          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
919                                value => ''};                                value => ''};
920          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
921          !!!next-input-character;          !!!next-input-character;
922          redo A;                  redo A;        
923        }        }
924      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
925        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
926            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
927            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 785  sub _get_next_token ($) { Line 931  sub _get_next_token ($) {
931          !!!next-input-character;          !!!next-input-character;
932          redo A;          redo A;
933        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
934          $self->{state} = 'attribute value (double-quoted)';          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
935          !!!next-input-character;          !!!next-input-character;
936          redo A;          redo A;
937        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
938          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
939          ## reconsume          ## reconsume
940          redo A;          redo A;
941        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
942          $self->{state} = 'attribute value (single-quoted)';          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
943          !!!next-input-character;          !!!next-input-character;
944          redo A;          redo A;
945        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
# Line 809  sub _get_next_token ($) { Line 955  sub _get_next_token ($) {
955          } else {          } else {
956            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
957          }          }
958          $self->{state} = 'data';          $self->{state} = DATA_STATE;
959          !!!next-input-character;          !!!next-input-character;
960    
961          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 829  sub _get_next_token ($) { Line 975  sub _get_next_token ($) {
975          } else {          } else {
976            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
977          }          }
978          $self->{state} = 'data';          $self->{state} = DATA_STATE;
979          ## reconsume          ## reconsume
980    
981          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 837  sub _get_next_token ($) { Line 983  sub _get_next_token ($) {
983          redo A;          redo A;
984        } else {        } else {
985          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});
986          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
987          !!!next-input-character;          !!!next-input-character;
988          redo A;          redo A;
989        }        }
990      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
991        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
992          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
993          !!!next-input-character;          !!!next-input-character;
994          redo A;          redo A;
995        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
996          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          $self->{last_attribute_value_state} = $self->{state};
997          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
998          !!!next-input-character;          !!!next-input-character;
999          redo A;          redo A;
1000        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
# Line 865  sub _get_next_token ($) { Line 1011  sub _get_next_token ($) {
1011          } else {          } else {
1012            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1013          }          }
1014          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1015          ## reconsume          ## reconsume
1016    
1017          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 877  sub _get_next_token ($) { Line 1023  sub _get_next_token ($) {
1023          !!!next-input-character;          !!!next-input-character;
1024          redo A;          redo A;
1025        }        }
1026      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1027        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1028          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1029          !!!next-input-character;          !!!next-input-character;
1030          redo A;          redo A;
1031        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
1032          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          $self->{last_attribute_value_state} = $self->{state};
1033          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1034          !!!next-input-character;          !!!next-input-character;
1035          redo A;          redo A;
1036        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
# Line 901  sub _get_next_token ($) { Line 1047  sub _get_next_token ($) {
1047          } else {          } else {
1048            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1049          }          }
1050          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1051          ## reconsume          ## reconsume
1052    
1053          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 913  sub _get_next_token ($) { Line 1059  sub _get_next_token ($) {
1059          !!!next-input-character;          !!!next-input-character;
1060          redo A;          redo A;
1061        }        }
1062      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1063        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1064            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1065            $self->{next_input_character} == 0x000B or # HT            $self->{next_input_character} == 0x000B or # HT
1066            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1067            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1068          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1069          !!!next-input-character;          !!!next-input-character;
1070          redo A;          redo A;
1071        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
1072          $self->{last_attribute_value_state} = 'attribute value (unquoted)';          $self->{last_attribute_value_state} = $self->{state};
1073          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1074          !!!next-input-character;          !!!next-input-character;
1075          redo A;          redo A;
1076        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
# Line 940  sub _get_next_token ($) { Line 1086  sub _get_next_token ($) {
1086          } else {          } else {
1087            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1088          }          }
1089          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1090          !!!next-input-character;          !!!next-input-character;
1091    
1092          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 960  sub _get_next_token ($) { Line 1106  sub _get_next_token ($) {
1106          } else {          } else {
1107            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1108          }          }
1109          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1110          ## reconsume          ## reconsume
1111    
1112          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 972  sub _get_next_token ($) { Line 1118  sub _get_next_token ($) {
1118          !!!next-input-character;          !!!next-input-character;
1119          redo A;          redo A;
1120        }        }
1121      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1122        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);
1123    
1124        unless (defined $token) {        unless (defined $token) {
1125          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1126        } else {        } else {
1127          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1128            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1129          ## ISSUE: spec says "append the returned character token to the current attribute's value"          ## ISSUE: spec says "append the returned character token to the current attribute's value"
1130        }        }
1131    
1132        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1133        # next-input-character is already done        # next-input-character is already done
1134        redo A;        redo A;
1135      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1136        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1137                
1138        my $token = {type => COMMENT_TOKEN, data => ''};        my $token = {type => COMMENT_TOKEN, data => ''};
1139    
1140        BC: {        BC: {
1141          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_input_character} == 0x003E) { # >
1142            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1143            !!!next-input-character;            !!!next-input-character;
1144    
1145            !!!emit ($token);            !!!emit ($token);
1146    
1147            redo A;            redo A;
1148          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_input_character} == -1) {
1149            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1150            ## reconsume            ## reconsume
1151    
1152            !!!emit ($token);            !!!emit ($token);
# Line 1011  sub _get_next_token ($) { Line 1158  sub _get_next_token ($) {
1158            redo BC;            redo BC;
1159          }          }
1160        } # BC        } # BC
1161      } elsif ($self->{state} eq 'markup declaration open') {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1162        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1163    
1164        my @next_char;        my @next_char;
# Line 1022  sub _get_next_token ($) { Line 1169  sub _get_next_token ($) {
1169          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_input_character};
1170          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_input_character} == 0x002D) { # -
1171            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};
1172            $self->{state} = 'comment start';            $self->{state} = COMMENT_START_STATE;
1173            !!!next-input-character;            !!!next-input-character;
1174            redo A;            redo A;
1175          }          }
# Line 1053  sub _get_next_token ($) { Line 1200  sub _get_next_token ($) {
1200                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_input_character} == 0x0045 or # E
1201                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_input_character} == 0x0065) { # e
1202                      ## ISSUE: What a stupid code this is!                      ## ISSUE: What a stupid code this is!
1203                      $self->{state} = 'DOCTYPE';                      $self->{state} = DOCTYPE_STATE;
1204                      !!!next-input-character;                      !!!next-input-character;
1205                      redo A;                      redo A;
1206                    }                    }
# Line 1067  sub _get_next_token ($) { Line 1214  sub _get_next_token ($) {
1214        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
1215        $self->{next_input_character} = shift @next_char;        $self->{next_input_character} = shift @next_char;
1216        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1217        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
1218        redo A;        redo A;
1219                
1220        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1221        ## 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?        ## 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?
1222      } elsif ($self->{state} eq 'comment start') {      } elsif ($self->{state} == COMMENT_START_STATE) {
1223        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1224          $self->{state} = 'comment start dash';          $self->{state} = COMMENT_START_DASH_STATE;
1225          !!!next-input-character;          !!!next-input-character;
1226          redo A;          redo A;
1227        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1228          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1229          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1230          !!!next-input-character;          !!!next-input-character;
1231    
1232          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1087  sub _get_next_token ($) { Line 1234  sub _get_next_token ($) {
1234          redo A;          redo A;
1235        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1236          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1237          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1238          ## reconsume          ## reconsume
1239    
1240          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1096  sub _get_next_token ($) { Line 1243  sub _get_next_token ($) {
1243        } else {        } else {
1244          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1245              .= chr ($self->{next_input_character});              .= chr ($self->{next_input_character});
1246          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1247          !!!next-input-character;          !!!next-input-character;
1248          redo A;          redo A;
1249        }        }
1250      } elsif ($self->{state} eq 'comment start dash') {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1251        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1252          $self->{state} = 'comment end';          $self->{state} = COMMENT_END_STATE;
1253          !!!next-input-character;          !!!next-input-character;
1254          redo A;          redo A;
1255        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1256          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1257          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1258          !!!next-input-character;          !!!next-input-character;
1259    
1260          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1115  sub _get_next_token ($) { Line 1262  sub _get_next_token ($) {
1262          redo A;          redo A;
1263        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1264          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1265          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1266          ## reconsume          ## reconsume
1267    
1268          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1124  sub _get_next_token ($) { Line 1271  sub _get_next_token ($) {
1271        } else {        } else {
1272          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1273              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_input_character});
1274          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1275          !!!next-input-character;          !!!next-input-character;
1276          redo A;          redo A;
1277        }        }
1278      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_STATE) {
1279        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1280          $self->{state} = 'comment end dash';          $self->{state} = COMMENT_END_DASH_STATE;
1281          !!!next-input-character;          !!!next-input-character;
1282          redo A;          redo A;
1283        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1284          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1285          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1286          ## reconsume          ## reconsume
1287    
1288          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1147  sub _get_next_token ($) { Line 1294  sub _get_next_token ($) {
1294          !!!next-input-character;          !!!next-input-character;
1295          redo A;          redo A;
1296        }        }
1297      } elsif ($self->{state} eq 'comment end dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1298        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1299          $self->{state} = 'comment end';          $self->{state} = COMMENT_END_STATE;
1300          !!!next-input-character;          !!!next-input-character;
1301          redo A;          redo A;
1302        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1303          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1304          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1305          ## reconsume          ## reconsume
1306    
1307          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1162  sub _get_next_token ($) { Line 1309  sub _get_next_token ($) {
1309          redo A;          redo A;
1310        } else {        } else {
1311          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment
1312          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1313          !!!next-input-character;          !!!next-input-character;
1314          redo A;          redo A;
1315        }        }
1316      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
1317        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1318          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1319          !!!next-input-character;          !!!next-input-character;
1320    
1321          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1182  sub _get_next_token ($) { Line 1329  sub _get_next_token ($) {
1329          redo A;          redo A;
1330        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1331          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1332          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1333          ## reconsume          ## reconsume
1334    
1335          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1191  sub _get_next_token ($) { Line 1338  sub _get_next_token ($) {
1338        } else {        } else {
1339          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment');
1340          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment
1341          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1342          !!!next-input-character;          !!!next-input-character;
1343          redo A;          redo A;
1344        }        }
1345      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
1346        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1347            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1348            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1349            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1350            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1351          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1352          !!!next-input-character;          !!!next-input-character;
1353          redo A;          redo A;
1354        } else {        } else {
1355          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
1356          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1357          ## reconsume          ## reconsume
1358          redo A;          redo A;
1359        }        }
1360      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
1361        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1362            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1363            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1221  sub _get_next_token ($) { Line 1368  sub _get_next_token ($) {
1368          redo A;          redo A;
1369        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1370          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1371          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1372          !!!next-input-character;          !!!next-input-character;
1373    
1374          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
# Line 1229  sub _get_next_token ($) { Line 1376  sub _get_next_token ($) {
1376          redo A;          redo A;
1377        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1378          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1379          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1380          ## reconsume          ## reconsume
1381    
1382          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
# Line 1241  sub _get_next_token ($) { Line 1388  sub _get_next_token ($) {
1388                 name => chr ($self->{next_input_character}),                 name => chr ($self->{next_input_character}),
1389                 correct => 1};                 correct => 1};
1390  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1391          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
1392          !!!next-input-character;          !!!next-input-character;
1393          redo A;          redo A;
1394        }        }
1395      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1396  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
1397        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1398            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1399            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1400            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1401            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1402          $self->{state} = 'after DOCTYPE name';          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
1403          !!!next-input-character;          !!!next-input-character;
1404          redo A;          redo A;
1405        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1406          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1407          !!!next-input-character;          !!!next-input-character;
1408    
1409          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1264  sub _get_next_token ($) { Line 1411  sub _get_next_token ($) {
1411          redo A;          redo A;
1412        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1413          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1414          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1415          ## reconsume          ## reconsume
1416    
1417          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1278  sub _get_next_token ($) { Line 1425  sub _get_next_token ($) {
1425          !!!next-input-character;          !!!next-input-character;
1426          redo A;          redo A;
1427        }        }
1428      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
1429        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1430            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1431            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1288  sub _get_next_token ($) { Line 1435  sub _get_next_token ($) {
1435          !!!next-input-character;          !!!next-input-character;
1436          redo A;          redo A;
1437        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1438          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1439          !!!next-input-character;          !!!next-input-character;
1440    
1441          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1296  sub _get_next_token ($) { Line 1443  sub _get_next_token ($) {
1443          redo A;          redo A;
1444        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1445          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1446          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1447          ## reconsume          ## reconsume
1448    
1449          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1320  sub _get_next_token ($) { Line 1467  sub _get_next_token ($) {
1467                  !!!next-input-character;                  !!!next-input-character;
1468                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_input_character} == 0x0043 or # C
1469                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_input_character} == 0x0063) { # c
1470                    $self->{state} = 'before DOCTYPE public identifier';                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1471                    !!!next-input-character;                    !!!next-input-character;
1472                    redo A;                    redo A;
1473                  }                  }
# Line 1347  sub _get_next_token ($) { Line 1494  sub _get_next_token ($) {
1494                  !!!next-input-character;                  !!!next-input-character;
1495                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_input_character} == 0x004D or # M
1496                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_input_character} == 0x006D) { # m
1497                    $self->{state} = 'before DOCTYPE system identifier';                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1498                    !!!next-input-character;                    !!!next-input-character;
1499                    redo A;                    redo A;
1500                  }                  }
# Line 1363  sub _get_next_token ($) { Line 1510  sub _get_next_token ($) {
1510        }        }
1511    
1512        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
1513        $self->{state} = 'bogus DOCTYPE';        $self->{state} = BOGUS_DOCTYPE_STATE;
1514        # next-input-character is already done        # next-input-character is already done
1515        redo A;        redo A;
1516      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1517        if ({        if ({
1518              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1519              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1376  sub _get_next_token ($) { Line 1523  sub _get_next_token ($) {
1523          redo A;          redo A;
1524        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_input_character} eq 0x0022) { # "
1525          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1526          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
1527          !!!next-input-character;          !!!next-input-character;
1528          redo A;          redo A;
1529        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_input_character} eq 0x0027) { # '
1530          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1531          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
1532          !!!next-input-character;          !!!next-input-character;
1533          redo A;          redo A;
1534        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_input_character} eq 0x003E) { # >
1535          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
1536    
1537          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1538          !!!next-input-character;          !!!next-input-character;
1539    
1540          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1397  sub _get_next_token ($) { Line 1544  sub _get_next_token ($) {
1544        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1545          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1546    
1547          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1548          ## reconsume          ## reconsume
1549    
1550          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1406  sub _get_next_token ($) { Line 1553  sub _get_next_token ($) {
1553          redo A;          redo A;
1554        } else {        } else {
1555          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
1556          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1557          !!!next-input-character;          !!!next-input-character;
1558          redo A;          redo A;
1559        }        }
1560      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1561        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1562          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1563            !!!next-input-character;
1564            redo A;
1565          } elsif ($self->{next_input_character} == 0x003E) { # >
1566            !!!parse-error (type => 'unclosed PUBLIC literal');
1567    
1568            $self->{state} = DATA_STATE;
1569          !!!next-input-character;          !!!next-input-character;
1570    
1571            delete $self->{current_token}->{correct};
1572            !!!emit ($self->{current_token}); # DOCTYPE
1573    
1574          redo A;          redo A;
1575        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1576          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1577    
1578          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1579          ## reconsume          ## reconsume
1580    
1581          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1432  sub _get_next_token ($) { Line 1589  sub _get_next_token ($) {
1589          !!!next-input-character;          !!!next-input-character;
1590          redo A;          redo A;
1591        }        }
1592      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
1593        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1594          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1595          !!!next-input-character;          !!!next-input-character;
1596          redo A;          redo A;
1597          } elsif ($self->{next_input_character} == 0x003E) { # >
1598            !!!parse-error (type => 'unclosed PUBLIC literal');
1599    
1600            $self->{state} = DATA_STATE;
1601            !!!next-input-character;
1602    
1603            delete $self->{current_token}->{correct};
1604            !!!emit ($self->{current_token}); # DOCTYPE
1605    
1606            redo A;
1607        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1608          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1609    
1610          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1611          ## reconsume          ## reconsume
1612    
1613          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1454  sub _get_next_token ($) { Line 1621  sub _get_next_token ($) {
1621          !!!next-input-character;          !!!next-input-character;
1622          redo A;          redo A;
1623        }        }
1624      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1625        if ({        if ({
1626              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1627              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1464  sub _get_next_token ($) { Line 1631  sub _get_next_token ($) {
1631          redo A;          redo A;
1632        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1633          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1634          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1635          !!!next-input-character;          !!!next-input-character;
1636          redo A;          redo A;
1637        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1638          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1639          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1640          !!!next-input-character;          !!!next-input-character;
1641          redo A;          redo A;
1642        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1643          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1644          !!!next-input-character;          !!!next-input-character;
1645    
1646          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1482  sub _get_next_token ($) { Line 1649  sub _get_next_token ($) {
1649        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1650          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1651    
1652          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1653          ## reconsume          ## reconsume
1654    
1655          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1491  sub _get_next_token ($) { Line 1658  sub _get_next_token ($) {
1658          redo A;          redo A;
1659        } else {        } else {
1660          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
1661          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1662          !!!next-input-character;          !!!next-input-character;
1663          redo A;          redo A;
1664        }        }
1665      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1666        if ({        if ({
1667              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1668              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1505  sub _get_next_token ($) { Line 1672  sub _get_next_token ($) {
1672          redo A;          redo A;
1673        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1674          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1675          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1676          !!!next-input-character;          !!!next-input-character;
1677          redo A;          redo A;
1678        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1679          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1680          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1681          !!!next-input-character;          !!!next-input-character;
1682          redo A;          redo A;
1683        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1684          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
1685          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1686          !!!next-input-character;          !!!next-input-character;
1687    
1688          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1525  sub _get_next_token ($) { Line 1692  sub _get_next_token ($) {
1692        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1693          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1694    
1695          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1696          ## reconsume          ## reconsume
1697    
1698          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1534  sub _get_next_token ($) { Line 1701  sub _get_next_token ($) {
1701          redo A;          redo A;
1702        } else {        } else {
1703          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
1704          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1705          !!!next-input-character;          !!!next-input-character;
1706          redo A;          redo A;
1707        }        }
1708      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1709        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1710          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1711            !!!next-input-character;
1712            redo A;
1713          } elsif ($self->{next_input_character} == 0x003E) { # >
1714            !!!parse-error (type => 'unclosed PUBLIC literal');
1715    
1716            $self->{state} = DATA_STATE;
1717          !!!next-input-character;          !!!next-input-character;
1718    
1719            delete $self->{current_token}->{correct};
1720            !!!emit ($self->{current_token}); # DOCTYPE
1721    
1722          redo A;          redo A;
1723        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1724          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1725    
1726          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1727          ## reconsume          ## reconsume
1728    
1729          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1560  sub _get_next_token ($) { Line 1737  sub _get_next_token ($) {
1737          !!!next-input-character;          !!!next-input-character;
1738          redo A;          redo A;
1739        }        }
1740      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
1741        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1742          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1743            !!!next-input-character;
1744            redo A;
1745          } elsif ($self->{next_input_character} == 0x003E) { # >
1746            !!!parse-error (type => 'unclosed PUBLIC literal');
1747    
1748            $self->{state} = DATA_STATE;
1749          !!!next-input-character;          !!!next-input-character;
1750    
1751            delete $self->{current_token}->{correct};
1752            !!!emit ($self->{current_token}); # DOCTYPE
1753    
1754          redo A;          redo A;
1755        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1756          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1757    
1758          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1759          ## reconsume          ## reconsume
1760    
1761          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1582  sub _get_next_token ($) { Line 1769  sub _get_next_token ($) {
1769          !!!next-input-character;          !!!next-input-character;
1770          redo A;          redo A;
1771        }        }
1772      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1773        if ({        if ({
1774              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1775              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1591  sub _get_next_token ($) { Line 1778  sub _get_next_token ($) {
1778          !!!next-input-character;          !!!next-input-character;
1779          redo A;          redo A;
1780        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1781          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1782          !!!next-input-character;          !!!next-input-character;
1783    
1784          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1600  sub _get_next_token ($) { Line 1787  sub _get_next_token ($) {
1787        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1788          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1789    
1790          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1791          ## reconsume          ## reconsume
1792    
1793          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1609  sub _get_next_token ($) { Line 1796  sub _get_next_token ($) {
1796          redo A;          redo A;
1797        } else {        } else {
1798          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
1799          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1800          !!!next-input-character;          !!!next-input-character;
1801          redo A;          redo A;
1802        }        }
1803      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
1804        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1805          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1806          !!!next-input-character;          !!!next-input-character;
1807    
1808          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1624  sub _get_next_token ($) { Line 1811  sub _get_next_token ($) {
1811          redo A;          redo A;
1812        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1813          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1814          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1815          ## reconsume          ## reconsume
1816    
1817          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1705  sub _tokenize_attempt_to_consume_an_enti Line 1892  sub _tokenize_attempt_to_consume_an_enti
1892            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
1893          }          }
1894    
1895          return {type => CHARACTER_TOKEN, data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code,
1896                    has_reference => 1};
1897        } # X        } # X
1898      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_input_character} and
1899               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_input_character} <= 0x0039) { # 0..9
# Line 1740  sub _tokenize_attempt_to_consume_an_enti Line 1928  sub _tokenize_attempt_to_consume_an_enti
1928          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
1929        }        }
1930                
1931        return {type => CHARACTER_TOKEN, data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};
1932      } else {      } else {
1933        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero');
1934        !!!back-next-input-character ($self->{next_input_character});        !!!back-next-input-character ($self->{next_input_character});
# Line 1788  sub _tokenize_attempt_to_consume_an_enti Line 1976  sub _tokenize_attempt_to_consume_an_enti
1976      }      }
1977            
1978      if ($match > 0) {      if ($match > 0) {
1979        return {type => CHARACTER_TOKEN, data => $value};        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
1980      } elsif ($match < 0) {      } elsif ($match < 0) {
1981        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc');
1982        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
1983          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
1984        } else {        } else {
1985          return {type => CHARACTER_TOKEN, data => $value};          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
1986        }        }
1987      } else {      } else {
1988        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
1989        ## NOTE: No characters are consumed in the spec.        ## NOTE: "No characters are consumed" in the spec.
1990        return {type => CHARACTER_TOKEN, data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value};
1991      }      }
1992    } else {    } else {
# Line 2046  sub _tree_construction_root_element ($) Line 2234  sub _tree_construction_root_element ($)
2234              redo B;              redo B;
2235            }            }
2236          }          }
2237    
2238            $self->{application_cache_selection}->(undef);
2239    
2240            #
2241          } elsif ($token->{type} == START_TAG_TOKEN) {
2242            if ($token->{tag_name} eq 'html' and
2243                $token->{attributes}->{manifest}) {
2244              $self->{application_cache_selection}
2245                   ->($token->{attributes}->{manifest}->{value});
2246              ## ISSUE: No relative reference resolution?
2247            } else {
2248              $self->{application_cache_selection}->(undef);
2249            }
2250    
2251            ## ISSUE: There is an issue in the spec
2252          #          #
2253        } elsif ({        } elsif ({
                 START_TAG_TOKEN, 1,  
2254                  END_TAG_TOKEN, 1,                  END_TAG_TOKEN, 1,
2255                  END_OF_FILE_TOKEN, 1,                  END_OF_FILE_TOKEN, 1,
2256                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2257            $self->{application_cache_selection}->(undef);
2258    
2259          ## ISSUE: There is an issue in the spec          ## ISSUE: There is an issue in the spec
2260          #          #
2261        } else {        } else {
2262          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
2263        }        }
2264    
2265        my $root_element; !!!create-element ($root_element, 'html');        my $root_element; !!!create-element ($root_element, 'html');
2266        $self->{document}->append_child ($root_element);        $self->{document}->append_child ($root_element);
2267        push @{$self->{open_elements}}, [$root_element, 'html'];        push @{$self->{open_elements}}, [$root_element, 'html'];
# Line 2713  sub _tree_construction_main ($) { Line 2918  sub _tree_construction_main ($) {
2918                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2919                }                }
2920                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
2921                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2922    
2923                unless ($self->{confident}) {                unless ($self->{confident}) {
                 my $charset;  
2924                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
2925                    $charset = $token->{attributes}->{charset}->{value};                    $self->{change_encoding}
2926                  }                        ->($self, $token->{attributes}->{charset}->{value});
2927                  if ($token->{attributes}->{'http-equiv'}) {                    
2928                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
2929                          ->set_user_data (manakai_has_reference =>
2930                                               $token->{attributes}->{charset}
2931                                                   ->{has_reference});
2932                    } elsif ($token->{attributes}->{content}) {
2933                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
2934                    if ($token->{attributes}->{'http-equiv'}->{value}                    if ($token->{attributes}->{content}->{value}
2935                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
2936                              [\x09-\x0D\x20]*=
2937                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2938                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
2939                      $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                      $self->{change_encoding}
2940                    } ## TODO: And if supported                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
2941                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
2942                            ->set_user_data (manakai_has_reference =>
2943                                                 $token->{attributes}->{content}
2944                                                       ->{has_reference});
2945                      }
2946                    }
2947                  } else {
2948                    if ($token->{attributes}->{charset}) {
2949                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
2950                          ->set_user_data (manakai_has_reference =>
2951                                               $token->{attributes}->{charset}
2952                                                   ->{has_reference});
2953                    }
2954                    if ($token->{attributes}->{content}) {
2955                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
2956                          ->set_user_data (manakai_has_reference =>
2957                                               $token->{attributes}->{content}
2958                                                   ->{has_reference});
2959                  }                  }
                 ## TODO: Change the encoding  
2960                }                }
2961    
               ## TODO: Extracting |charset| from |meta|.  
2962                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2963                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2964                !!!next-token;                !!!next-token;
# Line 3302  sub _tree_construction_main ($) { Line 3528  sub _tree_construction_main ($) {
3528        $insert = $insert_to_current;        $insert = $insert_to_current;
3529        #        #
3530      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
3531            if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
             ## NOTE: There are "character in table" code clones.  
3532              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3533                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3534                                
# Line 3360  sub _tree_construction_main ($) { Line 3585  sub _tree_construction_main ($) {
3585                            
3586              !!!next-token;              !!!next-token;
3587              redo B;              redo B;
3588            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3589              if ({              if ({
3590                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
3591                   th => 1, td => 1,                   th => 1, td => 1,
# Line 3546  sub _tree_construction_main ($) { Line 3771  sub _tree_construction_main ($) {
3771                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
3772                }                }
3773              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
               ## NOTE: There are code clones for this "table in table"  
3774                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3775    
3776                ## As if </table>                ## As if </table>
# Line 3594  sub _tree_construction_main ($) { Line 3818  sub _tree_construction_main ($) {
3818    
3819                ## reprocess                ## reprocess
3820                redo B;                redo B;
3821              } else {          } else {
3822                #            !!!parse-error (type => 'in table:'.$token->{tag_name});
3823              }  
3824            } elsif ($token->{type} == END_TAG_TOKEN) {            $insert = $insert_to_foster;
3825              #
3826            }
3827          } elsif ($token->{type} == END_TAG_TOKEN) {
3828              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
3829                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
3830                ## have an element in table scope                ## have an element in table scope
# Line 3854  sub _tree_construction_main ($) { Line 4081  sub _tree_construction_main ($) {
4081                ## Ignore the token                ## Ignore the token
4082                !!!next-token;                !!!next-token;
4083                redo B;                redo B;
4084              } else {          } else {
4085                #            !!!parse-error (type => 'in table:/'.$token->{tag_name});
             }  
           } else {  
             die "$0: $token->{type}: Unknown token type";  
           }  
   
       !!!parse-error (type => 'in table:'.$token->{tag_name});  
4086    
4087        $insert = $insert_to_foster;            $insert = $insert_to_foster;
4088        #            #
4089            }
4090          } else {
4091            die "$0: $token->{type}: Unknown token type";
4092          }
4093      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
4094            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
4095              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
# Line 3923  sub _tree_construction_main ($) { Line 4148  sub _tree_construction_main ($) {
4148              redo B;              redo B;
4149            }            }
4150      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
4151            if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4152              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4153              !!!next-token;          !!!next-token;
4154              redo B;          redo B;
4155            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4156              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
4157                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4158                  ## As if </option>                  ## As if </option>
# Line 3980  sub _tree_construction_main ($) { Line 4205  sub _tree_construction_main ($) {
4205    
4206                !!!next-token;                !!!next-token;
4207                redo B;                redo B;
4208              } else {          } else {
4209                #            !!!parse-error (type => 'in select:'.$token->{tag_name});
4210              }            ## Ignore the token
4211            } elsif ($token->{type} == END_TAG_TOKEN) {            !!!next-token;
4212              redo B;
4213            }
4214          } elsif ($token->{type} == END_TAG_TOKEN) {
4215              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
4216                if ($self->{open_elements}->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
4217                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
# Line 4085  sub _tree_construction_main ($) { Line 4313  sub _tree_construction_main ($) {
4313    
4314                ## reprocess                ## reprocess
4315                redo B;                redo B;
4316              } else {          } else {
4317                #            !!!parse-error (type => 'in select:/'.$token->{tag_name});
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in select:'.$token->{tag_name});  
4318            ## Ignore the token            ## Ignore the token
4319            !!!next-token;            !!!next-token;
4320            redo B;            redo B;
4321            }
4322          } else {
4323            die "$0: $token->{type}: Unknown token type";
4324          }
4325      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
4326        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4327          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
# Line 4303  sub _tree_construction_main ($) { Line 4529  sub _tree_construction_main ($) {
4529        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
4530          ## NOTE: This is an "as if in head" code clone, only "-t" differs          ## NOTE: This is an "as if in head" code clone, only "-t" differs
4531          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4532          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4533    
4534          unless ($self->{confident}) {          unless ($self->{confident}) {
           my $charset;  
4535            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) { ## TODO: And if supported
4536              $charset = $token->{attributes}->{charset}->{value};              $self->{change_encoding}
4537            }                  ->($self, $token->{attributes}->{charset}->{value});
4538            if ($token->{attributes}->{'http-equiv'}) {              
4539                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4540                    ->set_user_data (manakai_has_reference =>
4541                                         $token->{attributes}->{charset}
4542                                             ->{has_reference});
4543              } elsif ($token->{attributes}->{content}) {
4544              ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.              ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.
4545              if ($token->{attributes}->{'http-equiv'}->{value}              if ($token->{attributes}->{content}->{value}
4546                  =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4547                        [\x09-\x0D\x20]*=
4548                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4549                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4550                $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                $self->{change_encoding}
4551              } ## TODO: And if supported                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
4552                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4553                      ->set_user_data (manakai_has_reference =>
4554                                           $token->{attributes}->{content}
4555                                                 ->{has_reference});
4556                }
4557              }
4558            } else {
4559              if ($token->{attributes}->{charset}) {
4560                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4561                    ->set_user_data (manakai_has_reference =>
4562                                         $token->{attributes}->{charset}
4563                                             ->{has_reference});
4564              }
4565              if ($token->{attributes}->{content}) {
4566                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4567                    ->set_user_data (manakai_has_reference =>
4568                                         $token->{attributes}->{content}
4569                                             ->{has_reference});
4570            }            }
           ## TODO: Change the encoding  
4571          }          }
4572    
4573          !!!next-token;          !!!next-token;
# Line 4626  sub _tree_construction_main ($) { Line 4874  sub _tree_construction_main ($) {
4874          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4875            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
4876            if ($node->[1] eq 'nobr') {            if ($node->[1] eq 'nobr') {
4877              !!!parse-error (type => 'not closed:nobr');              !!!parse-error (type => 'in nobr:nobr');
4878              !!!back-token;              !!!back-token;
4879              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
4880              redo B;              redo B;
# Line 4831  sub _tree_construction_main ($) { Line 5079  sub _tree_construction_main ($) {
5079                  noframes => 1,                  noframes => 1,
5080                  noscript => 0, ## TODO: 1 if scripting is enabled                  noscript => 0, ## TODO: 1 if scripting is enabled
5081                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5082          ## NOTE: There are two "as if in body" code clones.          ## NOTE: There is an "as if in body" code clone.
5083          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
5084          redo B;          redo B;
5085        } elsif ($token->{tag_name} eq 'select') {        } elsif ($token->{tag_name} eq 'select') {
# Line 4987  sub _tree_construction_main ($) { Line 5235  sub _tree_construction_main ($) {
5235          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
5236            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
5237          } else {          } else {
5238            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5239          }          }
5240    
5241          undef $self->{form_element};          undef $self->{form_element};
# Line 5025  sub _tree_construction_main ($) { Line 5273  sub _tree_construction_main ($) {
5273          } # INSCOPE          } # INSCOPE
5274                    
5275          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5276            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5277          }          }
5278                    
5279          splice @{$self->{open_elements}}, $i if defined $i;          splice @{$self->{open_elements}}, $i if defined $i;
# Line 5094  sub _tree_construction_main ($) { Line 5342  sub _tree_construction_main ($) {
5342                    
5343              ## Step 2              ## Step 2
5344              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
5345                  ## NOTE: <x><y></x>
5346                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5347              }              }
5348                            
# Line 5144  sub set_inner_html ($$$) { Line 5393  sub set_inner_html ($$$) {
5393    my $s = \$_[0];    my $s = \$_[0];
5394    my $onerror = $_[1];    my $onerror = $_[1];
5395    
5396      ## ISSUE: Should {confident} be true?
5397    
5398    my $nt = $node->node_type;    my $nt = $node->node_type;
5399    if ($nt == 9) {    if ($nt == 9) {
5400      # MUST      # MUST
# Line 5216  sub set_inner_html ($$$) { Line 5467  sub set_inner_html ($$$) {
5467      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
5468    
5469      ## Step 2      ## Step 2
5470      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
5471      $p->{content_model} = {      $p->{content_model} = {
5472        title => RCDATA_CONTENT_MODEL,        title => RCDATA_CONTENT_MODEL,
5473        textarea => RCDATA_CONTENT_MODEL,        textarea => RCDATA_CONTENT_MODEL,
# Line 5256  sub set_inner_html ($$$) { Line 5507  sub set_inner_html ($$$) {
5507        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
5508          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
5509          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
5510            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
5511              $p->{form_element} = $anode;              $p->{form_element} = $anode;
5512              last AN;              last AN;
5513            }            }
# Line 5296  sub set_inner_html ($$$) { Line 5547  sub set_inner_html ($$$) {
5547    
5548  } # tree construction stage  } # tree construction stage
5549    
5550  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
5551    my (undef, $node, $on_error) = @_;  push our @ISA, 'Error';
   
   ## Step 1  
   my $s = '';  
   
   my $in_cdata;  
   my $parent = $node;  
   while (defined $parent) {  
     if ($parent->node_type == 1 and  
         $parent->namespace_uri eq 'http://www.w3.org/1999/xhtml' and  
         {  
           style => 1, script => 1, xmp => 1, iframe => 1,  
           noembed => 1, noframes => 1, noscript => 1,  
         }->{$parent->local_name}) { ## TODO: case thingy  
       $in_cdata = 1;  
     }  
     $parent = $parent->parent_node;  
   }  
   
   ## Step 2  
   my @node = @{$node->child_nodes};  
   C: while (@node) {  
     my $child = shift @node;  
     unless (ref $child) {  
       if ($child eq 'cdata-out') {  
         $in_cdata = 0;  
       } else {  
         $s .= $child; # end tag  
       }  
       next C;  
     }  
       
     my $nt = $child->node_type;  
     if ($nt == 1) { # Element  
       my $tag_name = $child->tag_name; ## TODO: manakai_tag_name  
       $s .= '<' . $tag_name;  
       ## NOTE: Non-HTML case:  
       ## <http://permalink.gmane.org/gmane.org.w3c.whatwg.discuss/11191>  
   
       my @attrs = @{$child->attributes}; # sort order MUST be stable  
       for my $attr (@attrs) { # order is implementation dependent  
         my $attr_name = $attr->name; ## TODO: manakai_name  
         $s .= ' ' . $attr_name . '="';  
         my $attr_value = $attr->value;  
         ## escape  
         $attr_value =~ s/&/&amp;/g;  
         $attr_value =~ s/</&lt;/g;  
         $attr_value =~ s/>/&gt;/g;  
         $attr_value =~ s/"/&quot;/g;  
         $s .= $attr_value . '"';  
       }  
       $s .= '>';  
         
       next C if {  
         area => 1, base => 1, basefont => 1, bgsound => 1,  
         br => 1, col => 1, embed => 1, frame => 1, hr => 1,  
         img => 1, input => 1, link => 1, meta => 1, param => 1,  
         spacer => 1, wbr => 1,  
       }->{$tag_name};  
   
       $s .= "\x0A" if $tag_name eq 'pre' or $tag_name eq 'textarea';  
   
       if (not $in_cdata and {  
         style => 1, script => 1, xmp => 1, iframe => 1,  
         noembed => 1, noframes => 1, noscript => 1,  
         plaintext => 1,  
       }->{$tag_name}) {  
         unshift @node, 'cdata-out';  
         $in_cdata = 1;  
       }  
   
       unshift @node, @{$child->child_nodes}, '</' . $tag_name . '>';  
     } elsif ($nt == 3 or $nt == 4) {  
       if ($in_cdata) {  
         $s .= $child->data;  
       } else {  
         my $value = $child->data;  
         $value =~ s/&/&amp;/g;  
         $value =~ s/</&lt;/g;  
         $value =~ s/>/&gt;/g;  
         $value =~ s/"/&quot;/g;  
         $s .= $value;  
       }  
     } elsif ($nt == 8) {  
       $s .= '<!--' . $child->data . '-->';  
     } elsif ($nt == 10) {  
       $s .= '<!DOCTYPE ' . $child->name . '>';  
     } elsif ($nt == 5) { # entrefs  
       push @node, @{$child->child_nodes};  
     } else {  
       $on_error->($child) if defined $on_error;  
     }  
     ## ISSUE: This code does not support PIs.  
   } # C  
     
   ## Step 3  
   return \$s;  
 } # get_inner_html  
5552    
5553  1;  1;
5554  # $Date$  # $Date$

Legend:
Removed from v.1.56  
changed lines
  Added in v.1.71

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24