/[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.69 by wakaba, Sun Feb 17 12:39:32 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;
1557            redo A;
1558          } elsif ($self->{next_input_character} == 0x003E) { # >
1559            !!!parse-error (type => 'unclosed PUBLIC literal');
1560    
1561            $self->{state} = DATA_STATE;
1562          !!!next-input-character;          !!!next-input-character;
1563    
1564            delete $self->{current_token}->{correct};
1565            !!!emit ($self->{current_token}); # DOCTYPE
1566    
1567          redo A;          redo A;
1568        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1569          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1570    
1571          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1572          ## reconsume          ## reconsume
1573    
1574          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1397  sub _get_next_token ($) { Line 1582  sub _get_next_token ($) {
1582          !!!next-input-character;          !!!next-input-character;
1583          redo A;          redo A;
1584        }        }
1585      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
1586        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1587          $self->{state} = 'after DOCTYPE public identifier';          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
1588            !!!next-input-character;
1589            redo A;
1590          } elsif ($self->{next_input_character} == 0x003E) { # >
1591            !!!parse-error (type => 'unclosed PUBLIC literal');
1592    
1593            $self->{state} = DATA_STATE;
1594          !!!next-input-character;          !!!next-input-character;
1595    
1596            delete $self->{current_token}->{correct};
1597            !!!emit ($self->{current_token}); # DOCTYPE
1598    
1599          redo A;          redo A;
1600        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1601          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
1602    
1603          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1604          ## reconsume          ## reconsume
1605    
1606          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1419  sub _get_next_token ($) { Line 1614  sub _get_next_token ($) {
1614          !!!next-input-character;          !!!next-input-character;
1615          redo A;          redo A;
1616        }        }
1617      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
1618        if ({        if ({
1619              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1620              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1429  sub _get_next_token ($) { Line 1624  sub _get_next_token ($) {
1624          redo A;          redo A;
1625        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1626          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1627          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1628          !!!next-input-character;          !!!next-input-character;
1629          redo A;          redo A;
1630        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1631          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1632          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1633          !!!next-input-character;          !!!next-input-character;
1634          redo A;          redo A;
1635        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1636          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1637          !!!next-input-character;          !!!next-input-character;
1638    
1639          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1447  sub _get_next_token ($) { Line 1642  sub _get_next_token ($) {
1642        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1643          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1644    
1645          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1646          ## reconsume          ## reconsume
1647    
1648          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1456  sub _get_next_token ($) { Line 1651  sub _get_next_token ($) {
1651          redo A;          redo A;
1652        } else {        } else {
1653          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
1654          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1655          !!!next-input-character;          !!!next-input-character;
1656          redo A;          redo A;
1657        }        }
1658      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1659        if ({        if ({
1660              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1661              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1470  sub _get_next_token ($) { Line 1665  sub _get_next_token ($) {
1665          redo A;          redo A;
1666        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_input_character} == 0x0022) { # "
1667          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1668          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
1669          !!!next-input-character;          !!!next-input-character;
1670          redo A;          redo A;
1671        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_input_character} == 0x0027) { # '
1672          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
1673          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
1674          !!!next-input-character;          !!!next-input-character;
1675          redo A;          redo A;
1676        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1677          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
1678          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1679          !!!next-input-character;          !!!next-input-character;
1680    
1681          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1490  sub _get_next_token ($) { Line 1685  sub _get_next_token ($) {
1685        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1686          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1687    
1688          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1689          ## reconsume          ## reconsume
1690    
1691          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1499  sub _get_next_token ($) { Line 1694  sub _get_next_token ($) {
1694          redo A;          redo A;
1695        } else {        } else {
1696          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
1697          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1698          !!!next-input-character;          !!!next-input-character;
1699          redo A;          redo A;
1700        }        }
1701      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
1702        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_input_character} == 0x0022) { # "
1703          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1704            !!!next-input-character;
1705            redo A;
1706          } elsif ($self->{next_input_character} == 0x003E) { # >
1707            !!!parse-error (type => 'unclosed PUBLIC literal');
1708    
1709            $self->{state} = DATA_STATE;
1710          !!!next-input-character;          !!!next-input-character;
1711    
1712            delete $self->{current_token}->{correct};
1713            !!!emit ($self->{current_token}); # DOCTYPE
1714    
1715          redo A;          redo A;
1716        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1717          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1718    
1719          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1720          ## reconsume          ## reconsume
1721    
1722          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1525  sub _get_next_token ($) { Line 1730  sub _get_next_token ($) {
1730          !!!next-input-character;          !!!next-input-character;
1731          redo A;          redo A;
1732        }        }
1733      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
1734        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_input_character} == 0x0027) { # '
1735          $self->{state} = 'after DOCTYPE system identifier';          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
1736            !!!next-input-character;
1737            redo A;
1738          } elsif ($self->{next_input_character} == 0x003E) { # >
1739            !!!parse-error (type => 'unclosed PUBLIC literal');
1740    
1741            $self->{state} = DATA_STATE;
1742          !!!next-input-character;          !!!next-input-character;
1743    
1744            delete $self->{current_token}->{correct};
1745            !!!emit ($self->{current_token}); # DOCTYPE
1746    
1747          redo A;          redo A;
1748        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1749          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
1750    
1751          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1752          ## reconsume          ## reconsume
1753    
1754          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1547  sub _get_next_token ($) { Line 1762  sub _get_next_token ($) {
1762          !!!next-input-character;          !!!next-input-character;
1763          redo A;          redo A;
1764        }        }
1765      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
1766        if ({        if ({
1767              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
1768              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
# Line 1556  sub _get_next_token ($) { Line 1771  sub _get_next_token ($) {
1771          !!!next-input-character;          !!!next-input-character;
1772          redo A;          redo A;
1773        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_input_character} == 0x003E) { # >
1774          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1775          !!!next-input-character;          !!!next-input-character;
1776    
1777          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
# Line 1565  sub _get_next_token ($) { Line 1780  sub _get_next_token ($) {
1780        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1781          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1782    
1783          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1784          ## reconsume          ## reconsume
1785    
1786          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1574  sub _get_next_token ($) { Line 1789  sub _get_next_token ($) {
1789          redo A;          redo A;
1790        } else {        } else {
1791          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
1792          $self->{state} = 'bogus DOCTYPE';          $self->{state} = BOGUS_DOCTYPE_STATE;
1793          !!!next-input-character;          !!!next-input-character;
1794          redo A;          redo A;
1795        }        }
1796      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
1797        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_input_character} == 0x003E) { # >
1798          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1799          !!!next-input-character;          !!!next-input-character;
1800    
1801          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1589  sub _get_next_token ($) { Line 1804  sub _get_next_token ($) {
1804          redo A;          redo A;
1805        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_input_character} == -1) {
1806          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
1807          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1808          ## reconsume          ## reconsume
1809    
1810          delete $self->{current_token}->{correct};          delete $self->{current_token}->{correct};
# Line 1670  sub _tokenize_attempt_to_consume_an_enti Line 1885  sub _tokenize_attempt_to_consume_an_enti
1885            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
1886          }          }
1887    
1888          return {type => 'character', data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code,
1889                    has_reference => 1};
1890        } # X        } # X
1891      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_input_character} and
1892               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_input_character} <= 0x0039) { # 0..9
# Line 1705  sub _tokenize_attempt_to_consume_an_enti Line 1921  sub _tokenize_attempt_to_consume_an_enti
1921          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
1922        }        }
1923                
1924        return {type => 'character', data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};
1925      } else {      } else {
1926        !!!parse-error (type => 'bare nero');        !!!parse-error (type => 'bare nero');
1927        !!!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 1969  sub _tokenize_attempt_to_consume_an_enti
1969      }      }
1970            
1971      if ($match > 0) {      if ($match > 0) {
1972        return {type => 'character', data => $value};        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
1973      } elsif ($match < 0) {      } elsif ($match < 0) {
1974        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc');
1975        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
1976          return {type => 'character', data => '&'.$entity_name};          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};
1977        } else {        } else {
1978          return {type => 'character', data => $value};          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};
1979        }        }
1980      } else {      } else {
1981        !!!parse-error (type => 'bare ero');        !!!parse-error (type => 'bare ero');
1982        ## NOTE: No characters are consumed in the spec.        ## NOTE: "No characters are consumed" in the spec.
1983        return {type => 'character', data => '&'.$value};        return {type => CHARACTER_TOKEN, data => '&'.$value};
1984      }      }
1985    } else {    } else {
1986      ## no characters are consumed      ## no characters are consumed
# Line 1806  sub _construct_tree ($) { Line 2022  sub _construct_tree ($) {
2022        
2023    !!!next-token;    !!!next-token;
2024    
2025    $self->{insertion_mode} = 'before head';    $self->{insertion_mode} = BEFORE_HEAD_IM;
2026    undef $self->{form_element};    undef $self->{form_element};
2027    undef $self->{head_element};    undef $self->{head_element};
2028    $self->{open_elements} = [];    $self->{open_elements} = [];
# Line 1820  sub _construct_tree ($) { Line 2036  sub _construct_tree ($) {
2036  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
2037    my $self = shift;    my $self = shift;
2038    INITIAL: {    INITIAL: {
2039      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
2040        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
2041        ## error, switch to a conformance checking mode for another        ## error, switch to a conformance checking mode for another
2042        ## language.        ## language.
# Line 1947  sub _tree_construction_initial ($) { Line 2163  sub _tree_construction_initial ($) {
2163        !!!next-token;        !!!next-token;
2164        return;        return;
2165      } elsif ({      } elsif ({
2166                'start tag' => 1,                START_TAG_TOKEN, 1,
2167                'end tag' => 1,                END_TAG_TOKEN, 1,
2168                'end-of-file' => 1,                END_OF_FILE_TOKEN, 1,
2169               }->{$token->{type}}) {               }->{$token->{type}}) {
2170        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE');
2171        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
2172        ## Go to the root element phase        ## Go to the root element phase
2173        ## reprocess        ## reprocess
2174        return;        return;
2175      } elsif ($token->{type} eq 'character') {      } elsif ($token->{type} == CHARACTER_TOKEN) {
2176        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2177          ## Ignore the token          ## Ignore the token
2178    
# Line 1972  sub _tree_construction_initial ($) { Line 2188  sub _tree_construction_initial ($) {
2188        ## Go to the root element phase        ## Go to the root element phase
2189        ## reprocess        ## reprocess
2190        return;        return;
2191      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
2192        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2193        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
2194                
# Line 1980  sub _tree_construction_initial ($) { Line 2196  sub _tree_construction_initial ($) {
2196        !!!next-token;        !!!next-token;
2197        redo INITIAL;        redo INITIAL;
2198      } else {      } else {
2199        die "$0: $token->{type}: Unknown token";        die "$0: $token->{type}: Unknown token type";
2200      }      }
2201    } # INITIAL    } # INITIAL
2202  } # _tree_construction_initial  } # _tree_construction_initial
# Line 1989  sub _tree_construction_root_element ($) Line 2205  sub _tree_construction_root_element ($)
2205    my $self = shift;    my $self = shift;
2206        
2207    B: {    B: {
2208        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
2209          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE');
2210          ## Ignore the token          ## Ignore the token
2211          ## Stay in the phase          ## Stay in the phase
2212          !!!next-token;          !!!next-token;
2213          redo B;          redo B;
2214        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
2215          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
2216          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2217          ## Stay in the phase          ## Stay in the phase
2218          !!!next-token;          !!!next-token;
2219          redo B;          redo B;
2220        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
2221          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
2222            ## Ignore the token.            ## Ignore the token.
2223    
# Line 2011  sub _tree_construction_root_element ($) Line 2227  sub _tree_construction_root_element ($)
2227              redo B;              redo B;
2228            }            }
2229          }          }
2230    
2231            $self->{application_cache_selection}->(undef);
2232    
2233            #
2234          } elsif ($token->{type} == START_TAG_TOKEN) {
2235            if ($token->{tag_name} eq 'html' and
2236                $token->{attributes}->{manifest}) {
2237              $self->{application_cache_selection}
2238                   ->($token->{attributes}->{manifest}->{value});
2239              ## ISSUE: No relative reference resolution?
2240            } else {
2241              $self->{application_cache_selection}->(undef);
2242            }
2243    
2244            ## ISSUE: There is an issue in the spec
2245          #          #
2246        } elsif ({        } elsif ({
2247                  'start tag' => 1,                  END_TAG_TOKEN, 1,
2248                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
2249                 }->{$token->{type}}) {                 }->{$token->{type}}) {
2250            $self->{application_cache_selection}->(undef);
2251    
2252          ## ISSUE: There is an issue in the spec          ## ISSUE: There is an issue in the spec
2253          #          #
2254        } else {        } else {
2255          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
2256        }        }
2257    
2258        my $root_element; !!!create-element ($root_element, 'html');        my $root_element; !!!create-element ($root_element, 'html');
2259        $self->{document}->append_child ($root_element);        $self->{document}->append_child ($root_element);
2260        push @{$self->{open_elements}}, [$root_element, 'html'];        push @{$self->{open_elements}}, [$root_element, 'html'];
# Line 2062  sub _reset_insertion_mode ($) { Line 2295  sub _reset_insertion_mode ($) {
2295            
2296        ## Step 4..13        ## Step 4..13
2297        my $new_mode = {        my $new_mode = {
2298                        select => 'in select',                        select => IN_SELECT_IM,
2299                        td => 'in cell',                        td => IN_CELL_IM,
2300                        th => 'in cell',                        th => IN_CELL_IM,
2301                        tr => 'in row',                        tr => IN_ROW_IM,
2302                        tbody => 'in table body',                        tbody => IN_TABLE_BODY_IM,
2303                        thead => 'in table body',                        thead => IN_TABLE_BODY_IM,
2304                        tfoot => 'in table body',                        tfoot => IN_TABLE_BODY_IM,
2305                        caption => 'in caption',                        caption => IN_CAPTION_IM,
2306                        colgroup => 'in column group',                        colgroup => IN_COLUMN_GROUP_IM,
2307                        table => 'in table',                        table => IN_TABLE_IM,
2308                        head => 'in body', # not in head!                        head => IN_BODY_IM, # not in head!
2309                        body => 'in body',                        body => IN_BODY_IM,
2310                        frameset => 'in frameset',                        frameset => IN_FRAMESET_IM,
2311                       }->{$node->[1]};                       }->{$node->[1]};
2312        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
2313                
2314        ## Step 14        ## Step 14
2315        if ($node->[1] eq 'html') {        if ($node->[1] eq 'html') {
2316          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
2317            $self->{insertion_mode} = 'before head';            $self->{insertion_mode} = BEFORE_HEAD_IM;
2318          } else {          } else {
2319            $self->{insertion_mode} = 'after head';            $self->{insertion_mode} = AFTER_HEAD_IM;
2320          }          }
2321          return;          return;
2322        }        }
2323                
2324        ## Step 15        ## Step 15
2325        $self->{insertion_mode} = 'in body' and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
2326                
2327        ## Step 16        ## Step 16
2328        $i--;        $i--;
# Line 2203  sub _tree_construction_main ($) { Line 2436  sub _tree_construction_main ($) {
2436      ## Step 4      ## Step 4
2437      my $text = '';      my $text = '';
2438      !!!next-token;      !!!next-token;
2439      while ($token->{type} eq 'character') { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
2440        $text .= $token->{data};        $text .= $token->{data};
2441        !!!next-token;        !!!next-token;
2442      }      }
# Line 2218  sub _tree_construction_main ($) { Line 2451  sub _tree_construction_main ($) {
2451      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
2452    
2453      ## Step 7      ## Step 7
2454      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) {
2455        ## Ignore the token        ## Ignore the token
2456      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {      } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {
2457        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#'.$token->{type});
# Line 2241  sub _tree_construction_main ($) { Line 2474  sub _tree_construction_main ($) {
2474            
2475      my $text = '';      my $text = '';
2476      !!!next-token;      !!!next-token;
2477      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
2478        $text .= $token->{data};        $text .= $token->{data};
2479        !!!next-token;        !!!next-token;
2480      } # 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 2484  sub _tree_construction_main ($) {
2484                                
2485      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
2486    
2487      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
2488          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
2489        ## Ignore the token        ## Ignore the token
2490      } else {      } else {
# Line 2497  sub _tree_construction_main ($) { Line 2730  sub _tree_construction_main ($) {
2730    my $insert;    my $insert;
2731    
2732    B: {    B: {
2733      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
2734        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'DOCTYPE in the middle');
2735        ## Ignore the token        ## Ignore the token
2736        ## Stay in the phase        ## Stay in the phase
2737        !!!next-token;        !!!next-token;
2738        redo B;        redo B;
2739      } elsif ($token->{type} eq 'end-of-file') {      } elsif ($token->{type} == END_OF_FILE_TOKEN) {
2740        if ($self->{insertion_mode} eq 'after html body' or        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
           $self->{insertion_mode} eq 'after html frameset') {  
2741          #          #
2742        } else {        } else {
2743          ## Generate implied end tags          ## Generate implied end tags
# Line 2514  sub _tree_construction_main ($) { Line 2746  sub _tree_construction_main ($) {
2746               tbody => 1, tfoot=> 1, thead => 1,               tbody => 1, tfoot=> 1, thead => 1,
2747              }->{$self->{open_elements}->[-1]->[1]}) {              }->{$self->{open_elements}->[-1]->[1]}) {
2748            !!!back-token;            !!!back-token;
2749            $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};            $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};
2750            redo B;            redo B;
2751          }          }
2752                    
# Line 2532  sub _tree_construction_main ($) { Line 2764  sub _tree_construction_main ($) {
2764    
2765        ## Stop parsing        ## Stop parsing
2766        last B;        last B;
2767      } elsif ($token->{type} eq 'start tag' and      } elsif ($token->{type} == START_TAG_TOKEN and
2768               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
2769        if ($self->{insertion_mode} eq 'after html body') {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
2770          ## Turn into the main phase          ## Turn into the main phase
2771          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html');
2772          $self->{insertion_mode} = 'after body';          $self->{insertion_mode} = AFTER_BODY_IM;
2773        } elsif ($self->{insertion_mode} eq 'after html frameset') {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
2774          ## Turn into the main phase          ## Turn into the main phase
2775          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html');
2776          $self->{insertion_mode} = 'after frameset';          $self->{insertion_mode} = AFTER_FRAMESET_IM;
2777        }        }
2778    
2779  ## ISSUE: "aa<html>" is not a parse error.  ## ISSUE: "aa<html>" is not a parse error.
# Line 2559  sub _tree_construction_main ($) { Line 2791  sub _tree_construction_main ($) {
2791        }        }
2792        !!!next-token;        !!!next-token;
2793        redo B;        redo B;
2794      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
2795        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
2796        if ($self->{insertion_mode} eq 'after html body' or        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
           $self->{insertion_mode} eq 'after html frameset') {  
2797          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
2798        } elsif ($self->{insertion_mode} eq 'after body') {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
2799          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
2800        } else {        } else {
2801          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
2802        }        }
2803        !!!next-token;        !!!next-token;
2804        redo B;        redo B;
2805      } elsif ($self->{insertion_mode} eq 'in head' or      } elsif ($self->{insertion_mode} & HEAD_IMS) {
2806               $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') {  
2807          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
2808            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
2809            unless (length $token->{data}) {            unless (length $token->{data}) {
# Line 2584  sub _tree_construction_main ($) { Line 2812  sub _tree_construction_main ($) {
2812            }            }
2813          }          }
2814    
2815          if ($self->{insertion_mode} eq 'before head') {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2816            ## As if <head>            ## As if <head>
2817            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, 'head');
2818            $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 2822  sub _tree_construction_main ($) {
2822            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
2823    
2824            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
2825          } elsif ($self->{insertion_mode} eq 'in head noscript') {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2826            ## As if </noscript>            ## As if </noscript>
2827            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
2828            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character');
# Line 2604  sub _tree_construction_main ($) { Line 2832  sub _tree_construction_main ($) {
2832            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
2833    
2834            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
2835          } elsif ($self->{insertion_mode} eq 'in head') {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
2836            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
2837    
2838            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
# Line 2613  sub _tree_construction_main ($) { Line 2841  sub _tree_construction_main ($) {
2841              ## "after head" insertion mode              ## "after head" insertion mode
2842              ## As if <body>              ## As if <body>
2843              !!!insert-element ('body');              !!!insert-element ('body');
2844              $self->{insertion_mode} = 'in body';              $self->{insertion_mode} = IN_BODY_IM;
2845              ## reprocess              ## reprocess
2846              redo B;              redo B;
2847            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
2848              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
2849                if ($self->{insertion_mode} eq 'before head') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2850                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});
2851                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2852                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];
2853                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
2854                  !!!next-token;                  !!!next-token;
2855                  redo B;                  redo B;
2856                } elsif ($self->{insertion_mode} ne 'after head') {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2857                    #
2858                  } else {
2859                  !!!parse-error (type => 'in head:head'); # or in head noscript                  !!!parse-error (type => 'in head:head'); # or in head noscript
2860                  ## Ignore the token                  ## Ignore the token
2861                  !!!next-token;                  !!!next-token;
2862                  redo B;                  redo B;
               } else {  
                 #  
2863                }                }
2864              } elsif ($self->{insertion_mode} eq 'before head') {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
2865                ## As if <head>                ## As if <head>
2866                !!!create-element ($self->{head_element}, 'head');                !!!create-element ($self->{head_element}, 'head');
2867                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
2868                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2869    
2870                $self->{insertion_mode} = 'in head';                $self->{insertion_mode} = IN_HEAD_IM;
2871                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
2872              }              }
2873    
2874              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
2875                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2876                  ## As if </noscript>                  ## As if </noscript>
2877                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2878                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base');
2879                                
2880                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
2881                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
2882                }                }
2883    
2884                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2885                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2886                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2887                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2888                }                }
2889                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
2890                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2891                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2892                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2893                !!!next-token;                !!!next-token;
2894                redo B;                redo B;
2895              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
2896                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2897                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2898                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2899                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2900                }                }
2901                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
2902                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
2903                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2904                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2905                !!!next-token;                !!!next-token;
2906                redo B;                redo B;
2907              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
2908                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2909                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2910                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2911                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2912                }                }
2913                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
2914                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.
2915    
2916                unless ($self->{confident}) {                unless ($self->{confident}) {
                 my $charset;  
2917                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) { ## TODO: And if supported
2918                    $charset = $token->{attributes}->{charset}->{value};                    $self->{change_encoding}
2919                  }                        ->($self, $token->{attributes}->{charset}->{value});
2920                  if ($token->{attributes}->{'http-equiv'}) {                    
2921                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
2922                          ->set_user_data (manakai_has_reference =>
2923                                               $token->{attributes}->{charset}
2924                                                   ->{has_reference});
2925                    } elsif ($token->{attributes}->{content}) {
2926                    ## 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.
2927                    if ($token->{attributes}->{'http-equiv'}->{value}                    if ($token->{attributes}->{content}->{value}
2928                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
2929                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
2930                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
2931                      $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                      $self->{change_encoding}
2932                    } ## TODO: And if supported                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
2933                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
2934                            ->set_user_data (manakai_has_reference =>
2935                                                 $token->{attributes}->{content}
2936                                                       ->{has_reference});
2937                      }
2938                    }
2939                  } else {
2940                    if ($token->{attributes}->{charset}) {
2941                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
2942                          ->set_user_data (manakai_has_reference =>
2943                                               $token->{attributes}->{charset}
2944                                                   ->{has_reference});
2945                    }
2946                    if ($token->{attributes}->{content}) {
2947                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
2948                          ->set_user_data (manakai_has_reference =>
2949                                               $token->{attributes}->{content}
2950                                                   ->{has_reference});
2951                  }                  }
                 ## TODO: Change the encoding  
2952                }                }
2953    
               ## TODO: Extracting |charset| from |meta|.  
2954                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2955                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2956                !!!next-token;                !!!next-token;
2957                redo B;                redo B;
2958              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
2959                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2960                  ## As if </noscript>                  ## As if </noscript>
2961                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
2962                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title');
2963                                
2964                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
2965                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
2966                } elsif ($self->{insertion_mode} eq 'after head') {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
2967                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2968                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2969                }                }
# Line 2726  sub _tree_construction_main ($) { Line 2974  sub _tree_construction_main ($) {
2974                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL,
2975                                sub { $parent->append_child ($_[0]) });                                sub { $parent->append_child ($_[0]) });
2976                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2977                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2978                redo B;                redo B;
2979              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
2980                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
2981                ## insertion mode 'in head')                ## insertion mode IN_HEAD_IM)
2982                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
2983                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
2984                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
2985                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
2986                }                }
2987                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
2988                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
2989                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
2990                redo B;                redo B;
2991              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
2992                if ($self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_IM) {
2993                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
2994                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes});
2995                  $self->{insertion_mode} = 'in head noscript';                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
2996                  !!!next-token;                  !!!next-token;
2997                  redo B;                  redo B;
2998                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
2999                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript:noscript');
3000                  ## Ignore the token                  ## Ignore the token
3001                  !!!next-token;                  !!!next-token;
# Line 2756  sub _tree_construction_main ($) { Line 3004  sub _tree_construction_main ($) {
3004                  #                  #
3005                }                }
3006              } elsif ($token->{tag_name} eq 'script') {              } elsif ($token->{tag_name} eq 'script') {
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:script');                  !!!parse-error (type => 'in noscript:script');
3011                                
3012                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
3013                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3014                } elsif ($self->{insertion_mode} eq 'after head') {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
3015                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head:'.$token->{tag_name});
3016                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3017                }                }
# Line 2771  sub _tree_construction_main ($) { Line 3019  sub _tree_construction_main ($) {
3019                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
3020                $script_start_tag->($insert_to_current);                $script_start_tag->($insert_to_current);
3021                pop @{$self->{open_elements}}                pop @{$self->{open_elements}}
3022                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
3023                redo B;                redo B;
3024              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
3025                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
3026                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3027                  ## As if </noscript>                  ## As if </noscript>
3028                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3029                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});
# Line 2785  sub _tree_construction_main ($) { Line 3033  sub _tree_construction_main ($) {
3033                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3034                                    
3035                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
3036                } elsif ($self->{insertion_mode} eq 'in head') {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3037                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3038                                    
3039                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
# Line 2793  sub _tree_construction_main ($) { Line 3041  sub _tree_construction_main ($) {
3041    
3042                ## "after head" insertion mode                ## "after head" insertion mode
3043                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3044                $self->{insertion_mode} = 'in '.$token->{tag_name};                if ($token->{tag_name} eq 'body') {
3045                    $self->{insertion_mode} = IN_BODY_IM;
3046                  } elsif ($token->{tag_name} eq 'frameset') {
3047                    $self->{insertion_mode} = IN_FRAMESET_IM;
3048                  } else {
3049                    die "$0: tag name: $self->{tag_name}";
3050                  }
3051                !!!next-token;                !!!next-token;
3052                redo B;                redo B;
3053              } else {              } else {
3054                #                #
3055              }              }
3056    
3057              if ($self->{insertion_mode} eq 'in head noscript') {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3058                ## As if </noscript>                ## As if </noscript>
3059                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3060                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
# Line 2810  sub _tree_construction_main ($) { Line 3064  sub _tree_construction_main ($) {
3064                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3065    
3066                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3067              } elsif ($self->{insertion_mode} eq 'in head') {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3068                ## As if </head>                ## As if </head>
3069                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3070    
# Line 2820  sub _tree_construction_main ($) { Line 3074  sub _tree_construction_main ($) {
3074              ## "after head" insertion mode              ## "after head" insertion mode
3075              ## As if <body>              ## As if <body>
3076              !!!insert-element ('body');              !!!insert-element ('body');
3077              $self->{insertion_mode} = 'in body';              $self->{insertion_mode} = IN_BODY_IM;
3078              ## reprocess              ## reprocess
3079              redo B;              redo B;
3080            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
3081              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
3082                if ($self->{insertion_mode} eq 'before head') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3083                  ## As if <head>                  ## As if <head>
3084                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head');
3085                  $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 3087  sub _tree_construction_main ($) {
3087    
3088                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3089                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3090                  $self->{insertion_mode} = 'after head';                  $self->{insertion_mode} = AFTER_HEAD_IM;
3091                  !!!next-token;                  !!!next-token;
3092                  redo B;                  redo B;
3093                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3094                  ## As if </noscript>                  ## As if </noscript>
3095                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3096                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script');
3097                                    
3098                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3099                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3100                  $self->{insertion_mode} = 'after head';                  $self->{insertion_mode} = AFTER_HEAD_IM;
3101                  !!!next-token;                  !!!next-token;
3102                  redo B;                  redo B;
3103                } elsif ($self->{insertion_mode} eq 'in head') {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3104                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3105                  $self->{insertion_mode} = 'after head';                  $self->{insertion_mode} = AFTER_HEAD_IM;
3106                  !!!next-token;                  !!!next-token;
3107                  redo B;                  redo B;
3108                } else {                } else {
3109                  #                  #
3110                }                }
3111              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
3112                if ($self->{insertion_mode} eq 'in head noscript') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3113                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3114                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
3115                  !!!next-token;                  !!!next-token;
3116                  redo B;                  redo B;
3117                } elsif ($self->{insertion_mode} eq 'before head') {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3118                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!parse-error (type => 'unmatched end tag:noscript');
3119                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
3120                  !!!next-token;                  !!!next-token;
# Line 2871  sub _tree_construction_main ($) { Line 3125  sub _tree_construction_main ($) {
3125              } elsif ({              } elsif ({
3126                        body => 1, html => 1,                        body => 1, html => 1,
3127                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3128                if ($self->{insertion_mode} eq 'before head') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3129                  ## As if <head>                  ## As if <head>
3130                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head');
3131                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3132                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3133    
3134                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
3135                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3136                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3137                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3138                  ## Ignore the token                  ## Ignore the token
3139                  !!!next-token;                  !!!next-token;
# Line 2890  sub _tree_construction_main ($) { Line 3144  sub _tree_construction_main ($) {
3144              } elsif ({              } elsif ({
3145                        p => 1, br => 1,                        p => 1, br => 1,
3146                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3147                if ($self->{insertion_mode} eq 'before head') {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3148                  ## As if <head>                  ## As if <head>
3149                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, 'head');
3150                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
3151                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];
3152    
3153                  $self->{insertion_mode} = 'in head';                  $self->{insertion_mode} = IN_HEAD_IM;
3154                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
3155                }                }
3156    
3157                #                #
3158              } else {              } else {
3159                if ($self->{insertion_mode} ne 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
3160                    #
3161                  } else {
3162                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3163                  ## Ignore the token                  ## Ignore the token
3164                  !!!next-token;                  !!!next-token;
3165                  redo B;                  redo B;
               } else {  
                 #  
3166                }                }
3167              }              }
3168    
3169              if ($self->{insertion_mode} eq 'in head noscript') {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
3170                ## As if </noscript>                ## As if </noscript>
3171                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3172                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});
# Line 2922  sub _tree_construction_main ($) { Line 3176  sub _tree_construction_main ($) {
3176                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3177    
3178                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3179              } elsif ($self->{insertion_mode} eq 'in head') {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
3180                ## As if </head>                ## As if </head>
3181                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
3182    
3183                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
3184              } elsif ($self->{insertion_mode} eq 'before head') {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
3185                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3186                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
3187                !!!next-token;                !!!next-token;
# Line 2937  sub _tree_construction_main ($) { Line 3191  sub _tree_construction_main ($) {
3191              ## "after head" insertion mode              ## "after head" insertion mode
3192              ## As if <body>              ## As if <body>
3193              !!!insert-element ('body');              !!!insert-element ('body');
3194              $self->{insertion_mode} = 'in body';              $self->{insertion_mode} = IN_BODY_IM;
3195              ## reprocess              ## reprocess
3196              redo B;              redo B;
3197            } else {            } else {
# Line 2945  sub _tree_construction_main ($) { Line 3199  sub _tree_construction_main ($) {
3199            }            }
3200    
3201            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
3202      } elsif ($self->{insertion_mode} eq 'in body' or      } elsif ($self->{insertion_mode} & BODY_IMS) {
3203               $self->{insertion_mode} eq 'in cell' or            if ($token->{type} == CHARACTER_TOKEN) {
              $self->{insertion_mode} eq 'in caption') {  
           if ($token->{type} eq 'character') {  
3204              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
3205              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
3206                            
# Line 2956  sub _tree_construction_main ($) { Line 3208  sub _tree_construction_main ($) {
3208    
3209              !!!next-token;              !!!next-token;
3210              redo B;              redo B;
3211            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
3212              if ({              if ({
3213                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
3214                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
3215                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3216                if ($self->{insertion_mode} eq 'in cell') {                if ($self->{insertion_mode} == IN_CELL_IM) {
3217                  ## have an element in table scope                  ## have an element in table scope
3218                  my $tn;                  my $tn;
3219                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 2984  sub _tree_construction_main ($) { Line 3236  sub _tree_construction_main ($) {
3236                                    
3237                  ## Close the cell                  ## Close the cell
3238                  !!!back-token; # <?>                  !!!back-token; # <?>
3239                  $token = {type => 'end tag', tag_name => $tn};                  $token = {type => END_TAG_TOKEN, tag_name => $tn};
3240                  redo B;                  redo B;
3241                } elsif ($self->{insertion_mode} eq 'in caption') {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3242                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption');
3243                                    
3244                  ## As if </caption>                  ## As if </caption>
# Line 3017  sub _tree_construction_main ($) { Line 3269  sub _tree_construction_main ($) {
3269                       tbody => 1, tfoot=> 1, thead => 1,                       tbody => 1, tfoot=> 1, thead => 1,
3270                      }->{$self->{open_elements}->[-1]->[1]}) {                      }->{$self->{open_elements}->[-1]->[1]}) {
3271                    !!!back-token; # <?>                    !!!back-token; # <?>
3272                    $token = {type => 'end tag', tag_name => 'caption'};                    $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3273                    !!!back-token;                    !!!back-token;
3274                    $token = {type => 'end tag',                    $token = {type => END_TAG_TOKEN,
3275                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3276                    redo B;                    redo B;
3277                  }                  }
# Line 3032  sub _tree_construction_main ($) { Line 3284  sub _tree_construction_main ($) {
3284                                    
3285                  $clear_up_to_marker->();                  $clear_up_to_marker->();
3286                                    
3287                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3288                                    
3289                  ## reprocess                  ## reprocess
3290                  redo B;                  redo B;
# Line 3042  sub _tree_construction_main ($) { Line 3294  sub _tree_construction_main ($) {
3294              } else {              } else {
3295                #                #
3296              }              }
3297            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
3298              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
3299                if ($self->{insertion_mode} eq 'in cell') {                if ($self->{insertion_mode} == IN_CELL_IM) {
3300                  ## have an element in table scope                  ## have an element in table scope
3301                  my $i;                  my $i;
3302                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3074  sub _tree_construction_main ($) { Line 3326  sub _tree_construction_main ($) {
3326                       tbody => 1, tfoot=> 1, thead => 1,                       tbody => 1, tfoot=> 1, thead => 1,
3327                      }->{$self->{open_elements}->[-1]->[1]}) {                      }->{$self->{open_elements}->[-1]->[1]}) {
3328                    !!!back-token;                    !!!back-token;
3329                    $token = {type => 'end tag',                    $token = {type => END_TAG_TOKEN,
3330                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3331                    redo B;                    redo B;
3332                  }                  }
# Line 3087  sub _tree_construction_main ($) { Line 3339  sub _tree_construction_main ($) {
3339                                    
3340                  $clear_up_to_marker->();                  $clear_up_to_marker->();
3341                                    
3342                  $self->{insertion_mode} = 'in row';                  $self->{insertion_mode} = IN_ROW_IM;
3343                                    
3344                  !!!next-token;                  !!!next-token;
3345                  redo B;                  redo B;
3346                } elsif ($self->{insertion_mode} eq 'in caption') {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
3347                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3348                  ## Ignore the token                  ## Ignore the token
3349                  !!!next-token;                  !!!next-token;
# Line 3100  sub _tree_construction_main ($) { Line 3352  sub _tree_construction_main ($) {
3352                  #                  #
3353                }                }
3354              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
3355                if ($self->{insertion_mode} eq 'in caption') {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
3356                  ## have a table element in table scope                  ## have a table element in table scope
3357                  my $i;                  my $i;
3358                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3128  sub _tree_construction_main ($) { Line 3380  sub _tree_construction_main ($) {
3380                       tbody => 1, tfoot=> 1, thead => 1,                       tbody => 1, tfoot=> 1, thead => 1,
3381                      }->{$self->{open_elements}->[-1]->[1]}) {                      }->{$self->{open_elements}->[-1]->[1]}) {
3382                    !!!back-token;                    !!!back-token;
3383                    $token = {type => 'end tag',                    $token = {type => END_TAG_TOKEN,
3384                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                              tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3385                    redo B;                    redo B;
3386                  }                  }
# Line 3141  sub _tree_construction_main ($) { Line 3393  sub _tree_construction_main ($) {
3393                                    
3394                  $clear_up_to_marker->();                  $clear_up_to_marker->();
3395                                    
3396                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3397                                    
3398                  !!!next-token;                  !!!next-token;
3399                  redo B;                  redo B;
3400                } elsif ($self->{insertion_mode} eq 'in cell') {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
3401                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3402                  ## Ignore the token                  ## Ignore the token
3403                  !!!next-token;                  !!!next-token;
# Line 3157  sub _tree_construction_main ($) { Line 3409  sub _tree_construction_main ($) {
3409                        table => 1, tbody => 1, tfoot => 1,                        table => 1, tbody => 1, tfoot => 1,
3410                        thead => 1, tr => 1,                        thead => 1, tr => 1,
3411                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
3412                       $self->{insertion_mode} eq 'in cell') {                       $self->{insertion_mode} == IN_CELL_IM) {
3413                ## have an element in table scope                ## have an element in table scope
3414                my $i;                my $i;
3415                my $tn;                my $tn;
# Line 3185  sub _tree_construction_main ($) { Line 3437  sub _tree_construction_main ($) {
3437    
3438                ## Close the cell                ## Close the cell
3439                !!!back-token; # </?>                !!!back-token; # </?>
3440                $token = {type => 'end tag', tag_name => $tn};                $token = {type => END_TAG_TOKEN, tag_name => $tn};
3441                redo B;                redo B;
3442              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
3443                       $self->{insertion_mode} eq 'in caption') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
3444                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption');
3445    
3446                ## As if </caption>                ## As if </caption>
# Line 3219  sub _tree_construction_main ($) { Line 3471  sub _tree_construction_main ($) {
3471                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
3472                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3473                  !!!back-token; # </table>                  !!!back-token; # </table>
3474                  $token = {type => 'end tag', tag_name => 'caption'};                  $token = {type => END_TAG_TOKEN, tag_name => 'caption'};
3475                  !!!back-token;                  !!!back-token;
3476                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3477                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3478                  redo B;                  redo B;
3479                }                }
# Line 3234  sub _tree_construction_main ($) { Line 3486  sub _tree_construction_main ($) {
3486    
3487                $clear_up_to_marker->();                $clear_up_to_marker->();
3488    
3489                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
3490    
3491                ## reprocess                ## reprocess
3492                redo B;                redo B;
3493              } elsif ({              } elsif ({
3494                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
3495                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3496                if ($self->{insertion_mode} eq 'in cell' or                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
                   $self->{insertion_mode} eq 'in caption') {  
3497                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3498                  ## Ignore the token                  ## Ignore the token
3499                  !!!next-token;                  !!!next-token;
# Line 3254  sub _tree_construction_main ($) { Line 3505  sub _tree_construction_main ($) {
3505                        tbody => 1, tfoot => 1,                        tbody => 1, tfoot => 1,
3506                        thead => 1, tr => 1,                        thead => 1, tr => 1,
3507                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
3508                       $self->{insertion_mode} eq 'in caption') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
3509                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
3510                ## Ignore the token                ## Ignore the token
3511                !!!next-token;                !!!next-token;
# Line 3268  sub _tree_construction_main ($) { Line 3519  sub _tree_construction_main ($) {
3519    
3520        $insert = $insert_to_current;        $insert = $insert_to_current;
3521        #        #
3522      } elsif ($self->{insertion_mode} eq 'in row' or      } elsif ($self->{insertion_mode} & TABLE_IMS) {
3523               $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.  
3524              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
3525                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
3526                                
# Line 3329  sub _tree_construction_main ($) { Line 3577  sub _tree_construction_main ($) {
3577                            
3578              !!!next-token;              !!!next-token;
3579              redo B;              redo B;
3580            } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
3581              if ({              if ({
3582                   tr => ($self->{insertion_mode} ne 'in row'),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
3583                   th => 1, td => 1,                   th => 1, td => 1,
3584                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
3585                if ($self->{insertion_mode} eq 'in table') {                if ($self->{insertion_mode} == IN_TABLE_IM) {
3586                  ## Clear back to table context                  ## Clear back to table context
3587                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while ($self->{open_elements}->[-1]->[1] ne 'table' and
3588                         $self->{open_elements}->[-1]->[1] ne 'html') {                         $self->{open_elements}->[-1]->[1] ne 'html') {
# Line 3343  sub _tree_construction_main ($) { Line 3591  sub _tree_construction_main ($) {
3591                  }                  }
3592                                    
3593                  !!!insert-element ('tbody');                  !!!insert-element ('tbody');
3594                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3595                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
3596                }                }
3597    
3598                if ($self->{insertion_mode} eq 'in table body') {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3599                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
3600                    !!!parse-error (type => 'missing start tag:tr');                    !!!parse-error (type => 'missing start tag:tr');
3601                  }                  }
# Line 3360  sub _tree_construction_main ($) { Line 3608  sub _tree_construction_main ($) {
3608                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
3609                  }                  }
3610                                    
3611                  $self->{insertion_mode} = 'in row';                  $self->{insertion_mode} = IN_ROW_IM;
3612                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
3613                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes});
3614                    !!!next-token;                    !!!next-token;
# Line 3380  sub _tree_construction_main ($) { Line 3628  sub _tree_construction_main ($) {
3628                }                }
3629                                
3630                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
3631                $self->{insertion_mode} = 'in cell';                $self->{insertion_mode} = IN_CELL_IM;
3632    
3633                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
3634                                
# Line 3389  sub _tree_construction_main ($) { Line 3637  sub _tree_construction_main ($) {
3637              } elsif ({              } elsif ({
3638                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
3639                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3640                        tr => 1, # $self->{insertion_mode} eq 'in row'                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
3641                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
3642                if ($self->{insertion_mode} eq 'in row') {                if ($self->{insertion_mode} == IN_ROW_IM) {
3643                  ## As if </tr>                  ## As if </tr>
3644                  ## have an element in table scope                  ## have an element in table scope
3645                  my $i;                  my $i;
# Line 3422  sub _tree_construction_main ($) { Line 3670  sub _tree_construction_main ($) {
3670                  }                  }
3671                                    
3672                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
3673                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3674                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
3675                    ## reprocess                    ## reprocess
3676                    redo B;                    redo B;
# Line 3431  sub _tree_construction_main ($) { Line 3679  sub _tree_construction_main ($) {
3679                  }                  }
3680                }                }
3681    
3682                if ($self->{insertion_mode} eq 'in table body') {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3683                  ## have an element in table scope                  ## have an element in table scope
3684                  my $i;                  my $i;
3685                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3470  sub _tree_construction_main ($) { Line 3718  sub _tree_construction_main ($) {
3718                  ## nop by definition                  ## nop by definition
3719                                    
3720                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3721                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3722                  ## reprocess in "in table" insertion mode...                  ## reprocess in "in table" insertion mode...
3723                }                }
3724    
# Line 3483  sub _tree_construction_main ($) { Line 3731  sub _tree_construction_main ($) {
3731                  }                  }
3732                                    
3733                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup');
3734                  $self->{insertion_mode} = 'in column group';                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
3735                  ## reprocess                  ## reprocess
3736                  redo B;                  redo B;
3737                } elsif ({                } elsif ({
# Line 3503  sub _tree_construction_main ($) { Line 3751  sub _tree_construction_main ($) {
3751                                    
3752                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes});
3753                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
3754                                             caption => 'in caption',                                             caption => IN_CAPTION_IM,
3755                                             colgroup => 'in column group',                                             colgroup => IN_COLUMN_GROUP_IM,
3756                                             tbody => 'in table body',                                             tbody => IN_TABLE_BODY_IM,
3757                                             tfoot => 'in table body',                                             tfoot => IN_TABLE_BODY_IM,
3758                                             thead => 'in table body',                                             thead => IN_TABLE_BODY_IM,
3759                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
3760                  !!!next-token;                  !!!next-token;
3761                  redo B;                  redo B;
# Line 3515  sub _tree_construction_main ($) { Line 3763  sub _tree_construction_main ($) {
3763                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
3764                }                }
3765              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
               ## NOTE: There are code clones for this "table in table"  
3766                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
3767    
3768                ## As if </table>                ## As if </table>
# Line 3546  sub _tree_construction_main ($) { Line 3793  sub _tree_construction_main ($) {
3793                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
3794                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3795                  !!!back-token; # <table>                  !!!back-token; # <table>
3796                  $token = {type => 'end tag', tag_name => 'table'};                  $token = {type => END_TAG_TOKEN, tag_name => 'table'};
3797                  !!!back-token;                  !!!back-token;
3798                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3799                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3800                  redo B;                  redo B;
3801                }                }
# Line 3563  sub _tree_construction_main ($) { Line 3810  sub _tree_construction_main ($) {
3810    
3811                ## reprocess                ## reprocess
3812                redo B;                redo B;
3813              } else {          } else {
3814                #            !!!parse-error (type => 'in table:'.$token->{tag_name});
3815              }  
3816            } elsif ($token->{type} eq 'end tag') {            $insert = $insert_to_foster;
3817              #
3818            }
3819          } elsif ($token->{type} == END_TAG_TOKEN) {
3820              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
3821                  $self->{insertion_mode} eq 'in row') {                  $self->{insertion_mode} == IN_ROW_IM) {
3822                ## have an element in table scope                ## have an element in table scope
3823                my $i;                my $i;
3824                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3598  sub _tree_construction_main ($) { Line 3848  sub _tree_construction_main ($) {
3848                }                }
3849    
3850                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
3851                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
3852                !!!next-token;                !!!next-token;
3853                redo B;                redo B;
3854              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
3855                if ($self->{insertion_mode} eq 'in row') {                if ($self->{insertion_mode} == IN_ROW_IM) {
3856                  ## As if </tr>                  ## As if </tr>
3857                  ## have an element in table scope                  ## have an element in table scope
3858                  my $i;                  my $i;
# Line 3633  sub _tree_construction_main ($) { Line 3883  sub _tree_construction_main ($) {
3883                  }                  }
3884                                    
3885                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
3886                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
3887                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
3888                }                }
3889    
3890                if ($self->{insertion_mode} eq 'in table body') {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
3891                  ## have an element in table scope                  ## have an element in table scope
3892                  my $i;                  my $i;
3893                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3676  sub _tree_construction_main ($) { Line 3926  sub _tree_construction_main ($) {
3926                  ## nop by definition                  ## nop by definition
3927                                    
3928                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
3929                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
3930                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
3931                }                }
3932    
# Line 3707  sub _tree_construction_main ($) { Line 3957  sub _tree_construction_main ($) {
3957                     tbody => 1, tfoot=> 1, thead => 1,                     tbody => 1, tfoot=> 1, thead => 1,
3958                    }->{$self->{open_elements}->[-1]->[1]}) {                    }->{$self->{open_elements}->[-1]->[1]}) {
3959                  !!!back-token;                  !!!back-token;
3960                  $token = {type => 'end tag',                  $token = {type => END_TAG_TOKEN,
3961                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
3962                  redo B;                  redo B;
3963                }                }
# Line 3725  sub _tree_construction_main ($) { Line 3975  sub _tree_construction_main ($) {
3975              } elsif ({              } elsif ({
3976                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
3977                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
3978                       ($self->{insertion_mode} eq 'in row' or                       $self->{insertion_mode} & ROW_IMS) {
3979                        $self->{insertion_mode} eq 'in table body')) {                if ($self->{insertion_mode} == IN_ROW_IM) {
               if ($self->{insertion_mode} eq 'in row') {  
3980                  ## have an element in table scope                  ## have an element in table scope
3981                  my $i;                  my $i;
3982                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
# Line 3778  sub _tree_construction_main ($) { Line 4027  sub _tree_construction_main ($) {
4027                  }                  }
4028                                    
4029                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
4030                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
4031                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
4032                }                }
4033    
# Line 3811  sub _tree_construction_main ($) { Line 4060  sub _tree_construction_main ($) {
4060                }                }
4061    
4062                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4063                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
4064                !!!next-token;                !!!next-token;
4065                redo B;                redo B;
4066              } elsif ({              } elsif ({
4067                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
4068                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
4069                        tr => 1, # $self->{insertion_mode} eq 'in row'                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
4070                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} eq 'in table'                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
4071                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4072                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
4073                ## Ignore the token                ## Ignore the token
4074                !!!next-token;                !!!next-token;
4075                redo B;                redo B;
4076              } else {          } else {
4077                #            !!!parse-error (type => 'in table:/'.$token->{tag_name});
             }  
           } else {  
             die "$0: $token->{type}: Unknown token type";  
           }  
   
       !!!parse-error (type => 'in table:'.$token->{tag_name});  
4078    
4079        $insert = $insert_to_foster;            $insert = $insert_to_foster;
4080        #            #
4081      } elsif ($self->{insertion_mode} eq 'in column group') {          }
4082            if ($token->{type} eq 'character') {        } else {
4083            die "$0: $token->{type}: Unknown token type";
4084          }
4085        } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
4086              if ($token->{type} == CHARACTER_TOKEN) {
4087              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4088                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4089                unless (length $token->{data}) {                unless (length $token->{data}) {
# Line 3846  sub _tree_construction_main ($) { Line 4093  sub _tree_construction_main ($) {
4093              }              }
4094                            
4095              #              #
4096            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
4097              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
4098                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes});
4099                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
# Line 3855  sub _tree_construction_main ($) { Line 4102  sub _tree_construction_main ($) {
4102              } else {              } else {
4103                #                #
4104              }              }
4105            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
4106              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
4107                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] eq 'html') {
4108                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag:colgroup');
# Line 3864  sub _tree_construction_main ($) { Line 4111  sub _tree_construction_main ($) {
4111                  redo B;                  redo B;
4112                } else {                } else {
4113                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
4114                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
4115                  !!!next-token;                  !!!next-token;
4116                  redo B;                              redo B;            
4117                }                }
# Line 3888  sub _tree_construction_main ($) { Line 4135  sub _tree_construction_main ($) {
4135              redo B;              redo B;
4136            } else {            } else {
4137              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
4138              $self->{insertion_mode} = 'in table';              $self->{insertion_mode} = IN_TABLE_IM;
4139              ## reprocess              ## reprocess
4140              redo B;              redo B;
4141            }            }
4142      } elsif ($self->{insertion_mode} eq 'in select') {      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {
4143            if ($token->{type} eq 'character') {        if ($token->{type} == CHARACTER_TOKEN) {
4144              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4145              !!!next-token;          !!!next-token;
4146              redo B;          redo B;
4147            } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4148              if ($token->{tag_name} eq 'option') {              if ($token->{tag_name} eq 'option') {
4149                if ($self->{open_elements}->[-1]->[1] eq 'option') {                if ($self->{open_elements}->[-1]->[1] eq 'option') {
4150                  ## As if </option>                  ## As if </option>
# Line 3950  sub _tree_construction_main ($) { Line 4197  sub _tree_construction_main ($) {
4197    
4198                !!!next-token;                !!!next-token;
4199                redo B;                redo B;
4200              } else {          } else {
4201                #            !!!parse-error (type => 'in select:'.$token->{tag_name});
4202              }            ## Ignore the token
4203            } elsif ($token->{type} eq 'end tag') {            !!!next-token;
4204              redo B;
4205            }
4206          } elsif ($token->{type} == END_TAG_TOKEN) {
4207              if ($token->{tag_name} eq 'optgroup') {              if ($token->{tag_name} eq 'optgroup') {
4208                if ($self->{open_elements}->[-1]->[1] eq 'option' and                if ($self->{open_elements}->[-1]->[1] eq 'option' and
4209                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {
# Line 4055  sub _tree_construction_main ($) { Line 4305  sub _tree_construction_main ($) {
4305    
4306                ## reprocess                ## reprocess
4307                redo B;                redo B;
4308              } else {          } else {
4309                #            !!!parse-error (type => 'in select:/'.$token->{tag_name});
             }  
           } else {  
             #  
           }  
   
           !!!parse-error (type => 'in select:'.$token->{tag_name});  
4310            ## Ignore the token            ## Ignore the token
4311            !!!next-token;            !!!next-token;
4312            redo B;            redo B;
4313      } elsif ($self->{insertion_mode} eq 'after body' or          }
4314               $self->{insertion_mode} eq 'after html body') {        } else {
4315        if ($token->{type} eq 'character') {          die "$0: $token->{type}: Unknown token type";
4316          }
4317        } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
4318          if ($token->{type} == CHARACTER_TOKEN) {
4319          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4320            my $data = $1;            my $data = $1;
4321            ## As if in body            ## As if in body
# Line 4082  sub _tree_construction_main ($) { Line 4329  sub _tree_construction_main ($) {
4329            }            }
4330          }          }
4331                    
4332          if ($self->{insertion_mode} eq 'after html body') {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4333            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#character');
4334    
4335            ## 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 4338  sub _tree_construction_main ($) {
4338          ## "after body" insertion mode          ## "after body" insertion mode
4339          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#character');
4340    
4341          $self->{insertion_mode} = 'in body';          $self->{insertion_mode} = IN_BODY_IM;
4342          ## reprocess          ## reprocess
4343          redo B;          redo B;
4344        } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4345          if ($self->{insertion_mode} eq 'after html body') {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4346            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name});
4347                        
4348            ## 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 4351  sub _tree_construction_main ($) {
4351          ## "after body" insertion mode          ## "after body" insertion mode
4352          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body:'.$token->{tag_name});
4353    
4354          $self->{insertion_mode} = 'in body';          $self->{insertion_mode} = IN_BODY_IM;
4355          ## reprocess          ## reprocess
4356          redo B;          redo B;
4357        } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} == END_TAG_TOKEN) {
4358          if ($self->{insertion_mode} eq 'after html body') {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4359            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name});
4360                        
4361            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = AFTER_BODY_IM;
4362            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "main" phase, "after body" insertion mode...
4363          }          }
4364    
# Line 4123  sub _tree_construction_main ($) { Line 4370  sub _tree_construction_main ($) {
4370              !!!next-token;              !!!next-token;
4371              redo B;              redo B;
4372            } else {            } else {
4373              $self->{insertion_mode} = 'after html body';              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
4374              !!!next-token;              !!!next-token;
4375              redo B;              redo B;
4376            }            }
4377          } else {          } else {
4378            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!parse-error (type => 'after body:/'.$token->{tag_name});
4379    
4380            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
4381            ## reprocess            ## reprocess
4382            redo B;            redo B;
4383          }          }
4384        } else {        } else {
4385          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4386        }        }
4387      } elsif ($self->{insertion_mode} eq 'in frameset' or      } elsif ($self->{insertion_mode} & FRAME_IMS) {
4388               $self->{insertion_mode} eq 'after frameset' or        if ($token->{type} == CHARACTER_TOKEN) {
              $self->{insertion_mode} eq 'after html frameset') {  
       if ($token->{type} eq 'character') {  
4389          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4390            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4391                        
# Line 4151  sub _tree_construction_main ($) { Line 4396  sub _tree_construction_main ($) {
4396          }          }
4397                    
4398          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
4399            if ($self->{insertion_mode} eq 'in frameset') {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4400              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#character');
4401            } elsif ($self->{insertion_mode} eq 'after frameset') {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
4402              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character');
4403            } else { # "after html frameset"            } else { # "after html frameset"
4404              !!!parse-error (type => 'after html:#character');              !!!parse-error (type => 'after html:#character');
4405    
4406              $self->{insertion_mode} = 'after frameset';              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4407              ## Reprocess in the "main" phase, "after frameset"...              ## Reprocess in the "main" phase, "after frameset"...
4408              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character');
4409            }            }
# Line 4173  sub _tree_construction_main ($) { Line 4418  sub _tree_construction_main ($) {
4418          }          }
4419                    
4420          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
4421        } elsif ($token->{type} eq 'start tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4422          if ($self->{insertion_mode} eq 'after html frameset') {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4423            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html:'.$token->{tag_name});
4424    
4425            $self->{insertion_mode} = 'after frameset';            $self->{insertion_mode} = AFTER_FRAMESET_IM;
4426            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "main" phase, "after frameset" insertion mode...
4427          }          }
4428    
4429          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
4430              $self->{insertion_mode} eq 'in frameset') {              $self->{insertion_mode} == IN_FRAMESET_IM) {
4431            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
4432            !!!next-token;            !!!next-token;
4433            redo B;            redo B;
4434          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
4435                   $self->{insertion_mode} eq 'in frameset') {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
4436            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes});
4437            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4438            !!!next-token;            !!!next-token;
# Line 4197  sub _tree_construction_main ($) { Line 4442  sub _tree_construction_main ($) {
4442            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);
4443            redo B;            redo B;
4444          } else {          } else {
4445            if ($self->{insertion_mode} eq 'in frameset') {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4446              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset:'.$token->{tag_name});
4447            } else {            } else {
4448              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset:'.$token->{tag_name});
# Line 4206  sub _tree_construction_main ($) { Line 4451  sub _tree_construction_main ($) {
4451            !!!next-token;            !!!next-token;
4452            redo B;            redo B;
4453          }          }
4454        } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} == END_TAG_TOKEN) {
4455          if ($self->{insertion_mode} eq 'after html frameset') {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4456            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/'.$token->{tag_name});
4457    
4458            $self->{insertion_mode} = 'after frameset';            $self->{insertion_mode} = AFTER_FRAMESET_IM;
4459            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "main" phase, "after frameset" insertion mode...
4460          }          }
4461    
4462          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
4463              $self->{insertion_mode} eq 'in frameset') {              $self->{insertion_mode} == IN_FRAMESET_IM) {
4464            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] eq 'html' and
4465                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
4466              !!!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 4473  sub _tree_construction_main ($) {
4473    
4474            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
4475                $self->{open_elements}->[-1]->[1] ne 'frameset') {                $self->{open_elements}->[-1]->[1] ne 'frameset') {
4476              $self->{insertion_mode} = 'after frameset';              $self->{insertion_mode} = AFTER_FRAMESET_IM;
4477            }            }
4478            redo B;            redo B;
4479          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
4480                   $self->{insertion_mode} eq 'after frameset') {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
4481            $self->{insertion_mode} = 'after html frameset';            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
4482            !!!next-token;            !!!next-token;
4483            redo B;            redo B;
4484          } else {          } else {
4485            if ($self->{insertion_mode} eq 'in frameset') {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
4486              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});
4487            } else {            } else {
4488              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});
# Line 4256  sub _tree_construction_main ($) { Line 4501  sub _tree_construction_main ($) {
4501      }      }
4502    
4503      ## "in body" insertion mode      ## "in body" insertion mode
4504      if ($token->{type} eq 'start tag') {      if ($token->{type} == START_TAG_TOKEN) {
4505        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
4506          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
4507          $script_start_tag->($insert);          $script_start_tag->($insert);
# Line 4276  sub _tree_construction_main ($) { Line 4521  sub _tree_construction_main ($) {
4521        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
4522          ## 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
4523          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4524          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.
4525    
4526          unless ($self->{confident}) {          unless ($self->{confident}) {
           my $charset;  
4527            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) { ## TODO: And if supported
4528              $charset = $token->{attributes}->{charset}->{value};              $self->{change_encoding}
4529            }                  ->($self, $token->{attributes}->{charset}->{value});
4530            if ($token->{attributes}->{'http-equiv'}) {              
4531                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4532                    ->set_user_data (manakai_has_reference =>
4533                                         $token->{attributes}->{charset}
4534                                             ->{has_reference});
4535              } elsif ($token->{attributes}->{content}) {
4536              ## 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.
4537              if ($token->{attributes}->{'http-equiv'}->{value}              if ($token->{attributes}->{content}->{value}
4538                  =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                  =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=
4539                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4540                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4541                $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                $self->{change_encoding}
4542              } ## TODO: And if supported                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);
4543                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4544                      ->set_user_data (manakai_has_reference =>
4545                                           $token->{attributes}->{content}
4546                                                 ->{has_reference});
4547                }
4548              }
4549            } else {
4550              if ($token->{attributes}->{charset}) {
4551                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4552                    ->set_user_data (manakai_has_reference =>
4553                                         $token->{attributes}->{charset}
4554                                             ->{has_reference});
4555              }
4556              if ($token->{attributes}->{content}) {
4557                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4558                    ->set_user_data (manakai_has_reference =>
4559                                         $token->{attributes}->{content}
4560                                             ->{has_reference});
4561            }            }
           ## TODO: Change the encoding  
4562          }          }
4563    
4564          !!!next-token;          !!!next-token;
# Line 4336  sub _tree_construction_main ($) { Line 4602  sub _tree_construction_main ($) {
4602          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4603            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4604              !!!back-token;              !!!back-token;
4605              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4606              redo B;              redo B;
4607            } elsif ({            } elsif ({
4608                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
# Line 4349  sub _tree_construction_main ($) { Line 4615  sub _tree_construction_main ($) {
4615          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4616          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre') {
4617            !!!next-token;            !!!next-token;
4618            if ($token->{type} eq 'character') {            if ($token->{type} == CHARACTER_TOKEN) {
4619              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
4620              unless (length $token->{data}) {              unless (length $token->{data}) {
4621                !!!next-token;                !!!next-token;
# Line 4370  sub _tree_construction_main ($) { Line 4636  sub _tree_construction_main ($) {
4636            INSCOPE: for (reverse @{$self->{open_elements}}) {            INSCOPE: for (reverse @{$self->{open_elements}}) {
4637              if ($_->[1] eq 'p') {              if ($_->[1] eq 'p') {
4638                !!!back-token;                !!!back-token;
4639                $token = {type => 'end tag', tag_name => 'p'};                $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4640                redo B;                redo B;
4641              } elsif ({              } elsif ({
4642                        table => 1, caption => 1, td => 1, th => 1,                        table => 1, caption => 1, td => 1, th => 1,
# Line 4390  sub _tree_construction_main ($) { Line 4656  sub _tree_construction_main ($) {
4656          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4657            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4658              !!!back-token;              !!!back-token;
4659              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4660              redo B;              redo B;
4661            } elsif ({            } elsif ({
4662                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
# Line 4437  sub _tree_construction_main ($) { Line 4703  sub _tree_construction_main ($) {
4703          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4704            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4705              !!!back-token;              !!!back-token;
4706              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4707              redo B;              redo B;
4708            } elsif ({            } elsif ({
4709                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
# Line 4484  sub _tree_construction_main ($) { Line 4750  sub _tree_construction_main ($) {
4750          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4751            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4752              !!!back-token;              !!!back-token;
4753              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4754              redo B;              redo B;
4755            } elsif ({            } elsif ({
4756                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
# Line 4508  sub _tree_construction_main ($) { Line 4774  sub _tree_construction_main ($) {
4774            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
4775            if ($node->[1] eq 'p') {            if ($node->[1] eq 'p') {
4776              !!!back-token;              !!!back-token;
4777              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4778              redo B;              redo B;
4779            } elsif ({            } elsif ({
4780                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
# Line 4552  sub _tree_construction_main ($) { Line 4818  sub _tree_construction_main ($) {
4818              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a');
4819                            
4820              !!!back-token;              !!!back-token;
4821              $token = {type => 'end tag', tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a'};
4822              $formatting_end_tag->($token->{tag_name});              $formatting_end_tag->($token->{tag_name});
4823                            
4824              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
# Line 4599  sub _tree_construction_main ($) { Line 4865  sub _tree_construction_main ($) {
4865          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4866            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
4867            if ($node->[1] eq 'nobr') {            if ($node->[1] eq 'nobr') {
4868              !!!parse-error (type => 'not closed:nobr');              !!!parse-error (type => 'in nobr:nobr');
4869              !!!back-token;              !!!back-token;
4870              $token = {type => 'end tag', tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};
4871              redo B;              redo B;
4872            } elsif ({            } elsif ({
4873                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
# Line 4623  sub _tree_construction_main ($) { Line 4889  sub _tree_construction_main ($) {
4889            if ($node->[1] eq 'button') {            if ($node->[1] eq 'button') {
4890              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button');
4891              !!!back-token;              !!!back-token;
4892              $token = {type => 'end tag', tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button'};
4893              redo B;              redo B;
4894            } elsif ({            } elsif ({
4895                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
# Line 4658  sub _tree_construction_main ($) { Line 4924  sub _tree_construction_main ($) {
4924          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4925            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4926              !!!back-token;              !!!back-token;
4927              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4928              redo B;              redo B;
4929            } elsif ({            } elsif ({
4930                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
# Line 4670  sub _tree_construction_main ($) { Line 4936  sub _tree_construction_main ($) {
4936                        
4937          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
4938                        
4939          $self->{insertion_mode} = 'in table';          $self->{insertion_mode} = IN_TABLE_IM;
4940                        
4941          !!!next-token;          !!!next-token;
4942          redo B;          redo B;
# Line 4697  sub _tree_construction_main ($) { Line 4963  sub _tree_construction_main ($) {
4963          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
4964            if ($_->[1] eq 'p') {            if ($_->[1] eq 'p') {
4965              !!!back-token;              !!!back-token;
4966              $token = {type => 'end tag', tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p'};
4967              redo B;              redo B;
4968            } elsif ({            } elsif ({
4969                      table => 1, caption => 1, td => 1, th => 1,                      table => 1, caption => 1, td => 1, th => 1,
# Line 4737  sub _tree_construction_main ($) { Line 5003  sub _tree_construction_main ($) {
5003            delete $at->{action};            delete $at->{action};
5004            delete $at->{prompt};            delete $at->{prompt};
5005            my @tokens = (            my @tokens = (
5006                          {type => 'start tag', tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
5007                           attributes => $form_attrs},                           attributes => $form_attrs},
5008                          {type => 'start tag', tag_name => 'hr'},                          {type => START_TAG_TOKEN, tag_name => 'hr'},
5009                          {type => 'start tag', tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'p'},
5010                          {type => 'start tag', tag_name => 'label'},                          {type => START_TAG_TOKEN, tag_name => 'label'},
5011                         );                         );
5012            if ($prompt_attr) {            if ($prompt_attr) {
5013              push @tokens, {type => 'character', data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};
5014            } else {            } else {
5015              push @tokens, {type => 'character',              push @tokens, {type => CHARACTER_TOKEN,
5016                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD
5017              ## TODO: make this configurable              ## TODO: make this configurable
5018            }            }
5019            push @tokens,            push @tokens,
5020                          {type => 'start tag', tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},
5021                          #{type => 'character', data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
5022                          {type => 'end tag', tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label'},
5023                          {type => 'end tag', tag_name => 'p'},                          {type => END_TAG_TOKEN, tag_name => 'p'},
5024                          {type => 'start tag', tag_name => 'hr'},                          {type => START_TAG_TOKEN, tag_name => 'hr'},
5025                          {type => 'end tag', tag_name => 'form'};                          {type => END_TAG_TOKEN, tag_name => 'form'};
5026            $token = shift @tokens;            $token = shift @tokens;
5027            !!!back-token (@tokens);            !!!back-token (@tokens);
5028            redo B;            redo B;
# Line 4774  sub _tree_construction_main ($) { Line 5040  sub _tree_construction_main ($) {
5040                    
5041          my $text = '';          my $text = '';
5042          !!!next-token;          !!!next-token;
5043          if ($token->{type} eq 'character') {          if ($token->{type} == CHARACTER_TOKEN) {
5044            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
5045            unless (length $token->{data}) {            unless (length $token->{data}) {
5046              !!!next-token;              !!!next-token;
5047            }            }
5048          }          }
5049          while ($token->{type} eq 'character') {          while ($token->{type} == CHARACTER_TOKEN) {
5050            $text .= $token->{data};            $text .= $token->{data};
5051            !!!next-token;            !!!next-token;
5052          }          }
# Line 4790  sub _tree_construction_main ($) { Line 5056  sub _tree_construction_main ($) {
5056                    
5057          $self->{content_model} = PCDATA_CONTENT_MODEL;          $self->{content_model} = PCDATA_CONTENT_MODEL;
5058                    
5059          if ($token->{type} eq 'end tag' and          if ($token->{type} == END_TAG_TOKEN and
5060              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
5061            ## Ignore the token            ## Ignore the token
5062          } else {          } else {
# Line 4804  sub _tree_construction_main ($) { Line 5070  sub _tree_construction_main ($) {
5070                  noframes => 1,                  noframes => 1,
5071                  noscript => 0, ## TODO: 1 if scripting is enabled                  noscript => 0, ## TODO: 1 if scripting is enabled
5072                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
5073          ## NOTE: There are two "as if in body" code clones.          ## NOTE: There is an "as if in body" code clone.
5074          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);
5075          redo B;          redo B;
5076        } elsif ($token->{tag_name} eq 'select') {        } elsif ($token->{tag_name} eq 'select') {
# Line 4812  sub _tree_construction_main ($) { Line 5078  sub _tree_construction_main ($) {
5078                    
5079          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes});
5080                    
5081          $self->{insertion_mode} = 'in select';          $self->{insertion_mode} = IN_SELECT_IM;
5082          !!!next-token;          !!!next-token;
5083          redo B;          redo B;
5084        } elsif ({        } elsif ({
# Line 4835  sub _tree_construction_main ($) { Line 5101  sub _tree_construction_main ($) {
5101          !!!next-token;          !!!next-token;
5102          redo B;          redo B;
5103        }        }
5104      } elsif ($token->{type} eq 'end tag') {      } elsif ($token->{type} == END_TAG_TOKEN) {
5105        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
5106          if (@{$self->{open_elements}} > 1 and          if (@{$self->{open_elements}} > 1 and
5107              $self->{open_elements}->[1]->[1] eq 'body') {              $self->{open_elements}->[1]->[1] eq 'body') {
# Line 4849  sub _tree_construction_main ($) { Line 5115  sub _tree_construction_main ($) {
5115              }              }
5116            }            }
5117    
5118            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = AFTER_BODY_IM;
5119            !!!next-token;            !!!next-token;
5120            redo B;            redo B;
5121          } else {          } else {
# Line 4864  sub _tree_construction_main ($) { Line 5130  sub _tree_construction_main ($) {
5130            if ($self->{open_elements}->[-1]->[1] ne 'body') {            if ($self->{open_elements}->[-1]->[1] ne 'body') {
5131              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);
5132            }            }
5133            $self->{insertion_mode} = 'after body';            $self->{insertion_mode} = AFTER_BODY_IM;
5134            ## reprocess            ## reprocess
5135            redo B;            redo B;
5136          } else {          } else {
# Line 4896  sub _tree_construction_main ($) { Line 5162  sub _tree_construction_main ($) {
5162                   tbody => 1, tfoot=> 1, thead => 1,                   tbody => 1, tfoot=> 1, thead => 1,
5163                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5164                !!!back-token;                !!!back-token;
5165                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5166                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5167                redo B;                redo B;
5168              }              }
# Line 4944  sub _tree_construction_main ($) { Line 5210  sub _tree_construction_main ($) {
5210                   tbody => 1, tfoot=> 1, thead => 1,                   tbody => 1, tfoot=> 1, thead => 1,
5211                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5212                !!!back-token;                !!!back-token;
5213                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5214                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5215                redo B;                redo B;
5216              }              }
# Line 4960  sub _tree_construction_main ($) { Line 5226  sub _tree_construction_main ($) {
5226          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {
5227            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
5228          } else {          } else {
5229            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5230          }          }
5231    
5232          undef $self->{form_element};          undef $self->{form_element};
# Line 4983  sub _tree_construction_main ($) { Line 5249  sub _tree_construction_main ($) {
5249                   tbody => 1, tfoot=> 1, thead => 1,                   tbody => 1, tfoot=> 1, thead => 1,
5250                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5251                !!!back-token;                !!!back-token;
5252                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5253                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5254                redo B;                redo B;
5255              }              }
# Line 4998  sub _tree_construction_main ($) { Line 5264  sub _tree_construction_main ($) {
5264          } # INSCOPE          } # INSCOPE
5265                    
5266          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {
5267            !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});
5268          }          }
5269                    
5270          splice @{$self->{open_elements}}, $i if defined $i;          splice @{$self->{open_elements}}, $i if defined $i;
# Line 5057  sub _tree_construction_main ($) { Line 5323  sub _tree_construction_main ($) {
5323              if ({              if ({
5324                   dd => 1, dt => 1, li => 1, p => 1,                   dd => 1, dt => 1, li => 1, p => 1,
5325                   td => 1, th => 1, tr => 1,                   td => 1, th => 1, tr => 1,
5326                   tbody => 1, tfoot=> 1, thead => 1,                   tbody => 1, tfoot => 1, thead => 1,
5327                  }->{$self->{open_elements}->[-1]->[1]}) {                  }->{$self->{open_elements}->[-1]->[1]}) {
5328                !!!back-token;                !!!back-token;
5329                $token = {type => 'end tag',                $token = {type => END_TAG_TOKEN,
5330                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST
5331                redo B;                redo B;
5332              }              }
5333                    
5334              ## Step 2              ## Step 2
5335              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {
5336                  ## NOTE: <x><y></x>
5337                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);
5338              }              }
5339                            
# Line 5117  sub set_inner_html ($$$) { Line 5384  sub set_inner_html ($$$) {
5384    my $s = \$_[0];    my $s = \$_[0];
5385    my $onerror = $_[1];    my $onerror = $_[1];
5386    
5387      ## ISSUE: Should {confident} be true?
5388    
5389    my $nt = $node->node_type;    my $nt = $node->node_type;
5390    if ($nt == 9) {    if ($nt == 9) {
5391      # MUST      # MUST
# Line 5269  sub set_inner_html ($$$) { Line 5538  sub set_inner_html ($$$) {
5538    
5539  } # tree construction stage  } # tree construction stage
5540    
5541  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
5542    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  
5543    
5544  1;  1;
5545  # $Date$  # $Date$

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24