/[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.53 by wakaba, Sat Jul 21 12:37:57 2007 UTC revision 1.67 by wakaba, Sat Feb 16 03:47:33 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);
# Line 84  my $formatting_category = { Line 85  my $formatting_category = {
85  };  };
86  # $phrasing_category: all other elements  # $phrasing_category: all other elements
87    
88    sub parse_byte_string ($$$$;$) {
89      my $self = ref $_[0] ? shift : shift->new;
90      my $charset = shift;
91      my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);
92      my $s;
93      
94      if (defined $charset) {
95        require Encode; ## TODO: decode(utf8) don't delete BOM
96        $s = \ (Encode::decode ($charset, $$bytes_s));
97        $self->{input_encoding} = lc $charset; ## TODO: normalize name
98        $self->{confident} = 1;
99      } else {
100        ## TODO: Implement HTML5 detection algorithm
101        require Whatpm::Charset::UniversalCharDet;
102        $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string
103            (substr ($$bytes_s, 0, 1024));
104        $charset ||= 'windows-1252';
105        $s = \ (Encode::decode ($charset, $$bytes_s));
106        $self->{input_encoding} = $charset;
107        $self->{confident} = 0;
108      }
109    
110      $self->{change_encoding} = sub {
111        my $self = shift;
112        my $charset = lc shift;
113        ## TODO: if $charset is supported
114        ## TODO: normalize charset name
115    
116        ## "Change the encoding" algorithm:
117    
118        ## Step 1    
119        if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
120          $charset = 'utf-8';
121        }
122    
123        ## Step 2
124        if (defined $self->{input_encoding} and
125            $self->{input_encoding} eq $charset) {
126          $self->{confident} = 1;
127          return;
128        }
129    
130        !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
131            ':'.$charset, level => 'w');
132    
133        ## Step 3
134        # if (can) {
135          ## change the encoding on the fly.
136          #$self->{confident} = 1;
137          #return;
138        # }
139    
140        ## Step 4
141        throw Whatpm::HTML::RestartParser (charset => $charset);
142      }; # $self->{change_encoding}
143    
144      my @args = @_; shift @args; # $s
145      my $return;
146      try {
147        $return = $self->parse_char_string ($s, @args);  
148      } catch Whatpm::HTML::RestartParser with {
149        my $charset = shift->{charset};
150        $s = \ (Encode::decode ($charset, $$bytes_s));    
151        $self->{input_encoding} = $charset; ## TODO: normalize
152        $self->{confident} = 1;
153        $return = $self->parse_char_string ($s, @args);
154      };
155      return $return;
156    } # parse_byte_string
157    
158    *parse_char_string = \&parse_string;
159    
160  sub parse_string ($$$;$) {  sub parse_string ($$$;$) {
161    my $self = shift->new;    my $self = ref $_[0] ? shift : shift->new;
162    my $s = \$_[0];    my $s = ref $_[0] ? $_[0] : \($_[0]);
163    $self->{document} = $_[1];    $self->{document} = $_[1];
164      @{$self->{document}->child_nodes} = ();
165    
166    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
167    
168      $self->{confident} = 1 unless exists $self->{confident};
169      $self->{document}->input_encoding ($self->{input_encoding})
170          if defined $self->{input_encoding};
171    
172    my $i = 0;    my $i = 0;
173    my $line = 1;    my $line = 1;
174    my $column = 0;    my $column = 0;
# Line 147  sub new ($) { Line 225  sub new ($) {
225    $self->{parse_error} = sub {    $self->{parse_error} = sub {
226      #      #
227    };    };
228      $self->{change_encoding} = sub {
229        # if ($_[0] is a supported encoding) {
230        #   run "change the encoding" algorithm;
231        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
232        # }
233      };
234      $self->{application_cache_selection} = sub {
235        #
236      };
237    return $self;    return $self;
238  } # new  } # new
239    
# Line 159  sub CDATA_CONTENT_MODEL () { CM_LIMITED_ Line 246  sub CDATA_CONTENT_MODEL () { CM_LIMITED_
246  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
247  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
248    
249    sub DATA_STATE () { 0 }
250    sub ENTITY_DATA_STATE () { 1 }
251    sub TAG_OPEN_STATE () { 2 }
252    sub CLOSE_TAG_OPEN_STATE () { 3 }
253    sub TAG_NAME_STATE () { 4 }
254    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
255    sub ATTRIBUTE_NAME_STATE () { 6 }
256    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
257    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
258    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
259    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
260    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
261    sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
262    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
263    sub COMMENT_START_STATE () { 14 }
264    sub COMMENT_START_DASH_STATE () { 15 }
265    sub COMMENT_STATE () { 16 }
266    sub COMMENT_END_STATE () { 17 }
267    sub COMMENT_END_DASH_STATE () { 18 }
268    sub BOGUS_COMMENT_STATE () { 19 }
269    sub DOCTYPE_STATE () { 20 }
270    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
271    sub DOCTYPE_NAME_STATE () { 22 }
272    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
273    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
274    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
275    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
276    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
277    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
278    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
279    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
280    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
281    sub BOGUS_DOCTYPE_STATE () { 32 }
282    
283    sub DOCTYPE_TOKEN () { 1 }
284    sub COMMENT_TOKEN () { 2 }
285    sub START_TAG_TOKEN () { 3 }
286    sub END_TAG_TOKEN () { 4 }
287    sub END_OF_FILE_TOKEN () { 5 }
288    sub CHARACTER_TOKEN () { 6 }
289    
290    sub AFTER_HTML_IMS () { 0b100 }
291    sub HEAD_IMS ()       { 0b1000 }
292    sub BODY_IMS ()       { 0b10000 }
293    sub BODY_TABLE_IMS () { 0b100000 }
294    sub TABLE_IMS ()      { 0b1000000 }
295    sub ROW_IMS ()        { 0b10000000 }
296    sub BODY_AFTER_IMS () { 0b100000000 }
297    sub FRAME_IMS ()      { 0b1000000000 }
298    
299    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
300    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
301    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
302    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
303    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
304    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
305    sub IN_BODY_IM () { BODY_IMS }
306    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
307    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
308    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
309    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
310    sub IN_TABLE_IM () { TABLE_IMS }
311    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
312    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
313    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
314    sub IN_SELECT_IM () { 0b01 }
315    sub IN_COLUMN_GROUP_IM () { 0b10 }
316    
317  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
318    
319  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
320    my $self = shift;    my $self = shift;
321    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
322    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
323    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE
324    undef $self->{current_attribute};    undef $self->{current_attribute};
# Line 177  sub _initialize_tokenizer ($) { Line 332  sub _initialize_tokenizer ($) {
332  } # _initialize_tokenizer  } # _initialize_tokenizer
333    
334  ## A token has:  ## A token has:
335  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
336  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
337  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))  ##   ->{name} (DOCTYPE_TOKEN)
338  ##   ->{public_identifier} (DOCTYPE)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
339  ##   ->{system_identifier} (DOCTYPE)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
340  ##   ->{correct} == 1 or 0 (DOCTYPE)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
341  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)
342  ##   ->{data} (comment, character)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
343    ##        ->{name}
344    ##        ->{value}
345    ##        ->{has_reference} == 1 or 0
346    ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
347    
348  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
349    
# Line 194  sub _initialize_tokenizer ($) { Line 353  sub _initialize_tokenizer ($) {
353  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
354  ## and removed from the list.  ## and removed from the list.
355    
356    ## NOTE: HTML5 "Writing HTML documents" section, applied to
357    ## documents and not to user agents and conformance checkers,
358    ## contains some requirements that are not detected by the
359    ## parsing algorithm:
360    ## - Some requirements on character encoding declarations. ## TODO
361    ## - "Elements MUST NOT contain content that their content model disallows."
362    ##   ... Some are parse error, some are not (will be reported by c.c.).
363    ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO
364    ## - Text (in elements, attributes, and comments) SHOULD NOT contain
365    ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)
366    
367    ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot
368    ## be detected by the HTML5 parsing algorithm:
369    ## - Text,
370    
371  sub _get_next_token ($) {  sub _get_next_token ($) {
372    my $self = shift;    my $self = shift;
373    if (@{$self->{token}}) {    if (@{$self->{token}}) {
# Line 201  sub _get_next_token ($) { Line 375  sub _get_next_token ($) {
375    }    }
376    
377    A: {    A: {
378      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
379        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_input_character} == 0x0026) { # &
380          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA
381            $self->{state} = 'entity data';            $self->{state} = ENTITY_DATA_STATE;
382            !!!next-input-character;            !!!next-input-character;
383            redo A;            redo A;
384          } else {          } else {
# Line 226  sub _get_next_token ($) { Line 400  sub _get_next_token ($) {
400          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
401              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
402               not $self->{escape})) {               not $self->{escape})) {
403            $self->{state} = 'tag open';            $self->{state} = TAG_OPEN_STATE;
404            !!!next-input-character;            !!!next-input-character;
405            redo A;            redo A;
406          } else {          } else {
# Line 243  sub _get_next_token ($) { Line 417  sub _get_next_token ($) {
417                    
418          #          #
419        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
420          !!!emit ({type => 'end-of-file'});          !!!emit ({type => END_OF_FILE_TOKEN});
421          last A; ## TODO: ok?          last A; ## TODO: ok?
422        }        }
423        # Anything else        # Anything else
424        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
425                     data => chr $self->{next_input_character}};                     data => chr $self->{next_input_character}};
426        ## Stay in the data state        ## Stay in the data state
427        !!!next-input-character;        !!!next-input-character;
# Line 255  sub _get_next_token ($) { Line 429  sub _get_next_token ($) {
429        !!!emit ($token);        !!!emit ($token);
430    
431        redo A;        redo A;
432      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
433        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
434                
435        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);
436    
437        $self->{state} = 'data';        $self->{state} = DATA_STATE;
438        # next-input-character is already done        # next-input-character is already done
439    
440        unless (defined $token) {        unless (defined $token) {
441          !!!emit ({type => 'character', data => '&'});          !!!emit ({type => CHARACTER_TOKEN, data => '&'});
442        } else {        } else {
443          !!!emit ($token);          !!!emit ($token);
444        }        }
445    
446        redo A;        redo A;
447      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
448        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
449          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_input_character} == 0x002F) { # /
450            !!!next-input-character;            !!!next-input-character;
451            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
452            redo A;            redo A;
453          } else {          } else {
454            ## reconsume            ## reconsume
455            $self->{state} = 'data';            $self->{state} = DATA_STATE;
456    
457            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
458    
459            redo A;            redo A;
460          }          }
461        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
462          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_input_character} == 0x0021) { # !
463            $self->{state} = 'markup declaration open';            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
464            !!!next-input-character;            !!!next-input-character;
465            redo A;            redo A;
466          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_input_character} == 0x002F) { # /
467            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
468            !!!next-input-character;            !!!next-input-character;
469            redo A;            redo A;
470          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_input_character} and
471                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_input_character} <= 0x005A) { # A..Z
472            $self->{current_token}            $self->{current_token}
473              = {type => 'start tag',              = {type => START_TAG_TOKEN,
474                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_input_character} + 0x0020)};
475            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
476            !!!next-input-character;            !!!next-input-character;
477            redo A;            redo A;
478          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_input_character} and
479                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_input_character} <= 0x007A) { # a..z
480            $self->{current_token} = {type => 'start tag',            $self->{current_token} = {type => START_TAG_TOKEN,
481                              tag_name => chr ($self->{next_input_character})};                              tag_name => chr ($self->{next_input_character})};
482            $self->{state} = 'tag name';            $self->{state} = TAG_NAME_STATE;
483            !!!next-input-character;            !!!next-input-character;
484            redo A;            redo A;
485          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_input_character} == 0x003E) { # >
486            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag');
487            $self->{state} = 'data';            $self->{state} = DATA_STATE;
488            !!!next-input-character;            !!!next-input-character;
489    
490            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});
491    
492            redo A;            redo A;
493          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_input_character} == 0x003F) { # ?
494            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio');
495            $self->{state} = 'bogus comment';            $self->{state} = BOGUS_COMMENT_STATE;
496            ## $self->{next_input_character} is intentionally left as is            ## $self->{next_input_character} is intentionally left as is
497            redo A;            redo A;
498          } else {          } else {
499            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago');
500            $self->{state} = 'data';            $self->{state} = DATA_STATE;
501            ## reconsume            ## reconsume
502    
503            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<'});
504    
505            redo A;            redo A;
506          }          }
507        } else {        } else {
508          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
509        }        }
510      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
511        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
512          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
513            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
# Line 348  sub _get_next_token ($) { Line 522  sub _get_next_token ($) {
522              } else {              } else {
523                $self->{next_input_character} = shift @next_char; # reconsume                $self->{next_input_character} = shift @next_char; # reconsume
524                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
525                $self->{state} = 'data';                $self->{state} = DATA_STATE;
526    
527                !!!emit ({type => 'character', data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</'});
528        
529                redo A;                redo A;
530              }              }
# Line 367  sub _get_next_token ($) { Line 541  sub _get_next_token ($) {
541                    $self->{next_input_character} == -1) {                    $self->{next_input_character} == -1) {
542              $self->{next_input_character} = shift @next_char; # reconsume              $self->{next_input_character} = shift @next_char; # reconsume
543              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
544              $self->{state} = 'data';              $self->{state} = DATA_STATE;
545              !!!emit ({type => 'character', data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</'});
546              redo A;              redo A;
547            } else {            } else {
548              $self->{next_input_character} = shift @next_char;              $self->{next_input_character} = shift @next_char;
# Line 378  sub _get_next_token ($) { Line 552  sub _get_next_token ($) {
552          } else {          } else {
553            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
554            # next-input-character is already done            # next-input-character is already done
555            $self->{state} = 'data';            $self->{state} = DATA_STATE;
556            !!!emit ({type => 'character', data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</'});
557            redo A;            redo A;
558          }          }
559        }        }
560                
561        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_input_character} and
562            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_input_character} <= 0x005A) { # A..Z
563          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
564                            tag_name => chr ($self->{next_input_character} + 0x0020)};                            tag_name => chr ($self->{next_input_character} + 0x0020)};
565          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
566          !!!next-input-character;          !!!next-input-character;
567          redo A;          redo A;
568        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_input_character} and
569                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_input_character} <= 0x007A) { # a..z
570          $self->{current_token} = {type => 'end tag',          $self->{current_token} = {type => END_TAG_TOKEN,
571                            tag_name => chr ($self->{next_input_character})};                            tag_name => chr ($self->{next_input_character})};
572          $self->{state} = 'tag name';          $self->{state} = TAG_NAME_STATE;
573          !!!next-input-character;          !!!next-input-character;
574          redo A;          redo A;
575        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
576          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag');
577          $self->{state} = 'data';          $self->{state} = DATA_STATE;
578          !!!next-input-character;          !!!next-input-character;
579          redo A;          redo A;
580        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
581          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
582          $self->{state} = 'data';          $self->{state} = DATA_STATE;
583          # reconsume          # reconsume
584    
585          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</'});
586    
587          redo A;          redo A;
588        } else {        } else {
589          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
590          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
591          ## $self->{next_input_character} is intentionally left as is          ## $self->{next_input_character} is intentionally left as is
592          redo A;          redo A;
593        }        }
594      } elsif ($self->{state} eq 'tag name') {      } elsif ($self->{state} == TAG_NAME_STATE) {
595        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
596            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
597            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
598            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
599            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
600          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
601          !!!next-input-character;          !!!next-input-character;
602          redo A;          redo A;
603        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
604          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
605            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
606                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
607            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
608          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
609            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
610            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
611              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 439  sub _get_next_token ($) { Line 613  sub _get_next_token ($) {
613          } else {          } else {
614            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
615          }          }
616          $self->{state} = 'data';          $self->{state} = DATA_STATE;
617          !!!next-input-character;          !!!next-input-character;
618    
619          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 454  sub _get_next_token ($) { Line 628  sub _get_next_token ($) {
628          redo A;          redo A;
629        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
630          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
631          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
632            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
633                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
634            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
635          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
636            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
637            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
638              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 466  sub _get_next_token ($) { Line 640  sub _get_next_token ($) {
640          } else {          } else {
641            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
642          }          }
643          $self->{state} = 'data';          $self->{state} = DATA_STATE;
644          # reconsume          # reconsume
645    
646          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 475  sub _get_next_token ($) { Line 649  sub _get_next_token ($) {
649        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
650          !!!next-input-character;          !!!next-input-character;
651          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
652              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
653              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
654            # permitted slash            # permitted slash
655            #            #
656          } else {          } else {
657            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
658          }          }
659          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
660          # next-input-character is already done          # next-input-character is already done
661          redo A;          redo A;
662        } else {        } else {
# Line 492  sub _get_next_token ($) { Line 666  sub _get_next_token ($) {
666          !!!next-input-character;          !!!next-input-character;
667          redo A;          redo A;
668        }        }
669      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
670        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
671            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
672            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 502  sub _get_next_token ($) { Line 676  sub _get_next_token ($) {
676          !!!next-input-character;          !!!next-input-character;
677          redo A;          redo A;
678        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
679          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
680            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
681                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
682            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
683          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
684            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
685            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
686              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 514  sub _get_next_token ($) { Line 688  sub _get_next_token ($) {
688          } else {          } else {
689            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
690          }          }
691          $self->{state} = 'data';          $self->{state} = DATA_STATE;
692          !!!next-input-character;          !!!next-input-character;
693    
694          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 524  sub _get_next_token ($) { Line 698  sub _get_next_token ($) {
698                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
699          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
700                                value => ''};                                value => ''};
701          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
702          !!!next-input-character;          !!!next-input-character;
703          redo A;          redo A;
704        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
705          !!!next-input-character;          !!!next-input-character;
706          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
707              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
708              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
709            # permitted slash            # permitted slash
710            #            #
# Line 542  sub _get_next_token ($) { Line 716  sub _get_next_token ($) {
716          redo A;          redo A;
717        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
718          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
719          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
720            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
721                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
722            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
723          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
724            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
725            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
726              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 554  sub _get_next_token ($) { Line 728  sub _get_next_token ($) {
728          } else {          } else {
729            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
730          }          }
731          $self->{state} = 'data';          $self->{state} = DATA_STATE;
732          # reconsume          # reconsume
733    
734          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 563  sub _get_next_token ($) { Line 737  sub _get_next_token ($) {
737        } else {        } else {
738          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
739                                value => ''};                                value => ''};
740          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
741          !!!next-input-character;          !!!next-input-character;
742          redo A;          redo A;
743        }        }
744      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
745        my $before_leave = sub {        my $before_leave = sub {
746          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
747              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
# Line 585  sub _get_next_token ($) { Line 759  sub _get_next_token ($) {
759            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
760            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
761          $before_leave->();          $before_leave->();
762          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
763          !!!next-input-character;          !!!next-input-character;
764          redo A;          redo A;
765        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
766          $before_leave->();          $before_leave->();
767          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
768          !!!next-input-character;          !!!next-input-character;
769          redo A;          redo A;
770        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
771          $before_leave->();          $before_leave->();
772          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
773            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
774                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
775            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
776          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
777            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
778            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
779              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 607  sub _get_next_token ($) { Line 781  sub _get_next_token ($) {
781          } else {          } else {
782            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
783          }          }
784          $self->{state} = 'data';          $self->{state} = DATA_STATE;
785          !!!next-input-character;          !!!next-input-character;
786    
787          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 623  sub _get_next_token ($) { Line 797  sub _get_next_token ($) {
797          $before_leave->();          $before_leave->();
798          !!!next-input-character;          !!!next-input-character;
799          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
800              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
801              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
802            # permitted slash            # permitted slash
803            #            #
804          } else {          } else {
805            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
806          }          }
807          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
808          # next-input-character is already done          # next-input-character is already done
809          redo A;          redo A;
810        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
811          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
812          $before_leave->();          $before_leave->();
813          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
814            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
815                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
816            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
817          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
818            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
819            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
820              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 648  sub _get_next_token ($) { Line 822  sub _get_next_token ($) {
822          } else {          } else {
823            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
824          }          }
825          $self->{state} = 'data';          $self->{state} = DATA_STATE;
826          # reconsume          # reconsume
827    
828          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 660  sub _get_next_token ($) { Line 834  sub _get_next_token ($) {
834          !!!next-input-character;          !!!next-input-character;
835          redo A;          redo A;
836        }        }
837      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
838        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
839            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
840            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 670  sub _get_next_token ($) { Line 844  sub _get_next_token ($) {
844          !!!next-input-character;          !!!next-input-character;
845          redo A;          redo A;
846        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_input_character} == 0x003D) { # =
847          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
848          !!!next-input-character;          !!!next-input-character;
849          redo A;          redo A;
850        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
851          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
852            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
853                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
854            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
855          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
856            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
857            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
858              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 686  sub _get_next_token ($) { Line 860  sub _get_next_token ($) {
860          } else {          } else {
861            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
862          }          }
863          $self->{state} = 'data';          $self->{state} = DATA_STATE;
864          !!!next-input-character;          !!!next-input-character;
865    
866          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 696  sub _get_next_token ($) { Line 870  sub _get_next_token ($) {
870                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_input_character} <= 0x005A) { # A..Z
871          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),
872                                value => ''};                                value => ''};
873          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
874          !!!next-input-character;          !!!next-input-character;
875          redo A;          redo A;
876        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_input_character} == 0x002F) { # /
877          !!!next-input-character;          !!!next-input-character;
878          if ($self->{next_input_character} == 0x003E and # >          if ($self->{next_input_character} == 0x003E and # >
879              $self->{current_token}->{type} eq 'start tag' and              $self->{current_token}->{type} == START_TAG_TOKEN and
880              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {
881            # permitted slash            # permitted slash
882            #            #
# Line 710  sub _get_next_token ($) { Line 884  sub _get_next_token ($) {
884            !!!parse-error (type => 'nestc');            !!!parse-error (type => 'nestc');
885            ## TODO: Different error type for <aa / bb> than <aa/>            ## TODO: Different error type for <aa / bb> than <aa/>
886          }          }
887          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
888          # next-input-character is already done          # next-input-character is already done
889          redo A;          redo A;
890        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
891          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
892          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
893            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
894                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
895            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
896          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
897            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
898            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
899              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 727  sub _get_next_token ($) { Line 901  sub _get_next_token ($) {
901          } else {          } else {
902            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
903          }          }
904          $self->{state} = 'data';          $self->{state} = DATA_STATE;
905          # reconsume          # reconsume
906    
907          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 736  sub _get_next_token ($) { Line 910  sub _get_next_token ($) {
910        } else {        } else {
911          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute} = {name => chr ($self->{next_input_character}),
912                                value => ''};                                value => ''};
913          $self->{state} = 'attribute name';          $self->{state} = ATTRIBUTE_NAME_STATE;
914          !!!next-input-character;          !!!next-input-character;
915          redo A;                  redo A;        
916        }        }
917      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
918        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
919            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
920            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 750  sub _get_next_token ($) { Line 924  sub _get_next_token ($) {
924          !!!next-input-character;          !!!next-input-character;
925          redo A;          redo A;
926        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
927          $self->{state} = 'attribute value (double-quoted)';          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
928          !!!next-input-character;          !!!next-input-character;
929          redo A;          redo A;
930        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
931          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
932          ## reconsume          ## reconsume
933          redo A;          redo A;
934        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
935          $self->{state} = 'attribute value (single-quoted)';          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
936          !!!next-input-character;          !!!next-input-character;
937          redo A;          redo A;
938        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
939          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
940            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
941                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
942            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
943          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
944            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
945            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
946              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 774  sub _get_next_token ($) { Line 948  sub _get_next_token ($) {
948          } else {          } else {
949            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
950          }          }
951          $self->{state} = 'data';          $self->{state} = DATA_STATE;
952          !!!next-input-character;          !!!next-input-character;
953    
954          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 782  sub _get_next_token ($) { Line 956  sub _get_next_token ($) {
956          redo A;          redo A;
957        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
958          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
959          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
960            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
961                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
962            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
963          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
964            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
965            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
966              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 794  sub _get_next_token ($) { Line 968  sub _get_next_token ($) {
968          } else {          } else {
969            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
970          }          }
971          $self->{state} = 'data';          $self->{state} = DATA_STATE;
972          ## reconsume          ## reconsume
973    
974          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 802  sub _get_next_token ($) { Line 976  sub _get_next_token ($) {
976          redo A;          redo A;
977        } else {        } else {
978          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});
979          $self->{state} = 'attribute value (unquoted)';          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
980          !!!next-input-character;          !!!next-input-character;
981          redo A;          redo A;
982        }        }
983      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
984        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
985          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
986          !!!next-input-character;          !!!next-input-character;
987          redo A;          redo A;
988        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
989          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          $self->{last_attribute_value_state} = $self->{state};
990          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
991          !!!next-input-character;          !!!next-input-character;
992          redo A;          redo A;
993        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
994          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
995          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
996            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
997                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
998            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
999          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1000            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1001            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1002              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 830  sub _get_next_token ($) { Line 1004  sub _get_next_token ($) {
1004          } else {          } else {
1005            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1006          }          }
1007          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1008          ## reconsume          ## reconsume
1009    
1010          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 842  sub _get_next_token ($) { Line 1016  sub _get_next_token ($) {
1016          !!!next-input-character;          !!!next-input-character;
1017          redo A;          redo A;
1018        }        }
1019      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1020        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1021          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1022          !!!next-input-character;          !!!next-input-character;
1023          redo A;          redo A;
1024        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
1025          $self->{last_attribute_value_state} = 'attribute value (single-quoted)';          $self->{last_attribute_value_state} = $self->{state};
1026          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1027          !!!next-input-character;          !!!next-input-character;
1028          redo A;          redo A;
1029        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1030          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1031          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1032            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1033                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1034            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1035          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1036            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1037            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1038              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 866  sub _get_next_token ($) { Line 1040  sub _get_next_token ($) {
1040          } else {          } else {
1041            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1042          }          }
1043          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1044          ## reconsume          ## reconsume
1045    
1046          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 878  sub _get_next_token ($) { Line 1052  sub _get_next_token ($) {
1052          !!!next-input-character;          !!!next-input-character;
1053          redo A;          redo A;
1054        }        }
1055      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1056        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1057            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1058            $self->{next_input_character} == 0x000B or # HT            $self->{next_input_character} == 0x000B or # HT
1059            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1060            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1061          $self->{state} = 'before attribute name';          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1062          !!!next-input-character;          !!!next-input-character;
1063          redo A;          redo A;
1064        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_input_character} == 0x0026) { # &
1065          $self->{last_attribute_value_state} = 'attribute value (unquoted)';          $self->{last_attribute_value_state} = $self->{state};
1066          $self->{state} = 'entity in attribute value';          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1067          !!!next-input-character;          !!!next-input-character;
1068          redo A;          redo A;
1069        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1070          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1071            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1072                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1073            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1074          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1075            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1076            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1077              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 905  sub _get_next_token ($) { Line 1079  sub _get_next_token ($) {
1079          } else {          } else {
1080            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1081          }          }
1082          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1083          !!!next-input-character;          !!!next-input-character;
1084    
1085          !!!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 1087  sub _get_next_token ($) {
1087          redo A;          redo A;
1088        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1089          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1090          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1091            $self->{current_token}->{first_start_tag}            $self->{current_token}->{first_start_tag}
1092                = not defined $self->{last_emitted_start_tag_name};                = not defined $self->{last_emitted_start_tag_name};
1093            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1094          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1095            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1096            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1097              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 925  sub _get_next_token ($) { Line 1099  sub _get_next_token ($) {
1099          } else {          } else {
1100            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1101          }          }
1102          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1103          ## reconsume          ## reconsume
1104    
1105          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
# Line 937  sub _get_next_token ($) { Line 1111  sub _get_next_token ($) {
1111          !!!next-input-character;          !!!next-input-character;
1112          redo A;          redo A;
1113        }        }
1114      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1115        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);
1116    
1117        unless (defined $token) {        unless (defined $token) {
1118          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1119        } else {        } else {
1120          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1121            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1122          ## 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"
1123        }        }
1124    
1125        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1126        # next-input-character is already done        # next-input-character is already done
1127        redo A;        redo A;
1128      } elsif ($self->{state} eq 'bogus comment') {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1129        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1130                
1131        my $token = {type => 'comment', data => ''};        my $token = {type => COMMENT_TOKEN, data => ''};
1132    
1133        BC: {        BC: {
1134          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_input_character} == 0x003E) { # >
1135            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1136            !!!next-input-character;            !!!next-input-character;
1137    
1138            !!!emit ($token);            !!!emit ($token);
1139    
1140            redo A;            redo A;
1141          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_input_character} == -1) {
1142            $self->{state} = 'data';            $self->{state} = DATA_STATE;
1143            ## reconsume            ## reconsume
1144    
1145            !!!emit ($token);            !!!emit ($token);
# Line 976  sub _get_next_token ($) { Line 1151  sub _get_next_token ($) {
1151            redo BC;            redo BC;
1152          }          }
1153        } # BC        } # BC
1154      } elsif ($self->{state} eq 'markup declaration open') {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1155        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1156    
1157        my @next_char;        my @next_char;
# Line 986  sub _get_next_token ($) { Line 1161  sub _get_next_token ($) {
1161          !!!next-input-character;          !!!next-input-character;
1162          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_input_character};
1163          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_input_character} == 0x002D) { # -
1164            $self->{current_token} = {type => 'comment', data => ''};            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};
1165            $self->{state} = 'comment start';            $self->{state} = COMMENT_START_STATE;
1166            !!!next-input-character;            !!!next-input-character;
1167            redo A;            redo A;
1168          }          }
# Line 1018  sub _get_next_token ($) { Line 1193  sub _get_next_token ($) {
1193                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_input_character} == 0x0045 or # E
1194                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_input_character} == 0x0065) { # e
1195                      ## ISSUE: What a stupid code this is!                      ## ISSUE: What a stupid code this is!
1196                      $self->{state} = 'DOCTYPE';                      $self->{state} = DOCTYPE_STATE;
1197                      !!!next-input-character;                      !!!next-input-character;
1198                      redo A;                      redo A;
1199                    }                    }
# Line 1032  sub _get_next_token ($) { Line 1207  sub _get_next_token ($) {
1207        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
1208        $self->{next_input_character} = shift @next_char;        $self->{next_input_character} = shift @next_char;
1209        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
1210        $self->{state} = 'bogus comment';        $self->{state} = BOGUS_COMMENT_STATE;
1211        redo A;        redo A;
1212                
1213        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
1214        ## 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?
1215      } elsif ($self->{state} eq 'comment start') {      } elsif ($self->{state} == COMMENT_START_STATE) {
1216        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1217          $self->{state} = 'comment start dash';          $self->{state} = COMMENT_START_DASH_STATE;
1218          !!!next-input-character;          !!!next-input-character;
1219          redo A;          redo A;
1220        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1221          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1222          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1223          !!!next-input-character;          !!!next-input-character;
1224    
1225          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1052  sub _get_next_token ($) { Line 1227  sub _get_next_token ($) {
1227          redo A;          redo A;
1228        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1229          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1230          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1231          ## reconsume          ## reconsume
1232    
1233          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1061  sub _get_next_token ($) { Line 1236  sub _get_next_token ($) {
1236        } else {        } else {
1237          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1238              .= chr ($self->{next_input_character});              .= chr ($self->{next_input_character});
1239          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1240          !!!next-input-character;          !!!next-input-character;
1241          redo A;          redo A;
1242        }        }
1243      } elsif ($self->{state} eq 'comment start dash') {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
1244        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1245          $self->{state} = 'comment end';          $self->{state} = COMMENT_END_STATE;
1246          !!!next-input-character;          !!!next-input-character;
1247          redo A;          redo A;
1248        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1249          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
1250          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1251          !!!next-input-character;          !!!next-input-character;
1252    
1253          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1080  sub _get_next_token ($) { Line 1255  sub _get_next_token ($) {
1255          redo A;          redo A;
1256        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1257          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1258          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1259          ## reconsume          ## reconsume
1260    
1261          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1089  sub _get_next_token ($) { Line 1264  sub _get_next_token ($) {
1264        } else {        } else {
1265          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
1266              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_input_character});
1267          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1268          !!!next-input-character;          !!!next-input-character;
1269          redo A;          redo A;
1270        }        }
1271      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_STATE) {
1272        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1273          $self->{state} = 'comment end dash';          $self->{state} = COMMENT_END_DASH_STATE;
1274          !!!next-input-character;          !!!next-input-character;
1275          redo A;          redo A;
1276        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1277          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1278          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1279          ## reconsume          ## reconsume
1280    
1281          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1112  sub _get_next_token ($) { Line 1287  sub _get_next_token ($) {
1287          !!!next-input-character;          !!!next-input-character;
1288          redo A;          redo A;
1289        }        }
1290      } elsif ($self->{state} eq 'comment end dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
1291        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_input_character} == 0x002D) { # -
1292          $self->{state} = 'comment end';          $self->{state} = COMMENT_END_STATE;
1293          !!!next-input-character;          !!!next-input-character;
1294          redo A;          redo A;
1295        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1296          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1297          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1298          ## reconsume          ## reconsume
1299    
1300          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1127  sub _get_next_token ($) { Line 1302  sub _get_next_token ($) {
1302          redo A;          redo A;
1303        } else {        } else {
1304          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment
1305          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1306          !!!next-input-character;          !!!next-input-character;
1307          redo A;          redo A;
1308        }        }
1309      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
1310        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1311          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1312          !!!next-input-character;          !!!next-input-character;
1313    
1314          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1147  sub _get_next_token ($) { Line 1322  sub _get_next_token ($) {
1322          redo A;          redo A;
1323        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1324          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
1325          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1326          ## reconsume          ## reconsume
1327    
1328          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
# Line 1156  sub _get_next_token ($) { Line 1331  sub _get_next_token ($) {
1331        } else {        } else {
1332          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment');
1333          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment
1334          $self->{state} = 'comment';          $self->{state} = COMMENT_STATE;
1335          !!!next-input-character;          !!!next-input-character;
1336          redo A;          redo A;
1337        }        }
1338      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
1339        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1340            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1341            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1342            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1343            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1344          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1345          !!!next-input-character;          !!!next-input-character;
1346          redo A;          redo A;
1347        } else {        } else {
1348          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
1349          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
1350          ## reconsume          ## reconsume
1351          redo A;          redo A;
1352        }        }
1353      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
1354        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1355            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1356            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1186  sub _get_next_token ($) { Line 1361  sub _get_next_token ($) {
1361          redo A;          redo A;
1362        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1363          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1364          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1365          !!!next-input-character;          !!!next-input-character;
1366    
1367          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1368    
1369          redo A;          redo A;
1370        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1371          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
1372          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1373          ## reconsume          ## reconsume
1374    
1375          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect
1376    
1377          redo A;          redo A;
1378        } else {        } else {
1379          $self->{current_token}          $self->{current_token}
1380              = {type => 'DOCTYPE',              = {type => DOCTYPE_TOKEN,
1381                 name => chr ($self->{next_input_character}),                 name => chr ($self->{next_input_character}),
1382                 correct => 1};                 correct => 1};
1383  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
1384          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
1385          !!!next-input-character;          !!!next-input-character;
1386          redo A;          redo A;
1387        }        }
1388      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
1389  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
1390        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1391            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1392            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
1393            $self->{next_input_character} == 0x000C or # FF            $self->{next_input_character} == 0x000C or # FF
1394            $self->{next_input_character} == 0x0020) { # SP            $self->{next_input_character} == 0x0020) { # SP
1395          $self->{state} = 'after DOCTYPE name';          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
1396          !!!next-input-character;          !!!next-input-character;
1397          redo A;          redo A;
1398        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1399          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1400          !!!next-input-character;          !!!next-input-character;
1401    
1402          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1229  sub _get_next_token ($) { Line 1404  sub _get_next_token ($) {
1404          redo A;          redo A;
1405        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1406          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1407          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1408          ## reconsume          ## reconsume
1409    
1410          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1243  sub _get_next_token ($) { Line 1418  sub _get_next_token ($) {
1418          !!!next-input-character;          !!!next-input-character;
1419          redo A;          redo A;
1420        }        }
1421      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
1422        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_input_character} == 0x0009 or # HT
1423            $self->{next_input_character} == 0x000A or # LF            $self->{next_input_character} == 0x000A or # LF
1424            $self->{next_input_character} == 0x000B or # VT            $self->{next_input_character} == 0x000B or # VT
# Line 1253  sub _get_next_token ($) { Line 1428  sub _get_next_token ($) {
1428          !!!next-input-character;          !!!next-input-character;
1429          redo A;          redo A;
1430        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1431          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1432          !!!next-input-character;          !!!next-input-character;
1433    
1434          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1261  sub _get_next_token ($) { Line 1436  sub _get_next_token ($) {
1436          redo A;          redo A;
1437        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1438          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1439          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1440          ## reconsume          ## reconsume
1441    
1442          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1285  sub _get_next_token ($) { Line 1460  sub _get_next_token ($) {
1460                  !!!next-input-character;                  !!!next-input-character;
1461                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_input_character} == 0x0043 or # C
1462                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_input_character} == 0x0063) { # c
1463                    $self->{state} = 'before DOCTYPE public identifier';                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1464                    !!!next-input-character;                    !!!next-input-character;
1465                    redo A;                    redo A;
1466                  }                  }
# Line 1312  sub _get_next_token ($) { Line 1487  sub _get_next_token ($) {
1487                  !!!next-input-character;                  !!!next-input-character;
1488                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_input_character} == 0x004D or # M
1489                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_input_character} == 0x006D) { # m
1490                    $self->{state} = 'before DOCTYPE system identifier';                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1491                    !!!next-input-character;                    !!!next-input-character;
1492                    redo A;                    redo A;
1493                  }                  }
# Line 1328  sub _get_next_token ($) { Line 1503  sub _get_next_token ($) {
1503        }        }
1504    
1505        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
1506        $self->{state} = 'bogus DOCTYPE';        $self->{state} = BOGUS_DOCTYPE_STATE;
1507        # next-input-character is already done        # next-input-character is already done
1508        redo A;        redo A;
1509      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1510        if ({        if ({
1511              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1512              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1341  sub _get_next_token ($) { Line 1516  sub _get_next_token ($) {
1516          redo A;          redo A;
1517        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_input_character} eq 0x0022) { # "
1518          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1519          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
1520          !!!next-input-character;          !!!next-input-character;
1521          redo A;          redo A;
1522        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_input_character} eq 0x0027) { # '
1523          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
1524          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
1525          !!!next-input-character;          !!!next-input-character;
1526          redo A;          redo A;
1527        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_input_character} eq 0x003E) { # >
1528          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
1529    
1530          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1531          !!!next-input-character;          !!!next-input-character;
1532    
1533          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1362  sub _get_next_token ($) { Line 1537  sub _get_next_token ($) {
1537        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1538          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1539    
1540          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1541          ## reconsume          ## reconsume
1542    
1543          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1371  sub _get_next_token ($) { Line 1546  sub _get_next_token ($) {
1546          redo A;          redo A;
1547        } else {        } else {
1548          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
1549          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1550          !!!next-input-character;          !!!next-input-character;
1551          redo A;          redo A;
1552        }        }
1553      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1554        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1555          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1556          !!!next-input-character;          !!!next-input-character;
1557          redo A;          redo A;
1558        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1559          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1560    
1561          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1562          ## reconsume          ## reconsume
1563    
1564          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1397  sub _get_next_token ($) { Line 1572  sub _get_next_token ($) {
1572          !!!next-input-character;          !!!next-input-character;
1573          redo A;          redo A;
1574        }        }
1575      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
1576        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1577          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1578          !!!next-input-character;          !!!next-input-character;
1579          redo A;          redo A;
1580        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1581          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1582    
1583          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1584          ## reconsume          ## reconsume
1585    
1586          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1419  sub _get_next_token ($) { Line 1594  sub _get_next_token ($) {
1594          !!!next-input-character;          !!!next-input-character;
1595          redo A;          redo A;
1596        }        }
1597      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1598        if ({        if ({
1599              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1600              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1429  sub _get_next_token ($) { Line 1604  sub _get_next_token ($) {
1604          redo A;          redo A;
1605        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1606          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1607          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1608          !!!next-input-character;          !!!next-input-character;
1609          redo A;          redo A;
1610        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1611          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1612          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1613          !!!next-input-character;          !!!next-input-character;
1614          redo A;          redo A;
1615        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1616          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1617          !!!next-input-character;          !!!next-input-character;
1618    
1619          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1447  sub _get_next_token ($) { Line 1622  sub _get_next_token ($) {
1622        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1623          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1624    
1625          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1626          ## reconsume          ## reconsume
1627    
1628          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1456  sub _get_next_token ($) { Line 1631  sub _get_next_token ($) {
1631          redo A;          redo A;
1632        } else {        } else {
1633          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
1634          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1635          !!!next-input-character;          !!!next-input-character;
1636          redo A;          redo A;
1637        }        }
1638      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1639        if ({        if ({
1640              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1641              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1470  sub _get_next_token ($) { Line 1645  sub _get_next_token ($) {
1645          redo A;          redo A;
1646        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1647          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1648          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1649          !!!next-input-character;          !!!next-input-character;
1650          redo A;          redo A;
1651        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1652          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1653          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1654          !!!next-input-character;          !!!next-input-character;
1655          redo A;          redo A;
1656        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1657          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
1658          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1659          !!!next-input-character;          !!!next-input-character;
1660    
1661          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1490  sub _get_next_token ($) { Line 1665  sub _get_next_token ($) {
1665        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1666          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1667    
1668          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1669          ## reconsume          ## reconsume
1670    
1671          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1499  sub _get_next_token ($) { Line 1674  sub _get_next_token ($) {
1674          redo A;          redo A;
1675        } else {        } else {
1676          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
1677          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1678          !!!next-input-character;          !!!next-input-character;
1679          redo A;          redo A;
1680        }        }
1681      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1682        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1683          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1684          !!!next-input-character;          !!!next-input-character;
1685          redo A;          redo A;
1686        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1687          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1688    
1689          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1690          ## reconsume          ## reconsume
1691    
1692          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1525  sub _get_next_token ($) { Line 1700  sub _get_next_token ($) {
1700          !!!next-input-character;          !!!next-input-character;
1701          redo A;          redo A;
1702        }        }
1703      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
1704        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1705          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1706          !!!next-input-character;          !!!next-input-character;
1707          redo A;          redo A;
1708        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1709          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1710    
1711          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1712          ## reconsume          ## reconsume
1713    
1714          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1547  sub _get_next_token ($) { Line 1722  sub _get_next_token ($) {
1722          !!!next-input-character;          !!!next-input-character;
1723          redo A;          redo A;
1724        }        }
1725      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1726        if ({        if ({
1727              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1728              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1556  sub _get_next_token ($) { Line 1731  sub _get_next_token ($) {
1731          !!!next-input-character;          !!!next-input-character;
1732          redo A;          redo A;
1733        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1734          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1735          !!!next-input-character;          !!!next-input-character;
1736    
1737          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1565  sub _get_next_token ($) { Line 1740  sub _get_next_token ($) {
1740        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1741          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1742    
1743          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1744          ## reconsume          ## reconsume
1745    
1746          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1574  sub _get_next_token ($) { Line 1749  sub _get_next_token ($) {
1749          redo A;          redo A;
1750        } else {        } else {
1751          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
1752          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1753          !!!next-input-character;          !!!next-input-character;
1754          redo A;          redo A;
1755        }        }
1756      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
1757        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1758          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1759          !!!next-input-character;          !!!next-input-character;
1760    
1761          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1589  sub _get_next_token ($) { Line 1764  sub _get_next_token ($) {
1764          redo A;          redo A;
1765        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1766          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1767          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1768          ## reconsume          ## reconsume
1769    
1770          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1670  sub _tokenize_attempt_to_consume_an_enti Line 1845  sub _tokenize_attempt_to_consume_an_enti
1845            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
1846          }          }
1847    
1848          return {type => 'character', data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code,
1849                    has_reference => 1};
1850        } # X        } # X
1851      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_input_character} and
1852               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_input_character} <= 0x0039) { # 0..9
# Line 1705  sub _tokenize_attempt_to_consume_an_enti Line 1881  sub _tokenize_attempt_to_consume_an_enti
1881          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
1882        }        }
1883                
1884        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};
1885      } else {      } else {
1886        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero');
1887        !!!back-next-input-character ($self->{next_input_character});        !!!back-next-input-character ($self->{next_input_character});
# Line 1753  sub _tokenize_attempt_to_consume_an_enti Line 1929  sub _tokenize_attempt_to_consume_an_enti
1929      }      }
1930            
1931      if ($match > 0) {      if ($match > 0) {
1932        return {type => 'character', data => $value};        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
1933      } elsif ($match < 0) {      } elsif ($match < 0) {
1934        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc');
1935        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
1936          return {type => 'character', data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
1937        } else {        } else {
1938          return {type => 'character', data => $value};          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
1939        }        }
1940      } else {      } else {
1941        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
1942        ## NOTE: No characters are consumed in the spec.        ## NOTE: "No characters are consumed" in the spec.
1943        return {type => 'character', data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value};
1944      }      }
1945    } else {    } else {
1946      ## no characters are consumed      ## no characters are consumed
# Line 1806  sub _construct_tree ($) { Line 1982  sub _construct_tree ($) {
1982        
1983    !!!next-token;    !!!next-token;
1984    
1985    $self->{insertion_mode} = 'before head';    $self->{insertion_mode} = BEFORE_HEAD_IM;
1986    undef $self->{form_element};    undef $self->{form_element};
1987    undef $self->{head_element};    undef $self->{head_element};
1988    $self->{open_elements} = [];    $self->{open_elements} = [];
# Line 1820  sub _construct_tree ($) { Line 1996  sub _construct_tree ($) {
1996  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
1997    my $self = shift;    my $self = shift;
1998    INITIAL: {    INITIAL: {
1999      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
2000        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
2001        ## error, switch to a conformance checking mode for another        ## error, switch to a conformance checking mode for another
2002        ## language.        ## language.
# Line 1947  sub _tree_construction_initial ($) { Line 2123  sub _tree_construction_initial ($) {
2123        !!!next-token;        !!!next-token;
2124        return;        return;
2125      } elsif ({      } elsif ({
2126                'start tag' => 1,                START_TAG_TOKEN, 1,
2127                'end tag' => 1,                END_TAG_TOKEN, 1,
2128                'end-of-file' => 1,                END_OF_FILE_TOKEN, 1,
2129               }->{$token->{type}}) {               }->{$token->{type}}) {
2130        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE');
2131        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2132        ## Go to the root element phase        ## Go to the root element phase
2133        ## reprocess        ## reprocess
2134        return;        return;
2135      } elsif ($token->{type} eq 'character') {      } elsif ($token->{type} == CHARACTER_TOKEN) {
2136        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2137          ## Ignore the token          ## Ignore the token
2138    
# Line 1972  sub _tree_construction_initial ($) { Line 2148  sub _tree_construction_initial ($) {
2148        ## Go to the root element phase        ## Go to the root element phase
2149        ## reprocess        ## reprocess
2150        return;        return;
2151      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
2152        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2153        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
2154                
# Line 1980  sub _tree_construction_initial ($) { Line 2156  sub _tree_construction_initial ($) {
2156        !!!next-token;        !!!next-token;
2157        redo INITIAL;        redo INITIAL;
2158      } else {      } else {
2159        die "$0: $token->{type}: Unknown token";        die "$0: $token->{type}: Unknown token type";
2160      }      }
2161    } # INITIAL    } # INITIAL
2162  } # _tree_construction_initial  } # _tree_construction_initial
# Line 1989  sub _tree_construction_root_element ($) Line 2165  sub _tree_construction_root_element ($)
2165    my $self = shift;    my $self = shift;
2166        
2167    B: {    B: {
2168        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
2169          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE');
2170          ## Ignore the token          ## Ignore the token
2171          ## Stay in the phase          ## Stay in the phase
2172          !!!next-token;          !!!next-token;
2173          redo B;          redo B;
2174        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
2175          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2176          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2177          ## Stay in the phase          ## Stay in the phase
2178          !!!next-token;          !!!next-token;
2179          redo B;          redo B;
2180        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
2181          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2182            ## Ignore the token.            ## Ignore the token.
2183    
# Line 2011  sub _tree_construction_root_element ($) Line 2187  sub _tree_construction_root_element ($)
2187              redo B;              redo B;
2188            }            }
2189          }          }
2190    
2191            $self->{application_cache_selection}->(undef);
2192    
2193            #
2194          } elsif ($token->{type} == START_TAG_TOKEN) {
2195            if ($token->{tag_name} eq 'html' and
2196                $token->{attributes}->{manifest}) {
2197              $self->{application_cache_selection}
2198                   ->($token->{attributes}->{manifest}->{value});
2199              ## ISSUE: No relative reference resolution?
2200            } else {
2201              $self->{application_cache_selection}->(undef);
2202            }
2203    
2204            ## ISSUE: There is an issue in the spec
2205          #          #
2206        } elsif ({        } elsif ({
2207                  'start tag' => 1,                  END_TAG_TOKEN, 1,
2208                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
2209                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2210            $self->{application_cache_selection}->(undef);
2211    
2212          ## ISSUE: There is an issue in the spec          ## ISSUE: There is an issue in the spec
2213          #          #
2214        } else {        } else {
2215          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
2216        }        }
2217    
2218        my $root_element; !!!create-element ($root_element, 'html');        my $root_element; !!!create-element ($root_element, 'html');
2219        $self->{document}->append_child ($root_element);        $self->{document}->append_child ($root_element);
2220        push @{$self->{open_elements}}, [$root_element, 'html'];        push @{$self->{open_elements}}, [$root_element, 'html'];
# Line 2062  sub _reset_insertion_mode ($) { Line 2255  sub _reset_insertion_mode ($) {
2255            
2256        ## Step 4..13        ## Step 4..13
2257        my $new_mode = {        my $new_mode = {
2258                        select => 'in select',                        select => IN_SELECT_IM,
2259                        td => 'in cell',                        td => IN_CELL_IM,
2260                        th => 'in cell',                        th => IN_CELL_IM,
2261                        tr => 'in row',                        tr => IN_ROW_IM,
2262                        tbody => 'in table body',                        tbody => IN_TABLE_BODY_IM,
2263                        thead => 'in table body',                        thead => IN_TABLE_BODY_IM,
2264                        tfoot => 'in table body',                        tfoot => IN_TABLE_BODY_IM,
2265                        caption => 'in caption',                        caption => IN_CAPTION_IM,
2266                        colgroup => 'in column group',                        colgroup => IN_COLUMN_GROUP_IM,
2267                        table => 'in table',                        table => IN_TABLE_IM,
2268                        head => 'in body', # not in head!                        head => IN_BODY_IM, # not in head!
2269                        body => 'in body',                        body => IN_BODY_IM,
2270                        frameset => 'in frameset',                        frameset => IN_FRAMESET_IM,
2271                       }->{$node->[1]};                       }->{$node->[1]};
2272        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2273                
2274        ## Step 14        ## Step 14
2275        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2276          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2277            $self->{insertion_mode} = 'before head';            $self->{insertion_mode} = BEFORE_HEAD_IM;
2278          } else {          } else {
2279            $self->{insertion_mode} = 'after head';            $self->{insertion_mode} = AFTER_HEAD_IM;
2280          }          }
2281          return;          return;
2282        }        }
2283                
2284        ## Step 15        ## Step 15
2285        $self->{insertion_mode} = 'in body' and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
2286                
2287        ## Step 16        ## Step 16
2288        $i--;        $i--;
# Line 2203  sub _tree_construction_main ($) { Line 2396  sub _tree_construction_main ($) {
2396      ## Step 4      ## Step 4
2397      my $text = '';      my $text = '';
2398      !!!next-token;      !!!next-token;
2399      while ($token->{type} eq 'character') { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2400        $text .= $token->{data};        $text .= $token->{data};
2401        !!!next-token;        !!!next-token;
2402      }      }
# Line 2218  sub _tree_construction_main ($) { Line 2411  sub _tree_construction_main ($) {
2411      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
2412    
2413      ## Step 7      ## Step 7
2414      if ($token->{type} eq 'end tag' and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {
2415        ## Ignore the token        ## Ignore the token
2416      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2417        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type});
# Line 2241  sub _tree_construction_main ($) { Line 2434  sub _tree_construction_main ($) {
2434            
2435      my $text = '';      my $text = '';
2436      !!!next-token;      !!!next-token;
2437      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
2438        $text .= $token->{data};        $text .= $token->{data};
2439        !!!next-token;        !!!next-token;
2440      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
# Line 2251  sub _tree_construction_main ($) { Line 2444  sub _tree_construction_main ($) {
2444                                
2445      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
2446    
2447      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
2448          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
2449        ## Ignore the token        ## Ignore the token
2450      } else {      } else {
# Line 2497  sub _tree_construction_main ($) { Line 2690  sub _tree_construction_main ($) {
2690    my $insert;    my $insert;
2691    
2692    B: {    B: {
2693      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
2694        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'DOCTYPE in the middle');
2695        ## Ignore the token        ## Ignore the token
2696        ## Stay in the phase        ## Stay in the phase
2697        !!!next-token;        !!!next-token;
2698        redo B;        redo B;
2699      } elsif ($token->{type} eq 'end-of-file') {      } elsif ($token->{type} == END_OF_FILE_TOKEN) {
2700        if ($self->{insertion_mode} eq 'after html body' or        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
           $self->{insertion_mode} eq 'after html frameset') {  
2701          #          #
2702        } else {        } else {
2703          ## Generate implied end tags          ## Generate implied end tags
# Line 2514  sub _tree_construction_main ($) { Line 2706  sub _tree_construction_main ($) {
2706               tbody => 1, tfoot=> 1, thead => 1,               tbody => 1, tfoot=> 1, thead => 1,
2707              }->{$self->{open_elements}->[-1]->[1]}) {              }->{$self->{open_elements}->[-1]->[1]}) {
2708            !!!back-token;            !!!back-token;
2709            $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};            $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};
2710            redo B;            redo B;
2711          }          }
2712                    
# Line 2532  sub _tree_construction_main ($) { Line 2724  sub _tree_construction_main ($) {
2724    
2725        ## Stop parsing        ## Stop parsing
2726        last B;        last B;
2727      } elsif ($token->{type} eq 'start tag' and      } elsif ($token->{type} == START_TAG_TOKEN and
2728               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
2729        if ($self->{insertion_mode} eq 'after html body') {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
2730          ## Turn into the main phase          ## Turn into the main phase
2731          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html');
2732          $self->{insertion_mode} = 'after body';          $self->{insertion_mode} = AFTER_BODY_IM;
2733        } elsif ($self->{insertion_mode} eq 'after html frameset') {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
2734          ## Turn into the main phase          ## Turn into the main phase
2735          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html');
2736          $self->{insertion_mode} = 'after frameset';          $self->{insertion_mode} = AFTER_FRAMESET_IM;
2737        }        }
2738    
2739  ## ISSUE: "aa<html>" is not a parse error.  ## ISSUE: "aa<html>" is not a parse error.
# Line 2559  sub _tree_construction_main ($) { Line 2751  sub _tree_construction_main ($) {
2751        }        }
2752        !!!next-token;        !!!next-token;
2753        redo B;        redo B;
2754      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
2755        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2756        if ($self->{insertion_mode} eq 'after html body' or        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
           $self->{insertion_mode} eq 'after html frameset') {  
2757          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2758        } elsif ($self->{insertion_mode} eq 'after body') {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
2759          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
2760        } else {        } else {
2761          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
2762        }        }
2763        !!!next-token;        !!!next-token;
2764        redo B;        redo B;
2765      } elsif ($self->{insertion_mode} eq 'in head' or      } elsif ($self->{insertion_mode} & HEAD_IMS) {
2766               $self->{insertion_mode} eq 'in head noscript' or        if ($token->{type} == CHARACTER_TOKEN) {
              $self->{insertion_mode} eq 'after head' or  
              $self->{insertion_mode} eq 'before head') {  
       if ($token->{type} eq 'character') {  
2767          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
2768            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
2769            unless (length $token->{data}) {            unless (length $token->{data}) {
# Line 2584  sub _tree_construction_main ($) { Line 2772  sub _tree_construction_main ($) {
2772            }            }
2773          }          }
2774    
2775          if ($self->{insertion_mode} eq 'before head') {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2776            ## As if <head>            ## As if <head>
2777            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, 'head');
2778            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
# Line 2594  sub _tree_construction_main ($) { Line 2782  sub _tree_construction_main ($) {
2782            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
2783    
2784            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
2785          } elsif ($self->{insertion_mode} eq 'in head noscript') {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2786            ## As if </noscript>            ## As if </noscript>
2787            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
2788            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character');
# Line 2604  sub _tree_construction_main ($) { Line 2792  sub _tree_construction_main ($) {
2792            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
2793    
2794            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
2795          } elsif ($self->{insertion_mode} eq 'in head') {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2796            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
2797    
2798            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
# Line 2613  sub _tree_construction_main ($) { Line 2801  sub _tree_construction_main ($) {
2801              ## "after head" insertion mode              ## "after head" insertion mode
2802              ## As if <body>              ## As if <body>
2803              !!!insert-element ('body');              !!!insert-element ('body');
2804              $self->{insertion_mode} = 'in body';              $self->{insertion_mode} = IN_BODY_IM;
2805              ## reprocess              ## reprocess
2806              redo B;              redo B;
2807            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
2808              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
2809                if ($self->{insertion_mode} eq 'before head') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2810                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
2811                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2812                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
2813                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
2814                  !!!next-token;                  !!!next-token;
2815                  redo B;                  redo B;
2816                } elsif ($self->{insertion_mode} ne 'after head') {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2817                    #
2818                  } else {
2819                  !!!parse-error (type => 'in head:head'); # or in head noscript                  !!!parse-error (type => 'in head:head'); # or in head noscript
2820                  ## Ignore the token                  ## Ignore the token
2821                  !!!next-token;                  !!!next-token;
2822                  redo B;                  redo B;
               } else {  
                 #  
2823                }                }
2824              } elsif ($self->{insertion_mode} eq 'before head') {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2825                ## As if <head>                ## As if <head>
2826                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
2827                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2828                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2829    
2830                $self->{insertion_mode} = 'in head';                $self->{insertion_mode} = IN_HEAD_IM;
2831                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
2832              }              }
2833    
2834              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
2835                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2836                  ## As if </noscript>                  ## As if </noscript>
2837                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2838                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base');
2839                                
2840                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
2841                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
2842                }                }
2843    
2844                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2845                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2846                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2847                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2848                }                }
2849                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
2850                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2851                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2852                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2853                !!!next-token;                !!!next-token;
2854                redo B;                redo B;
2855              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
2856                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2857                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2858                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2859                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2860                }                }
2861                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
2862                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2863                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2864                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2865                !!!next-token;                !!!next-token;
2866                redo B;                redo B;
2867              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
2868                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2869                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2870                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2871                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2872                }                }
2873                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
2874                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.
2875    
2876                unless ($self->{confident}) {                unless ($self->{confident}) {
                 my $charset;  
2877                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
2878                    $charset = $token->{attributes}->{charset}->{value};                    $self->{change_encoding}
2879                  }                        ->($self, $token->{attributes}->{charset}->{value});
2880                  if ($token->{attributes}->{'http-equiv'}) {                    
2881                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
2882                          ->set_user_data (manakai_has_reference =>
2883                                               $token->{attributes}->{charset}
2884                                                   ->{has_reference});
2885                    } elsif ($token->{attributes}->{content}) {
2886                    ## 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.
2887                    if ($token->{attributes}->{'http-equiv'}->{value}                    if ($token->{attributes}->{content}->{value}
2888                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
2889                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2890                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
2891                      $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                      $self->{change_encoding}
2892                    } ## TODO: And if supported                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
2893                      }
2894                    }
2895                  } else {
2896                    if ($token->{attributes}->{charset}) {
2897                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
2898                          ->set_user_data (manakai_has_reference =>
2899                                               $token->{attributes}->{charset}
2900                                                   ->{has_reference});
2901                  }                  }
                 ## TODO: Change the encoding  
2902                }                }
2903    
               ## TODO: Extracting |charset| from |meta|.  
2904                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2905                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2906                !!!next-token;                !!!next-token;
2907                redo B;                redo B;
2908              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
2909                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2910                  ## As if </noscript>                  ## As if </noscript>
2911                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2912                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title');
2913                                
2914                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
2915                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
2916                } elsif ($self->{insertion_mode} eq 'after head') {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2917                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2918                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2919                }                }
# Line 2726  sub _tree_construction_main ($) { Line 2924  sub _tree_construction_main ($) {
2924                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL,
2925                                sub { $parent->append_child ($_[0]) });                                sub { $parent->append_child ($_[0]) });
2926                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2927                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2928                redo B;                redo B;
2929              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
2930                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
2931                ## insertion mode 'in head')                ## insertion mode IN_HEAD_IM)
2932                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2933                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2934                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2935                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2936                }                }
2937                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
2938                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2939                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2940                redo B;                redo B;
2941              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
2942                if ($self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_IM) {
2943                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
2944                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2945                  $self->{insertion_mode} = 'in head noscript';                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
2946                  !!!next-token;                  !!!next-token;
2947                  redo B;                  redo B;
2948                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2949                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript:noscript');
2950                  ## Ignore the token                  ## Ignore the token
2951                  !!!next-token;                  !!!next-token;
# Line 2756  sub _tree_construction_main ($) { Line 2954  sub _tree_construction_main ($) {
2954                  #                  #
2955                }                }
2956              } elsif ($token->{tag_name} eq 'script') {              } elsif ($token->{tag_name} eq 'script') {
2957                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2958                  ## As if </noscript>                  ## As if </noscript>
2959                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2960                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script');
2961                                
2962                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
2963                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
2964                } elsif ($self->{insertion_mode} eq 'after head') {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2965                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2966                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2967                }                }
# Line 2771  sub _tree_construction_main ($) { Line 2969  sub _tree_construction_main ($) {
2969                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2970                $script_start_tag->($insert_to_current);                $script_start_tag->($insert_to_current);
2971                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2972                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2973                redo B;                redo B;
2974              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
2975                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
2976                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2977                  ## As if </noscript>                  ## As if </noscript>
2978                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2979                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});
# Line 2785  sub _tree_construction_main ($) { Line 2983  sub _tree_construction_main ($) {
2983                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2984                                    
2985                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
2986                } elsif ($self->{insertion_mode} eq 'in head') {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2987                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2988                                    
2989                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
# Line 2793  sub _tree_construction_main ($) { Line 2991  sub _tree_construction_main ($) {
2991    
2992                ## "after head" insertion mode                ## "after head" insertion mode
2993                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
2994                $self->{insertion_mode} = 'in '.$token->{tag_name};                if ($token->{tag_name} eq 'body') {
2995                    $self->{insertion_mode} = IN_BODY_IM;
2996                  } elsif ($token->{tag_name} eq 'frameset') {
2997                    $self->{insertion_mode} = IN_FRAMESET_IM;
2998                  } else {
2999                    die "$0: tag name: $self->{tag_name}";
3000                  }
3001                !!!next-token;                !!!next-token;
3002                redo B;                redo B;
3003              } else {              } else {
3004                #                #
3005              }              }
3006    
3007              if ($self->{insertion_mode} eq 'in head noscript') {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3008                ## As if </noscript>                ## As if </noscript>
3009                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3010                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
# Line 2810  sub _tree_construction_main ($) { Line 3014  sub _tree_construction_main ($) {
3014                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3015    
3016                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3017              } elsif ($self->{insertion_mode} eq 'in head') {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3018                ## As if </head>                ## As if </head>
3019                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3020    
# Line 2820  sub _tree_construction_main ($) { Line 3024  sub _tree_construction_main ($) {
3024              ## "after head" insertion mode              ## "after head" insertion mode
3025              ## As if <body>              ## As if <body>
3026              !!!insert-element ('body');              !!!insert-element ('body');
3027              $self->{insertion_mode} = 'in body';              $self->{insertion_mode} = IN_BODY_IM;
3028              ## reprocess              ## reprocess
3029              redo B;              redo B;
3030            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
3031              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
3032                if ($self->{insertion_mode} eq 'before head') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3033                  ## As if <head>                  ## As if <head>
3034                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head');
3035                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
# Line 2833  sub _tree_construction_main ($) { Line 3037  sub _tree_construction_main ($) {
3037    
3038                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3039                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3040                  $self->{insertion_mode} = 'after head';                  $self->{insertion_mode} = AFTER_HEAD_IM;
3041                  !!!next-token;                  !!!next-token;
3042                  redo B;                  redo B;
3043                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3044                  ## As if </noscript>                  ## As if </noscript>
3045                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3046                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script');
3047                                    
3048                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3049                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3050                  $self->{insertion_mode} = 'after head';                  $self->{insertion_mode} = AFTER_HEAD_IM;
3051                  !!!next-token;                  !!!next-token;
3052                  redo B;                  redo B;
3053                } elsif ($self->{insertion_mode} eq 'in head') {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3054                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3055                  $self->{insertion_mode} = 'after head';                  $self->{insertion_mode} = AFTER_HEAD_IM;
3056                  !!!next-token;                  !!!next-token;
3057                  redo B;                  redo B;
3058                } else {                } else {
3059                  #                  #
3060                }                }
3061              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
3062                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3063                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3064                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
3065                  !!!next-token;                  !!!next-token;
3066                  redo B;                  redo B;
3067                } elsif ($self->{insertion_mode} eq 'before head') {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3068                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!parse-error (type => 'unmatched end tag:noscript');
3069                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
3070                  !!!next-token;                  !!!next-token;
# Line 2871  sub _tree_construction_main ($) { Line 3075  sub _tree_construction_main ($) {
3075              } elsif ({              } elsif ({
3076                        body => 1, html => 1,                        body => 1, html => 1,
3077                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3078                if ($self->{insertion_mode} eq 'before head') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3079                  ## As if <head>                  ## As if <head>
3080                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head');
3081                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3082                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3083    
3084                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
3085                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3086                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3087                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3088                  ## Ignore the token                  ## Ignore the token
3089                  !!!next-token;                  !!!next-token;
# Line 2890  sub _tree_construction_main ($) { Line 3094  sub _tree_construction_main ($) {
3094              } elsif ({              } elsif ({
3095                        p => 1, br => 1,                        p => 1, br => 1,
3096                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3097                if ($self->{insertion_mode} eq 'before head') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3098                  ## As if <head>                  ## As if <head>
3099                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head');
3100                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3101                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3102    
3103                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
3104                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3105                }                }
3106    
3107                #                #
3108              } else {              } else {
3109                if ($self->{insertion_mode} ne 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3110                    #
3111                  } else {
3112                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3113                  ## Ignore the token                  ## Ignore the token
3114                  !!!next-token;                  !!!next-token;
3115                  redo B;                  redo B;
               } else {  
                 #  
3116                }                }
3117              }              }
3118    
3119              if ($self->{insertion_mode} eq 'in head noscript') {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3120                ## As if </noscript>                ## As if </noscript>
3121                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3122                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
# Line 2922  sub _tree_construction_main ($) { Line 3126  sub _tree_construction_main ($) {
3126                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3127    
3128                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3129              } elsif ($self->{insertion_mode} eq 'in head') {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3130                ## As if </head>                ## As if </head>
3131                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3132    
3133                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3134              } elsif ($self->{insertion_mode} eq 'before head') {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3135                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3136                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
3137                !!!next-token;                !!!next-token;
# Line 2937  sub _tree_construction_main ($) { Line 3141  sub _tree_construction_main ($) {
3141              ## "after head" insertion mode              ## "after head" insertion mode
3142              ## As if <body>              ## As if <body>
3143              !!!insert-element ('body');              !!!insert-element ('body');
3144              $self->{insertion_mode} = 'in body';              $self->{insertion_mode} = IN_BODY_IM;
3145              ## reprocess              ## reprocess
3146              redo B;              redo B;
3147            } else {            } else {
# Line 2945  sub _tree_construction_main ($) { Line 3149  sub _tree_construction_main ($) {
3149            }            }
3150    
3151            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
3152      } elsif ($self->{insertion_mode} eq 'in body' or      } elsif ($self->{insertion_mode} & BODY_IMS) {
3153               $self->{insertion_mode} eq 'in cell' or            if ($token->{type} == CHARACTER_TOKEN) {
              $self->{insertion_mode} eq 'in caption') {  
           if ($token->{type} eq 'character') {  
3154              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
3155              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
3156                            
# Line 2956  sub _tree_construction_main ($) { Line 3158  sub _tree_construction_main ($) {
3158    
3159              !!!next-token;              !!!next-token;
3160              redo B;              redo B;
3161            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
3162              if ({              if ({
3163                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
3164                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3165                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3166                if ($self->{insertion_mode} eq 'in cell') {                if ($self->{insertion_mode} == IN_CELL_IM) {
3167                  ## have an element in table scope                  ## have an element in table scope
3168                  my $tn;                  my $tn;
3169                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 2984  sub _tree_construction_main ($) { Line 3186  sub _tree_construction_main ($) {
3186                                    
3187                  ## Close the cell                  ## Close the cell
3188                  !!!back-token; # <?>                  !!!back-token; # <?>
3189                  $token = {type => 'end tag', tag_name => $tn};                  $token = {type => END_TAG_TOKEN, tag_name => $tn};
3190                  redo B;                  redo B;
3191                } elsif ($self->{insertion_mode} eq 'in caption') {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3192                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption');
3193                                    
3194                  ## As if </caption>                  ## As if </caption>
# Line 3017  sub _tree_construction_main ($) { Line 3219  sub _tree_construction_main ($) {
3219                       tbody => 1, tfoot=> 1, thead => 1,                       tbody => 1, tfoot=> 1, thead => 1,
3220                      }->{$self->{open_elements}->[-1]->[1]}) {                      }->{$self->{open_elements}->[-1]->[1]}) {
3221                    !!!back-token; # <?>                    !!!back-token; # <?>
3222                    $token = {type => 'end tag', tag_name => 'caption'};                    $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3223                    !!!back-token;                    !!!back-token;
3224                    $token = {type => 'end tag',                    $token = {type => END_TAG_TOKEN,
3225                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3226                    redo B;                    redo B;
3227                  }                  }
# Line 3032  sub _tree_construction_main ($) { Line 3234  sub _tree_construction_main ($) {
3234                                    
3235                  $clear_up_to_marker->();                  $clear_up_to_marker->();
3236                                    
3237                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3238                                    
3239                  ## reprocess                  ## reprocess
3240                  redo B;                  redo B;
# Line 3042  sub _tree_construction_main ($) { Line 3244  sub _tree_construction_main ($) {
3244              } else {              } else {
3245                #                #
3246              }              }
3247            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
3248              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3249                if ($self->{insertion_mode} eq 'in cell') {                if ($self->{insertion_mode} == IN_CELL_IM) {
3250                  ## have an element in table scope                  ## have an element in table scope
3251                  my $i;                  my $i;
3252                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3074  sub _tree_construction_main ($) { Line 3276  sub _tree_construction_main ($) {
3276                       tbody => 1, tfoot=> 1, thead => 1,                       tbody => 1, tfoot=> 1, thead => 1,
3277                      }->{$self->{open_elements}->[-1]->[1]}) {                      }->{$self->{open_elements}->[-1]->[1]}) {
3278                    !!!back-token;                    !!!back-token;
3279                    $token = {type => 'end tag',                    $token = {type => END_TAG_TOKEN,
3280                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3281                    redo B;                    redo B;
3282                  }                  }
# Line 3087  sub _tree_construction_main ($) { Line 3289  sub _tree_construction_main ($) {
3289                                    
3290                  $clear_up_to_marker->();                  $clear_up_to_marker->();
3291                                    
3292                  $self->{insertion_mode} = 'in row';                  $self->{insertion_mode} = IN_ROW_IM;
3293                                    
3294                  !!!next-token;                  !!!next-token;
3295                  redo B;                  redo B;
3296                } elsif ($self->{insertion_mode} eq 'in caption') {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3297                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3298                  ## Ignore the token                  ## Ignore the token
3299                  !!!next-token;                  !!!next-token;
# Line 3100  sub _tree_construction_main ($) { Line 3302  sub _tree_construction_main ($) {
3302                  #                  #
3303                }                }
3304              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
3305                if ($self->{insertion_mode} eq 'in caption') {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
3306                  ## have a table element in table scope                  ## have a table element in table scope
3307                  my $i;                  my $i;
3308                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3128  sub _tree_construction_main ($) { Line 3330  sub _tree_construction_main ($) {
3330                       tbody => 1, tfoot=> 1, thead => 1,                       tbody => 1, tfoot=> 1, thead => 1,
3331                      }->{$self->{open_elements}->[-1]->[1]}) {                      }->{$self->{open_elements}->[-1]->[1]}) {
3332                    !!!back-token;                    !!!back-token;
3333                    $token = {type => 'end tag',                    $token = {type => END_TAG_TOKEN,
3334                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3335                    redo B;                    redo B;
3336                  }                  }
# Line 3141  sub _tree_construction_main ($) { Line 3343  sub _tree_construction_main ($) {
3343                                    
3344                  $clear_up_to_marker->();                  $clear_up_to_marker->();
3345                                    
3346                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3347                                    
3348                  !!!next-token;                  !!!next-token;
3349                  redo B;                  redo B;
3350                } elsif ($self->{insertion_mode} eq 'in cell') {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
3351                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3352                  ## Ignore the token                  ## Ignore the token
3353                  !!!next-token;                  !!!next-token;
# Line 3157  sub _tree_construction_main ($) { Line 3359  sub _tree_construction_main ($) {
3359                        table => 1, tbody => 1, tfoot => 1,                        table => 1, tbody => 1, tfoot => 1,
3360                        thead => 1, tr => 1,                        thead => 1, tr => 1,
3361                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
3362                       $self->{insertion_mode} eq 'in cell') {                       $self->{insertion_mode} == IN_CELL_IM) {
3363                ## have an element in table scope                ## have an element in table scope
3364                my $i;                my $i;
3365                my $tn;                my $tn;
# Line 3185  sub _tree_construction_main ($) { Line 3387  sub _tree_construction_main ($) {
3387    
3388                ## Close the cell                ## Close the cell
3389                !!!back-token; # </?>                !!!back-token; # </?>
3390                $token = {type => 'end tag', tag_name => $tn};                $token = {type => END_TAG_TOKEN, tag_name => $tn};
3391                redo B;                redo B;
3392              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
3393                       $self->{insertion_mode} eq 'in caption') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
3394                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption');
3395    
3396                ## As if </caption>                ## As if </caption>
# Line 3219  sub _tree_construction_main ($) { Line 3421  sub _tree_construction_main ($) {
3421                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
3422                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3423                  !!!back-token; # </table>                  !!!back-token; # </table>
3424                  $token = {type => 'end tag', tag_name => 'caption'};                  $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3425                  !!!back-token;                  !!!back-token;
3426                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3427                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3428                  redo B;                  redo B;
3429                }                }
# Line 3234  sub _tree_construction_main ($) { Line 3436  sub _tree_construction_main ($) {
3436    
3437                $clear_up_to_marker->();                $clear_up_to_marker->();
3438    
3439                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
3440    
3441                ## reprocess                ## reprocess
3442                redo B;                redo B;
3443              } elsif ({              } elsif ({
3444                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
3445                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3446                if ($self->{insertion_mode} eq 'in cell' or                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
                   $self->{insertion_mode} eq 'in caption') {  
3447                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3448                  ## Ignore the token                  ## Ignore the token
3449                  !!!next-token;                  !!!next-token;
# Line 3254  sub _tree_construction_main ($) { Line 3455  sub _tree_construction_main ($) {
3455                        tbody => 1, tfoot => 1,                        tbody => 1, tfoot => 1,
3456                        thead => 1, tr => 1,                        thead => 1, tr => 1,
3457                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
3458                       $self->{insertion_mode} eq 'in caption') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
3459                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3460                ## Ignore the token                ## Ignore the token
3461                !!!next-token;                !!!next-token;
# Line 3268  sub _tree_construction_main ($) { Line 3469  sub _tree_construction_main ($) {
3469    
3470        $insert = $insert_to_current;        $insert = $insert_to_current;
3471        #        #
3472      } elsif ($self->{insertion_mode} eq 'in row' or      } elsif ($self->{insertion_mode} & TABLE_IMS) {
3473               $self->{insertion_mode} eq 'in table body' or        if ($token->{type} == CHARACTER_TOKEN) {
              $self->{insertion_mode} eq 'in table') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There are "character in table" code clones.  
3474              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3475                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3476                                
# Line 3329  sub _tree_construction_main ($) { Line 3527  sub _tree_construction_main ($) {
3527                            
3528              !!!next-token;              !!!next-token;
3529              redo B;              redo B;
3530            } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
3531              if ({              if ({
3532                   tr => ($self->{insertion_mode} ne 'in row'),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
3533                   th => 1, td => 1,                   th => 1, td => 1,
3534                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3535                if ($self->{insertion_mode} eq 'in table') {                if ($self->{insertion_mode} == IN_TABLE_IM) {
3536                  ## Clear back to table context                  ## Clear back to table context
3537                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
3538                         $self->{open_elements}->[-1]->[1] ne 'html') {                         $self->{open_elements}->[-1]->[1] ne 'html') {
# Line 3343  sub _tree_construction_main ($) { Line 3541  sub _tree_construction_main ($) {
3541                  }                  }
3542                                    
3543                  !!!insert-element ('tbody');                  !!!insert-element ('tbody');
3544                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3545                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
3546                }                }
3547    
3548                if ($self->{insertion_mode} eq 'in table body') {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3549                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
3550                    !!!parse-error (type => 'missing start tag:tr');                    !!!parse-error (type => 'missing start tag:tr');
3551                  }                  }
# Line 3360  sub _tree_construction_main ($) { Line 3558  sub _tree_construction_main ($) {
3558                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
3559                  }                  }
3560                                    
3561                  $self->{insertion_mode} = 'in row';                  $self->{insertion_mode} = IN_ROW_IM;
3562                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
3563                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3564                    !!!next-token;                    !!!next-token;
# Line 3380  sub _tree_construction_main ($) { Line 3578  sub _tree_construction_main ($) {
3578                }                }
3579                                
3580                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3581                $self->{insertion_mode} = 'in cell';                $self->{insertion_mode} = IN_CELL_IM;
3582    
3583                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
3584                                
# Line 3389  sub _tree_construction_main ($) { Line 3587  sub _tree_construction_main ($) {
3587              } elsif ({              } elsif ({
3588                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
3589                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3590                        tr => 1, # $self->{insertion_mode} eq 'in row'                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3591                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3592                if ($self->{insertion_mode} eq 'in row') {                if ($self->{insertion_mode} == IN_ROW_IM) {
3593                  ## As if </tr>                  ## As if </tr>
3594                  ## have an element in table scope                  ## have an element in table scope
3595                  my $i;                  my $i;
# Line 3422  sub _tree_construction_main ($) { Line 3620  sub _tree_construction_main ($) {
3620                  }                  }
3621                                    
3622                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
3623                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3624                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
3625                    ## reprocess                    ## reprocess
3626                    redo B;                    redo B;
# Line 3431  sub _tree_construction_main ($) { Line 3629  sub _tree_construction_main ($) {
3629                  }                  }
3630                }                }
3631    
3632                if ($self->{insertion_mode} eq 'in table body') {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3633                  ## have an element in table scope                  ## have an element in table scope
3634                  my $i;                  my $i;
3635                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3470  sub _tree_construction_main ($) { Line 3668  sub _tree_construction_main ($) {
3668                  ## nop by definition                  ## nop by definition
3669                                    
3670                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3671                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3672                  ## reprocess in "in table" insertion mode...                  ## reprocess in "in table" insertion mode...
3673                }                }
3674    
# Line 3483  sub _tree_construction_main ($) { Line 3681  sub _tree_construction_main ($) {
3681                  }                  }
3682                                    
3683                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup');
3684                  $self->{insertion_mode} = 'in column group';                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
3685                  ## reprocess                  ## reprocess
3686                  redo B;                  redo B;
3687                } elsif ({                } elsif ({
# Line 3503  sub _tree_construction_main ($) { Line 3701  sub _tree_construction_main ($) {
3701                                    
3702                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3703                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
3704                                             caption => 'in caption',                                             caption => IN_CAPTION_IM,
3705                                             colgroup => 'in column group',                                             colgroup => IN_COLUMN_GROUP_IM,
3706                                             tbody => 'in table body',                                             tbody => IN_TABLE_BODY_IM,
3707                                             tfoot => 'in table body',                                             tfoot => IN_TABLE_BODY_IM,
3708                                             thead => 'in table body',                                             thead => IN_TABLE_BODY_IM,
3709                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
3710                  !!!next-token;                  !!!next-token;
3711                  redo B;                  redo B;
# Line 3515  sub _tree_construction_main ($) { Line 3713  sub _tree_construction_main ($) {
3713                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
3714                }                }
3715              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
               ## NOTE: There are code clones for this "table in table"  
3716                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3717    
3718                ## As if </table>                ## As if </table>
# Line 3546  sub _tree_construction_main ($) { Line 3743  sub _tree_construction_main ($) {
3743                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
3744                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3745                  !!!back-token; # <table>                  !!!back-token; # <table>
3746                  $token = {type => 'end tag', tag_name => 'table'};                  $token = {type => END_TAG_TOKEN, tag_name => 'table'};
3747                  !!!back-token;                  !!!back-token;
3748                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3749                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3750                  redo B;                  redo B;
3751                }                }
# Line 3563  sub _tree_construction_main ($) { Line 3760  sub _tree_construction_main ($) {
3760    
3761                ## reprocess                ## reprocess
3762                redo B;                redo B;
3763              } else {          } else {
3764                #            !!!parse-error (type => 'in table:'.$token->{tag_name});
3765              }  
3766            } elsif ($token->{type} eq 'end tag') {            $insert = $insert_to_foster;
3767              #
3768            }
3769          } elsif ($token->{type} == END_TAG_TOKEN) {
3770              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
3771                  $self->{insertion_mode} eq 'in row') {                  $self->{insertion_mode} == IN_ROW_IM) {
3772                ## have an element in table scope                ## have an element in table scope
3773                my $i;                my $i;
3774                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3598  sub _tree_construction_main ($) { Line 3798  sub _tree_construction_main ($) {
3798                }                }
3799    
3800                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
3801                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
3802                !!!next-token;                !!!next-token;
3803                redo B;                redo B;
3804              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3805                if ($self->{insertion_mode} eq 'in row') {                if ($self->{insertion_mode} == IN_ROW_IM) {
3806                  ## As if </tr>                  ## As if </tr>
3807                  ## have an element in table scope                  ## have an element in table scope
3808                  my $i;                  my $i;
# Line 3633  sub _tree_construction_main ($) { Line 3833  sub _tree_construction_main ($) {
3833                  }                  }
3834                                    
3835                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
3836                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3837                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
3838                }                }
3839    
3840                if ($self->{insertion_mode} eq 'in table body') {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3841                  ## have an element in table scope                  ## have an element in table scope
3842                  my $i;                  my $i;
3843                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3676  sub _tree_construction_main ($) { Line 3876  sub _tree_construction_main ($) {
3876                  ## nop by definition                  ## nop by definition
3877                                    
3878                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3879                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3880                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
3881                }                }
3882    
# Line 3707  sub _tree_construction_main ($) { Line 3907  sub _tree_construction_main ($) {
3907                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
3908                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3909                  !!!back-token;                  !!!back-token;
3910                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3911                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3912                  redo B;                  redo B;
3913                }                }
# Line 3725  sub _tree_construction_main ($) { Line 3925  sub _tree_construction_main ($) {
3925              } elsif ({              } elsif ({
3926                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3927                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
3928                       ($self->{insertion_mode} eq 'in row' or                       $self->{insertion_mode} & ROW_IMS) {
3929                        $self->{insertion_mode} eq 'in table body')) {                if ($self->{insertion_mode} == IN_ROW_IM) {
               if ($self->{insertion_mode} eq 'in row') {  
3930                  ## have an element in table scope                  ## have an element in table scope
3931                  my $i;                  my $i;
3932                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3778  sub _tree_construction_main ($) { Line 3977  sub _tree_construction_main ($) {
3977                  }                  }
3978                                    
3979                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
3980                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3981                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
3982                }                }
3983    
# Line 3811  sub _tree_construction_main ($) { Line 4010  sub _tree_construction_main ($) {
4010                }                }
4011    
4012                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4013                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
4014                !!!next-token;                !!!next-token;
4015                redo B;                redo B;
4016              } elsif ({              } elsif ({
4017                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
4018                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
4019                        tr => 1, # $self->{insertion_mode} eq 'in row'                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4020                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} eq 'in table'                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
4021                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4022                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4023                ## Ignore the token                ## Ignore the token
4024                !!!next-token;                !!!next-token;
4025                redo B;                redo B;
4026              } else {          } else {
4027                #            !!!parse-error (type => 'in table:/'.$token->{tag_name});
             }  
           } else {  
             die "$0: $token->{type}: Unknown token type";  
           }  
   
       !!!parse-error (type => 'in table:'.$token->{tag_name});  
4028    
4029        $insert = $insert_to_foster;            $insert = $insert_to_foster;
4030        #            #
4031      } elsif ($self->{insertion_mode} eq 'in column group') {          }
4032            if ($token->{type} eq 'character') {        } else {
4033            die "$0: $token->{type}: Unknown token type";
4034          }
4035        } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
4036              if ($token->{type} == CHARACTER_TOKEN) {
4037              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4038                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4039                unless (length $token->{data}) {                unless (length $token->{data}) {
# Line 3846  sub _tree_construction_main ($) { Line 4043  sub _tree_construction_main ($) {
4043              }              }
4044                            
4045              #              #
4046            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
4047              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
4048                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
4049                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
# Line 3855  sub _tree_construction_main ($) { Line 4052  sub _tree_construction_main ($) {
4052              } else {              } else {
4053                #                #
4054              }              }
4055            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4056              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
4057                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] eq 'html') {
4058                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag:colgroup');
# Line 3864  sub _tree_construction_main ($) { Line 4061  sub _tree_construction_main ($) {
4061                  redo B;                  redo B;
4062                } else {                } else {
4063                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
4064                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
4065                  !!!next-token;                  !!!next-token;
4066                  redo B;                              redo B;            
4067                }                }
# Line 3888  sub _tree_construction_main ($) { Line 4085  sub _tree_construction_main ($) {
4085              redo B;              redo B;
4086            } else {            } else {
4087              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
4088              $self->{insertion_mode} = 'in table';              $self->{insertion_mode} = IN_TABLE_IM;
4089              ## reprocess              ## reprocess
4090              redo B;              redo B;
4091            }            }
4092      } elsif ($self->{insertion_mode} eq 'in select') {      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
4093            if ($token->{type} eq 'character') {        if ($token->{type} == CHARACTER_TOKEN) {
4094              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4095              !!!next-token;          !!!next-token;
4096              redo B;          redo B;
4097            } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4098              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
4099                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4100                  ## As if </option>                  ## As if </option>
# Line 3950  sub _tree_construction_main ($) { Line 4147  sub _tree_construction_main ($) {
4147    
4148                !!!next-token;                !!!next-token;
4149                redo B;                redo B;
4150              } else {          } else {
4151                #            !!!parse-error (type => 'in select:'.$token->{tag_name});
4152              }            ## Ignore the token
4153            } elsif ($token->{type} eq 'end tag') {            !!!next-token;
4154              redo B;
4155            }
4156          } elsif ($token->{type} == END_TAG_TOKEN) {
4157              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
4158                if ($self->{open_elements}->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
4159                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
# Line 4055  sub _tree_construction_main ($) { Line 4255  sub _tree_construction_main ($) {
4255    
4256                ## reprocess                ## reprocess
4257                redo B;                redo B;
4258              } else {          } else {
4259                #            !!!parse-error (type => 'in select:/'.$token->{tag_name});
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in select:'.$token->{tag_name});  
4260            ## Ignore the token            ## Ignore the token
4261            !!!next-token;            !!!next-token;
4262            redo B;            redo B;
4263      } elsif ($self->{insertion_mode} eq 'after body' or          }
4264               $self->{insertion_mode} eq 'after html body') {        } else {
4265        if ($token->{type} eq 'character') {          die "$0: $token->{type}: Unknown token type";
4266          }
4267        } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
4268          if ($token->{type} == CHARACTER_TOKEN) {
4269          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4270            my $data = $1;            my $data = $1;
4271            ## As if in body            ## As if in body
# Line 4082  sub _tree_construction_main ($) { Line 4279  sub _tree_construction_main ($) {
4279            }            }
4280          }          }
4281                    
4282          if ($self->{insertion_mode} eq 'after html body') {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4283            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#character');
4284    
4285            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "main" phase, "after body" insertion mode...
# Line 4091  sub _tree_construction_main ($) { Line 4288  sub _tree_construction_main ($) {
4288          ## "after body" insertion mode          ## "after body" insertion mode
4289          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#character');
4290    
4291          $self->{insertion_mode} = 'in body';          $self->{insertion_mode} = IN_BODY_IM;
4292          ## reprocess          ## reprocess
4293          redo B;          redo B;
4294        } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4295          if ($self->{insertion_mode} eq 'after html body') {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4296            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name});
4297                        
4298            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "main" phase, "after body" insertion mode...
# Line 4104  sub _tree_construction_main ($) { Line 4301  sub _tree_construction_main ($) {
4301          ## "after body" insertion mode          ## "after body" insertion mode
4302          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body:'.$token->{tag_name});
4303    
4304          $self->{insertion_mode} = 'in body';          $self->{insertion_mode} = IN_BODY_IM;
4305          ## reprocess          ## reprocess
4306          redo B;          redo B;
4307        } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} == END_TAG_TOKEN) {
4308          if ($self->{insertion_mode} eq 'after html body') {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4309            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name});
4310                        
4311            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = AFTER_BODY_IM;
4312            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "main" phase, "after body" insertion mode...
4313          }          }
4314    
# Line 4123  sub _tree_construction_main ($) { Line 4320  sub _tree_construction_main ($) {
4320              !!!next-token;              !!!next-token;
4321              redo B;              redo B;
4322            } else {            } else {
4323              $self->{insertion_mode} = 'after html body';              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
4324              !!!next-token;              !!!next-token;
4325              redo B;              redo B;
4326            }            }
4327          } else {          } else {
4328            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!parse-error (type => 'after body:/'.$token->{tag_name});
4329    
4330            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
4331            ## reprocess            ## reprocess
4332            redo B;            redo B;
4333          }          }
4334        } else {        } else {
4335          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4336        }        }
4337      } elsif ($self->{insertion_mode} eq 'in frameset' or      } elsif ($self->{insertion_mode} & FRAME_IMS) {
4338               $self->{insertion_mode} eq 'after frameset' or        if ($token->{type} == CHARACTER_TOKEN) {
              $self->{insertion_mode} eq 'after html frameset') {  
       if ($token->{type} eq 'character') {  
4339          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4340            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4341                        
# Line 4151  sub _tree_construction_main ($) { Line 4346  sub _tree_construction_main ($) {
4346          }          }
4347                    
4348          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
4349            if ($self->{insertion_mode} eq 'in frameset') {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4350              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#character');
4351            } elsif ($self->{insertion_mode} eq 'after frameset') {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4352              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character');
4353            } else { # "after html frameset"            } else { # "after html frameset"
4354              !!!parse-error (type => 'after html:#character');              !!!parse-error (type => 'after html:#character');
4355    
4356              $self->{insertion_mode} = 'after frameset';              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4357              ## Reprocess in the "main" phase, "after frameset"...              ## Reprocess in the "main" phase, "after frameset"...
4358              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character');
4359            }            }
# Line 4173  sub _tree_construction_main ($) { Line 4368  sub _tree_construction_main ($) {
4368          }          }
4369                    
4370          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
4371        } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4372          if ($self->{insertion_mode} eq 'after html frameset') {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4373            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name});
4374    
4375            $self->{insertion_mode} = 'after frameset';            $self->{insertion_mode} = AFTER_FRAMESET_IM;
4376            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "main" phase, "after frameset" insertion mode...
4377          }          }
4378    
4379          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
4380              $self->{insertion_mode} eq 'in frameset') {              $self->{insertion_mode} == IN_FRAMESET_IM) {
4381            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
4382            !!!next-token;            !!!next-token;
4383            redo B;            redo B;
4384          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
4385                   $self->{insertion_mode} eq 'in frameset') {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
4386            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
4387            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4388            !!!next-token;            !!!next-token;
# Line 4197  sub _tree_construction_main ($) { Line 4392  sub _tree_construction_main ($) {
4392            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4393            redo B;            redo B;
4394          } else {          } else {
4395            if ($self->{insertion_mode} eq 'in frameset') {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4396              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4397            } else {            } else {
4398              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset:'.$token->{tag_name});
# Line 4206  sub _tree_construction_main ($) { Line 4401  sub _tree_construction_main ($) {
4401            !!!next-token;            !!!next-token;
4402            redo B;            redo B;
4403          }          }
4404        } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} == END_TAG_TOKEN) {
4405          if ($self->{insertion_mode} eq 'after html frameset') {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4406            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name});
4407    
4408            $self->{insertion_mode} = 'after frameset';            $self->{insertion_mode} = AFTER_FRAMESET_IM;
4409            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "main" phase, "after frameset" insertion mode...
4410          }          }
4411    
4412          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
4413              $self->{insertion_mode} eq 'in frameset') {              $self->{insertion_mode} == IN_FRAMESET_IM) {
4414            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] eq 'html' and
4415                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
4416              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
# Line 4228  sub _tree_construction_main ($) { Line 4423  sub _tree_construction_main ($) {
4423    
4424            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
4425                $self->{open_elements}->[-1]->[1] ne 'frameset') {                $self->{open_elements}->[-1]->[1] ne 'frameset') {
4426              $self->{insertion_mode} = 'after frameset';              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4427            }            }
4428            redo B;            redo B;
4429          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
4430                   $self->{insertion_mode} eq 'after frameset') {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
4431            $self->{insertion_mode} = 'after html frameset';            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
4432            !!!next-token;            !!!next-token;
4433            redo B;            redo B;
4434          } else {          } else {
4435            if ($self->{insertion_mode} eq 'in frameset') {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4436              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
4437            } else {            } else {
4438              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
# Line 4256  sub _tree_construction_main ($) { Line 4451  sub _tree_construction_main ($) {
4451      }      }
4452    
4453      ## "in body" insertion mode      ## "in body" insertion mode
4454      if ($token->{type} eq 'start tag') {      if ($token->{type} == START_TAG_TOKEN) {
4455        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
4456          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
4457          $script_start_tag->($insert);          $script_start_tag->($insert);
# Line 4276  sub _tree_construction_main ($) { Line 4471  sub _tree_construction_main ($) {
4471        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
4472          ## 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
4473          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4474          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.
4475    
4476          unless ($self->{confident}) {          unless ($self->{confident}) {
           my $charset;  
4477            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) { ## TODO: And if supported
4478              $charset = $token->{attributes}->{charset}->{value};              $self->{change_encoding}
4479            }                  ->($self, $token->{attributes}->{charset}->{value});
4480            if ($token->{attributes}->{'http-equiv'}) {              
4481                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4482                    ->set_user_data (manakai_has_reference =>
4483                                         $token->{attributes}->{charset}
4484                                             ->{has_reference});
4485              } elsif ($token->{attributes}->{content}) {
4486              ## 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.
4487              if ($token->{attributes}->{'http-equiv'}->{value}              if ($token->{attributes}->{content}->{value}
4488                  =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                  =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
4489                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4490                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4491                $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                $self->{change_encoding}
4492              } ## TODO: And if supported                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
4493                }
4494              }
4495            } else {
4496              if ($token->{attributes}->{charset}) {
4497                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4498                    ->set_user_data (manakai_has_reference =>
4499                                         $token->{attributes}->{charset}
4500                                             ->{has_reference});
4501            }            }
           ## TODO: Change the encoding  
4502          }          }
4503    
4504          !!!next-token;          !!!next-token;
# Line 4336  sub _tree_construction_main ($) { Line 4542  sub _tree_construction_main ($) {
4542          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4543            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4544              !!!back-token;              !!!back-token;
4545              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4546              redo B;              redo B;
4547            } elsif ({            } elsif ({
4548                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
# Line 4349  sub _tree_construction_main ($) { Line 4555  sub _tree_construction_main ($) {
4555          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4556          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre') {
4557            !!!next-token;            !!!next-token;
4558            if ($token->{type} eq 'character') {            if ($token->{type} == CHARACTER_TOKEN) {
4559              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
4560              unless (length $token->{data}) {              unless (length $token->{data}) {
4561                !!!next-token;                !!!next-token;
# Line 4370  sub _tree_construction_main ($) { Line 4576  sub _tree_construction_main ($) {
4576            INSCOPE: for (reverse @{$self->{open_elements}}) {            INSCOPE: for (reverse @{$self->{open_elements}}) {
4577              if ($_->[1] eq 'p') {              if ($_->[1] eq 'p') {
4578                !!!back-token;                !!!back-token;
4579                $token = {type => 'end tag', tag_name => 'p'};                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4580                redo B;                redo B;
4581              } elsif ({              } elsif ({
4582                        table => 1, caption => 1, td => 1, th => 1,                        table => 1, caption => 1, td => 1, th => 1,
# Line 4390  sub _tree_construction_main ($) { Line 4596  sub _tree_construction_main ($) {
4596          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4597            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4598              !!!back-token;              !!!back-token;
4599              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4600              redo B;              redo B;
4601            } elsif ({            } elsif ({
4602                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
# Line 4437  sub _tree_construction_main ($) { Line 4643  sub _tree_construction_main ($) {
4643          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4644            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4645              !!!back-token;              !!!back-token;
4646              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4647              redo B;              redo B;
4648            } elsif ({            } elsif ({
4649                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
# Line 4484  sub _tree_construction_main ($) { Line 4690  sub _tree_construction_main ($) {
4690          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4691            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4692              !!!back-token;              !!!back-token;
4693              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4694              redo B;              redo B;
4695            } elsif ({            } elsif ({
4696                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
# Line 4508  sub _tree_construction_main ($) { Line 4714  sub _tree_construction_main ($) {
4714            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
4715            if ($node->[1] eq 'p') {            if ($node->[1] eq 'p') {
4716              !!!back-token;              !!!back-token;
4717              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4718              redo B;              redo B;
4719            } elsif ({            } elsif ({
4720                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
# Line 4552  sub _tree_construction_main ($) { Line 4758  sub _tree_construction_main ($) {
4758              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a');
4759                            
4760              !!!back-token;              !!!back-token;
4761              $token = {type => 'end tag', tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a'};
4762              $formatting_end_tag->($token->{tag_name});              $formatting_end_tag->($token->{tag_name});
4763                            
4764              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
# Line 4599  sub _tree_construction_main ($) { Line 4805  sub _tree_construction_main ($) {
4805          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4806            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
4807            if ($node->[1] eq 'nobr') {            if ($node->[1] eq 'nobr') {
4808              !!!parse-error (type => 'not closed:nobr');              !!!parse-error (type => 'in nobr:nobr');
4809              !!!back-token;              !!!back-token;
4810              $token = {type => 'end tag', tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
4811              redo B;              redo B;
4812            } elsif ({            } elsif ({
4813                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
# Line 4623  sub _tree_construction_main ($) { Line 4829  sub _tree_construction_main ($) {
4829            if ($node->[1] eq 'button') {            if ($node->[1] eq 'button') {
4830              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button');
4831              !!!back-token;              !!!back-token;
4832              $token = {type => 'end tag', tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button'};
4833              redo B;              redo B;
4834            } elsif ({            } elsif ({
4835                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
# Line 4658  sub _tree_construction_main ($) { Line 4864  sub _tree_construction_main ($) {
4864          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4865            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4866              !!!back-token;              !!!back-token;
4867              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4868              redo B;              redo B;
4869            } elsif ({            } elsif ({
4870                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
# Line 4670  sub _tree_construction_main ($) { Line 4876  sub _tree_construction_main ($) {
4876                        
4877          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4878                        
4879          $self->{insertion_mode} = 'in table';          $self->{insertion_mode} = IN_TABLE_IM;
4880                        
4881          !!!next-token;          !!!next-token;
4882          redo B;          redo B;
# Line 4697  sub _tree_construction_main ($) { Line 4903  sub _tree_construction_main ($) {
4903          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4904            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4905              !!!back-token;              !!!back-token;
4906              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4907              redo B;              redo B;
4908            } elsif ({            } elsif ({
4909                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
# Line 4737  sub _tree_construction_main ($) { Line 4943  sub _tree_construction_main ($) {
4943            delete $at->{action};            delete $at->{action};
4944            delete $at->{prompt};            delete $at->{prompt};
4945            my @tokens = (            my @tokens = (
4946                          {type => 'start tag', tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
4947                           attributes => $form_attrs},                           attributes => $form_attrs},
4948                          {type => 'start tag', tag_name => 'hr'},                          {type => START_TAG_TOKEN, tag_name => 'hr'},
4949                          {type => 'start tag', tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'p'},
4950                          {type => 'start tag', tag_name => 'label'},                          {type => START_TAG_TOKEN, tag_name => 'label'},
4951                         );                         );
4952            if ($prompt_attr) {            if ($prompt_attr) {
4953              push @tokens, {type => 'character', data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
4954            } else {            } else {
4955              push @tokens, {type => 'character',              push @tokens, {type => CHARACTER_TOKEN,
4956                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
4957              ## TODO: make this configurable              ## TODO: make this configurable
4958            }            }
4959            push @tokens,            push @tokens,
4960                          {type => 'start tag', tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},
4961                          #{type => 'character', data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
4962                          {type => 'end tag', tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label'},
4963                          {type => 'end tag', tag_name => 'p'},                          {type => END_TAG_TOKEN, tag_name => 'p'},
4964                          {type => 'start tag', tag_name => 'hr'},                          {type => START_TAG_TOKEN, tag_name => 'hr'},
4965                          {type => 'end tag', tag_name => 'form'};                          {type => END_TAG_TOKEN, tag_name => 'form'};
4966            $token = shift @tokens;            $token = shift @tokens;
4967            !!!back-token (@tokens);            !!!back-token (@tokens);
4968            redo B;            redo B;
# Line 4774  sub _tree_construction_main ($) { Line 4980  sub _tree_construction_main ($) {
4980                    
4981          my $text = '';          my $text = '';
4982          !!!next-token;          !!!next-token;
4983          if ($token->{type} eq 'character') {          if ($token->{type} == CHARACTER_TOKEN) {
4984            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
4985            unless (length $token->{data}) {            unless (length $token->{data}) {
4986              !!!next-token;              !!!next-token;
4987            }            }
4988          }          }
4989          while ($token->{type} eq 'character') {          while ($token->{type} == CHARACTER_TOKEN) {
4990            $text .= $token->{data};            $text .= $token->{data};
4991            !!!next-token;            !!!next-token;
4992          }          }
# Line 4790  sub _tree_construction_main ($) { Line 4996  sub _tree_construction_main ($) {
4996                    
4997          $self->{content_model} = PCDATA_CONTENT_MODEL;          $self->{content_model} = PCDATA_CONTENT_MODEL;
4998                    
4999          if ($token->{type} eq 'end tag' and          if ($token->{type} == END_TAG_TOKEN and
5000              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
5001            ## Ignore the token            ## Ignore the token
5002          } else {          } else {
# Line 4804  sub _tree_construction_main ($) { Line 5010  sub _tree_construction_main ($) {
5010                  noframes => 1,                  noframes => 1,
5011                  noscript => 0, ## TODO: 1 if scripting is enabled                  noscript => 0, ## TODO: 1 if scripting is enabled
5012                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5013          ## NOTE: There are two "as if in body" code clones.          ## NOTE: There is an "as if in body" code clone.
5014          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
5015          redo B;          redo B;
5016        } elsif ($token->{tag_name} eq 'select') {        } elsif ($token->{tag_name} eq 'select') {
# Line 4812  sub _tree_construction_main ($) { Line 5018  sub _tree_construction_main ($) {
5018                    
5019          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5020                    
5021          $self->{insertion_mode} = 'in select';          $self->{insertion_mode} = IN_SELECT_IM;
5022          !!!next-token;          !!!next-token;
5023          redo B;          redo B;
5024        } elsif ({        } elsif ({
# Line 4835  sub _tree_construction_main ($) { Line 5041  sub _tree_construction_main ($) {
5041          !!!next-token;          !!!next-token;
5042          redo B;          redo B;
5043        }        }
5044      } elsif ($token->{type} eq 'end tag') {      } elsif ($token->{type} == END_TAG_TOKEN) {
5045        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
5046          if (@{$self->{open_elements}} > 1 and          if (@{$self->{open_elements}} > 1 and
5047              $self->{open_elements}->[1]->[1] eq 'body') {              $self->{open_elements}->[1]->[1] eq 'body') {
# Line 4849  sub _tree_construction_main ($) { Line 5055  sub _tree_construction_main ($) {
5055              }              }
5056            }            }
5057    
5058            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = AFTER_BODY_IM;
5059            !!!next-token;            !!!next-token;
5060            redo B;            redo B;
5061          } else {          } else {
# Line 4864  sub _tree_construction_main ($) { Line 5070  sub _tree_construction_main ($) {
5070            if ($self->{open_elements}->[-1]->[1] ne 'body') {            if ($self->{open_elements}->[-1]->[1] ne 'body') {
5071              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
5072            }            }
5073            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = AFTER_BODY_IM;
5074            ## reprocess            ## reprocess
5075            redo B;            redo B;
5076          } else {          } else {
# Line 4896  sub _tree_construction_main ($) { Line 5102  sub _tree_construction_main ($) {
5102                   tbody => 1, tfoot=> 1, thead => 1,                   tbody => 1, tfoot=> 1, thead => 1,
5103                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5104                !!!back-token;                !!!back-token;
5105                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5106                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5107                redo B;                redo B;
5108              }              }
# Line 4944  sub _tree_construction_main ($) { Line 5150  sub _tree_construction_main ($) {
5150                   tbody => 1, tfoot=> 1, thead => 1,                   tbody => 1, tfoot=> 1, thead => 1,
5151                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5152                !!!back-token;                !!!back-token;
5153                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5154                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5155                redo B;                redo B;
5156              }              }
# Line 4960  sub _tree_construction_main ($) { Line 5166  sub _tree_construction_main ($) {
5166          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
5167            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
5168          } else {          } else {
5169            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5170          }          }
5171    
5172          undef $self->{form_element};          undef $self->{form_element};
# Line 4983  sub _tree_construction_main ($) { Line 5189  sub _tree_construction_main ($) {
5189                   tbody => 1, tfoot=> 1, thead => 1,                   tbody => 1, tfoot=> 1, thead => 1,
5190                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5191                !!!back-token;                !!!back-token;
5192                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5193                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5194                redo B;                redo B;
5195              }              }
# Line 4998  sub _tree_construction_main ($) { Line 5204  sub _tree_construction_main ($) {
5204          } # INSCOPE          } # INSCOPE
5205                    
5206          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5207            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5208          }          }
5209                    
5210          splice @{$self->{open_elements}}, $i if defined $i;          splice @{$self->{open_elements}}, $i if defined $i;
# Line 5057  sub _tree_construction_main ($) { Line 5263  sub _tree_construction_main ($) {
5263              if ({              if ({
5264                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
5265                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
5266                   tbody => 1, tfoot=> 1, thead => 1,                   tbody => 1, tfoot => 1, thead => 1,
5267                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5268                !!!back-token;                !!!back-token;
5269                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5270                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5271                redo B;                redo B;
5272              }              }
5273                    
5274              ## Step 2              ## Step 2
5275              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
5276                  ## NOTE: <x><y></x>
5277                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5278              }              }
5279                            
# Line 5117  sub set_inner_html ($$$) { Line 5324  sub set_inner_html ($$$) {
5324    my $s = \$_[0];    my $s = \$_[0];
5325    my $onerror = $_[1];    my $onerror = $_[1];
5326    
5327      ## ISSUE: Should {confident} be true?
5328    
5329    my $nt = $node->node_type;    my $nt = $node->node_type;
5330    if ($nt == 9) {    if ($nt == 9) {
5331      # MUST      # MUST
# Line 5269  sub set_inner_html ($$$) { Line 5478  sub set_inner_html ($$$) {
5478    
5479  } # tree construction stage  } # tree construction stage
5480    
5481  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
5482    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  
5483    
5484  1;  1;
5485  # $Date$  # $Date$

Legend:
Removed from v.1.53  
changed lines
  Added in v.1.67

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24